@remotion/renderer 4.0.462 → 4.0.463
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/BrowserFetcher.js +2 -5
- package/dist/browser/extract-zip-archive.d.ts +1 -0
- package/dist/browser/extract-zip-archive.js +144 -0
- package/dist/esm/index.mjs +1899 -354
- package/package.json +13 -14
- package/vendor/yauzl-patched/LICENSE +21 -0
- package/vendor/yauzl-patched/buffer-crc32.js +111 -0
- package/vendor/yauzl-patched/fd-slicer.js +326 -0
- package/vendor/yauzl-patched/index.d.ts +23 -0
- package/vendor/yauzl-patched/index.js +969 -0
- package/vendor/yauzl-patched/pend.js +55 -0
- package/dist/options/experimental-visual-mode.d.ts +0 -16
- package/dist/options/experimental-visual-mode.js +0 -30
|
@@ -0,0 +1,969 @@
|
|
|
1
|
+
var fs = require("fs");
|
|
2
|
+
var zlib = require("zlib");
|
|
3
|
+
var fd_slicer = require("./fd-slicer");
|
|
4
|
+
var crc32 = require("./buffer-crc32");
|
|
5
|
+
var util = require("util");
|
|
6
|
+
var EventEmitter = require("events").EventEmitter;
|
|
7
|
+
var Transform = require("stream").Transform;
|
|
8
|
+
var PassThrough = require("stream").PassThrough;
|
|
9
|
+
var Writable = require("stream").Writable;
|
|
10
|
+
|
|
11
|
+
exports.open = open;
|
|
12
|
+
exports.fromFd = fromFd;
|
|
13
|
+
exports.fromBuffer = fromBuffer;
|
|
14
|
+
exports.fromRandomAccessReader = fromRandomAccessReader;
|
|
15
|
+
exports.dosDateTimeToDate = dosDateTimeToDate;
|
|
16
|
+
exports.getFileNameLowLevel = getFileNameLowLevel;
|
|
17
|
+
exports.validateFileName = validateFileName;
|
|
18
|
+
exports.parseExtraFields = parseExtraFields;
|
|
19
|
+
exports.ZipFile = ZipFile;
|
|
20
|
+
exports.Entry = Entry;
|
|
21
|
+
exports.LocalFileHeader = LocalFileHeader;
|
|
22
|
+
exports.RandomAccessReader = RandomAccessReader;
|
|
23
|
+
|
|
24
|
+
function open(path, options, callback) {
|
|
25
|
+
if (typeof options === "function") {
|
|
26
|
+
callback = options;
|
|
27
|
+
options = null;
|
|
28
|
+
}
|
|
29
|
+
if (options == null) options = {};
|
|
30
|
+
if (options.autoClose == null) options.autoClose = true;
|
|
31
|
+
if (options.lazyEntries == null) options.lazyEntries = false;
|
|
32
|
+
if (options.decodeStrings == null) options.decodeStrings = true;
|
|
33
|
+
if (options.validateEntrySizes == null) options.validateEntrySizes = true;
|
|
34
|
+
if (options.strictFileNames == null) options.strictFileNames = false;
|
|
35
|
+
if (callback == null) callback = defaultCallback;
|
|
36
|
+
fs.open(path, "r", function(err, fd) {
|
|
37
|
+
if (err) return callback(err);
|
|
38
|
+
fromFd(fd, options, function(err, zipfile) {
|
|
39
|
+
if (err) fs.close(fd, defaultCallback);
|
|
40
|
+
callback(err, zipfile);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function fromFd(fd, options, callback) {
|
|
46
|
+
if (typeof options === "function") {
|
|
47
|
+
callback = options;
|
|
48
|
+
options = null;
|
|
49
|
+
}
|
|
50
|
+
if (options == null) options = {};
|
|
51
|
+
if (options.autoClose == null) options.autoClose = false;
|
|
52
|
+
if (options.lazyEntries == null) options.lazyEntries = false;
|
|
53
|
+
if (options.decodeStrings == null) options.decodeStrings = true;
|
|
54
|
+
if (options.validateEntrySizes == null) options.validateEntrySizes = true;
|
|
55
|
+
if (options.strictFileNames == null) options.strictFileNames = false;
|
|
56
|
+
if (callback == null) callback = defaultCallback;
|
|
57
|
+
fs.fstat(fd, function(err, stats) {
|
|
58
|
+
if (err) return callback(err);
|
|
59
|
+
var reader = fd_slicer.createFromFd(fd, {autoClose: true});
|
|
60
|
+
fromRandomAccessReader(reader, stats.size, options, callback);
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function fromBuffer(buffer, options, callback) {
|
|
65
|
+
if (typeof options === "function") {
|
|
66
|
+
callback = options;
|
|
67
|
+
options = null;
|
|
68
|
+
}
|
|
69
|
+
if (options == null) options = {};
|
|
70
|
+
options.autoClose = false;
|
|
71
|
+
if (options.lazyEntries == null) options.lazyEntries = false;
|
|
72
|
+
if (options.decodeStrings == null) options.decodeStrings = true;
|
|
73
|
+
if (options.validateEntrySizes == null) options.validateEntrySizes = true;
|
|
74
|
+
if (options.strictFileNames == null) options.strictFileNames = false;
|
|
75
|
+
// limit the max chunk size. see https://github.com/thejoshwolfe/yauzl/issues/87
|
|
76
|
+
var reader = fd_slicer.createFromBuffer(buffer, {maxChunkSize: 0x10000});
|
|
77
|
+
fromRandomAccessReader(reader, buffer.length, options, callback);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function fromRandomAccessReader(reader, totalSize, options, callback) {
|
|
81
|
+
if (typeof options === "function") {
|
|
82
|
+
callback = options;
|
|
83
|
+
options = null;
|
|
84
|
+
}
|
|
85
|
+
if (options == null) options = {};
|
|
86
|
+
if (options.autoClose == null) options.autoClose = true;
|
|
87
|
+
if (options.lazyEntries == null) options.lazyEntries = false;
|
|
88
|
+
if (options.decodeStrings == null) options.decodeStrings = true;
|
|
89
|
+
var decodeStrings = !!options.decodeStrings;
|
|
90
|
+
if (options.validateEntrySizes == null) options.validateEntrySizes = true;
|
|
91
|
+
if (options.strictFileNames == null) options.strictFileNames = false;
|
|
92
|
+
if (callback == null) callback = defaultCallback;
|
|
93
|
+
if (typeof totalSize !== "number") throw new Error("expected totalSize parameter to be a number");
|
|
94
|
+
if (totalSize > Number.MAX_SAFE_INTEGER) {
|
|
95
|
+
throw new Error("zip file too large. only file sizes up to 2^52 are supported due to JavaScript's Number type being an IEEE 754 double.");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// the matching unref() call is in zipfile.close()
|
|
99
|
+
reader.ref();
|
|
100
|
+
|
|
101
|
+
// eocdr means End of Central Directory Record.
|
|
102
|
+
// search backwards for the eocdr signature.
|
|
103
|
+
// the last field of the eocdr is a variable-length comment.
|
|
104
|
+
// the comment size is encoded in a 2-byte field in the eocdr, which we can't find without trudging backwards through the comment to find it.
|
|
105
|
+
// as a consequence of this design decision, it's possible to have ambiguous zip file metadata if a coherent eocdr was in the comment.
|
|
106
|
+
// we search backwards for a eocdr signature, and hope that whoever made the zip file was smart enough to forbid the eocdr signature in the comment.
|
|
107
|
+
var eocdrWithoutCommentSize = 22;
|
|
108
|
+
var zip64EocdlSize = 20; // Zip64 end of central directory locator
|
|
109
|
+
var maxCommentSize = 0xffff; // 2-byte size
|
|
110
|
+
var bufferSize = Math.min(zip64EocdlSize + eocdrWithoutCommentSize + maxCommentSize, totalSize);
|
|
111
|
+
var buffer = newBuffer(bufferSize);
|
|
112
|
+
var bufferReadStart = totalSize - buffer.length;
|
|
113
|
+
readAndAssertNoEof(reader, buffer, 0, bufferSize, bufferReadStart, function(err) {
|
|
114
|
+
if (err) return callback(err);
|
|
115
|
+
for (var i = bufferSize - eocdrWithoutCommentSize; i >= 0; i -= 1) {
|
|
116
|
+
if (buffer.readUInt32LE(i) !== 0x06054b50) continue;
|
|
117
|
+
// found eocdr
|
|
118
|
+
var eocdrBuffer = buffer.subarray(i);
|
|
119
|
+
|
|
120
|
+
// 0 - End of central directory signature = 0x06054b50
|
|
121
|
+
// 4 - Number of this disk
|
|
122
|
+
var diskNumber = eocdrBuffer.readUInt16LE(4);
|
|
123
|
+
// 6 - Disk where central directory starts
|
|
124
|
+
// 8 - Number of central directory records on this disk
|
|
125
|
+
// 10 - Total number of central directory records
|
|
126
|
+
var entryCount = eocdrBuffer.readUInt16LE(10);
|
|
127
|
+
// 12 - Size of central directory (bytes)
|
|
128
|
+
// 16 - Offset of start of central directory, relative to start of archive
|
|
129
|
+
var centralDirectoryOffset = eocdrBuffer.readUInt32LE(16);
|
|
130
|
+
// 20 - Comment length
|
|
131
|
+
var commentLength = eocdrBuffer.readUInt16LE(20);
|
|
132
|
+
var expectedCommentLength = eocdrBuffer.length - eocdrWithoutCommentSize;
|
|
133
|
+
if (commentLength !== expectedCommentLength) {
|
|
134
|
+
return callback(new Error("Invalid comment length. Expected: " + expectedCommentLength + ". Found: " + commentLength + ". Are there extra bytes at the end of the file? Or is the end of central dir signature `PK☺☻` in the comment?"));
|
|
135
|
+
}
|
|
136
|
+
// 22 - Comment
|
|
137
|
+
// the encoding is always cp437.
|
|
138
|
+
var comment = decodeStrings ? decodeBuffer(eocdrBuffer.subarray(22), false)
|
|
139
|
+
: eocdrBuffer.subarray(22);
|
|
140
|
+
|
|
141
|
+
// Look for a Zip64 end of central directory locator
|
|
142
|
+
if (i - zip64EocdlSize >= 0 && buffer.readUInt32LE(i - zip64EocdlSize) === 0x07064b50) {
|
|
143
|
+
// ZIP64 format
|
|
144
|
+
var zip64EocdlBuffer = buffer.subarray(i - zip64EocdlSize, i - zip64EocdlSize + zip64EocdlSize);
|
|
145
|
+
// 0 - zip64 end of central dir locator signature = 0x07064b50
|
|
146
|
+
// 4 - number of the disk with the start of the zip64 end of central directory
|
|
147
|
+
// 8 - relative offset of the zip64 end of central directory record
|
|
148
|
+
var zip64EocdrOffset = readUInt64LE(zip64EocdlBuffer, 8);
|
|
149
|
+
// 16 - total number of disks
|
|
150
|
+
|
|
151
|
+
// ZIP64 end of central directory record
|
|
152
|
+
var zip64EocdrBuffer = newBuffer(56);
|
|
153
|
+
return readAndAssertNoEof(reader, zip64EocdrBuffer, 0, zip64EocdrBuffer.length, zip64EocdrOffset, function(err) {
|
|
154
|
+
if (err) return callback(err);
|
|
155
|
+
|
|
156
|
+
// 0 - zip64 end of central dir signature 4 bytes (0x06064b50)
|
|
157
|
+
if (zip64EocdrBuffer.readUInt32LE(0) !== 0x06064b50) {
|
|
158
|
+
return callback(new Error("invalid zip64 end of central directory record signature"));
|
|
159
|
+
}
|
|
160
|
+
// 4 - size of zip64 end of central directory record 8 bytes
|
|
161
|
+
// 12 - version made by 2 bytes
|
|
162
|
+
// 14 - version needed to extract 2 bytes
|
|
163
|
+
// 16 - number of this disk 4 bytes
|
|
164
|
+
diskNumber = zip64EocdrBuffer.readUInt32LE(16);
|
|
165
|
+
if (diskNumber !== 0) {
|
|
166
|
+
// Check this only after zip64 overrides. See #118.
|
|
167
|
+
return callback(new Error("multi-disk zip files are not supported: found disk number: " + diskNumber));
|
|
168
|
+
}
|
|
169
|
+
// 20 - number of the disk with the start of the central directory 4 bytes
|
|
170
|
+
// 24 - total number of entries in the central directory on this disk 8 bytes
|
|
171
|
+
// 32 - total number of entries in the central directory 8 bytes
|
|
172
|
+
entryCount = readUInt64LE(zip64EocdrBuffer, 32);
|
|
173
|
+
// 40 - size of the central directory 8 bytes
|
|
174
|
+
// 48 - offset of start of central directory with respect to the starting disk number 8 bytes
|
|
175
|
+
centralDirectoryOffset = readUInt64LE(zip64EocdrBuffer, 48);
|
|
176
|
+
// 56 - zip64 extensible data sector (variable size)
|
|
177
|
+
return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries, decodeStrings, options.validateEntrySizes, options.strictFileNames));
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Not ZIP64 format
|
|
182
|
+
if (diskNumber !== 0) {
|
|
183
|
+
return callback(new Error("multi-disk zip files are not supported: found disk number: " + diskNumber));
|
|
184
|
+
}
|
|
185
|
+
return callback(null, new ZipFile(reader, centralDirectoryOffset, totalSize, entryCount, comment, options.autoClose, options.lazyEntries, decodeStrings, options.validateEntrySizes, options.strictFileNames));
|
|
186
|
+
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Not a zip file.
|
|
190
|
+
callback(new Error("End of central directory record signature not found. Either not a zip file, or file is truncated."));
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
util.inherits(ZipFile, EventEmitter);
|
|
195
|
+
function ZipFile(reader, centralDirectoryOffset, fileSize, entryCount, comment, autoClose, lazyEntries, decodeStrings, validateEntrySizes, strictFileNames) {
|
|
196
|
+
var self = this;
|
|
197
|
+
EventEmitter.call(self);
|
|
198
|
+
self.reader = reader;
|
|
199
|
+
// forward close events
|
|
200
|
+
self.reader.on("error", function(err) {
|
|
201
|
+
// error closing the fd
|
|
202
|
+
emitError(self, err);
|
|
203
|
+
});
|
|
204
|
+
self.reader.once("close", function() {
|
|
205
|
+
self.emit("close");
|
|
206
|
+
});
|
|
207
|
+
self.readEntryCursor = centralDirectoryOffset;
|
|
208
|
+
self.fileSize = fileSize;
|
|
209
|
+
self.entryCount = entryCount;
|
|
210
|
+
self.comment = comment;
|
|
211
|
+
self.entriesRead = 0;
|
|
212
|
+
self.autoClose = !!autoClose;
|
|
213
|
+
self.lazyEntries = !!lazyEntries;
|
|
214
|
+
self.decodeStrings = !!decodeStrings;
|
|
215
|
+
self.validateEntrySizes = !!validateEntrySizes;
|
|
216
|
+
self.strictFileNames = !!strictFileNames;
|
|
217
|
+
self.isOpen = true;
|
|
218
|
+
self.emittedError = false;
|
|
219
|
+
|
|
220
|
+
if (!self.lazyEntries) self._readEntry();
|
|
221
|
+
}
|
|
222
|
+
ZipFile.prototype.close = function() {
|
|
223
|
+
if (!this.isOpen) return;
|
|
224
|
+
this.isOpen = false;
|
|
225
|
+
this.reader.unref();
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
function emitErrorAndAutoClose(self, err) {
|
|
229
|
+
if (self.autoClose) self.close();
|
|
230
|
+
emitError(self, err);
|
|
231
|
+
}
|
|
232
|
+
function emitError(self, err) {
|
|
233
|
+
if (self.emittedError) return;
|
|
234
|
+
self.emittedError = true;
|
|
235
|
+
self.emit("error", err);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
ZipFile.prototype.readEntry = function() {
|
|
239
|
+
if (!this.lazyEntries) throw new Error("readEntry() called without lazyEntries:true");
|
|
240
|
+
this._readEntry();
|
|
241
|
+
};
|
|
242
|
+
ZipFile.prototype._readEntry = function() {
|
|
243
|
+
var self = this;
|
|
244
|
+
if (self.entryCount === self.entriesRead) {
|
|
245
|
+
// done with metadata
|
|
246
|
+
setImmediate(function() {
|
|
247
|
+
if (self.autoClose) self.close();
|
|
248
|
+
if (self.emittedError) return;
|
|
249
|
+
self.emit("end");
|
|
250
|
+
});
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
if (self.emittedError) return;
|
|
254
|
+
var buffer = newBuffer(46);
|
|
255
|
+
readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
|
|
256
|
+
if (err) return emitErrorAndAutoClose(self, err);
|
|
257
|
+
if (self.emittedError) return;
|
|
258
|
+
var entry = new Entry();
|
|
259
|
+
// 0 - Central directory file header signature
|
|
260
|
+
var signature = buffer.readUInt32LE(0);
|
|
261
|
+
if (signature !== 0x02014b50) return emitErrorAndAutoClose(self, new Error("invalid central directory file header signature: 0x" + signature.toString(16)));
|
|
262
|
+
// 4 - Version made by
|
|
263
|
+
entry.versionMadeBy = buffer.readUInt16LE(4);
|
|
264
|
+
// 6 - Version needed to extract (minimum)
|
|
265
|
+
entry.versionNeededToExtract = buffer.readUInt16LE(6);
|
|
266
|
+
// 8 - General purpose bit flag
|
|
267
|
+
entry.generalPurposeBitFlag = buffer.readUInt16LE(8);
|
|
268
|
+
// 10 - Compression method
|
|
269
|
+
entry.compressionMethod = buffer.readUInt16LE(10);
|
|
270
|
+
// 12 - File last modification time
|
|
271
|
+
entry.lastModFileTime = buffer.readUInt16LE(12);
|
|
272
|
+
// 14 - File last modification date
|
|
273
|
+
entry.lastModFileDate = buffer.readUInt16LE(14);
|
|
274
|
+
// 16 - CRC-32
|
|
275
|
+
entry.crc32 = buffer.readUInt32LE(16);
|
|
276
|
+
// 20 - Compressed size
|
|
277
|
+
entry.compressedSize = buffer.readUInt32LE(20);
|
|
278
|
+
// 24 - Uncompressed size
|
|
279
|
+
entry.uncompressedSize = buffer.readUInt32LE(24);
|
|
280
|
+
// 28 - File name length (n)
|
|
281
|
+
entry.fileNameLength = buffer.readUInt16LE(28);
|
|
282
|
+
// 30 - Extra field length (m)
|
|
283
|
+
entry.extraFieldLength = buffer.readUInt16LE(30);
|
|
284
|
+
// 32 - File comment length (k)
|
|
285
|
+
entry.fileCommentLength = buffer.readUInt16LE(32);
|
|
286
|
+
// 34 - Disk number where file starts
|
|
287
|
+
// 36 - Internal file attributes
|
|
288
|
+
entry.internalFileAttributes = buffer.readUInt16LE(36);
|
|
289
|
+
// 38 - External file attributes
|
|
290
|
+
entry.externalFileAttributes = buffer.readUInt32LE(38);
|
|
291
|
+
// 42 - Relative offset of local file header
|
|
292
|
+
entry.relativeOffsetOfLocalHeader = buffer.readUInt32LE(42);
|
|
293
|
+
|
|
294
|
+
if (entry.generalPurposeBitFlag & 0x40) return emitErrorAndAutoClose(self, new Error("strong encryption is not supported"));
|
|
295
|
+
|
|
296
|
+
self.readEntryCursor += 46;
|
|
297
|
+
|
|
298
|
+
buffer = newBuffer(entry.fileNameLength + entry.extraFieldLength + entry.fileCommentLength);
|
|
299
|
+
readAndAssertNoEof(self.reader, buffer, 0, buffer.length, self.readEntryCursor, function(err) {
|
|
300
|
+
if (err) return emitErrorAndAutoClose(self, err);
|
|
301
|
+
if (self.emittedError) return;
|
|
302
|
+
// 46 - File name
|
|
303
|
+
entry.fileNameRaw = buffer.subarray(0, entry.fileNameLength);
|
|
304
|
+
// 46+n - Extra field
|
|
305
|
+
var fileCommentStart = entry.fileNameLength + entry.extraFieldLength;
|
|
306
|
+
entry.extraFieldRaw = buffer.subarray(entry.fileNameLength, fileCommentStart);
|
|
307
|
+
// 46+n+m - File comment
|
|
308
|
+
entry.fileCommentRaw = buffer.subarray(fileCommentStart, fileCommentStart + entry.fileCommentLength);
|
|
309
|
+
|
|
310
|
+
// Parse the extra fields, which we need for processing other fields.
|
|
311
|
+
try {
|
|
312
|
+
entry.extraFields = parseExtraFields(entry.extraFieldRaw);
|
|
313
|
+
} catch (err) {
|
|
314
|
+
return emitErrorAndAutoClose(self, err);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// Interpret strings according to bit flags, extra fields, and options.
|
|
318
|
+
if (self.decodeStrings) {
|
|
319
|
+
var isUtf8 = (entry.generalPurposeBitFlag & 0x800) !== 0;
|
|
320
|
+
entry.fileComment = decodeBuffer(entry.fileCommentRaw, isUtf8);
|
|
321
|
+
entry.fileName = getFileNameLowLevel(entry.generalPurposeBitFlag, entry.fileNameRaw, entry.extraFields, self.strictFileNames);
|
|
322
|
+
var errorMessage = validateFileName(entry.fileName);
|
|
323
|
+
if (errorMessage != null) return emitErrorAndAutoClose(self, new Error(errorMessage));
|
|
324
|
+
} else {
|
|
325
|
+
entry.fileComment = entry.fileCommentRaw;
|
|
326
|
+
entry.fileName = entry.fileNameRaw;
|
|
327
|
+
}
|
|
328
|
+
// Maintain API compatibility. See https://github.com/thejoshwolfe/yauzl/issues/47
|
|
329
|
+
entry.comment = entry.fileComment;
|
|
330
|
+
|
|
331
|
+
self.readEntryCursor += buffer.length;
|
|
332
|
+
self.entriesRead += 1;
|
|
333
|
+
|
|
334
|
+
// Check for the Zip64 Extended Information Extra Field.
|
|
335
|
+
for (var i = 0; i < entry.extraFields.length; i++) {
|
|
336
|
+
var extraField = entry.extraFields[i];
|
|
337
|
+
if (extraField.id !== 0x0001) continue;
|
|
338
|
+
// Found it.
|
|
339
|
+
|
|
340
|
+
var zip64EiefBuffer = extraField.data;
|
|
341
|
+
var index = 0;
|
|
342
|
+
// 0 - Original Size 8 bytes
|
|
343
|
+
if (entry.uncompressedSize === 0xffffffff) {
|
|
344
|
+
if (index + 8 > zip64EiefBuffer.length) {
|
|
345
|
+
return emitErrorAndAutoClose(self, new Error("zip64 extended information extra field does not include uncompressed size"));
|
|
346
|
+
}
|
|
347
|
+
entry.uncompressedSize = readUInt64LE(zip64EiefBuffer, index);
|
|
348
|
+
index += 8;
|
|
349
|
+
}
|
|
350
|
+
// 8 - Compressed Size 8 bytes
|
|
351
|
+
if (entry.compressedSize === 0xffffffff) {
|
|
352
|
+
if (index + 8 > zip64EiefBuffer.length) {
|
|
353
|
+
return emitErrorAndAutoClose(self, new Error("zip64 extended information extra field does not include compressed size"));
|
|
354
|
+
}
|
|
355
|
+
entry.compressedSize = readUInt64LE(zip64EiefBuffer, index);
|
|
356
|
+
index += 8;
|
|
357
|
+
}
|
|
358
|
+
// 16 - Relative Header Offset 8 bytes
|
|
359
|
+
if (entry.relativeOffsetOfLocalHeader === 0xffffffff) {
|
|
360
|
+
if (index + 8 > zip64EiefBuffer.length) {
|
|
361
|
+
return emitErrorAndAutoClose(self, new Error("zip64 extended information extra field does not include relative header offset"));
|
|
362
|
+
}
|
|
363
|
+
entry.relativeOffsetOfLocalHeader = readUInt64LE(zip64EiefBuffer, index);
|
|
364
|
+
index += 8;
|
|
365
|
+
}
|
|
366
|
+
// 24 - Disk Start Number 4 bytes
|
|
367
|
+
|
|
368
|
+
break;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
// validate file size
|
|
372
|
+
if (self.validateEntrySizes && entry.compressionMethod === 0) {
|
|
373
|
+
var expectedCompressedSize = entry.uncompressedSize;
|
|
374
|
+
if (entry.isEncrypted()) {
|
|
375
|
+
// traditional encryption prefixes the file data with a header
|
|
376
|
+
expectedCompressedSize += 12;
|
|
377
|
+
}
|
|
378
|
+
if (entry.compressedSize !== expectedCompressedSize) {
|
|
379
|
+
var msg = "compressed/uncompressed size mismatch for stored file: " + entry.compressedSize + " != " + entry.uncompressedSize;
|
|
380
|
+
return emitErrorAndAutoClose(self, new Error(msg));
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
self.emit("entry", entry);
|
|
385
|
+
|
|
386
|
+
if (!self.lazyEntries) self._readEntry();
|
|
387
|
+
});
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
|
|
391
|
+
ZipFile.prototype.openReadStream = function(entry, options, callback) {
|
|
392
|
+
var self = this;
|
|
393
|
+
// parameter validation
|
|
394
|
+
var relativeStart = 0;
|
|
395
|
+
var relativeEnd = entry.compressedSize;
|
|
396
|
+
if (callback == null) {
|
|
397
|
+
callback = options;
|
|
398
|
+
options = null;
|
|
399
|
+
}
|
|
400
|
+
if (options == null) {
|
|
401
|
+
options = {};
|
|
402
|
+
} else {
|
|
403
|
+
if (options.decodeFileData === false) {
|
|
404
|
+
// new, simple option
|
|
405
|
+
if (options.decrypt != null) {
|
|
406
|
+
throw new Error("cannot use options.decrypt when options.decodeFileData === false");
|
|
407
|
+
}
|
|
408
|
+
if (options.decompress != null) {
|
|
409
|
+
throw new Error("cannot use options.decompress when options.decodeFileData === false");
|
|
410
|
+
}
|
|
411
|
+
// start and end are allowed
|
|
412
|
+
} else {
|
|
413
|
+
// old, complicated options
|
|
414
|
+
if (options.decrypt != null) {
|
|
415
|
+
if (!entry.isEncrypted()) {
|
|
416
|
+
throw new Error("options.decrypt can only be specified for encrypted entries. See also option decodeFileData.");
|
|
417
|
+
}
|
|
418
|
+
if (options.decrypt !== false) throw new Error("invalid options.decrypt value: " + options.decrypt);
|
|
419
|
+
if (entry.isCompressed()) {
|
|
420
|
+
if (options.decompress !== false) throw new Error("entry is encrypted and compressed, and options.decompress !== false. See also option decodeFileData.");
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (options.decompress != null) {
|
|
424
|
+
if (!entry.isCompressed()) {
|
|
425
|
+
throw new Error("options.decompress can only be specified for compressed entries. See also option decodeFileData.");
|
|
426
|
+
}
|
|
427
|
+
if (!(options.decompress === false || options.decompress === true)) {
|
|
428
|
+
throw new Error("invalid options.decompress value: " + options.decompress);
|
|
429
|
+
}
|
|
430
|
+
decompress = options.decompress;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (options.start != null) {
|
|
434
|
+
relativeStart = options.start;
|
|
435
|
+
if (relativeStart < 0) throw new Error("options.start < 0");
|
|
436
|
+
if (relativeStart > entry.compressedSize) throw new Error("options.start > entry.compressedSize");
|
|
437
|
+
}
|
|
438
|
+
if (options.end != null) {
|
|
439
|
+
relativeEnd = options.end;
|
|
440
|
+
if (relativeEnd < 0) throw new Error("options.end < 0");
|
|
441
|
+
if (relativeEnd > entry.compressedSize) throw new Error("options.end > entry.compressedSize");
|
|
442
|
+
if (relativeEnd < relativeStart) throw new Error("options.end < options.start");
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
var rawMode = (
|
|
446
|
+
options.decodeFileData === false || // Explicitly requested raw.
|
|
447
|
+
(
|
|
448
|
+
(entry.compressionMethod === 0 || // Naturally without compression.
|
|
449
|
+
(entry.compressionMethod === 8 && options.decompress === false) // Deprecated compression bypass option.
|
|
450
|
+
) &&
|
|
451
|
+
(!entry.isEncrypted() || // Naturally without encryption.
|
|
452
|
+
options.decrypt === false // Deprecated encryption bypass option.
|
|
453
|
+
)
|
|
454
|
+
)
|
|
455
|
+
);
|
|
456
|
+
if (options.start != null || options.end != null) {
|
|
457
|
+
// Ensure slicing deals with raw data.
|
|
458
|
+
if (!rawMode) throw new Error("start/end range require options.decodeFileData === false for non-trivial encoded entries.");
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// any further errors can either be caused by the zipfile,
|
|
462
|
+
// or were introduced in a minor version of yauzl,
|
|
463
|
+
// so should be passed to the client rather than thrown.
|
|
464
|
+
if (!self.isOpen) return callback(new Error("closed"));
|
|
465
|
+
if (entry.isEncrypted() && !rawMode) {
|
|
466
|
+
if (options.decrypt !== false) return callback(new Error("entry is encrypted, and options.decodeFileData !== false"));
|
|
467
|
+
}
|
|
468
|
+
var decompress;
|
|
469
|
+
if (rawMode) {
|
|
470
|
+
decompress = false;
|
|
471
|
+
} else if (entry.compressionMethod === 8) {
|
|
472
|
+
// 8 - The file is Deflated
|
|
473
|
+
decompress = options.decodeFileData !== true;
|
|
474
|
+
} else {
|
|
475
|
+
return callback(new Error("unsupported compression method: " + entry.compressionMethod));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
self.readLocalFileHeader(entry, {minimal: true}, function(err, localFileHeader) {
|
|
479
|
+
if (err) return callback(err);
|
|
480
|
+
self.openReadStreamLowLevel(
|
|
481
|
+
localFileHeader.fileDataStart, entry.compressedSize,
|
|
482
|
+
relativeStart, relativeEnd,
|
|
483
|
+
decompress, entry.uncompressedSize,
|
|
484
|
+
callback);
|
|
485
|
+
});
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
ZipFile.prototype.openReadStreamLowLevel = function(fileDataStart, compressedSize, relativeStart, relativeEnd, decompress, uncompressedSize, callback) {
|
|
489
|
+
var self = this;
|
|
490
|
+
|
|
491
|
+
var fileDataEnd = fileDataStart + compressedSize;
|
|
492
|
+
var readStream = self.reader.createReadStream({
|
|
493
|
+
start: fileDataStart + relativeStart,
|
|
494
|
+
end: fileDataStart + relativeEnd,
|
|
495
|
+
});
|
|
496
|
+
var endpointStream = readStream;
|
|
497
|
+
if (decompress) {
|
|
498
|
+
var destroyed = false;
|
|
499
|
+
var inflateFilter = zlib.createInflateRaw();
|
|
500
|
+
readStream.on("error", function(err) {
|
|
501
|
+
// setImmediate here because errors can be emitted during the first call to pipe()
|
|
502
|
+
setImmediate(function() {
|
|
503
|
+
if (!destroyed) inflateFilter.emit("error", err);
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
readStream.pipe(inflateFilter);
|
|
507
|
+
|
|
508
|
+
if (self.validateEntrySizes) {
|
|
509
|
+
endpointStream = new AssertByteCountStream(uncompressedSize);
|
|
510
|
+
inflateFilter.on("error", function(err) {
|
|
511
|
+
// forward zlib errors to the client-visible stream
|
|
512
|
+
setImmediate(function() {
|
|
513
|
+
if (!destroyed) endpointStream.emit("error", err);
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
inflateFilter.pipe(endpointStream);
|
|
517
|
+
} else {
|
|
518
|
+
// the zlib filter is the client-visible stream
|
|
519
|
+
endpointStream = inflateFilter;
|
|
520
|
+
}
|
|
521
|
+
// this is part of yauzl's API, so implement this function on the client-visible stream
|
|
522
|
+
installDestroyFn(endpointStream, function() {
|
|
523
|
+
destroyed = true;
|
|
524
|
+
if (inflateFilter !== endpointStream) inflateFilter.unpipe(endpointStream);
|
|
525
|
+
readStream.unpipe(inflateFilter);
|
|
526
|
+
// TODO: the inflateFilter may cause a memory leak. see Issue #27.
|
|
527
|
+
readStream.destroy();
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
callback(null, endpointStream);
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
ZipFile.prototype.readLocalFileHeader = function(entry, options, callback) {
|
|
534
|
+
var self = this;
|
|
535
|
+
if (callback == null) {
|
|
536
|
+
callback = options;
|
|
537
|
+
options = null;
|
|
538
|
+
}
|
|
539
|
+
if (options == null) options = {};
|
|
540
|
+
|
|
541
|
+
self.reader.ref();
|
|
542
|
+
var buffer = newBuffer(30);
|
|
543
|
+
readAndAssertNoEof(self.reader, buffer, 0, buffer.length, entry.relativeOffsetOfLocalHeader, function(err) {
|
|
544
|
+
try {
|
|
545
|
+
if (err) return callback(err);
|
|
546
|
+
// 0 - Local file header signature = 0x04034b50
|
|
547
|
+
var signature = buffer.readUInt32LE(0);
|
|
548
|
+
if (signature !== 0x04034b50) {
|
|
549
|
+
return callback(new Error("invalid local file header signature: 0x" + signature.toString(16)));
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
var fileNameLength = buffer.readUInt16LE(26);
|
|
553
|
+
var extraFieldLength = buffer.readUInt16LE(28);
|
|
554
|
+
var fileDataStart = entry.relativeOffsetOfLocalHeader + 30 + fileNameLength + extraFieldLength;
|
|
555
|
+
// We now have enough information to do this bounds check.
|
|
556
|
+
if (fileDataStart + entry.compressedSize > self.fileSize) {
|
|
557
|
+
return callback(new Error("file data overflows file bounds: " +
|
|
558
|
+
fileDataStart + " + " + entry.compressedSize + " > " + self.fileSize));
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
if (options.minimal) {
|
|
562
|
+
return callback(null, {fileDataStart: fileDataStart});
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
var localFileHeader = new LocalFileHeader();
|
|
566
|
+
localFileHeader.fileDataStart = fileDataStart;
|
|
567
|
+
|
|
568
|
+
// 4 - Version needed to extract (minimum)
|
|
569
|
+
localFileHeader.versionNeededToExtract = buffer.readUInt16LE(4);
|
|
570
|
+
// 6 - General purpose bit flag
|
|
571
|
+
localFileHeader.generalPurposeBitFlag = buffer.readUInt16LE(6);
|
|
572
|
+
// 8 - Compression method
|
|
573
|
+
localFileHeader.compressionMethod = buffer.readUInt16LE(8);
|
|
574
|
+
// 10 - File last modification time
|
|
575
|
+
localFileHeader.lastModFileTime = buffer.readUInt16LE(10);
|
|
576
|
+
// 12 - File last modification date
|
|
577
|
+
localFileHeader.lastModFileDate = buffer.readUInt16LE(12);
|
|
578
|
+
// 14 - CRC-32
|
|
579
|
+
localFileHeader.crc32 = buffer.readUInt32LE(14);
|
|
580
|
+
// 18 - Compressed size
|
|
581
|
+
localFileHeader.compressedSize = buffer.readUInt32LE(18);
|
|
582
|
+
// 22 - Uncompressed size
|
|
583
|
+
localFileHeader.uncompressedSize = buffer.readUInt32LE(22);
|
|
584
|
+
// 26 - File name length (n)
|
|
585
|
+
localFileHeader.fileNameLength = fileNameLength;
|
|
586
|
+
// 28 - Extra field length (m)
|
|
587
|
+
localFileHeader.extraFieldLength = extraFieldLength;
|
|
588
|
+
// 30 - File name
|
|
589
|
+
// 30+n - Extra field
|
|
590
|
+
|
|
591
|
+
buffer = newBuffer(fileNameLength + extraFieldLength);
|
|
592
|
+
self.reader.ref();
|
|
593
|
+
readAndAssertNoEof(self.reader, buffer, 0, buffer.length, entry.relativeOffsetOfLocalHeader + 30, function(err) {
|
|
594
|
+
try {
|
|
595
|
+
if (err) return callback(err);
|
|
596
|
+
localFileHeader.fileName = buffer.subarray(0, fileNameLength);
|
|
597
|
+
localFileHeader.extraField = buffer.subarray(fileNameLength);
|
|
598
|
+
return callback(null, localFileHeader);
|
|
599
|
+
} finally {
|
|
600
|
+
self.reader.unref();
|
|
601
|
+
}
|
|
602
|
+
});
|
|
603
|
+
} finally {
|
|
604
|
+
self.reader.unref();
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
function Entry() {
|
|
610
|
+
}
|
|
611
|
+
Entry.prototype.getLastModDate = function(options) {
|
|
612
|
+
if (options == null) options = {};
|
|
613
|
+
|
|
614
|
+
if (!options.forceDosFormat) {
|
|
615
|
+
// Check extended fields.
|
|
616
|
+
for (var i = 0; i < this.extraFields.length; i++) {
|
|
617
|
+
var extraField = this.extraFields[i];
|
|
618
|
+
if (extraField.id === 0x5455) {
|
|
619
|
+
// Info-ZIP "universal timestamp" extended field (`0x5455` aka `"UT"`).
|
|
620
|
+
// See the Info-ZIP source code unix/unix.c:set_extra_field() and zipfile.c:ef_scan_ut_time().
|
|
621
|
+
var data = extraField.data;
|
|
622
|
+
if (data.length < 5) continue; // Too short.
|
|
623
|
+
// The flags define which of the three fields are present: mtime, atime, ctime.
|
|
624
|
+
// We only care about mtime.
|
|
625
|
+
// Also, ctime is never included in practice.
|
|
626
|
+
// And also, atime is only included in the local file header for some reason
|
|
627
|
+
// despite the flags lying about its inclusion in the central header.
|
|
628
|
+
var flags = data[0];
|
|
629
|
+
var HAS_MTIME = 1;
|
|
630
|
+
if (!(flags & HAS_MTIME)) continue; // This will realistically never happen.
|
|
631
|
+
// Although the positions of all of the fields shift around depending on the presence of other fields,
|
|
632
|
+
// mtime is always first if present, and that's the only one we care about.
|
|
633
|
+
var posixTimestamp = data.readInt32LE(1);
|
|
634
|
+
return new Date(posixTimestamp * 1000);
|
|
635
|
+
} else if (extraField.id === 0x000a) {
|
|
636
|
+
var data = extraField.data;
|
|
637
|
+
if (data.length !== 32) continue; // The length is always the same.
|
|
638
|
+
// 4 bytes reserved
|
|
639
|
+
// 2 bytes Tag
|
|
640
|
+
if (data.readUInt16LE(4) !== 1) continue; // Tag1 is actually the only defined Tag.
|
|
641
|
+
// 2 bytes Size
|
|
642
|
+
if (data.readUInt16LE(6) !== 24) continue; // Size is always 24.
|
|
643
|
+
// 8 bytes Mtime
|
|
644
|
+
var hundredNanoSecondsSince1601 = data.readUInt32LE(8) + 4294967296 * data.readInt32LE(12);
|
|
645
|
+
// Convert from NTFS to POSIX milliseconds.
|
|
646
|
+
// The big number below is the milliseconds between year 1601 and year 1970
|
|
647
|
+
// (i.e. the negative POSIX timestamp of 1601-01-01 00:00:00Z)
|
|
648
|
+
var millisecondsSince1970 = hundredNanoSecondsSince1601 / 10000 - 11644473600000;
|
|
649
|
+
// Note on numeric precision: JavaScript Number objects lose precision above Number.MAX_SAFE_INTEGER,
|
|
650
|
+
// and NTFS timestamps are typically much bigger than that limit.
|
|
651
|
+
// (MAX_SAFE_INTEGER would represent 1629-07-17T23:58:45.475Z.)
|
|
652
|
+
// However, we're losing precision in the conversion from 100nanosecond units to millisecond units anyway,
|
|
653
|
+
// and the time at which we also lose 1-millisecond precision is year 275760, the JavaScript Date limit (by design).
|
|
654
|
+
// Up through the year 2057, this conversion only drops 4 bits of precision,
|
|
655
|
+
// which is well under the 13-14 bits ratio between the milliseconds and 100nanoseconds.
|
|
656
|
+
return new Date(millisecondsSince1970);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// Fallback to non-extended encoding.
|
|
662
|
+
return dosDateTimeToDate(this.lastModFileDate, this.lastModFileTime, options.timezone);
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
Entry.prototype.canDecodeFileData = function() {
|
|
666
|
+
return (
|
|
667
|
+
!this.isEncrypted() &&
|
|
668
|
+
(this.compressionMethod === 0 || this.compressionMethod === 8)
|
|
669
|
+
);
|
|
670
|
+
};
|
|
671
|
+
Entry.prototype.isEncrypted = function() {
|
|
672
|
+
return (this.generalPurposeBitFlag & 0x1) !== 0;
|
|
673
|
+
};
|
|
674
|
+
Entry.prototype.isCompressed = function() {
|
|
675
|
+
return this.compressionMethod === 8;
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
function LocalFileHeader() {
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
function dosDateTimeToDate(date, time, timezone) {
|
|
682
|
+
var day = date & 0x1f; // 1-31
|
|
683
|
+
var month = (date >> 5 & 0xf) - 1; // 1-12, 0-11
|
|
684
|
+
var year = (date >> 9 & 0x7f) + 1980; // 0-128, 1980-2108
|
|
685
|
+
|
|
686
|
+
var millisecond = 0;
|
|
687
|
+
var second = (time & 0x1f) * 2; // 0-29, 0-58 (even numbers)
|
|
688
|
+
var minute = time >> 5 & 0x3f; // 0-59
|
|
689
|
+
var hour = time >> 11 & 0x1f; // 0-23
|
|
690
|
+
|
|
691
|
+
if (timezone == null || timezone === "local") {
|
|
692
|
+
return new Date(year, month, day, hour, minute, second, millisecond);
|
|
693
|
+
} else if (timezone === "UTC") {
|
|
694
|
+
return new Date(Date.UTC(year, month, day, hour, minute, second, millisecond));
|
|
695
|
+
} else {
|
|
696
|
+
throw new Error("unrecognized options.timezone: " + options.timezone);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function getFileNameLowLevel(generalPurposeBitFlag, fileNameBuffer, extraFields, strictFileNames) {
|
|
701
|
+
var fileName = null;
|
|
702
|
+
|
|
703
|
+
// check for Info-ZIP Unicode Path Extra Field (0x7075)
|
|
704
|
+
// see https://github.com/thejoshwolfe/yauzl/issues/33
|
|
705
|
+
for (var i = 0; i < extraFields.length; i++) {
|
|
706
|
+
var extraField = extraFields[i];
|
|
707
|
+
if (extraField.id === 0x7075) {
|
|
708
|
+
if (extraField.data.length < 6) {
|
|
709
|
+
// too short to be meaningful
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
// Version 1 byte version of this extra field, currently 1
|
|
713
|
+
if (extraField.data.readUInt8(0) !== 1) {
|
|
714
|
+
// > Changes may not be backward compatible so this extra
|
|
715
|
+
// > field should not be used if the version is not recognized.
|
|
716
|
+
continue;
|
|
717
|
+
}
|
|
718
|
+
// NameCRC32 4 bytes File Name Field CRC32 Checksum
|
|
719
|
+
var oldNameCrc32 = extraField.data.readUInt32LE(1);
|
|
720
|
+
if (crc32.unsigned(fileNameBuffer) !== oldNameCrc32) {
|
|
721
|
+
// > If the CRC check fails, this UTF-8 Path Extra Field should be
|
|
722
|
+
// > ignored and the File Name field in the header should be used instead.
|
|
723
|
+
continue;
|
|
724
|
+
}
|
|
725
|
+
// UnicodeName Variable UTF-8 version of the entry File Name
|
|
726
|
+
fileName = decodeBuffer(extraField.data.subarray(5), true);
|
|
727
|
+
break;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
if (fileName == null) {
|
|
732
|
+
// The typical case.
|
|
733
|
+
var isUtf8 = (generalPurposeBitFlag & 0x800) !== 0;
|
|
734
|
+
fileName = decodeBuffer(fileNameBuffer, isUtf8);
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
if (!strictFileNames) {
|
|
738
|
+
// Allow backslash.
|
|
739
|
+
fileName = fileName.replace(/\\/g, "/");
|
|
740
|
+
}
|
|
741
|
+
return fileName;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function validateFileName(fileName) {
|
|
745
|
+
if (fileName.indexOf("\\") !== -1) {
|
|
746
|
+
return "invalid characters in fileName: " + fileName;
|
|
747
|
+
}
|
|
748
|
+
if (/^[a-zA-Z]:/.test(fileName) || /^\//.test(fileName)) {
|
|
749
|
+
return "absolute path: " + fileName;
|
|
750
|
+
}
|
|
751
|
+
if (fileName.split("/").indexOf("..") !== -1) {
|
|
752
|
+
return "invalid relative path: " + fileName;
|
|
753
|
+
}
|
|
754
|
+
// all good
|
|
755
|
+
return null;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
function parseExtraFields(extraFieldBuffer) {
|
|
759
|
+
var extraFields = [];
|
|
760
|
+
var i = 0;
|
|
761
|
+
while (i < extraFieldBuffer.length - 3) {
|
|
762
|
+
var headerId = extraFieldBuffer.readUInt16LE(i + 0);
|
|
763
|
+
var dataSize = extraFieldBuffer.readUInt16LE(i + 2);
|
|
764
|
+
var dataStart = i + 4;
|
|
765
|
+
var dataEnd = dataStart + dataSize;
|
|
766
|
+
if (dataEnd > extraFieldBuffer.length) throw new Error("extra field length exceeds extra field buffer size");
|
|
767
|
+
var dataBuffer = extraFieldBuffer.subarray(dataStart, dataEnd);
|
|
768
|
+
extraFields.push({
|
|
769
|
+
id: headerId,
|
|
770
|
+
data: dataBuffer,
|
|
771
|
+
});
|
|
772
|
+
i = dataEnd;
|
|
773
|
+
}
|
|
774
|
+
return extraFields;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
function readAndAssertNoEof(reader, buffer, offset, length, position, callback) {
|
|
778
|
+
if (length === 0) {
|
|
779
|
+
// fs.read will throw an out-of-bounds error if you try to read 0 bytes from a 0 byte file
|
|
780
|
+
return setImmediate(function() { callback(null, newBuffer(0)); });
|
|
781
|
+
}
|
|
782
|
+
reader.read(buffer, offset, length, position, function(err, bytesRead) {
|
|
783
|
+
if (err) return callback(err);
|
|
784
|
+
if (bytesRead < length) {
|
|
785
|
+
return callback(new Error("unexpected EOF"));
|
|
786
|
+
}
|
|
787
|
+
callback();
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
util.inherits(AssertByteCountStream, Transform);
|
|
792
|
+
function AssertByteCountStream(byteCount) {
|
|
793
|
+
Transform.call(this);
|
|
794
|
+
this.actualByteCount = 0;
|
|
795
|
+
this.expectedByteCount = byteCount;
|
|
796
|
+
}
|
|
797
|
+
AssertByteCountStream.prototype._transform = function(chunk, encoding, cb) {
|
|
798
|
+
this.actualByteCount += chunk.length;
|
|
799
|
+
if (this.actualByteCount > this.expectedByteCount) {
|
|
800
|
+
var msg = "too many bytes in the stream. expected " + this.expectedByteCount + ". got at least " + this.actualByteCount;
|
|
801
|
+
return cb(new Error(msg));
|
|
802
|
+
}
|
|
803
|
+
cb(null, chunk);
|
|
804
|
+
};
|
|
805
|
+
AssertByteCountStream.prototype._flush = function(cb) {
|
|
806
|
+
if (this.actualByteCount < this.expectedByteCount) {
|
|
807
|
+
var msg = "not enough bytes in the stream. expected " + this.expectedByteCount + ". got only " + this.actualByteCount;
|
|
808
|
+
return cb(new Error(msg));
|
|
809
|
+
}
|
|
810
|
+
cb();
|
|
811
|
+
};
|
|
812
|
+
|
|
813
|
+
util.inherits(RandomAccessReader, EventEmitter);
|
|
814
|
+
function RandomAccessReader() {
|
|
815
|
+
EventEmitter.call(this);
|
|
816
|
+
this.refCount = 0;
|
|
817
|
+
}
|
|
818
|
+
RandomAccessReader.prototype.ref = function() {
|
|
819
|
+
this.refCount += 1;
|
|
820
|
+
};
|
|
821
|
+
RandomAccessReader.prototype.unref = function() {
|
|
822
|
+
var self = this;
|
|
823
|
+
self.refCount -= 1;
|
|
824
|
+
|
|
825
|
+
if (self.refCount > 0) return;
|
|
826
|
+
if (self.refCount < 0) throw new Error("invalid unref");
|
|
827
|
+
|
|
828
|
+
self.close(onCloseDone);
|
|
829
|
+
|
|
830
|
+
function onCloseDone(err) {
|
|
831
|
+
if (err) return self.emit('error', err);
|
|
832
|
+
self.emit('close');
|
|
833
|
+
}
|
|
834
|
+
};
|
|
835
|
+
RandomAccessReader.prototype.createReadStream = function(options) {
|
|
836
|
+
if (options == null) options = {};
|
|
837
|
+
var start = options.start;
|
|
838
|
+
var end = options.end;
|
|
839
|
+
if (start === end) {
|
|
840
|
+
var emptyStream = new PassThrough();
|
|
841
|
+
setImmediate(function() {
|
|
842
|
+
emptyStream.end();
|
|
843
|
+
});
|
|
844
|
+
return emptyStream;
|
|
845
|
+
}
|
|
846
|
+
var stream = this._readStreamForRange(start, end);
|
|
847
|
+
|
|
848
|
+
var destroyed = false;
|
|
849
|
+
var refUnrefFilter = new RefUnrefFilter(this);
|
|
850
|
+
stream.on("error", function(err) {
|
|
851
|
+
setImmediate(function() {
|
|
852
|
+
if (!destroyed) refUnrefFilter.emit("error", err);
|
|
853
|
+
});
|
|
854
|
+
});
|
|
855
|
+
installDestroyFn(refUnrefFilter, function() {
|
|
856
|
+
stream.unpipe(refUnrefFilter);
|
|
857
|
+
refUnrefFilter.unref();
|
|
858
|
+
stream.destroy();
|
|
859
|
+
});
|
|
860
|
+
|
|
861
|
+
var byteCounter = new AssertByteCountStream(end - start);
|
|
862
|
+
refUnrefFilter.on("error", function(err) {
|
|
863
|
+
setImmediate(function() {
|
|
864
|
+
if (!destroyed) byteCounter.emit("error", err);
|
|
865
|
+
});
|
|
866
|
+
});
|
|
867
|
+
installDestroyFn(byteCounter, function() {
|
|
868
|
+
destroyed = true;
|
|
869
|
+
refUnrefFilter.unpipe(byteCounter);
|
|
870
|
+
refUnrefFilter.destroy();
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
return stream.pipe(refUnrefFilter).pipe(byteCounter);
|
|
874
|
+
};
|
|
875
|
+
RandomAccessReader.prototype._readStreamForRange = function(start, end) {
|
|
876
|
+
throw new Error("not implemented");
|
|
877
|
+
};
|
|
878
|
+
RandomAccessReader.prototype.read = function(buffer, offset, length, position, callback) {
|
|
879
|
+
var readStream = this.createReadStream({start: position, end: position + length});
|
|
880
|
+
var writeStream = new Writable();
|
|
881
|
+
var written = 0;
|
|
882
|
+
writeStream._write = function(chunk, encoding, cb) {
|
|
883
|
+
chunk.copy(buffer, offset + written, 0, chunk.length);
|
|
884
|
+
written += chunk.length;
|
|
885
|
+
cb();
|
|
886
|
+
};
|
|
887
|
+
writeStream.on("finish", callback);
|
|
888
|
+
readStream.on("error", function(error) {
|
|
889
|
+
callback(error);
|
|
890
|
+
});
|
|
891
|
+
readStream.pipe(writeStream);
|
|
892
|
+
};
|
|
893
|
+
RandomAccessReader.prototype.close = function(callback) {
|
|
894
|
+
setImmediate(callback);
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
util.inherits(RefUnrefFilter, PassThrough);
|
|
898
|
+
function RefUnrefFilter(context) {
|
|
899
|
+
PassThrough.call(this);
|
|
900
|
+
this.context = context;
|
|
901
|
+
this.context.ref();
|
|
902
|
+
this.unreffedYet = false;
|
|
903
|
+
}
|
|
904
|
+
RefUnrefFilter.prototype._flush = function(cb) {
|
|
905
|
+
this.unref();
|
|
906
|
+
cb();
|
|
907
|
+
};
|
|
908
|
+
RefUnrefFilter.prototype.unref = function(cb) {
|
|
909
|
+
if (this.unreffedYet) return;
|
|
910
|
+
this.unreffedYet = true;
|
|
911
|
+
this.context.unref();
|
|
912
|
+
};
|
|
913
|
+
|
|
914
|
+
var cp437 = '\u0000☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ ';
|
|
915
|
+
function decodeBuffer(buffer, isUtf8) {
|
|
916
|
+
if (isUtf8) {
|
|
917
|
+
return buffer.toString("utf8");
|
|
918
|
+
} else {
|
|
919
|
+
var result = "";
|
|
920
|
+
for (var i = 0; i < buffer.length; i++) {
|
|
921
|
+
result += cp437[buffer[i]];
|
|
922
|
+
}
|
|
923
|
+
return result;
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
function readUInt64LE(buffer, offset) {
|
|
928
|
+
// There is no native function for this, because we can't actually store 64-bit integers precisely.
|
|
929
|
+
// after 53 bits, JavaScript's Number type (IEEE 754 double) can't store individual integers anymore.
|
|
930
|
+
// but since 53 bits is a whole lot more than 32 bits, we do our best anyway.
|
|
931
|
+
// As of 2020, Node has added support for BigInt, which obviates this whole function,
|
|
932
|
+
// but yauzl hasn't been updated to depend on BigInt (yet?).
|
|
933
|
+
var lower32 = buffer.readUInt32LE(offset);
|
|
934
|
+
var upper32 = buffer.readUInt32LE(offset + 4);
|
|
935
|
+
// we can't use bitshifting here, because JavaScript bitshifting only works on 32-bit integers.
|
|
936
|
+
return upper32 * 0x100000000 + lower32;
|
|
937
|
+
// as long as we're bounds checking the result of this function against the total file size,
|
|
938
|
+
// we'll catch any overflow errors, because we already made sure the total file size was within reason.
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// Node 10 deprecated new Buffer().
|
|
942
|
+
var newBuffer;
|
|
943
|
+
if (typeof Buffer.allocUnsafe === "function") {
|
|
944
|
+
newBuffer = function(len) {
|
|
945
|
+
return Buffer.allocUnsafe(len);
|
|
946
|
+
};
|
|
947
|
+
} else {
|
|
948
|
+
newBuffer = function(len) {
|
|
949
|
+
return new Buffer(len);
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
// Node 8 introduced a proper destroy() implementation on writable streams.
|
|
954
|
+
function installDestroyFn(stream, fn) {
|
|
955
|
+
if (typeof stream.destroy === "function") {
|
|
956
|
+
// New API.
|
|
957
|
+
stream._destroy = function(err, cb) {
|
|
958
|
+
fn();
|
|
959
|
+
if (cb != null) cb(err);
|
|
960
|
+
};
|
|
961
|
+
} else {
|
|
962
|
+
// Old API.
|
|
963
|
+
stream.destroy = fn;
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
function defaultCallback(err) {
|
|
968
|
+
if (err) throw err;
|
|
969
|
+
}
|