Merge pull request #26212 from dlee-nvisia/master

Add typings for yauzl-promise
This commit is contained in:
Armando Aguirre
2018-06-04 17:59:58 -07:00
committed by GitHub
4 changed files with 141 additions and 0 deletions

68
types/yauzl-promise/index.d.ts vendored Normal file
View File

@@ -0,0 +1,68 @@
// Type definitions for yauzl-promise 2.1
// Project: https://github.com/overlookmotel/yauzl-promise
// Definitions by: Dave Lee <https://github.com/dlee-nvisia>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
/// <reference types="node" />
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<void>;
readEntry(): Promise<Entry>;
readEntries(numEntries?: number): Promise<Entry[]>;
walkEntries(callback: (entry: Entry) => Promise<void> | void, numEntries?: number): Promise<void>;
openReadStream(entry: Entry, options?: ZipFileOptions): Promise<Readable>;
}
export class Entry extends BaseEntry {
openReadStream(options?: ZipFileOptions): Promise<Readable>;
}
export function open(path: string, options?: Options): Promise<ZipFile>;
// export function open(path: string): Promise<ZipFile>;
export function fromFd(fd: number, options?: Options): Promise<ZipFile>;
// export function fromFd(fd: number): Promise<ZipFile>;
export function fromBuffer(buffer: Buffer, options?: Options): Promise<ZipFile>;
// export function fromBuffer(buffer: Buffer): Promise<ZipFile>;
export function fromRandomAccessReader(reader: RandomAccessReader, totalSize: number, options?: Options): Promise<ZipFile>;
// export function fromRandomAccessReader(reader: RandomAccessReader, totalSize: number): Promise<ZipFile>;
// 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 };

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }

View File

@@ -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);
}