Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#70963 [zip-stream] Add types for zip-strea…
Browse files Browse the repository at this point in the history
…m by @MysteryBlokHed
  • Loading branch information
MysteryBlokHed authored Oct 22, 2024
1 parent 59b152e commit 08f9649
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
5 changes: 5 additions & 0 deletions types/zip-stream/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!**/*.d.ts
!**/*.d.cts
!**/*.d.mts
!**/*.d.*.ts
80 changes: 80 additions & 0 deletions types/zip-stream/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/// <reference types="node" />
import { ZipArchiveEntry, ZipArchiveOutputStream } from "compress-commons";
import { Stream, TransformOptions } from "readable-stream";

import { Stream as NodeStream } from "node:stream";
import { ZlibOptions } from "node:zlib";

export interface ZipStreamOptions extends TransformOptions {
/**
* Prepends a forward slash to archive file paths
* @default false
*/
namePrependSlash?: boolean | undefined;
/** Sets the zip archive comment */
comment?: string | undefined;
/**
* Forces archive to use local file times instead of UTC
* @default false
*/
forceLocalTime?: boolean | undefined;
/**
* Forces the archive to have Zip64 headers
* @default false
*/
forceZip64?: boolean | undefined;
/**
* Sets the compression method to STORE
* @default false
*/
store?: boolean | undefined;
/**
* Options passed to Zlib
* @see {@link ZlibOptions}
*/
zlib?: ZlibOptions | undefined;
/** @see {@link ZlibOptions.level} */
level?: number | undefined;
}

export interface FileDataInput {
/** Entry type. Defaults to `directory` if name ends with forward slash */
type?: "file" | "directory" | "symlink" | undefined;
/** Entry name, including internal path */
name?: string | null | undefined;
namePrependSlash?: boolean | undefined;
linkname?: string | null | undefined;
/** Sets the entry date. Defaults to current date */
date?: string | Date | null | undefined;
/** Sets the entry permissions. Defaults to D:0755/F:0644 */
mode?: number | null | undefined;
/** Sets the compression method to STORE */
store?: boolean | undefined;
/** Sets the entry comment */
comment?: string | undefined;
}

export interface FileDataNormalized {
type: "file" | "directory" | "symlink";
name: string | null;
namePrependSlash: boolean;
linkname: string | null;
date: Date | null;
store: boolean;
comment: string;
}

export default class ZipStream extends ZipArchiveOutputStream {
constructor(options?: ZipStreamOptions);

_normalizeFileData(data: FileDataInput): FileDataNormalized;

// @ts-expect-error TS2416 since overrided function signature is incompatible with extended class
entry(
source?: Buffer | Stream | NodeStream | string | null,
data?: FileDataInput,
callback?: (err: Error | null, ae?: ZipArchiveEntry) => void,
): this | undefined;

finalize(): void;
}
23 changes: 23 additions & 0 deletions types/zip-stream/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"private": true,
"name": "@types/zip-stream",
"version": "7.0.9999",
"type": "module",
"projects": [
"https://github.com/archiverjs/node-zip-stream"
],
"dependencies": {
"@types/compress-commons": "*",
"@types/node": "*",
"@types/readable-stream": "*"
},
"devDependencies": {
"@types/zip-stream": "workspace:."
},
"owners": [
{
"name": "Adam Thompson-Sharpe",
"githubUsername": "MysteryBlokHed"
}
]
}
17 changes: 17 additions & 0 deletions types/zip-stream/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"module": "node16",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"zip-stream-tests.ts"
]
}
52 changes: 52 additions & 0 deletions types/zip-stream/zip-stream-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import ZipStream from "zip-stream";

new ZipStream();
new ZipStream({});
new ZipStream({
namePrependSlash: true,
comment: "Foobar",
forceLocalTime: true,
forceZip64: true,
store: true,
level: 123,
});
// Testing Zlib options
new ZipStream({ zlib: { flush: 123 } });
// Testing transform options
new ZipStream({ defaultEncoding: "utf-8" });

const archive = new ZipStream();
archive.entry("abc");
archive.entry("abc", {});
archive.entry("abc", {
type: "directory",
name: "foo/",
namePrependSlash: false,
linkname: "foobar",
date: "2024-10-21",
mode: 123,
store: true,
comment: "Foobar",
});
archive.entry("abc", { date: new Date() });
archive.entry("abc", {}, (err, entry) => {
if (err) throw err;
entry!; // $ExpectType ZipArchiveEntry
});

archive.finalize();

// Examples below adapted from README
// See https://github.com/archiverjs/node-zip-stream?tab=readme-ov-file#usage

archive.on("error", err => {
throw err;
});

archive.entry("string contents", { name: "string.txt" }, (err, entry) => {
if (err) throw err;
archive.entry(null, { name: "directory/" }, (err, entry) => {
if (err) throw err;
archive.finish();
});
});

0 comments on commit 08f9649

Please sign in to comment.