-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathmarketplace.ts
174 lines (158 loc) · 5.44 KB
/
marketplace.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import * as fs from 'fs';
import * as path from 'path';
import * as tarStream from 'tar-stream';
import * as util from 'util';
import { CancellationToken } from 'vs/base/common/cancellation';
import { mkdirp } from 'vs/base/node/pfs';
import * as vszip from 'vs/base/node/zip';
import * as nls from 'vs/nls';
import product from 'vs/platform/product/common/product';
// We will be overriding these, so keep a reference to the original.
const vszipExtract = vszip.extract;
const vszipBuffer = vszip.buffer;
export interface IExtractOptions {
overwrite?: boolean;
/**
* Source path within the TAR/ZIP archive. Only the files
* contained in this path will be extracted.
*/
sourcePath?: string;
}
export interface IFile {
path: string;
contents?: Buffer | string;
localPath?: string;
}
export const tar = async (tarPath: string, files: IFile[]): Promise<string> => {
const pack = tarStream.pack();
const chunks: Buffer[] = [];
const ended = new Promise<Buffer>((resolve) => {
pack.on('end', () => resolve(Buffer.concat(chunks)));
});
pack.on('data', (chunk: Buffer) => chunks.push(chunk));
for (let i = 0; i < files.length; i++) {
const file = files[i];
pack.entry({ name: file.path }, file.contents);
}
pack.finalize();
await util.promisify(fs.writeFile)(tarPath, await ended);
return tarPath;
};
export const extract = async (archivePath: string, extractPath: string, options: IExtractOptions = {}, token: CancellationToken): Promise<void> => {
try {
await extractTar(archivePath, extractPath, options, token);
} catch (error) {
if (error.toString().includes('Invalid tar header')) {
await vszipExtract(archivePath, extractPath, options, token);
}
}
};
export const buffer = (targetPath: string, filePath: string): Promise<Buffer> => {
return new Promise<Buffer>(async (resolve, reject) => {
try {
let done: boolean = false;
await extractAssets(targetPath, new RegExp(filePath), (assetPath: string, data: Buffer) => {
if (path.normalize(assetPath) === path.normalize(filePath)) {
done = true;
resolve(data);
}
});
if (!done) {
throw new Error('couldn\'t find asset ' + filePath);
}
} catch (error) {
if (error.toString().includes('Invalid tar header')) {
vszipBuffer(targetPath, filePath).then(resolve).catch(reject);
} else {
reject(error);
}
}
});
};
const extractAssets = async (tarPath: string, match: RegExp, callback: (path: string, data: Buffer) => void): Promise<void> => {
return new Promise<void>((resolve, reject): void => {
const extractor = tarStream.extract();
const fail = (error: Error) => {
extractor.destroy();
reject(error);
};
extractor.once('error', fail);
extractor.on('entry', async (header, stream, next) => {
const name = header.name;
if (match.test(name)) {
extractData(stream).then((data) => {
callback(name, data);
next();
}).catch(fail);
} else {
stream.on('end', () => next());
stream.resume(); // Just drain it.
}
});
extractor.on('finish', resolve);
fs.createReadStream(tarPath).pipe(extractor);
});
};
const extractData = (stream: NodeJS.ReadableStream): Promise<Buffer> => {
return new Promise((resolve, reject): void => {
const fileData: Buffer[] = [];
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(fileData)));
stream.on('data', (data) => fileData.push(data));
});
};
const extractTar = async (tarPath: string, targetPath: string, options: IExtractOptions = {}, token: CancellationToken): Promise<void> => {
return new Promise<void>((resolve, reject): void => {
const sourcePathRegex = new RegExp(options.sourcePath ? `^${options.sourcePath}` : '');
const extractor = tarStream.extract();
const fail = (error: Error) => {
extractor.destroy();
reject(error);
};
extractor.once('error', fail);
extractor.on('entry', async (header, stream, next) => {
const nextEntry = (): void => {
stream.on('end', () => next());
stream.resume();
};
const rawName = path.normalize(header.name);
if (token.isCancellationRequested || !sourcePathRegex.test(rawName)) {
return nextEntry();
}
const fileName = rawName.replace(sourcePathRegex, '');
const targetFileName = path.join(targetPath, fileName);
if (/\/$/.test(fileName)) {
return mkdirp(targetFileName).then(nextEntry);
}
const dirName = path.dirname(fileName);
const targetDirName = path.join(targetPath, dirName);
if (targetDirName.indexOf(targetPath) !== 0) {
return fail(new Error(nls.localize('invalid file', 'Error extracting {0}. Invalid file.', fileName)));
}
await mkdirp(targetDirName, undefined);
const fstream = fs.createWriteStream(targetFileName, { mode: header.mode });
fstream.once('close', () => next());
fstream.once('error', fail);
stream.pipe(fstream);
});
extractor.once('finish', resolve);
fs.createReadStream(tarPath).pipe(extractor);
});
};
/**
* Override original functionality so we can use a custom marketplace with
* either tars or zips.
*/
export const enableCustomMarketplace = (): void => {
(<any>product).extensionsGallery = { // Use `any` to override readonly.
serviceUrl: process.env.SERVICE_URL ? [process.env.SERVICE_URL] : ['https://open-vsx.org/vscode/gallery', 'https://extensions.coder.com/api'],
itemUrl: process.env.ITEM_URL || '',
controlUrl: '',
recommendationsUrl: '',
...(product.extensionsGallery || {}),
};
const target = vszip as typeof vszip;
target.zip = tar;
target.extract = extract;
target.buffer = buffer;
};