@helloao/cli 0.0.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.
Files changed (110) hide show
  1. package/actions.d.ts +57 -0
  2. package/actions.js +262 -0
  3. package/cli.d.ts +2 -0
  4. package/cli.js +139 -0
  5. package/db.d.ts +110 -0
  6. package/db.js +754 -0
  7. package/downloads.d.ts +2 -0
  8. package/downloads.js +12 -0
  9. package/files.d.ts +56 -0
  10. package/files.js +232 -0
  11. package/index.d.ts +8 -0
  12. package/index.js +38 -0
  13. package/migrations/20240420231455_initial/migration.sql +66 -0
  14. package/migrations/20240623183848_add_book_order/migration.sql +26 -0
  15. package/migrations/20240629194121_add_chapter_links/migration.sql +45 -0
  16. package/migrations/20240629194513_add_chapter_content/migration.sql +30 -0
  17. package/migrations/20240705221833_remove_unused_columns/migration.sql +27 -0
  18. package/migrations/20240711173108_add_chapter_audio/migration.sql +13 -0
  19. package/migrations/20240724212651_add_hashing/migration.sql +25 -0
  20. package/node_modules/@zip.js/zip.js/LICENSE +28 -0
  21. package/node_modules/@zip.js/zip.js/README.md +173 -0
  22. package/node_modules/@zip.js/zip.js/deno.json +8 -0
  23. package/node_modules/@zip.js/zip.js/dist/README.md +28 -0
  24. package/node_modules/@zip.js/zip.js/dist/z-worker-fflate.js +1 -0
  25. package/node_modules/@zip.js/zip.js/dist/z-worker-pako.js +1 -0
  26. package/node_modules/@zip.js/zip.js/dist/z-worker.js +1 -0
  27. package/node_modules/@zip.js/zip.js/dist/zip-fs-full.js +11935 -0
  28. package/node_modules/@zip.js/zip.js/dist/zip-fs-full.min.js +1 -0
  29. package/node_modules/@zip.js/zip.js/dist/zip-fs.js +6079 -0
  30. package/node_modules/@zip.js/zip.js/dist/zip-fs.min.js +1 -0
  31. package/node_modules/@zip.js/zip.js/dist/zip-full.js +9463 -0
  32. package/node_modules/@zip.js/zip.js/dist/zip-full.min.js +1 -0
  33. package/node_modules/@zip.js/zip.js/dist/zip-no-worker-deflate.min.js +1 -0
  34. package/node_modules/@zip.js/zip.js/dist/zip-no-worker-inflate.min.js +1 -0
  35. package/node_modules/@zip.js/zip.js/dist/zip-no-worker.min.js +1 -0
  36. package/node_modules/@zip.js/zip.js/dist/zip.js +5240 -0
  37. package/node_modules/@zip.js/zip.js/dist/zip.min.js +1 -0
  38. package/node_modules/@zip.js/zip.js/index-fflate.js +82 -0
  39. package/node_modules/@zip.js/zip.js/index.cjs +11927 -0
  40. package/node_modules/@zip.js/zip.js/index.d.ts +2048 -0
  41. package/node_modules/@zip.js/zip.js/index.js +87 -0
  42. package/node_modules/@zip.js/zip.js/index.min.js +1 -0
  43. package/node_modules/@zip.js/zip.js/lib/core/codec-pool.js +127 -0
  44. package/node_modules/@zip.js/zip.js/lib/core/codec-worker.js +348 -0
  45. package/node_modules/@zip.js/zip.js/lib/core/configuration.js +127 -0
  46. package/node_modules/@zip.js/zip.js/lib/core/constants.js +114 -0
  47. package/node_modules/@zip.js/zip.js/lib/core/io.js +749 -0
  48. package/node_modules/@zip.js/zip.js/lib/core/streams/aes-crypto-stream.js +326 -0
  49. package/node_modules/@zip.js/zip.js/lib/core/streams/codec-stream.js +154 -0
  50. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/crc32.js +63 -0
  51. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/deflate.js +2063 -0
  52. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/inflate.js +2167 -0
  53. package/node_modules/@zip.js/zip.js/lib/core/streams/codecs/sjcl.js +827 -0
  54. package/node_modules/@zip.js/zip.js/lib/core/streams/common-crypto.js +55 -0
  55. package/node_modules/@zip.js/zip.js/lib/core/streams/crc32-stream.js +56 -0
  56. package/node_modules/@zip.js/zip.js/lib/core/streams/stream-adapter.js +55 -0
  57. package/node_modules/@zip.js/zip.js/lib/core/streams/zip-crypto-stream.js +162 -0
  58. package/node_modules/@zip.js/zip.js/lib/core/streams/zip-entry-stream.js +165 -0
  59. package/node_modules/@zip.js/zip.js/lib/core/util/cp437-decode.js +48 -0
  60. package/node_modules/@zip.js/zip.js/lib/core/util/decode-text.js +43 -0
  61. package/node_modules/@zip.js/zip.js/lib/core/util/default-mime-type.js +38 -0
  62. package/node_modules/@zip.js/zip.js/lib/core/util/encode-text.js +48 -0
  63. package/node_modules/@zip.js/zip.js/lib/core/util/mime-type.js +1639 -0
  64. package/node_modules/@zip.js/zip.js/lib/core/util/stream-codec-shim.js +91 -0
  65. package/node_modules/@zip.js/zip.js/lib/core/z-worker-core.js +176 -0
  66. package/node_modules/@zip.js/zip.js/lib/core/zip-entry.js +86 -0
  67. package/node_modules/@zip.js/zip.js/lib/core/zip-fs-core.js +865 -0
  68. package/node_modules/@zip.js/zip.js/lib/core/zip-reader.js +757 -0
  69. package/node_modules/@zip.js/zip.js/lib/core/zip-writer.js +1186 -0
  70. package/node_modules/@zip.js/zip.js/lib/z-worker-bootstrap-fflate.js +40 -0
  71. package/node_modules/@zip.js/zip.js/lib/z-worker-bootstrap-pako.js +39 -0
  72. package/node_modules/@zip.js/zip.js/lib/z-worker-fflate.js +40 -0
  73. package/node_modules/@zip.js/zip.js/lib/z-worker-inline-template.js +42 -0
  74. package/node_modules/@zip.js/zip.js/lib/z-worker-inline.js +1 -0
  75. package/node_modules/@zip.js/zip.js/lib/z-worker.js +38 -0
  76. package/node_modules/@zip.js/zip.js/lib/zip-data-uri.js +53 -0
  77. package/node_modules/@zip.js/zip.js/lib/zip-fflate-shim.js +37 -0
  78. package/node_modules/@zip.js/zip.js/lib/zip-fs.js +53 -0
  79. package/node_modules/@zip.js/zip.js/lib/zip-full-fflate.js +53 -0
  80. package/node_modules/@zip.js/zip.js/lib/zip-full.js +54 -0
  81. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-deflate.js +42 -0
  82. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate-deflate.js +42 -0
  83. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate-inflate.js +42 -0
  84. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-fflate.js +43 -0
  85. package/node_modules/@zip.js/zip.js/lib/zip-no-worker-inflate.js +42 -0
  86. package/node_modules/@zip.js/zip.js/lib/zip-no-worker.js +44 -0
  87. package/node_modules/@zip.js/zip.js/lib/zip.js +52 -0
  88. package/node_modules/@zip.js/zip.js/package.json +86 -0
  89. package/package.json +43 -0
  90. package/prisma-gen/default.d.ts +1 -0
  91. package/prisma-gen/default.js +1 -0
  92. package/prisma-gen/edge.d.ts +1 -0
  93. package/prisma-gen/edge.js +242 -0
  94. package/prisma-gen/index-browser.js +236 -0
  95. package/prisma-gen/index.d.ts +13248 -0
  96. package/prisma-gen/index.js +265 -0
  97. package/prisma-gen/runtime/edge-esm.js +28 -0
  98. package/prisma-gen/runtime/edge.js +28 -0
  99. package/prisma-gen/runtime/index-browser.d.ts +365 -0
  100. package/prisma-gen/runtime/index-browser.js +13 -0
  101. package/prisma-gen/runtime/library.d.ts +3168 -0
  102. package/prisma-gen/runtime/library.js +140 -0
  103. package/prisma-gen/runtime/wasm.js +29 -0
  104. package/prisma-gen/wasm.d.ts +1 -0
  105. package/prisma-gen/wasm.js +236 -0
  106. package/s3.d.ts +14 -0
  107. package/s3.js +76 -0
  108. package/schema.prisma +154 -0
  109. package/uploads.d.ts +54 -0
  110. package/uploads.js +141 -0
@@ -0,0 +1,127 @@
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 setTimeout, clearTimeout */
30
+
31
+ import { UNDEFINED_VALUE } from "./constants.js";
32
+ import {
33
+ CODEC_INFLATE,
34
+ CODEC_DEFLATE,
35
+ ERR_INVALID_SIGNATURE,
36
+ ERR_INVALID_PASSWORD,
37
+ ERR_ABORT_CHECK_PASSWORD
38
+ } from "./streams/codec-stream.js";
39
+ import { CodecWorker } from "./codec-worker.js";
40
+
41
+ let pool = [];
42
+ const pendingRequests = [];
43
+
44
+ export {
45
+ runWorker,
46
+ terminateWorkers,
47
+ CODEC_DEFLATE,
48
+ CODEC_INFLATE,
49
+ ERR_INVALID_SIGNATURE,
50
+ ERR_INVALID_PASSWORD,
51
+ ERR_ABORT_CHECK_PASSWORD
52
+ };
53
+
54
+ let indexWorker = 0;
55
+
56
+ async function runWorker(stream, workerOptions) {
57
+ const { options, config } = workerOptions;
58
+ const { transferStreams, useWebWorkers, useCompressionStream, codecType, compressed, signed, encrypted } = options;
59
+ const { workerScripts, maxWorkers } = config;
60
+ workerOptions.transferStreams = transferStreams || transferStreams === UNDEFINED_VALUE;
61
+ const streamCopy = !compressed && !signed && !encrypted && !workerOptions.transferStreams;
62
+ workerOptions.useWebWorkers = !streamCopy && (useWebWorkers || (useWebWorkers === UNDEFINED_VALUE && config.useWebWorkers));
63
+ workerOptions.scripts = workerOptions.useWebWorkers && workerScripts ? workerScripts[codecType] : [];
64
+ options.useCompressionStream = useCompressionStream || (useCompressionStream === UNDEFINED_VALUE && config.useCompressionStream);
65
+ return (await getWorker()).run();
66
+
67
+ async function getWorker() {
68
+ const workerData = pool.find(workerData => !workerData.busy);
69
+ if (workerData) {
70
+ clearTerminateTimeout(workerData);
71
+ return new CodecWorker(workerData, stream, workerOptions, onTaskFinished);
72
+ } else if (pool.length < maxWorkers) {
73
+ const workerData = { indexWorker };
74
+ indexWorker++;
75
+ pool.push(workerData);
76
+ return new CodecWorker(workerData, stream, workerOptions, onTaskFinished);
77
+ } else {
78
+ return new Promise(resolve => pendingRequests.push({ resolve, stream, workerOptions }));
79
+ }
80
+ }
81
+
82
+ function onTaskFinished(workerData) {
83
+ if (pendingRequests.length) {
84
+ const [{ resolve, stream, workerOptions }] = pendingRequests.splice(0, 1);
85
+ resolve(new CodecWorker(workerData, stream, workerOptions, onTaskFinished));
86
+ } else if (workerData.worker) {
87
+ clearTerminateTimeout(workerData);
88
+ terminateWorker(workerData, workerOptions);
89
+ } else {
90
+ pool = pool.filter(data => data != workerData);
91
+ }
92
+ }
93
+ }
94
+
95
+ function terminateWorker(workerData, workerOptions) {
96
+ const { config } = workerOptions;
97
+ const { terminateWorkerTimeout } = config;
98
+ if (Number.isFinite(terminateWorkerTimeout) && terminateWorkerTimeout >= 0) {
99
+ if (workerData.terminated) {
100
+ workerData.terminated = false;
101
+ } else {
102
+ workerData.terminateTimeout = setTimeout(async () => {
103
+ pool = pool.filter(data => data != workerData);
104
+ try {
105
+ await workerData.terminate();
106
+ } catch (_error) {
107
+ // ignored
108
+ }
109
+ }, terminateWorkerTimeout);
110
+ }
111
+ }
112
+ }
113
+
114
+ function clearTerminateTimeout(workerData) {
115
+ const { terminateTimeout } = workerData;
116
+ if (terminateTimeout) {
117
+ clearTimeout(terminateTimeout);
118
+ workerData.terminateTimeout = null;
119
+ }
120
+ }
121
+
122
+ async function terminateWorkers() {
123
+ await Promise.allSettled(pool.map(workerData => {
124
+ clearTerminateTimeout(workerData);
125
+ return workerData.terminate();
126
+ }));
127
+ }
@@ -0,0 +1,348 @@
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
+ ChunkStream,
38
+ MESSAGE_EVENT_TYPE,
39
+ MESSAGE_START,
40
+ MESSAGE_PULL,
41
+ MESSAGE_DATA,
42
+ MESSAGE_ACK_DATA,
43
+ MESSAGE_CLOSE
44
+ } from "./streams/codec-stream.js";
45
+
46
+ // deno-lint-ignore valid-typeof
47
+ let WEB_WORKERS_SUPPORTED = typeof Worker != UNDEFINED_TYPE;
48
+
49
+ export {
50
+ CodecWorker
51
+ };
52
+
53
+ class CodecWorker {
54
+
55
+ constructor(workerData, { readable, writable }, { options, config, streamOptions, useWebWorkers, transferStreams, scripts }, onTaskFinished) {
56
+ const { signal } = streamOptions;
57
+ Object.assign(workerData, {
58
+ busy: true,
59
+ readable: readable
60
+ .pipeThrough(new ChunkStream(config.chunkSize))
61
+ .pipeThrough(new ProgressWatcherStream(readable, streamOptions), { signal }),
62
+ writable,
63
+ options: Object.assign({}, options),
64
+ scripts,
65
+ transferStreams,
66
+ terminate() {
67
+ return new Promise(resolve => {
68
+ const { worker, busy } = workerData;
69
+ if (worker) {
70
+ if (busy) {
71
+ workerData.resolveTerminated = resolve;
72
+ } else {
73
+ worker.terminate();
74
+ resolve();
75
+ }
76
+ workerData.interface = null;
77
+ } else {
78
+ resolve();
79
+ }
80
+ });
81
+ },
82
+ onTaskFinished() {
83
+ const { resolveTerminated } = workerData;
84
+ if (resolveTerminated) {
85
+ workerData.resolveTerminated = null;
86
+ workerData.terminated = true;
87
+ workerData.worker.terminate();
88
+ resolveTerminated();
89
+ }
90
+ workerData.busy = false;
91
+ onTaskFinished(workerData);
92
+ }
93
+ });
94
+ return (useWebWorkers && WEB_WORKERS_SUPPORTED ? createWebWorkerInterface : createWorkerInterface)(workerData, config);
95
+ }
96
+ }
97
+
98
+ class ProgressWatcherStream extends TransformStream {
99
+
100
+ constructor(readableSource, { onstart, onprogress, size, onend }) {
101
+ let chunkOffset = 0;
102
+ super({
103
+ async start() {
104
+ if (onstart) {
105
+ await callHandler(onstart, size);
106
+ }
107
+ },
108
+ async transform(chunk, controller) {
109
+ chunkOffset += chunk.length;
110
+ if (onprogress) {
111
+ await callHandler(onprogress, chunkOffset, size);
112
+ }
113
+ controller.enqueue(chunk);
114
+ },
115
+ async flush() {
116
+ readableSource.size = chunkOffset;
117
+ if (onend) {
118
+ await callHandler(onend, chunkOffset);
119
+ }
120
+ }
121
+ });
122
+ }
123
+ }
124
+
125
+ async function callHandler(handler, ...parameters) {
126
+ try {
127
+ await handler(...parameters);
128
+ } catch (_error) {
129
+ // ignored
130
+ }
131
+ }
132
+
133
+ function createWorkerInterface(workerData, config) {
134
+ return {
135
+ run: () => runWorker(workerData, config)
136
+ };
137
+ }
138
+
139
+ function createWebWorkerInterface(workerData, config) {
140
+ const { baseURL, chunkSize } = config;
141
+ if (!workerData.interface) {
142
+ let worker;
143
+ try {
144
+ worker = getWebWorker(workerData.scripts[0], baseURL, workerData);
145
+ } catch (error) {
146
+ WEB_WORKERS_SUPPORTED = false;
147
+ return createWorkerInterface(workerData, config);
148
+ }
149
+ Object.assign(workerData, {
150
+ worker,
151
+ interface: {
152
+ run: () => runWebWorker(workerData, { chunkSize })
153
+ }
154
+ });
155
+ }
156
+ return workerData.interface;
157
+ }
158
+
159
+ async function runWorker({ options, readable, writable, onTaskFinished }, config) {
160
+ try {
161
+ const codecStream = new CodecStream(options, config);
162
+ await readable.pipeThrough(codecStream).pipeTo(writable, { preventClose: true, preventAbort: true });
163
+ const {
164
+ signature,
165
+ inputSize,
166
+ outputSize
167
+ } = codecStream;
168
+ return {
169
+ signature,
170
+ inputSize,
171
+ outputSize
172
+ };
173
+ } finally {
174
+ onTaskFinished();
175
+ }
176
+ }
177
+
178
+ async function runWebWorker(workerData, config) {
179
+ let resolveResult, rejectResult;
180
+ const result = new Promise((resolve, reject) => {
181
+ resolveResult = resolve;
182
+ rejectResult = reject;
183
+ });
184
+ Object.assign(workerData, {
185
+ reader: null,
186
+ writer: null,
187
+ resolveResult,
188
+ rejectResult,
189
+ result
190
+ });
191
+ const { readable, options, scripts } = workerData;
192
+ const { writable, closed } = watchClosedStream(workerData.writable);
193
+ const streamsTransferred = sendMessage({
194
+ type: MESSAGE_START,
195
+ scripts: scripts.slice(1),
196
+ options,
197
+ config,
198
+ readable,
199
+ writable
200
+ }, workerData);
201
+ if (!streamsTransferred) {
202
+ Object.assign(workerData, {
203
+ reader: readable.getReader(),
204
+ writer: writable.getWriter()
205
+ });
206
+ }
207
+ const resultValue = await result;
208
+ if (!streamsTransferred) {
209
+ await writable.getWriter().close();
210
+ }
211
+ await closed;
212
+ return resultValue;
213
+ }
214
+
215
+ function watchClosedStream(writableSource) {
216
+ let resolveStreamClosed;
217
+ const closed = new Promise(resolve => resolveStreamClosed = resolve);
218
+ const writable = new WritableStream({
219
+ async write(chunk) {
220
+ const writer = writableSource.getWriter();
221
+ await writer.ready;
222
+ await writer.write(chunk);
223
+ writer.releaseLock();
224
+ },
225
+ close() {
226
+ resolveStreamClosed();
227
+ },
228
+ abort(reason) {
229
+ const writer = writableSource.getWriter();
230
+ return writer.abort(reason);
231
+ }
232
+ });
233
+ return { writable, closed };
234
+ }
235
+
236
+ let classicWorkersSupported = true;
237
+ let transferStreamsSupported = true;
238
+
239
+ function getWebWorker(url, baseURL, workerData) {
240
+ const workerOptions = { type: "module" };
241
+ let scriptUrl, worker;
242
+ // deno-lint-ignore valid-typeof
243
+ if (typeof url == FUNCTION_TYPE) {
244
+ url = url();
245
+ }
246
+ try {
247
+ scriptUrl = new URL(url, baseURL);
248
+ } catch (_error) {
249
+ scriptUrl = url;
250
+ }
251
+ if (classicWorkersSupported) {
252
+ try {
253
+ worker = new Worker(scriptUrl);
254
+ } catch (_error) {
255
+ classicWorkersSupported = false;
256
+ worker = new Worker(scriptUrl, workerOptions);
257
+ }
258
+ } else {
259
+ worker = new Worker(scriptUrl, workerOptions);
260
+ }
261
+ worker.addEventListener(MESSAGE_EVENT_TYPE, event => onMessage(event, workerData));
262
+ return worker;
263
+ }
264
+
265
+ function sendMessage(message, { worker, writer, onTaskFinished, transferStreams }) {
266
+ try {
267
+ let { value, readable, writable } = message;
268
+ const transferables = [];
269
+ if (value) {
270
+ if (value.byteLength < value.buffer.byteLength) {
271
+ message.value = value.buffer.slice(0, value.byteLength);
272
+ }
273
+ else {
274
+ message.value = value.buffer;
275
+ }
276
+ transferables.push(message.value);
277
+ }
278
+ if (transferStreams && transferStreamsSupported) {
279
+ if (readable) {
280
+ transferables.push(readable);
281
+ }
282
+ if (writable) {
283
+ transferables.push(writable);
284
+ }
285
+ } else {
286
+ message.readable = message.writable = null;
287
+ }
288
+ if (transferables.length) {
289
+ try {
290
+ worker.postMessage(message, transferables);
291
+ return true;
292
+ } catch (_error) {
293
+ transferStreamsSupported = false;
294
+ message.readable = message.writable = null;
295
+ worker.postMessage(message);
296
+ }
297
+ } else {
298
+ worker.postMessage(message);
299
+ }
300
+ } catch (error) {
301
+ if (writer) {
302
+ writer.releaseLock();
303
+ }
304
+ onTaskFinished();
305
+ throw error;
306
+ }
307
+ }
308
+
309
+ async function onMessage({ data }, workerData) {
310
+ const { type, value, messageId, result, error } = data;
311
+ const { reader, writer, resolveResult, rejectResult, onTaskFinished } = workerData;
312
+ try {
313
+ if (error) {
314
+ const { message, stack, code, name } = error;
315
+ const responseError = new Error(message);
316
+ Object.assign(responseError, { stack, code, name });
317
+ close(responseError);
318
+ } else {
319
+ if (type == MESSAGE_PULL) {
320
+ const { value, done } = await reader.read();
321
+ sendMessage({ type: MESSAGE_DATA, value, done, messageId }, workerData);
322
+ }
323
+ if (type == MESSAGE_DATA) {
324
+ await writer.ready;
325
+ await writer.write(new Uint8Array(value));
326
+ sendMessage({ type: MESSAGE_ACK_DATA, messageId }, workerData);
327
+ }
328
+ if (type == MESSAGE_CLOSE) {
329
+ close(null, result);
330
+ }
331
+ }
332
+ } catch (error) {
333
+ sendMessage({ type: MESSAGE_CLOSE, messageId }, workerData);
334
+ close(error);
335
+ }
336
+
337
+ function close(error, result) {
338
+ if (error) {
339
+ rejectResult(error);
340
+ } else {
341
+ resolveResult(result);
342
+ }
343
+ if (writer) {
344
+ writer.releaseLock();
345
+ }
346
+ onTaskFinished();
347
+ }
348
+ }
@@ -0,0 +1,127 @@
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 navigator, CompressionStream, DecompressionStream */
30
+
31
+ import {
32
+ UNDEFINED_VALUE,
33
+ UNDEFINED_TYPE
34
+ } from "./constants.js";
35
+ import { StreamAdapter } from "./streams/stream-adapter.js";
36
+
37
+ const MINIMUM_CHUNK_SIZE = 64;
38
+ let maxWorkers = 2;
39
+ try {
40
+ if (typeof navigator != UNDEFINED_TYPE && navigator.hardwareConcurrency) {
41
+ maxWorkers = navigator.hardwareConcurrency;
42
+ }
43
+ } catch (_error) {
44
+ // ignored
45
+ }
46
+ const DEFAULT_CONFIGURATION = {
47
+ chunkSize: 512 * 1024,
48
+ maxWorkers,
49
+ terminateWorkerTimeout: 5000,
50
+ useWebWorkers: true,
51
+ useCompressionStream: true,
52
+ workerScripts: UNDEFINED_VALUE,
53
+ CompressionStreamNative: typeof CompressionStream != UNDEFINED_TYPE && CompressionStream,
54
+ DecompressionStreamNative: typeof DecompressionStream != UNDEFINED_TYPE && DecompressionStream
55
+ };
56
+
57
+ const config = Object.assign({}, DEFAULT_CONFIGURATION);
58
+
59
+ export {
60
+ configure,
61
+ getConfiguration,
62
+ getChunkSize
63
+ };
64
+
65
+ function getConfiguration() {
66
+ return config;
67
+ }
68
+
69
+ function getChunkSize(config) {
70
+ return Math.max(config.chunkSize, MINIMUM_CHUNK_SIZE);
71
+ }
72
+
73
+ function configure(configuration) {
74
+ const {
75
+ baseURL,
76
+ chunkSize,
77
+ maxWorkers,
78
+ terminateWorkerTimeout,
79
+ useCompressionStream,
80
+ useWebWorkers,
81
+ Deflate,
82
+ Inflate,
83
+ CompressionStream,
84
+ DecompressionStream,
85
+ workerScripts
86
+ } = configuration;
87
+ setIfDefined("baseURL", baseURL);
88
+ setIfDefined("chunkSize", chunkSize);
89
+ setIfDefined("maxWorkers", maxWorkers);
90
+ setIfDefined("terminateWorkerTimeout", terminateWorkerTimeout);
91
+ setIfDefined("useCompressionStream", useCompressionStream);
92
+ setIfDefined("useWebWorkers", useWebWorkers);
93
+ if (Deflate) {
94
+ config.CompressionStream = new StreamAdapter(Deflate);
95
+ }
96
+ if (Inflate) {
97
+ config.DecompressionStream = new StreamAdapter(Inflate);
98
+ }
99
+ setIfDefined("CompressionStream", CompressionStream);
100
+ setIfDefined("DecompressionStream", DecompressionStream);
101
+ if (workerScripts !== UNDEFINED_VALUE) {
102
+ const { deflate, inflate } = workerScripts;
103
+ if (deflate || inflate) {
104
+ if (!config.workerScripts) {
105
+ config.workerScripts = {};
106
+ }
107
+ }
108
+ if (deflate) {
109
+ if (!Array.isArray(deflate)) {
110
+ throw new Error("workerScripts.deflate must be an array");
111
+ }
112
+ config.workerScripts.deflate = deflate;
113
+ }
114
+ if (inflate) {
115
+ if (!Array.isArray(inflate)) {
116
+ throw new Error("workerScripts.inflate must be an array");
117
+ }
118
+ config.workerScripts.inflate = inflate;
119
+ }
120
+ }
121
+ }
122
+
123
+ function setIfDefined(propertyName, propertyValue) {
124
+ if (propertyValue !== UNDEFINED_VALUE) {
125
+ config[propertyName] = propertyValue;
126
+ }
127
+ }
@@ -0,0 +1,114 @@
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 MAX_32_BITS = 0xffffffff;
30
+ const MAX_16_BITS = 0xffff;
31
+ const COMPRESSION_METHOD_DEFLATE = 0x08;
32
+ const COMPRESSION_METHOD_STORE = 0x00;
33
+ const COMPRESSION_METHOD_AES = 0x63;
34
+
35
+ const LOCAL_FILE_HEADER_SIGNATURE = 0x04034b50;
36
+ const SPLIT_ZIP_FILE_SIGNATURE = 0x08074b50;
37
+ const DATA_DESCRIPTOR_RECORD_SIGNATURE = SPLIT_ZIP_FILE_SIGNATURE;
38
+ const CENTRAL_FILE_HEADER_SIGNATURE = 0x02014b50;
39
+ const END_OF_CENTRAL_DIR_SIGNATURE = 0x06054b50;
40
+ const ZIP64_END_OF_CENTRAL_DIR_SIGNATURE = 0x06064b50;
41
+ const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE = 0x07064b50;
42
+ const END_OF_CENTRAL_DIR_LENGTH = 22;
43
+ const ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH = 20;
44
+ const ZIP64_END_OF_CENTRAL_DIR_LENGTH = 56;
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;
46
+
47
+ const EXTRAFIELD_TYPE_ZIP64 = 0x0001;
48
+ const EXTRAFIELD_TYPE_AES = 0x9901;
49
+ const EXTRAFIELD_TYPE_NTFS = 0x000a;
50
+ const EXTRAFIELD_TYPE_NTFS_TAG1 = 0x0001;
51
+ const EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP = 0x5455;
52
+ const EXTRAFIELD_TYPE_UNICODE_PATH = 0x7075;
53
+ const EXTRAFIELD_TYPE_UNICODE_COMMENT = 0x6375;
54
+ const EXTRAFIELD_TYPE_USDZ = 0x1986;
55
+
56
+ const BITFLAG_ENCRYPTED = 0x01;
57
+ const BITFLAG_LEVEL = 0x06;
58
+ const BITFLAG_DATA_DESCRIPTOR = 0x0008;
59
+ const BITFLAG_LANG_ENCODING_FLAG = 0x0800;
60
+ const FILE_ATTR_MSDOS_DIR_MASK = 0x10;
61
+
62
+ const VERSION_DEFLATE = 0x14;
63
+ const VERSION_ZIP64 = 0x2D;
64
+ const VERSION_AES = 0x33;
65
+
66
+ const DIRECTORY_SIGNATURE = "/";
67
+
68
+ const MAX_DATE = new Date(2107, 11, 31);
69
+ const MIN_DATE = new Date(1980, 0, 1);
70
+
71
+ const UNDEFINED_VALUE = undefined;
72
+ const UNDEFINED_TYPE = "undefined";
73
+ const FUNCTION_TYPE = "function";
74
+
75
+ export {
76
+ MAX_32_BITS,
77
+ MAX_16_BITS,
78
+ COMPRESSION_METHOD_DEFLATE,
79
+ COMPRESSION_METHOD_STORE,
80
+ COMPRESSION_METHOD_AES,
81
+ SPLIT_ZIP_FILE_SIGNATURE,
82
+ LOCAL_FILE_HEADER_SIGNATURE,
83
+ DATA_DESCRIPTOR_RECORD_SIGNATURE,
84
+ CENTRAL_FILE_HEADER_SIGNATURE,
85
+ END_OF_CENTRAL_DIR_SIGNATURE,
86
+ ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIGNATURE,
87
+ ZIP64_END_OF_CENTRAL_DIR_SIGNATURE,
88
+ EXTRAFIELD_TYPE_ZIP64,
89
+ EXTRAFIELD_TYPE_AES,
90
+ EXTRAFIELD_TYPE_NTFS,
91
+ EXTRAFIELD_TYPE_NTFS_TAG1,
92
+ EXTRAFIELD_TYPE_EXTENDED_TIMESTAMP,
93
+ EXTRAFIELD_TYPE_UNICODE_PATH,
94
+ EXTRAFIELD_TYPE_UNICODE_COMMENT,
95
+ EXTRAFIELD_TYPE_USDZ,
96
+ END_OF_CENTRAL_DIR_LENGTH,
97
+ ZIP64_END_OF_CENTRAL_DIR_LOCATOR_LENGTH,
98
+ ZIP64_END_OF_CENTRAL_DIR_LENGTH,
99
+ ZIP64_END_OF_CENTRAL_DIR_TOTAL_LENGTH,
100
+ BITFLAG_ENCRYPTED,
101
+ BITFLAG_LEVEL,
102
+ BITFLAG_DATA_DESCRIPTOR,
103
+ BITFLAG_LANG_ENCODING_FLAG,
104
+ FILE_ATTR_MSDOS_DIR_MASK,
105
+ VERSION_DEFLATE,
106
+ VERSION_ZIP64,
107
+ VERSION_AES,
108
+ DIRECTORY_SIGNATURE,
109
+ MIN_DATE,
110
+ MAX_DATE,
111
+ UNDEFINED_VALUE,
112
+ UNDEFINED_TYPE,
113
+ FUNCTION_TYPE
114
+ };