@zip.js/zip.js 2.6.32 → 2.6.34
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/z-worker-fflate.js +1 -1
- package/dist/z-worker-pako.js +1 -1
- package/dist/z-worker.js +1 -1
- package/dist/zip-fs-full.js +2441 -2287
- package/dist/zip-fs-full.min.js +1 -1
- package/dist/zip-fs.js +1970 -1816
- package/dist/zip-fs.min.js +1 -1
- package/dist/zip-full.js +1969 -1815
- package/dist/zip-full.min.js +1 -1
- package/dist/zip-no-worker-deflate.min.js +1 -1
- package/dist/zip-no-worker-inflate.min.js +1 -1
- package/dist/zip-no-worker.min.js +1 -1
- package/dist/zip.js +1971 -1817
- package/dist/zip.min.js +1 -1
- package/index-fflate.js +3 -1
- package/index.cjs +2441 -2287
- package/index.d.ts +36 -7
- package/index.js +3 -1
- package/lib/core/codec-pool.js +6 -5
- package/lib/core/codec-worker.js +321 -0
- package/lib/core/configuration.js +11 -8
- package/lib/core/constants.js +11 -5
- package/lib/core/io.js +150 -51
- package/lib/core/streams/aes-crypto-stream.js +2 -0
- package/lib/core/streams/codec-stream.js +60 -13
- package/lib/core/streams/codecs/sjcl.js +1 -1
- package/lib/core/streams/stream-adapter.js +55 -0
- package/lib/core/streams/zip-crypto-stream.js +1 -0
- package/lib/core/streams/zip-entry-stream.js +1 -0
- package/lib/core/util/stream-codec-shim.js +2 -0
- package/lib/core/z-worker-core.js +23 -12
- package/lib/core/zip-entry.js +2 -1
- package/lib/core/zip-fs-core.js +1 -0
- package/lib/core/zip-reader.js +16 -12
- package/lib/core/zip-writer.js +102 -54
- package/lib/z-worker-inline.js +1 -1
- package/lib/zip.js +1 -1
- package/package.json +5 -5
- package/rollup-es5.config.dev.js +199 -0
- package/rollup-es5.config.js +243 -0
- package/tests/all/test-http-split-zip.js +37 -0
- package/tests/all/test-readable-stream.js +1 -2
- package/tests/all/test-readable-zip-stream.js +1 -1
- package/tests/all/test-split-zip.js +25 -22
- package/tests/tests-data.js +2 -1
- package/lib/core/codec-interface.js +0 -115
- package/lib/core/codec-pool-worker.js +0 -289
package/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ declare global {
|
|
|
7
7
|
*
|
|
8
8
|
* @see {@link https://wicg.github.io/entries-api/#api-entry|specification}
|
|
9
9
|
*/
|
|
10
|
+
// deno-lint-ignore no-empty-interface
|
|
10
11
|
interface FileSystemEntry { }
|
|
11
12
|
}
|
|
12
13
|
|
|
@@ -141,6 +142,7 @@ interface WorkerConfiguration {
|
|
|
141
142
|
* @param registerDataHandler The function called to handle the `data` events triggered by a third-party codec implementation.
|
|
142
143
|
* @returns An instance containing classes compatible with `ZipDeflate` and `ZipInflate`.
|
|
143
144
|
*/
|
|
145
|
+
// deno-lint-ignore no-explicit-any
|
|
144
146
|
export function initShimAsyncCodec(library: EventBasedZipLibrary, constructorOptions: any | null, registerDataHandler: registerDataHandler): ZipLibrary
|
|
145
147
|
|
|
146
148
|
/**
|
|
@@ -273,7 +275,6 @@ export function getMimeType(fileExtension: string): string
|
|
|
273
275
|
interface DataProcessor {
|
|
274
276
|
/**
|
|
275
277
|
* Initializes the instance asynchronously
|
|
276
|
-
*
|
|
277
278
|
*/
|
|
278
279
|
init?(): Promise<void>
|
|
279
280
|
}
|
|
@@ -295,17 +296,17 @@ interface ReadableReader {
|
|
|
295
296
|
* Here is an example of custom `Reader` class used to read binary strings:
|
|
296
297
|
* ```
|
|
297
298
|
* class BinaryStringReader extends Reader {
|
|
298
|
-
*
|
|
299
|
+
*
|
|
299
300
|
* constructor(binaryString) {
|
|
300
301
|
* super();
|
|
301
302
|
* this.binaryString = binaryString;
|
|
302
303
|
* }
|
|
303
|
-
*
|
|
304
|
+
*
|
|
304
305
|
* init() {
|
|
305
306
|
* super.init();
|
|
306
307
|
* this.size = this.binaryString.length;
|
|
307
308
|
* }
|
|
308
|
-
*
|
|
309
|
+
*
|
|
309
310
|
* readUint8Array(offset, length) {
|
|
310
311
|
* const result = new Uint8Array(length);
|
|
311
312
|
* for (let indexCharacter = 0; indexCharacter < length; indexCharacter++) {
|
|
@@ -333,7 +334,6 @@ export class Reader<Type> implements ReadableReader, DataProcessor {
|
|
|
333
334
|
size: number
|
|
334
335
|
/**
|
|
335
336
|
* Initializes the instance asynchronously
|
|
336
|
-
*
|
|
337
337
|
*/
|
|
338
338
|
init?(): Promise<void>
|
|
339
339
|
/**
|
|
@@ -369,11 +369,13 @@ export class Uint8ArrayReader extends Reader<Uint8Array> { }
|
|
|
369
369
|
/**
|
|
370
370
|
* Represents a `Reader` instance used to read data provided as an array of `Reader` instances (i.e. split zip files).
|
|
371
371
|
*/
|
|
372
|
+
// deno-lint-ignore no-explicit-any
|
|
372
373
|
export class SplitZipReader extends Reader<Reader<any>[] | ReadableReader[] | ReadableStream[]> { }
|
|
373
374
|
|
|
374
375
|
/**
|
|
375
376
|
* Represents a URL stored into a `string`.
|
|
376
377
|
*/
|
|
378
|
+
// deno-lint-ignore no-empty-interface
|
|
377
379
|
interface URLString extends String { }
|
|
378
380
|
|
|
379
381
|
/**
|
|
@@ -540,6 +542,27 @@ export class Data64URIWriter extends Writer<string> {
|
|
|
540
542
|
constructor(mimeString?: string)
|
|
541
543
|
}
|
|
542
544
|
|
|
545
|
+
/**
|
|
546
|
+
* Represents a `Writer` instance used to retrieve the written data from a generator of `Writer` instances (i.e. split zip files).
|
|
547
|
+
*/
|
|
548
|
+
export class SplitZipWriter implements DataProcessor, WritableWriter {
|
|
549
|
+
/**
|
|
550
|
+
* The `WritableStream` instance.
|
|
551
|
+
*/
|
|
552
|
+
writable: WritableStream
|
|
553
|
+
/**
|
|
554
|
+
* Initializes the instance asynchronously
|
|
555
|
+
*/
|
|
556
|
+
init(): Promise<void>
|
|
557
|
+
/**
|
|
558
|
+
* Creates the `SplitZipWriter` instance
|
|
559
|
+
*
|
|
560
|
+
* @param writerGenerator The MIME type of the content.
|
|
561
|
+
* @param maxSize The maximum size of the data written into `Writer` instances.
|
|
562
|
+
*/
|
|
563
|
+
constructor(writerGenerator: AsyncGenerator<Writer<any> | WritableWriter | WritableStream, boolean>, maxSize: number)
|
|
564
|
+
}
|
|
565
|
+
|
|
543
566
|
/**
|
|
544
567
|
* Represents a `Writer` instance used to retrieve the written data as a `Uint8Array` instance.
|
|
545
568
|
*/
|
|
@@ -584,6 +607,7 @@ export class ZipReader<Type> {
|
|
|
584
607
|
* @param reader The `Reader` instance used to read data.
|
|
585
608
|
* @param options The options.
|
|
586
609
|
*/
|
|
610
|
+
// deno-lint-ignore no-explicit-any
|
|
587
611
|
constructor(reader: Reader<Type> | ReadableReader | ReadableStream | Reader<any>[] | ReadableReader[] | ReadableStream[], options?: ZipReaderConstructorOptions)
|
|
588
612
|
/**
|
|
589
613
|
* The global comment of the zip file.
|
|
@@ -794,6 +818,10 @@ export interface Entry {
|
|
|
794
818
|
* The external file attribute (raw).
|
|
795
819
|
*/
|
|
796
820
|
externalFileAttribute: number
|
|
821
|
+
/**
|
|
822
|
+
* The number of the disk where the entry data starts.
|
|
823
|
+
*/
|
|
824
|
+
diskNumberStart: number
|
|
797
825
|
/**
|
|
798
826
|
* Returns the content of the entry
|
|
799
827
|
*
|
|
@@ -836,7 +864,7 @@ export class ZipWriter<Type> {
|
|
|
836
864
|
* @param writer The `Writer` instance where the zip content will be written.
|
|
837
865
|
* @param options The options.
|
|
838
866
|
*/
|
|
839
|
-
constructor(writer: Writer<Type> | WritableWriter | WritableStream, options?: ZipWriterConstructorOptions)
|
|
867
|
+
constructor(writer: Writer<Type> | WritableWriter | WritableStream | SplitZipWriter | AsyncGenerator<Writer<any> | WritableWriter | WritableStream, boolean>, options?: ZipWriterConstructorOptions)
|
|
840
868
|
/**
|
|
841
869
|
* `true` if the zip contains at least one entry that has been partially written.
|
|
842
870
|
*/
|
|
@@ -1446,4 +1474,5 @@ export const ERR_INVALID_EXTRAFIELD_TYPE: string
|
|
|
1446
1474
|
export const ERR_INVALID_EXTRAFIELD_DATA: string
|
|
1447
1475
|
export const ERR_INVALID_ENCRYPTION_STRENGTH: string
|
|
1448
1476
|
export const ERR_UNSUPPORTED_FORMAT: string
|
|
1449
|
-
export const ERR_SPLIT_ZIP_FILE: string
|
|
1477
|
+
export const ERR_SPLIT_ZIP_FILE: string
|
|
1478
|
+
export const ERR_ITERATOR_COMPLETED_TOO_SOON: string
|
package/index.js
CHANGED
|
@@ -55,6 +55,7 @@ export {
|
|
|
55
55
|
Uint8ArrayWriter,
|
|
56
56
|
Uint8ArrayReader,
|
|
57
57
|
SplitZipReader,
|
|
58
|
+
SplitZipWriter,
|
|
58
59
|
ERR_HTTP_RANGE,
|
|
59
60
|
ERR_BAD_FORMAT,
|
|
60
61
|
ERR_EOCDR_NOT_FOUND,
|
|
@@ -77,6 +78,7 @@ export {
|
|
|
77
78
|
ERR_INVALID_EXTRAFIELD_DATA,
|
|
78
79
|
ERR_INVALID_ENCRYPTION_STRENGTH,
|
|
79
80
|
ERR_UNSUPPORTED_FORMAT,
|
|
80
|
-
ERR_SPLIT_ZIP_FILE
|
|
81
|
+
ERR_SPLIT_ZIP_FILE,
|
|
82
|
+
ERR_ITERATOR_COMPLETED_TOO_SOON
|
|
81
83
|
} from "./lib/zip-fs.js";
|
|
82
84
|
export { getMimeType, terminateWorkers };
|
package/lib/core/codec-pool.js
CHANGED
|
@@ -28,13 +28,14 @@
|
|
|
28
28
|
|
|
29
29
|
/* global setTimeout, clearTimeout */
|
|
30
30
|
|
|
31
|
+
import { UNDEFINED_VALUE } from "./constants.js";
|
|
31
32
|
import {
|
|
32
33
|
CODEC_INFLATE,
|
|
33
34
|
CODEC_DEFLATE,
|
|
34
35
|
ERR_INVALID_SIGNATURE,
|
|
35
36
|
ERR_INVALID_PASSWORD
|
|
36
|
-
} from "./codec-
|
|
37
|
-
import { CodecWorker } from "./codec-
|
|
37
|
+
} from "./streams/codec-stream.js";
|
|
38
|
+
import { CodecWorker } from "./codec-worker.js";
|
|
38
39
|
|
|
39
40
|
let pool = [];
|
|
40
41
|
const pendingRequests = [];
|
|
@@ -54,11 +55,11 @@ async function runWorker(stream, workerOptions) {
|
|
|
54
55
|
const { options, config } = workerOptions;
|
|
55
56
|
const { transferStreams, useWebWorkers, useCompressionStream, codecType, compressed, signed, encrypted } = options;
|
|
56
57
|
const { workerScripts } = config;
|
|
57
|
-
workerOptions.transferStreams = transferStreams || transferStreams ===
|
|
58
|
+
workerOptions.transferStreams = transferStreams || transferStreams === UNDEFINED_VALUE;
|
|
58
59
|
const streamCopy = !compressed && !signed && !encrypted && !workerOptions.transferStreams;
|
|
59
|
-
workerOptions.useWebWorkers = !streamCopy && (useWebWorkers || (useWebWorkers ===
|
|
60
|
+
workerOptions.useWebWorkers = !streamCopy && (useWebWorkers || (useWebWorkers === UNDEFINED_VALUE && config.useWebWorkers));
|
|
60
61
|
workerOptions.scripts = workerOptions.useWebWorkers && workerScripts ? workerScripts[codecType] : [];
|
|
61
|
-
options.useCompressionStream = useCompressionStream || (useCompressionStream ===
|
|
62
|
+
options.useCompressionStream = useCompressionStream || (useCompressionStream === UNDEFINED_VALUE && config.useCompressionStream);
|
|
62
63
|
let worker;
|
|
63
64
|
if (pool.length < config.maxWorkers) {
|
|
64
65
|
const workerData = { indexWorker };
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright (c) 2022 Gildas Lormeau. All rights reserved.
|
|
3
|
+
|
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
|
6
|
+
|
|
7
|
+
1. Redistributions of source code must retain the above copyright notice,
|
|
8
|
+
this list of conditions and the following disclaimer.
|
|
9
|
+
|
|
10
|
+
2. Redistributions in binary form must reproduce the above copyright
|
|
11
|
+
notice, this list of conditions and the following disclaimer in
|
|
12
|
+
the documentation and/or other materials provided with the distribution.
|
|
13
|
+
|
|
14
|
+
3. The names of the authors may not be used to endorse or promote products
|
|
15
|
+
derived from this software without specific prior written permission.
|
|
16
|
+
|
|
17
|
+
THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
|
|
18
|
+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT,
|
|
20
|
+
INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
21
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
22
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
|
23
|
+
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
24
|
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
25
|
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
26
|
+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/* global Worker, URL, TransformStream, WritableStream */
|
|
30
|
+
|
|
31
|
+
import {
|
|
32
|
+
UNDEFINED_TYPE,
|
|
33
|
+
FUNCTION_TYPE
|
|
34
|
+
} from "./constants.js";
|
|
35
|
+
import {
|
|
36
|
+
CodecStream,
|
|
37
|
+
MESSAGE_EVENT_TYPE,
|
|
38
|
+
MESSAGE_START,
|
|
39
|
+
MESSAGE_PULL,
|
|
40
|
+
MESSAGE_DATA,
|
|
41
|
+
MESSAGE_ACK_DATA,
|
|
42
|
+
MESSAGE_CLOSE
|
|
43
|
+
} from "./streams/codec-stream.js";
|
|
44
|
+
|
|
45
|
+
// deno-lint-ignore valid-typeof
|
|
46
|
+
const WEB_WORKERS_SUPPORTED = typeof Worker != UNDEFINED_TYPE;
|
|
47
|
+
|
|
48
|
+
export {
|
|
49
|
+
CodecWorker
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
class CodecWorker {
|
|
53
|
+
|
|
54
|
+
constructor(workerData, stream, workerOptions, onTaskFinished) {
|
|
55
|
+
const { readable, writable } = stream;
|
|
56
|
+
const { options, config, streamOptions, useWebWorkers, transferStreams, scripts } = workerOptions;
|
|
57
|
+
const { signal } = streamOptions;
|
|
58
|
+
Object.assign(workerData, {
|
|
59
|
+
busy: true,
|
|
60
|
+
readable: readable.pipeThrough(new ReadableStreamHooks(readable, streamOptions, config), { signal }),
|
|
61
|
+
writable,
|
|
62
|
+
options: Object.assign({}, options),
|
|
63
|
+
scripts,
|
|
64
|
+
transferStreams,
|
|
65
|
+
terminate() {
|
|
66
|
+
const { worker, busy } = workerData;
|
|
67
|
+
if (worker && !busy) {
|
|
68
|
+
worker.terminate();
|
|
69
|
+
workerData.interface = null;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
onTaskFinished() {
|
|
73
|
+
workerData.busy = false;
|
|
74
|
+
onTaskFinished(workerData);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return (useWebWorkers && WEB_WORKERS_SUPPORTED ? createWebWorkerInterface : createWorkerInterface)(workerData, config);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
class ReadableStreamHooks extends TransformStream {
|
|
82
|
+
|
|
83
|
+
constructor(readableSource, { onstart, onprogress, size, onend }, { chunkSize }) {
|
|
84
|
+
let chunkOffset = 0;
|
|
85
|
+
super({
|
|
86
|
+
start() {
|
|
87
|
+
if (onstart) {
|
|
88
|
+
callHandler(onstart, size);
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
async transform(chunk, controller) {
|
|
92
|
+
chunkOffset += chunk.length;
|
|
93
|
+
if (onprogress) {
|
|
94
|
+
await callHandler(onprogress, chunkOffset, size);
|
|
95
|
+
}
|
|
96
|
+
controller.enqueue(chunk);
|
|
97
|
+
},
|
|
98
|
+
flush() {
|
|
99
|
+
readableSource.size = chunkOffset;
|
|
100
|
+
if (onend) {
|
|
101
|
+
callHandler(onend, chunkOffset);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}, { highWaterMark: 1, size: () => chunkSize });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async function callHandler(handler, ...parameters) {
|
|
109
|
+
try {
|
|
110
|
+
await handler(...parameters);
|
|
111
|
+
} catch (_error) {
|
|
112
|
+
// ignored
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function createWorkerInterface(workerData, config) {
|
|
117
|
+
return {
|
|
118
|
+
run: () => runWorker(workerData, config)
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function createWebWorkerInterface(workerData, { baseURL, chunkSize }) {
|
|
123
|
+
if (!workerData.interface) {
|
|
124
|
+
Object.assign(workerData, {
|
|
125
|
+
worker: getWebWorker(workerData.scripts[0], baseURL, workerData),
|
|
126
|
+
interface: {
|
|
127
|
+
run: () => runWebWorker(workerData, { chunkSize })
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
return workerData.interface;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function runWorker({ options, readable, writable, onTaskFinished }, config) {
|
|
135
|
+
const codecStream = new CodecStream(options, config);
|
|
136
|
+
try {
|
|
137
|
+
await readable.pipeThrough(codecStream).pipeTo(writable, { preventClose: true, preventAbort: true });
|
|
138
|
+
const {
|
|
139
|
+
signature,
|
|
140
|
+
size
|
|
141
|
+
} = codecStream;
|
|
142
|
+
return {
|
|
143
|
+
signature,
|
|
144
|
+
size
|
|
145
|
+
};
|
|
146
|
+
} finally {
|
|
147
|
+
onTaskFinished();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function runWebWorker(workerData, config) {
|
|
152
|
+
let resolveResult, rejectResult;
|
|
153
|
+
const result = new Promise((resolve, reject) => {
|
|
154
|
+
resolveResult = resolve;
|
|
155
|
+
rejectResult = reject;
|
|
156
|
+
});
|
|
157
|
+
Object.assign(workerData, {
|
|
158
|
+
reader: null,
|
|
159
|
+
writer: null,
|
|
160
|
+
resolveResult,
|
|
161
|
+
rejectResult,
|
|
162
|
+
result
|
|
163
|
+
});
|
|
164
|
+
const { readable, options } = workerData;
|
|
165
|
+
const { writable, closed } = detectClosedStream(workerData.writable);
|
|
166
|
+
const streamsTransferred = sendMessage({
|
|
167
|
+
type: MESSAGE_START,
|
|
168
|
+
scripts: workerData.scripts.slice(1),
|
|
169
|
+
options,
|
|
170
|
+
config,
|
|
171
|
+
readable,
|
|
172
|
+
writable
|
|
173
|
+
}, workerData);
|
|
174
|
+
if (!streamsTransferred) {
|
|
175
|
+
Object.assign(workerData, {
|
|
176
|
+
reader: readable.getReader(),
|
|
177
|
+
writer: writable.getWriter()
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
const resultValue = await result;
|
|
181
|
+
try {
|
|
182
|
+
await writable.close();
|
|
183
|
+
} catch (_error) {
|
|
184
|
+
// ignored
|
|
185
|
+
}
|
|
186
|
+
await closed;
|
|
187
|
+
return resultValue;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function detectClosedStream(writableSource) {
|
|
191
|
+
const writer = writableSource.getWriter();
|
|
192
|
+
let resolveStreamClosed;
|
|
193
|
+
const closed = new Promise(resolve => resolveStreamClosed = resolve);
|
|
194
|
+
const writable = new WritableStream({
|
|
195
|
+
async write(chunk) {
|
|
196
|
+
await writer.ready;
|
|
197
|
+
await writer.write(chunk);
|
|
198
|
+
},
|
|
199
|
+
close() {
|
|
200
|
+
writer.releaseLock();
|
|
201
|
+
resolveStreamClosed();
|
|
202
|
+
},
|
|
203
|
+
abort(reason) {
|
|
204
|
+
writer.abort(reason);
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
return { writable, closed };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
let classicWorkersSupported = true;
|
|
211
|
+
let transferStreamsSupported = true;
|
|
212
|
+
|
|
213
|
+
function getWebWorker(url, baseURL, workerData) {
|
|
214
|
+
const workerOptions = { type: "module" };
|
|
215
|
+
let scriptUrl, worker;
|
|
216
|
+
if (typeof url == FUNCTION_TYPE) {
|
|
217
|
+
url = url();
|
|
218
|
+
}
|
|
219
|
+
try {
|
|
220
|
+
scriptUrl = new URL(url, baseURL);
|
|
221
|
+
} catch (_error) {
|
|
222
|
+
scriptUrl = url;
|
|
223
|
+
}
|
|
224
|
+
if (classicWorkersSupported) {
|
|
225
|
+
try {
|
|
226
|
+
worker = new Worker(scriptUrl);
|
|
227
|
+
} catch (_error) {
|
|
228
|
+
classicWorkersSupported = false;
|
|
229
|
+
worker = new Worker(scriptUrl, workerOptions);
|
|
230
|
+
}
|
|
231
|
+
} else {
|
|
232
|
+
worker = new Worker(scriptUrl, workerOptions);
|
|
233
|
+
}
|
|
234
|
+
worker.addEventListener(MESSAGE_EVENT_TYPE, event => onMessage(event, workerData));
|
|
235
|
+
return worker;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function sendMessage(message, { worker, writer, onTaskFinished, transferStreams }) {
|
|
239
|
+
try {
|
|
240
|
+
let { data, readable, writable } = message;
|
|
241
|
+
const transferrables = [];
|
|
242
|
+
if (data) {
|
|
243
|
+
const { buffer, length } = data;
|
|
244
|
+
if (length != buffer.byteLength) {
|
|
245
|
+
data = new Uint8Array(data);
|
|
246
|
+
}
|
|
247
|
+
message.data = data.buffer;
|
|
248
|
+
transferrables.push(message.data);
|
|
249
|
+
}
|
|
250
|
+
if (transferStreams && transferStreamsSupported) {
|
|
251
|
+
if (readable) {
|
|
252
|
+
transferrables.push(readable);
|
|
253
|
+
}
|
|
254
|
+
if (writable) {
|
|
255
|
+
transferrables.push(writable);
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
message.readable = message.writable = null;
|
|
259
|
+
}
|
|
260
|
+
if (transferrables.length) {
|
|
261
|
+
try {
|
|
262
|
+
worker.postMessage(message, transferrables);
|
|
263
|
+
return true;
|
|
264
|
+
} catch (_error) {
|
|
265
|
+
transferStreamsSupported = false;
|
|
266
|
+
message.readable = message.writable = null;
|
|
267
|
+
worker.postMessage(message);
|
|
268
|
+
}
|
|
269
|
+
} else {
|
|
270
|
+
worker.postMessage(message);
|
|
271
|
+
}
|
|
272
|
+
} catch (error) {
|
|
273
|
+
if (writer) {
|
|
274
|
+
writer.releaseLock();
|
|
275
|
+
}
|
|
276
|
+
onTaskFinished();
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
async function onMessage(event, workerData) {
|
|
282
|
+
const message = event.data;
|
|
283
|
+
const { reader, writer, resolveResult, rejectResult, onTaskFinished } = workerData;
|
|
284
|
+
const { type, data, messageId, result } = message;
|
|
285
|
+
const reponseError = message.error;
|
|
286
|
+
try {
|
|
287
|
+
if (reponseError) {
|
|
288
|
+
const { message, stack, code, name } = reponseError;
|
|
289
|
+
const error = new Error(message);
|
|
290
|
+
Object.assign(error, { stack, code, name });
|
|
291
|
+
close(error);
|
|
292
|
+
} else {
|
|
293
|
+
if (type == MESSAGE_PULL) {
|
|
294
|
+
const { value, done } = await reader.read();
|
|
295
|
+
sendMessage({ type: MESSAGE_DATA, data: value, done, messageId }, workerData);
|
|
296
|
+
}
|
|
297
|
+
if (type == MESSAGE_DATA) {
|
|
298
|
+
await writer.ready;
|
|
299
|
+
await writer.write(new Uint8Array(data));
|
|
300
|
+
sendMessage({ type: MESSAGE_ACK_DATA, messageId }, workerData);
|
|
301
|
+
}
|
|
302
|
+
if (type == MESSAGE_CLOSE) {
|
|
303
|
+
close(null, result);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
} catch (error) {
|
|
307
|
+
close(error);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function close(error, result) {
|
|
311
|
+
if (error) {
|
|
312
|
+
rejectResult(error);
|
|
313
|
+
} else {
|
|
314
|
+
resolveResult(result);
|
|
315
|
+
}
|
|
316
|
+
if (writer) {
|
|
317
|
+
writer.releaseLock();
|
|
318
|
+
}
|
|
319
|
+
onTaskFinished();
|
|
320
|
+
}
|
|
321
|
+
}
|
|
@@ -28,13 +28,16 @@
|
|
|
28
28
|
|
|
29
29
|
/* global navigator, CompressionStream, DecompressionStream */
|
|
30
30
|
|
|
31
|
-
import {
|
|
31
|
+
import {
|
|
32
|
+
UNDEFINED_VALUE,
|
|
33
|
+
UNDEFINED_TYPE
|
|
34
|
+
} from "./constants.js";
|
|
35
|
+
import { StreamAdapter } from "./streams/stream-adapter.js";
|
|
32
36
|
|
|
33
37
|
const MINIMUM_CHUNK_SIZE = 64;
|
|
34
|
-
const UNDEFINED_VALUE = undefined;
|
|
35
38
|
let maxWorkers = 2;
|
|
36
39
|
try {
|
|
37
|
-
if (typeof navigator !=
|
|
40
|
+
if (typeof navigator != UNDEFINED_TYPE && navigator.hardwareConcurrency) {
|
|
38
41
|
maxWorkers = navigator.hardwareConcurrency;
|
|
39
42
|
}
|
|
40
43
|
} catch (_error) {
|
|
@@ -46,9 +49,9 @@ const DEFAULT_CONFIGURATION = {
|
|
|
46
49
|
terminateWorkerTimeout: 5000,
|
|
47
50
|
useWebWorkers: true,
|
|
48
51
|
useCompressionStream: true,
|
|
49
|
-
workerScripts:
|
|
50
|
-
CompressionStreamNative: typeof CompressionStream !=
|
|
51
|
-
DecompressionStreamNative: typeof DecompressionStream !=
|
|
52
|
+
workerScripts: UNDEFINED_VALUE,
|
|
53
|
+
CompressionStreamNative: typeof CompressionStream != UNDEFINED_TYPE && CompressionStream,
|
|
54
|
+
DecompressionStreamNative: typeof DecompressionStream != UNDEFINED_TYPE && DecompressionStream
|
|
52
55
|
};
|
|
53
56
|
|
|
54
57
|
const config = Object.assign({}, DEFAULT_CONFIGURATION);
|
|
@@ -88,10 +91,10 @@ function configure(configuration) {
|
|
|
88
91
|
setIfDefined("useCompressionStream", useCompressionStream);
|
|
89
92
|
setIfDefined("useWebWorkers", useWebWorkers);
|
|
90
93
|
if (Deflate) {
|
|
91
|
-
config.CompressionStream =
|
|
94
|
+
config.CompressionStream = new StreamAdapter(Deflate);
|
|
92
95
|
}
|
|
93
96
|
if (Inflate) {
|
|
94
|
-
config.DecompressionStream =
|
|
97
|
+
config.DecompressionStream = new StreamAdapter(Inflate);
|
|
95
98
|
}
|
|
96
99
|
setIfDefined("CompressionStream", CompressionStream);
|
|
97
100
|
setIfDefined("DecompressionStream", DecompressionStream);
|
package/lib/core/constants.js
CHANGED
|
@@ -33,7 +33,8 @@ const COMPRESSION_METHOD_STORE = 0x00;
|
|
|
33
33
|
const COMPRESSION_METHOD_AES = 0x63;
|
|
34
34
|
|
|
35
35
|
const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
|
|
36
|
-
const
|
|
36
|
+
const SPLIT_ZIP_FILE_SIGNATURE = 0x08074b50;
|
|
37
|
+
const DATA_DESCRIPTOR_RECORD_SIGNATURE = SPLIT_ZIP_FILE_SIGNATURE;
|
|
37
38
|
const CENTRAL_FILE_HEADER_SIGNATURE = 0x02014b50;
|
|
38
39
|
const END_OF_CENTRAL_DIR_SIGNATURE = 0x06054b50;
|
|
39
40
|
const ZIP64_END_OF_CENTRAL_DIR_SIGNATURE = 0x06064b50;
|
|
@@ -43,8 +44,6 @@ const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH = 20;
|
|
|
43
44
|
const ZIP64_END_OF_CENTRAL_DIR_LENGTH = 56;
|
|
44
45
|
const ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH = END_OF_CENTRAL_DIR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH + ZIP64_END_OF_CENTRAL_DIR_LENGTH;
|
|
45
46
|
|
|
46
|
-
const ZIP64_TOTAL_NUMBER_OF_DISKS = 1;
|
|
47
|
-
|
|
48
47
|
const EXTRAFIELD_TYPE_ZIP64 = 0x0001;
|
|
49
48
|
const EXTRAFIELD_TYPE_AES = 0x9901;
|
|
50
49
|
const EXTRAFIELD_TYPE_NTFS = 0x000a;
|
|
@@ -68,12 +67,17 @@ const DIRECTORY_SIGNATURE = "/";
|
|
|
68
67
|
const MAX_DATE = new Date(2107, 11, 31);
|
|
69
68
|
const MIN_DATE = new Date(1980, 0, 1);
|
|
70
69
|
|
|
70
|
+
const UNDEFINED_VALUE = undefined;
|
|
71
|
+
const UNDEFINED_TYPE = "undefined";
|
|
72
|
+
const FUNCTION_TYPE = "function";
|
|
73
|
+
|
|
71
74
|
export {
|
|
72
75
|
MAX_32_BITS,
|
|
73
76
|
MAX_16_BITS,
|
|
74
77
|
COMPRESSION_METHOD_DEFLATE,
|
|
75
78
|
COMPRESSION_METHOD_STORE,
|
|
76
79
|
COMPRESSION_METHOD_AES,
|
|
80
|
+
SPLIT_ZIP_FILE_SIGNATURE,
|
|
77
81
|
LOCAL_FILE_HEADER_SIGNATURE,
|
|
78
82
|
DATA_DESCRIPTOR_RECORD_SIGNATURE,
|
|
79
83
|
CENTRAL_FILE_HEADER_SIGNATURE,
|
|
@@ -91,7 +95,6 @@ export {
|
|
|
91
95
|
ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH,
|
|
92
96
|
ZIP64_END_OF_CENTRAL_DIR_LENGTH,
|
|
93
97
|
ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH,
|
|
94
|
-
ZIP64_TOTAL_NUMBER_OF_DISKS,
|
|
95
98
|
BITFLAG_ENCRYPTED,
|
|
96
99
|
BITFLAG_LEVEL,
|
|
97
100
|
BITFLAG_DATA_DESCRIPTOR,
|
|
@@ -102,5 +105,8 @@ export {
|
|
|
102
105
|
VERSION_AES,
|
|
103
106
|
DIRECTORY_SIGNATURE,
|
|
104
107
|
MIN_DATE,
|
|
105
|
-
MAX_DATE
|
|
108
|
+
MAX_DATE,
|
|
109
|
+
UNDEFINED_VALUE,
|
|
110
|
+
UNDEFINED_TYPE,
|
|
111
|
+
FUNCTION_TYPE
|
|
106
112
|
};
|