Skip to content

Commit 65acac0

Browse files
authored
🤖 Merge PR DefinitelyTyped#75039 [archiver] Update to v8 by @vdistortion
1 parent 121a561 commit 65acac0

4 files changed

Lines changed: 156 additions & 167 deletions

File tree

types/archiver/archiver-tests.ts

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
1-
import Archiver = require("archiver");
1+
import {
2+
Archiver,
3+
type ArchiverError,
4+
ArchiverOptions,
5+
EntryData,
6+
EntryDataFunction,
7+
JsonArchive,
8+
ProgressData,
9+
TarArchive,
10+
ZipArchive,
11+
} from "archiver";
12+
213
import * as fs from "fs";
314
import { Readable, Writable } from "stream";
415

5-
const options: Archiver.ArchiverOptions = {
16+
const options: ArchiverOptions = {
617
statConcurrency: 1,
718
allowHalfOpen: true,
819
readableObjectMode: true,
9-
writeableObjectMode: true,
20+
writableObjectMode: true,
1021
decodeStrings: true,
11-
encoding: "test",
22+
encoding: "utf8",
1223
highWaterMark: 1,
13-
objectmode: true,
24+
objectMode: true,
1425
comment: "test",
1526
forceLocalTime: true,
1627
forceZip64: true,
1728
namePrependSlash: true,
1829
store: true,
30+
level: 9,
1931
zlib: {},
2032
gzip: true,
2133
gzipOptions: {},
2234
};
2335

24-
Archiver("zip", options);
25-
26-
const archiver = Archiver.create("zip");
36+
const zipArchiver: ZipArchive = new ZipArchive(options);
37+
const tarArchiver: TarArchive = new TarArchive({ gzip: true });
38+
const jsonArchiver: JsonArchive = new JsonArchive();
2739

28-
const archiverAsReadable: Readable = archiver;
29-
const archiverAsWritable: Writable = archiver;
40+
const archiver: Archiver = zipArchiver;
41+
const readable: Readable = archiver;
42+
const writable: Writable = archiver;
3043

3144
const writeStream = fs.createWriteStream("./archiver.d.ts");
3245
const readStream = fs.createReadStream("./archiver.d.ts");
@@ -38,54 +51,53 @@ archiver.append(readStream, { name: "archiver.d.ts" });
3851
archiver.append(readStream, { name: "buffer.txt", date: "05/05/1991" });
3952
archiver.append(readStream, { name: "buffer.txt", date: new Date() });
4053
archiver.append(readStream, { name: "buffer.txt", mode: 1 });
41-
archiver.append(readStream, { name: "buffer.txt", mode: 1, stats: ({} as fs.Stats) });
54+
archiver.append(readStream, {
55+
name: "buffer.txt",
56+
mode: 1,
57+
stats: {} as fs.Stats,
58+
});
4259
archiver.append("Some content", { name: "filename", store: true });
43-
archiver.append(readStream, { name: "archiver.d.ts" }).append(readStream, { name: "archiver.d.ts" });
60+
archiver
61+
.append(readStream, { name: "archiver.d.ts" })
62+
.append(readStream, { name: "archiver.d.ts" });
4463
archiver.append(Readable.from(["test"]), { name: "buffer.txt", date: new Date() });
4564

4665
archiver.directory("./path", "./someOtherPath");
4766
archiver.directory("./", "", {});
4867
archiver.directory("./", false, { name: "test" });
49-
archiver.directory("./", false, (entry: Archiver.EntryData) => {
68+
archiver.directory("./", false, (entry: EntryData) => {
5069
entry.name = "foobar";
5170
return entry;
5271
});
53-
archiver.directory("./", false, (entry: Archiver.EntryData) => false);
54-
55-
archiver.append(readStream, {
56-
name: "sub/folder.xml",
57-
});
58-
59-
archiver.glob("**", {
60-
cwd: "path/to/files",
61-
});
62-
archiver.glob("./path", {}, {});
72+
archiver.directory("./", false, (entry: EntryData) => false);
6373

74+
archiver.file("./path");
6475
archiver.file("./path", { name: "test" });
6576

66-
archiver.setFormat("zip");
67-
archiver.setModule(() => {});
77+
archiver.glob("**", { cwd: "path/to/files" });
78+
archiver.glob("./path", {}, {});
6879

6980
archiver.pointer();
70-
archiver.use(() => {});
7181

7282
archiver.finalize(); // $ExpectType Promise<void>
7383

7484
archiver.symlink("./path", "./target");
85+
archiver.symlink("directory/directory", "../../directory", 493);
7586

76-
function fakeHandler(err: Archiver.ArchiverError) {
87+
function fakeHandler(err: ArchiverError) {
7788
console.log(err.code);
7889
console.log(err.message);
7990
console.log(err.stack);
8091
console.log(err.data);
8192
}
8293

83-
const fakeError = new Archiver.ArchiverError("code", "foo");
84-
8594
archiver.on("error", fakeHandler);
8695
archiver.on("warning", fakeHandler);
8796

8897
archiver.on("data", (chunk: Buffer) => console.log(chunk));
89-
90-
Archiver.isRegisteredFormat("zip"); // $ExpectType boolean
91-
archiver.symlink("directory/directory", "../../directory", 493); // $ExpectType Archiver
98+
archiver.on("progress", (progress: ProgressData) => {
99+
console.log(progress.entries.total);
100+
});
101+
archiver.on("entry", (entry: EntryData) => {
102+
console.log(entry.name);
103+
});

types/archiver/index.d.ts

Lines changed: 103 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,108 @@
11
import * as fs from "fs";
2-
import ReaddirGlob = require("readdir-glob");
3-
import stream = require("stream");
2+
import * as ReaddirGlob from "readdir-glob";
3+
import * as stream from "stream";
44
import { ZlibOptions } from "zlib";
55

6-
type Partial<T> = {
7-
[P in keyof T]?: T[P];
8-
};
9-
10-
// This library adds `cwd` to the options
11-
type GlobOptions = ReaddirGlob.Options & { cwd?: string };
12-
13-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type -- support for ConstructorFn function and classes
14-
type ConstructorFn<T> = Function | (new(...params: any[]) => T);
15-
16-
declare function archiver(format: archiver.Format, options?: archiver.ArchiverOptions): archiver.Archiver;
17-
18-
declare namespace archiver {
19-
type Format = "zip" | "tar";
20-
21-
function create(format: string, options?: ArchiverOptions): Archiver;
22-
23-
/** Check if the format is already registered. */
24-
function isRegisteredFormat(format: string): boolean;
25-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
26-
function registerFormat(format: string, module: Function): void;
27-
28-
interface EntryData {
29-
/** Sets the entry name including internal path */
30-
name: string;
31-
/** Sets the entry date */
32-
date?: Date | string | undefined;
33-
/** Sets the entry permissions */
34-
mode?: number | undefined;
35-
/**
36-
* Sets a path prefix for the entry name.
37-
* Useful when working with methods like `directory` or `glob`
38-
*/
39-
prefix?: string | undefined;
40-
/**
41-
* Sets the fs stat data for this entry allowing
42-
* for reduction of fs stat calls when stat data is already known
43-
*/
44-
stats?: fs.Stats | undefined;
45-
}
46-
47-
interface ZipEntryData extends EntryData {
48-
/** Sets the compression method to STORE */
49-
store?: boolean | undefined;
50-
}
51-
52-
type TarEntryData = EntryData;
53-
54-
interface ProgressData {
55-
entries: {
56-
total: number;
57-
processed: number;
58-
};
59-
fs: {
60-
totalBytes: number;
61-
processedBytes: number;
62-
};
63-
}
64-
65-
/** A function that lets you either opt out of including an entry (by returning false), or modify the contents of an entry as it is added (by returning an EntryData) */
66-
type EntryDataFunction = (entry: EntryData) => false | EntryData;
67-
68-
class ArchiverError extends Error {
69-
code: string; // Since archiver format support is modular, we cannot enumerate all possible error codes, as the modules can throw arbitrary ones.
70-
data: any;
71-
path?: any;
72-
73-
constructor(code: string, data: any);
74-
}
75-
76-
interface Archiver extends stream.Transform {
77-
abort(): this;
78-
append(source: stream.Readable | Buffer | string, data?: EntryData | ZipEntryData | TarEntryData): this;
79-
80-
/** if false is passed for destpath, the path of a chunk of data in the archive is set to the root */
81-
directory(dirpath: string, destpath: false | string, data?: Partial<EntryData> | EntryDataFunction): this;
82-
file(filename: string, data: EntryData): this;
83-
glob(pattern: string, options?: GlobOptions, data?: Partial<EntryData>): this;
84-
finalize(): Promise<void>;
85-
86-
setFormat(format: string): this;
87-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
88-
setModule(module: Function): this;
89-
90-
pointer(): number;
91-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
92-
use(plugin: Function): this;
93-
94-
symlink(filepath: string, target: string, mode?: number): this;
95-
96-
on(event: "error" | "warning", listener: (error: ArchiverError) => void): this;
97-
on(event: "data", listener: (data: Buffer) => void): this;
98-
on(event: "progress", listener: (progress: ProgressData) => void): this;
99-
on(event: "close" | "drain" | "finish", listener: () => void): this;
100-
on(event: "pipe" | "unpipe", listener: (src: stream.Readable) => void): this;
101-
on(event: "entry", listener: (entry: EntryData) => void): this;
102-
on(event: string, listener: (...args: any[]) => void): this;
103-
}
104-
105-
type ArchiverOptions = CoreOptions & TransformOptions & ZipOptions & TarOptions;
106-
107-
interface CoreOptions {
108-
statConcurrency?: number | undefined;
109-
}
110-
111-
interface TransformOptions {
112-
allowHalfOpen?: boolean | undefined;
113-
readableObjectMode?: boolean | undefined;
114-
writeableObjectMode?: boolean | undefined;
115-
decodeStrings?: boolean | undefined;
116-
encoding?: string | undefined;
117-
highWaterMark?: number | undefined;
118-
objectmode?: boolean | undefined;
119-
}
120-
121-
interface ZipOptions {
122-
comment?: string | undefined;
123-
forceLocalTime?: boolean | undefined;
124-
forceZip64?: boolean | undefined;
125-
/** @default false */
126-
namePrependSlash?: boolean | undefined;
127-
store?: boolean | undefined;
128-
zlib?: ZlibOptions | undefined;
129-
}
130-
131-
interface TarOptions {
132-
gzip?: boolean | undefined;
133-
gzipOptions?: ZlibOptions | undefined;
134-
}
6+
export type GlobOptions = ReaddirGlob.Options & { cwd?: string };
7+
8+
export interface EntryData {
9+
name: string;
10+
type?: "directory" | "file" | "symlink";
11+
date?: Date | string;
12+
mode?: number;
13+
prefix?: string;
14+
stats?: fs.Stats;
15+
}
16+
17+
export interface ZipEntryData extends EntryData {
18+
store?: boolean;
19+
comment?: string;
20+
namePrependSlash?: boolean;
21+
}
22+
23+
export type TarEntryData = EntryData;
24+
25+
export interface ProgressData {
26+
entries: {
27+
total: number;
28+
processed: number;
29+
};
30+
fs: {
31+
totalBytes: number;
32+
processedBytes: number;
33+
};
34+
}
35+
36+
export type EntryDataFunction = (entry: EntryData) => false | EntryData;
37+
38+
export interface ArchiverError extends Error {
39+
code: string;
40+
data?: any;
41+
}
42+
43+
export class Archiver extends stream.Transform {
44+
constructor(options?: CoreOptions & TransformOptions);
45+
abort(): this;
46+
append(source: stream.Readable | Buffer | string, data?: EntryData | ZipEntryData | TarEntryData): this;
47+
directory(
48+
dirpath: string,
49+
destpath: false | string,
50+
data?: Partial<EntryData> | EntryDataFunction,
51+
): this;
52+
/** @param data - entry data (optional) */
53+
file(filename: string, data?: EntryData): this;
54+
glob(pattern: string, options?: GlobOptions, data?: Partial<EntryData>): this;
55+
finalize(): Promise<void>;
56+
pointer(): number;
57+
symlink(filepath: string, target: string, mode?: number): this;
58+
on(event: "error" | "warning", listener: (error: ArchiverError) => void): this;
59+
on(event: "data", listener: (data: Buffer) => void): this;
60+
on(event: "progress", listener: (progress: ProgressData) => void): this;
61+
on(event: "close" | "drain" | "finish", listener: () => void): this;
62+
on(event: "pipe" | "unpipe", listener: (src: stream.Readable) => void): this;
63+
on(event: "entry", listener: (entry: EntryData) => void): this;
64+
on(event: string, listener: (...args: any[]) => void): this;
65+
}
66+
67+
export interface CoreOptions {
68+
statConcurrency?: number;
13569
}
13670

137-
export = archiver;
71+
export interface TransformOptions {
72+
allowHalfOpen?: boolean;
73+
readableObjectMode?: boolean;
74+
writableObjectMode?: boolean;
75+
decodeStrings?: boolean;
76+
encoding?: BufferEncoding;
77+
highWaterMark?: number;
78+
objectMode?: boolean;
79+
}
80+
81+
export interface ZipOptions {
82+
comment?: string;
83+
forceLocalTime?: boolean;
84+
forceZip64?: boolean;
85+
namePrependSlash?: boolean;
86+
store?: boolean;
87+
level?: number;
88+
zlib?: ZlibOptions;
89+
}
90+
91+
export interface TarOptions {
92+
gzip?: boolean;
93+
gzipOptions?: ZlibOptions;
94+
}
95+
96+
export type ArchiverOptions = CoreOptions & TransformOptions & ZipOptions & TarOptions;
97+
98+
export class ZipArchive extends Archiver {
99+
constructor(options?: CoreOptions & TransformOptions & ZipOptions);
100+
}
101+
102+
export class TarArchive extends Archiver {
103+
constructor(options?: CoreOptions & TransformOptions & TarOptions);
104+
}
105+
106+
export class JsonArchive extends Archiver {
107+
constructor(options?: CoreOptions & TransformOptions);
108+
}

types/archiver/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"private": true,
33
"name": "@types/archiver",
4-
"version": "7.0.9999",
4+
"version": "8.0.9999",
55
"projects": [
66
"https://github.com/archiverjs/node-archiver"
77
],
8+
"type": "module",
89
"dependencies": {
10+
"@types/node": "*",
911
"@types/readdir-glob": "*"
1012
},
1113
"devDependencies": {
@@ -23,6 +25,10 @@
2325
{
2426
"name": "Piotr Błażejewicz",
2527
"githubUsername": "peterblazejewicz"
28+
},
29+
{
30+
"name": "Valentin Zolotov",
31+
"githubUsername": "vdistortion"
2632
}
2733
]
2834
}

0 commit comments

Comments
 (0)