|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as nls from "vs/nls"; |
| 7 | +import * as fs from "fs"; |
| 8 | +import * as path from "path"; |
| 9 | +import * as tarStream from "tar-stream"; |
| 10 | +import { promisify } from "util"; |
| 11 | +import { ILogService } from "vs/platform/log/common/log"; |
| 12 | +import { CancellationToken } from "vs/base/common/cancellation"; |
| 13 | +import { mkdirp } from "vs/base/node/pfs"; |
| 14 | + |
| 15 | +export interface IExtractOptions { |
| 16 | + overwrite?: boolean; |
| 17 | + |
| 18 | + /** |
| 19 | + * Source path within the ZIP archive. Only the files contained in this |
| 20 | + * path will be extracted. |
| 21 | + */ |
| 22 | + sourcePath?: string; |
| 23 | +} |
| 24 | + |
| 25 | +export interface IFile { |
| 26 | + path: string; |
| 27 | + contents?: Buffer | string; |
| 28 | + localPath?: string; |
| 29 | +} |
| 30 | + |
| 31 | +export function zip(tarPath: string, files: IFile[]): Promise<string> { |
| 32 | + return new Promise<string>((c, e) => { |
| 33 | + const pack = tarStream.pack(); |
| 34 | + const chunks: Buffer[] = []; |
| 35 | + const ended = new Promise<Buffer>((res, rej) => { |
| 36 | + pack.on("end", () => { |
| 37 | + res(Buffer.concat(chunks)); |
| 38 | + }); |
| 39 | + }); |
| 40 | + pack.on("data", (chunk) => { |
| 41 | + chunks.push(chunk as Buffer); |
| 42 | + }); |
| 43 | + for (let i = 0; i < files.length; i++) { |
| 44 | + const file = files[i]; |
| 45 | + pack.entry({ |
| 46 | + name: file.path, |
| 47 | + }, file.contents); |
| 48 | + } |
| 49 | + pack.finalize(); |
| 50 | + |
| 51 | + ended.then((buffer) => { |
| 52 | + return promisify(fs.writeFile)(tarPath, buffer); |
| 53 | + }).then(() => { |
| 54 | + c(tarPath); |
| 55 | + }).catch((ex) => { |
| 56 | + e(ex); |
| 57 | + }); |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +export async function extract(tarPath: string, targetPath: string, options: IExtractOptions = {}, logService: ILogService, token: CancellationToken): Promise<void> { |
| 62 | + const sourcePathRegex = new RegExp(options.sourcePath ? `^${options.sourcePath}` : ''); |
| 63 | + |
| 64 | + return new Promise<void>(async (c, e) => { |
| 65 | + const buffer = await promisify(fs.readFile)(tarPath); |
| 66 | + const extractor = tarStream.extract(); |
| 67 | + extractor.once('error', e); |
| 68 | + extractor.on('entry', (header, stream, next) => { |
| 69 | + const rawName = header.name; |
| 70 | + |
| 71 | + const nextEntry = (): void => { |
| 72 | + stream.resume(); |
| 73 | + next(); |
| 74 | + }; |
| 75 | + |
| 76 | + if (token.isCancellationRequested) { |
| 77 | + return nextEntry(); |
| 78 | + } |
| 79 | + |
| 80 | + if (!sourcePathRegex.test(rawName)) { |
| 81 | + return nextEntry(); |
| 82 | + } |
| 83 | + |
| 84 | + const fileName = rawName.replace(sourcePathRegex, ''); |
| 85 | + |
| 86 | + const targetFileName = path.join(targetPath, fileName); |
| 87 | + if (/\/$/.test(fileName)) { |
| 88 | + stream.resume(); |
| 89 | + mkdirp(targetFileName).then(() => { |
| 90 | + next(); |
| 91 | + }, e); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const dirName = path.dirname(fileName); |
| 96 | + const targetDirName = path.join(targetPath, dirName); |
| 97 | + if (targetDirName.indexOf(targetPath) !== 0) { |
| 98 | + e(nls.localize('invalid file', "Error extracting {0}. Invalid file.", fileName)); |
| 99 | + return nextEntry(); |
| 100 | + } |
| 101 | + |
| 102 | + mkdirp(targetDirName, void 0, token).then(() => { |
| 103 | + const fstream = fs.createWriteStream(targetFileName, { mode: header.mode }); |
| 104 | + fstream.once('close', () => { |
| 105 | + next(); |
| 106 | + }); |
| 107 | + fstream.once('error', (err) => { |
| 108 | + e(err); |
| 109 | + }); |
| 110 | + stream.pipe(fstream); |
| 111 | + stream.resume(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + extractor.once('finish', () => { |
| 115 | + c(); |
| 116 | + }); |
| 117 | + extractor.write(buffer); |
| 118 | + extractor.end(); |
| 119 | + }); |
| 120 | +} |
| 121 | + |
| 122 | +export function buffer(tarPath: string, filePath: string): Promise<Buffer> { |
| 123 | + return new Promise<Buffer>(async (c, e) => { |
| 124 | + let done: boolean = false; |
| 125 | + extractAssets(tarPath, new RegExp(filePath), (path: string, data: Buffer) => { |
| 126 | + if (path === filePath) { |
| 127 | + done = true; |
| 128 | + c(data); |
| 129 | + } |
| 130 | + }).then(() => { |
| 131 | + if (!done) { |
| 132 | + e("couldnt find asset " + filePath); |
| 133 | + } |
| 134 | + }).catch((ex) => { |
| 135 | + e(ex); |
| 136 | + }); |
| 137 | + }); |
| 138 | +} |
| 139 | + |
| 140 | +async function extractAssets(tarPath: string, match: RegExp, callback: (path: string, data: Buffer) => void): Promise<void> { |
| 141 | + const buffer = await promisify(fs.readFile)(tarPath); |
| 142 | + const extractor = tarStream.extract(); |
| 143 | + let callbackResolve: () => void; |
| 144 | + let callbackReject: (ex?) => void; |
| 145 | + const complete = new Promise<void>((r, rej) => { |
| 146 | + callbackResolve = r; |
| 147 | + callbackReject = rej; |
| 148 | + }); |
| 149 | + extractor.once("error", (err) => { |
| 150 | + callbackReject(err); |
| 151 | + }); |
| 152 | + extractor.on("entry", (header, stream, next) => { |
| 153 | + const name = header.name; |
| 154 | + if (match.test(name)) { |
| 155 | + extractData(stream).then((data) => { |
| 156 | + callback(name, data); |
| 157 | + next(); |
| 158 | + }); |
| 159 | + stream.resume(); |
| 160 | + } else { |
| 161 | + stream.on("end", () => { |
| 162 | + next(); |
| 163 | + }); |
| 164 | + stream.resume(); |
| 165 | + } |
| 166 | + }); |
| 167 | + extractor.on("finish", () => { |
| 168 | + callbackResolve(); |
| 169 | + }); |
| 170 | + extractor.write(buffer); |
| 171 | + extractor.end(); |
| 172 | + return complete; |
| 173 | +} |
| 174 | + |
| 175 | +async function extractData(stream: NodeJS.ReadableStream): Promise<Buffer> { |
| 176 | + return new Promise<Buffer>((res, rej) => { |
| 177 | + const fileData: Buffer[] = []; |
| 178 | + stream.on('data', (data) => fileData.push(data)); |
| 179 | + stream.on('end', () => { |
| 180 | + const fd = Buffer.concat(fileData); |
| 181 | + res(fd); |
| 182 | + }); |
| 183 | + stream.on('error', (err) => { |
| 184 | + rej(err); |
| 185 | + }); |
| 186 | + }); |
| 187 | +} |
0 commit comments