This repository was archived by the owner on Feb 20, 2026. It is now read-only.
forked from archiverjs/node-compress-commons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip-archive-output-stream.test.js
More file actions
172 lines (138 loc) · 4.89 KB
/
Copy pathzip-archive-output-stream.test.js
File metadata and controls
172 lines (138 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { createReadStream, mkdirSync } from "node:fs";
import { Transform } from "node:stream";
import { Readable } from "node:stream";
import { expect, it, beforeAll, describe } from "bun:test";
import { WriteHashStream, binaryBuffer } from "./helpers/index.js";
import {
ZipArchiveEntry,
ZipArchiveOutputStream,
} from "../lib/compress-commons.js";
const testBuffer = binaryBuffer(1024 * 16);
beforeAll(() => {
mkdirSync("tmp", { recursive: true });
});
describe("ZipArchiveOutputStream", () => {
describe("#entry", () => {
it("should append Buffer sources", async () => {
const archive = new ZipArchiveOutputStream();
const testStream = new WriteHashStream("tmp/zip-buffer.zip");
const promise = new Promise((resolve) => {
testStream.on("close", resolve);
});
archive.pipe(testStream);
archive.entry(new ZipArchiveEntry("buffer.txt"), testBuffer).finish();
await promise;
});
it("should append Stream sources", async () => {
const archive = new ZipArchiveOutputStream();
const testStream = new WriteHashStream("tmp/zip-stream.zip");
const promise = new Promise((resolve) => {
testStream.on("close", resolve);
});
archive.pipe(testStream);
archive
.entry(
new ZipArchiveEntry("stream.txt"),
createReadStream("tests/fixtures/test.txt"),
)
.finish();
await promise;
});
it("should append Stream-like sources", async () => {
const archive = new ZipArchiveOutputStream();
const testStream = new WriteHashStream("tmp/zip-stream-like.zip");
const promise = new Promise((resolve) => {
testStream.on("close", resolve);
});
archive.pipe(testStream);
archive
.entry(new ZipArchiveEntry("stream-like.txt"), Readable.from(["it"]))
.finish();
await promise;
});
it("should stop streaming on Stream error", async () => {
const archive = new ZipArchiveOutputStream();
const testStream = new WriteHashStream("tmp/zip-stream-error.zip");
let callbackError = null;
let callbackCalls = 0;
const promise = new Promise((resolve) => {
testStream.on("close", () => {
expect(callbackError?.message).toBe("something went wrong");
expect(callbackCalls).toBe(1);
resolve(undefined);
});
});
archive.pipe(testStream);
const file = new Transform();
archive.entry(new ZipArchiveEntry("stream.txt"), file, (err) => {
callbackCalls++;
callbackError = err;
});
archive.finish();
// Give it a tick to make sure entry is being processed
await Bun.sleep(1);
file.emit("error", new Error("something went wrong"));
await promise;
});
it("should append multiple sources", async () => {
const archive = new ZipArchiveOutputStream();
const testStream = new WriteHashStream("tmp/zip-multiple.zip");
const promise = new Promise((resolve) => {
testStream.on("close", resolve);
});
archive.pipe(testStream);
const entry1 = new ZipArchiveEntry("string.txt");
const entry2 = new ZipArchiveEntry("buffer.txt");
const entry3 = new ZipArchiveEntry("stream.txt");
const entry4 = new ZipArchiveEntry("stream-store.png");
entry4.setMethod(0); // STORE
const entry5 = new ZipArchiveEntry("buffer-store.txt");
entry5.setMethod(0); // STORE
await new Promise((resolve, reject) => {
archive.entry(entry1, "string", (err) => {
if (err) return reject(err);
archive.entry(entry2, testBuffer, (err) => {
if (err) return reject(err);
archive.entry(
entry3,
createReadStream("tests/fixtures/test.txt"),
(err) => {
if (err) return reject(err);
archive.entry(
entry4,
createReadStream("tests/fixtures/image.png"),
(err) => {
if (err) return reject(err);
archive.entry(entry5, testBuffer, (err) => {
if (err) return reject(err);
archive.finish();
resolve(undefined);
});
},
);
},
);
});
});
});
await promise;
});
it("should force ZIP64", async () => {
const archive = new ZipArchiveOutputStream({
forceZip64: true,
});
const testStream = new WriteHashStream("tmp/zip-stream64.zip");
const promise = new Promise((resolve) => {
testStream.on("close", resolve);
});
archive.pipe(testStream);
archive
.entry(
new ZipArchiveEntry("stream.txt"),
createReadStream("tests/fixtures/test.txt"),
)
.finish();
await promise;
});
});
});