From 945b7bdd9bdf3a48e901818e52884ee908cb444d Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 1 Jun 2018 15:05:26 -0500 Subject: [PATCH 1/4] Add typings for yauzl-promise --- types/yauzl-promise/index.d.ts | 61 ++++++++++++++++++++++ types/yauzl-promise/package.json | 6 +++ types/yauzl-promise/tsconfig.json | 22 ++++++++ types/yauzl-promise/tslint.json | 1 + types/yauzl-promise/yauzl-promise-tests.ts | 51 ++++++++++++++++++ 5 files changed, 141 insertions(+) create mode 100644 types/yauzl-promise/index.d.ts create mode 100644 types/yauzl-promise/package.json create mode 100644 types/yauzl-promise/tsconfig.json create mode 100644 types/yauzl-promise/tslint.json create mode 100644 types/yauzl-promise/yauzl-promise-tests.ts diff --git a/types/yauzl-promise/index.d.ts b/types/yauzl-promise/index.d.ts new file mode 100644 index 0000000000..9374a62e5c --- /dev/null +++ b/types/yauzl-promise/index.d.ts @@ -0,0 +1,61 @@ +import { Entry as BaseEntry, Options, ZipFileOptions, RandomAccessReader} from 'yauzl'; +import { Readable } from 'stream'; +import { EventEmitter } from 'events'; + +// This class is not directly compatible with @types/yauzl 's ZipFile as this library changes the function signatures +// Therefore, it is replaced, albeit with a significant portion +export class ZipFile extends EventEmitter { + // This chunk taken directly from @types/yauzl + autoClose: boolean; + comment: string; + decodeStrings: boolean; + emittedError: boolean; + entriesRead: number; + entryCount: number; + fileSize: number; + isOpen: boolean; + lazyEntries: boolean; + readEntryCursor: boolean; + validateEntrySizes: boolean; + + constructor( + reader: RandomAccessReader, + centralDirectoryOffset: number, + fileSize: number, + entryCount: number, + comment: string, + autoClose: boolean, + lazyEntries: boolean, + decodeStrings: boolean, + validateEntrySizes: boolean, + ); + + // These funcitons are custom to yauzl-promise + + close(): Promise; + readEntry(): Promise; + readEntries(numEntries?: number): Promise; + walkEntries(callback: (entry: Entry) => Promise | void, numEntries?: number): Promise; + openReadStream(entry: Entry, options?: ZipFileOptions): Promise; +} + +export class Entry extends BaseEntry { + openReadStream(options?: ZipFileOptions): Promise; +} + + +export function open(path: string, options?: Options): Promise; +// export function open(path: string): Promise; +export function fromFd(fd: number, options?: Options): Promise; +// export function fromFd(fd: number): Promise; +export function fromBuffer(buffer: Buffer, options?: Options): Promise; +// export function fromBuffer(buffer: Buffer): Promise; +export function fromRandomAccessReader(reader: RandomAccessReader, totalSize: number, options?: Options): Promise; +// export function fromRandomAccessReader(reader: RandomAccessReader, totalSize: number): Promise; + +// These are copied directly from @types/yauzl, I beleive they are unmodified +export function dosDateTimeToDate(date: number, time: number): Date; +export function validateFileName(fileName: string): string | null; + + +export { RandomAccessReader, Options, ZipFileOptions } \ No newline at end of file diff --git a/types/yauzl-promise/package.json b/types/yauzl-promise/package.json new file mode 100644 index 0000000000..747879f783 --- /dev/null +++ b/types/yauzl-promise/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "@types/yauzl": "^2.9.0" + } +} diff --git a/types/yauzl-promise/tsconfig.json b/types/yauzl-promise/tsconfig.json new file mode 100644 index 0000000000..73510344fe --- /dev/null +++ b/types/yauzl-promise/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "yauzl-promise-tests.ts" + ] +} diff --git a/types/yauzl-promise/tslint.json b/types/yauzl-promise/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/yauzl-promise/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/yauzl-promise/yauzl-promise-tests.ts b/types/yauzl-promise/yauzl-promise-tests.ts new file mode 100644 index 0000000000..6d7ed97298 --- /dev/null +++ b/types/yauzl-promise/yauzl-promise-tests.ts @@ -0,0 +1,51 @@ +import * as yauzl from 'yauzl-promise'; + +class FakeRaR extends yauzl.RandomAccessReader { + +} + +const options: yauzl.Options = { + autoClose: true +}; + +const zipOptions: yauzl.ZipFileOptions = { + decrypt: true, + decompress: true, + start: 0, + end: 1 +}; + +const date = yauzl.dosDateTimeToDate(1, 1); +const fn = yauzl.validateFileName("fake"); + +async function test() { + const zip = await yauzl.open(""); + const open2 = await yauzl.open("", options); + + const fd1 = await yauzl.fromFd(0); + const fd2 = await yauzl.fromFd(0, options); + + const buffer1 = await yauzl.fromBuffer(Buffer.from("test", "utf-8")); + const buffer2 = await yauzl.fromBuffer(Buffer.from("test", "utf-8"), options); + + const rar1 = await yauzl.fromRandomAccessReader(new FakeRaR(), 1); + const rar2 = await yauzl.fromRandomAccessReader(new FakeRaR(), 1, options); + + const entry = await zip.readEntry(); + await zip.readEntries(); + await zip.readEntries(1); + + const rs = await zip.openReadStream(entry); + await zip.openReadStream(entry, zipOptions); + + await entry.openReadStream(); + await entry.openReadStream(zipOptions); + + await zip.walkEntries(async (entry: yauzl.Entry) => { + console.log("foo"); + }); + + await zip.walkEntries(async (entry: yauzl.Entry) => { + console.log("foo"); + }, 1); +} From ea6a794194004c55af94ee03e05e76b25999b53a Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 1 Jun 2018 15:20:18 -0500 Subject: [PATCH 2/4] fix lint issues, add header --- types/yauzl-promise/index.d.ts | 15 +++++++++++---- types/yauzl-promise/tsconfig.json | 1 + types/yauzl-promise/yauzl-promise-tests.ts | 4 +--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/types/yauzl-promise/index.d.ts b/types/yauzl-promise/index.d.ts index 9374a62e5c..b859d8891e 100644 --- a/types/yauzl-promise/index.d.ts +++ b/types/yauzl-promise/index.d.ts @@ -1,4 +1,13 @@ -import { Entry as BaseEntry, Options, ZipFileOptions, RandomAccessReader} from 'yauzl'; +// Type definitions for yauzl-promise 2.1 +// Project: https://github.com/overlookmotel/yauzl-promise +// Definitions by: Dave Lee +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +// TypeScript Version: 2.1 + +/// + +import { Entry as BaseEntry, Options, ZipFileOptions, RandomAccessReader } from 'yauzl'; import { Readable } from 'stream'; import { EventEmitter } from 'events'; @@ -43,7 +52,6 @@ export class Entry extends BaseEntry { openReadStream(options?: ZipFileOptions): Promise; } - export function open(path: string, options?: Options): Promise; // export function open(path: string): Promise; export function fromFd(fd: number, options?: Options): Promise; @@ -57,5 +65,4 @@ export function fromRandomAccessReader(reader: RandomAccessReader, totalSize: nu export function dosDateTimeToDate(date: number, time: number): Date; export function validateFileName(fileName: string): string | null; - -export { RandomAccessReader, Options, ZipFileOptions } \ No newline at end of file +export { RandomAccessReader, Options, ZipFileOptions }; diff --git a/types/yauzl-promise/tsconfig.json b/types/yauzl-promise/tsconfig.json index 73510344fe..86c1696468 100644 --- a/types/yauzl-promise/tsconfig.json +++ b/types/yauzl-promise/tsconfig.json @@ -7,6 +7,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/yauzl-promise/yauzl-promise-tests.ts b/types/yauzl-promise/yauzl-promise-tests.ts index 6d7ed97298..0a33ad6440 100644 --- a/types/yauzl-promise/yauzl-promise-tests.ts +++ b/types/yauzl-promise/yauzl-promise-tests.ts @@ -1,8 +1,6 @@ import * as yauzl from 'yauzl-promise'; -class FakeRaR extends yauzl.RandomAccessReader { - -} +class FakeRaR extends yauzl.RandomAccessReader {} const options: yauzl.Options = { autoClose: true From 9a0c840e20cc95cc368cc61af802f993e99de426 Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 1 Jun 2018 15:26:00 -0500 Subject: [PATCH 3/4] travis says that I shouldn't use a package.json for a @types dependency. Use relative path instead? --- types/yauzl-promise/index.d.ts | 2 +- types/yauzl-promise/package.json | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 types/yauzl-promise/package.json diff --git a/types/yauzl-promise/index.d.ts b/types/yauzl-promise/index.d.ts index b859d8891e..ed84f3273c 100644 --- a/types/yauzl-promise/index.d.ts +++ b/types/yauzl-promise/index.d.ts @@ -7,7 +7,7 @@ /// -import { Entry as BaseEntry, Options, ZipFileOptions, RandomAccessReader } from 'yauzl'; +import { Entry as BaseEntry, Options, ZipFileOptions, RandomAccessReader } from '../yauzl'; import { Readable } from 'stream'; import { EventEmitter } from 'events'; diff --git a/types/yauzl-promise/package.json b/types/yauzl-promise/package.json deleted file mode 100644 index 747879f783..0000000000 --- a/types/yauzl-promise/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "private": true, - "dependencies": { - "@types/yauzl": "^2.9.0" - } -} From 25c3781cfe19ad8e5cba50d26ea732ddc839239d Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 1 Jun 2018 15:34:42 -0500 Subject: [PATCH 4/4] Travis has informed that relative paths are wrong too. 3rd time is the charm? --- types/yauzl-promise/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/yauzl-promise/index.d.ts b/types/yauzl-promise/index.d.ts index ed84f3273c..b859d8891e 100644 --- a/types/yauzl-promise/index.d.ts +++ b/types/yauzl-promise/index.d.ts @@ -7,7 +7,7 @@ /// -import { Entry as BaseEntry, Options, ZipFileOptions, RandomAccessReader } from '../yauzl'; +import { Entry as BaseEntry, Options, ZipFileOptions, RandomAccessReader } from 'yauzl'; import { Readable } from 'stream'; import { EventEmitter } from 'events';