-
Notifications
You must be signed in to change notification settings - Fork 27.9k
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
Debt: avoid promise.cancel in zip.ts #56656
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,11 +7,13 @@ import * as nls from 'vs/nls'; | |
import * as path from 'path'; | ||
import { createWriteStream, WriteStream } from 'fs'; | ||
import { Readable } from 'stream'; | ||
import { nfcall, ninvoke, SimpleThrottler } from 'vs/base/common/async'; | ||
import { nfcall, ninvoke, SimpleThrottler, createCancelablePromise, CancelablePromise } from 'vs/base/common/async'; | ||
import { mkdirp, rimraf } from 'vs/base/node/pfs'; | ||
import { TPromise } from 'vs/base/common/winjs.base'; | ||
import { open as _openZip, Entry, ZipFile } from 'yauzl'; | ||
import { ILogService } from 'vs/platform/log/common/log'; | ||
import { CancellationToken } from 'vs/base/common/cancellation'; | ||
import { once } from 'vs/base/common/event'; | ||
|
||
export interface IExtractOptions { | ||
overwrite?: boolean; | ||
|
@@ -69,7 +71,7 @@ function toExtractError(err: Error): ExtractError { | |
return new ExtractError(type, err); | ||
} | ||
|
||
function extractEntry(stream: Readable, fileName: string, mode: number, targetPath: string, options: IOptions): TPromise<void> { | ||
function extractEntry(stream: Readable, fileName: string, mode: number, targetPath: string, options: IOptions, token: CancellationToken): TPromise<void> { | ||
const dirName = path.dirname(fileName); | ||
const targetDirName = path.join(targetPath, dirName); | ||
if (targetDirName.indexOf(targetPath) !== 0) { | ||
|
@@ -78,72 +80,88 @@ function extractEntry(stream: Readable, fileName: string, mode: number, targetPa | |
const targetFileName = path.join(targetPath, fileName); | ||
|
||
let istream: WriteStream; | ||
return mkdirp(targetDirName).then(() => new TPromise((c, e) => { | ||
|
||
once(token.onCancellationRequested)(() => { | ||
if (istream) { | ||
istream.close(); | ||
} | ||
}); | ||
|
||
return mkdirp(targetDirName, void 0, token).then(() => new TPromise((c, e) => { | ||
if (token.isCancellationRequested) { | ||
return; | ||
} | ||
|
||
istream = createWriteStream(targetFileName, { mode }); | ||
istream.once('close', () => c(null)); | ||
istream.once('error', e); | ||
stream.once('error', e); | ||
stream.pipe(istream); | ||
}, () => { | ||
if (istream) { | ||
istream.close(); | ||
} | ||
})); | ||
} | ||
|
||
function extractZip(zipfile: ZipFile, targetPath: string, options: IOptions, logService: ILogService): TPromise<void> { | ||
function extractZip(zipfile: ZipFile, targetPath: string, options: IOptions, logService: ILogService): CancelablePromise<void> { | ||
let isCanceled = false; | ||
let last = TPromise.wrap<any>(null); | ||
let last = createCancelablePromise(() => Promise.resolve(null)); | ||
let extractedEntriesCount = 0; | ||
|
||
return new TPromise((c, e) => { | ||
const throttler = new SimpleThrottler(); | ||
return createCancelablePromise(token => { | ||
|
||
once(token.onCancellationRequested)(() => { | ||
logService.debug(targetPath, 'Cancelled.'); | ||
isCanceled = true; | ||
last.cancel(); | ||
zipfile.close(); | ||
}); | ||
|
||
return new TPromise((c, e) => { | ||
const throttler = new SimpleThrottler(); | ||
|
||
const readNextEntry = (token: CancellationToken) => { | ||
if (token.isCancellationRequested) { | ||
return; | ||
} | ||
|
||
extractedEntriesCount++; | ||
zipfile.readEntry(); | ||
}; | ||
|
||
const readNextEntry = () => { | ||
extractedEntriesCount++; | ||
zipfile.once('error', e); | ||
zipfile.once('close', () => last.then(() => { | ||
if (isCanceled || zipfile.entryCount === extractedEntriesCount) { | ||
c(null); | ||
} else { | ||
e(new ExtractError('Incomplete', new Error(nls.localize('incompleteExtract', "Incomplete. Found {0} of {1} entries", extractedEntriesCount, zipfile.entryCount)))); | ||
} | ||
}, e)); | ||
zipfile.readEntry(); | ||
}; | ||
|
||
zipfile.once('error', e); | ||
zipfile.once('close', () => last.then(() => { | ||
if (isCanceled || zipfile.entryCount === extractedEntriesCount) { | ||
c(null); | ||
} else { | ||
e(new ExtractError('Incomplete', new Error(nls.localize('incompleteExtract', "Incomplete. Found {0} of {1} entries", extractedEntriesCount, zipfile.entryCount)))); | ||
} | ||
}, e)); | ||
zipfile.readEntry(); | ||
zipfile.on('entry', (entry: Entry) => { | ||
|
||
if (isCanceled) { | ||
return; | ||
} | ||
|
||
if (!options.sourcePathRegex.test(entry.fileName)) { | ||
readNextEntry(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we not passing the real token instead of none token? |
||
return; | ||
} | ||
|
||
const fileName = entry.fileName.replace(options.sourcePathRegex, ''); | ||
|
||
// directory file names end with '/' | ||
if (/\/$/.test(fileName)) { | ||
const targetFileName = path.join(targetPath, fileName); | ||
last = mkdirp(targetFileName).then(() => readNextEntry()); | ||
return; | ||
} | ||
|
||
const stream = ninvoke(zipfile, zipfile.openReadStream, entry); | ||
const mode = modeFromEntry(entry); | ||
|
||
last = throttler.queue(() => stream.then(stream => extractEntry(stream, fileName, mode, targetPath, options).then(() => readNextEntry()))); | ||
zipfile.on('entry', (entry: Entry) => { | ||
|
||
if (isCanceled) { | ||
return; | ||
} | ||
|
||
if (!options.sourcePathRegex.test(entry.fileName)) { | ||
readNextEntry(CancellationToken.None); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we not passing the real token instead of none token? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, I guess I added this first and only later added the token. |
||
return; | ||
} | ||
|
||
const fileName = entry.fileName.replace(options.sourcePathRegex, ''); | ||
|
||
// directory file names end with '/' | ||
if (/\/$/.test(fileName)) { | ||
const targetFileName = path.join(targetPath, fileName); | ||
last = createCancelablePromise(token => mkdirp(targetFileName, void 0, token).then(() => readNextEntry(token))); | ||
return; | ||
} | ||
|
||
const stream = ninvoke(zipfile, zipfile.openReadStream, entry); | ||
const mode = modeFromEntry(entry); | ||
|
||
last = createCancelablePromise(token => throttler.queue(() => stream.then(stream => extractEntry(stream, fileName, mode, targetPath, options, token).then(() => readNextEntry(token))))); | ||
}); | ||
}); | ||
}, () => { | ||
logService.debug(targetPath, 'Cancelled.'); | ||
isCanceled = true; | ||
last.cancel(); | ||
zipfile.close(); | ||
}).then(null, err => TPromise.wrapError(toExtractError(err))); | ||
}); | ||
} | ||
|
||
function openZip(zipFile: string, lazy: boolean = false): TPromise<ZipFile> { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No
once
not needed as the event will only ever fire once