@zip.js/zip.js 2.5.12 → 2.5.15

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.
@@ -44,9 +44,9 @@ export {
44
44
  getWorker
45
45
  };
46
46
 
47
- function getWorker(workerData, codecConstructor, stream, workerOptions, onTaskFinished) {
47
+ function getWorker(workerData, stream, workerOptions, onTaskFinished) {
48
48
  const { readable, writable } = stream;
49
- const { options, config, streamOptions, webWorker, scripts } = workerOptions;
49
+ const { options, config, streamOptions, webWorker, scripts, codecConstructor } = workerOptions;
50
50
  Object.assign(workerData, {
51
51
  busy: true,
52
52
  codecConstructor,
@@ -82,7 +82,7 @@ function getWorker(workerData, codecConstructor, stream, workerOptions, onTaskFi
82
82
  if (onend) {
83
83
  transformer.flush = () => callHandler(onend);
84
84
  }
85
- workerData.readable = readable.pipeThrough(new TransformStream(transformer), { signal });
85
+ workerData.readable = readable.pipeThrough(new TransformStream(transformer, { highWaterMark: 1, size: () => config.chunkSize }), { signal });
86
86
  }
87
87
  return webWorker ? createWebWorkerInterface(workerData, config) : createWorkerInterface(workerData, config);
88
88
  }
@@ -97,14 +97,15 @@ async function callHandler(handler, ...parameters) {
97
97
 
98
98
  function createWorkerInterface(workerData, config) {
99
99
  const interfaceCodec = createCodec(workerData.codecConstructor, workerData.readable, workerData.writable, workerData.options, config);
100
+ const { onTaskFinished } = workerData;
100
101
  const codec = {
101
102
  abort() {
102
103
  interfaceCodec.abort();
103
- workerData.onTaskFinished();
104
+ onTaskFinished();
104
105
  },
105
- run() {
106
- const result = interfaceCodec.run();
107
- workerData.onTaskFinished();
106
+ async run() {
107
+ const result = await interfaceCodec.run();
108
+ onTaskFinished();
108
109
  return result;
109
110
  }
110
111
  };
@@ -113,11 +114,14 @@ function createWorkerInterface(workerData, config) {
113
114
 
114
115
  function createWebWorkerInterface(workerData, { baseURL, chunkSize }) {
115
116
  const workerOptions = { type: "module" };
116
- workerData.reader = workerData.readable.getReader();
117
- workerData.writer = workerData.writable.getWriter();
118
- workerData.result = new Promise((resolve, reject) => {
119
- workerData.resolveResult = resolve;
120
- workerData.rejectResult = reject;
117
+ const { readable, writable } = workerData;
118
+ Object.assign(workerData, {
119
+ reader: readable.getReader(),
120
+ writer: writable.getWriter(),
121
+ result: new Promise((resolve, reject) => {
122
+ workerData.resolveResult = resolve;
123
+ workerData.rejectResult = reject;
124
+ })
121
125
  });
122
126
  if (!workerData.interface) {
123
127
  if (!classicWorkersSupported) {
@@ -133,12 +137,13 @@ function createWebWorkerInterface(workerData, { baseURL, chunkSize }) {
133
137
  workerData.worker.addEventListener(MESSAGE_EVENT_TYPE, onMessage, false);
134
138
  workerData.interface = {
135
139
  abort() {
140
+ workerData.interface.aborted = true;
136
141
  sendMessage({ type: MESSAGE_ABORT });
137
142
  workerData.writer.releaseLock();
138
143
  workerData.onTaskFinished();
139
144
  },
140
145
  run() {
141
- const options = workerData.options;
146
+ const { options } = workerData;
142
147
  const scripts = workerData.scripts.slice(1);
143
148
  sendMessage({ type: MESSAGE_START, scripts, options, config: { chunkSize } });
144
149
  return workerData.result;
@@ -162,12 +167,13 @@ function createWebWorkerInterface(workerData, { baseURL, chunkSize }) {
162
167
  }
163
168
 
164
169
  function sendMessage(message) {
165
- const worker = workerData.worker;
170
+ const { worker, writer, onTaskFinished } = workerData;
166
171
  try {
167
172
  let { data } = message;
168
173
  if (data) {
169
174
  try {
170
- if (data.length != data.buffer.byteLength) {
175
+ const { buffer, length } = data;
176
+ if (length != buffer.byteLength) {
171
177
  data = new Uint8Array(data);
172
178
  }
173
179
  message.data = data.buffer;
@@ -179,43 +185,48 @@ function createWebWorkerInterface(workerData, { baseURL, chunkSize }) {
179
185
  worker.postMessage(message);
180
186
  }
181
187
  } catch (error) {
182
- workerData.writer.releaseLock();
183
- workerData.onTaskFinished();
188
+ writer.releaseLock();
189
+ onTaskFinished();
184
190
  throw error;
185
191
  }
186
192
  }
187
193
 
188
194
  async function onMessage(event) {
189
195
  const message = event.data;
196
+ const { reader, writer, resolveResult, rejectResult, onTaskFinished } = workerData;
197
+ const { type, data, messageId, result } = message;
190
198
  const reponseError = message.error;
191
199
  try {
192
200
  if (reponseError) {
193
- const error = new Error(reponseError.message);
194
- error.stack = reponseError.stack;
195
- workerData.rejectResult(error);
196
- workerData.writer.releaseLock();
197
- workerData.onTaskFinished();
201
+ const { message, stack } = reponseError;
202
+ const error = new Error(message);
203
+ error.stack = stack;
204
+ close(error);
198
205
  } else {
199
- if (message.type == MESSAGE_PULL) {
200
- const { value, done } = await workerData.reader.read();
201
- sendMessage({ type: MESSAGE_DATA, data: value, done, messageId: message.messageId });
206
+ if (type == MESSAGE_PULL) {
207
+ const { value, done } = await reader.read();
208
+ sendMessage({ type: MESSAGE_DATA, data: value, done, messageId });
202
209
  }
203
- if (message.type == MESSAGE_DATA) {
204
- let { data } = message;
205
- data = new Uint8Array(data);
206
- await workerData.writer.ready;
207
- await workerData.writer.write(data);
210
+ if (type == MESSAGE_DATA) {
211
+ await writer.ready;
212
+ await writer.write(new Uint8Array(data));
208
213
  }
209
- if (message.type == MESSAGE_CLOSE) {
210
- workerData.resolveResult(message.result);
211
- workerData.writer.releaseLock();
212
- workerData.onTaskFinished();
214
+ if (type == MESSAGE_CLOSE) {
215
+ close(null, result);
213
216
  }
214
217
  }
215
218
  } catch (error) {
216
- workerData.rejectResult(error);
217
- workerData.writer.releaseLock();
218
- workerData.onTaskFinished();
219
+ close(error);
220
+ }
221
+
222
+ function close(error, result) {
223
+ if (error && !workerData.interface.aborted) {
224
+ rejectResult(error);
225
+ } else {
226
+ resolveResult(result);
227
+ }
228
+ writer.releaseLock();
229
+ onTaskFinished();
219
230
  }
220
231
  }
221
232
  }
@@ -41,7 +41,7 @@ let pool = [];
41
41
  const pendingRequests = [];
42
42
 
43
43
  export {
44
- runCodec,
44
+ runWorker,
45
45
  terminateWorkers,
46
46
  CODEC_DEFLATE,
47
47
  CODEC_INFLATE,
@@ -52,26 +52,27 @@ export {
52
52
 
53
53
  let indexWorker = 0;
54
54
 
55
- async function runCodec(codecConstructor, stream, codecOptions) {
56
- const { options, config, streamOptions } = codecOptions;
55
+ async function runWorker(stream, workerOptions) {
56
+ const { options, config, streamOptions } = workerOptions;
57
57
  const streamCopy = !options.compressed && !options.signed && !options.encrypted;
58
- codecOptions.webWorker = !streamCopy && (options.useWebWorkers || (options.useWebWorkers === undefined && config.useWebWorkers));
59
- codecOptions.scripts = codecOptions.webWorker && config.workerScripts ? config.workerScripts[options.codecType] : [];
60
- options.useCompressionStream = options.useCompressionStream === undefined ? config.useCompressionStream : options.useCompressionStream;
58
+ const { useWebWorkers, useCompressionStream, codecType } = options;
59
+ const { workerScripts } = config;
60
+ workerOptions.webWorker = !streamCopy && (useWebWorkers || (useWebWorkers === undefined && config.useWebWorkers));
61
+ workerOptions.scripts = workerOptions.webWorker && workerScripts ? workerScripts[codecType] : [];
62
+ options.useCompressionStream = useCompressionStream === undefined ? config.useCompressionStream : useCompressionStream;
61
63
  let codec;
62
-
63
64
  if (pool.length < config.maxWorkers) {
64
65
  const workerData = { indexWorker };
65
66
  indexWorker++;
66
67
  pool.push(workerData);
67
- codec = getWorker(workerData, codecConstructor, stream, codecOptions, onTaskFinished);
68
+ codec = getWorker(workerData, stream, workerOptions, onTaskFinished);
68
69
  } else {
69
70
  const workerData = pool.find(workerData => !workerData.busy);
70
71
  if (workerData) {
71
72
  clearTerminateTimeout(workerData);
72
- codec = getWorker(workerData, codecConstructor, stream, codecOptions, onTaskFinished);
73
+ codec = getWorker(workerData, stream, workerOptions, onTaskFinished);
73
74
  } else {
74
- codec = await new Promise(resolve => pendingRequests.push({ resolve, codecConstructor, stream, codecOptions }));
75
+ codec = await new Promise(resolve => pendingRequests.push({ resolve, stream, workerOptions }));
75
76
  }
76
77
  }
77
78
  const { signal } = streamOptions;
@@ -88,15 +89,16 @@ async function runCodec(codecConstructor, stream, codecOptions) {
88
89
 
89
90
  function onTaskFinished(workerData) {
90
91
  if (pendingRequests.length) {
91
- const [{ resolve, codecConstructor, stream, codecOptions }] = pendingRequests.splice(0, 1);
92
- resolve(getWorker(workerData, codecConstructor, stream, codecOptions, onTaskFinished));
92
+ const [{ resolve, stream, workerOptions }] = pendingRequests.splice(0, 1);
93
+ resolve(getWorker(workerData, stream, workerOptions, onTaskFinished));
93
94
  } else if (workerData.worker) {
95
+ const { terminateWorkerTimeout } = config;
94
96
  clearTerminateTimeout(workerData);
95
- if (Number.isFinite(config.terminateWorkerTimeout) && config.terminateWorkerTimeout >= 0) {
97
+ if (Number.isFinite(terminateWorkerTimeout) && terminateWorkerTimeout >= 0) {
96
98
  workerData.terminateTimeout = setTimeout(() => {
97
99
  pool = pool.filter(data => data != workerData);
98
100
  workerData.terminate();
99
- }, config.terminateWorkerTimeout);
101
+ }, terminateWorkerTimeout);
100
102
  }
101
103
  } else {
102
104
  pool = pool.filter(data => data != workerData);
@@ -105,8 +107,9 @@ async function runCodec(codecConstructor, stream, codecOptions) {
105
107
  }
106
108
 
107
109
  function clearTerminateTimeout(workerData) {
108
- if (workerData.terminateTimeout) {
109
- clearTimeout(workerData.terminateTimeout);
110
+ const { terminateTimeout } = workerData;
111
+ if (terminateTimeout) {
112
+ clearTimeout(terminateTimeout);
110
113
  workerData.terminateTimeout = null;
111
114
  }
112
115
  }
@@ -29,6 +29,7 @@
29
29
  /* global navigator */
30
30
 
31
31
  const MINIMUM_CHUNK_SIZE = 64;
32
+ const UNDEFINED_TYPE = undefined;
32
33
  const DEFAULT_CONFIGURATION = {
33
34
  chunkSize: 512 * 1024,
34
35
  maxWorkers: (typeof navigator != "undefined" && navigator.hardwareConcurrency) || 2,
@@ -54,48 +55,59 @@ function getChunkSize(config) {
54
55
  }
55
56
 
56
57
  function configure(configuration) {
57
- if (configuration.baseURL !== undefined) {
58
- config.baseURL = configuration.baseURL;
58
+ const {
59
+ baseURL,
60
+ chunkSize,
61
+ maxWorkers,
62
+ terminateWorkerTimeout,
63
+ useCompressionStream,
64
+ useWebWorkers,
65
+ Deflate,
66
+ Inflate,
67
+ workerScripts
68
+ } = configuration;
69
+ if (baseURL !== UNDEFINED_TYPE) {
70
+ config.baseURL = baseURL;
59
71
  }
60
- if (configuration.chunkSize !== undefined) {
61
- config.chunkSize = configuration.chunkSize;
72
+ if (chunkSize !== UNDEFINED_TYPE) {
73
+ config.chunkSize = chunkSize;
62
74
  }
63
- if (configuration.maxWorkers !== undefined) {
64
- config.maxWorkers = configuration.maxWorkers;
75
+ if (maxWorkers !== UNDEFINED_TYPE) {
76
+ config.maxWorkers = maxWorkers;
65
77
  }
66
- if (configuration.terminateWorkerTimeout !== undefined) {
67
- config.terminateWorkerTimeout = configuration.terminateWorkerTimeout;
78
+ if (terminateWorkerTimeout !== UNDEFINED_TYPE) {
79
+ config.terminateWorkerTimeout = terminateWorkerTimeout;
68
80
  }
69
- if (configuration.useCompressionStream !== undefined) {
70
- config.useCompressionStream = configuration.useCompressionStream;
81
+ if (useCompressionStream !== UNDEFINED_TYPE) {
82
+ config.useCompressionStream = useCompressionStream;
71
83
  }
72
- if (configuration.useWebWorkers !== undefined) {
73
- config.useWebWorkers = configuration.useWebWorkers;
84
+ if (useWebWorkers !== UNDEFINED_TYPE) {
85
+ config.useWebWorkers = useWebWorkers;
74
86
  }
75
- if (configuration.Deflate !== undefined) {
76
- config.Deflate = configuration.Deflate;
87
+ if (Deflate !== UNDEFINED_TYPE) {
88
+ config.Deflate = Deflate;
77
89
  }
78
- if (configuration.Inflate !== undefined) {
79
- config.Inflate = configuration.Inflate;
90
+ if (Inflate !== UNDEFINED_TYPE) {
91
+ config.Inflate = Inflate;
80
92
  }
81
- if (configuration.workerScripts !== undefined) {
82
- if (configuration.workerScripts.deflate) {
83
- if (!Array.isArray(configuration.workerScripts.deflate)) {
84
- throw new Error("workerScripts.deflate must be an array");
85
- }
93
+ if (workerScripts !== UNDEFINED_TYPE) {
94
+ const { deflate, inflate } = workerScripts;
95
+ if (deflate || inflate) {
86
96
  if (!config.workerScripts) {
87
97
  config.workerScripts = {};
88
98
  }
89
- config.workerScripts.deflate = configuration.workerScripts.deflate;
90
99
  }
91
- if (configuration.workerScripts.inflate) {
92
- if (!Array.isArray(configuration.workerScripts.inflate)) {
93
- throw new Error("workerScripts.inflate must be an array");
100
+ if (deflate) {
101
+ if (!Array.isArray(deflate)) {
102
+ throw new Error("workerScripts.deflate must be an array");
94
103
  }
95
- if (!config.workerScripts) {
96
- config.workerScripts = {};
104
+ config.workerScripts.deflate = deflate;
105
+ }
106
+ if (inflate) {
107
+ if (!Array.isArray(inflate)) {
108
+ throw new Error("workerScripts.inflate must be an array");
97
109
  }
98
- config.workerScripts.inflate = configuration.workerScripts.inflate;
110
+ config.workerScripts.inflate = inflate;
99
111
  }
100
112
  }
101
113
  }
package/lib/core/io.js CHANGED
@@ -26,7 +26,7 @@
26
26
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
 
29
- /* global Blob, FileReader, atob, btoa, XMLHttpRequest, document, fetch, ReadableStream, WritableStream, TransformStream */
29
+ /* global Blob, atob, btoa, XMLHttpRequest, document, fetch, ReadableStream, WritableStream, TransformStream */
30
30
 
31
31
  const ERR_HTTP_STATUS = "HTTP error ";
32
32
  const ERR_HTTP_RANGE = "HTTP Range not supported";
@@ -127,16 +127,7 @@ class TextWriter extends Writer {
127
127
 
128
128
  getData() {
129
129
  const writer = this;
130
- if (writer.blob.text) {
131
- return writer.blob.text();
132
- } else {
133
- const reader = new FileReader();
134
- return new Promise((resolve, reject) => {
135
- reader.onload = event => resolve(event.target.result);
136
- reader.onerror = () => reject(reader.error);
137
- reader.readAsText(writer.blob, writer.encoding);
138
- });
139
- }
130
+ return writer.blob.text();
140
131
  }
141
132
  }
142
133
 
@@ -210,16 +201,7 @@ class BlobReader extends Reader {
210
201
 
211
202
  async readUint8Array(offset, length) {
212
203
  const reader = this;
213
- if (reader.blob.arrayBuffer) {
214
- return new Uint8Array(await reader.blob.slice(offset, offset + length).arrayBuffer());
215
- } else {
216
- const reader = new FileReader();
217
- return new Promise((resolve, reject) => {
218
- reader.onload = event => resolve(new Uint8Array(event.target.result));
219
- reader.onerror = () => reject(reader.error);
220
- reader.readAsArrayBuffer(reader.blob.slice(offset, offset + length));
221
- });
222
- }
204
+ return new Uint8Array(await reader.blob.slice(offset, offset + length).arrayBuffer());
223
205
  }
224
206
  }
225
207
 
@@ -267,9 +249,12 @@ function flushArrayBuffers(blobWriter) {
267
249
 
268
250
  class ReadableStreamReader {
269
251
 
270
- constructor(readableStream) {
252
+ constructor(readable) {
271
253
  const reader = this;
272
- reader.readableStream = readableStream.pipeThrough(new TransformStream({
254
+ if (!readable) {
255
+ readable = new ReadableStream({});
256
+ }
257
+ reader.readable = readable.pipeThrough(new TransformStream({
273
258
  transform(chunk, controller) {
274
259
  if (chunk) {
275
260
  reader.size += chunk.length;
@@ -285,14 +270,18 @@ class ReadableStreamReader {
285
270
  }
286
271
 
287
272
  getReadable() {
288
- return this.readableStream;
273
+ return this.readable;
289
274
  }
290
275
  }
291
276
 
292
277
  class WritableStreamWriter extends Writer {
293
278
 
294
- constructor(writable) {
279
+ constructor(writable, { preventClose } = {}) {
295
280
  super();
281
+ this.preventClose = preventClose;
282
+ if (!writable) {
283
+ writable = new WritableStream({});
284
+ }
296
285
  Object.defineProperty(this, "writable", {
297
286
  get() {
298
287
  return writable;
@@ -300,16 +289,13 @@ class WritableStreamWriter extends Writer {
300
289
  });
301
290
  }
302
291
 
303
- async writeUint8Array(array) {
304
- await this.writer.ready;
305
- const writer = this.writable.getWriter();
306
- await writer.write(array);
307
- writer.releaseLock();
308
- }
309
-
310
292
  async getData() {
311
- await this.writable.getWriter().close();
312
- return this.writable;
293
+ const writer = this;
294
+ const { writable } = writer;
295
+ if (!writer.preventClose) {
296
+ await writable.getWriter().close();
297
+ }
298
+ return writable;
313
299
  }
314
300
  }
315
301
 
@@ -543,7 +529,7 @@ class Uint8ArrayWriter extends Writer {
543
529
 
544
530
  constructor() {
545
531
  super();
546
- this.array = new Uint8Array(0);
532
+ this.array = new Uint8Array();
547
533
  }
548
534
 
549
535
  writeUint8Array(array) {
@@ -74,7 +74,7 @@ class AESDecryptionStream extends TransformStream {
74
74
  password,
75
75
  signed,
76
76
  strength: strength - 1,
77
- pending: new Uint8Array(0)
77
+ pending: new Uint8Array()
78
78
  });
79
79
  },
80
80
  async transform(chunk, controller) {
@@ -100,7 +100,7 @@ class AESDecryptionStream extends TransformStream {
100
100
  const pending = aesCrypto.pending;
101
101
  const chunkToDecrypt = subarray(pending, 0, pending.length - SIGNATURE_LENGTH);
102
102
  const originalSignature = subarray(pending, pending.length - SIGNATURE_LENGTH);
103
- let decryptedChunkArray = new Uint8Array(0);
103
+ let decryptedChunkArray = new Uint8Array();
104
104
  if (chunkToDecrypt.length) {
105
105
  const encryptedChunk = toBits(codecBytes, chunkToDecrypt);
106
106
  aesCrypto.hmac.update(encryptedChunk);
@@ -133,12 +133,12 @@ class AESEncryptionStream extends TransformStream {
133
133
  ready: new Promise(resolve => this.resolveReady = resolve),
134
134
  password,
135
135
  strength: strength - 1,
136
- pending: new Uint8Array(0)
136
+ pending: new Uint8Array()
137
137
  });
138
138
  },
139
139
  async transform(chunk, controller) {
140
140
  const aesCrypto = this;
141
- let preamble = new Uint8Array(0);
141
+ let preamble = new Uint8Array();
142
142
  if (aesCrypto.password) {
143
143
  const password = aesCrypto.password;
144
144
  aesCrypto.password = null;
@@ -156,7 +156,7 @@ class AESEncryptionStream extends TransformStream {
156
156
  async flush(controller) {
157
157
  const aesCrypto = this;
158
158
  await aesCrypto.ready;
159
- let encryptedChunkArray = new Uint8Array(0);
159
+ let encryptedChunkArray = new Uint8Array();
160
160
  if (aesCrypto.pending.length) {
161
161
  const encryptedChunk = aesCrypto.ctr.update(toBits(codecBytes, aesCrypto.pending));
162
162
  aesCrypto.hmac.update(encryptedChunk);
@@ -2029,7 +2029,7 @@ const ZipDeflate = (() => {
2029
2029
  bufferIndex += chunk.length;
2030
2030
  });
2031
2031
  } else {
2032
- array = buffers[0] || new Uint8Array(0);
2032
+ array = buffers[0] || new Uint8Array();
2033
2033
  }
2034
2034
  return array;
2035
2035
  };
@@ -2155,7 +2155,7 @@ const ZipInflate = (() => {
2155
2155
  bufferIndex += chunk.length;
2156
2156
  });
2157
2157
  } else {
2158
- array = buffers[0] || new Uint8Array(0);
2158
+ array = buffers[0] || new Uint8Array();
2159
2159
  }
2160
2160
  return array;
2161
2161
  };
@@ -26,7 +26,7 @@
26
26
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
27
  */
28
28
 
29
- /* global TextDecoder, FileReader, Blob */
29
+ /* global TextDecoder */
30
30
 
31
31
  import decodeCP437 from "./cp437-decode.js";
32
32
 
@@ -35,13 +35,6 @@ export default decodeText;
35
35
  function decodeText(value, encoding) {
36
36
  if (encoding && encoding.trim().toLowerCase() == "cp437") {
37
37
  return decodeCP437(value);
38
- } else if (typeof TextDecoder == "undefined") {
39
- const fileReader = new FileReader();
40
- return new Promise((resolve, reject) => {
41
- fileReader.onload = event => resolve(event.target.result);
42
- fileReader.onerror = () => reject(fileReader.error);
43
- fileReader.readAsText(new Blob([value]));
44
- });
45
38
  } else {
46
39
  return new TextDecoder(encoding).decode(value);
47
40
  }
@@ -41,7 +41,7 @@ function createCodecClass(constructor, constructorOptions, registerDataHandler)
41
41
  const codecAdapter = this;
42
42
  const onData = data => {
43
43
  if (codecAdapter.pendingData) {
44
- const pendingData = codecAdapter.pendingData;
44
+ const { pendingData } = codecAdapter;
45
45
  codecAdapter.pendingData = new Uint8Array(pendingData.length + data.length);
46
46
  codecAdapter.pendingData.set(pendingData, 0);
47
47
  codecAdapter.pendingData.set(data, pendingData.length);
@@ -61,7 +61,7 @@ function createCodecClass(constructor, constructorOptions, registerDataHandler)
61
61
  return getResponse(this);
62
62
  }
63
63
  flush() {
64
- this.codec.push(new Uint8Array(0), true);
64
+ this.codec.push(new Uint8Array(), true);
65
65
  return getResponse(this);
66
66
  }
67
67
  };
@@ -72,7 +72,7 @@ function createCodecClass(constructor, constructorOptions, registerDataHandler)
72
72
  codec.pendingData = null;
73
73
  return output;
74
74
  } else {
75
- return new Uint8Array(0);
75
+ return new Uint8Array();
76
76
  }
77
77
  }
78
78
  }
@@ -46,15 +46,15 @@ let messageId = 0;
46
46
 
47
47
  addEventListener(MESSAGE_EVENT_TYPE, async event => {
48
48
  const message = event.data;
49
- const type = message.type;
49
+ const { type, messageId, data, done } = message;
50
50
  try {
51
51
  if (type == MESSAGE_START) {
52
52
  init(message);
53
53
  }
54
54
  if (type == MESSAGE_DATA) {
55
- const { resolve } = pendingMessages.get(message.messageId);
56
- pendingMessages.delete(message.messageId);
57
- resolve({ value: new Uint8Array(message.data), done: message.done });
55
+ const { resolve } = pendingMessages.get(messageId);
56
+ pendingMessages.delete(messageId);
57
+ resolve({ value: new Uint8Array(data), done });
58
58
  }
59
59
  if (type == MESSAGE_ABORT) {
60
60
  codec.abort();
@@ -66,45 +66,45 @@ addEventListener(MESSAGE_EVENT_TYPE, async event => {
66
66
 
67
67
  async function init(message) {
68
68
  try {
69
- if (message.scripts && message.scripts.length) {
70
- importScripts.apply(undefined, message.scripts);
69
+ const { options, scripts, config } = message;
70
+ const { codecType } = options;
71
+ if (scripts && scripts.length) {
72
+ importScripts.apply(undefined, scripts);
71
73
  }
72
- const options = message.options;
73
74
  if (self.initCodec) {
74
75
  self.initCodec();
75
76
  }
76
77
  let codecConstructor;
77
- if (options.codecType.startsWith(CODEC_DEFLATE)) {
78
+ if (codecType.startsWith(CODEC_DEFLATE)) {
78
79
  codecConstructor = self.Deflate;
79
- } else if (options.codecType.startsWith(CODEC_INFLATE)) {
80
+ } else if (codecType.startsWith(CODEC_INFLATE)) {
80
81
  codecConstructor = self.Inflate;
81
82
  }
83
+ const strategy = { highWaterMark: 1, size: () => config.chunkSize };
82
84
  const readable = new ReadableStream({
83
85
  async pull(controller) {
84
86
  let result = new Promise((resolve, reject) => pendingMessages.set(messageId, { resolve, reject }));
85
87
  sendMessage({ type: MESSAGE_PULL, messageId });
86
- messageId++;
88
+ messageId = (messageId + 1) % Number.MAX_SAFE_INTEGER;
87
89
  const { value, done } = await result;
88
- // debugger;
89
90
  controller.enqueue(value);
90
91
  if (done) {
91
92
  controller.close();
92
93
  }
93
94
  }
94
- }, {
95
- highWaterMark: 1
96
- });
95
+ }, strategy);
97
96
  const writable = new WritableStream({
98
97
  write(data) {
99
98
  sendMessage({ type: MESSAGE_DATA, data });
100
99
  }
101
- });
102
- codec = createCodec(codecConstructor, readable, writable, options, message.config);
100
+ }, strategy);
101
+ codec = createCodec(codecConstructor, readable, writable, options, config);
103
102
  const result = await codec.run();
104
103
  sendMessage({ type: MESSAGE_CLOSE, result });
105
104
 
106
105
  } catch (error) {
107
- postMessage({ error: { message: error.message, stack: error.stack } });
106
+ const { message, stack } = error;
107
+ postMessage({ error: { message, stack } });
108
108
  }
109
109
  }
110
110