From 01b4323056c8c310f29fdc875d58843f45a09e1b Mon Sep 17 00:00:00 2001 From: Josh Kelley Date: Sat, 13 Jun 2026 13:23:59 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#75106=20[yauzl]=20?= =?UTF-8?q?Add=20support=20for=20promises=20in=20yauzl=203.4.0=20by=20@jos?= =?UTF-8?q?hkel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/yauzl/index.d.ts | 20 ++++++++++++++++++++ types/yauzl/package.json | 2 +- types/yauzl/tsconfig.json | 2 +- types/yauzl/yauzl-tests.ts | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/types/yauzl/index.d.ts b/types/yauzl/index.d.ts index 31a8fc4defa250..9273d285fdcf82 100644 --- a/types/yauzl/index.d.ts +++ b/types/yauzl/index.d.ts @@ -128,6 +128,18 @@ export class ZipFile extends EventEmitter { uncompressedSize: number | null, callback: (err: Error | null, stream: Readable) => void, ): void; + openReadStreamPromise(entry: Entry, options?: ZipFileOptions): Promise; + readLocalFileHeaderPromise(entry: Entry, options: { minimal: true }): Promise<{ fileDataStart: number }>; + readLocalFileHeaderPromise(entry: Entry, options?: { minimal?: boolean }): Promise; + openReadStreamLowLevelPromise( + fileDataStart: number, + compressedSize: number, + relativeStart: number, + relativeEnd: number, + decompress: boolean, + uncompressedSize: number | null, + ): Promise; + eachEntry(): AsyncIterableIterator; close(): void; readEntry(): void; } @@ -161,6 +173,14 @@ export function fromRandomAccessReader( totalSize: number, callback: (err: Error | null, zipfile: ZipFile) => void, ): void; +export function openPromise(path: string, options?: Options): Promise; +export function fromFdPromise(fd: number, options?: Options): Promise; +export function fromBufferPromise(buffer: Buffer, options?: Options): Promise; +export function fromRandomAccessReaderPromise( + reader: RandomAccessReader, + totalSize: number, + options?: Options, +): Promise; /** @deprecated Use `entry.getLastModDate()` instead. */ export function dosDateTimeToDate(date: number, time: number): Date; export function validateFileName(fileName: string): string | null; diff --git a/types/yauzl/package.json b/types/yauzl/package.json index bac6fe251c4a07..2f2a805be7a689 100644 --- a/types/yauzl/package.json +++ b/types/yauzl/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "@types/yauzl", - "version": "3.3.9999", + "version": "3.4.9999", "projects": [ "https://github.com/thejoshwolfe/yauzl" ], diff --git a/types/yauzl/tsconfig.json b/types/yauzl/tsconfig.json index d287e4dfc369a7..0dcad6011dfd6b 100644 --- a/types/yauzl/tsconfig.json +++ b/types/yauzl/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "module": "node16", "lib": [ - "es6" + "es2018" ], "noImplicitAny": true, "noImplicitThis": true, diff --git a/types/yauzl/yauzl-tests.ts b/types/yauzl/yauzl-tests.ts index 988000feff4c7e..28e14a368ed3a6 100644 --- a/types/yauzl/yauzl-tests.ts +++ b/types/yauzl/yauzl-tests.ts @@ -119,3 +119,39 @@ if (fromDos.getTime() < 0) throw new Error("unreachable"); const decodedName: string = yauzl.getFileNameLowLevel(0x800, Buffer.from("a.txt"), [], true); const parsed: yauzl.ExtraField[] = yauzl.parseExtraFields(Buffer.alloc(0)); if (decodedName.length < 0 || parsed.length < 0) throw new Error("unreachable"); + +// Promises API. +async function withPromises() { + const zipfile: yauzl.ZipFile = await yauzl.openPromise("path/to/file.zip"); + await yauzl.openPromise("path/to/file.zip", { strictFileNames: true }); + await yauzl.fromFdPromise(0); + await yauzl.fromBufferPromise(Buffer.alloc(0), { decodeStrings: false }); + await yauzl.fromRandomAccessReaderPromise(new MemoryReader(), 1024); + + for await (const entry of zipfile.eachEntry()) { + const name: string = entry.fileName; + if (name.length < 0) throw new Error("unreachable"); + + const readStream: Readable = await zipfile.openReadStreamPromise(entry); + readStream.pipe(new Writable()); + await zipfile.openReadStreamPromise(entry, { decodeFileData: false }); + + const minimal = await zipfile.readLocalFileHeaderPromise(entry, { minimal: true }); + const start: number = minimal.fileDataStart; + + const header: yauzl.LocalFileHeader = await zipfile.readLocalFileHeaderPromise(entry); + const rawName: Buffer = header.fileName; + if (rawName.length < 0) throw new Error("unreachable"); + + const lowStream: Readable = await zipfile.openReadStreamLowLevelPromise( + start, + entry.compressedSize, + 0, + entry.compressedSize, + true, + entry.uncompressedSize, + ); + lowStream.pipe(new Writable()); + } +} +withPromises();