diff --git a/types/yauzl-promise/index.d.ts b/types/yauzl-promise/index.d.ts new file mode 100644 index 0000000000..b859d8891e --- /dev/null +++ b/types/yauzl-promise/index.d.ts @@ -0,0 +1,68 @@ +// 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'; + +// 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 }; diff --git a/types/yauzl-promise/tsconfig.json b/types/yauzl-promise/tsconfig.json new file mode 100644 index 0000000000..86c1696468 --- /dev/null +++ b/types/yauzl-promise/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": 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..0a33ad6440 --- /dev/null +++ b/types/yauzl-promise/yauzl-promise-tests.ts @@ -0,0 +1,49 @@ +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); +}