From 1ebb489ba70b71a5241c653fa1bb3bd6510f5be3 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 29 Jun 2018 11:33:46 -0500 Subject: [PATCH] Add ArchiverError, some explicit handlers --- types/archiver/archiver-tests.ts | 12 ++++++++++++ types/archiver/index.d.ts | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/types/archiver/archiver-tests.ts b/types/archiver/archiver-tests.ts index dbb1df1de7..d61950b657 100644 --- a/types/archiver/archiver-tests.ts +++ b/types/archiver/archiver-tests.ts @@ -67,3 +67,15 @@ archiver.use(() => {}); archiver.finalize(); archiver.symlink('./path', './target'); + +function fakeHandler(err: Archiver.ArchiverError) { + console.log(err.code); + console.log(err.message); + console.log(err.stack); + console.log(err.data); +} + +const fakeError = new Archiver.ArchiverError('code', 'foo'); + +archiver.on('error', fakeHandler); +archiver.on('warning', fakeHandler); diff --git a/types/archiver/index.d.ts b/types/archiver/index.d.ts index d33e8ec6dd..a9cafca24b 100644 --- a/types/archiver/index.d.ts +++ b/types/archiver/index.d.ts @@ -24,9 +24,26 @@ declare namespace archiver { mode?: number; } + interface ProgressData { + entries: { + total: number; + processed: number; + }; + fs: { + totalBytes: number; + processedBytes: number; + }; + } + /** A function that lets you either opt out of including an entry (by returning false), or modify the contents of an entry as it is added (by returning an EntryData) */ type EntryDataFunction = (entry: EntryData) => false | EntryData; + class ArchiverError extends Error { + code: string; // Since archiver format support is modular, we cannot enumerate all possible error codes, as the modules can throw arbitrary ones. + data: any; + constructor(code: string, data: any); + } + interface Archiver extends stream.Transform { abort(): this; append(source: stream.Readable | Buffer | string, name?: EntryData): this; @@ -44,6 +61,13 @@ declare namespace archiver { use(plugin: Function): this; symlink(filepath: string, target: string): this; + + on(event: 'error' | 'warning', listener: (error: ArchiverError) => void): this; + on(event: 'data', listener: (data: EntryData) => void): this; + on(event: 'progress', listener: (progress: ProgressData) => void): this; + on(event: 'close' | 'drain' | 'finish', listener: () => void): this; + on(event: 'pipe' | 'unpipe', listener: (src: stream.Readable) => void): this; + on(event: string, listener: (...args: any[]) => void): this; } type ArchiverOptions = CoreOptions & TransformOptions & ZipOptions & TarOptions;