This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
694 lines (625 loc) · 26 KB
/
index.js
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
import { Buffer } from 'node:buffer';
import { createReadStream } from 'node:fs';
import { stat } from 'node:fs/promises';
import { Transform } from 'node:stream';
import { finished } from 'node:stream/promises';
import { promisify } from 'node:util';
import { DeflateRaw, crc32, deflateRaw } from 'node:zlib';
// Entry `compressionMethod` field enum values
const NO_COMPRESSION = 0;
const DEFLATE_COMPRESSION = 8;
/** End of central directory record size */ const EOCDR_SIZE = 22;
/** ZIP64 end of central directory record size */ const ZIP64_EOCDR_SIZE = 56;
/** ZIP64 end of central directory locator size */ const ZIP64_EOCDL_SIZE = 20;
/** Central directory record fixed size */ const CDR_FIXED_SIZE = 46;
/** ZIP64 extended information extra field size */ const ZIP64_EIEF_SIZE = 28;
/** End of central directory record signature */
const EOCDR_SIGNATURE = Uint8Array.of(0x50, 0x4b, 0x05, 0x06);
const LOCAL_FILE_HEADER_FIXED_SIZE = 30;
const VERSION_NEEDED_TO_EXTRACT_UTF8 = 20;
const VERSION_NEEDED_TO_EXTRACT_ZIP64 = 45;
const VERSION_MADE_BY = (3 << 8) | 63; // 3 = unix. 63 = spec version 6.3
const FILE_NAME_IS_UTF8 = 1 << 11;
const UNKNOWN_CRC32_AND_FILE_SIZES = 1 << 3;
const DATA_DESCRIPTOR_SIZE = 16;
const ZIP64_DATA_DESCRIPTOR_SIZE = 24;
const EMPTY_BUFFER = Buffer.allocUnsafe(0);
const EMPTY_PROMISE = Promise.resolve();
const deflateRawPromise = promisify(deflateRaw);
/**
* @typedef {Object} Options
* @property {?Date|number} [mtime]
* @property {?number} [mode]
* @property {?boolean} [compress]
* @property {?boolean} [forceZip64Format]
* @property {?Buffer|Uint8Array} [fileComment]
* @property {?number} [size]
*/
class Entry {
/**
* @param {string} metadataPath
* @param {boolean} isDirectory
* @param {Options} options
*/
constructor(metadataPath, isDirectory, options) {
const { mtime, fileComment } = options;
const fileName = Buffer.from(metadataPath, 'utf8');
const compress = isDirectory ? false : Boolean(options.compress ?? true);
const hasComment = (fileComment != null);
if (fileName.length > 0xffff) {
throw new RangeError(`metadataPath too long. ${fileName.length} > 65535`);
} else if (!hasComment) {
} else if (!(fileComment instanceof Uint8Array)) {
throw new TypeError('fileComment must be a Buffer/Uint8Array if it exists');
} else if (fileComment.length > 0xffff) {
throw new RangeError('fileComment is too large');
}
this.fileName = fileName;
this.isDirectory = isDirectory;
this.lastMod = (typeof mtime === 'number') ? mtime : dateToDosDateTime(mtime ?? new Date());
this.setFileAttributesMode(options.mode ?? 0o000664, isDirectory);
this.crcAndFileSizeKnown = isDirectory;
this.crc32 = 0;
this.uncompressedSize = isDirectory ? 0 : (options.size ?? -1);
this.compressedSize = 0;
this.compress = compress;
this.compressionMethod = compress ? DEFLATE_COMPRESSION : NO_COMPRESSION;
this.forceZip64Format = Boolean(options.forceZip64Format ?? false);
this.relativeOffsetOfLocalHeader = 0;
this.fileComment = hasComment ? fileComment : EMPTY_BUFFER;
}
/**
* @param {number} mode
*/
setFileAttributesMode(mode, isDirectory) {
if ((mode & 0xffff) !== mode) {
throw new RangeError(`invalid mode. expected: 0 <= ${mode} <= 65535`);
}
if (isDirectory) {
// https://github.com/thejoshwolfe/yazl/pull/59
// Set executable bit on directories if any other bits are set for that user/group/all
// Fixes creating unusable zip files on platforms that do not use an executable bit
mode |= ((mode >> 1) | (mode >> 2)) & 0o000111;
mode |= 0o040000; // S_IFDIR
} else {
mode |= 0o100000; // S_IFREG
}
// http://unix.stackexchange.com/questions/14705/the-zip-formats-external-file-attribute/14727#14727
this.externalFileAttributes = (mode << 16) >>> 0;
}
/**
* @returns {boolean}
*/
useZip64Format() {
return (
(this.forceZip64Format) ||
(this.uncompressedSize > 0xfffffffe) ||
(this.compressedSize > 0xfffffffe) ||
(this.relativeOffsetOfLocalHeader > 0xfffffffe)
);
}
/**
* @todo Is supported ZIP64 really?
* @returns {Buffer}
*/
getLocalFileHeader() {
const { fileName } = this;
const fileNameLength = fileName.length;
const buffer = Buffer.allocUnsafe(
LOCAL_FILE_HEADER_FIXED_SIZE +
fileNameLength // file name (variable size)
// extra field (variable size)
);
let uncompressedSize = 0;
let generalPurposeBitFlag = FILE_NAME_IS_UTF8;
if (this.crcAndFileSizeKnown) {
uncompressedSize = this.uncompressedSize;
} else {
generalPurposeBitFlag |= UNKNOWN_CRC32_AND_FILE_SIZES;
}
// local file header signature 4 bytes (0x04034b50)
buffer.writeUInt32LE(0x04034b50, 0);
// version needed to extract 2 bytes
buffer.writeUInt16LE(VERSION_NEEDED_TO_EXTRACT_UTF8, 4);
// general purpose bit flag 2 bytes
buffer.writeUInt16LE(generalPurposeBitFlag, 6);
// compression method 2 bytes
buffer.writeUInt16LE(this.compressionMethod, 8);
// last mod file date/time 4 bytes
buffer.writeUInt32LE(this.lastMod, 10);
// crc-32 4 bytes
buffer.writeUInt32LE(this.crc32, 14);
// compressed size 4 bytes
buffer.writeUInt32LE(this.compressedSize, 18);
// uncompressed size 4 bytes
buffer.writeUInt32LE(uncompressedSize, 22);
// file name length 2 bytes
buffer.writeUInt16LE(fileNameLength, 26);
// extra field length 2 bytes
buffer.writeUInt16LE(0, 28);
// file name (variable size)
buffer.set(fileName, LOCAL_FILE_HEADER_FIXED_SIZE);
return buffer;
}
/**
* @returns {Buffer}
*/
getDataDescriptor() {
if (this.crcAndFileSizeKnown) {
// the Mac Archive Utility requires this not be present unless we set general purpose bit 3
return EMPTY_BUFFER;
} else if (this.useZip64Format()) {
// ZIP64 format
const buffer = Buffer.allocUnsafe(ZIP64_DATA_DESCRIPTOR_SIZE);
// optional signature (unknown if anyone cares about this)
buffer.writeUInt32LE(0x08074b50, 0);
// crc-32 4 bytes
buffer.writeUInt32LE(this.crc32, 4);
// compressed size 8 bytes
buffer.writeBigUInt64LE(BigInt(this.compressedSize), 8);
// uncompressed size 8 bytes
buffer.writeBigUInt64LE(BigInt(this.uncompressedSize), 16);
return buffer;
} else {
const buffer = Buffer.allocUnsafe(DATA_DESCRIPTOR_SIZE);
// optional signature (required according to Archive Utility)
buffer.writeUInt32LE(0x08074b50, 0);
// crc-32 4 bytes
buffer.writeUInt32LE(this.crc32, 4);
// compressed size 4 bytes
buffer.writeUInt32LE(this.compressedSize, 8);
// uncompressed size 4 bytes
buffer.writeUInt32LE(this.uncompressedSize, 12);
return buffer;
}
}
/**
* @returns {Buffer}
*/
getCentralDirectoryRecord() {
const { fileName, fileComment } = this;
const fileNameLength = fileName.length;
const fileCommentLength = fileComment.length;
const fileNameOffset = CDR_FIXED_SIZE;
let generalPurposeBitFlag = FILE_NAME_IS_UTF8;
if (!this.crcAndFileSizeKnown) generalPurposeBitFlag |= UNKNOWN_CRC32_AND_FILE_SIZES;
if (this.useZip64Format()) {
const zeiefOffset = fileNameOffset + fileNameLength;
const fileCommentOffset = zeiefOffset + ZIP64_EIEF_SIZE;
const buffer = Buffer.allocUnsafe(fileCommentOffset + fileCommentLength);
// central file header signature 4 bytes (0x02014b50)
buffer.writeUInt32LE(0x02014b50, 0);
// version made by 2 bytes
buffer.writeUInt16LE(VERSION_MADE_BY, 4);
// version needed to extract 2 bytes
buffer.writeUInt16LE(VERSION_NEEDED_TO_EXTRACT_ZIP64, 6);
// general purpose bit flag 2 bytes
buffer.writeUInt16LE(generalPurposeBitFlag, 8);
// compression method 2 bytes
buffer.writeUInt16LE(this.compressionMethod, 10);
// last mod file date/time 4 bytes
buffer.writeUInt32LE(this.lastMod, 12);
// crc-32 4 bytes
buffer.writeUInt32LE(this.crc32, 16);
// compressed size 4 bytes
buffer.writeUInt32LE(0xffffffff, 20);
// uncompressed size 4 bytes
buffer.writeUInt32LE(0xffffffff, 24);
// file name length 2 bytes
buffer.writeUInt16LE(fileNameLength, 28);
// extra field length 2 bytes
buffer.writeUInt16LE(ZIP64_EIEF_SIZE, 30);
// file comment length 2 bytes
buffer.writeUInt16LE(fileCommentLength, 32);
// disk number start 2 bytes
buffer.writeUInt16LE(0, 34);
// internal file attributes 2 bytes
buffer.writeUInt16LE(0, 36);
// external file attributes 4 bytes
buffer.writeUInt32LE(this.externalFileAttributes, 38);
// relative offset of local header 4 bytes
buffer.writeUInt32LE(0xffffffff, 42);
// file name (variable size)
buffer.set(fileName, fileNameOffset);
// ZIP64 extended information extra field
// 0x0001 2 bytes Tag for this "extra" block type
buffer.writeUInt16LE(0x0001, zeiefOffset);
// Size 2 bytes Size of this "extra" block
buffer.writeUInt16LE(ZIP64_EIEF_SIZE - 4, zeiefOffset + 2);
// Original Size 8 bytes Original uncompressed file size
buffer.writeBigUInt64LE(BigInt(this.uncompressedSize), zeiefOffset + 4);
// Compressed Size 8 bytes Size of compressed data
buffer.writeBigUInt64LE(BigInt(this.compressedSize), zeiefOffset + 12);
// Relative Header Offset 8 bytes Offset of local header record
buffer.writeBigUInt64LE(BigInt(this.relativeOffsetOfLocalHeader), zeiefOffset + 20);
// Disk Start Number 4 bytes Number of the disk on which this file starts
// (omit)
// file comment (variable size)
buffer.set(fileComment, fileCommentOffset);
return buffer;
} else {
const fileCommentOffset = CDR_FIXED_SIZE + fileNameLength;
const buffer = Buffer.allocUnsafe(fileCommentOffset + fileCommentLength);
// central file header signature 4 bytes (0x02014b50)
buffer.writeUInt32LE(0x02014b50, 0);
// version made by 2 bytes
buffer.writeUInt16LE(VERSION_MADE_BY, 4);
// version needed to extract 2 bytes
buffer.writeUInt16LE(VERSION_NEEDED_TO_EXTRACT_UTF8, 6);
// general purpose bit flag 2 bytes
buffer.writeUInt16LE(generalPurposeBitFlag, 8);
// compression method 2 bytes
buffer.writeUInt16LE(this.compressionMethod, 10);
// last mod file date/time 4 bytes
buffer.writeUInt32LE(this.lastMod, 12);
// crc-32 4 bytes
buffer.writeUInt32LE(this.crc32, 16);
// compressed size 4 bytes
buffer.writeUInt32LE(this.compressedSize, 20);
// uncompressed size 4 bytes
buffer.writeUInt32LE(this.uncompressedSize, 24);
// file name length 2 bytes
buffer.writeUInt16LE(fileNameLength, 28);
// extra field length 2 bytes
buffer.writeUInt16LE(0, 30);
// file comment length 2 bytes
buffer.writeUInt16LE(fileCommentLength, 32);
// disk number start 2 bytes
buffer.writeUInt16LE(0, 34);
// internal file attributes 2 bytes
buffer.writeUInt16LE(0, 36);
// external file attributes 4 bytes
buffer.writeUInt32LE(this.externalFileAttributes, 38);
// relative offset of local header 4 bytes
buffer.writeUInt32LE(this.relativeOffsetOfLocalHeader, 42);
// file name (variable size)
buffer.set(fileName, fileNameOffset);
// file comment (variable size)
buffer.set(fileComment, fileCommentOffset);
return buffer;
}
}
}
class Crc32Watcher extends Transform {
bytesWritten = 0;
crc32 = 0;
_transform(chunk, encoding, callback) {
this.bytesWritten += chunk.length;
this.crc32 = crc32(chunk, this.crc32);
callback(null, chunk);
}
}
class ZipFile extends Transform {
/** @type {Entry[]} */ entries = [];
/** @type {Promise[]} */ loadings = [];
bytesWritten = 0;
ended = false; // .end() sets this
loaded = false; // are all loadings fulfilled after .end()?
forceZip64Eocd = false; // configurable in .end()
comment = EMPTY_BUFFER;
streaming = EMPTY_PROMISE;
outputStream = this;
_flush(callback) {
// head for the exit
const { bytesWritten: centralDirOffset, entries } = this;
const totalEntries = entries.length;
for (let i = 0; i < totalEntries; ++i) {
const cdr = entries[i].getCentralDirectoryRecord();
this.bytesWritten += cdr.length;
this.push(cdr);
}
const eocdr = getEndOfCentralDirectoryRecord(this, centralDirOffset);
this.bytesWritten += eocdr.length;
this.push(eocdr);
callback(null);
}
_transform(chunk, encoding, callback) {
this.bytesWritten += chunk.length;
callback(null, chunk);
}
/**
* @param {string} realPath
* @param {string} metadataPath
* @param {?Options} [options]
*/
addFile(realPath, metadataPath, options) {
metadataPath = validateMetadataPath(metadataPath, false);
options ??= {};
const entry = new Entry(metadataPath, false, options);
const loading = stat(realPath).then((stats) => {
if (!stats.isFile()) throw new Error(`not a file: ${realPath}`);
entry.uncompressedSize = stats.size;
if (options.mtime == null) entry.lastMod = dateToDosDateTime(stats.mtime);
if (options.mode == null) entry.setFileAttributesMode(stats.mode, false);
appendStream(this, entry, writeReadStream, entry, createReadStream(realPath));
}).catch((err) => this.emit('error', err));
this.entries.push(entry);
this.loadings.push(loading);
}
/**
* @param {ReadStream} readStream
* @param {string} metadataPath
* @param {?Options} [options]
*/
addReadStream(readStream, metadataPath, options) {
metadataPath = validateMetadataPath(metadataPath, false);
options ??= {};
const entry = new Entry(metadataPath, false, options);
this.entries.push(entry);
appendStream(this, entry, writeReadStream, entry, readStream);
}
/**
* @param {Buffer} buffer
* @param {string} metadataPath
* @param {?Options} [options]
*/
addBuffer(buffer, metadataPath, options) {
metadataPath = validateMetadataPath(metadataPath, false);
options ??= {};
if (options.size != null) throw new Error('options.size not allowed');
const entry = new Entry(metadataPath, false, options);
const flush = (buffer) => {
entry.compressedSize = buffer.length;
appendStream(this, entry, writeBuffer, buffer);
};
entry.uncompressedSize = buffer.length;
entry.crc32 = crc32(buffer);
entry.crcAndFileSizeKnown = true;
this.entries.push(entry);
if (entry.compress) {
this.loadings.push(deflateRawPromise(buffer).then(flush).catch((err) => this.emit('error', err)));
} else {
flush(buffer);
}
}
/**
* @param {string} metadataPath
* @param {?Options} [options]
*/
addEmptyDirectory(metadataPath, options) {
metadataPath = validateMetadataPath(metadataPath, true);
options ??= {};
if (options.size != null) throw new Error('options.size not allowed');
if (options.compress != null) throw new Error('options.compress not allowed');
const entry = new Entry(metadataPath, true, options);
this.entries.push(entry);
appendStream(this, entry);
}
/**
* @returns {Promise<number>}
*/
async calculateTotalSize() {
if (this.writableFinished) return this.bytesWritten;
if (this.loaded) return calculateTotalSize(this);
return new Promise((resolve, reject) => {
this.on('loadend', () => resolve(calculateTotalSize(this)));
this.on('error', reject);
});
}
/**
* @param {?Function|Object} [options]
*/
end(options) {
if (this.ended || this.writableFinished) return;
options ??= {};
const comment = options.comment;
if (comment == null) {
} else if (!(comment instanceof Uint8Array)) {
throw new TypeError('comment must be a Buffer/Uint8Array if it exists');
} else if (comment.length > 0xffff) {
throw new RangeError('comment is too large');
} else if (comment.includes(EOCDR_SIGNATURE)) {
throw new Error('comment contains end of central directory record signature');
} else {
this.comment = comment;
}
this.ended = true;
this.forceZip64Eocd = Boolean(options.forceZip64Format ?? false);
// all cought up on writing entries
Promise.all(this.loadings).then(async () => {
this.loaded = true;
this.emit('loadend');
await this.streaming;
super.end();
}).catch((err) => this.emit('error', err));
}
}
/**
* @param {ZipFile} zipfile
* @param {Buffer} buffer
*/
function writeBuffer(zipfile, buffer) {
zipfile.write(buffer);
}
/**
* @param {ZipFile} zipfile
* @param {Entry} entry
* @param {ReadStream} readStream
* @returns {Promise<void>}
*/
async function writeReadStream(zipfile, entry, readStream) {
const crc32Watcher = new Crc32Watcher();
readStream = readStream.pipe(crc32Watcher);
if (entry.compress) readStream = readStream.pipe(new DeflateRaw());
readStream.pipe(zipfile, { end: false });
await finished(readStream);
entry.compressedSize = readStream.bytesWritten;
entry.crc32 = crc32Watcher.crc32;
if (entry.uncompressedSize === -1) {
entry.uncompressedSize = crc32Watcher.bytesWritten;
} else if (entry.uncompressedSize !== crc32Watcher.bytesWritten) {
throw new RangeError('file data stream has unexpected number of bytes');
}
}
/**
* @param {ZipFile} zipfile
* @param {Entry} entry
* @param {Function} [writer]
* @param {*} [...args]
*/
function appendStream(zipfile, entry, writer, ...args) {
zipfile.streaming = zipfile.streaming.then(async () => {
entry.relativeOffsetOfLocalHeader = zipfile.bytesWritten;
zipfile.write(entry.getLocalFileHeader());
if (writer !== undefined) await writer(zipfile, ...args);
zipfile.write(entry.getDataDescriptor());
}).catch((err) => zipfile.emit('error', err));
}
/**
* @param {ZipFile} zipfile
* @returns {number}
*/
function calculateTotalSize(zipfile) {
const entries = zipfile.entries;
const totalEntries = entries.length;
let pretendOutputCursor = 0;
let centralDirectorySize = 0;
for (let i = 0; i < totalEntries; ++i) {
const entry = entries[i];
// compression is too hard to predict
if (entry.compress) return -1;
// if addReadStream was called without providing the size, can't predict the final size
if (entry.uncompressedSize === -1) return -1;
// we know this for sure, and this is important to know if we need ZIP64 format.
entry.relativeOffsetOfLocalHeader = pretendOutputCursor;
const useZip64Format = entry.useZip64Format();
const fileNameLength = entry.fileName.length;
pretendOutputCursor += LOCAL_FILE_HEADER_FIXED_SIZE + fileNameLength;
pretendOutputCursor += entry.uncompressedSize;
if (!entry.crcAndFileSizeKnown) {
// use a data descriptor
pretendOutputCursor += useZip64Format ? ZIP64_DATA_DESCRIPTOR_SIZE : DATA_DESCRIPTOR_SIZE;
}
centralDirectorySize += CDR_FIXED_SIZE + fileNameLength + entry.fileComment.length;
if (useZip64Format) centralDirectorySize += ZIP64_EIEF_SIZE;
}
let endOfCentralDirectorySize = EOCDR_SIZE + zipfile.comment.length;
if (
(zipfile.forceZip64Eocd) ||
(totalEntries >= 0xffff) ||
(centralDirectorySize >= 0xffff) ||
(pretendOutputCursor >= 0xffffffff)
) {
// use zip64 end of central directory stuff
endOfCentralDirectorySize += ZIP64_EOCDR_SIZE + ZIP64_EOCDL_SIZE;
}
return pretendOutputCursor + centralDirectorySize + endOfCentralDirectorySize;
}
/**
* @param {ZipFile} zipfile
* @param {number} centralDirOffset
* @returns {Buffer}
*/
function getEndOfCentralDirectoryRecord(zipfile, centralDirOffset) {
const { entries: { length: totalEntries },
bytesWritten,
comment,
forceZip64Eocd } = zipfile;
let needZip64Format = (forceZip64Eocd || totalEntries >= 0xffff);
const normalTotalEntries = needZip64Format ? 0xffff : totalEntries;
const sizeOfCentralDirectory = bytesWritten - centralDirOffset;
let normalSizeOfCentralDirectory = sizeOfCentralDirectory;
if (forceZip64Eocd || sizeOfCentralDirectory >= 0xffffffff) {
normalSizeOfCentralDirectory = 0xffffffff;
needZip64Format = true;
}
let normalOffsetOfStartOfCentralDirectory = centralDirOffset;
if (forceZip64Eocd || centralDirOffset >= 0xffffffff) {
normalOffsetOfStartOfCentralDirectory = 0xffffffff;
needZip64Format = true;
}
const commentLength = comment.length;
const eocdrBuffer = Buffer.allocUnsafe(EOCDR_SIZE + commentLength);
// end of central dir signature 4 bytes (0x06054b50)
eocdrBuffer.writeUInt32LE(0x06054b50, 0);
// number of this disk 2 bytes
eocdrBuffer.writeUInt16LE(0, 4);
// number of the disk with the start of the central directory 2 bytes
eocdrBuffer.writeUInt16LE(0, 6);
// total number of entries in the central directory on this disk 2 bytes
eocdrBuffer.writeUInt16LE(normalTotalEntries, 8);
// total number of entries in the central directory 2 bytes
eocdrBuffer.writeUInt16LE(normalTotalEntries, 10);
// size of the central directory 4 bytes
eocdrBuffer.writeUInt32LE(normalSizeOfCentralDirectory, 12);
// offset of start of central directory with respect to the starting disk number 4 bytes
eocdrBuffer.writeUInt32LE(normalOffsetOfStartOfCentralDirectory, 16);
// .ZIP file comment length 2 bytes
eocdrBuffer.writeUInt16LE(commentLength, 20);
// .ZIP file comment (variable size)
eocdrBuffer.set(comment, 22);
if (!needZip64Format) return eocdrBuffer;
// ZIP64 format
const zip64Buffer = Buffer.allocUnsafe(ZIP64_EOCDR_SIZE + ZIP64_EOCDL_SIZE + EOCDR_SIZE + commentLength);
zip64Buffer.set(eocdrBuffer, ZIP64_EOCDR_SIZE + ZIP64_EOCDL_SIZE);
// ZIP64 End of Central Directory Record
// zip64 end of central dir signature 4 bytes (0x06064b50)
zip64Buffer.writeUInt32LE(0x06064b50, 0);
// size of zip64 end of central directory record 8 bytes
zip64Buffer.writeBigUInt64LE(BigInt(ZIP64_EOCDR_SIZE - 12), 4);
// version made by 2 bytes
zip64Buffer.writeUInt16LE(VERSION_MADE_BY, 12);
// version needed to extract 2 bytes
zip64Buffer.writeUInt16LE(VERSION_NEEDED_TO_EXTRACT_ZIP64, 14);
// number of this disk 4 bytes
zip64Buffer.writeUInt32LE(0, 16);
// number of the disk with the start of the central directory 4 bytes
zip64Buffer.writeUInt32LE(0, 20);
// total number of entries in the central directory on this disk 8 bytes
zip64Buffer.writeBigUInt64LE(BigInt(totalEntries), 24);
// total number of entries in the central directory 8 bytes
zip64Buffer.writeBigUInt64LE(BigInt(totalEntries), 32);
// size of the central directory 8 bytes
zip64Buffer.writeBigUInt64LE(BigInt(sizeOfCentralDirectory), 40);
// offset of start of central directory with respect to the starting disk number 8 bytes
zip64Buffer.writeBigUInt64LE(BigInt(centralDirOffset), 48);
// zip64 extensible data sector (variable size)
// nothing in the zip64 extensible data sector
// ZIP64 End of Central Directory Locator
// zip64 end of central dir locator signature 4 bytes (0x07064b50)
zip64Buffer.writeUInt32LE(0x07064b50, ZIP64_EOCDR_SIZE + 0);
// number of the disk with the start of the zip64 end of central directory 4 bytes
zip64Buffer.writeUInt32LE(0, ZIP64_EOCDR_SIZE + 4);
// relative offset of the zip64 end of central directory record 8 bytes
zip64Buffer.writeBigUInt64LE(BigInt(bytesWritten), ZIP64_EOCDR_SIZE + 8);
// total number of disks 4 bytes
zip64Buffer.writeUInt32LE(1, ZIP64_EOCDR_SIZE + 16);
return zip64Buffer;
}
/**
* @param {string} metadataPath
* @param {boolean} isDirectory
* @returns {string}
*/
function validateMetadataPath(metadataPath, isDirectory) {
if (metadataPath === '') throw new Error('empty metadataPath');
metadataPath = metadataPath.replaceAll('\\', '/');
if (/^[A-Za-z]:/.test(metadataPath) || metadataPath.startsWith('/')) {
throw new Error(`absolute path: ${metadataPath}`);
}
if (metadataPath.split('/').includes('..')) {
throw new Error(`invalid relative path: ${metadataPath}`);
}
const looksLikeDirectory = metadataPath.endsWith('/');
if (isDirectory) {
// append a trailing '/' if necessary.
if (!looksLikeDirectory) metadataPath += '/';
} else if (looksLikeDirectory) {
throw new Error(`file path cannot end with '/': ${metadataPath}`);
}
return metadataPath;
}
/**
* @param {Date} date
* @returns {number}
*/
function dateToDosDateTime(date) {
let dosDate = date.getDate() & 0x1f; // 1-31
dosDate |= ((date.getMonth() + 1) & 0xf) << 5; // 0-11, 1-12
dosDate |= ((date.getFullYear() - 1980) & 0x7f) << 9; // 0-128, 1980-2108
let dosTime = Math.floor(date.getSeconds() / 2); // 0-59, 0-29 (lose odd numbers)
dosTime |= (date.getMinutes() & 0x3f) << 5; // 0-59
dosTime |= (date.getHours() & 0x1f) << 11; // 0-23
return (dosDate << 16) | dosTime;
}
export { ZipFile, dateToDosDateTime };