@share-crm/sharedev-cli 0.0.4 → 0.0.5-rc.1
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/891.sharedev.js +3643 -0
- package/dist/sharedev.js +68976 -2
- package/package.json +21 -5
- package/readme.md +47 -1
- package/dist/sharedev.js.LICENSE.txt +0 -15
|
@@ -0,0 +1,3643 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
export const __webpack_esm_ids__ = [891];
|
|
3
|
+
export const __webpack_esm_modules__ = {
|
|
4
|
+
|
|
5
|
+
/***/ 39891
|
|
6
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
7
|
+
|
|
8
|
+
const Utils = __webpack_require__(6942);
|
|
9
|
+
const pth = __webpack_require__(16928);
|
|
10
|
+
const ZipEntry = __webpack_require__(66488);
|
|
11
|
+
const ZipFile = __webpack_require__(71312);
|
|
12
|
+
|
|
13
|
+
const get_Bool = (...val) => Utils.findLast(val, (c) => typeof c === "boolean");
|
|
14
|
+
const get_Str = (...val) => Utils.findLast(val, (c) => typeof c === "string");
|
|
15
|
+
const get_Fun = (...val) => Utils.findLast(val, (c) => typeof c === "function");
|
|
16
|
+
|
|
17
|
+
const defaultOptions = {
|
|
18
|
+
// option "noSort" : if true it disables files sorting
|
|
19
|
+
noSort: false,
|
|
20
|
+
// read entries during load (initial loading may be slower)
|
|
21
|
+
readEntries: false,
|
|
22
|
+
// default method is none
|
|
23
|
+
method: Utils.Constants.NONE,
|
|
24
|
+
// file system
|
|
25
|
+
fs: null
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = function (/**String*/ input, /** object */ options) {
|
|
29
|
+
let inBuffer = null;
|
|
30
|
+
|
|
31
|
+
// create object based default options, allowing them to be overwritten
|
|
32
|
+
const opts = Object.assign(Object.create(null), defaultOptions);
|
|
33
|
+
|
|
34
|
+
// test input variable
|
|
35
|
+
if (input && "object" === typeof input) {
|
|
36
|
+
// if value is not buffer we accept it to be object with options
|
|
37
|
+
if (!(input instanceof Uint8Array)) {
|
|
38
|
+
Object.assign(opts, input);
|
|
39
|
+
input = opts.input ? opts.input : undefined;
|
|
40
|
+
if (opts.input) delete opts.input;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// if input is buffer
|
|
44
|
+
if (Buffer.isBuffer(input)) {
|
|
45
|
+
inBuffer = input;
|
|
46
|
+
opts.method = Utils.Constants.BUFFER;
|
|
47
|
+
input = undefined;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// assign options
|
|
52
|
+
Object.assign(opts, options);
|
|
53
|
+
|
|
54
|
+
// instanciate utils filesystem
|
|
55
|
+
const filetools = new Utils(opts);
|
|
56
|
+
|
|
57
|
+
// Restore the archived permissions on extracted directories. This has to run
|
|
58
|
+
// after a directory's contents are written: applying a restrictive mode
|
|
59
|
+
// (e.g. 0o500) up front would stop us writing the files it contains. Applying
|
|
60
|
+
// the deepest paths first keeps parent directories traversable while their
|
|
61
|
+
// children are updated (issue #530).
|
|
62
|
+
const applyDirAttributes = (dirEntries) => {
|
|
63
|
+
dirEntries
|
|
64
|
+
.filter((d) => d.attr)
|
|
65
|
+
.sort((a, b) => b.path.length - a.path.length)
|
|
66
|
+
.forEach((d) => filetools.fs.chmodSync(d.path, d.attr));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
if (typeof opts.decoder !== "object" || typeof opts.decoder.encode !== "function" || typeof opts.decoder.decode !== "function") {
|
|
70
|
+
opts.decoder = Utils.decoder;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// if input is file name we retrieve its content
|
|
74
|
+
if (input && "string" === typeof input) {
|
|
75
|
+
// load zip file
|
|
76
|
+
if (filetools.fs.existsSync(input)) {
|
|
77
|
+
opts.method = Utils.Constants.FILE;
|
|
78
|
+
opts.filename = input;
|
|
79
|
+
inBuffer = filetools.fs.readFileSync(input);
|
|
80
|
+
} else {
|
|
81
|
+
throw Utils.Errors.INVALID_FILENAME();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// create variable
|
|
86
|
+
const _zip = new ZipFile(inBuffer, opts);
|
|
87
|
+
|
|
88
|
+
const { canonical, sanitize, zipnamefix } = Utils;
|
|
89
|
+
|
|
90
|
+
function getEntry(/**Object*/ entry) {
|
|
91
|
+
if (entry && _zip) {
|
|
92
|
+
var item;
|
|
93
|
+
// If entry was given as a file name
|
|
94
|
+
if (typeof entry === "string") item = _zip.getEntry(pth.posix.normalize(entry));
|
|
95
|
+
// if entry was given as a ZipEntry object
|
|
96
|
+
if (typeof entry === "object" && typeof entry.entryName !== "undefined" && typeof entry.header !== "undefined") item = _zip.getEntry(entry.entryName);
|
|
97
|
+
|
|
98
|
+
if (item) {
|
|
99
|
+
return item;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function fixPath(zipPath) {
|
|
106
|
+
const { join, normalize, sep } = pth.posix;
|
|
107
|
+
// convert windows file separators and normalize
|
|
108
|
+
return join(pth.isAbsolute(zipPath) ? "/": '.', normalize(sep + zipPath.split("\\").join(sep) + sep));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function filenameFilter(filterfn) {
|
|
112
|
+
if (filterfn instanceof RegExp) {
|
|
113
|
+
// if filter is RegExp wrap it
|
|
114
|
+
return (function (rx) {
|
|
115
|
+
return function (filename) {
|
|
116
|
+
return rx.test(filename);
|
|
117
|
+
};
|
|
118
|
+
})(filterfn);
|
|
119
|
+
} else if ("function" !== typeof filterfn) {
|
|
120
|
+
// if filter is not function we will replace it
|
|
121
|
+
return () => true;
|
|
122
|
+
}
|
|
123
|
+
return filterfn;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// keep last character on folders
|
|
127
|
+
const relativePath = (local, entry) => {
|
|
128
|
+
let lastChar = entry.slice(-1);
|
|
129
|
+
lastChar = lastChar === filetools.sep ? filetools.sep : "";
|
|
130
|
+
return pth.relative(local, entry) + lastChar;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
/**
|
|
135
|
+
* Extracts the given entry from the archive and returns the content as a Buffer object
|
|
136
|
+
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
|
137
|
+
* @param {Buffer|string} [pass] - password
|
|
138
|
+
* @return Buffer or Null in case of error
|
|
139
|
+
*/
|
|
140
|
+
readFile: function (entry, pass) {
|
|
141
|
+
var item = getEntry(entry);
|
|
142
|
+
return (item && item.getData(pass)) || null;
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Returns how many child elements has on entry (directories) on files it is always 0
|
|
147
|
+
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
|
148
|
+
* @returns {integer}
|
|
149
|
+
*/
|
|
150
|
+
childCount: function (entry) {
|
|
151
|
+
const item = getEntry(entry);
|
|
152
|
+
if (item) {
|
|
153
|
+
return _zip.getChildCount(item);
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Asynchronous readFile
|
|
159
|
+
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
|
160
|
+
* @param {callback} callback
|
|
161
|
+
*
|
|
162
|
+
* @return Buffer or Null in case of error
|
|
163
|
+
*/
|
|
164
|
+
readFileAsync: function (entry, callback) {
|
|
165
|
+
var item = getEntry(entry);
|
|
166
|
+
if (item) {
|
|
167
|
+
item.getDataAsync(callback);
|
|
168
|
+
} else {
|
|
169
|
+
callback(null, "getEntry failed for:" + entry);
|
|
170
|
+
}
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Extracts the given entry from the archive and returns the content as plain text in the given encoding
|
|
175
|
+
* @param {ZipEntry|string} entry - ZipEntry object or String with the full path of the entry
|
|
176
|
+
* @param {string} encoding - Optional. If no encoding is specified utf8 is used
|
|
177
|
+
*
|
|
178
|
+
* @return String
|
|
179
|
+
*/
|
|
180
|
+
readAsText: function (entry, encoding) {
|
|
181
|
+
var item = getEntry(entry);
|
|
182
|
+
if (item) {
|
|
183
|
+
var data = item.getData();
|
|
184
|
+
if (data && data.length) {
|
|
185
|
+
return data.toString(encoding || "utf8");
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return "";
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Asynchronous readAsText
|
|
193
|
+
* @param {ZipEntry|string} entry ZipEntry object or String with the full path of the entry
|
|
194
|
+
* @param {callback} callback
|
|
195
|
+
* @param {string} [encoding] - Optional. If no encoding is specified utf8 is used
|
|
196
|
+
*
|
|
197
|
+
* @return String
|
|
198
|
+
*/
|
|
199
|
+
readAsTextAsync: function (entry, callback, encoding) {
|
|
200
|
+
var item = getEntry(entry);
|
|
201
|
+
if (item) {
|
|
202
|
+
item.getDataAsync(function (data, err) {
|
|
203
|
+
if (err) {
|
|
204
|
+
callback(data, err);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (data && data.length) {
|
|
209
|
+
callback(data.toString(encoding || "utf8"));
|
|
210
|
+
} else {
|
|
211
|
+
callback("");
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
} else {
|
|
215
|
+
callback("");
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Remove the entry from the file or the entry and all it's nested directories and files if the given entry is a directory
|
|
221
|
+
*
|
|
222
|
+
* @param {ZipEntry|string} entry
|
|
223
|
+
* @param {boolean} withsubfolders
|
|
224
|
+
* @returns {void}
|
|
225
|
+
*/
|
|
226
|
+
deleteFile: function (entry, withsubfolders = true) {
|
|
227
|
+
// @TODO: test deleteFile
|
|
228
|
+
var item = getEntry(entry);
|
|
229
|
+
if (item) {
|
|
230
|
+
_zip.deleteFile(item.entryName, withsubfolders);
|
|
231
|
+
}
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Remove the entry from the file or directory without affecting any nested entries
|
|
236
|
+
*
|
|
237
|
+
* @param {ZipEntry|string} entry
|
|
238
|
+
* @returns {void}
|
|
239
|
+
*/
|
|
240
|
+
deleteEntry: function (entry) {
|
|
241
|
+
// @TODO: test deleteEntry
|
|
242
|
+
var item = getEntry(entry);
|
|
243
|
+
if (item) {
|
|
244
|
+
_zip.deleteEntry(item.entryName);
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Adds a comment to the zip. The zip must be rewritten after adding the comment.
|
|
250
|
+
*
|
|
251
|
+
* @param {string} comment
|
|
252
|
+
*/
|
|
253
|
+
addZipComment: function (comment) {
|
|
254
|
+
// @TODO: test addZipComment
|
|
255
|
+
_zip.comment = comment;
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Returns the zip comment
|
|
260
|
+
*
|
|
261
|
+
* @return String
|
|
262
|
+
*/
|
|
263
|
+
getZipComment: function () {
|
|
264
|
+
return _zip.comment || "";
|
|
265
|
+
},
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Adds a comment to a specified zipEntry. The zip must be rewritten after adding the comment
|
|
269
|
+
* The comment cannot exceed 65535 characters in length
|
|
270
|
+
*
|
|
271
|
+
* @param {ZipEntry} entry
|
|
272
|
+
* @param {string} comment
|
|
273
|
+
*/
|
|
274
|
+
addZipEntryComment: function (entry, comment) {
|
|
275
|
+
var item = getEntry(entry);
|
|
276
|
+
if (item) {
|
|
277
|
+
item.comment = comment;
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Returns the comment of the specified entry
|
|
283
|
+
*
|
|
284
|
+
* @param {ZipEntry} entry
|
|
285
|
+
* @return String
|
|
286
|
+
*/
|
|
287
|
+
getZipEntryComment: function (entry) {
|
|
288
|
+
var item = getEntry(entry);
|
|
289
|
+
if (item) {
|
|
290
|
+
return item.comment || "";
|
|
291
|
+
}
|
|
292
|
+
return "";
|
|
293
|
+
},
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Updates the content of an existing entry inside the archive. The zip must be rewritten after updating the content
|
|
297
|
+
*
|
|
298
|
+
* @param {ZipEntry} entry
|
|
299
|
+
* @param {Buffer} content
|
|
300
|
+
*/
|
|
301
|
+
updateFile: function (entry, content) {
|
|
302
|
+
var item = getEntry(entry);
|
|
303
|
+
if (item) {
|
|
304
|
+
item.setData(content);
|
|
305
|
+
}
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Adds a file from the disk to the archive
|
|
310
|
+
*
|
|
311
|
+
* @param {string} localPath File to add to zip
|
|
312
|
+
* @param {string} [zipPath] Optional path inside the zip
|
|
313
|
+
* @param {string} [zipName] Optional name for the file
|
|
314
|
+
* @param {string} [comment] Optional file comment
|
|
315
|
+
*/
|
|
316
|
+
addLocalFile: function (localPath, zipPath, zipName, comment) {
|
|
317
|
+
if (filetools.fs.existsSync(localPath)) {
|
|
318
|
+
// fix ZipPath
|
|
319
|
+
zipPath = zipPath ? fixPath(zipPath) : "";
|
|
320
|
+
|
|
321
|
+
// p - local file name
|
|
322
|
+
const p = pth.win32.basename(pth.win32.normalize(localPath));
|
|
323
|
+
|
|
324
|
+
// add file name into zippath
|
|
325
|
+
zipPath += zipName ? zipName : p;
|
|
326
|
+
|
|
327
|
+
// read file attributes
|
|
328
|
+
const _attr = filetools.fs.statSync(localPath);
|
|
329
|
+
|
|
330
|
+
// get file content
|
|
331
|
+
const data = _attr.isFile() ? filetools.fs.readFileSync(localPath) : Buffer.alloc(0);
|
|
332
|
+
|
|
333
|
+
// if folder
|
|
334
|
+
if (_attr.isDirectory()) zipPath += filetools.sep;
|
|
335
|
+
|
|
336
|
+
// add file into zip file
|
|
337
|
+
this.addFile(zipPath, data, comment, _attr);
|
|
338
|
+
} else {
|
|
339
|
+
throw Utils.Errors.FILE_NOT_FOUND(localPath);
|
|
340
|
+
}
|
|
341
|
+
},
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Callback for showing if everything was done.
|
|
345
|
+
*
|
|
346
|
+
* @callback doneCallback
|
|
347
|
+
* @param {Error} err - Error object
|
|
348
|
+
* @param {boolean} done - was request fully completed
|
|
349
|
+
*/
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Adds a file from the disk to the archive
|
|
353
|
+
*
|
|
354
|
+
* @param {(object|string)} options - options object, if it is string it us used as localPath.
|
|
355
|
+
* @param {string} options.localPath - Local path to the file.
|
|
356
|
+
* @param {string} [options.comment] - Optional file comment.
|
|
357
|
+
* @param {string} [options.zipPath] - Optional path inside the zip
|
|
358
|
+
* @param {string} [options.zipName] - Optional name for the file
|
|
359
|
+
* @param {doneCallback} callback - The callback that handles the response.
|
|
360
|
+
*/
|
|
361
|
+
addLocalFileAsync: function (options, callback) {
|
|
362
|
+
options = typeof options === "object" ? options : { localPath: options };
|
|
363
|
+
const localPath = pth.resolve(options.localPath);
|
|
364
|
+
const { comment } = options;
|
|
365
|
+
let { zipPath, zipName } = options;
|
|
366
|
+
const self = this;
|
|
367
|
+
|
|
368
|
+
filetools.fs.stat(localPath, function (err, stats) {
|
|
369
|
+
if (err) return callback(err, false);
|
|
370
|
+
// fix ZipPath
|
|
371
|
+
zipPath = zipPath ? fixPath(zipPath) : "";
|
|
372
|
+
// p - local file name
|
|
373
|
+
const p = pth.win32.basename(pth.win32.normalize(localPath));
|
|
374
|
+
// add file name into zippath
|
|
375
|
+
zipPath += zipName ? zipName : p;
|
|
376
|
+
|
|
377
|
+
if (stats.isFile()) {
|
|
378
|
+
filetools.fs.readFile(localPath, function (err, data) {
|
|
379
|
+
if (err) return callback(err, false);
|
|
380
|
+
self.addFile(zipPath, data, comment, stats);
|
|
381
|
+
return setImmediate(callback, undefined, true);
|
|
382
|
+
});
|
|
383
|
+
} else if (stats.isDirectory()) {
|
|
384
|
+
zipPath += filetools.sep;
|
|
385
|
+
self.addFile(zipPath, Buffer.alloc(0), comment, stats);
|
|
386
|
+
return setImmediate(callback, undefined, true);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
},
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Adds a local directory and all its nested files and directories to the archive
|
|
393
|
+
*
|
|
394
|
+
* @param {string} localPath - local path to the folder
|
|
395
|
+
* @param {string} [zipPath] - optional path inside zip
|
|
396
|
+
* @param {(RegExp|function)} [filter] - optional RegExp or Function if files match will be included.
|
|
397
|
+
*/
|
|
398
|
+
addLocalFolder: function (localPath, zipPath, filter) {
|
|
399
|
+
// Prepare filter
|
|
400
|
+
filter = filenameFilter(filter);
|
|
401
|
+
|
|
402
|
+
// fix ZipPath
|
|
403
|
+
zipPath = zipPath ? fixPath(zipPath) : "";
|
|
404
|
+
|
|
405
|
+
// normalize the path first
|
|
406
|
+
localPath = pth.normalize(localPath);
|
|
407
|
+
|
|
408
|
+
if (filetools.fs.existsSync(localPath)) {
|
|
409
|
+
const items = filetools.findFiles(localPath);
|
|
410
|
+
const self = this;
|
|
411
|
+
|
|
412
|
+
if (items.length) {
|
|
413
|
+
for (const filepath of items) {
|
|
414
|
+
const p = pth.join(zipPath, relativePath(localPath, filepath));
|
|
415
|
+
if (filter(p)) {
|
|
416
|
+
self.addLocalFile(filepath, pth.dirname(p));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
throw Utils.Errors.FILE_NOT_FOUND(localPath);
|
|
422
|
+
}
|
|
423
|
+
},
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Asynchronous addLocalFolder
|
|
427
|
+
* @param {string} localPath
|
|
428
|
+
* @param {callback} callback
|
|
429
|
+
* @param {string} [zipPath] optional path inside zip
|
|
430
|
+
* @param {RegExp|function} [filter] optional RegExp or Function if files match will
|
|
431
|
+
* be included.
|
|
432
|
+
*/
|
|
433
|
+
addLocalFolderAsync: function (localPath, callback, zipPath, filter) {
|
|
434
|
+
// Prepare filter
|
|
435
|
+
filter = filenameFilter(filter);
|
|
436
|
+
|
|
437
|
+
// fix ZipPath
|
|
438
|
+
zipPath = zipPath ? fixPath(zipPath) : "";
|
|
439
|
+
|
|
440
|
+
// normalize the path first
|
|
441
|
+
localPath = pth.normalize(localPath);
|
|
442
|
+
|
|
443
|
+
var self = this;
|
|
444
|
+
filetools.fs.open(localPath, "r", function (err) {
|
|
445
|
+
if (err && err.code === "ENOENT") {
|
|
446
|
+
callback(undefined, Utils.Errors.FILE_NOT_FOUND(localPath));
|
|
447
|
+
} else if (err) {
|
|
448
|
+
callback(undefined, err);
|
|
449
|
+
} else {
|
|
450
|
+
var items = filetools.findFiles(localPath);
|
|
451
|
+
var i = -1;
|
|
452
|
+
|
|
453
|
+
var next = function () {
|
|
454
|
+
i += 1;
|
|
455
|
+
if (i < items.length) {
|
|
456
|
+
var filepath = items[i];
|
|
457
|
+
var p = relativePath(localPath, filepath).split("\\").join("/"); //windows fix
|
|
458
|
+
p = p
|
|
459
|
+
.normalize("NFD")
|
|
460
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
461
|
+
.replace(/[^\x20-\x7E]/g, ""); // accent fix
|
|
462
|
+
if (filter(p)) {
|
|
463
|
+
filetools.fs.stat(filepath, function (er0, stats) {
|
|
464
|
+
if (er0) callback(undefined, er0);
|
|
465
|
+
if (stats.isFile()) {
|
|
466
|
+
filetools.fs.readFile(filepath, function (er1, data) {
|
|
467
|
+
if (er1) {
|
|
468
|
+
callback(undefined, er1);
|
|
469
|
+
} else {
|
|
470
|
+
self.addFile(zipPath + p, data, "", stats);
|
|
471
|
+
next();
|
|
472
|
+
}
|
|
473
|
+
});
|
|
474
|
+
} else {
|
|
475
|
+
self.addFile(zipPath + p + "/", Buffer.alloc(0), "", stats);
|
|
476
|
+
next();
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
} else {
|
|
480
|
+
process.nextTick(() => {
|
|
481
|
+
next();
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
} else {
|
|
485
|
+
callback(true, undefined);
|
|
486
|
+
}
|
|
487
|
+
};
|
|
488
|
+
|
|
489
|
+
next();
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
},
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Adds a local directory and all its nested files and directories to the archive
|
|
496
|
+
*
|
|
497
|
+
* @param {object | string} options - options object, if it is string it us used as localPath.
|
|
498
|
+
* @param {string} options.localPath - Local path to the folder.
|
|
499
|
+
* @param {string} [options.zipPath] - optional path inside zip.
|
|
500
|
+
* @param {RegExp|function} [options.filter] - optional RegExp or Function if files match will be included.
|
|
501
|
+
* @param {function|string} [options.namefix] - optional function to help fix filename
|
|
502
|
+
* @param {doneCallback} callback - The callback that handles the response.
|
|
503
|
+
*
|
|
504
|
+
*/
|
|
505
|
+
addLocalFolderAsync2: function (options, callback) {
|
|
506
|
+
const self = this;
|
|
507
|
+
options = typeof options === "object" ? options : { localPath: options };
|
|
508
|
+
// Resolve the local filesystem path with the platform resolver. Do NOT
|
|
509
|
+
// run it through fixPath: that normalizes ZIP-internal paths to POSIX
|
|
510
|
+
// and prepends "/", which turns a Windows path like C:\dir into
|
|
511
|
+
// \C:\dir, so readdir finds nothing and the archive comes out empty.
|
|
512
|
+
const localPath = pth.resolve(options.localPath);
|
|
513
|
+
let { zipPath, filter, namefix } = options;
|
|
514
|
+
|
|
515
|
+
if (filter instanceof RegExp) {
|
|
516
|
+
filter = (function (rx) {
|
|
517
|
+
return function (filename) {
|
|
518
|
+
return rx.test(filename);
|
|
519
|
+
};
|
|
520
|
+
})(filter);
|
|
521
|
+
} else if ("function" !== typeof filter) {
|
|
522
|
+
filter = function () {
|
|
523
|
+
return true;
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// fix ZipPath
|
|
528
|
+
zipPath = zipPath ? fixPath(zipPath) : "";
|
|
529
|
+
|
|
530
|
+
// Check Namefix function
|
|
531
|
+
if (namefix === "latin1") {
|
|
532
|
+
namefix = (str) =>
|
|
533
|
+
str
|
|
534
|
+
.normalize("NFD")
|
|
535
|
+
.replace(/[\u0300-\u036f]/g, "")
|
|
536
|
+
.replace(/[^\x20-\x7E]/g, ""); // accent fix (latin1 characers only)
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (typeof namefix !== "function") namefix = (str) => str;
|
|
540
|
+
|
|
541
|
+
// internal, create relative path + fix the name
|
|
542
|
+
const relPathFix = (entry) => pth.join(zipPath, namefix(relativePath(localPath, entry)));
|
|
543
|
+
const fileNameFix = (entry) => pth.win32.basename(pth.win32.normalize(namefix(entry)));
|
|
544
|
+
|
|
545
|
+
filetools.fs.open(localPath, "r", function (err) {
|
|
546
|
+
if (err && err.code === "ENOENT") {
|
|
547
|
+
// callback is (err, done); errors belong in the first argument,
|
|
548
|
+
// otherwise addLocalFolderPromise treats the error as "done" and
|
|
549
|
+
// resolves instead of rejecting.
|
|
550
|
+
callback(Utils.Errors.FILE_NOT_FOUND(localPath), false);
|
|
551
|
+
} else if (err) {
|
|
552
|
+
callback(err, false);
|
|
553
|
+
} else {
|
|
554
|
+
filetools.findFilesAsync(localPath, function (err, fileEntries) {
|
|
555
|
+
if (err) return callback(err, false);
|
|
556
|
+
fileEntries = fileEntries.filter((dir) => filter(relPathFix(dir)));
|
|
557
|
+
// Nothing to add (empty folder or everything filtered out) is a
|
|
558
|
+
// success, not an error. Report done and stop, otherwise the
|
|
559
|
+
// reduce below runs and the callback fires a second time -- and
|
|
560
|
+
// signalling done=false left addLocalFolderPromise hanging.
|
|
561
|
+
if (!fileEntries.length) return callback(undefined, true);
|
|
562
|
+
|
|
563
|
+
setImmediate(
|
|
564
|
+
fileEntries.reverse().reduce(function (next, entry) {
|
|
565
|
+
return function (err, done) {
|
|
566
|
+
if (err || done === false) return setImmediate(next, err, false);
|
|
567
|
+
|
|
568
|
+
self.addLocalFileAsync(
|
|
569
|
+
{
|
|
570
|
+
localPath: entry,
|
|
571
|
+
zipPath: pth.dirname(relPathFix(entry)),
|
|
572
|
+
zipName: fileNameFix(entry)
|
|
573
|
+
},
|
|
574
|
+
next
|
|
575
|
+
);
|
|
576
|
+
};
|
|
577
|
+
}, callback)
|
|
578
|
+
);
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
},
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* Adds a local directory and all its nested files and directories to the archive
|
|
586
|
+
*
|
|
587
|
+
* @param {string} localPath - path where files will be extracted
|
|
588
|
+
* @param {object} props - optional properties
|
|
589
|
+
* @param {string} [props.zipPath] - optional path inside zip
|
|
590
|
+
* @param {RegExp|function} [props.filter] - optional RegExp or Function if files match will be included.
|
|
591
|
+
* @param {function|string} [props.namefix] - optional function to help fix filename
|
|
592
|
+
*/
|
|
593
|
+
addLocalFolderPromise: function (localPath, props) {
|
|
594
|
+
return new Promise((resolve, reject) => {
|
|
595
|
+
this.addLocalFolderAsync2(Object.assign({ localPath }, props), (err, done) => {
|
|
596
|
+
if (err) return reject(err);
|
|
597
|
+
if (done) resolve(this);
|
|
598
|
+
});
|
|
599
|
+
});
|
|
600
|
+
},
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Allows you to create a entry (file or directory) in the zip file.
|
|
604
|
+
* If you want to create a directory the entryName must end in / and a null buffer should be provided.
|
|
605
|
+
* Comment and attributes are optional
|
|
606
|
+
*
|
|
607
|
+
* @param {string} entryName
|
|
608
|
+
* @param {Buffer | string} content - file content as buffer or utf8 coded string
|
|
609
|
+
* @param {string} [comment] - file comment
|
|
610
|
+
* @param {number | object} [attr] - number as unix file permissions, object as filesystem Stats object
|
|
611
|
+
*/
|
|
612
|
+
addFile: function (entryName, content, comment, attr) {
|
|
613
|
+
entryName = zipnamefix(entryName);
|
|
614
|
+
let entry = getEntry(entryName);
|
|
615
|
+
const update = entry != null;
|
|
616
|
+
|
|
617
|
+
// prepare new entry
|
|
618
|
+
if (!update) {
|
|
619
|
+
entry = new ZipEntry(opts);
|
|
620
|
+
entry.entryName = entryName;
|
|
621
|
+
}
|
|
622
|
+
entry.comment = comment || "";
|
|
623
|
+
|
|
624
|
+
const isStat = "object" === typeof attr && attr instanceof filetools.fs.Stats;
|
|
625
|
+
|
|
626
|
+
// last modification time from file stats
|
|
627
|
+
if (isStat) {
|
|
628
|
+
entry.header.time = attr.mtime;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// Set file attribute
|
|
632
|
+
var fileattr = entry.isDirectory ? 0x10 : 0; // (MS-DOS directory flag)
|
|
633
|
+
|
|
634
|
+
// extended attributes field for Unix
|
|
635
|
+
// set file type either S_IFDIR / S_IFREG
|
|
636
|
+
let unix = entry.isDirectory ? 0x4000 : 0x8000;
|
|
637
|
+
|
|
638
|
+
if (isStat) {
|
|
639
|
+
// File attributes from file stats
|
|
640
|
+
unix |= 0xfff & attr.mode;
|
|
641
|
+
} else if ("number" === typeof attr) {
|
|
642
|
+
// attr from given attr values
|
|
643
|
+
unix |= 0xfff & attr;
|
|
644
|
+
} else {
|
|
645
|
+
// Default values:
|
|
646
|
+
unix |= entry.isDirectory ? 0o755 : 0o644; // permissions (drwxr-xr-x) or (-r-wr--r--)
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
fileattr = (fileattr | (unix << 16)) >>> 0; // add attributes
|
|
650
|
+
|
|
651
|
+
entry.attr = fileattr;
|
|
652
|
+
|
|
653
|
+
entry.setData(content);
|
|
654
|
+
if (!update) _zip.setEntry(entry);
|
|
655
|
+
|
|
656
|
+
return entry;
|
|
657
|
+
},
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Returns an array of ZipEntry objects representing the files and folders inside the archive
|
|
661
|
+
*
|
|
662
|
+
* @param {string} [password]
|
|
663
|
+
* @returns Array
|
|
664
|
+
*/
|
|
665
|
+
getEntries: function (password) {
|
|
666
|
+
_zip.password = password;
|
|
667
|
+
return _zip ? _zip.entries : [];
|
|
668
|
+
},
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Returns a ZipEntry object representing the file or folder specified by ``name``.
|
|
672
|
+
*
|
|
673
|
+
* @param {string} name
|
|
674
|
+
* @return ZipEntry
|
|
675
|
+
*/
|
|
676
|
+
getEntry: function (/**String*/ name) {
|
|
677
|
+
return getEntry(name);
|
|
678
|
+
},
|
|
679
|
+
|
|
680
|
+
getEntryCount: function () {
|
|
681
|
+
return _zip.getEntryCount();
|
|
682
|
+
},
|
|
683
|
+
|
|
684
|
+
forEach: function (callback) {
|
|
685
|
+
return _zip.forEach(callback);
|
|
686
|
+
},
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* Extracts the given entry to the given targetPath
|
|
690
|
+
* If the entry is a directory inside the archive, the entire directory and it's subdirectories will be extracted
|
|
691
|
+
*
|
|
692
|
+
* @param {string|ZipEntry} entry - ZipEntry object or String with the full path of the entry
|
|
693
|
+
* @param {string} targetPath - Target folder where to write the file
|
|
694
|
+
* @param {boolean} [maintainEntryPath=true] - If maintainEntryPath is true and the entry is inside a folder, the entry folder will be created in targetPath as well. Default is TRUE
|
|
695
|
+
* @param {boolean} [overwrite=false] - If the file already exists at the target path, the file will be overwriten if this is true.
|
|
696
|
+
* @param {boolean} [keepOriginalPermission=false] - The file will be set as the permission from the entry if this is true.
|
|
697
|
+
* @param {string} [outFileName] - String If set will override the filename of the extracted file (Only works if the entry is a file)
|
|
698
|
+
*
|
|
699
|
+
* @return Boolean
|
|
700
|
+
*/
|
|
701
|
+
extractEntryTo: function (entry, targetPath, maintainEntryPath, overwrite, keepOriginalPermission, outFileName) {
|
|
702
|
+
overwrite = get_Bool(false, overwrite);
|
|
703
|
+
keepOriginalPermission = get_Bool(false, keepOriginalPermission);
|
|
704
|
+
maintainEntryPath = get_Bool(true, maintainEntryPath);
|
|
705
|
+
outFileName = get_Str(keepOriginalPermission, outFileName);
|
|
706
|
+
|
|
707
|
+
var item = getEntry(entry);
|
|
708
|
+
if (!item) {
|
|
709
|
+
throw Utils.Errors.NO_ENTRY();
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
var entryName = canonical(item.entryName);
|
|
713
|
+
|
|
714
|
+
var target = sanitize(targetPath, outFileName && !item.isDirectory ? canonical(outFileName) : maintainEntryPath ? entryName : pth.basename(entryName));
|
|
715
|
+
|
|
716
|
+
if (item.isDirectory) {
|
|
717
|
+
var children = _zip.getEntryChildren(item);
|
|
718
|
+
children.forEach(function (child) {
|
|
719
|
+
if (child.isDirectory) return;
|
|
720
|
+
var content = child.getData();
|
|
721
|
+
if (!content) {
|
|
722
|
+
throw Utils.Errors.CANT_EXTRACT_FILE();
|
|
723
|
+
}
|
|
724
|
+
// When not maintaining the full entry path, keep each child's path
|
|
725
|
+
// relative to the extracted directory (drop the directory's own
|
|
726
|
+
// prefix) instead of flattening every file to its basename, which
|
|
727
|
+
// collapsed subdirectories together (issue #306).
|
|
728
|
+
var name = canonical(maintainEntryPath ? child.entryName : child.entryName.substring(item.entryName.length));
|
|
729
|
+
var childName = sanitize(targetPath, name);
|
|
730
|
+
// reject writing through a pre-existing symlink inside the target
|
|
731
|
+
filetools.assertPathSafe(targetPath, childName);
|
|
732
|
+
// The reverse operation for attr depend on method addFile()
|
|
733
|
+
const fileAttr = keepOriginalPermission ? child.header.fileAttr : undefined;
|
|
734
|
+
filetools.writeFileTo(childName, content, overwrite, fileAttr);
|
|
735
|
+
});
|
|
736
|
+
return true;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
var content = item.getData(_zip.password);
|
|
740
|
+
if (!content) throw Utils.Errors.CANT_EXTRACT_FILE();
|
|
741
|
+
|
|
742
|
+
// reject writing through a pre-existing symlink inside the target
|
|
743
|
+
filetools.assertPathSafe(targetPath, target);
|
|
744
|
+
|
|
745
|
+
if (filetools.fs.existsSync(target) && !overwrite) {
|
|
746
|
+
throw Utils.Errors.CANT_OVERRIDE();
|
|
747
|
+
}
|
|
748
|
+
// The reverse operation for attr depend on method addFile()
|
|
749
|
+
const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
|
|
750
|
+
filetools.writeFileTo(target, content, overwrite, fileAttr);
|
|
751
|
+
|
|
752
|
+
return true;
|
|
753
|
+
},
|
|
754
|
+
|
|
755
|
+
/**
|
|
756
|
+
* Test the archive
|
|
757
|
+
* @param {string} [pass]
|
|
758
|
+
*/
|
|
759
|
+
test: function (pass) {
|
|
760
|
+
if (!_zip) {
|
|
761
|
+
return false;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
for (var entry of _zip.entries) {
|
|
765
|
+
try {
|
|
766
|
+
if (entry.isDirectory) {
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
// was `_zip.entries[entry]` (indexing the array with an entry
|
|
770
|
+
// object -> undefined -> threw -> test() always returned false)
|
|
771
|
+
var content = entry.getData(pass);
|
|
772
|
+
if (!content) {
|
|
773
|
+
return false;
|
|
774
|
+
}
|
|
775
|
+
} catch (err) {
|
|
776
|
+
return false;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
return true;
|
|
780
|
+
},
|
|
781
|
+
|
|
782
|
+
/**
|
|
783
|
+
* Extracts the entire archive to the given location
|
|
784
|
+
*
|
|
785
|
+
* @param {string} targetPath Target location
|
|
786
|
+
* @param {boolean} [overwrite=false] If the file already exists at the target path, the file will be overwriten if this is true.
|
|
787
|
+
* Default is FALSE
|
|
788
|
+
* @param {boolean} [keepOriginalPermission=false] The file will be set as the permission from the entry if this is true.
|
|
789
|
+
* Default is FALSE
|
|
790
|
+
* @param {string|Buffer} [pass] password
|
|
791
|
+
*/
|
|
792
|
+
extractAllTo: function (targetPath, overwrite, keepOriginalPermission, pass) {
|
|
793
|
+
keepOriginalPermission = get_Bool(false, keepOriginalPermission);
|
|
794
|
+
pass = get_Str(keepOriginalPermission, pass);
|
|
795
|
+
overwrite = get_Bool(false, overwrite);
|
|
796
|
+
if (!_zip) throw Utils.Errors.NO_ZIP();
|
|
797
|
+
|
|
798
|
+
const dirEntries = [];
|
|
799
|
+
_zip.entries.forEach(function (entry) {
|
|
800
|
+
var entryName = sanitize(targetPath, canonical(entry.entryName));
|
|
801
|
+
// reject writing through a pre-existing symlink inside the target
|
|
802
|
+
filetools.assertPathSafe(targetPath, entryName);
|
|
803
|
+
if (entry.isDirectory) {
|
|
804
|
+
filetools.makeDir(entryName);
|
|
805
|
+
// defer restoring the directory permission until its files are written
|
|
806
|
+
if (keepOriginalPermission) dirEntries.push({ path: entryName, attr: entry.header.fileAttr });
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
var content = entry.getData(pass);
|
|
810
|
+
if (!content) {
|
|
811
|
+
throw Utils.Errors.CANT_EXTRACT_FILE();
|
|
812
|
+
}
|
|
813
|
+
// The reverse operation for attr depend on method addFile()
|
|
814
|
+
const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
|
|
815
|
+
filetools.writeFileTo(entryName, content, overwrite, fileAttr);
|
|
816
|
+
try {
|
|
817
|
+
// best-effort: an invalid date in the archive or a filesystem that
|
|
818
|
+
// rejects utimes must not fail extraction of already-written content (issue #379)
|
|
819
|
+
filetools.fs.utimesSync(entryName, entry.header.time, entry.header.time);
|
|
820
|
+
} catch (err) {
|
|
821
|
+
/* ignore timestamp failures */
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
applyDirAttributes(dirEntries);
|
|
826
|
+
},
|
|
827
|
+
|
|
828
|
+
/**
|
|
829
|
+
* Asynchronous extractAllTo
|
|
830
|
+
*
|
|
831
|
+
* @param {string} targetPath Target location
|
|
832
|
+
* @param {boolean} [overwrite=false] If the file already exists at the target path, the file will be overwriten if this is true.
|
|
833
|
+
* Default is FALSE
|
|
834
|
+
* @param {boolean} [keepOriginalPermission=false] The file will be set as the permission from the entry if this is true.
|
|
835
|
+
* Default is FALSE
|
|
836
|
+
* @param {function} callback The callback will be executed when all entries are extracted successfully or any error is thrown.
|
|
837
|
+
*/
|
|
838
|
+
extractAllToAsync: function (targetPath, overwrite, keepOriginalPermission, callback) {
|
|
839
|
+
callback = get_Fun(overwrite, keepOriginalPermission, callback);
|
|
840
|
+
keepOriginalPermission = get_Bool(false, keepOriginalPermission);
|
|
841
|
+
overwrite = get_Bool(false, overwrite);
|
|
842
|
+
if (!callback) {
|
|
843
|
+
return new Promise((resolve, reject) => {
|
|
844
|
+
this.extractAllToAsync(targetPath, overwrite, keepOriginalPermission, function (err) {
|
|
845
|
+
if (err) {
|
|
846
|
+
reject(err);
|
|
847
|
+
} else {
|
|
848
|
+
resolve(this);
|
|
849
|
+
}
|
|
850
|
+
});
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
if (!_zip) {
|
|
854
|
+
callback(Utils.Errors.NO_ZIP());
|
|
855
|
+
return;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
targetPath = pth.resolve(targetPath);
|
|
859
|
+
// convert entryName to
|
|
860
|
+
const getPath = (entry) => sanitize(targetPath, pth.normalize(canonical(entry.entryName)));
|
|
861
|
+
const getError = (msg, file) => new Error(msg + ': "' + file + '"');
|
|
862
|
+
|
|
863
|
+
// separate directories from files
|
|
864
|
+
const dirEntries = [];
|
|
865
|
+
const fileEntries = [];
|
|
866
|
+
_zip.entries.forEach((e) => {
|
|
867
|
+
if (e.isDirectory) {
|
|
868
|
+
dirEntries.push(e);
|
|
869
|
+
} else {
|
|
870
|
+
fileEntries.push(e);
|
|
871
|
+
}
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
// Create directory entries first synchronously
|
|
875
|
+
// this prevents race condition and assures folders are there before writing files
|
|
876
|
+
const deferredDirAttr = [];
|
|
877
|
+
for (const entry of dirEntries) {
|
|
878
|
+
const dirPath = getPath(entry);
|
|
879
|
+
// The reverse operation for attr depend on method addFile()
|
|
880
|
+
const dirAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
|
|
881
|
+
try {
|
|
882
|
+
// reject writing through a pre-existing symlink inside the target
|
|
883
|
+
filetools.assertPathSafe(targetPath, dirPath);
|
|
884
|
+
filetools.makeDir(dirPath);
|
|
885
|
+
} catch (er) {
|
|
886
|
+
callback(getError("Unable to create folder", dirPath));
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
// defer restoring the directory permission until its files are written:
|
|
890
|
+
// a restrictive mode applied now would block writing them
|
|
891
|
+
if (dirAttr) deferredDirAttr.push({ path: dirPath, attr: dirAttr });
|
|
892
|
+
try {
|
|
893
|
+
// in unix timestamp will change if files are later added to folder, but still.
|
|
894
|
+
// best-effort: a utimes failure must not abort extraction (issue #379)
|
|
895
|
+
filetools.fs.utimesSync(dirPath, entry.header.time, entry.header.time);
|
|
896
|
+
} catch (er) {
|
|
897
|
+
/* ignore timestamp failures */
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// restore directory permissions once every file has been extracted
|
|
902
|
+
const done = (err) => {
|
|
903
|
+
if (!err) {
|
|
904
|
+
try {
|
|
905
|
+
applyDirAttributes(deferredDirAttr);
|
|
906
|
+
} catch (er) {
|
|
907
|
+
return callback(getError("Unable to set folder permissions", er.path || ""));
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
callback(err);
|
|
911
|
+
};
|
|
912
|
+
|
|
913
|
+
fileEntries.reverse().reduce(function (next, entry) {
|
|
914
|
+
return function (err) {
|
|
915
|
+
if (err) {
|
|
916
|
+
next(err);
|
|
917
|
+
} else {
|
|
918
|
+
const entryName = pth.normalize(canonical(entry.entryName));
|
|
919
|
+
const filePath = sanitize(targetPath, entryName);
|
|
920
|
+
try {
|
|
921
|
+
// reject writing through a pre-existing symlink inside the target
|
|
922
|
+
filetools.assertPathSafe(targetPath, filePath);
|
|
923
|
+
} catch (er) {
|
|
924
|
+
return next(er);
|
|
925
|
+
}
|
|
926
|
+
entry.getDataAsync(function (content, err_1) {
|
|
927
|
+
if (err_1) {
|
|
928
|
+
next(err_1);
|
|
929
|
+
} else if (!content) {
|
|
930
|
+
next(Utils.Errors.CANT_EXTRACT_FILE());
|
|
931
|
+
} else {
|
|
932
|
+
// The reverse operation for attr depend on method addFile()
|
|
933
|
+
const fileAttr = keepOriginalPermission ? entry.header.fileAttr : undefined;
|
|
934
|
+
filetools.writeFileToAsync(filePath, content, overwrite, fileAttr, function (succ) {
|
|
935
|
+
if (!succ) {
|
|
936
|
+
return next(getError("Unable to write file", filePath));
|
|
937
|
+
}
|
|
938
|
+
filetools.fs.utimes(filePath, entry.header.time, entry.header.time, function () {
|
|
939
|
+
// best-effort: a utimes failure must not abort extraction
|
|
940
|
+
// of already-written content (issue #379)
|
|
941
|
+
next();
|
|
942
|
+
});
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
}, done)();
|
|
949
|
+
},
|
|
950
|
+
|
|
951
|
+
/**
|
|
952
|
+
* Writes the newly created zip file to disk at the specified location or if a zip was opened and no ``targetFileName`` is provided, it will overwrite the opened zip
|
|
953
|
+
*
|
|
954
|
+
* @param {string} targetFileName
|
|
955
|
+
* @param {function} callback
|
|
956
|
+
*/
|
|
957
|
+
writeZip: function (targetFileName, callback) {
|
|
958
|
+
if (arguments.length === 1) {
|
|
959
|
+
if (typeof targetFileName === "function") {
|
|
960
|
+
callback = targetFileName;
|
|
961
|
+
targetFileName = "";
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
if (!targetFileName && opts.filename) {
|
|
966
|
+
targetFileName = opts.filename;
|
|
967
|
+
}
|
|
968
|
+
if (!targetFileName) return;
|
|
969
|
+
|
|
970
|
+
var zipData = _zip.compressToBuffer();
|
|
971
|
+
if (zipData) {
|
|
972
|
+
var ok = filetools.writeFileTo(targetFileName, zipData, true);
|
|
973
|
+
if (typeof callback === "function") callback(!ok ? new Error("failed") : null, "");
|
|
974
|
+
}
|
|
975
|
+
},
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
*
|
|
979
|
+
* @param {string} targetFileName
|
|
980
|
+
* @param {object} [props]
|
|
981
|
+
* @param {boolean} [props.overwrite=true] If the file already exists at the target path, the file will be overwriten if this is true.
|
|
982
|
+
* @param {boolean} [props.perm] The file will be set as the permission from the entry if this is true.
|
|
983
|
+
|
|
984
|
+
* @returns {Promise<void>}
|
|
985
|
+
*/
|
|
986
|
+
writeZipPromise: function (/**String*/ targetFileName, /* object */ props) {
|
|
987
|
+
const { overwrite, perm } = Object.assign({ overwrite: true }, props);
|
|
988
|
+
|
|
989
|
+
return new Promise((resolve, reject) => {
|
|
990
|
+
// find file name
|
|
991
|
+
if (!targetFileName && opts.filename) targetFileName = opts.filename;
|
|
992
|
+
if (!targetFileName) reject("ADM-ZIP: ZIP File Name Missing");
|
|
993
|
+
|
|
994
|
+
this.toBufferPromise().then((zipData) => {
|
|
995
|
+
const ret = (done) => (done ? resolve(done) : reject("ADM-ZIP: Wasn't able to write zip file"));
|
|
996
|
+
filetools.writeFileToAsync(targetFileName, zipData, overwrite, perm, ret);
|
|
997
|
+
}, reject);
|
|
998
|
+
});
|
|
999
|
+
},
|
|
1000
|
+
|
|
1001
|
+
/**
|
|
1002
|
+
* @returns {Promise<Buffer>} A promise to the Buffer.
|
|
1003
|
+
*/
|
|
1004
|
+
toBufferPromise: function () {
|
|
1005
|
+
return new Promise((resolve, reject) => {
|
|
1006
|
+
_zip.toAsyncBuffer(resolve, reject);
|
|
1007
|
+
});
|
|
1008
|
+
},
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Returns the content of the entire zip file as a Buffer object
|
|
1012
|
+
*
|
|
1013
|
+
* @prop {function} [onSuccess]
|
|
1014
|
+
* @prop {function} [onFail]
|
|
1015
|
+
* @prop {function} [onItemStart]
|
|
1016
|
+
* @prop {function} [onItemEnd]
|
|
1017
|
+
* @returns {Buffer}
|
|
1018
|
+
*/
|
|
1019
|
+
toBuffer: function (onSuccess, onFail, onItemStart, onItemEnd) {
|
|
1020
|
+
if (typeof onSuccess === "function") {
|
|
1021
|
+
_zip.toAsyncBuffer(onSuccess, onFail, onItemStart, onItemEnd);
|
|
1022
|
+
return null;
|
|
1023
|
+
}
|
|
1024
|
+
return _zip.compressToBuffer();
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
};
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
/***/ },
|
|
1031
|
+
|
|
1032
|
+
/***/ 20537
|
|
1033
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
1034
|
+
|
|
1035
|
+
var Utils = __webpack_require__(6942),
|
|
1036
|
+
Constants = Utils.Constants;
|
|
1037
|
+
|
|
1038
|
+
/* The central directory file header */
|
|
1039
|
+
module.exports = function () {
|
|
1040
|
+
var _verMade = 20, // v2.0
|
|
1041
|
+
_version = 10, // v1.0
|
|
1042
|
+
_flags = 0,
|
|
1043
|
+
_method = 0,
|
|
1044
|
+
_time = 0,
|
|
1045
|
+
_crc = 0,
|
|
1046
|
+
_compressedSize = 0,
|
|
1047
|
+
_size = 0,
|
|
1048
|
+
_fnameLen = 0,
|
|
1049
|
+
_extraLen = 0,
|
|
1050
|
+
_comLen = 0,
|
|
1051
|
+
_diskStart = 0,
|
|
1052
|
+
_inattr = 0,
|
|
1053
|
+
_attr = 0,
|
|
1054
|
+
_offset = 0;
|
|
1055
|
+
|
|
1056
|
+
_verMade |= Utils.isWin ? 0x0a00 : 0x0300;
|
|
1057
|
+
|
|
1058
|
+
// Set EFS flag since filename and comment fields are all by default encoded using UTF-8.
|
|
1059
|
+
// Without it file names may be corrupted for other apps when file names use unicode chars
|
|
1060
|
+
_flags |= Constants.FLG_EFS;
|
|
1061
|
+
|
|
1062
|
+
const _localHeader = {
|
|
1063
|
+
extraLen: 0
|
|
1064
|
+
};
|
|
1065
|
+
|
|
1066
|
+
// casting
|
|
1067
|
+
const uint32 = (val) => Math.max(0, val) >>> 0;
|
|
1068
|
+
const uint16 = (val) => Math.max(0, val) & 0xffff;
|
|
1069
|
+
const uint8 = (val) => Math.max(0, val) & 0xff;
|
|
1070
|
+
|
|
1071
|
+
_time = Utils.fromDate2DOS(new Date());
|
|
1072
|
+
|
|
1073
|
+
return {
|
|
1074
|
+
get made() {
|
|
1075
|
+
return _verMade;
|
|
1076
|
+
},
|
|
1077
|
+
set made(val) {
|
|
1078
|
+
_verMade = val;
|
|
1079
|
+
},
|
|
1080
|
+
|
|
1081
|
+
get version() {
|
|
1082
|
+
return _version;
|
|
1083
|
+
},
|
|
1084
|
+
set version(val) {
|
|
1085
|
+
_version = val;
|
|
1086
|
+
},
|
|
1087
|
+
|
|
1088
|
+
get flags() {
|
|
1089
|
+
return _flags;
|
|
1090
|
+
},
|
|
1091
|
+
set flags(val) {
|
|
1092
|
+
_flags = val;
|
|
1093
|
+
},
|
|
1094
|
+
|
|
1095
|
+
get flags_efs() {
|
|
1096
|
+
return (_flags & Constants.FLG_EFS) > 0;
|
|
1097
|
+
},
|
|
1098
|
+
set flags_efs(val) {
|
|
1099
|
+
if (val) {
|
|
1100
|
+
_flags |= Constants.FLG_EFS;
|
|
1101
|
+
} else {
|
|
1102
|
+
_flags &= ~Constants.FLG_EFS;
|
|
1103
|
+
}
|
|
1104
|
+
},
|
|
1105
|
+
|
|
1106
|
+
get flags_desc() {
|
|
1107
|
+
return (_flags & Constants.FLG_DESC) > 0;
|
|
1108
|
+
},
|
|
1109
|
+
set flags_desc(val) {
|
|
1110
|
+
if (val) {
|
|
1111
|
+
_flags |= Constants.FLG_DESC;
|
|
1112
|
+
} else {
|
|
1113
|
+
_flags &= ~Constants.FLG_DESC;
|
|
1114
|
+
}
|
|
1115
|
+
},
|
|
1116
|
+
|
|
1117
|
+
get method() {
|
|
1118
|
+
return _method;
|
|
1119
|
+
},
|
|
1120
|
+
set method(val) {
|
|
1121
|
+
switch (val) {
|
|
1122
|
+
case Constants.STORED:
|
|
1123
|
+
this.version = 10;
|
|
1124
|
+
break;
|
|
1125
|
+
case Constants.DEFLATED:
|
|
1126
|
+
default:
|
|
1127
|
+
this.version = 20;
|
|
1128
|
+
}
|
|
1129
|
+
_method = val;
|
|
1130
|
+
},
|
|
1131
|
+
|
|
1132
|
+
get time() {
|
|
1133
|
+
return Utils.fromDOS2Date(this.timeval);
|
|
1134
|
+
},
|
|
1135
|
+
set time(val) {
|
|
1136
|
+
val = new Date(val);
|
|
1137
|
+
this.timeval = Utils.fromDate2DOS(val);
|
|
1138
|
+
},
|
|
1139
|
+
|
|
1140
|
+
get timeval() {
|
|
1141
|
+
return _time;
|
|
1142
|
+
},
|
|
1143
|
+
set timeval(val) {
|
|
1144
|
+
_time = uint32(val);
|
|
1145
|
+
},
|
|
1146
|
+
|
|
1147
|
+
get timeHighByte() {
|
|
1148
|
+
return uint8(_time >>> 8);
|
|
1149
|
+
},
|
|
1150
|
+
get crc() {
|
|
1151
|
+
return _crc;
|
|
1152
|
+
},
|
|
1153
|
+
set crc(val) {
|
|
1154
|
+
_crc = uint32(val);
|
|
1155
|
+
},
|
|
1156
|
+
|
|
1157
|
+
get compressedSize() {
|
|
1158
|
+
return _compressedSize;
|
|
1159
|
+
},
|
|
1160
|
+
set compressedSize(val) {
|
|
1161
|
+
_compressedSize = uint32(val);
|
|
1162
|
+
},
|
|
1163
|
+
|
|
1164
|
+
get size() {
|
|
1165
|
+
return _size;
|
|
1166
|
+
},
|
|
1167
|
+
set size(val) {
|
|
1168
|
+
_size = uint32(val);
|
|
1169
|
+
},
|
|
1170
|
+
|
|
1171
|
+
get fileNameLength() {
|
|
1172
|
+
return _fnameLen;
|
|
1173
|
+
},
|
|
1174
|
+
set fileNameLength(val) {
|
|
1175
|
+
_fnameLen = val;
|
|
1176
|
+
},
|
|
1177
|
+
|
|
1178
|
+
get extraLength() {
|
|
1179
|
+
return _extraLen;
|
|
1180
|
+
},
|
|
1181
|
+
set extraLength(val) {
|
|
1182
|
+
_extraLen = val;
|
|
1183
|
+
},
|
|
1184
|
+
|
|
1185
|
+
get extraLocalLength() {
|
|
1186
|
+
return _localHeader.extraLen;
|
|
1187
|
+
},
|
|
1188
|
+
set extraLocalLength(val) {
|
|
1189
|
+
_localHeader.extraLen = val;
|
|
1190
|
+
},
|
|
1191
|
+
|
|
1192
|
+
get commentLength() {
|
|
1193
|
+
return _comLen;
|
|
1194
|
+
},
|
|
1195
|
+
set commentLength(val) {
|
|
1196
|
+
_comLen = val;
|
|
1197
|
+
},
|
|
1198
|
+
|
|
1199
|
+
get diskNumStart() {
|
|
1200
|
+
return _diskStart;
|
|
1201
|
+
},
|
|
1202
|
+
set diskNumStart(val) {
|
|
1203
|
+
_diskStart = uint32(val);
|
|
1204
|
+
},
|
|
1205
|
+
|
|
1206
|
+
get inAttr() {
|
|
1207
|
+
return _inattr;
|
|
1208
|
+
},
|
|
1209
|
+
set inAttr(val) {
|
|
1210
|
+
_inattr = uint32(val);
|
|
1211
|
+
},
|
|
1212
|
+
|
|
1213
|
+
get attr() {
|
|
1214
|
+
return _attr;
|
|
1215
|
+
},
|
|
1216
|
+
set attr(val) {
|
|
1217
|
+
_attr = uint32(val);
|
|
1218
|
+
},
|
|
1219
|
+
|
|
1220
|
+
// get Unix file permissions
|
|
1221
|
+
get fileAttr() {
|
|
1222
|
+
// Mask to the 9 rwxrwxrwx bits only. The setuid (0o4000), setgid
|
|
1223
|
+
// (0o2000) and sticky (0o1000) bits are attacker-controlled archive
|
|
1224
|
+
// metadata; preserving them on extraction (keepOriginalPermission)
|
|
1225
|
+
// lets a crafted zip plant a setuid-root binary when extracting as
|
|
1226
|
+
// root, a local privilege escalation (GHSA-679w-jf3m-wh39).
|
|
1227
|
+
return ((_attr || 0) >> 16) & 0o777;
|
|
1228
|
+
},
|
|
1229
|
+
|
|
1230
|
+
get offset() {
|
|
1231
|
+
return _offset;
|
|
1232
|
+
},
|
|
1233
|
+
set offset(val) {
|
|
1234
|
+
_offset = uint32(val);
|
|
1235
|
+
},
|
|
1236
|
+
|
|
1237
|
+
get encrypted() {
|
|
1238
|
+
return (_flags & Constants.FLG_ENC) === Constants.FLG_ENC;
|
|
1239
|
+
},
|
|
1240
|
+
|
|
1241
|
+
get centralHeaderSize() {
|
|
1242
|
+
return Constants.CENHDR + _fnameLen + _extraLen + _comLen;
|
|
1243
|
+
},
|
|
1244
|
+
|
|
1245
|
+
get realDataOffset() {
|
|
1246
|
+
return _offset + Constants.LOCHDR + _localHeader.fnameLen + _localHeader.extraLen;
|
|
1247
|
+
},
|
|
1248
|
+
|
|
1249
|
+
get localHeader() {
|
|
1250
|
+
return _localHeader;
|
|
1251
|
+
},
|
|
1252
|
+
|
|
1253
|
+
loadLocalHeaderFromBinary: function (/*Buffer*/ input) {
|
|
1254
|
+
// The LOC offset comes from the central directory and is attacker
|
|
1255
|
+
// controlled. Reject one that would read past the end of the buffer,
|
|
1256
|
+
// otherwise readUInt32LE below throws a raw RangeError instead of a
|
|
1257
|
+
// clean INVALID_LOC (and escapes the async error path).
|
|
1258
|
+
if (_offset < 0 || _offset + Constants.LOCHDR > input.length) {
|
|
1259
|
+
throw Utils.Errors.INVALID_LOC();
|
|
1260
|
+
}
|
|
1261
|
+
var data = input.slice(_offset, _offset + Constants.LOCHDR);
|
|
1262
|
+
// 30 bytes and should start with "PK\003\004"
|
|
1263
|
+
if (data.readUInt32LE(0) !== Constants.LOCSIG) {
|
|
1264
|
+
throw Utils.Errors.INVALID_LOC();
|
|
1265
|
+
}
|
|
1266
|
+
|
|
1267
|
+
// version needed to extract
|
|
1268
|
+
_localHeader.version = data.readUInt16LE(Constants.LOCVER);
|
|
1269
|
+
// general purpose bit flag
|
|
1270
|
+
_localHeader.flags = data.readUInt16LE(Constants.LOCFLG);
|
|
1271
|
+
// desc flag
|
|
1272
|
+
_localHeader.flags_desc = (_localHeader.flags & Constants.FLG_DESC) > 0;
|
|
1273
|
+
// compression method
|
|
1274
|
+
_localHeader.method = data.readUInt16LE(Constants.LOCHOW);
|
|
1275
|
+
// modification time (2 bytes time, 2 bytes date)
|
|
1276
|
+
_localHeader.time = data.readUInt32LE(Constants.LOCTIM);
|
|
1277
|
+
// uncompressed file crc-32 valu
|
|
1278
|
+
_localHeader.crc = data.readUInt32LE(Constants.LOCCRC);
|
|
1279
|
+
// compressed size
|
|
1280
|
+
_localHeader.compressedSize = data.readUInt32LE(Constants.LOCSIZ);
|
|
1281
|
+
// uncompressed size
|
|
1282
|
+
_localHeader.size = data.readUInt32LE(Constants.LOCLEN);
|
|
1283
|
+
// filename length
|
|
1284
|
+
_localHeader.fnameLen = data.readUInt16LE(Constants.LOCNAM);
|
|
1285
|
+
// extra field length
|
|
1286
|
+
_localHeader.extraLen = data.readUInt16LE(Constants.LOCEXT);
|
|
1287
|
+
|
|
1288
|
+
// read extra data
|
|
1289
|
+
const extraStart = _offset + Constants.LOCHDR + _localHeader.fnameLen;
|
|
1290
|
+
const extraEnd = extraStart + _localHeader.extraLen;
|
|
1291
|
+
return input.slice(extraStart, extraEnd);
|
|
1292
|
+
},
|
|
1293
|
+
|
|
1294
|
+
loadFromBinary: function (/*Buffer*/ data) {
|
|
1295
|
+
// data should be 46 bytes and start with "PK 01 02"
|
|
1296
|
+
if (data.length !== Constants.CENHDR || data.readUInt32LE(0) !== Constants.CENSIG) {
|
|
1297
|
+
throw Utils.Errors.INVALID_CEN();
|
|
1298
|
+
}
|
|
1299
|
+
// version made by
|
|
1300
|
+
_verMade = data.readUInt16LE(Constants.CENVEM);
|
|
1301
|
+
// version needed to extract
|
|
1302
|
+
_version = data.readUInt16LE(Constants.CENVER);
|
|
1303
|
+
// encrypt, decrypt flags
|
|
1304
|
+
_flags = data.readUInt16LE(Constants.CENFLG);
|
|
1305
|
+
// compression method
|
|
1306
|
+
_method = data.readUInt16LE(Constants.CENHOW);
|
|
1307
|
+
// modification time (2 bytes time, 2 bytes date)
|
|
1308
|
+
_time = data.readUInt32LE(Constants.CENTIM);
|
|
1309
|
+
// uncompressed file crc-32 value
|
|
1310
|
+
_crc = data.readUInt32LE(Constants.CENCRC);
|
|
1311
|
+
// compressed size
|
|
1312
|
+
_compressedSize = data.readUInt32LE(Constants.CENSIZ);
|
|
1313
|
+
// uncompressed size
|
|
1314
|
+
_size = data.readUInt32LE(Constants.CENLEN);
|
|
1315
|
+
// filename length
|
|
1316
|
+
_fnameLen = data.readUInt16LE(Constants.CENNAM);
|
|
1317
|
+
// extra field length
|
|
1318
|
+
_extraLen = data.readUInt16LE(Constants.CENEXT);
|
|
1319
|
+
// file comment length
|
|
1320
|
+
_comLen = data.readUInt16LE(Constants.CENCOM);
|
|
1321
|
+
// volume number start
|
|
1322
|
+
_diskStart = data.readUInt16LE(Constants.CENDSK);
|
|
1323
|
+
// internal file attributes
|
|
1324
|
+
_inattr = data.readUInt16LE(Constants.CENATT);
|
|
1325
|
+
// external file attributes
|
|
1326
|
+
_attr = data.readUInt32LE(Constants.CENATX);
|
|
1327
|
+
// LOC header offset
|
|
1328
|
+
_offset = data.readUInt32LE(Constants.CENOFF);
|
|
1329
|
+
},
|
|
1330
|
+
|
|
1331
|
+
localHeaderToBinary: function () {
|
|
1332
|
+
// LOC header size (30 bytes)
|
|
1333
|
+
var data = Buffer.alloc(Constants.LOCHDR);
|
|
1334
|
+
// "PK\003\004"
|
|
1335
|
+
data.writeUInt32LE(Constants.LOCSIG, 0);
|
|
1336
|
+
// version needed to extract
|
|
1337
|
+
data.writeUInt16LE(_version, Constants.LOCVER);
|
|
1338
|
+
// general purpose bit flag
|
|
1339
|
+
// clear bit 3 (data descriptor): we always write the real crc-32
|
|
1340
|
+
// and sizes into this local header, so no trailing descriptor is
|
|
1341
|
+
// emitted. Leaving the flag set would make the output unreadable
|
|
1342
|
+
// (see issue #555).
|
|
1343
|
+
data.writeUInt16LE(_flags & ~Constants.FLG_DESC, Constants.LOCFLG);
|
|
1344
|
+
// compression method
|
|
1345
|
+
data.writeUInt16LE(_method, Constants.LOCHOW);
|
|
1346
|
+
// modification time (2 bytes time, 2 bytes date)
|
|
1347
|
+
data.writeUInt32LE(_time, Constants.LOCTIM);
|
|
1348
|
+
// uncompressed file crc-32 value
|
|
1349
|
+
data.writeUInt32LE(_crc, Constants.LOCCRC);
|
|
1350
|
+
// compressed size
|
|
1351
|
+
data.writeUInt32LE(_compressedSize, Constants.LOCSIZ);
|
|
1352
|
+
// uncompressed size
|
|
1353
|
+
data.writeUInt32LE(_size, Constants.LOCLEN);
|
|
1354
|
+
// filename length
|
|
1355
|
+
data.writeUInt16LE(_fnameLen, Constants.LOCNAM);
|
|
1356
|
+
// extra field length
|
|
1357
|
+
data.writeUInt16LE(_localHeader.extraLen, Constants.LOCEXT);
|
|
1358
|
+
return data;
|
|
1359
|
+
},
|
|
1360
|
+
|
|
1361
|
+
centralHeaderToBinary: function () {
|
|
1362
|
+
// CEN header size (46 bytes)
|
|
1363
|
+
var data = Buffer.alloc(Constants.CENHDR + _fnameLen + _extraLen + _comLen);
|
|
1364
|
+
// "PK\001\002"
|
|
1365
|
+
data.writeUInt32LE(Constants.CENSIG, 0);
|
|
1366
|
+
// version made by
|
|
1367
|
+
data.writeUInt16LE(_verMade, Constants.CENVEM);
|
|
1368
|
+
// version needed to extract
|
|
1369
|
+
data.writeUInt16LE(_version, Constants.CENVER);
|
|
1370
|
+
// encrypt, decrypt flags
|
|
1371
|
+
// clear bit 3 (data descriptor) to match the local header we emit
|
|
1372
|
+
// (real crc/sizes are written, no descriptor follows the data) — issue #555
|
|
1373
|
+
data.writeUInt16LE(_flags & ~Constants.FLG_DESC, Constants.CENFLG);
|
|
1374
|
+
// compression method
|
|
1375
|
+
data.writeUInt16LE(_method, Constants.CENHOW);
|
|
1376
|
+
// modification time (2 bytes time, 2 bytes date)
|
|
1377
|
+
data.writeUInt32LE(_time, Constants.CENTIM);
|
|
1378
|
+
// uncompressed file crc-32 value
|
|
1379
|
+
data.writeUInt32LE(_crc, Constants.CENCRC);
|
|
1380
|
+
// compressed size
|
|
1381
|
+
data.writeUInt32LE(_compressedSize, Constants.CENSIZ);
|
|
1382
|
+
// uncompressed size
|
|
1383
|
+
data.writeUInt32LE(_size, Constants.CENLEN);
|
|
1384
|
+
// filename length
|
|
1385
|
+
data.writeUInt16LE(_fnameLen, Constants.CENNAM);
|
|
1386
|
+
// extra field length
|
|
1387
|
+
data.writeUInt16LE(_extraLen, Constants.CENEXT);
|
|
1388
|
+
// file comment length
|
|
1389
|
+
data.writeUInt16LE(_comLen, Constants.CENCOM);
|
|
1390
|
+
// volume number start
|
|
1391
|
+
data.writeUInt16LE(_diskStart, Constants.CENDSK);
|
|
1392
|
+
// internal file attributes
|
|
1393
|
+
data.writeUInt16LE(_inattr, Constants.CENATT);
|
|
1394
|
+
// external file attributes
|
|
1395
|
+
data.writeUInt32LE(_attr, Constants.CENATX);
|
|
1396
|
+
// LOC header offset
|
|
1397
|
+
data.writeUInt32LE(_offset, Constants.CENOFF);
|
|
1398
|
+
return data;
|
|
1399
|
+
},
|
|
1400
|
+
|
|
1401
|
+
toJSON: function () {
|
|
1402
|
+
const bytes = function (nr) {
|
|
1403
|
+
return nr + " bytes";
|
|
1404
|
+
};
|
|
1405
|
+
|
|
1406
|
+
return {
|
|
1407
|
+
made: _verMade,
|
|
1408
|
+
version: _version,
|
|
1409
|
+
flags: _flags,
|
|
1410
|
+
method: Utils.methodToString(_method),
|
|
1411
|
+
time: this.time,
|
|
1412
|
+
crc: "0x" + _crc.toString(16).toUpperCase(),
|
|
1413
|
+
compressedSize: bytes(_compressedSize),
|
|
1414
|
+
size: bytes(_size),
|
|
1415
|
+
fileNameLength: bytes(_fnameLen),
|
|
1416
|
+
extraLength: bytes(_extraLen),
|
|
1417
|
+
commentLength: bytes(_comLen),
|
|
1418
|
+
diskNumStart: _diskStart,
|
|
1419
|
+
inAttr: _inattr,
|
|
1420
|
+
attr: _attr,
|
|
1421
|
+
offset: _offset,
|
|
1422
|
+
centralHeaderSize: bytes(Constants.CENHDR + _fnameLen + _extraLen + _comLen)
|
|
1423
|
+
};
|
|
1424
|
+
},
|
|
1425
|
+
|
|
1426
|
+
toString: function () {
|
|
1427
|
+
return JSON.stringify(this.toJSON(), null, "\t");
|
|
1428
|
+
}
|
|
1429
|
+
};
|
|
1430
|
+
};
|
|
1431
|
+
|
|
1432
|
+
|
|
1433
|
+
/***/ },
|
|
1434
|
+
|
|
1435
|
+
/***/ 21204
|
|
1436
|
+
(__unused_webpack_module, exports, __webpack_require__) {
|
|
1437
|
+
|
|
1438
|
+
exports.EntryHeader = __webpack_require__(20537);
|
|
1439
|
+
exports.MainHeader = __webpack_require__(39522);
|
|
1440
|
+
|
|
1441
|
+
|
|
1442
|
+
/***/ },
|
|
1443
|
+
|
|
1444
|
+
/***/ 39522
|
|
1445
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
1446
|
+
|
|
1447
|
+
var Utils = __webpack_require__(6942),
|
|
1448
|
+
Constants = Utils.Constants;
|
|
1449
|
+
|
|
1450
|
+
/* The entries in the end of central directory */
|
|
1451
|
+
module.exports = function () {
|
|
1452
|
+
var _volumeEntries = 0,
|
|
1453
|
+
_totalEntries = 0,
|
|
1454
|
+
_size = 0,
|
|
1455
|
+
_offset = 0,
|
|
1456
|
+
_commentLength = 0;
|
|
1457
|
+
|
|
1458
|
+
const needsZip64 = () => _volumeEntries > Constants.EF_ZIP64_OR_16 || _totalEntries > Constants.EF_ZIP64_OR_16 || _size > Constants.EF_ZIP64_OR_32 || _offset > Constants.EF_ZIP64_OR_32;
|
|
1459
|
+
|
|
1460
|
+
return {
|
|
1461
|
+
get diskEntries() {
|
|
1462
|
+
return _volumeEntries;
|
|
1463
|
+
},
|
|
1464
|
+
set diskEntries(/*Number*/ val) {
|
|
1465
|
+
_volumeEntries = _totalEntries = val;
|
|
1466
|
+
},
|
|
1467
|
+
|
|
1468
|
+
get totalEntries() {
|
|
1469
|
+
return _totalEntries;
|
|
1470
|
+
},
|
|
1471
|
+
set totalEntries(/*Number*/ val) {
|
|
1472
|
+
_totalEntries = _volumeEntries = val;
|
|
1473
|
+
},
|
|
1474
|
+
|
|
1475
|
+
get size() {
|
|
1476
|
+
return _size;
|
|
1477
|
+
},
|
|
1478
|
+
set size(/*Number*/ val) {
|
|
1479
|
+
_size = val;
|
|
1480
|
+
},
|
|
1481
|
+
|
|
1482
|
+
get offset() {
|
|
1483
|
+
return _offset;
|
|
1484
|
+
},
|
|
1485
|
+
set offset(/*Number*/ val) {
|
|
1486
|
+
_offset = val;
|
|
1487
|
+
},
|
|
1488
|
+
|
|
1489
|
+
get commentLength() {
|
|
1490
|
+
return _commentLength;
|
|
1491
|
+
},
|
|
1492
|
+
set commentLength(/*Number*/ val) {
|
|
1493
|
+
_commentLength = val;
|
|
1494
|
+
},
|
|
1495
|
+
|
|
1496
|
+
get mainHeaderSize() {
|
|
1497
|
+
return (needsZip64() ? Constants.ZIP64HDR + Constants.END64HDR : 0) + Constants.ENDHDR + _commentLength;
|
|
1498
|
+
},
|
|
1499
|
+
|
|
1500
|
+
loadFromBinary: function (/*Buffer*/ data) {
|
|
1501
|
+
// data should be 22 bytes and start with "PK 05 06"
|
|
1502
|
+
// or be 56+ bytes and start with "PK 06 06" for Zip64
|
|
1503
|
+
if (
|
|
1504
|
+
(data.length !== Constants.ENDHDR || data.readUInt32LE(0) !== Constants.ENDSIG) &&
|
|
1505
|
+
(data.length < Constants.ZIP64HDR || data.readUInt32LE(0) !== Constants.ZIP64SIG)
|
|
1506
|
+
) {
|
|
1507
|
+
throw Utils.Errors.INVALID_END();
|
|
1508
|
+
}
|
|
1509
|
+
|
|
1510
|
+
if (data.readUInt32LE(0) === Constants.ENDSIG) {
|
|
1511
|
+
// number of entries on this volume
|
|
1512
|
+
_volumeEntries = data.readUInt16LE(Constants.ENDSUB);
|
|
1513
|
+
// total number of entries
|
|
1514
|
+
_totalEntries = data.readUInt16LE(Constants.ENDTOT);
|
|
1515
|
+
// central directory size in bytes
|
|
1516
|
+
_size = data.readUInt32LE(Constants.ENDSIZ);
|
|
1517
|
+
// offset of first CEN header
|
|
1518
|
+
_offset = data.readUInt32LE(Constants.ENDOFF);
|
|
1519
|
+
// zip file comment length
|
|
1520
|
+
_commentLength = data.readUInt16LE(Constants.ENDCOM);
|
|
1521
|
+
} else {
|
|
1522
|
+
// number of entries on this volume
|
|
1523
|
+
_volumeEntries = Utils.readBigUInt64LE(data, Constants.ZIP64SUB);
|
|
1524
|
+
// total number of entries
|
|
1525
|
+
_totalEntries = Utils.readBigUInt64LE(data, Constants.ZIP64TOT);
|
|
1526
|
+
// central directory size in bytes
|
|
1527
|
+
_size = Utils.readBigUInt64LE(data, Constants.ZIP64SIZB);
|
|
1528
|
+
// offset of first CEN header
|
|
1529
|
+
_offset = Utils.readBigUInt64LE(data, Constants.ZIP64OFF);
|
|
1530
|
+
|
|
1531
|
+
_commentLength = 0;
|
|
1532
|
+
}
|
|
1533
|
+
},
|
|
1534
|
+
|
|
1535
|
+
toBinary: function () {
|
|
1536
|
+
if (!needsZip64()) {
|
|
1537
|
+
var b = Buffer.alloc(Constants.ENDHDR + _commentLength);
|
|
1538
|
+
// "PK 05 06" signature
|
|
1539
|
+
b.writeUInt32LE(Constants.ENDSIG, 0);
|
|
1540
|
+
b.writeUInt32LE(0, 4);
|
|
1541
|
+
// number of entries on this volume
|
|
1542
|
+
b.writeUInt16LE(_volumeEntries, Constants.ENDSUB);
|
|
1543
|
+
// total number of entries
|
|
1544
|
+
b.writeUInt16LE(_totalEntries, Constants.ENDTOT);
|
|
1545
|
+
// central directory size in bytes
|
|
1546
|
+
b.writeUInt32LE(_size, Constants.ENDSIZ);
|
|
1547
|
+
// offset of first CEN header
|
|
1548
|
+
b.writeUInt32LE(_offset, Constants.ENDOFF);
|
|
1549
|
+
// zip file comment length
|
|
1550
|
+
b.writeUInt16LE(_commentLength, Constants.ENDCOM);
|
|
1551
|
+
// fill comment memory with spaces so no garbage is left there
|
|
1552
|
+
b.fill(" ", Constants.ENDHDR);
|
|
1553
|
+
|
|
1554
|
+
return b;
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
var b = Buffer.alloc(this.mainHeaderSize);
|
|
1558
|
+
let offset = 0;
|
|
1559
|
+
|
|
1560
|
+
// Zip64 end of central directory record.
|
|
1561
|
+
b.writeUInt32LE(Constants.ZIP64SIG, offset);
|
|
1562
|
+
Utils.writeBigUInt64LE(b, Constants.ZIP64HDR - Constants.ZIP64LEAD, offset + Constants.ZIP64SIZE);
|
|
1563
|
+
b.writeUInt16LE(45, offset + Constants.ZIP64VEM);
|
|
1564
|
+
b.writeUInt16LE(45, offset + Constants.ZIP64VER);
|
|
1565
|
+
b.writeUInt32LE(0, offset + Constants.ZIP64DSK);
|
|
1566
|
+
b.writeUInt32LE(0, offset + Constants.ZIP64DSKDIR);
|
|
1567
|
+
Utils.writeBigUInt64LE(b, _volumeEntries, offset + Constants.ZIP64SUB);
|
|
1568
|
+
Utils.writeBigUInt64LE(b, _totalEntries, offset + Constants.ZIP64TOT);
|
|
1569
|
+
Utils.writeBigUInt64LE(b, _size, offset + Constants.ZIP64SIZB);
|
|
1570
|
+
Utils.writeBigUInt64LE(b, _offset, offset + Constants.ZIP64OFF);
|
|
1571
|
+
|
|
1572
|
+
const zip64EndOffset = _offset + _size;
|
|
1573
|
+
offset += Constants.ZIP64HDR;
|
|
1574
|
+
|
|
1575
|
+
// Zip64 end of central directory locator.
|
|
1576
|
+
b.writeUInt32LE(Constants.END64SIG, offset);
|
|
1577
|
+
b.writeUInt32LE(0, offset + Constants.END64START);
|
|
1578
|
+
Utils.writeBigUInt64LE(b, zip64EndOffset, offset + Constants.END64OFF);
|
|
1579
|
+
b.writeUInt32LE(1, offset + Constants.END64NUMDISKS);
|
|
1580
|
+
offset += Constants.END64HDR;
|
|
1581
|
+
|
|
1582
|
+
// "PK 05 06" signature
|
|
1583
|
+
b.writeUInt32LE(Constants.ENDSIG, offset);
|
|
1584
|
+
b.writeUInt32LE(0, offset + 4);
|
|
1585
|
+
// number of entries on this volume
|
|
1586
|
+
b.writeUInt16LE(Math.min(_volumeEntries, Constants.EF_ZIP64_OR_16), offset + Constants.ENDSUB);
|
|
1587
|
+
// total number of entries
|
|
1588
|
+
b.writeUInt16LE(Math.min(_totalEntries, Constants.EF_ZIP64_OR_16), offset + Constants.ENDTOT);
|
|
1589
|
+
// central directory size in bytes
|
|
1590
|
+
b.writeUInt32LE(Math.min(_size, Constants.EF_ZIP64_OR_32), offset + Constants.ENDSIZ);
|
|
1591
|
+
// offset of first CEN header
|
|
1592
|
+
b.writeUInt32LE(Math.min(_offset, Constants.EF_ZIP64_OR_32), offset + Constants.ENDOFF);
|
|
1593
|
+
// zip file comment length
|
|
1594
|
+
b.writeUInt16LE(_commentLength, offset + Constants.ENDCOM);
|
|
1595
|
+
// fill comment memory with spaces so no garbage is left there
|
|
1596
|
+
b.fill(" ", offset + Constants.ENDHDR);
|
|
1597
|
+
|
|
1598
|
+
return b;
|
|
1599
|
+
},
|
|
1600
|
+
|
|
1601
|
+
toJSON: function () {
|
|
1602
|
+
// creates 0x0000 style output
|
|
1603
|
+
const offset = function (nr, len) {
|
|
1604
|
+
let offs = nr.toString(16).toUpperCase();
|
|
1605
|
+
while (offs.length < len) offs = "0" + offs;
|
|
1606
|
+
return "0x" + offs;
|
|
1607
|
+
};
|
|
1608
|
+
|
|
1609
|
+
return {
|
|
1610
|
+
diskEntries: _volumeEntries,
|
|
1611
|
+
totalEntries: _totalEntries,
|
|
1612
|
+
size: _size + " bytes",
|
|
1613
|
+
offset: offset(_offset, 4),
|
|
1614
|
+
commentLength: _commentLength
|
|
1615
|
+
};
|
|
1616
|
+
},
|
|
1617
|
+
|
|
1618
|
+
toString: function () {
|
|
1619
|
+
return JSON.stringify(this.toJSON(), null, "\t");
|
|
1620
|
+
}
|
|
1621
|
+
};
|
|
1622
|
+
};
|
|
1623
|
+
// Misspelled
|
|
1624
|
+
|
|
1625
|
+
|
|
1626
|
+
/***/ },
|
|
1627
|
+
|
|
1628
|
+
/***/ 52393
|
|
1629
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
1630
|
+
|
|
1631
|
+
module.exports = function (/*Buffer*/ inbuf) {
|
|
1632
|
+
var zlib = __webpack_require__(43106);
|
|
1633
|
+
|
|
1634
|
+
var opts = { chunkSize: (parseInt(inbuf.length / 1024) + 1) * 1024 };
|
|
1635
|
+
|
|
1636
|
+
return {
|
|
1637
|
+
deflate: function () {
|
|
1638
|
+
return zlib.deflateRawSync(inbuf, opts);
|
|
1639
|
+
},
|
|
1640
|
+
|
|
1641
|
+
deflateAsync: function (/*Function*/ callback) {
|
|
1642
|
+
var tmp = zlib.createDeflateRaw(opts),
|
|
1643
|
+
parts = [],
|
|
1644
|
+
total = 0;
|
|
1645
|
+
tmp.on("data", function (data) {
|
|
1646
|
+
parts.push(data);
|
|
1647
|
+
total += data.length;
|
|
1648
|
+
});
|
|
1649
|
+
tmp.on("end", function () {
|
|
1650
|
+
var buf = Buffer.alloc(total),
|
|
1651
|
+
written = 0;
|
|
1652
|
+
buf.fill(0);
|
|
1653
|
+
for (var i = 0; i < parts.length; i++) {
|
|
1654
|
+
var part = parts[i];
|
|
1655
|
+
part.copy(buf, written);
|
|
1656
|
+
written += part.length;
|
|
1657
|
+
}
|
|
1658
|
+
callback && callback(buf);
|
|
1659
|
+
});
|
|
1660
|
+
tmp.end(inbuf);
|
|
1661
|
+
}
|
|
1662
|
+
};
|
|
1663
|
+
};
|
|
1664
|
+
|
|
1665
|
+
|
|
1666
|
+
/***/ },
|
|
1667
|
+
|
|
1668
|
+
/***/ 16552
|
|
1669
|
+
(__unused_webpack_module, exports, __webpack_require__) {
|
|
1670
|
+
|
|
1671
|
+
exports.Deflater = __webpack_require__(52393);
|
|
1672
|
+
exports.Inflater = __webpack_require__(91845);
|
|
1673
|
+
exports.ZipCrypto = __webpack_require__(37574);
|
|
1674
|
+
|
|
1675
|
+
|
|
1676
|
+
/***/ },
|
|
1677
|
+
|
|
1678
|
+
/***/ 91845
|
|
1679
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
1680
|
+
|
|
1681
|
+
const version = +(process?.versions?.node ?? "").split(".")[0] || 0;
|
|
1682
|
+
const Errors = __webpack_require__(22435);
|
|
1683
|
+
|
|
1684
|
+
module.exports = function (/*Buffer*/ inbuf, /*number*/ expectedLength) {
|
|
1685
|
+
var zlib = __webpack_require__(43106);
|
|
1686
|
+
// Cap decompression output at the entry's declared uncompressed size to bound
|
|
1687
|
+
// decompression bombs (CVE-2026-39244). A declared size of 0 must not disable
|
|
1688
|
+
// the cap: a genuinely empty entry inflates to 0 bytes, so a 1-byte floor
|
|
1689
|
+
// still lets it through while stopping a bomb that lies about its size
|
|
1690
|
+
// (GHSA-rcw4-f5rp-g42v). zlib requires maxOutputLength >= 1.
|
|
1691
|
+
const maxOutputLength = expectedLength > 0 ? expectedLength : 1;
|
|
1692
|
+
const option = version >= 15 ? { maxOutputLength } : {};
|
|
1693
|
+
|
|
1694
|
+
return {
|
|
1695
|
+
inflate: function () {
|
|
1696
|
+
return zlib.inflateRawSync(inbuf, option);
|
|
1697
|
+
},
|
|
1698
|
+
|
|
1699
|
+
inflateAsync: function (/*Function*/ callback) {
|
|
1700
|
+
var tmp = zlib.createInflateRaw(option),
|
|
1701
|
+
parts = [],
|
|
1702
|
+
total = 0,
|
|
1703
|
+
done = false;
|
|
1704
|
+
const fail = function (err) {
|
|
1705
|
+
if (done) return;
|
|
1706
|
+
done = true;
|
|
1707
|
+
tmp.destroy();
|
|
1708
|
+
callback && callback(Buffer.alloc(0), err);
|
|
1709
|
+
};
|
|
1710
|
+
// Route stream errors (e.g. Z_DATA_ERROR on malformed input) through the
|
|
1711
|
+
// callback. Without an "error" listener zlib re-throws the event as an
|
|
1712
|
+
// uncaught exception on a later tick, crashing the host process instead
|
|
1713
|
+
// of failing the call (GHSA-8238-w5pm-2374).
|
|
1714
|
+
tmp.on("error", function (err) {
|
|
1715
|
+
fail(err);
|
|
1716
|
+
});
|
|
1717
|
+
tmp.on("data", function (data) {
|
|
1718
|
+
if (done) return;
|
|
1719
|
+
total += data.length;
|
|
1720
|
+
// The streaming API ignores maxOutputLength, so enforce the cap by
|
|
1721
|
+
// hand; otherwise the async path decompresses without limit while the
|
|
1722
|
+
// sync path is capped (GHSA-v429-h5qx-84wm, GHSA-c6fg-446q-cg94).
|
|
1723
|
+
if (total > maxOutputLength) {
|
|
1724
|
+
return fail(Errors.MAX_OUTPUT_EXCEEDED());
|
|
1725
|
+
}
|
|
1726
|
+
parts.push(data);
|
|
1727
|
+
});
|
|
1728
|
+
tmp.on("end", function () {
|
|
1729
|
+
if (done) return;
|
|
1730
|
+
done = true;
|
|
1731
|
+
var buf = Buffer.alloc(total),
|
|
1732
|
+
written = 0;
|
|
1733
|
+
buf.fill(0);
|
|
1734
|
+
for (var i = 0; i < parts.length; i++) {
|
|
1735
|
+
var part = parts[i];
|
|
1736
|
+
part.copy(buf, written);
|
|
1737
|
+
written += part.length;
|
|
1738
|
+
}
|
|
1739
|
+
callback && callback(buf);
|
|
1740
|
+
});
|
|
1741
|
+
tmp.end(inbuf);
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
1744
|
+
};
|
|
1745
|
+
|
|
1746
|
+
|
|
1747
|
+
/***/ },
|
|
1748
|
+
|
|
1749
|
+
/***/ 37574
|
|
1750
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
1751
|
+
|
|
1752
|
+
|
|
1753
|
+
|
|
1754
|
+
// node crypt, we use it for generate salt
|
|
1755
|
+
// eslint-disable-next-line node/no-unsupported-features/node-builtins
|
|
1756
|
+
const { randomFillSync } = __webpack_require__(76982);
|
|
1757
|
+
const Errors = __webpack_require__(22435);
|
|
1758
|
+
|
|
1759
|
+
// generate CRC32 lookup table
|
|
1760
|
+
const crctable = new Uint32Array(256).map((t, crc) => {
|
|
1761
|
+
for (let j = 0; j < 8; j++) {
|
|
1762
|
+
if (0 !== (crc & 1)) {
|
|
1763
|
+
crc = (crc >>> 1) ^ 0xedb88320;
|
|
1764
|
+
} else {
|
|
1765
|
+
crc >>>= 1;
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
return crc >>> 0;
|
|
1769
|
+
});
|
|
1770
|
+
|
|
1771
|
+
// C-style uInt32 Multiply (discards higher bits, when JS multiply discards lower bits)
|
|
1772
|
+
const uMul = (a, b) => Math.imul(a, b) >>> 0;
|
|
1773
|
+
|
|
1774
|
+
// crc32 byte single update (actually same function is part of utils.crc32 function :) )
|
|
1775
|
+
const crc32update = (pCrc32, bval) => {
|
|
1776
|
+
return crctable[(pCrc32 ^ bval) & 0xff] ^ (pCrc32 >>> 8);
|
|
1777
|
+
};
|
|
1778
|
+
|
|
1779
|
+
// function for generating salt for encrytion header
|
|
1780
|
+
const genSalt = () => {
|
|
1781
|
+
if ("function" === typeof randomFillSync) {
|
|
1782
|
+
return randomFillSync(Buffer.alloc(12));
|
|
1783
|
+
} else {
|
|
1784
|
+
// fallback if function is not defined
|
|
1785
|
+
return genSalt.node();
|
|
1786
|
+
}
|
|
1787
|
+
};
|
|
1788
|
+
|
|
1789
|
+
// salt generation with node random function (mainly as fallback)
|
|
1790
|
+
genSalt.node = () => {
|
|
1791
|
+
const salt = Buffer.alloc(12);
|
|
1792
|
+
const len = salt.length;
|
|
1793
|
+
for (let i = 0; i < len; i++) salt[i] = (Math.random() * 256) & 0xff;
|
|
1794
|
+
return salt;
|
|
1795
|
+
};
|
|
1796
|
+
|
|
1797
|
+
// general config
|
|
1798
|
+
const config = {
|
|
1799
|
+
genSalt
|
|
1800
|
+
};
|
|
1801
|
+
|
|
1802
|
+
// Class Initkeys handles same basic ops with keys
|
|
1803
|
+
function Initkeys(pw) {
|
|
1804
|
+
const pass = Buffer.isBuffer(pw) ? pw : Buffer.from(pw);
|
|
1805
|
+
this.keys = new Uint32Array([0x12345678, 0x23456789, 0x34567890]);
|
|
1806
|
+
for (let i = 0; i < pass.length; i++) {
|
|
1807
|
+
this.updateKeys(pass[i]);
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
|
|
1811
|
+
Initkeys.prototype.updateKeys = function (byteValue) {
|
|
1812
|
+
const keys = this.keys;
|
|
1813
|
+
keys[0] = crc32update(keys[0], byteValue);
|
|
1814
|
+
keys[1] += keys[0] & 0xff;
|
|
1815
|
+
keys[1] = uMul(keys[1], 134775813) + 1;
|
|
1816
|
+
keys[2] = crc32update(keys[2], keys[1] >>> 24);
|
|
1817
|
+
return byteValue;
|
|
1818
|
+
};
|
|
1819
|
+
|
|
1820
|
+
Initkeys.prototype.next = function () {
|
|
1821
|
+
const k = (this.keys[2] | 2) >>> 0; // key
|
|
1822
|
+
return (uMul(k, k ^ 1) >> 8) & 0xff; // decode
|
|
1823
|
+
};
|
|
1824
|
+
|
|
1825
|
+
function make_decrypter(/*Buffer*/ pwd) {
|
|
1826
|
+
// 1. Stage initialize key
|
|
1827
|
+
const keys = new Initkeys(pwd);
|
|
1828
|
+
|
|
1829
|
+
// return decrypter function
|
|
1830
|
+
return function (/*Buffer*/ data) {
|
|
1831
|
+
// result - we create new Buffer for results
|
|
1832
|
+
const result = Buffer.alloc(data.length);
|
|
1833
|
+
let pos = 0;
|
|
1834
|
+
// process input data
|
|
1835
|
+
for (let c of data) {
|
|
1836
|
+
//c ^= keys.next();
|
|
1837
|
+
//result[pos++] = c; // decode & Save Value
|
|
1838
|
+
result[pos++] = keys.updateKeys(c ^ keys.next()); // update keys with decoded byte
|
|
1839
|
+
}
|
|
1840
|
+
return result;
|
|
1841
|
+
};
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
function make_encrypter(/*Buffer*/ pwd) {
|
|
1845
|
+
// 1. Stage initialize key
|
|
1846
|
+
const keys = new Initkeys(pwd);
|
|
1847
|
+
|
|
1848
|
+
// return encrypting function, result and pos is here so we dont have to merge buffers later
|
|
1849
|
+
return function (/*Buffer*/ data, /*Buffer*/ result, /* Number */ pos = 0) {
|
|
1850
|
+
// result - we create new Buffer for results
|
|
1851
|
+
if (!result) result = Buffer.alloc(data.length);
|
|
1852
|
+
// process input data
|
|
1853
|
+
for (let c of data) {
|
|
1854
|
+
const k = keys.next(); // save key byte
|
|
1855
|
+
result[pos++] = c ^ k; // save val
|
|
1856
|
+
keys.updateKeys(c); // update keys with decoded byte
|
|
1857
|
+
}
|
|
1858
|
+
return result;
|
|
1859
|
+
};
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
function decrypt(/*Buffer*/ data, /*Object*/ header, /*String, Buffer*/ pwd) {
|
|
1863
|
+
if (!data || !Buffer.isBuffer(data) || data.length < 12) {
|
|
1864
|
+
return Buffer.alloc(0);
|
|
1865
|
+
}
|
|
1866
|
+
|
|
1867
|
+
// 1. We Initialize and generate decrypting function
|
|
1868
|
+
const decrypter = make_decrypter(pwd);
|
|
1869
|
+
|
|
1870
|
+
// 2. decrypt salt what is always 12 bytes and is a part of file content
|
|
1871
|
+
const salt = decrypter(data.slice(0, 12));
|
|
1872
|
+
|
|
1873
|
+
// if bit 3 (0x08) of the general-purpose flags field is set, check salt[11] with the high byte of the header time
|
|
1874
|
+
// 2 byte data block (as per Info-Zip spec), otherwise check with the high byte of the header entry
|
|
1875
|
+
const verifyByte = (header.flags & 0x8) === 0x8 ? header.timeHighByte : header.crc >>> 24;
|
|
1876
|
+
|
|
1877
|
+
//3. does password meet expectations
|
|
1878
|
+
if (salt[11] !== verifyByte) {
|
|
1879
|
+
throw Errors.WRONG_PASSWORD();
|
|
1880
|
+
}
|
|
1881
|
+
|
|
1882
|
+
// 4. decode content
|
|
1883
|
+
return decrypter(data.slice(12));
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
// lets add way to populate salt, NOT RECOMMENDED for production but maybe useful for testing general functionality
|
|
1887
|
+
function _salter(data) {
|
|
1888
|
+
if (Buffer.isBuffer(data) && data.length >= 12) {
|
|
1889
|
+
// be aware - currently salting buffer data is modified
|
|
1890
|
+
config.genSalt = function () {
|
|
1891
|
+
return data.slice(0, 12);
|
|
1892
|
+
};
|
|
1893
|
+
} else if (data === "node") {
|
|
1894
|
+
// test salt generation with node random function
|
|
1895
|
+
config.genSalt = genSalt.node;
|
|
1896
|
+
} else {
|
|
1897
|
+
// if value is not acceptable config gets reset.
|
|
1898
|
+
config.genSalt = genSalt;
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
function encrypt(/*Buffer*/ data, /*Object*/ header, /*String, Buffer*/ pwd, /*Boolean*/ oldlike = false) {
|
|
1903
|
+
// 1. test data if data is not Buffer we make buffer from it
|
|
1904
|
+
if (data == null) data = Buffer.alloc(0);
|
|
1905
|
+
// if data is not buffer be make buffer from it
|
|
1906
|
+
if (!Buffer.isBuffer(data)) data = Buffer.from(data.toString());
|
|
1907
|
+
|
|
1908
|
+
// 2. We Initialize and generate encrypting function
|
|
1909
|
+
const encrypter = make_encrypter(pwd);
|
|
1910
|
+
|
|
1911
|
+
// 3. generate salt (12-bytes of random data)
|
|
1912
|
+
const salt = config.genSalt();
|
|
1913
|
+
salt[11] = (header.crc >>> 24) & 0xff;
|
|
1914
|
+
|
|
1915
|
+
// old implementations (before PKZip 2.04g) used two byte check
|
|
1916
|
+
if (oldlike) salt[10] = (header.crc >>> 16) & 0xff;
|
|
1917
|
+
|
|
1918
|
+
// 4. create output
|
|
1919
|
+
const result = Buffer.alloc(data.length + 12);
|
|
1920
|
+
encrypter(salt, result);
|
|
1921
|
+
|
|
1922
|
+
// finally encode content
|
|
1923
|
+
return encrypter(data, result, 12);
|
|
1924
|
+
}
|
|
1925
|
+
|
|
1926
|
+
module.exports = { decrypt, encrypt, _salter };
|
|
1927
|
+
|
|
1928
|
+
|
|
1929
|
+
/***/ },
|
|
1930
|
+
|
|
1931
|
+
/***/ 37935
|
|
1932
|
+
(module) {
|
|
1933
|
+
|
|
1934
|
+
module.exports = {
|
|
1935
|
+
/* The local file header */
|
|
1936
|
+
LOCHDR : 30, // LOC header size
|
|
1937
|
+
LOCSIG : 0x04034b50, // "PK\003\004"
|
|
1938
|
+
LOCVER : 4, // version needed to extract
|
|
1939
|
+
LOCFLG : 6, // general purpose bit flag
|
|
1940
|
+
LOCHOW : 8, // compression method
|
|
1941
|
+
LOCTIM : 10, // modification time (2 bytes time, 2 bytes date)
|
|
1942
|
+
LOCCRC : 14, // uncompressed file crc-32 value
|
|
1943
|
+
LOCSIZ : 18, // compressed size
|
|
1944
|
+
LOCLEN : 22, // uncompressed size
|
|
1945
|
+
LOCNAM : 26, // filename length
|
|
1946
|
+
LOCEXT : 28, // extra field length
|
|
1947
|
+
|
|
1948
|
+
/* The Data descriptor */
|
|
1949
|
+
EXTSIG : 0x08074b50, // "PK\007\008"
|
|
1950
|
+
EXTHDR : 16, // EXT header size
|
|
1951
|
+
EXTCRC : 4, // uncompressed file crc-32 value
|
|
1952
|
+
EXTSIZ : 8, // compressed size
|
|
1953
|
+
EXTLEN : 12, // uncompressed size
|
|
1954
|
+
|
|
1955
|
+
/* The central directory file header */
|
|
1956
|
+
CENHDR : 46, // CEN header size
|
|
1957
|
+
CENSIG : 0x02014b50, // "PK\001\002"
|
|
1958
|
+
CENVEM : 4, // version made by
|
|
1959
|
+
CENVER : 6, // version needed to extract
|
|
1960
|
+
CENFLG : 8, // encrypt, decrypt flags
|
|
1961
|
+
CENHOW : 10, // compression method
|
|
1962
|
+
CENTIM : 12, // modification time (2 bytes time, 2 bytes date)
|
|
1963
|
+
CENCRC : 16, // uncompressed file crc-32 value
|
|
1964
|
+
CENSIZ : 20, // compressed size
|
|
1965
|
+
CENLEN : 24, // uncompressed size
|
|
1966
|
+
CENNAM : 28, // filename length
|
|
1967
|
+
CENEXT : 30, // extra field length
|
|
1968
|
+
CENCOM : 32, // file comment length
|
|
1969
|
+
CENDSK : 34, // volume number start
|
|
1970
|
+
CENATT : 36, // internal file attributes
|
|
1971
|
+
CENATX : 38, // external file attributes (host system dependent)
|
|
1972
|
+
CENOFF : 42, // LOC header offset
|
|
1973
|
+
|
|
1974
|
+
/* The entries in the end of central directory */
|
|
1975
|
+
ENDHDR : 22, // END header size
|
|
1976
|
+
ENDSIG : 0x06054b50, // "PK\005\006"
|
|
1977
|
+
ENDSUB : 8, // number of entries on this disk
|
|
1978
|
+
ENDTOT : 10, // total number of entries
|
|
1979
|
+
ENDSIZ : 12, // central directory size in bytes
|
|
1980
|
+
ENDOFF : 16, // offset of first CEN header
|
|
1981
|
+
ENDCOM : 20, // zip file comment length
|
|
1982
|
+
|
|
1983
|
+
END64HDR : 20, // zip64 END header size
|
|
1984
|
+
END64SIG : 0x07064b50, // zip64 Locator signature, "PK\006\007"
|
|
1985
|
+
END64START : 4, // number of the disk with the start of the zip64
|
|
1986
|
+
END64OFF : 8, // relative offset of the zip64 end of central directory
|
|
1987
|
+
END64NUMDISKS : 16, // total number of disks
|
|
1988
|
+
|
|
1989
|
+
ZIP64SIG : 0x06064b50, // zip64 signature, "PK\006\006"
|
|
1990
|
+
ZIP64HDR : 56, // zip64 record minimum size
|
|
1991
|
+
ZIP64LEAD : 12, // leading bytes at the start of the record, not counted by the value stored in ZIP64SIZE
|
|
1992
|
+
ZIP64SIZE : 4, // zip64 size of the central directory record
|
|
1993
|
+
ZIP64VEM : 12, // zip64 version made by
|
|
1994
|
+
ZIP64VER : 14, // zip64 version needed to extract
|
|
1995
|
+
ZIP64DSK : 16, // zip64 number of this disk
|
|
1996
|
+
ZIP64DSKDIR : 20, // number of the disk with the start of the record directory
|
|
1997
|
+
ZIP64SUB : 24, // number of entries on this disk
|
|
1998
|
+
ZIP64TOT : 32, // total number of entries
|
|
1999
|
+
ZIP64SIZB : 40, // zip64 central directory size in bytes
|
|
2000
|
+
ZIP64OFF : 48, // offset of start of central directory with respect to the starting disk number
|
|
2001
|
+
ZIP64EXTRA : 56, // extensible data sector
|
|
2002
|
+
|
|
2003
|
+
/* Compression methods */
|
|
2004
|
+
STORED : 0, // no compression
|
|
2005
|
+
SHRUNK : 1, // shrunk
|
|
2006
|
+
REDUCED1 : 2, // reduced with compression factor 1
|
|
2007
|
+
REDUCED2 : 3, // reduced with compression factor 2
|
|
2008
|
+
REDUCED3 : 4, // reduced with compression factor 3
|
|
2009
|
+
REDUCED4 : 5, // reduced with compression factor 4
|
|
2010
|
+
IMPLODED : 6, // imploded
|
|
2011
|
+
// 7 reserved for Tokenizing compression algorithm
|
|
2012
|
+
DEFLATED : 8, // deflated
|
|
2013
|
+
ENHANCED_DEFLATED: 9, // enhanced deflated
|
|
2014
|
+
PKWARE : 10,// PKWare DCL imploded
|
|
2015
|
+
// 11 reserved by PKWARE
|
|
2016
|
+
BZIP2 : 12, // compressed using BZIP2
|
|
2017
|
+
// 13 reserved by PKWARE
|
|
2018
|
+
LZMA : 14, // LZMA
|
|
2019
|
+
// 15-17 reserved by PKWARE
|
|
2020
|
+
IBM_TERSE : 18, // compressed using IBM TERSE
|
|
2021
|
+
IBM_LZ77 : 19, // IBM LZ77 z
|
|
2022
|
+
AES_ENCRYPT : 99, // WinZIP AES encryption method
|
|
2023
|
+
|
|
2024
|
+
/* General purpose bit flag */
|
|
2025
|
+
// values can obtained with expression 2**bitnr
|
|
2026
|
+
FLG_ENC : 1, // Bit 0: encrypted file
|
|
2027
|
+
FLG_COMP1 : 2, // Bit 1, compression option
|
|
2028
|
+
FLG_COMP2 : 4, // Bit 2, compression option
|
|
2029
|
+
FLG_DESC : 8, // Bit 3, data descriptor
|
|
2030
|
+
FLG_ENH : 16, // Bit 4, enhanced deflating
|
|
2031
|
+
FLG_PATCH : 32, // Bit 5, indicates that the file is compressed patched data.
|
|
2032
|
+
FLG_STR : 64, // Bit 6, strong encryption (patented)
|
|
2033
|
+
// Bits 7-10: Currently unused.
|
|
2034
|
+
FLG_EFS : 2048, // Bit 11: Language encoding flag (EFS)
|
|
2035
|
+
// Bit 12: Reserved by PKWARE for enhanced compression.
|
|
2036
|
+
// Bit 13: encrypted the Central Directory (patented).
|
|
2037
|
+
// Bits 14-15: Reserved by PKWARE.
|
|
2038
|
+
FLG_MSK : 4096, // mask header values
|
|
2039
|
+
|
|
2040
|
+
/* Load type */
|
|
2041
|
+
FILE : 2,
|
|
2042
|
+
BUFFER : 1,
|
|
2043
|
+
NONE : 0,
|
|
2044
|
+
|
|
2045
|
+
/* 4.5 Extensible data fields */
|
|
2046
|
+
EF_ID : 0,
|
|
2047
|
+
EF_SIZE : 2,
|
|
2048
|
+
|
|
2049
|
+
/* Header IDs */
|
|
2050
|
+
ID_ZIP64 : 0x0001,
|
|
2051
|
+
ID_AVINFO : 0x0007,
|
|
2052
|
+
ID_PFS : 0x0008,
|
|
2053
|
+
ID_OS2 : 0x0009,
|
|
2054
|
+
ID_NTFS : 0x000a,
|
|
2055
|
+
ID_OPENVMS : 0x000c,
|
|
2056
|
+
ID_UNIX : 0x000d,
|
|
2057
|
+
ID_FORK : 0x000e,
|
|
2058
|
+
ID_PATCH : 0x000f,
|
|
2059
|
+
ID_X509_PKCS7 : 0x0014,
|
|
2060
|
+
ID_X509_CERTID_F : 0x0015,
|
|
2061
|
+
ID_X509_CERTID_C : 0x0016,
|
|
2062
|
+
ID_STRONGENC : 0x0017,
|
|
2063
|
+
ID_RECORD_MGT : 0x0018,
|
|
2064
|
+
ID_X509_PKCS7_RL : 0x0019,
|
|
2065
|
+
ID_IBM1 : 0x0065,
|
|
2066
|
+
ID_IBM2 : 0x0066,
|
|
2067
|
+
ID_POSZIP : 0x4690,
|
|
2068
|
+
|
|
2069
|
+
EF_ZIP64_OR_32 : 0xffffffff,
|
|
2070
|
+
EF_ZIP64_OR_16 : 0xffff,
|
|
2071
|
+
EF_ZIP64_SUNCOMP : 0,
|
|
2072
|
+
EF_ZIP64_SCOMP : 8,
|
|
2073
|
+
EF_ZIP64_RHO : 16,
|
|
2074
|
+
EF_ZIP64_DSN : 24
|
|
2075
|
+
};
|
|
2076
|
+
|
|
2077
|
+
|
|
2078
|
+
/***/ },
|
|
2079
|
+
|
|
2080
|
+
/***/ 39346
|
|
2081
|
+
(module) {
|
|
2082
|
+
|
|
2083
|
+
module.exports = {
|
|
2084
|
+
efs: true,
|
|
2085
|
+
encode: (data) => Buffer.from(data, "utf8"),
|
|
2086
|
+
decode: (data) => data.toString("utf8")
|
|
2087
|
+
};
|
|
2088
|
+
|
|
2089
|
+
|
|
2090
|
+
/***/ },
|
|
2091
|
+
|
|
2092
|
+
/***/ 22435
|
|
2093
|
+
(__unused_webpack_module, exports) {
|
|
2094
|
+
|
|
2095
|
+
const errors = {
|
|
2096
|
+
/* Header error messages */
|
|
2097
|
+
INVALID_LOC: "Invalid LOC header (bad signature)",
|
|
2098
|
+
INVALID_CEN: "Invalid CEN header (bad signature)",
|
|
2099
|
+
INVALID_END: "Invalid END header (bad signature)",
|
|
2100
|
+
|
|
2101
|
+
/* Descriptor */
|
|
2102
|
+
DESCRIPTOR_NOT_EXIST: "No descriptor present",
|
|
2103
|
+
DESCRIPTOR_UNKNOWN: "Unknown descriptor format",
|
|
2104
|
+
DESCRIPTOR_FAULTY: "Descriptor data is malformed",
|
|
2105
|
+
|
|
2106
|
+
/* ZipEntry error messages*/
|
|
2107
|
+
NO_DATA: "Nothing to decompress",
|
|
2108
|
+
BAD_CRC: "CRC32 checksum failed {0}",
|
|
2109
|
+
MAX_OUTPUT_EXCEEDED: "Decompressed data exceeds the declared uncompressed size",
|
|
2110
|
+
FILE_IN_THE_WAY: "There is a file in the way: {0}",
|
|
2111
|
+
UNKNOWN_METHOD: "Invalid/unsupported compression method",
|
|
2112
|
+
|
|
2113
|
+
/* Inflater error messages */
|
|
2114
|
+
AVAIL_DATA: "inflate::Available inflate data did not terminate",
|
|
2115
|
+
INVALID_DISTANCE: "inflate::Invalid literal/length or distance code in fixed or dynamic block",
|
|
2116
|
+
TO_MANY_CODES: "inflate::Dynamic block code description: too many length or distance codes",
|
|
2117
|
+
INVALID_REPEAT_LEN: "inflate::Dynamic block code description: repeat more than specified lengths",
|
|
2118
|
+
INVALID_REPEAT_FIRST: "inflate::Dynamic block code description: repeat lengths with no first length",
|
|
2119
|
+
INCOMPLETE_CODES: "inflate::Dynamic block code description: code lengths codes incomplete",
|
|
2120
|
+
INVALID_DYN_DISTANCE: "inflate::Dynamic block code description: invalid distance code lengths",
|
|
2121
|
+
INVALID_CODES_LEN: "inflate::Dynamic block code description: invalid literal/length code lengths",
|
|
2122
|
+
INVALID_STORE_BLOCK: "inflate::Stored block length did not match one's complement",
|
|
2123
|
+
INVALID_BLOCK_TYPE: "inflate::Invalid block type (type == 3)",
|
|
2124
|
+
|
|
2125
|
+
/* ADM-ZIP error messages */
|
|
2126
|
+
CANT_EXTRACT_FILE: "Could not extract the file",
|
|
2127
|
+
CANT_OVERRIDE: "Target file already exists",
|
|
2128
|
+
DISK_ENTRY_TOO_LARGE: "Number of disk entries is too large",
|
|
2129
|
+
NO_ZIP: "No zip file was loaded",
|
|
2130
|
+
NO_ENTRY: "Entry doesn't exist",
|
|
2131
|
+
DUPLICATE_ENTRY: "Duplicate entry name {0}",
|
|
2132
|
+
DIRECTORY_CONTENT_ERROR: "A directory cannot have content",
|
|
2133
|
+
FILE_NOT_FOUND: 'File not found: "{0}"',
|
|
2134
|
+
NOT_IMPLEMENTED: "Not implemented",
|
|
2135
|
+
INVALID_FILENAME: "Invalid filename",
|
|
2136
|
+
INVALID_FORMAT: "Invalid or unsupported zip format. No END header found",
|
|
2137
|
+
ZIP64_VALUE_TOO_LARGE: "Zip64 value exceeds the maximum safe integer",
|
|
2138
|
+
INVALID_PASS_PARAM: "Incompatible password parameter",
|
|
2139
|
+
WRONG_PASSWORD: "Wrong Password",
|
|
2140
|
+
|
|
2141
|
+
/* ADM-ZIP */
|
|
2142
|
+
COMMENT_TOO_LONG: "Comment is too long", // Comment can be max 65535 bytes long (NOTE: some non-US characters may take more space)
|
|
2143
|
+
EXTRA_FIELD_PARSE_ERROR: "Extra field parsing error"
|
|
2144
|
+
};
|
|
2145
|
+
|
|
2146
|
+
// template
|
|
2147
|
+
function E(message) {
|
|
2148
|
+
return function (...args) {
|
|
2149
|
+
if (args.length) { // Allow {0} .. {9} arguments in error message, based on argument number
|
|
2150
|
+
message = message.replace(/\{(\d)\}/g, (_, n) => args[n] || '');
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
return new Error('ADM-ZIP: ' + message);
|
|
2154
|
+
};
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
// Init errors with template
|
|
2158
|
+
for (const msg of Object.keys(errors)) {
|
|
2159
|
+
exports[msg] = E(errors[msg]);
|
|
2160
|
+
}
|
|
2161
|
+
|
|
2162
|
+
|
|
2163
|
+
/***/ },
|
|
2164
|
+
|
|
2165
|
+
/***/ 11520
|
|
2166
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
2167
|
+
|
|
2168
|
+
const pth = __webpack_require__(16928);
|
|
2169
|
+
|
|
2170
|
+
module.exports = function (/*String*/ path, /*Utils object*/ { fs }) {
|
|
2171
|
+
var _path = path || "",
|
|
2172
|
+
_obj = newAttr(),
|
|
2173
|
+
_stat = null;
|
|
2174
|
+
|
|
2175
|
+
function newAttr() {
|
|
2176
|
+
return {
|
|
2177
|
+
directory: false,
|
|
2178
|
+
readonly: false,
|
|
2179
|
+
hidden: false,
|
|
2180
|
+
executable: false,
|
|
2181
|
+
mtime: 0,
|
|
2182
|
+
atime: 0
|
|
2183
|
+
};
|
|
2184
|
+
}
|
|
2185
|
+
|
|
2186
|
+
if (_path && fs.existsSync(_path)) {
|
|
2187
|
+
_stat = fs.statSync(_path);
|
|
2188
|
+
_obj.directory = _stat.isDirectory();
|
|
2189
|
+
_obj.mtime = _stat.mtime;
|
|
2190
|
+
_obj.atime = _stat.atime;
|
|
2191
|
+
_obj.executable = (0o111 & _stat.mode) !== 0; // file is executable who ever har right not just owner
|
|
2192
|
+
_obj.readonly = (0o200 & _stat.mode) === 0; // readonly if owner has no write right
|
|
2193
|
+
_obj.hidden = pth.basename(_path)[0] === ".";
|
|
2194
|
+
} else {
|
|
2195
|
+
console.warn("Invalid path: " + _path);
|
|
2196
|
+
}
|
|
2197
|
+
|
|
2198
|
+
return {
|
|
2199
|
+
get directory() {
|
|
2200
|
+
return _obj.directory;
|
|
2201
|
+
},
|
|
2202
|
+
|
|
2203
|
+
get readOnly() {
|
|
2204
|
+
return _obj.readonly;
|
|
2205
|
+
},
|
|
2206
|
+
|
|
2207
|
+
get hidden() {
|
|
2208
|
+
return _obj.hidden;
|
|
2209
|
+
},
|
|
2210
|
+
|
|
2211
|
+
get mtime() {
|
|
2212
|
+
return _obj.mtime;
|
|
2213
|
+
},
|
|
2214
|
+
|
|
2215
|
+
get atime() {
|
|
2216
|
+
return _obj.atime;
|
|
2217
|
+
},
|
|
2218
|
+
|
|
2219
|
+
get executable() {
|
|
2220
|
+
return _obj.executable;
|
|
2221
|
+
},
|
|
2222
|
+
|
|
2223
|
+
decodeAttributes: function () {},
|
|
2224
|
+
|
|
2225
|
+
encodeAttributes: function () {},
|
|
2226
|
+
|
|
2227
|
+
toJSON: function () {
|
|
2228
|
+
return {
|
|
2229
|
+
path: _path,
|
|
2230
|
+
isDirectory: _obj.directory,
|
|
2231
|
+
isReadOnly: _obj.readonly,
|
|
2232
|
+
isHidden: _obj.hidden,
|
|
2233
|
+
isExecutable: _obj.executable,
|
|
2234
|
+
mTime: _obj.mtime,
|
|
2235
|
+
aTime: _obj.atime
|
|
2236
|
+
};
|
|
2237
|
+
},
|
|
2238
|
+
|
|
2239
|
+
toString: function () {
|
|
2240
|
+
return JSON.stringify(this.toJSON(), null, "\t");
|
|
2241
|
+
}
|
|
2242
|
+
};
|
|
2243
|
+
};
|
|
2244
|
+
|
|
2245
|
+
|
|
2246
|
+
/***/ },
|
|
2247
|
+
|
|
2248
|
+
/***/ 6942
|
|
2249
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
2250
|
+
|
|
2251
|
+
module.exports = __webpack_require__(66863);
|
|
2252
|
+
module.exports.Constants = __webpack_require__(37935);
|
|
2253
|
+
module.exports.Errors = __webpack_require__(22435);
|
|
2254
|
+
module.exports.FileAttr = __webpack_require__(11520);
|
|
2255
|
+
module.exports.decoder = __webpack_require__(39346);
|
|
2256
|
+
|
|
2257
|
+
|
|
2258
|
+
/***/ },
|
|
2259
|
+
|
|
2260
|
+
/***/ 66863
|
|
2261
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
2262
|
+
|
|
2263
|
+
const fsystem = __webpack_require__(79896);
|
|
2264
|
+
const pth = __webpack_require__(16928);
|
|
2265
|
+
const Constants = __webpack_require__(37935);
|
|
2266
|
+
const Errors = __webpack_require__(22435);
|
|
2267
|
+
const isWin = typeof process === "object" && "win32" === process.platform;
|
|
2268
|
+
|
|
2269
|
+
const is_Obj = (obj) => typeof obj === "object" && obj !== null;
|
|
2270
|
+
|
|
2271
|
+
// generate CRC32 lookup table
|
|
2272
|
+
const crcTable = new Uint32Array(256).map((t, c) => {
|
|
2273
|
+
for (let k = 0; k < 8; k++) {
|
|
2274
|
+
if ((c & 1) !== 0) {
|
|
2275
|
+
c = 0xedb88320 ^ (c >>> 1);
|
|
2276
|
+
} else {
|
|
2277
|
+
c >>>= 1;
|
|
2278
|
+
}
|
|
2279
|
+
}
|
|
2280
|
+
return c >>> 0;
|
|
2281
|
+
});
|
|
2282
|
+
|
|
2283
|
+
// UTILS functions
|
|
2284
|
+
|
|
2285
|
+
function Utils(opts) {
|
|
2286
|
+
this.sep = pth.sep;
|
|
2287
|
+
this.fs = fsystem;
|
|
2288
|
+
|
|
2289
|
+
if (is_Obj(opts)) {
|
|
2290
|
+
// custom filesystem
|
|
2291
|
+
if (is_Obj(opts.fs) && typeof opts.fs.statSync === "function") {
|
|
2292
|
+
this.fs = opts.fs;
|
|
2293
|
+
}
|
|
2294
|
+
}
|
|
2295
|
+
}
|
|
2296
|
+
|
|
2297
|
+
module.exports = Utils;
|
|
2298
|
+
|
|
2299
|
+
// INSTANTIABLE functions
|
|
2300
|
+
|
|
2301
|
+
Utils.prototype.makeDir = function (/*String*/ folder) {
|
|
2302
|
+
const self = this;
|
|
2303
|
+
|
|
2304
|
+
// Sync - make directories tree
|
|
2305
|
+
function mkdirSync(/*String*/ fpath) {
|
|
2306
|
+
let resolvedPath = fpath.split(self.sep)[0];
|
|
2307
|
+
fpath.split(self.sep).forEach(function (name) {
|
|
2308
|
+
if (!name || name.substr(-1, 1) === ":") return;
|
|
2309
|
+
resolvedPath += self.sep + name;
|
|
2310
|
+
var stat;
|
|
2311
|
+
try {
|
|
2312
|
+
stat = self.fs.statSync(resolvedPath);
|
|
2313
|
+
} catch (e) {
|
|
2314
|
+
if (e.message && e.message.startsWith('ENOENT')) {
|
|
2315
|
+
self.fs.mkdirSync(resolvedPath);
|
|
2316
|
+
} else {
|
|
2317
|
+
throw e;
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
if (stat && stat.isFile()) throw Errors.FILE_IN_THE_WAY(`"${resolvedPath}"`);
|
|
2321
|
+
});
|
|
2322
|
+
}
|
|
2323
|
+
|
|
2324
|
+
mkdirSync(folder);
|
|
2325
|
+
};
|
|
2326
|
+
|
|
2327
|
+
Utils.prototype.writeFileTo = function (/*String*/ path, /*Buffer*/ content, /*Boolean*/ overwrite, /*Number*/ attr) {
|
|
2328
|
+
const self = this;
|
|
2329
|
+
if (self.fs.existsSync(path)) {
|
|
2330
|
+
if (!overwrite) return false; // cannot overwrite
|
|
2331
|
+
|
|
2332
|
+
var stat = self.fs.statSync(path);
|
|
2333
|
+
if (stat.isDirectory()) {
|
|
2334
|
+
return false;
|
|
2335
|
+
}
|
|
2336
|
+
}
|
|
2337
|
+
var folder = pth.dirname(path);
|
|
2338
|
+
if (!self.fs.existsSync(folder)) {
|
|
2339
|
+
self.makeDir(folder);
|
|
2340
|
+
}
|
|
2341
|
+
|
|
2342
|
+
var fd;
|
|
2343
|
+
try {
|
|
2344
|
+
fd = self.fs.openSync(path, "w", 0o666); // 0666
|
|
2345
|
+
} catch (e) {
|
|
2346
|
+
self.fs.chmodSync(path, 0o666);
|
|
2347
|
+
fd = self.fs.openSync(path, "w", 0o666);
|
|
2348
|
+
}
|
|
2349
|
+
if (fd) {
|
|
2350
|
+
try {
|
|
2351
|
+
self.fs.writeSync(fd, content, 0, content.length, 0);
|
|
2352
|
+
} finally {
|
|
2353
|
+
self.fs.closeSync(fd);
|
|
2354
|
+
}
|
|
2355
|
+
}
|
|
2356
|
+
self.fs.chmodSync(path, attr || 0o666);
|
|
2357
|
+
return true;
|
|
2358
|
+
};
|
|
2359
|
+
|
|
2360
|
+
Utils.prototype.writeFileToAsync = function (/*String*/ path, /*Buffer*/ content, /*Boolean*/ overwrite, /*Number*/ attr, /*Function*/ callback) {
|
|
2361
|
+
if (typeof attr === "function") {
|
|
2362
|
+
callback = attr;
|
|
2363
|
+
attr = undefined;
|
|
2364
|
+
}
|
|
2365
|
+
|
|
2366
|
+
const self = this;
|
|
2367
|
+
|
|
2368
|
+
self.fs.exists(path, function (exist) {
|
|
2369
|
+
if (exist && !overwrite) return callback(false);
|
|
2370
|
+
|
|
2371
|
+
self.fs.stat(path, function (err, stat) {
|
|
2372
|
+
if (exist && stat && stat.isDirectory()) {
|
|
2373
|
+
return callback(false);
|
|
2374
|
+
}
|
|
2375
|
+
|
|
2376
|
+
var folder = pth.dirname(path);
|
|
2377
|
+
self.fs.exists(folder, function (exists) {
|
|
2378
|
+
if (!exists) {
|
|
2379
|
+
// makeDir is synchronous and can throw (e.g. EACCES); report failure
|
|
2380
|
+
// rather than letting it escape this callback as an uncaught exception
|
|
2381
|
+
try {
|
|
2382
|
+
self.makeDir(folder);
|
|
2383
|
+
} catch (e) {
|
|
2384
|
+
return callback(false);
|
|
2385
|
+
}
|
|
2386
|
+
}
|
|
2387
|
+
|
|
2388
|
+
// write the content to an open descriptor, then apply the attributes
|
|
2389
|
+
const writeToFd = function (fd) {
|
|
2390
|
+
self.fs.write(fd, content, 0, content.length, 0, function (writeErr) {
|
|
2391
|
+
self.fs.close(fd, function () {
|
|
2392
|
+
// surface write failures instead of silently reporting success (issue #402)
|
|
2393
|
+
if (writeErr) return callback(false);
|
|
2394
|
+
self.fs.chmod(path, attr || 0o666, function () {
|
|
2395
|
+
callback(true);
|
|
2396
|
+
});
|
|
2397
|
+
});
|
|
2398
|
+
});
|
|
2399
|
+
};
|
|
2400
|
+
|
|
2401
|
+
self.fs.open(path, "w", 0o666, function (err, fd) {
|
|
2402
|
+
if (err) {
|
|
2403
|
+
// the target may exist but be read-only: make it writable and retry once
|
|
2404
|
+
self.fs.chmod(path, 0o666, function () {
|
|
2405
|
+
self.fs.open(path, "w", 0o666, function (retryErr, fd) {
|
|
2406
|
+
// Previously the retry error was ignored and an undefined fd was
|
|
2407
|
+
// passed to fs.write, throwing an uncaught ERR_INVALID_ARG_TYPE that
|
|
2408
|
+
// crashed the process (issues #470, #459, #402). Report failure instead.
|
|
2409
|
+
if (retryErr || !fd) return callback(false);
|
|
2410
|
+
writeToFd(fd);
|
|
2411
|
+
});
|
|
2412
|
+
});
|
|
2413
|
+
} else if (fd) {
|
|
2414
|
+
writeToFd(fd);
|
|
2415
|
+
} else {
|
|
2416
|
+
callback(false);
|
|
2417
|
+
}
|
|
2418
|
+
});
|
|
2419
|
+
});
|
|
2420
|
+
});
|
|
2421
|
+
});
|
|
2422
|
+
};
|
|
2423
|
+
|
|
2424
|
+
// Guard extraction against writing through a symlink that already exists inside
|
|
2425
|
+
// the target directory. sanitize() only proves the textual path stays under the
|
|
2426
|
+
// root; it cannot see that a component on disk is a symlink pointing elsewhere,
|
|
2427
|
+
// so open()/mkdir() would follow it and write outside the root. Walk every path
|
|
2428
|
+
// component strictly below root and reject any that is a symlink. Components at
|
|
2429
|
+
// or above root are the caller's own choice and are left untouched, so a root
|
|
2430
|
+
// that itself lives under a symlink (e.g. /tmp on macOS) still extracts.
|
|
2431
|
+
Utils.prototype.assertPathSafe = function (/*String*/ root, /*String*/ target) {
|
|
2432
|
+
const self = this;
|
|
2433
|
+
if (typeof self.fs.lstatSync !== "function") return;
|
|
2434
|
+
|
|
2435
|
+
const resolvedRoot = pth.resolve(root);
|
|
2436
|
+
const resolvedTarget = pth.resolve(target);
|
|
2437
|
+
if (resolvedTarget === resolvedRoot) return;
|
|
2438
|
+
|
|
2439
|
+
const rel = pth.relative(resolvedRoot, resolvedTarget);
|
|
2440
|
+
// Not under root: sanitize() is responsible for that case; nothing to walk.
|
|
2441
|
+
if (!rel || rel === ".." || rel.startsWith(".." + pth.sep) || pth.isAbsolute(rel)) return;
|
|
2442
|
+
|
|
2443
|
+
let cur = resolvedRoot;
|
|
2444
|
+
for (const part of rel.split(pth.sep)) {
|
|
2445
|
+
if (!part || part === ".") continue;
|
|
2446
|
+
cur = pth.join(cur, part);
|
|
2447
|
+
let stat;
|
|
2448
|
+
try {
|
|
2449
|
+
stat = self.fs.lstatSync(cur);
|
|
2450
|
+
} catch (e) {
|
|
2451
|
+
break; // component does not exist yet: nothing below it can be a symlink
|
|
2452
|
+
}
|
|
2453
|
+
if (stat.isSymbolicLink()) throw Errors.FILE_IN_THE_WAY(`"${cur}"`);
|
|
2454
|
+
}
|
|
2455
|
+
};
|
|
2456
|
+
|
|
2457
|
+
Utils.prototype.findFiles = function (/*String*/ path) {
|
|
2458
|
+
const self = this;
|
|
2459
|
+
const canLstat = typeof self.fs.lstatSync === "function";
|
|
2460
|
+
const rootReal = self.fs.realpathSync(path);
|
|
2461
|
+
|
|
2462
|
+
// A symlink whose target lies outside the folder being archived must not be
|
|
2463
|
+
// followed: statSync would dereference it and copy the target's contents into
|
|
2464
|
+
// the archive, disclosing files outside the root (GHSA-wx42-xcp7-pgr4). Allow
|
|
2465
|
+
// symlinks that resolve to a location inside the root, reject any that escape.
|
|
2466
|
+
function escapesRoot(/*String*/ p) {
|
|
2467
|
+
if (!canLstat) return false;
|
|
2468
|
+
if (!self.fs.lstatSync(p).isSymbolicLink()) return false;
|
|
2469
|
+
let real;
|
|
2470
|
+
try {
|
|
2471
|
+
real = self.fs.realpathSync(p);
|
|
2472
|
+
} catch (e) {
|
|
2473
|
+
return true; // dangling or unresolvable symlink: do not follow
|
|
2474
|
+
}
|
|
2475
|
+
return !(real === rootReal || real.startsWith(rootReal + pth.sep));
|
|
2476
|
+
}
|
|
2477
|
+
|
|
2478
|
+
function findSync(/*String*/ dir, /*RegExp*/ pattern, /*Boolean*/ recursive, /*Set*/ visited) {
|
|
2479
|
+
if (typeof pattern === "boolean") {
|
|
2480
|
+
recursive = pattern;
|
|
2481
|
+
pattern = undefined;
|
|
2482
|
+
}
|
|
2483
|
+
let files = [];
|
|
2484
|
+
self.fs.readdirSync(dir).forEach(function (file) {
|
|
2485
|
+
const path = pth.join(dir, file);
|
|
2486
|
+
|
|
2487
|
+
if (escapesRoot(path)) return;
|
|
2488
|
+
|
|
2489
|
+
const stat = self.fs.statSync(path);
|
|
2490
|
+
|
|
2491
|
+
if (!pattern || pattern.test(path)) {
|
|
2492
|
+
files.push(pth.normalize(path) + (stat.isDirectory() ? self.sep : ""));
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
if (stat.isDirectory() && recursive) {
|
|
2496
|
+
// Descend by resolved real path and skip directories we have already
|
|
2497
|
+
// visited. This stops a symlink that points back to an ancestor from
|
|
2498
|
+
// recursing forever until the path fails with ELOOP / ENAMETOOLONG
|
|
2499
|
+
// (issue #541).
|
|
2500
|
+
const realDir = self.fs.realpathSync(path);
|
|
2501
|
+
if (!visited.has(realDir)) {
|
|
2502
|
+
visited.add(realDir);
|
|
2503
|
+
files = files.concat(findSync(path, pattern, recursive, visited));
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
});
|
|
2507
|
+
return files;
|
|
2508
|
+
}
|
|
2509
|
+
|
|
2510
|
+
return findSync(path, undefined, true, new Set([rootReal]));
|
|
2511
|
+
};
|
|
2512
|
+
|
|
2513
|
+
/**
|
|
2514
|
+
* Callback for showing if everything was done.
|
|
2515
|
+
*
|
|
2516
|
+
* @callback filelistCallback
|
|
2517
|
+
* @param {Error} err - Error object
|
|
2518
|
+
* @param {string[]} list - was request fully completed
|
|
2519
|
+
*/
|
|
2520
|
+
|
|
2521
|
+
/**
|
|
2522
|
+
*
|
|
2523
|
+
* @param {string} dir
|
|
2524
|
+
* @param {filelistCallback} cb
|
|
2525
|
+
*/
|
|
2526
|
+
Utils.prototype.findFilesAsync = function (dir, cb) {
|
|
2527
|
+
const self = this;
|
|
2528
|
+
const results = [];
|
|
2529
|
+
let finished = false;
|
|
2530
|
+
const finish = function (err) {
|
|
2531
|
+
if (finished) return;
|
|
2532
|
+
finished = true;
|
|
2533
|
+
cb(err, err ? undefined : results);
|
|
2534
|
+
};
|
|
2535
|
+
|
|
2536
|
+
const canLstat = typeof self.fs.lstat === "function";
|
|
2537
|
+
let rootReal = null;
|
|
2538
|
+
|
|
2539
|
+
// Reject a symlink whose target escapes the root being archived, so its
|
|
2540
|
+
// contents are not dereferenced and copied into the archive
|
|
2541
|
+
// (GHSA-wx42-xcp7-pgr4). A symlink resolving to a location inside the root is
|
|
2542
|
+
// allowed; a dangling or escaping one is skipped. Calls back (err, escapes).
|
|
2543
|
+
const escapesRoot = function (file, cb) {
|
|
2544
|
+
if (!canLstat) return cb(null, false);
|
|
2545
|
+
self.fs.lstat(file, function (err, lst) {
|
|
2546
|
+
if (err) return cb(err);
|
|
2547
|
+
if (!lst || !lst.isSymbolicLink()) return cb(null, false);
|
|
2548
|
+
self.fs.realpath(file, function (err, real) {
|
|
2549
|
+
if (err) return cb(null, true); // dangling: do not follow
|
|
2550
|
+
cb(null, !(real === rootReal || real.startsWith(rootReal + pth.sep)));
|
|
2551
|
+
});
|
|
2552
|
+
});
|
|
2553
|
+
};
|
|
2554
|
+
|
|
2555
|
+
// Descend by resolved real path and skip directories already visited, so a
|
|
2556
|
+
// symlink pointing back to an ancestor cannot recurse forever (issue #541).
|
|
2557
|
+
const walk = function (dir, visited, done) {
|
|
2558
|
+
self.fs.readdir(dir, function (err, list) {
|
|
2559
|
+
if (err) return done(err);
|
|
2560
|
+
let pending = list.length;
|
|
2561
|
+
if (!pending) return done();
|
|
2562
|
+
list.forEach(function (name) {
|
|
2563
|
+
const file = pth.join(dir, name);
|
|
2564
|
+
escapesRoot(file, function (err, escapes) {
|
|
2565
|
+
if (err) return done(err);
|
|
2566
|
+
if (escapes) {
|
|
2567
|
+
if (!--pending) done();
|
|
2568
|
+
return;
|
|
2569
|
+
}
|
|
2570
|
+
self.fs.stat(file, function (err, stat) {
|
|
2571
|
+
if (err) return done(err);
|
|
2572
|
+
if (!stat) {
|
|
2573
|
+
if (!--pending) done();
|
|
2574
|
+
return;
|
|
2575
|
+
}
|
|
2576
|
+
results.push(pth.normalize(file) + (stat.isDirectory() ? self.sep : ""));
|
|
2577
|
+
if (!stat.isDirectory()) {
|
|
2578
|
+
if (!--pending) done();
|
|
2579
|
+
return;
|
|
2580
|
+
}
|
|
2581
|
+
self.fs.realpath(file, function (err, realDir) {
|
|
2582
|
+
if (err) return done(err);
|
|
2583
|
+
if (visited.has(realDir)) {
|
|
2584
|
+
if (!--pending) done();
|
|
2585
|
+
return;
|
|
2586
|
+
}
|
|
2587
|
+
visited.add(realDir);
|
|
2588
|
+
walk(file, visited, function (err) {
|
|
2589
|
+
if (err) return done(err);
|
|
2590
|
+
if (!--pending) done();
|
|
2591
|
+
});
|
|
2592
|
+
});
|
|
2593
|
+
});
|
|
2594
|
+
});
|
|
2595
|
+
});
|
|
2596
|
+
});
|
|
2597
|
+
};
|
|
2598
|
+
|
|
2599
|
+
self.fs.realpath(dir, function (err, realDir) {
|
|
2600
|
+
if (err) return finish(err);
|
|
2601
|
+
rootReal = realDir;
|
|
2602
|
+
walk(dir, new Set([realDir]), finish);
|
|
2603
|
+
});
|
|
2604
|
+
};
|
|
2605
|
+
|
|
2606
|
+
Utils.prototype.getAttributes = function () {};
|
|
2607
|
+
|
|
2608
|
+
Utils.prototype.setAttributes = function () {};
|
|
2609
|
+
|
|
2610
|
+
// STATIC functions
|
|
2611
|
+
|
|
2612
|
+
// crc32 single update (it is part of crc32)
|
|
2613
|
+
Utils.crc32update = function (crc, byte) {
|
|
2614
|
+
return crcTable[(crc ^ byte) & 0xff] ^ (crc >>> 8);
|
|
2615
|
+
};
|
|
2616
|
+
|
|
2617
|
+
Utils.crc32 = function (buf) {
|
|
2618
|
+
if (typeof buf === "string") {
|
|
2619
|
+
buf = Buffer.from(buf, "utf8");
|
|
2620
|
+
}
|
|
2621
|
+
|
|
2622
|
+
let len = buf.length;
|
|
2623
|
+
let crc = ~0;
|
|
2624
|
+
for (let off = 0; off < len; ) crc = Utils.crc32update(crc, buf[off++]);
|
|
2625
|
+
// xor and cast as uint32 number
|
|
2626
|
+
return ~crc >>> 0;
|
|
2627
|
+
};
|
|
2628
|
+
|
|
2629
|
+
Utils.methodToString = function (/*Number*/ method) {
|
|
2630
|
+
switch (method) {
|
|
2631
|
+
case Constants.STORED:
|
|
2632
|
+
return "STORED (" + method + ")";
|
|
2633
|
+
case Constants.DEFLATED:
|
|
2634
|
+
return "DEFLATED (" + method + ")";
|
|
2635
|
+
default:
|
|
2636
|
+
return "UNSUPPORTED (" + method + ")";
|
|
2637
|
+
}
|
|
2638
|
+
};
|
|
2639
|
+
|
|
2640
|
+
/**
|
|
2641
|
+
* removes ".." style path elements
|
|
2642
|
+
* @param {string} path - fixable path
|
|
2643
|
+
* @returns string - fixed filepath
|
|
2644
|
+
*/
|
|
2645
|
+
Utils.canonical = function (/*string*/ path) {
|
|
2646
|
+
if (!path) return "";
|
|
2647
|
+
// trick normalize think path is absolute
|
|
2648
|
+
const safeSuffix = pth.posix.normalize("/" + path.split("\\").join("/"));
|
|
2649
|
+
return pth.join(".", safeSuffix);
|
|
2650
|
+
};
|
|
2651
|
+
|
|
2652
|
+
/**
|
|
2653
|
+
* fix file names in achive
|
|
2654
|
+
* @param {string} path - fixable path
|
|
2655
|
+
* @returns string - fixed filepath
|
|
2656
|
+
*/
|
|
2657
|
+
|
|
2658
|
+
Utils.zipnamefix = function (path) {
|
|
2659
|
+
if (!path) return "";
|
|
2660
|
+
// trick normalize think path is absolute
|
|
2661
|
+
const safeSuffix = pth.posix.normalize("/" + path.split("\\").join("/"));
|
|
2662
|
+
return pth.posix.join(".", safeSuffix);
|
|
2663
|
+
};
|
|
2664
|
+
|
|
2665
|
+
/**
|
|
2666
|
+
*
|
|
2667
|
+
* @param {Array} arr
|
|
2668
|
+
* @param {function} callback
|
|
2669
|
+
* @returns
|
|
2670
|
+
*/
|
|
2671
|
+
Utils.findLast = function (arr, callback) {
|
|
2672
|
+
if (!Array.isArray(arr)) throw new TypeError("arr is not array");
|
|
2673
|
+
|
|
2674
|
+
const len = arr.length >>> 0;
|
|
2675
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
2676
|
+
if (callback(arr[i], i, arr)) {
|
|
2677
|
+
return arr[i];
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
return void 0;
|
|
2681
|
+
};
|
|
2682
|
+
|
|
2683
|
+
// make absolute paths taking prefix as root folder
|
|
2684
|
+
Utils.sanitize = function (/*string*/ prefix, /*string*/ name) {
|
|
2685
|
+
prefix = pth.resolve(pth.normalize(prefix));
|
|
2686
|
+
var parts = name.split("/");
|
|
2687
|
+
for (var i = 0, l = parts.length; i < l; i++) {
|
|
2688
|
+
var path = pth.normalize(pth.join(prefix, parts.slice(i, l).join(pth.sep)));
|
|
2689
|
+
if (path === prefix || path.startsWith(prefix + pth.sep)) {
|
|
2690
|
+
return path;
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
return pth.normalize(pth.join(prefix, pth.basename(name)));
|
|
2694
|
+
};
|
|
2695
|
+
|
|
2696
|
+
// converts buffer, Uint8Array, string types to buffer
|
|
2697
|
+
Utils.toBuffer = function toBuffer(/*buffer, Uint8Array, string*/ input, /* function */ encoder) {
|
|
2698
|
+
if (Buffer.isBuffer(input)) {
|
|
2699
|
+
return input;
|
|
2700
|
+
} else if (input instanceof Uint8Array) {
|
|
2701
|
+
return Buffer.from(input);
|
|
2702
|
+
} else {
|
|
2703
|
+
// expect string all other values are invalid and return empty buffer
|
|
2704
|
+
return typeof input === "string" ? encoder(input) : Buffer.alloc(0);
|
|
2705
|
+
}
|
|
2706
|
+
};
|
|
2707
|
+
|
|
2708
|
+
Utils.readBigUInt64LE = function (/*Buffer*/ buffer, /*int*/ index) {
|
|
2709
|
+
const lo = buffer.readUInt32LE(index);
|
|
2710
|
+
const hi = buffer.readUInt32LE(index + 4);
|
|
2711
|
+
const value = hi * 0x100000000 + lo;
|
|
2712
|
+
// The result is a JS number, so values above 2^53 - 1 cannot be represented
|
|
2713
|
+
// exactly. These are zip64 sizes/offsets/counts used as buffer indices; a
|
|
2714
|
+
// silently rounded value would misparse the archive. Reject instead.
|
|
2715
|
+
if (value > Number.MAX_SAFE_INTEGER) {
|
|
2716
|
+
throw Errors.ZIP64_VALUE_TOO_LARGE();
|
|
2717
|
+
}
|
|
2718
|
+
return value;
|
|
2719
|
+
};
|
|
2720
|
+
|
|
2721
|
+
Utils.writeBigUInt64LE = function (/*Buffer*/ buffer, /*Number*/ value, /*int*/ index) {
|
|
2722
|
+
const lo = value >>> 0;
|
|
2723
|
+
const hi = Math.floor(value / 0x100000000) >>> 0;
|
|
2724
|
+
buffer.writeUInt32LE(lo, index);
|
|
2725
|
+
buffer.writeUInt32LE(hi, index + 4);
|
|
2726
|
+
};
|
|
2727
|
+
|
|
2728
|
+
Utils.fromDOS2Date = function (val) {
|
|
2729
|
+
return new Date(((val >> 25) & 0x7f) + 1980, Math.max(((val >> 21) & 0x0f) - 1, 0), Math.max((val >> 16) & 0x1f, 1), (val >> 11) & 0x1f, (val >> 5) & 0x3f, (val & 0x1f) << 1);
|
|
2730
|
+
};
|
|
2731
|
+
|
|
2732
|
+
Utils.fromDate2DOS = function (val) {
|
|
2733
|
+
let date = 0;
|
|
2734
|
+
let time = 0;
|
|
2735
|
+
if (val.getFullYear() > 1979) {
|
|
2736
|
+
date = (((val.getFullYear() - 1980) & 0x7f) << 9) | ((val.getMonth() + 1) << 5) | val.getDate();
|
|
2737
|
+
time = (val.getHours() << 11) | (val.getMinutes() << 5) | (val.getSeconds() >> 1);
|
|
2738
|
+
}
|
|
2739
|
+
return (date << 16) | time;
|
|
2740
|
+
};
|
|
2741
|
+
|
|
2742
|
+
Utils.isWin = isWin; // Do we have windows system
|
|
2743
|
+
Utils.crcTable = crcTable;
|
|
2744
|
+
|
|
2745
|
+
|
|
2746
|
+
/***/ },
|
|
2747
|
+
|
|
2748
|
+
/***/ 66488
|
|
2749
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
2750
|
+
|
|
2751
|
+
var Utils = __webpack_require__(6942),
|
|
2752
|
+
Headers = __webpack_require__(21204),
|
|
2753
|
+
Constants = Utils.Constants,
|
|
2754
|
+
Methods = __webpack_require__(16552);
|
|
2755
|
+
|
|
2756
|
+
module.exports = function (/** object */ options, /*Buffer*/ input) {
|
|
2757
|
+
var _centralHeader = new Headers.EntryHeader(),
|
|
2758
|
+
_entryName = Buffer.alloc(0),
|
|
2759
|
+
_comment = Buffer.alloc(0),
|
|
2760
|
+
_isDirectory = false,
|
|
2761
|
+
uncompressedData = null,
|
|
2762
|
+
_extra = Buffer.alloc(0),
|
|
2763
|
+
_extralocal = Buffer.alloc(0),
|
|
2764
|
+
_efs = true;
|
|
2765
|
+
|
|
2766
|
+
// assign options
|
|
2767
|
+
const opts = options;
|
|
2768
|
+
|
|
2769
|
+
const decoder = typeof opts.decoder === "object" ? opts.decoder : Utils.decoder;
|
|
2770
|
+
_efs = decoder.hasOwnProperty("efs") ? decoder.efs : false;
|
|
2771
|
+
|
|
2772
|
+
function getCompressedDataFromZip() {
|
|
2773
|
+
//if (!input || !Buffer.isBuffer(input)) {
|
|
2774
|
+
if (!input || !(input instanceof Uint8Array)) {
|
|
2775
|
+
return Buffer.alloc(0);
|
|
2776
|
+
}
|
|
2777
|
+
_extralocal = _centralHeader.loadLocalHeaderFromBinary(input);
|
|
2778
|
+
const dataOffset = _centralHeader.realDataOffset;
|
|
2779
|
+
const dataEnd = dataOffset + _centralHeader.compressedSize;
|
|
2780
|
+
// The offsets and sizes come from attacker-controlled headers. Require the
|
|
2781
|
+
// declared compressed extent to be fully present rather than letting slice()
|
|
2782
|
+
// silently clamp to a short buffer (which only surfaces later as a CRC
|
|
2783
|
+
// failure). Fail loudly with a header error instead (GHSA-wwrv-q5gf-5843).
|
|
2784
|
+
if (dataOffset < 0 || dataEnd < dataOffset || dataEnd > input.length) {
|
|
2785
|
+
throw Utils.Errors.INVALID_LOC();
|
|
2786
|
+
}
|
|
2787
|
+
return input.slice(dataOffset, dataEnd);
|
|
2788
|
+
}
|
|
2789
|
+
|
|
2790
|
+
function crc32OK(data) {
|
|
2791
|
+
// When bit 3 (0x08) of the general-purpose flags is set, the crc-32 and
|
|
2792
|
+
// sizes were unknown when the local file header was written, so that
|
|
2793
|
+
// header carries placeholder zeros and the real values are repeated in a
|
|
2794
|
+
// data descriptor after the compressed data. adm-zip always parses the
|
|
2795
|
+
// central directory, whose header holds the authoritative crc-32 and
|
|
2796
|
+
// sizes, so we validate the payload against that value.
|
|
2797
|
+
//
|
|
2798
|
+
// Earlier versions instead located and parsed the trailing descriptor and
|
|
2799
|
+
// threw when it was absent or in an unexpected shape. Many valid archives
|
|
2800
|
+
// set the descriptor flag but write the real crc/sizes into the local and
|
|
2801
|
+
// central headers without emitting a descriptor (or write one we did not
|
|
2802
|
+
// recognise), so that strict handling rejected readable zips
|
|
2803
|
+
// (issues #533, #548, #554). Trusting the central-directory crc keeps the
|
|
2804
|
+
// integrity check while accepting those archives.
|
|
2805
|
+
const expectedCrc = _centralHeader.flags_desc || _centralHeader.localHeader.flags_desc ? _centralHeader.crc : _centralHeader.localHeader.crc;
|
|
2806
|
+
|
|
2807
|
+
return Utils.crc32(data) === expectedCrc;
|
|
2808
|
+
}
|
|
2809
|
+
|
|
2810
|
+
function decompress(/*Boolean*/ async, /*Function*/ callback, /*String, Buffer*/ pass) {
|
|
2811
|
+
if (typeof callback === "undefined" && typeof async === "string") {
|
|
2812
|
+
pass = async;
|
|
2813
|
+
async = void 0;
|
|
2814
|
+
}
|
|
2815
|
+
if (_isDirectory) {
|
|
2816
|
+
if (async && callback) {
|
|
2817
|
+
callback(Buffer.alloc(0), Utils.Errors.DIRECTORY_CONTENT_ERROR()); //si added error.
|
|
2818
|
+
}
|
|
2819
|
+
return Buffer.alloc(0);
|
|
2820
|
+
}
|
|
2821
|
+
|
|
2822
|
+
var compressedData;
|
|
2823
|
+
try {
|
|
2824
|
+
compressedData = getCompressedDataFromZip();
|
|
2825
|
+
|
|
2826
|
+
if (compressedData.length === 0) {
|
|
2827
|
+
// File is empty, nothing to decompress.
|
|
2828
|
+
if (async && callback) callback(compressedData);
|
|
2829
|
+
return compressedData;
|
|
2830
|
+
}
|
|
2831
|
+
|
|
2832
|
+
if (_centralHeader.encrypted) {
|
|
2833
|
+
if ("string" !== typeof pass && !Buffer.isBuffer(pass)) {
|
|
2834
|
+
throw Utils.Errors.INVALID_PASS_PARAM();
|
|
2835
|
+
}
|
|
2836
|
+
compressedData = Methods.ZipCrypto.decrypt(compressedData, _centralHeader, pass);
|
|
2837
|
+
}
|
|
2838
|
+
} catch (err) {
|
|
2839
|
+
// These synchronous parse/setup steps run before any callback fires.
|
|
2840
|
+
// In async mode a malformed local header (e.g. a bad LOC offset) would
|
|
2841
|
+
// otherwise throw out of getDataAsync and bypass the callback error
|
|
2842
|
+
// channel, crashing the caller. Route it through the callback instead.
|
|
2843
|
+
if (async && callback) {
|
|
2844
|
+
callback(Buffer.alloc(0), err);
|
|
2845
|
+
return Buffer.alloc(0);
|
|
2846
|
+
}
|
|
2847
|
+
throw err;
|
|
2848
|
+
}
|
|
2849
|
+
|
|
2850
|
+
var data;
|
|
2851
|
+
|
|
2852
|
+
switch (_centralHeader.method) {
|
|
2853
|
+
case Utils.Constants.STORED:
|
|
2854
|
+
// STORED entries are not compressed, so the uncompressed output is
|
|
2855
|
+
// exactly the bytes present in the archive. Allocate from the real
|
|
2856
|
+
// data length rather than the attacker-declared central-directory
|
|
2857
|
+
// size, otherwise a tiny archive can declare a huge size and force a
|
|
2858
|
+
// multi-gigabyte allocation before any validation (CVE-2026-39244).
|
|
2859
|
+
data = Buffer.alloc(compressedData.length);
|
|
2860
|
+
compressedData.copy(data);
|
|
2861
|
+
if (!crc32OK(data)) {
|
|
2862
|
+
if (async && callback) callback(data, Utils.Errors.BAD_CRC()); //si added error
|
|
2863
|
+
throw Utils.Errors.BAD_CRC();
|
|
2864
|
+
} else {
|
|
2865
|
+
//si added otherwise did not seem to return data.
|
|
2866
|
+
if (async && callback) callback(data);
|
|
2867
|
+
return data;
|
|
2868
|
+
}
|
|
2869
|
+
case Utils.Constants.DEFLATED:
|
|
2870
|
+
// Do not pre-allocate the declared uncompressed size. The inflater
|
|
2871
|
+
// grows its output buffer as zlib emits data and caps the total at
|
|
2872
|
+
// the declared size (maxOutputLength), so a bogus size can no longer
|
|
2873
|
+
// trigger an eager allocation before the data is read (CVE-2026-39244).
|
|
2874
|
+
var inflater = new Methods.Inflater(compressedData, _centralHeader.size);
|
|
2875
|
+
if (!async) {
|
|
2876
|
+
data = inflater.inflate();
|
|
2877
|
+
if (!crc32OK(data)) {
|
|
2878
|
+
throw Utils.Errors.BAD_CRC(`"${decoder.decode(_entryName)}"`);
|
|
2879
|
+
}
|
|
2880
|
+
return data;
|
|
2881
|
+
} else {
|
|
2882
|
+
inflater.inflateAsync(function (result, err) {
|
|
2883
|
+
if (!callback) return;
|
|
2884
|
+
if (err) {
|
|
2885
|
+
// surface inflater/stream failures instead of validating
|
|
2886
|
+
// the empty placeholder buffer against the CRC
|
|
2887
|
+
callback(Buffer.alloc(0), err);
|
|
2888
|
+
} else if (!crc32OK(result)) {
|
|
2889
|
+
callback(result, Utils.Errors.BAD_CRC()); //si added error
|
|
2890
|
+
} else {
|
|
2891
|
+
callback(result);
|
|
2892
|
+
}
|
|
2893
|
+
});
|
|
2894
|
+
}
|
|
2895
|
+
break;
|
|
2896
|
+
default:
|
|
2897
|
+
if (async && callback) callback(Buffer.alloc(0), Utils.Errors.UNKNOWN_METHOD());
|
|
2898
|
+
throw Utils.Errors.UNKNOWN_METHOD();
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
|
|
2902
|
+
function compress(/*Boolean*/ async, /*Function*/ callback) {
|
|
2903
|
+
if ((!uncompressedData || !uncompressedData.length) && Buffer.isBuffer(input)) {
|
|
2904
|
+
// no data set or the data wasn't changed to require recompression
|
|
2905
|
+
if (async && callback) callback(getCompressedDataFromZip());
|
|
2906
|
+
return getCompressedDataFromZip();
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
if (uncompressedData.length && !_isDirectory) {
|
|
2910
|
+
var compressedData;
|
|
2911
|
+
// Local file header
|
|
2912
|
+
switch (_centralHeader.method) {
|
|
2913
|
+
case Utils.Constants.STORED:
|
|
2914
|
+
_centralHeader.compressedSize = _centralHeader.size;
|
|
2915
|
+
|
|
2916
|
+
compressedData = Buffer.alloc(uncompressedData.length);
|
|
2917
|
+
uncompressedData.copy(compressedData);
|
|
2918
|
+
|
|
2919
|
+
if (async && callback) callback(compressedData);
|
|
2920
|
+
return compressedData;
|
|
2921
|
+
default:
|
|
2922
|
+
case Utils.Constants.DEFLATED:
|
|
2923
|
+
var deflater = new Methods.Deflater(uncompressedData);
|
|
2924
|
+
if (!async) {
|
|
2925
|
+
var deflated = deflater.deflate();
|
|
2926
|
+
_centralHeader.compressedSize = deflated.length;
|
|
2927
|
+
return deflated;
|
|
2928
|
+
} else {
|
|
2929
|
+
deflater.deflateAsync(function (data) {
|
|
2930
|
+
compressedData = Buffer.alloc(data.length);
|
|
2931
|
+
_centralHeader.compressedSize = data.length;
|
|
2932
|
+
data.copy(compressedData);
|
|
2933
|
+
callback && callback(compressedData);
|
|
2934
|
+
});
|
|
2935
|
+
}
|
|
2936
|
+
deflater = null;
|
|
2937
|
+
break;
|
|
2938
|
+
}
|
|
2939
|
+
} else if (async && callback) {
|
|
2940
|
+
callback(Buffer.alloc(0));
|
|
2941
|
+
} else {
|
|
2942
|
+
return Buffer.alloc(0);
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
function readUInt64LE(buffer, offset) {
|
|
2947
|
+
return Utils.readBigUInt64LE(buffer, offset);
|
|
2948
|
+
}
|
|
2949
|
+
|
|
2950
|
+
function parseExtra(data) {
|
|
2951
|
+
try {
|
|
2952
|
+
var offset = 0;
|
|
2953
|
+
var signature, size, part;
|
|
2954
|
+
while (offset + 4 < data.length) {
|
|
2955
|
+
signature = data.readUInt16LE(offset);
|
|
2956
|
+
offset += 2;
|
|
2957
|
+
size = data.readUInt16LE(offset);
|
|
2958
|
+
offset += 2;
|
|
2959
|
+
part = data.slice(offset, offset + size);
|
|
2960
|
+
offset += size;
|
|
2961
|
+
if (Constants.ID_ZIP64 === signature) {
|
|
2962
|
+
parseZip64ExtendedInformation(part);
|
|
2963
|
+
}
|
|
2964
|
+
}
|
|
2965
|
+
} catch (error) {
|
|
2966
|
+
throw Utils.Errors.EXTRA_FIELD_PARSE_ERROR();
|
|
2967
|
+
}
|
|
2968
|
+
}
|
|
2969
|
+
|
|
2970
|
+
//Override header field values with values from the ZIP64 extra field
|
|
2971
|
+
function parseZip64ExtendedInformation(data) {
|
|
2972
|
+
var size, compressedSize, offset, diskNumStart;
|
|
2973
|
+
|
|
2974
|
+
if (data.length >= Constants.EF_ZIP64_SCOMP) {
|
|
2975
|
+
size = readUInt64LE(data, Constants.EF_ZIP64_SUNCOMP);
|
|
2976
|
+
if (_centralHeader.size === Constants.EF_ZIP64_OR_32) {
|
|
2977
|
+
_centralHeader.size = size;
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2980
|
+
if (data.length >= Constants.EF_ZIP64_RHO) {
|
|
2981
|
+
compressedSize = readUInt64LE(data, Constants.EF_ZIP64_SCOMP);
|
|
2982
|
+
if (_centralHeader.compressedSize === Constants.EF_ZIP64_OR_32) {
|
|
2983
|
+
_centralHeader.compressedSize = compressedSize;
|
|
2984
|
+
}
|
|
2985
|
+
}
|
|
2986
|
+
if (data.length >= Constants.EF_ZIP64_DSN) {
|
|
2987
|
+
offset = readUInt64LE(data, Constants.EF_ZIP64_RHO);
|
|
2988
|
+
if (_centralHeader.offset === Constants.EF_ZIP64_OR_32) {
|
|
2989
|
+
_centralHeader.offset = offset;
|
|
2990
|
+
}
|
|
2991
|
+
}
|
|
2992
|
+
if (data.length >= Constants.EF_ZIP64_DSN + 4) {
|
|
2993
|
+
diskNumStart = data.readUInt32LE(Constants.EF_ZIP64_DSN);
|
|
2994
|
+
if (_centralHeader.diskNumStart === Constants.EF_ZIP64_OR_16) {
|
|
2995
|
+
_centralHeader.diskNumStart = diskNumStart;
|
|
2996
|
+
}
|
|
2997
|
+
}
|
|
2998
|
+
}
|
|
2999
|
+
|
|
3000
|
+
return {
|
|
3001
|
+
get entryName() {
|
|
3002
|
+
return decoder.decode(_entryName);
|
|
3003
|
+
},
|
|
3004
|
+
get rawEntryName() {
|
|
3005
|
+
return _entryName;
|
|
3006
|
+
},
|
|
3007
|
+
set entryName(val) {
|
|
3008
|
+
_entryName = Utils.toBuffer(val, decoder.encode);
|
|
3009
|
+
var lastChar = _entryName[_entryName.length - 1];
|
|
3010
|
+
_isDirectory = lastChar === 47 || lastChar === 92;
|
|
3011
|
+
_centralHeader.fileNameLength = _entryName.length;
|
|
3012
|
+
},
|
|
3013
|
+
|
|
3014
|
+
get efs() {
|
|
3015
|
+
if (typeof _efs === "function") {
|
|
3016
|
+
return _efs(this.entryName);
|
|
3017
|
+
} else {
|
|
3018
|
+
return _efs;
|
|
3019
|
+
}
|
|
3020
|
+
},
|
|
3021
|
+
|
|
3022
|
+
get extra() {
|
|
3023
|
+
return _extra;
|
|
3024
|
+
},
|
|
3025
|
+
set extra(val) {
|
|
3026
|
+
_extra = val;
|
|
3027
|
+
_centralHeader.extraLength = val.length;
|
|
3028
|
+
parseExtra(val);
|
|
3029
|
+
},
|
|
3030
|
+
|
|
3031
|
+
get comment() {
|
|
3032
|
+
return decoder.decode(_comment);
|
|
3033
|
+
},
|
|
3034
|
+
set comment(val) {
|
|
3035
|
+
_comment = Utils.toBuffer(val, decoder.encode);
|
|
3036
|
+
_centralHeader.commentLength = _comment.length;
|
|
3037
|
+
if (_comment.length > 0xffff) throw Utils.Errors.COMMENT_TOO_LONG();
|
|
3038
|
+
},
|
|
3039
|
+
|
|
3040
|
+
get name() {
|
|
3041
|
+
const n = decoder.decode(_entryName);
|
|
3042
|
+
// For directories the name is the last path segment; drop the trailing
|
|
3043
|
+
// separator first so "a/b/c/" yields "c" and not "" (issue #466).
|
|
3044
|
+
return _isDirectory
|
|
3045
|
+
? n
|
|
3046
|
+
.replace(/[/\\]$/, "")
|
|
3047
|
+
.split("/")
|
|
3048
|
+
.pop()
|
|
3049
|
+
: n.split("/").pop();
|
|
3050
|
+
},
|
|
3051
|
+
get isDirectory() {
|
|
3052
|
+
return _isDirectory;
|
|
3053
|
+
},
|
|
3054
|
+
|
|
3055
|
+
getCompressedData: function () {
|
|
3056
|
+
return compress(false, null);
|
|
3057
|
+
},
|
|
3058
|
+
|
|
3059
|
+
getCompressedDataAsync: function (/*Function*/ callback) {
|
|
3060
|
+
compress(true, callback);
|
|
3061
|
+
},
|
|
3062
|
+
|
|
3063
|
+
setData: function (value) {
|
|
3064
|
+
uncompressedData = Utils.toBuffer(value, Utils.decoder.encode);
|
|
3065
|
+
if (!_isDirectory && uncompressedData.length) {
|
|
3066
|
+
_centralHeader.size = uncompressedData.length;
|
|
3067
|
+
_centralHeader.method = Utils.Constants.DEFLATED;
|
|
3068
|
+
_centralHeader.crc = Utils.crc32(value);
|
|
3069
|
+
_centralHeader.changed = true;
|
|
3070
|
+
} else {
|
|
3071
|
+
// folders and blank files should be stored
|
|
3072
|
+
_centralHeader.method = Utils.Constants.STORED;
|
|
3073
|
+
}
|
|
3074
|
+
},
|
|
3075
|
+
|
|
3076
|
+
getData: function (pass) {
|
|
3077
|
+
if (_centralHeader.changed) {
|
|
3078
|
+
return uncompressedData;
|
|
3079
|
+
} else {
|
|
3080
|
+
return decompress(false, null, pass);
|
|
3081
|
+
}
|
|
3082
|
+
},
|
|
3083
|
+
|
|
3084
|
+
getDataAsync: function (/*Function*/ callback, pass) {
|
|
3085
|
+
if (_centralHeader.changed) {
|
|
3086
|
+
callback(uncompressedData);
|
|
3087
|
+
} else {
|
|
3088
|
+
decompress(true, callback, pass);
|
|
3089
|
+
}
|
|
3090
|
+
},
|
|
3091
|
+
|
|
3092
|
+
set attr(attr) {
|
|
3093
|
+
_centralHeader.attr = attr;
|
|
3094
|
+
},
|
|
3095
|
+
get attr() {
|
|
3096
|
+
return _centralHeader.attr;
|
|
3097
|
+
},
|
|
3098
|
+
|
|
3099
|
+
set header(/*Buffer*/ data) {
|
|
3100
|
+
_centralHeader.loadFromBinary(data);
|
|
3101
|
+
},
|
|
3102
|
+
|
|
3103
|
+
get header() {
|
|
3104
|
+
return _centralHeader;
|
|
3105
|
+
},
|
|
3106
|
+
|
|
3107
|
+
packCentralHeader: function () {
|
|
3108
|
+
_centralHeader.flags_efs = this.efs;
|
|
3109
|
+
_centralHeader.extraLength = _extra.length;
|
|
3110
|
+
// 1. create header (buffer)
|
|
3111
|
+
var header = _centralHeader.centralHeaderToBinary();
|
|
3112
|
+
var addpos = Utils.Constants.CENHDR;
|
|
3113
|
+
// 2. add file name
|
|
3114
|
+
_entryName.copy(header, addpos);
|
|
3115
|
+
addpos += _entryName.length;
|
|
3116
|
+
// 3. add extra data
|
|
3117
|
+
_extra.copy(header, addpos);
|
|
3118
|
+
addpos += _centralHeader.extraLength;
|
|
3119
|
+
// 4. add file comment
|
|
3120
|
+
_comment.copy(header, addpos);
|
|
3121
|
+
return header;
|
|
3122
|
+
},
|
|
3123
|
+
|
|
3124
|
+
packLocalHeader: function () {
|
|
3125
|
+
let addpos = 0;
|
|
3126
|
+
_centralHeader.flags_efs = this.efs;
|
|
3127
|
+
_centralHeader.extraLocalLength = _extralocal.length;
|
|
3128
|
+
// 1. construct local header Buffer
|
|
3129
|
+
const localHeaderBuf = _centralHeader.localHeaderToBinary();
|
|
3130
|
+
// 2. localHeader - crate header buffer
|
|
3131
|
+
const localHeader = Buffer.alloc(localHeaderBuf.length + _entryName.length + _centralHeader.extraLocalLength);
|
|
3132
|
+
// 2.1 add localheader
|
|
3133
|
+
localHeaderBuf.copy(localHeader, addpos);
|
|
3134
|
+
addpos += localHeaderBuf.length;
|
|
3135
|
+
// 2.2 add file name
|
|
3136
|
+
_entryName.copy(localHeader, addpos);
|
|
3137
|
+
addpos += _entryName.length;
|
|
3138
|
+
// 2.3 add extra field
|
|
3139
|
+
_extralocal.copy(localHeader, addpos);
|
|
3140
|
+
addpos += _extralocal.length;
|
|
3141
|
+
|
|
3142
|
+
return localHeader;
|
|
3143
|
+
},
|
|
3144
|
+
|
|
3145
|
+
toJSON: function () {
|
|
3146
|
+
const bytes = function (nr) {
|
|
3147
|
+
return "<" + ((nr && nr.length + " bytes buffer") || "null") + ">";
|
|
3148
|
+
};
|
|
3149
|
+
|
|
3150
|
+
return {
|
|
3151
|
+
entryName: this.entryName,
|
|
3152
|
+
name: this.name,
|
|
3153
|
+
comment: this.comment,
|
|
3154
|
+
isDirectory: this.isDirectory,
|
|
3155
|
+
header: _centralHeader.toJSON(),
|
|
3156
|
+
compressedData: bytes(input),
|
|
3157
|
+
data: bytes(uncompressedData)
|
|
3158
|
+
};
|
|
3159
|
+
},
|
|
3160
|
+
|
|
3161
|
+
toString: function () {
|
|
3162
|
+
return JSON.stringify(this.toJSON(), null, "\t");
|
|
3163
|
+
}
|
|
3164
|
+
};
|
|
3165
|
+
};
|
|
3166
|
+
|
|
3167
|
+
|
|
3168
|
+
/***/ },
|
|
3169
|
+
|
|
3170
|
+
/***/ 71312
|
|
3171
|
+
(module, __unused_webpack_exports, __webpack_require__) {
|
|
3172
|
+
|
|
3173
|
+
const ZipEntry = __webpack_require__(66488);
|
|
3174
|
+
const Headers = __webpack_require__(21204);
|
|
3175
|
+
const Utils = __webpack_require__(6942);
|
|
3176
|
+
|
|
3177
|
+
module.exports = function (/*Buffer|null*/ inBuffer, /** object */ options) {
|
|
3178
|
+
var entryList = [],
|
|
3179
|
+
// prototype-less: entry names come from untrusted input, so keys like
|
|
3180
|
+
// "__proto__" or "constructor" must be plain data, not touch the prototype
|
|
3181
|
+
// chain (which otherwise crashes addFile and hides such entries)
|
|
3182
|
+
entryTable = Object.create(null),
|
|
3183
|
+
_comment = Buffer.alloc(0),
|
|
3184
|
+
mainHeader = new Headers.MainHeader(),
|
|
3185
|
+
loadedEntries = false;
|
|
3186
|
+
var password = null;
|
|
3187
|
+
const temporary = new Set();
|
|
3188
|
+
|
|
3189
|
+
// assign options
|
|
3190
|
+
const opts = options;
|
|
3191
|
+
|
|
3192
|
+
const { noSort, decoder } = opts;
|
|
3193
|
+
|
|
3194
|
+
if (inBuffer) {
|
|
3195
|
+
// is a memory buffer
|
|
3196
|
+
readMainHeader(opts.readEntries);
|
|
3197
|
+
} else {
|
|
3198
|
+
// none. is a new file
|
|
3199
|
+
loadedEntries = true;
|
|
3200
|
+
}
|
|
3201
|
+
|
|
3202
|
+
function makeTemporaryFolders() {
|
|
3203
|
+
const foldersList = new Set();
|
|
3204
|
+
|
|
3205
|
+
// Make list of all folders in file
|
|
3206
|
+
for (const elem of Object.keys(entryTable)) {
|
|
3207
|
+
const elements = elem.split("/");
|
|
3208
|
+
elements.pop(); // filename
|
|
3209
|
+
if (!elements.length) continue; // no folders
|
|
3210
|
+
for (let i = 0; i < elements.length; i++) {
|
|
3211
|
+
const sub = elements.slice(0, i + 1).join("/") + "/";
|
|
3212
|
+
foldersList.add(sub);
|
|
3213
|
+
}
|
|
3214
|
+
}
|
|
3215
|
+
|
|
3216
|
+
// create missing folders as temporary
|
|
3217
|
+
for (const elem of foldersList) {
|
|
3218
|
+
if (!(elem in entryTable)) {
|
|
3219
|
+
const tempfolder = new ZipEntry(opts);
|
|
3220
|
+
tempfolder.entryName = elem;
|
|
3221
|
+
tempfolder.attr = 0x10;
|
|
3222
|
+
tempfolder.temporary = true;
|
|
3223
|
+
entryList.push(tempfolder);
|
|
3224
|
+
entryTable[tempfolder.entryName] = tempfolder;
|
|
3225
|
+
temporary.add(tempfolder);
|
|
3226
|
+
}
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
|
|
3230
|
+
function readEntries() {
|
|
3231
|
+
loadedEntries = true;
|
|
3232
|
+
entryTable = Object.create(null);
|
|
3233
|
+
if (mainHeader.diskEntries > (inBuffer.length - mainHeader.offset) / Utils.Constants.CENHDR) {
|
|
3234
|
+
throw Utils.Errors.DISK_ENTRY_TOO_LARGE();
|
|
3235
|
+
}
|
|
3236
|
+
entryList = new Array(mainHeader.diskEntries); // total number of entries
|
|
3237
|
+
var index = mainHeader.offset; // offset of first CEN header
|
|
3238
|
+
for (var i = 0; i < entryList.length; i++) {
|
|
3239
|
+
var tmp = index,
|
|
3240
|
+
entry = new ZipEntry(opts, inBuffer);
|
|
3241
|
+
entry.header = inBuffer.slice(tmp, (tmp += Utils.Constants.CENHDR));
|
|
3242
|
+
|
|
3243
|
+
entry.entryName = inBuffer.slice(tmp, (tmp += entry.header.fileNameLength));
|
|
3244
|
+
|
|
3245
|
+
if (entry.header.extraLength) {
|
|
3246
|
+
entry.extra = inBuffer.slice(tmp, (tmp += entry.header.extraLength));
|
|
3247
|
+
}
|
|
3248
|
+
|
|
3249
|
+
if (entry.header.commentLength) entry.comment = inBuffer.slice(tmp, tmp + entry.header.commentLength);
|
|
3250
|
+
|
|
3251
|
+
index += entry.header.centralHeaderSize;
|
|
3252
|
+
|
|
3253
|
+
// Reject archives that declare the same entry name twice. adm-zip keeps
|
|
3254
|
+
// every entry in entryList but only the last in entryTable, so getEntry()
|
|
3255
|
+
// (table) and extractAllTo() (list) could resolve one name to different
|
|
3256
|
+
// content: an app that validates entry bytes via getEntry() before
|
|
3257
|
+
// extracting could approve one file while a different one lands on disk
|
|
3258
|
+
// (GHSA-p634-w6r4-rjp2). Fail closed on the ambiguity.
|
|
3259
|
+
if (entry.entryName in entryTable) {
|
|
3260
|
+
throw Utils.Errors.DUPLICATE_ENTRY(`"${entry.entryName}"`);
|
|
3261
|
+
}
|
|
3262
|
+
|
|
3263
|
+
entryList[i] = entry;
|
|
3264
|
+
entryTable[entry.entryName] = entry;
|
|
3265
|
+
}
|
|
3266
|
+
temporary.clear();
|
|
3267
|
+
makeTemporaryFolders();
|
|
3268
|
+
}
|
|
3269
|
+
|
|
3270
|
+
function readMainHeader(/*Boolean*/ readNow) {
|
|
3271
|
+
var i = inBuffer.length - Utils.Constants.ENDHDR, // END header size
|
|
3272
|
+
max = Math.max(0, i - 0xffff), // 0xFFFF is the max zip file comment length
|
|
3273
|
+
n = max,
|
|
3274
|
+
endStart = inBuffer.length,
|
|
3275
|
+
endOffset = -1, // Start offset of the END header
|
|
3276
|
+
commentEnd = 0;
|
|
3277
|
+
|
|
3278
|
+
// option to search header form entire file
|
|
3279
|
+
const trailingSpace = typeof opts.trailingSpace === "boolean" ? opts.trailingSpace : false;
|
|
3280
|
+
if (trailingSpace) max = 0;
|
|
3281
|
+
|
|
3282
|
+
for (i; i >= n; i--) {
|
|
3283
|
+
if (inBuffer[i] !== 0x50) continue; // quick check that the byte is 'P'
|
|
3284
|
+
if (inBuffer.readUInt32LE(i) === Utils.Constants.ENDSIG) {
|
|
3285
|
+
// "PK\005\006"
|
|
3286
|
+
endOffset = i;
|
|
3287
|
+
commentEnd = i;
|
|
3288
|
+
endStart = i + Utils.Constants.ENDHDR;
|
|
3289
|
+
// We already found a regular signature, let's look just a bit further to check if there's any zip64 signature
|
|
3290
|
+
n = i - Utils.Constants.END64HDR;
|
|
3291
|
+
continue;
|
|
3292
|
+
}
|
|
3293
|
+
|
|
3294
|
+
if (inBuffer.readUInt32LE(i) === Utils.Constants.END64SIG) {
|
|
3295
|
+
// Found a zip64 signature, let's continue reading the whole zip64 record
|
|
3296
|
+
n = max;
|
|
3297
|
+
continue;
|
|
3298
|
+
}
|
|
3299
|
+
|
|
3300
|
+
if (inBuffer.readUInt32LE(i) === Utils.Constants.ZIP64SIG) {
|
|
3301
|
+
// Found the zip64 record, let's determine it's size
|
|
3302
|
+
endOffset = i;
|
|
3303
|
+
endStart = i + Utils.readBigUInt64LE(inBuffer, i + Utils.Constants.ZIP64SIZE) + Utils.Constants.ZIP64LEAD;
|
|
3304
|
+
break;
|
|
3305
|
+
}
|
|
3306
|
+
}
|
|
3307
|
+
|
|
3308
|
+
if (endOffset == -1) throw Utils.Errors.INVALID_FORMAT();
|
|
3309
|
+
|
|
3310
|
+
mainHeader.loadFromBinary(inBuffer.slice(endOffset, endStart));
|
|
3311
|
+
if (mainHeader.commentLength) {
|
|
3312
|
+
_comment = inBuffer.slice(commentEnd + Utils.Constants.ENDHDR);
|
|
3313
|
+
}
|
|
3314
|
+
if (readNow) readEntries();
|
|
3315
|
+
}
|
|
3316
|
+
|
|
3317
|
+
function sortEntries() {
|
|
3318
|
+
if (entryList.length > 1 && !noSort) {
|
|
3319
|
+
// Decode + lowercase each name once rather than on every comparison:
|
|
3320
|
+
// the entryName getter re-decodes the underlying buffer on each access,
|
|
3321
|
+
// so the previous inline comparator did O(n log n) redundant decoding.
|
|
3322
|
+
// Ordering is unchanged (same localeCompare on the same keys).
|
|
3323
|
+
entryList = entryList
|
|
3324
|
+
.map((entry) => ({ entry, key: entry.entryName.toLowerCase() }))
|
|
3325
|
+
.sort((a, b) => a.key.localeCompare(b.key))
|
|
3326
|
+
.map((pair) => pair.entry);
|
|
3327
|
+
}
|
|
3328
|
+
}
|
|
3329
|
+
|
|
3330
|
+
return {
|
|
3331
|
+
/**
|
|
3332
|
+
* Returns an array of ZipEntry objects existent in the current opened archive
|
|
3333
|
+
* @return Array
|
|
3334
|
+
*/
|
|
3335
|
+
get entries() {
|
|
3336
|
+
if (!loadedEntries) {
|
|
3337
|
+
readEntries();
|
|
3338
|
+
}
|
|
3339
|
+
return entryList.filter((e) => !temporary.has(e));
|
|
3340
|
+
},
|
|
3341
|
+
|
|
3342
|
+
/**
|
|
3343
|
+
* Archive comment
|
|
3344
|
+
* @return {String}
|
|
3345
|
+
*/
|
|
3346
|
+
get comment() {
|
|
3347
|
+
return decoder.decode(_comment);
|
|
3348
|
+
},
|
|
3349
|
+
set comment(val) {
|
|
3350
|
+
_comment = Utils.toBuffer(val, decoder.encode);
|
|
3351
|
+
mainHeader.commentLength = _comment.length;
|
|
3352
|
+
},
|
|
3353
|
+
|
|
3354
|
+
getEntryCount: function () {
|
|
3355
|
+
if (!loadedEntries) {
|
|
3356
|
+
return mainHeader.diskEntries;
|
|
3357
|
+
}
|
|
3358
|
+
|
|
3359
|
+
return entryList.length;
|
|
3360
|
+
},
|
|
3361
|
+
|
|
3362
|
+
forEach: function (callback) {
|
|
3363
|
+
this.entries.forEach(callback);
|
|
3364
|
+
},
|
|
3365
|
+
|
|
3366
|
+
/**
|
|
3367
|
+
* Returns a reference to the entry with the given name or null if entry is inexistent
|
|
3368
|
+
*
|
|
3369
|
+
* @param entryName
|
|
3370
|
+
* @return ZipEntry
|
|
3371
|
+
*/
|
|
3372
|
+
getEntry: function (/*String*/ entryName) {
|
|
3373
|
+
if (!loadedEntries) {
|
|
3374
|
+
readEntries();
|
|
3375
|
+
}
|
|
3376
|
+
return entryTable[entryName] || null;
|
|
3377
|
+
},
|
|
3378
|
+
|
|
3379
|
+
/**
|
|
3380
|
+
* Adds the given entry to the entry list
|
|
3381
|
+
*
|
|
3382
|
+
* @param entry
|
|
3383
|
+
*/
|
|
3384
|
+
setEntry: function (/*ZipEntry*/ entry) {
|
|
3385
|
+
if (!loadedEntries) {
|
|
3386
|
+
readEntries();
|
|
3387
|
+
}
|
|
3388
|
+
entryList.push(entry);
|
|
3389
|
+
entryTable[entry.entryName] = entry;
|
|
3390
|
+
mainHeader.totalEntries = entryList.length;
|
|
3391
|
+
},
|
|
3392
|
+
|
|
3393
|
+
/**
|
|
3394
|
+
* Removes the file with the given name from the entry list.
|
|
3395
|
+
*
|
|
3396
|
+
* If the entry is a directory, then all nested files and directories will be removed
|
|
3397
|
+
* @param entryName
|
|
3398
|
+
* @returns {void}
|
|
3399
|
+
*/
|
|
3400
|
+
deleteFile: function (/*String*/ entryName, withsubfolders = true) {
|
|
3401
|
+
if (!loadedEntries) {
|
|
3402
|
+
readEntries();
|
|
3403
|
+
}
|
|
3404
|
+
const entry = entryTable[entryName];
|
|
3405
|
+
const list = this.getEntryChildren(entry, withsubfolders).map((child) => child.entryName);
|
|
3406
|
+
|
|
3407
|
+
list.forEach(this.deleteEntry);
|
|
3408
|
+
},
|
|
3409
|
+
|
|
3410
|
+
/**
|
|
3411
|
+
* Removes the entry with the given name from the entry list.
|
|
3412
|
+
*
|
|
3413
|
+
* @param {string} entryName
|
|
3414
|
+
* @returns {void}
|
|
3415
|
+
*/
|
|
3416
|
+
deleteEntry: function (/*String*/ entryName) {
|
|
3417
|
+
if (!loadedEntries) {
|
|
3418
|
+
readEntries();
|
|
3419
|
+
}
|
|
3420
|
+
const entry = entryTable[entryName];
|
|
3421
|
+
const index = entryList.indexOf(entry);
|
|
3422
|
+
if (index >= 0) {
|
|
3423
|
+
entryList.splice(index, 1);
|
|
3424
|
+
delete entryTable[entryName];
|
|
3425
|
+
mainHeader.totalEntries = entryList.length;
|
|
3426
|
+
}
|
|
3427
|
+
},
|
|
3428
|
+
|
|
3429
|
+
/**
|
|
3430
|
+
* Iterates and returns all nested files and directories of the given entry
|
|
3431
|
+
*
|
|
3432
|
+
* @param entry
|
|
3433
|
+
* @return Array
|
|
3434
|
+
*/
|
|
3435
|
+
getEntryChildren: function (/*ZipEntry*/ entry, subfolders = true) {
|
|
3436
|
+
if (!loadedEntries) {
|
|
3437
|
+
readEntries();
|
|
3438
|
+
}
|
|
3439
|
+
if (typeof entry === "object") {
|
|
3440
|
+
if (entry.isDirectory && subfolders) {
|
|
3441
|
+
const list = [];
|
|
3442
|
+
const name = entry.entryName;
|
|
3443
|
+
|
|
3444
|
+
for (const zipEntry of entryList) {
|
|
3445
|
+
if (zipEntry.entryName.startsWith(name)) {
|
|
3446
|
+
list.push(zipEntry);
|
|
3447
|
+
}
|
|
3448
|
+
}
|
|
3449
|
+
return list;
|
|
3450
|
+
} else {
|
|
3451
|
+
return [entry];
|
|
3452
|
+
}
|
|
3453
|
+
}
|
|
3454
|
+
return [];
|
|
3455
|
+
},
|
|
3456
|
+
|
|
3457
|
+
/**
|
|
3458
|
+
* How many child elements entry has
|
|
3459
|
+
*
|
|
3460
|
+
* @param {ZipEntry} entry
|
|
3461
|
+
* @return {integer}
|
|
3462
|
+
*/
|
|
3463
|
+
getChildCount: function (entry) {
|
|
3464
|
+
if (entry && entry.isDirectory) {
|
|
3465
|
+
const list = this.getEntryChildren(entry);
|
|
3466
|
+
return list.includes(entry) ? list.length - 1 : list.length;
|
|
3467
|
+
}
|
|
3468
|
+
return 0;
|
|
3469
|
+
},
|
|
3470
|
+
|
|
3471
|
+
/**
|
|
3472
|
+
* Returns the zip file
|
|
3473
|
+
*
|
|
3474
|
+
* @return Buffer
|
|
3475
|
+
*/
|
|
3476
|
+
compressToBuffer: function () {
|
|
3477
|
+
if (!loadedEntries) {
|
|
3478
|
+
readEntries();
|
|
3479
|
+
}
|
|
3480
|
+
sortEntries();
|
|
3481
|
+
|
|
3482
|
+
const dataBlock = [];
|
|
3483
|
+
const headerBlocks = [];
|
|
3484
|
+
let totalSize = 0;
|
|
3485
|
+
let dindex = 0;
|
|
3486
|
+
|
|
3487
|
+
mainHeader.size = 0;
|
|
3488
|
+
mainHeader.offset = 0;
|
|
3489
|
+
let totalEntries = 0;
|
|
3490
|
+
|
|
3491
|
+
for (const entry of this.entries) {
|
|
3492
|
+
// compress data and set local and entry header accordingly. Reason why is called first
|
|
3493
|
+
const compressedData = entry.getCompressedData();
|
|
3494
|
+
entry.header.offset = dindex;
|
|
3495
|
+
|
|
3496
|
+
// 1. construct local header
|
|
3497
|
+
const localHeader = entry.packLocalHeader();
|
|
3498
|
+
|
|
3499
|
+
// 2. offsets
|
|
3500
|
+
const dataLength = localHeader.length + compressedData.length;
|
|
3501
|
+
dindex += dataLength;
|
|
3502
|
+
|
|
3503
|
+
// 3. store values in sequence
|
|
3504
|
+
dataBlock.push(localHeader);
|
|
3505
|
+
dataBlock.push(compressedData);
|
|
3506
|
+
|
|
3507
|
+
// 4. construct central header
|
|
3508
|
+
const centralHeader = entry.packCentralHeader();
|
|
3509
|
+
headerBlocks.push(centralHeader);
|
|
3510
|
+
// 5. update main header
|
|
3511
|
+
mainHeader.size += centralHeader.length;
|
|
3512
|
+
totalSize += dataLength + centralHeader.length;
|
|
3513
|
+
totalEntries++;
|
|
3514
|
+
}
|
|
3515
|
+
|
|
3516
|
+
totalSize += mainHeader.mainHeaderSize; // also includes zip file comment length
|
|
3517
|
+
// point to end of data and beginning of central directory first record
|
|
3518
|
+
mainHeader.offset = dindex;
|
|
3519
|
+
mainHeader.totalEntries = totalEntries;
|
|
3520
|
+
|
|
3521
|
+
dindex = 0;
|
|
3522
|
+
const outBuffer = Buffer.alloc(totalSize);
|
|
3523
|
+
// write data blocks
|
|
3524
|
+
for (const content of dataBlock) {
|
|
3525
|
+
content.copy(outBuffer, dindex);
|
|
3526
|
+
dindex += content.length;
|
|
3527
|
+
}
|
|
3528
|
+
|
|
3529
|
+
// write central directory entries
|
|
3530
|
+
for (const content of headerBlocks) {
|
|
3531
|
+
content.copy(outBuffer, dindex);
|
|
3532
|
+
dindex += content.length;
|
|
3533
|
+
}
|
|
3534
|
+
|
|
3535
|
+
// write main header
|
|
3536
|
+
const mh = mainHeader.toBinary();
|
|
3537
|
+
if (_comment) {
|
|
3538
|
+
_comment.copy(mh, mh.length - _comment.length); // add zip file comment
|
|
3539
|
+
}
|
|
3540
|
+
mh.copy(outBuffer, dindex);
|
|
3541
|
+
|
|
3542
|
+
// Since we update entry and main header offsets,
|
|
3543
|
+
// they are no longer valid and we have to reset content
|
|
3544
|
+
// (Issue 64)
|
|
3545
|
+
|
|
3546
|
+
inBuffer = outBuffer;
|
|
3547
|
+
loadedEntries = false;
|
|
3548
|
+
|
|
3549
|
+
return outBuffer;
|
|
3550
|
+
},
|
|
3551
|
+
|
|
3552
|
+
toAsyncBuffer: function (/*Function*/ onSuccess, /*Function*/ onFail, /*Function*/ onItemStart, /*Function*/ onItemEnd) {
|
|
3553
|
+
try {
|
|
3554
|
+
if (!loadedEntries) {
|
|
3555
|
+
readEntries();
|
|
3556
|
+
}
|
|
3557
|
+
sortEntries();
|
|
3558
|
+
|
|
3559
|
+
const dataBlock = [];
|
|
3560
|
+
const centralHeaders = [];
|
|
3561
|
+
let totalSize = 0;
|
|
3562
|
+
let dindex = 0;
|
|
3563
|
+
let totalEntries = 0;
|
|
3564
|
+
|
|
3565
|
+
mainHeader.size = 0;
|
|
3566
|
+
mainHeader.offset = 0;
|
|
3567
|
+
|
|
3568
|
+
const compress2Buffer = function (entryLists) {
|
|
3569
|
+
if (entryLists.length > 0) {
|
|
3570
|
+
const entry = entryLists.shift();
|
|
3571
|
+
const name = entry.entryName + entry.extra.toString();
|
|
3572
|
+
if (onItemStart) onItemStart(name);
|
|
3573
|
+
entry.getCompressedDataAsync(function (compressedData) {
|
|
3574
|
+
if (onItemEnd) onItemEnd(name);
|
|
3575
|
+
entry.header.offset = dindex;
|
|
3576
|
+
|
|
3577
|
+
// 1. construct local header
|
|
3578
|
+
const localHeader = entry.packLocalHeader();
|
|
3579
|
+
|
|
3580
|
+
// 2. offsets
|
|
3581
|
+
const dataLength = localHeader.length + compressedData.length;
|
|
3582
|
+
dindex += dataLength;
|
|
3583
|
+
|
|
3584
|
+
// 3. store values in sequence
|
|
3585
|
+
dataBlock.push(localHeader);
|
|
3586
|
+
dataBlock.push(compressedData);
|
|
3587
|
+
|
|
3588
|
+
// central header
|
|
3589
|
+
const centalHeader = entry.packCentralHeader();
|
|
3590
|
+
centralHeaders.push(centalHeader);
|
|
3591
|
+
mainHeader.size += centalHeader.length;
|
|
3592
|
+
totalSize += dataLength + centalHeader.length;
|
|
3593
|
+
totalEntries++;
|
|
3594
|
+
|
|
3595
|
+
compress2Buffer(entryLists);
|
|
3596
|
+
});
|
|
3597
|
+
} else {
|
|
3598
|
+
totalSize += mainHeader.mainHeaderSize; // also includes zip file comment length
|
|
3599
|
+
// point to end of data and beginning of central directory first record
|
|
3600
|
+
mainHeader.offset = dindex;
|
|
3601
|
+
mainHeader.totalEntries = totalEntries;
|
|
3602
|
+
|
|
3603
|
+
dindex = 0;
|
|
3604
|
+
const outBuffer = Buffer.alloc(totalSize);
|
|
3605
|
+
dataBlock.forEach(function (content) {
|
|
3606
|
+
content.copy(outBuffer, dindex); // write data blocks
|
|
3607
|
+
dindex += content.length;
|
|
3608
|
+
});
|
|
3609
|
+
centralHeaders.forEach(function (content) {
|
|
3610
|
+
content.copy(outBuffer, dindex); // write central directory entries
|
|
3611
|
+
dindex += content.length;
|
|
3612
|
+
});
|
|
3613
|
+
|
|
3614
|
+
const mh = mainHeader.toBinary();
|
|
3615
|
+
if (_comment) {
|
|
3616
|
+
_comment.copy(mh, mh.length - _comment.length); // add zip file comment
|
|
3617
|
+
}
|
|
3618
|
+
|
|
3619
|
+
mh.copy(outBuffer, dindex); // write main header
|
|
3620
|
+
|
|
3621
|
+
// Since we update entry and main header offsets, they are no
|
|
3622
|
+
// longer valid and we have to reset content using our new buffer
|
|
3623
|
+
// (Issue 64)
|
|
3624
|
+
|
|
3625
|
+
inBuffer = outBuffer;
|
|
3626
|
+
loadedEntries = false;
|
|
3627
|
+
|
|
3628
|
+
onSuccess(outBuffer);
|
|
3629
|
+
}
|
|
3630
|
+
};
|
|
3631
|
+
|
|
3632
|
+
compress2Buffer(Array.from(this.entries));
|
|
3633
|
+
} catch (e) {
|
|
3634
|
+
onFail(e);
|
|
3635
|
+
}
|
|
3636
|
+
}
|
|
3637
|
+
};
|
|
3638
|
+
};
|
|
3639
|
+
|
|
3640
|
+
|
|
3641
|
+
/***/ }
|
|
3642
|
+
|
|
3643
|
+
};
|