@zip.js/zip.js 2.5.2 → 2.5.5

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.
Files changed (55) hide show
  1. package/dist/z-worker-fflate.js +1 -1
  2. package/dist/z-worker-pako.js +1 -1
  3. package/dist/z-worker.js +1 -1
  4. package/dist/zip-fs-full.js +460 -482
  5. package/dist/zip-fs-full.min.js +1 -1
  6. package/dist/zip-fs.js +460 -482
  7. package/dist/zip-fs.min.js +1 -1
  8. package/dist/zip-full.js +460 -482
  9. package/dist/zip-full.min.js +1 -1
  10. package/dist/zip-no-worker-deflate.min.js +1 -1
  11. package/dist/zip-no-worker-inflate.min.js +1 -1
  12. package/dist/zip-no-worker.min.js +1 -1
  13. package/dist/zip.js +460 -482
  14. package/dist/zip.min.js +1 -1
  15. package/index-fflate.js +1 -1
  16. package/index.d.ts +1 -1
  17. package/index.js +3 -3
  18. package/lib/core/codec-interface.js +89 -0
  19. package/lib/core/{codecs/codec-pool-worker.js → codec-pool-worker.js} +55 -29
  20. package/lib/core/{codecs/codec-pool.js → codec-pool.js} +28 -9
  21. package/lib/core/configuration.js +10 -1
  22. package/lib/core/io.js +102 -100
  23. package/lib/core/{codecs → streams}/aes-crypto-stream.js +68 -54
  24. package/lib/core/{codecs → streams}/codec-stream.js +3 -6
  25. package/lib/core/{codecs → streams/codecs}/crc32.js +0 -0
  26. package/lib/core/{codecs → streams/codecs}/deflate.js +0 -0
  27. package/lib/core/{codecs → streams/codecs}/inflate.js +0 -0
  28. package/lib/core/{codecs → streams/codecs}/sjcl.js +0 -0
  29. package/lib/core/{codecs → streams}/crc32-stream.js +3 -4
  30. package/lib/core/{codecs → streams}/zip-crypto-stream.js +28 -32
  31. package/lib/core/streams/zip-entry-stream.js +178 -0
  32. package/lib/core/z-worker-core.js +30 -15
  33. package/lib/core/zip-fs-core.js +1 -1
  34. package/lib/core/zip-reader.js +21 -12
  35. package/lib/core/zip-writer.js +33 -22
  36. package/lib/z-worker-fflate.js +4 -1
  37. package/lib/z-worker-inline.js +1 -1
  38. package/lib/z-worker.js +2 -2
  39. package/lib/zip-fflate-shim.js +4 -1
  40. package/lib/zip-fs.js +6 -2
  41. package/lib/zip-full-fflate.js +6 -3
  42. package/lib/zip-full.js +8 -5
  43. package/lib/zip-no-worker-deflate.js +7 -3
  44. package/lib/zip-no-worker-fflate-deflate.js +6 -2
  45. package/lib/zip-no-worker-fflate-inflate.js +6 -3
  46. package/lib/zip-no-worker-fflate.js +6 -3
  47. package/lib/zip-no-worker-inflate.js +7 -4
  48. package/lib/zip-no-worker.js +8 -5
  49. package/lib/zip.js +6 -3
  50. package/package.json +2 -8
  51. package/tests/all/test-safe-closing.js +1 -1
  52. package/tests/node/package.json +2 -6
  53. package/tests/node/test-node.js +7 -11
  54. package/lib/core/codecs/codec.js +0 -230
  55. package/lib/core/engine.js +0 -89
@@ -29,16 +29,19 @@
29
29
  import { Deflate, Inflate } from "./zip-fflate-shim.js";
30
30
  import { configure } from "./core/configuration.js";
31
31
  import { getMimeType } from "./core/util/default-mime-type.js";
32
- import { terminateWorkers } from "./core/codecs/codec-pool.js";
32
+ import {
33
+ terminateWorkers,
34
+ ERR_ABORT
35
+ } from "./core/codec-pool.js";
33
36
 
34
37
  configure({ Deflate, Inflate });
35
38
 
36
- export { ERR_ABORT } from "./core/engine.js";
37
39
  export * from "./core/io.js";
38
40
  export * from "./core/zip-reader.js";
39
41
  export * from "./core/zip-writer.js";
40
42
  export {
41
43
  configure,
42
44
  getMimeType,
43
- terminateWorkers
45
+ terminateWorkers,
46
+ ERR_ABORT
44
47
  };
@@ -26,18 +26,21 @@
26
26
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
 
29
- import Inflate from "./core/codecs/inflate.js";
29
+ import Inflate from "./core/streams/codecs/inflate.js";
30
30
  import { configure } from "./core/configuration.js";
31
31
  import { getMimeType } from "./core/util/default-mime-type.js";
32
- import { terminateWorkers } from "./core/codecs/codec-pool.js";
32
+ import {
33
+ terminateWorkers,
34
+ ERR_ABORT
35
+ } from "./core/codec-pool.js";
33
36
 
34
37
  configure({ Inflate });
35
38
 
36
- export { ERR_ABORT } from "./core/engine.js";
37
39
  export * from "./core/io.js";
38
40
  export * from "./core/zip-reader.js";
39
41
  export {
40
42
  configure,
41
43
  getMimeType,
42
- terminateWorkers
44
+ terminateWorkers,
45
+ ERR_ABORT
43
46
  };
@@ -26,20 +26,23 @@
26
26
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
 
29
- import Deflate from "./core/codecs/deflate.js";
30
- import Inflate from "./core/codecs/inflate.js";
29
+ import Deflate from "./core/streams/codecs/deflate.js";
30
+ import Inflate from "./core/streams/codecs/inflate.js";
31
31
  import { configure } from "./core/configuration.js";
32
32
  import { getMimeType } from "./core/util/default-mime-type.js";
33
- import { terminateWorkers } from "./core/codecs/codec-pool.js";
33
+ import {
34
+ terminateWorkers,
35
+ ERR_ABORT
36
+ } from "./core/codec-pool.js";
34
37
 
35
38
  configure({ Deflate, Inflate });
36
39
 
37
- export { ERR_ABORT } from "./core/engine.js";
38
40
  export * from "./core/io.js";
39
41
  export * from "./core/zip-reader.js";
40
42
  export * from "./core/zip-writer.js";
41
43
  export {
42
44
  configure,
43
45
  getMimeType,
44
- terminateWorkers
46
+ terminateWorkers,
47
+ ERR_ABORT
45
48
  };
package/lib/zip.js CHANGED
@@ -30,7 +30,10 @@ import { configure } from "./core/configuration.js";
30
30
  import configureWebWorker from "./z-worker-inline.js";
31
31
  import { getMimeType } from "./core/util/default-mime-type.js";
32
32
  import initShimAsyncCodec from "./core/util/stream-codec-shim.js";
33
- import { terminateWorkers } from "./core/codecs/codec-pool.js";
33
+ import {
34
+ terminateWorkers,
35
+ ERR_ABORT
36
+ } from "./core/codec-pool.js";
34
37
 
35
38
  let baseURL;
36
39
  try {
@@ -41,7 +44,6 @@ try {
41
44
  configure({ baseURL });
42
45
  configureWebWorker(configure);
43
46
 
44
- export { ERR_ABORT } from "./core/engine.js";
45
47
  export * from "./core/io.js";
46
48
  export * from "./core/zip-reader.js";
47
49
  export * from "./core/zip-writer.js";
@@ -49,5 +51,6 @@ export {
49
51
  configure,
50
52
  getMimeType,
51
53
  initShimAsyncCodec,
52
- terminateWorkers
54
+ terminateWorkers,
55
+ ERR_ABORT
53
56
  };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "A JavaScript library to zip and unzip files in the browser and Deno",
4
4
  "author": "Gildas Lormeau",
5
5
  "license": "BSD-3-Clause",
6
- "version": "2.5.2",
6
+ "version": "2.5.5",
7
7
  "type": "module",
8
8
  "keywords": [
9
9
  "zip",
@@ -36,15 +36,9 @@
36
36
  },
37
37
  "homepage": "https://gildas-lormeau.github.io/zip.js",
38
38
  "devDependencies": {
39
- "@babel/core": "^7.18.6",
40
- "@babel/preset-env": "^7.18.6",
41
- "@rollup/plugin-babel": "^5.3.1",
42
- "@rollup/plugin-commonjs": "^22.0.1",
43
39
  "@rollup/plugin-node-resolve": "^13.3.0",
44
40
  "@rollup/plugin-replace": "^4.0.0",
45
- "babel-plugin-transform-async-to-promises": "^0.8.18",
46
- "core-js": "^3.23.3",
47
- "rollup": "^2.75.7",
41
+ "rollup": "^2.76.0",
48
42
  "rollup-plugin-terser": "^7.0.2"
49
43
  }
50
44
  }
@@ -14,7 +14,7 @@ async function test() {
14
14
  zip.configure({ chunkSize: 512, maxWorkers: 4, useWebWorkers: true });
15
15
  const blobWriter = new zip.BlobWriter("application/zip");
16
16
  const zipWriter = new zip.ZipWriter(blobWriter);
17
- ENTRIES_DATA.map(entryData => zipWriter.add(entryData.name, new zip.BlobReader(entryData.blob))); // missing await when calling zipWriter.add
17
+ ENTRIES_DATA.map(entryData => zipWriter.add(entryData.name, new zip.BlobReader(entryData.blob))); // missing call to `await Promise.all`
18
18
  await zipWriter.close();
19
19
  const zipReader = new zip.ZipReader(new zip.BlobReader(blobWriter.getData()));
20
20
  const entries = await zipReader.getEntries();
@@ -1,9 +1,5 @@
1
1
  {
2
2
  "name": "test-node-zipjs",
3
3
  "version": "1.0.0",
4
- "type": "module",
5
- "dependencies": {
6
- "blob-polyfill": "^6.0.20211015",
7
- "web-worker": "^1.2.0"
8
- }
9
- }
4
+ "type": "module"
5
+ }
@@ -1,28 +1,24 @@
1
- /* global globalThis */
1
+ /* global TextEncoder */
2
2
  /* eslint-disable no-console */
3
3
 
4
- import Worker from "web-worker";
5
- import { Blob } from "blob-polyfill";
6
- import { BlobWriter, BlobReader, TextWriter, ZipWriter, ZipReader, terminateWorkers } from "../../index.js";
4
+ import { configure, Uint8ArrayWriter, Uint8ArrayReader, TextWriter, ZipWriter, ZipReader, terminateWorkers } from "../../index.js";
7
5
 
8
6
  const TEXT_CONTENT = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.";
9
7
  const FILENAME = "lorem.txt";
10
- const TEXT_PLAIN_MIMETYPE = "text/plain";
11
- const BLOB = new Blob([TEXT_CONTENT], { type: TEXT_PLAIN_MIMETYPE });
8
+ const UINT8_ARRAY = (new TextEncoder()).encode(TEXT_CONTENT);
12
9
 
13
- globalThis.Blob = Blob;
14
- globalThis.Worker = Worker;
15
10
  test()
16
11
  .then(result => result && console.log("ok"))
17
12
  .catch(error => console.error(error));
18
13
 
19
14
  async function test() {
20
- const blobWriter = new BlobWriter("application/zip");
15
+ configure({ useWebWorkers: false });
16
+ const blobWriter = new Uint8ArrayWriter(UINT8_ARRAY);
21
17
  const zipWriter = new ZipWriter(blobWriter);
22
- const entry = await zipWriter.add(FILENAME, new BlobReader(BLOB));
18
+ const entry = await zipWriter.add(FILENAME, new Uint8ArrayReader(UINT8_ARRAY));
23
19
  if (entry.compressionMethod == 0x08) {
24
20
  await zipWriter.close();
25
- const zipReader = new ZipReader(new BlobReader(blobWriter.getData()));
21
+ const zipReader = new ZipReader(new Uint8ArrayReader(blobWriter.getData()));
26
22
  const entries = await zipReader.getEntries();
27
23
  if (entries[0].compressionMethod == 0x08) {
28
24
  const data = await entries[0].getData(new TextWriter());
@@ -1,230 +0,0 @@
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 ReadableStream, DecompressionStream, CompressionStream */
30
- // deno-lint-ignore-file no-this-alias
31
-
32
- import Crc32Stream from "./crc32-stream.js";
33
- import { AESEncryptStream, AESDecryptStream, ERR_INVALID_PASSWORD } from "./aes-crypto-stream.js";
34
- import { ZipCryptoDecryptStream, ZipCryptoEncryptStream } from "./zip-crypto-stream.js";
35
- import CodecStream from "./codec-stream.js";
36
-
37
- const CODEC_DEFLATE = "deflate";
38
- const CODEC_INFLATE = "inflate";
39
- const ERR_INVALID_SIGNATURE = "Invalid signature";
40
- const MESSAGE_EVENT_TYPE = "message";
41
- const MESSAGE_START = "start";
42
- const MESSAGE_PULL = "pull";
43
- const MESSAGE_DATA = "data";
44
- const MESSAGE_ABORT = "abort";
45
-
46
- class Inflate {
47
-
48
- constructor(codecConstructor, {
49
- signature,
50
- password,
51
- signed,
52
- compressed,
53
- zipCrypto,
54
- passwordVerification,
55
- encryptionStrength,
56
- useCompressionStream
57
- }, { chunkSize }) {
58
- const codec = this;
59
- codec.start = async () => {
60
- const encrypted = Boolean(password);
61
- Object.assign(codec, {
62
- signature,
63
- encrypted,
64
- signed,
65
- compressed,
66
- zipCrypto,
67
- stream: new ReadableStream({
68
- async pull(controller) {
69
- const { value, done } = await codec.pull();
70
- controller.enqueue(value);
71
- if (done || codec.aborted) {
72
- controller.close();
73
- }
74
- }
75
- })
76
- });
77
- if (encrypted) {
78
- if (zipCrypto) {
79
- codec.stream = codec.stream.pipeThrough(new ZipCryptoDecryptStream(password, passwordVerification));
80
- } else {
81
- codec.decryptStream = new AESDecryptStream(password, signed, encryptionStrength);
82
- codec.stream = codec.stream.pipeThrough(codec.decryptStream);
83
- }
84
- }
85
- if (compressed) {
86
- if ((useCompressionStream !== undefined && !useCompressionStream) || typeof DecompressionStream == "undefined") {
87
- codec.stream = codec.stream.pipeThrough(new CodecStream(codecConstructor, { chunkSize }));
88
- } else {
89
- codec.stream = codec.stream.pipeThrough(new DecompressionStream("deflate-raw"));
90
- }
91
- }
92
- if ((!encrypted || zipCrypto) && signed) {
93
- const tee = codec.stream.tee();
94
- codec.stream = tee[0];
95
- codec.crc32Stream = tee[1].pipeThrough(new Crc32Stream());
96
- }
97
- codec.reader = codec.stream.getReader();
98
- await codec.read();
99
- };
100
- codec.abort = () => codec.aborted = true;
101
- }
102
-
103
- async read() {
104
- const codec = this;
105
- const { value, done } = await codec.reader.read();
106
- if (value && value.length) {
107
- codec.ondata({ value: { data: new Uint8Array(value) } });
108
- }
109
- if (done) {
110
- let signature;
111
- if (codec.encrypted && !codec.zipCrypto) {
112
- if (!codec.decryptStream.valid) {
113
- throw new Error(ERR_INVALID_SIGNATURE);
114
- }
115
- }
116
- if ((!codec.encrypted || codec.zipCrypto) && codec.signed) {
117
- signature = await codec.crc32Stream.getReader().read();
118
- const dataViewSignature = new DataView(signature.value.buffer);
119
- if (codec.signature != dataViewSignature.getUint32(0, false)) {
120
- throw new Error(ERR_INVALID_SIGNATURE);
121
- }
122
- }
123
- codec.ondata({ value: { signature }, done });
124
- } else {
125
- await codec.read();
126
- }
127
- }
128
- }
129
-
130
- class Deflate {
131
-
132
- constructor(codecConstructor, {
133
- encrypted,
134
- signed,
135
- compressed,
136
- level,
137
- zipCrypto,
138
- password,
139
- passwordVerification,
140
- encryptionStrength,
141
- useCompressionStream
142
- }, { chunkSize }) {
143
- const codec = this;
144
- codec.start = async () => {
145
- Object.assign(this, {
146
- encrypted,
147
- signed,
148
- compressed,
149
- zipCrypto,
150
- stream: new ReadableStream({
151
- async pull(controller) {
152
- const { value, done } = await codec.pull();
153
- controller.enqueue(value);
154
- if (done || codec.aborted) {
155
- controller.close();
156
- }
157
- }
158
- })
159
- });
160
- if ((!encrypted || zipCrypto) && signed) {
161
- const tee = codec.stream.tee();
162
- codec.stream = tee[0];
163
- codec.crc32Stream = tee[1].pipeThrough(new Crc32Stream({ chunkSize }));
164
- }
165
- if (compressed) {
166
- if ((useCompressionStream !== undefined && !useCompressionStream) || typeof CompressionStream == "undefined") {
167
- codec.stream = codec.stream.pipeThrough(new CodecStream(codecConstructor, { chunkSize, level }));
168
- } else {
169
- codec.stream = codec.stream.pipeThrough(new CompressionStream("deflate-raw"));
170
- }
171
- }
172
- if (encrypted) {
173
- if (zipCrypto) {
174
- codec.stream = codec.stream.pipeThrough(new ZipCryptoEncryptStream(password, passwordVerification));
175
- } else {
176
- codec.encryptStream = new AESEncryptStream(password, encryptionStrength);
177
- codec.stream = codec.stream.pipeThrough(codec.encryptStream);
178
- }
179
- }
180
- codec.reader = codec.stream.getReader();
181
- await codec.read();
182
- };
183
- codec.abort = () => codec.aborted = true;
184
- }
185
-
186
- async read() {
187
- const codec = this;
188
- const { value, done } = await codec.reader.read();
189
- if (value && value.length) {
190
- codec.ondata({ value: { data: value } });
191
- }
192
- if (done) {
193
- let signature;
194
- if (codec.encrypted && !codec.zipCrypto) {
195
- signature = codec.encryptStream.signature;
196
- }
197
- if ((!codec.encrypted || codec.zipCrypto) && codec.signed) {
198
- signature = await codec.crc32Stream.getReader().read();
199
- signature = new DataView(signature.value.buffer).getUint32(0);
200
- }
201
- codec.ondata({ value: { signature }, done });
202
- } else {
203
- await codec.read();
204
- }
205
- }
206
-
207
- }
208
-
209
- export {
210
- Inflate,
211
- Deflate,
212
- createCodec,
213
- CODEC_DEFLATE,
214
- CODEC_INFLATE,
215
- ERR_INVALID_SIGNATURE,
216
- ERR_INVALID_PASSWORD,
217
- MESSAGE_EVENT_TYPE,
218
- MESSAGE_START,
219
- MESSAGE_PULL,
220
- MESSAGE_DATA,
221
- MESSAGE_ABORT
222
- };
223
-
224
- function createCodec(codecConstructor, options, config) {
225
- if (options.codecType.startsWith(CODEC_DEFLATE)) {
226
- return new Deflate(codecConstructor, options, config);
227
- } else if (options.codecType.startsWith(CODEC_INFLATE)) {
228
- return new Inflate(codecConstructor, options, config);
229
- }
230
- }
@@ -1,89 +0,0 @@
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
- const MINIMUM_CHUNK_SIZE = 64;
30
- const ERR_ABORT = "Abort error";
31
-
32
- export {
33
- ERR_ABORT,
34
- processData
35
- };
36
-
37
- async function processData(codec, reader, writer, offset, inputLengthGetter, config, options) {
38
- const chunkSize = Math.max(config.chunkSize, MINIMUM_CHUNK_SIZE);
39
- const signal = options.signal;
40
- let length = 0;
41
- let chunkOffset = 0;
42
-
43
- const result = new Promise((resolve, reject) => {
44
- codec.pull = async () => {
45
- testAborted(signal, codec);
46
- const inputLength = inputLengthGetter();
47
- let currentChunkOffset = chunkOffset;
48
- chunkOffset += chunkSize;
49
- const value = await reader.readUint8Array(currentChunkOffset + offset, Math.min(chunkSize, inputLength - currentChunkOffset));
50
- const done = chunkOffset > inputLength;
51
- return { value, done };
52
- };
53
- codec.ondata = async ({ value, done }) => {
54
- try {
55
- testAborted(signal, codec);
56
- const inputLength = inputLengthGetter();
57
- const { data, signature } = value;
58
- if (data && data.length) {
59
- length += data.length;
60
- await writer.writeUint8Array(data);
61
- }
62
- if (options.onprogress) {
63
- try {
64
- options.onprogress(chunkOffset, inputLength);
65
- } catch (error) {
66
- // ignored
67
- }
68
- }
69
- if (done) {
70
- resolve({ signature, length });
71
- }
72
- } catch (error) {
73
- reject(error);
74
- }
75
- };
76
- codec.onerror = (error) => {
77
- reject(error);
78
- };
79
- });
80
- await codec.start();
81
- return result;
82
- }
83
-
84
- function testAborted(signal, codec) {
85
- if (signal && signal.aborted) {
86
- codec.abort();
87
- throw new Error(ERR_ABORT);
88
- }
89
- }