-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathwrite.test.js
More file actions
284 lines (230 loc) · 8.01 KB
/
write.test.js
File metadata and controls
284 lines (230 loc) · 8.01 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import test from 'tape';
import { Writer, Reader } from '../src/index.js';
// Constants
const ABORT_MESSAGE = 'This is a bad stream';
function writeStuff(writable, inputReadable) {
const writer = writable.getWriter();
writer.write({
name: "/chonks.txt",
stream: () => inputReadable,
});
writer.close();
return writer;
}
function getInputReadable({
chunks = [[1, 2, 3, 4], [5, 6, 7, 8]],
abort = false,
long = false,
} = {}) {
let cancelReason;
const inputReadable = new ReadableStream({
start(ctrl) {
if (long) {
for (let x = 0; x < 1000; x++) ctrl.enqueue(chunks[0]);
}
if (abort) {
ctrl.error(ABORT_MESSAGE);
} else {
chunks.forEach((chunk) => ctrl.enqueue(chunk));
ctrl.close();
}
},
cancel(reason) {
cancelReason = reason;
}
});
inputReadable.cancelReason = cancelReason;
return inputReadable;
}
function getOutputWritable({ abort = false } = {}) {
let chunks = [];
let abortReason;
let closed;
const outputWritable = new WritableStream({
start(ctrl) {
closed = false;
if (abort) ctrl.error(ABORT_MESSAGE);
},
write(chunk) {
chunks.push(chunk);
},
abort(reason) {
abortReason = reason;
},
close() {
closed = true
}
});
outputWritable.outputChunks = chunks;
outputWritable.abortReason = abortReason;
outputWritable.closed = closed;
return outputWritable;
}
const date = +new Date('2012-02-05T15:40:48Z');
const dateMS = +new Date('2012-02-05T15:40:48.123Z');
const helloWorld = new File(['Hello World\n'], 'Hello.txt', {
lastModified: dateMS,
});
const fileLikeUtf8 = {
comment: `I'm a entry comment with utf8`,
stream: () => new Response('€15\n').body,
name: '€15.txt',
lastModified: new Date('2020-01-27T16:55:59'),
};
const folder = { name: 'folder/', directory: true };
test('Writing - All in one big test', async (t) => {
const { readable, writable } = new Writer();
const writer = writable.getWriter();
writer.write(helloWorld);
writer.write(fileLikeUtf8);
writer.write(folder);
writer.close();
const chunks = [];
const reader = readable.getReader();
for (;;) {
const v = await reader.read();
if (v.done) break;
chunks.push(v.value);
}
let entry;
const it = Reader(new Blob(chunks));
// entry 1, Writer accepts native File object
entry = (await it.next()).value;
t.equal(entry.versionMadeBy, 20, 'versionMadeBy should be 20');
t.equal(entry.versionNeeded, 20, 'versionNeeded should be 20');
t.equal(entry.bitFlag, 2056, 'bitflag should be 2056');
t.equal(entry.encrypted, false, 'entry is not encrypted');
t.equal(entry.compressionMethod, 0, 'entry has no compression');
t.equal(entry.crc32, 2962613731, 'crc checksum should be 2962613731');
t.equal(
entry.compressedSize,
12,
'compressed size should be same as size b/c no compression',
);
t.equal(entry.filenameLength, 9, 'filenameLength is correct');
t.equal(entry.extraFieldLength, 0, 'entry have no extra fields');
t.equal(entry.commentLength, 0, 'native file object dont have comments');
t.equal(entry.diskNumberStart, 0, 'diskNumberStart is 0');
t.equal(entry.internalFileAttributes, 0, 'internalFileAttributes is 0');
t.equal(entry.externalFileAttributes, 0, 'externalFileAttributes is 0');
t.equal(entry.directory, false, 'directory should be false');
t.equal(entry.offset, 0, 'first entry starts at offset 0');
t.equal(entry.zip64, false, 'small entries are not zip64');
t.equal(entry.comment, '', 'helloWorld have no comments');
t.equal(+entry.lastModifiedDate, date, 'time is the same');
t.equal(entry.lastModified, date, 'entries should not store ms');
t.equal(entry.name, 'Hello.txt', 'entry name should be Hello.txt');
t.equal(entry.size, 12, 'entry size should be 12');
t.equal(
(await entry.arrayBuffer()).byteLength,
12,
'it can return an arrayBuffer',
);
t.equal(await entry.text(), 'Hello World\n', 'getting the text is accurate');
// entry 2, The rest should be similar to entry 1 (test the differences is enough)
entry = (await it.next()).value;
t.equal(entry.name, '€15.txt', 'Name with utf8 chars works');
t.equal(
entry.comment,
`I'm a entry comment with utf8`,
`Entry should have a comment`,
);
t.equal(await entry.text(), '€15\n', 'Text should be the same');
t.equal(entry.filenameLength, 9, '€ takes up 3 bytes');
t.ok(entry.offset > 30, '2nd entry should not have the same offset as first');
// entry 3, it can create folders
entry = (await it.next()).value;
t.equal(entry.directory, true, 'Entry should be a directory');
t.equal(entry.name, 'folder/', 'Entry name should be folder/');
t.end();
});
test('Writing - from custom stream', async (t) => {
const { readable, writable } = new Writer();
const inputChunks = [[1, 2, 3, 4], [5, 6, 7, 8]];
const inputReadable = getInputReadable({ inputChunks })
const writer = writeStuff(writable, inputReadable);
const outputWritable = getOutputWritable();
await readable.pipeTo(outputWritable);
// Extract stored chunks off custom property on stream object
let { outputChunks } = outputWritable;
// Remove header and footer
outputChunks = outputChunks.slice(1, inputChunks.length + 1);
t.deepEqual(outputChunks, inputChunks);
});
test('Writing - abort input readable stream', async (t) => {
const { readable, writable } = new Writer();
const inputReadable = getInputReadable({ abort: true });
const writer = writeStuff(writable, inputReadable);
const outputWritable = getOutputWritable();
try {
await readable.pipeTo(outputWritable);
} catch(error) {
t.equals(error, ABORT_MESSAGE);
}
});
test('Writing - cancel input readable stream', async (t) => {
const { readable, writable } = new Writer();
const inputReadable = getInputReadable();
inputReadable.cancel(ABORT_MESSAGE);
const writer = writeStuff(writable, inputReadable);
const outputWritable = getOutputWritable();
try {
await readable.pipeTo(outputWritable);
} catch(error) {
const { cancelReason } = inputReadable;
const { abortReason } = outputWritable;
t.equals(error, ABORT_MESSAGE);
t.equals(abortReason, ABORT_MESSAGE);
t.equals(cancelReason, ABORT_MESSAGE);
}
});
test('Writing - abort output writable stream', async (t) => {
const { readable, writable } = new Writer();
const inputReadable = getInputReadable();
const writer = writeStuff(writable, inputReadable);
const outputWritable = getOutputWritable({ long: true });
try {
const promise = readable.pipeTo(outputWritable);
outputWritable.abort(ABORT_MESSAGE);
await promise;
} catch(error) {
const { cancelReason } = inputReadable;
const { abortReason } = outputWritable;
t.equals(error, ABORT_MESSAGE);
t.equals(abortReason, ABORT_MESSAGE);
t.equals(cancelReason, ABORT_MESSAGE);
}
});
test('Writing - early close output writable stream', async (t) => {
const { readable, writable } = new Writer();
const inputReadable = getInputReadable();
const writer = writeStuff(writable, inputReadable);
const outputWritable = getOutputWritable({ long: true });
try {
const promise = readable.pipeTo(outputWritable);
outputWritable.close();
await promise;
} catch(error) {
const { cancelReason } = inputReadable;
const { abortReason } = outputWritable;
t.equals(error, ABORT_MESSAGE);
t.equals(abortReason, ABORT_MESSAGE);
t.equals(cancelReason, ABORT_MESSAGE);
}
});
test('Writing - error inoutput writable stream', async (t) => {
const { readable, writable } = new Writer();
const inputReadable = getInputReadable();
const writer = writeStuff(writable, inputReadable);
const outputWritable = getOutputWritable({ abort: true });
try {
const promise = readable.pipeTo(outputWritable);
await promise;
} catch(error) {
const { cancelReason } = inputReadable;
const { abortReason } = outputWritable;
t.equals(error, ABORT_MESSAGE);
// t.equals(abortReason, ABORT_MESSAGE);
// t.equals(cancelReason, ABORT_MESSAGE);
}
});