From 945b7bdd9bdf3a48e901818e52884ee908cb444d Mon Sep 17 00:00:00 2001 From: Dave Lee Date: Fri, 1 Jun 2018 15:05:26 -0500 Subject: [PATCH] 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); +}