Skip to content

Commit 81301a6

Browse files
committed
throw errors on file not found in flush()
1 parent 1c667af commit 81301a6

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

src/read.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ const uint16LittleEndian = (b: Uint8Array, n: number): number => {
2525
return b1 | (b2 << 8);
2626
};
2727

28-
type FileLike = Pick<File, 'slice' | 'stream' | 'arrayBuffer' | 'size'>;
28+
/**
29+
* FileLike is the input argument to the Reader.
30+
* It is essentially `Blob`, and both `Blob` and `File` are assignable to FileLike.
31+
*/
32+
type FileLike = File | Blob;
2933

3034
class Entry {
3135
dataView: DataView;

src/write.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,27 +157,33 @@ class ZipTransformer {
157157

158158
for (const fileName of Object.keys(this.files)) {
159159
file = this.files[fileName];
160-
if (file) {
161-
length += 46 + file.nameBuf.length + file.comment.length;
160+
if (!file) {
161+
throw new TypeError(
162+
`File not found while flushing ZipTransformer: ${fileName}`,
163+
);
162164
}
165+
length += 46 + file.nameBuf.length + file.comment.length;
163166
}
164167

165168
const data = new Uint8Array(length + 22);
166169
const dv = new DataView(data.buffer);
167170

168171
for (const fileName of Object.keys(this.files)) {
169172
file = this.files[fileName];
170-
if (file) {
171-
dv.setUint32(index, 0x50_4b_01_02);
172-
dv.setUint16(index + 4, 0x14_00);
173-
dv.setUint16(index + 32, file.comment.length, true);
174-
dv.setUint8(index + 38, file.directory ? 16 : 0);
175-
dv.setUint32(index + 42, JSBI.toNumber(file.offset), true);
176-
data.set(file.header, index + 6);
177-
data.set(file.nameBuf, index + 46);
178-
data.set(file.comment, index + 46 + file.nameBuf.length);
179-
index += 46 + file.nameBuf.length + file.comment.length;
173+
if (!file) {
174+
throw new TypeError(
175+
`File not found while flushing ZipTransformer: ${fileName}`,
176+
);
180177
}
178+
dv.setUint32(index, 0x50_4b_01_02);
179+
dv.setUint16(index + 4, 0x14_00);
180+
dv.setUint16(index + 32, file.comment.length, true);
181+
dv.setUint8(index + 38, file.directory ? 16 : 0);
182+
dv.setUint32(index + 42, JSBI.toNumber(file.offset), true);
183+
data.set(file.header, index + 6);
184+
data.set(file.nameBuf, index + 46);
185+
data.set(file.comment, index + 46 + file.nameBuf.length);
186+
index += 46 + file.nameBuf.length + file.comment.length;
181187
}
182188

183189
dv.setUint32(index, 0x50_4b_05_06);

0 commit comments

Comments
 (0)