Skip to content

Fix install from VSIX for TAR and ZIP formats #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"xmlhttprequest": "1.8.0"
},
"jest": {
"globals": {
"ts-jest": {
"diagnostics": false
}
},
"moduleFileExtensions": [
"ts",
"tsx",
Expand All @@ -26,7 +31,9 @@
"@coder/ide/src/fill/evaluation": "<rootDir>/ide/src/fill/evaluation",
"@coder/ide/src/fill/client": "<rootDir>/ide/src/fill/client",
"@coder/(.*)/test": "<rootDir>/$1/test",
"@coder/(.*)": "<rootDir>/$1/src"
"@coder/(.*)": "<rootDir>/$1/src",
"vs/(.*)": "<rootDir>/../lib/vscode/src/vs/$1",
"vszip": "<rootDir>/../lib/vscode/src/vs/base/node/zip.ts"
},
"transform": {
"^.+\\.tsx?$": "ts-jest"
Expand All @@ -37,4 +44,4 @@
],
"testRegex": ".*\\.test\\.tsx?"
}
}
}
3 changes: 2 additions & 1 deletion packages/vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin
bin
test/.test*
155 changes: 88 additions & 67 deletions packages/vscode/src/fill/zip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*--------------------------------------------------------------------------------------------*/

import * as nls from "vs/nls";
import * as vszip from "vszip";
import * as fs from "fs";
import * as path from "path";
import * as tarStream from "tar-stream";
import { promisify } from "util";
import { ILogService } from "vs/platform/log/common/log";
import { CancellationToken } from "vs/base/common/cancellation";
import { mkdirp } from "vs/base/node/pfs";

Expand Down Expand Up @@ -58,81 +58,38 @@ export function zip(tarPath: string, files: IFile[]): Promise<string> {
});
}

export async function extract(tarPath: string, targetPath: string, options: IExtractOptions = {}, token: CancellationToken): Promise<void> {
const sourcePathRegex = new RegExp(options.sourcePath ? `^${options.sourcePath}` : '');

export async function extract(archivePath: string, extractPath: string, options: IExtractOptions = {}, token: CancellationToken): Promise<void> {
return new Promise<void>(async (c, e) => {
const buffer = await promisify(fs.readFile)(tarPath);
const extractor = tarStream.extract();
extractor.once('error', e);
extractor.on('entry', (header, stream, next) => {
const rawName = header.name;
extractZip(archivePath, extractPath, options, token).then(c).catch((ex) => {
if (!ex.toString().includes("Corrupt ZIP")) {
e(ex);

const nextEntry = (): void => {
stream.resume();
next();
};

if (token.isCancellationRequested) {
return nextEntry();
}

if (!sourcePathRegex.test(rawName)) {
return nextEntry();
}

const fileName = rawName.replace(sourcePathRegex, '');

const targetFileName = path.join(targetPath, fileName);
if (/\/$/.test(fileName)) {
stream.resume();
mkdirp(targetFileName).then(() => {
next();
}, e);
return;
}

const dirName = path.dirname(fileName);
const targetDirName = path.join(targetPath, dirName);
if (targetDirName.indexOf(targetPath) !== 0) {
e(nls.localize('invalid file', "Error extracting {0}. Invalid file.", fileName));
return nextEntry();
}

mkdirp(targetDirName, void 0, token).then(() => {
const fstream = fs.createWriteStream(targetFileName, { mode: header.mode });
fstream.once('close', () => {
next();
});
fstream.once('error', (err) => {
e(err);
});
stream.pipe(fstream);
stream.resume();
});
extractTar(archivePath, extractPath, options, token).then(c).catch(e);
});
extractor.once('finish', () => {
c();
});
extractor.write(buffer);
extractor.end();
});
}

export function buffer(tarPath: string, filePath: string): Promise<Buffer> {
export function buffer(targetPath: string, filePath: string): Promise<Buffer> {
return new Promise<Buffer>(async (c, e) => {
let done: boolean = false;
extractAssets(tarPath, new RegExp(filePath), (path: string, data: Buffer) => {
if (path === filePath) {
done = true;
c(data);
}
}).then(() => {
if (!done) {
e("couldnt find asset " + filePath);
vszip.buffer(targetPath, filePath).then(c).catch((ex) => {
if (!ex.toString().includes("Corrupt ZIP")) {
e(ex);

return;
}
}).catch((ex) => {
e(ex);
let done: boolean = false;
extractAssets(targetPath, new RegExp(filePath), (assetPath: string, data: Buffer) => {
if (path.normalize(assetPath) === path.normalize(filePath)) {
done = true;
c(data);
}
}).then(() => {
if (!done) {
e("couldnt find asset " + filePath);
}
}).catch(e);
});
});
}
Expand All @@ -141,7 +98,7 @@ async function extractAssets(tarPath: string, match: RegExp, callback: (path: st
const buffer = await promisify(fs.readFile)(tarPath);
const extractor = tarStream.extract();
let callbackResolve: () => void;
let callbackReject: (ex?) => void;
let callbackReject: (ex?: any) => void;
const complete = new Promise<void>((r, rej) => {
callbackResolve = r;
callbackReject = rej;
Expand Down Expand Up @@ -185,3 +142,67 @@ async function extractData(stream: NodeJS.ReadableStream): Promise<Buffer> {
});
});
}

async function extractZip(zipPath: string, targetPath: string, options: IExtractOptions = {}, token: CancellationToken): Promise<void> {
return new Promise<void>(async (c, e) => {
vszip.extract(zipPath, targetPath, options, token).then(() => c()).catch(e);
});
}

async function extractTar(tarPath: string, targetPath: string, options: IExtractOptions = {}, token: CancellationToken): Promise<void> {
const sourcePathRegex = new RegExp(options.sourcePath ? `^${options.sourcePath}` : "");

return new Promise<void>(async (c, e) => {
const buffer = await promisify(fs.readFile)(tarPath);
const extractor = tarStream.extract();
extractor.once("error", e);
extractor.on("entry", (header, stream, next) => {
const rawName = path.normalize(header.name);

const nextEntry = (): void => {
stream.resume();
next();
};

if (token.isCancellationRequested) {
return nextEntry();
}

if (!sourcePathRegex.test(rawName)) {
return nextEntry();
}

const fileName = rawName.replace(sourcePathRegex, "");
const targetFileName = path.join(targetPath, fileName);
if (/\/$/.test(fileName)) {
stream.resume();
mkdirp(targetFileName).then(() => {
next();
}, e);

return;
}

const dirName = path.dirname(fileName);
const targetDirName = path.join(targetPath, dirName);
if (targetDirName.indexOf(targetPath) !== 0) {
e(nls.localize("invalid file", "Error extracting {0}. Invalid file.", fileName));

return nextEntry();
}

return mkdirp(targetDirName, undefined, token).then(() => {
const fstream = fs.createWriteStream(targetFileName, { mode: header.mode });
fstream.once("close", () => {
next();
});
fstream.once("error", e);
stream.pipe(fstream);
stream.resume();
});
});
extractor.once("finish", c);
extractor.write(buffer);
extractor.end();
});
}
Binary file added packages/vscode/test/test-extension.tar
Binary file not shown.
Binary file added packages/vscode/test/test-extension.vsix
Binary file not shown.
60 changes: 60 additions & 0 deletions packages/vscode/test/zip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import * as zip from "../src/fill/zip";
import * as path from "path";
import * as fs from "fs";
import * as cp from "child_process";
import { CancellationToken } from "vs/base/common/cancellation";

jest.mock("vs/nls", () => ({ "localize": (...args: any): => `${JSON.stringify(args)}` }));

describe("zip", () => {
const tarPath = path.resolve(__dirname, "./test-extension.tar");
const vsixPath = path.resolve(__dirname, "./test-extension.vsix");
const extractPath = path.resolve(__dirname, "./.test-extension");

beforeEach(() => {
if (!fs.existsSync(extractPath) || path.dirname(extractPath) !== __dirname) {
return;
}
cp.execSync(`rm -rf '${extractPath}'`);
});

// tslint:disable-next-line:no-any
const extract = (archivePath: string): () => any => {
// tslint:disable-next-line:no-any
return async (): Promise<any> => {
await expect(zip.extract(
archivePath,
extractPath,
{ sourcePath: "extension", overwrite: true },
CancellationToken.None,
)).resolves.toBe(undefined);
expect(fs.existsSync(extractPath)).toEqual(true);
expect(fs.existsSync(path.resolve(extractPath, ".vsixmanifest"))).toEqual(true);
expect(fs.existsSync(path.resolve(extractPath, "package.json"))).toEqual(true);
};
};
it("should extract from tarred VSIX", extract(tarPath), 2000);
it("should extract from zipped VSIX", extract(vsixPath), 2000);

// tslint:disable-next-line:no-any
const buffer = (archivePath: string): () => any => {
// tslint:disable-next-line:no-any
return async (): Promise<any> => {
expect(fs.existsSync(archivePath)).toEqual(true);
await zip.extract(
archivePath,
extractPath,
{ sourcePath: "extension", overwrite: true },
CancellationToken.None,
);
expect(fs.existsSync(extractPath)).toEqual(true);
const manifestPath = path.resolve(extractPath, ".vsixmanifest");
expect(fs.existsSync(manifestPath)).toEqual(true);
const manifestBuf = fs.readFileSync(manifestPath);
expect(manifestBuf.length).toBeGreaterThan(0);
await expect(zip.buffer(archivePath, "extension.vsixmanifest")).resolves.toEqual(manifestBuf);
};
};
it("should buffer tarred VSIX", buffer(tarPath), 2000);
it("should buffer zipped VSIX", buffer(vsixPath), 2000);
});
1 change: 1 addition & 0 deletions packages/vscode/webpack.bootstrap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports = merge(
"vs/platform/product/node/package": path.resolve(vsFills, "package.ts"),
"vs/platform/product/node/product": path.resolve(vsFills, "product.ts"),
"vs/base/node/zip": path.resolve(vsFills, "zip.ts"),
"vszip": path.resolve(root, "lib/vscode/src/vs/base/node/zip.ts"),
"vs": path.resolve(root, "lib/vscode/src/vs"),
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/web/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.exports = merge(
"vs/platform/product/node/package": path.resolve(vsFills, "package.ts"),
"vs/platform/product/node/product": path.resolve(vsFills, "product.ts"),
"vs/base/node/zip": path.resolve(vsFills, "zip.ts"),
"vszip": path.resolve(root, "lib/vscode/src/vs/base/node/zip.ts"),
"vs": path.join(root, "lib", "vscode", "src", "vs"),
},
},
Expand Down