@lvce-editor/file-system-worker 4.2.0 → 5.0.0

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.
@@ -1,3 +1,170 @@
1
+ // TODO: including these in blob-util.ts causes typedoc to generate docs for them,
2
+ // even with --excludePrivate ¯\_(ツ)_/¯
3
+ /** @private */
4
+
5
+ /* global Promise, Image, Blob, FileReader, atob, btoa,
6
+ BlobBuilder, MSBlobBuilder, MozBlobBuilder, WebKitBlobBuilder, webkitURL */
7
+ /**
8
+ * Shim for
9
+ * [`new Blob()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob)
10
+ * to support
11
+ * [older browsers that use the deprecated `BlobBuilder` API](http://caniuse.com/blob).
12
+ *
13
+ * Example:
14
+ *
15
+ * ```js
16
+ * var myBlob = blobUtil.createBlob(['hello world'], {type: 'text/plain'});
17
+ * ```
18
+ *
19
+ * @param parts - content of the Blob
20
+ * @param properties - usually `{type: myContentType}`,
21
+ * you can also pass a string for the content type
22
+ * @returns Blob
23
+ */
24
+ function createBlob(parts, properties) {
25
+ parts = parts || [];
26
+ properties = properties || {};
27
+ if (typeof properties === 'string') {
28
+ properties = {
29
+ type: properties
30
+ }; // infer content type
31
+ }
32
+ try {
33
+ return new Blob(parts, properties);
34
+ } catch (e) {
35
+ if (e.name !== 'TypeError') {
36
+ throw e;
37
+ }
38
+ var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder : WebKitBlobBuilder;
39
+ var builder = new Builder();
40
+ for (var i = 0; i < parts.length; i += 1) {
41
+ builder.append(parts[i]);
42
+ }
43
+ return builder.getBlob(properties.type);
44
+ }
45
+ }
46
+ /**
47
+ * Convert a `Blob` to a binary string.
48
+ *
49
+ * Example:
50
+ *
51
+ * ```js
52
+ * blobUtil.blobToBinaryString(blob).then(function (binaryString) {
53
+ * // success
54
+ * }).catch(function (err) {
55
+ * // error
56
+ * });
57
+ * ```
58
+ *
59
+ * @param blob
60
+ * @returns Promise that resolves with the binary string
61
+ */
62
+ function blobToBinaryString$1(blob) {
63
+ return new Promise(function (resolve, reject) {
64
+ var reader = new FileReader();
65
+ var hasBinaryString = typeof reader.readAsBinaryString === 'function';
66
+ reader.onloadend = function () {
67
+ var result = reader.result || '';
68
+ if (hasBinaryString) {
69
+ return resolve(result);
70
+ }
71
+ resolve(arrayBufferToBinaryString(result));
72
+ };
73
+ reader.onerror = reject;
74
+ if (hasBinaryString) {
75
+ reader.readAsBinaryString(blob);
76
+ } else {
77
+ reader.readAsArrayBuffer(blob);
78
+ }
79
+ });
80
+ }
81
+ /**
82
+ * Convert a base64-encoded string to a `Blob`.
83
+ *
84
+ * Example:
85
+ *
86
+ * ```js
87
+ * var blob = blobUtil.base64StringToBlob(base64String);
88
+ * ```
89
+ * @param base64 - base64-encoded string
90
+ * @param type - the content type (optional)
91
+ * @returns Blob
92
+ */
93
+ function base64StringToBlob(base64, type) {
94
+ var parts = [binaryStringToArrayBuffer(atob(base64))];
95
+ return type ? createBlob(parts, {
96
+ type: type
97
+ }) : createBlob(parts);
98
+ }
99
+ /**
100
+ * Convert a binary string to a `Blob`.
101
+ *
102
+ * Example:
103
+ *
104
+ * ```js
105
+ * var blob = blobUtil.binaryStringToBlob(binaryString);
106
+ * ```
107
+ *
108
+ * @param binary - binary string
109
+ * @param type - the content type (optional)
110
+ * @returns Blob
111
+ */
112
+ function binaryStringToBlob$1(binary, type) {
113
+ return base64StringToBlob(btoa(binary), type);
114
+ }
115
+ /**
116
+ * Convert an `ArrayBuffer` to a binary string.
117
+ *
118
+ * Example:
119
+ *
120
+ * ```js
121
+ * var myString = blobUtil.arrayBufferToBinaryString(arrayBuff)
122
+ * ```
123
+ *
124
+ * @param buffer - array buffer
125
+ * @returns binary string
126
+ */
127
+ function arrayBufferToBinaryString(buffer) {
128
+ var binary = '';
129
+ var bytes = new Uint8Array(buffer);
130
+ var length = bytes.byteLength;
131
+ var i = -1;
132
+ while (++i < length) {
133
+ binary += String.fromCharCode(bytes[i]);
134
+ }
135
+ return binary;
136
+ }
137
+ /**
138
+ * Convert a binary string to an `ArrayBuffer`.
139
+ *
140
+ * ```js
141
+ * var myBuffer = blobUtil.binaryStringToArrayBuffer(binaryString)
142
+ * ```
143
+ *
144
+ * @param binary - binary string
145
+ * @returns array buffer
146
+ */
147
+ function binaryStringToArrayBuffer(binary) {
148
+ var length = binary.length;
149
+ var buf = new ArrayBuffer(length);
150
+ var arr = new Uint8Array(buf);
151
+ var i = -1;
152
+ while (++i < length) {
153
+ arr[i] = binary.charCodeAt(i);
154
+ }
155
+ return buf;
156
+ }
157
+
158
+ const normalizeBlobError = error => {
159
+ if (error && error instanceof ProgressEvent && error.target &&
160
+ // @ts-expect-error - target.error may not be in the type definition
161
+ error.target.error) {
162
+ // @ts-expect-error - target.error may not be in the type definition
163
+ return error.target.error;
164
+ }
165
+ return error;
166
+ };
167
+
1
168
  const normalizeLine = line => {
2
169
  if (line.startsWith('Error: ')) {
3
170
  return line.slice('Error: '.length);
@@ -54,6 +221,94 @@ class VError extends Error {
54
221
  }
55
222
  }
56
223
 
224
+ const binaryStringToBlob = async (string, type) => {
225
+ try {
226
+ return binaryStringToBlob$1(string, type);
227
+ } catch (error) {
228
+ const normalizedError = normalizeBlobError(error);
229
+ throw new VError(normalizedError, 'Failed to convert binary string to blob');
230
+ }
231
+ };
232
+ const blobToBinaryString = async blob => {
233
+ try {
234
+ return await blobToBinaryString$1(blob);
235
+ } catch (error) {
236
+ const normalizedError = normalizeBlobError(error);
237
+ throw new VError(normalizedError, 'Failed to convert blob to binary string');
238
+ }
239
+ };
240
+
241
+ const readFile$4 = async uri => {
242
+ const response = await fetch(uri);
243
+ if (!response.ok) {
244
+ throw new Error(response.statusText);
245
+ }
246
+ const result = await response.text();
247
+ return result;
248
+ };
249
+ const readFileAsBlob$1 = async uri => {
250
+ const response = await fetch(uri);
251
+ if (!response.ok) {
252
+ throw new Error(response.statusText);
253
+ }
254
+ const result = await response.blob();
255
+ return result;
256
+ };
257
+ const exists$3 = async uri => {
258
+ const response = await fetch(uri);
259
+ if (response.ok) {
260
+ return true;
261
+ }
262
+ return false;
263
+ };
264
+ const readJson$4 = async uri => {
265
+ const response = await fetch(uri);
266
+ if (!response.ok) {
267
+ throw new Error(`${response.statusText}`);
268
+ }
269
+ const json = await response.json();
270
+ return json;
271
+ };
272
+
273
+ const DebugWorker = 55;
274
+ const ExtensionHostWorker = 44;
275
+ const FileSystemProcess$1 = 210;
276
+ const FileSystemWorker = 209;
277
+ const RendererProcess = 1670;
278
+ const RendererWorker = 1;
279
+
280
+ const rpcs = Object.create(null);
281
+ const set$5 = (id, rpc) => {
282
+ rpcs[id] = rpc;
283
+ };
284
+ const get$1 = id => {
285
+ return rpcs[id];
286
+ };
287
+
288
+ const create$9 = rpcId => {
289
+ return {
290
+ async dispose() {
291
+ const rpc = get$1(rpcId);
292
+ await rpc.dispose();
293
+ },
294
+ // @ts-ignore
295
+ invoke(method, ...params) {
296
+ const rpc = get$1(rpcId);
297
+ // @ts-ignore
298
+ return rpc.invoke(method, ...params);
299
+ },
300
+ // @ts-ignore
301
+ invokeAndTransfer(method, ...params) {
302
+ const rpc = get$1(rpcId);
303
+ // @ts-ignore
304
+ return rpc.invokeAndTransfer(method, ...params);
305
+ },
306
+ set(rpc) {
307
+ set$5(rpcId, rpc);
308
+ }
309
+ };
310
+ };
311
+
57
312
  class AssertionError extends Error {
58
313
  constructor(message) {
59
314
  super(message);
@@ -225,8 +480,8 @@ const getModuleNotFoundError = stderr => {
225
480
  const messageIndex = lines.findIndex(isModuleNotFoundMessage);
226
481
  const message = lines[messageIndex];
227
482
  return {
228
- message,
229
- code: ERR_MODULE_NOT_FOUND
483
+ code: ERR_MODULE_NOT_FOUND,
484
+ message
230
485
  };
231
486
  };
232
487
  const isModuleNotFoundError = stderr => {
@@ -249,14 +504,14 @@ const isUnhelpfulNativeModuleError = stderr => {
249
504
  const getNativeModuleErrorMessage = stderr => {
250
505
  const message = getMessageCodeBlock(stderr);
251
506
  return {
252
- message: `Incompatible native node module: ${message}`,
253
- code: E_INCOMPATIBLE_NATIVE_MODULE
507
+ code: E_INCOMPATIBLE_NATIVE_MODULE,
508
+ message: `Incompatible native node module: ${message}`
254
509
  };
255
510
  };
256
511
  const getModuleSyntaxError = () => {
257
512
  return {
258
- message: `ES Modules are not supported in electron`,
259
- code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
513
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
514
+ message: `ES Modules are not supported in electron`
260
515
  };
261
516
  };
262
517
  const getHelpfulChildProcessError = (stdout, stderr) => {
@@ -275,8 +530,8 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
275
530
  rest
276
531
  } = getDetails(lines);
277
532
  return {
278
- message: actualMessage,
279
533
  code: '',
534
+ message: actualMessage,
280
535
  stack: rest
281
536
  };
282
537
  };
@@ -286,8 +541,8 @@ class IpcError extends VError {
286
541
  if (stdout || stderr) {
287
542
  // @ts-ignore
288
543
  const {
289
- message,
290
544
  code,
545
+ message,
291
546
  stack
292
547
  } = getHelpfulChildProcessError(stdout, stderr);
293
548
  const cause = new Error(message);
@@ -348,8 +603,8 @@ const wrap$f = global => {
348
603
  };
349
604
  const waitForFirstMessage = async port => {
350
605
  const {
351
- resolve,
352
- promise
606
+ promise,
607
+ resolve
353
608
  } = Promise.withResolvers();
354
609
  port.addEventListener('message', resolve, {
355
610
  once: true
@@ -369,8 +624,8 @@ const listen$6 = async () => {
369
624
  const type = firstMessage.params[0];
370
625
  if (type === 'message-port') {
371
626
  parentIpc.send({
372
- jsonrpc: '2.0',
373
627
  id: firstMessage.id,
628
+ jsonrpc: '2.0',
374
629
  result: null
375
630
  });
376
631
  parentIpc.dispose();
@@ -430,8 +685,8 @@ const removeListener = (emitter, type, callback) => {
430
685
  };
431
686
  const getFirstEvent = (eventEmitter, eventMap) => {
432
687
  const {
433
- resolve,
434
- promise
688
+ promise,
689
+ resolve
435
690
  } = Promise.withResolvers();
436
691
  const listenerMap = Object.create(null);
437
692
  const cleanup = value => {
@@ -443,8 +698,8 @@ const getFirstEvent = (eventEmitter, eventMap) => {
443
698
  for (const [event, type] of Object.entries(eventMap)) {
444
699
  const listener = event => {
445
700
  cleanup({
446
- type,
447
- event
701
+ event,
702
+ type
448
703
  });
449
704
  };
450
705
  addListener(eventEmitter, event, listener);
@@ -454,8 +709,8 @@ const getFirstEvent = (eventEmitter, eventMap) => {
454
709
  };
455
710
  const Message$1 = 3;
456
711
  const create$5$1 = async ({
457
- messagePort,
458
- isMessagePortOpen
712
+ isMessagePortOpen,
713
+ messagePort
459
714
  }) => {
460
715
  if (!isMessagePort(messagePort)) {
461
716
  throw new IpcError('port must be of type MessagePort');
@@ -468,8 +723,8 @@ const create$5$1 = async ({
468
723
  });
469
724
  messagePort.start();
470
725
  const {
471
- type,
472
- event
726
+ event,
727
+ type
473
728
  } = await eventPromise;
474
729
  if (type !== Message$1) {
475
730
  throw new IpcError('Failed to wait for ipc message');
@@ -523,12 +778,12 @@ const parse = content => {
523
778
  };
524
779
  const waitForWebSocketToBeOpen = webSocket => {
525
780
  return getFirstEvent(webSocket, {
526
- open: Open,
527
781
  close: Close,
528
- error: Error$3
782
+ error: Error$3,
783
+ open: Open
529
784
  });
530
785
  };
531
- const create$7 = async ({
786
+ const create$8 = async ({
532
787
  webSocket
533
788
  }) => {
534
789
  const firstWebSocketEvent = await waitForWebSocketToBeOpen(webSocket);
@@ -565,60 +820,18 @@ const wrap = webSocket => {
565
820
  };
566
821
  const IpcParentWithWebSocket$1 = {
567
822
  __proto__: null,
568
- create: create$7,
823
+ create: create$8,
569
824
  wrap
570
825
  };
571
826
 
572
- const Two = '2.0';
573
- const create$4$1 = (method, params) => {
574
- return {
575
- jsonrpc: Two,
576
- method,
577
- params
578
- };
579
- };
827
+ const Two$1 = '2.0';
580
828
  const callbacks = Object.create(null);
581
- const set$4 = (id, fn) => {
582
- callbacks[id] = fn;
583
- };
584
- const get$1 = id => {
829
+ const get = id => {
585
830
  return callbacks[id];
586
831
  };
587
832
  const remove$4 = id => {
588
833
  delete callbacks[id];
589
834
  };
590
- let id = 0;
591
- const create$3$1 = () => {
592
- return ++id;
593
- };
594
- const registerPromise = () => {
595
- const id = create$3$1();
596
- const {
597
- resolve,
598
- promise
599
- } = Promise.withResolvers();
600
- set$4(id, resolve);
601
- return {
602
- id,
603
- promise
604
- };
605
- };
606
- const create$2$1 = (method, params) => {
607
- const {
608
- id,
609
- promise
610
- } = registerPromise();
611
- const message = {
612
- jsonrpc: Two,
613
- method,
614
- params,
615
- id
616
- };
617
- return {
618
- message,
619
- promise
620
- };
621
- };
622
835
  class JsonRpcError extends Error {
623
836
  constructor(message) {
624
837
  super(message);
@@ -762,7 +975,7 @@ const warn = (...args) => {
762
975
  console.warn(...args);
763
976
  };
764
977
  const resolve = (id, response) => {
765
- const fn = get$1(id);
978
+ const fn = get(id);
766
979
  if (!fn) {
767
980
  console.log(response);
768
981
  warn(`callback ${id} may already be disposed`);
@@ -812,9 +1025,9 @@ const getErrorProperty = (error, prettyError) => {
812
1025
  }
813
1026
  };
814
1027
  };
815
- const create$1$1 = (id, error) => {
1028
+ const create$1 = (id, error) => {
816
1029
  return {
817
- jsonrpc: Two,
1030
+ jsonrpc: Two$1,
818
1031
  id,
819
1032
  error
820
1033
  };
@@ -823,11 +1036,11 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
823
1036
  const prettyError = preparePrettyError(error);
824
1037
  logError(error, prettyError);
825
1038
  const errorProperty = getErrorProperty(error, prettyError);
826
- return create$1$1(id, errorProperty);
1039
+ return create$1(id, errorProperty);
827
1040
  };
828
1041
  const create$6 = (message, result) => {
829
1042
  return {
830
- jsonrpc: Two,
1043
+ jsonrpc: Two$1,
831
1044
  id: message.id,
832
1045
  result: result ?? null
833
1046
  };
@@ -838,7 +1051,7 @@ const getSuccessResponse = (message, result) => {
838
1051
  };
839
1052
  const getErrorResponseSimple = (id, error) => {
840
1053
  return {
841
- jsonrpc: Two,
1054
+ jsonrpc: Two$1,
842
1055
  id,
843
1056
  error: {
844
1057
  code: Custom,
@@ -925,29 +1138,6 @@ const handleJsonRpcMessage = async (...args) => {
925
1138
  }
926
1139
  throw new JsonRpcError('unexpected message');
927
1140
  };
928
- const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
929
- const {
930
- message,
931
- promise
932
- } = create$2$1(method, params);
933
- if (useSendAndTransfer && ipc.sendAndTransfer) {
934
- ipc.sendAndTransfer(message);
935
- } else {
936
- ipc.send(message);
937
- }
938
- const responseMessage = await promise;
939
- return unwrapJsonRpcResult(responseMessage);
940
- };
941
- const send = (transport, method, ...params) => {
942
- const message = create$4$1(method, params);
943
- transport.send(message);
944
- };
945
- const invoke$4 = (ipc, method, ...params) => {
946
- return invokeHelper(ipc, method, params, false);
947
- };
948
- const invokeAndTransfer$2 = (ipc, method, ...params) => {
949
- return invokeHelper(ipc, method, params, true);
950
- };
951
1141
 
952
1142
  class CommandNotFoundError extends Error {
953
1143
  constructor(command) {
@@ -970,24 +1160,87 @@ const execute = (command, ...args) => {
970
1160
  return fn(...args);
971
1161
  };
972
1162
 
1163
+ const Two = '2.0';
1164
+ const create$p = (method, params) => {
1165
+ return {
1166
+ jsonrpc: Two,
1167
+ method,
1168
+ params
1169
+ };
1170
+ };
1171
+ const create$o = (id, method, params) => {
1172
+ const message = {
1173
+ id,
1174
+ jsonrpc: Two,
1175
+ method,
1176
+ params
1177
+ };
1178
+ return message;
1179
+ };
1180
+ let id = 0;
1181
+ const create$n = () => {
1182
+ return ++id;
1183
+ };
1184
+
1185
+ /* eslint-disable n/no-unsupported-features/es-syntax */
1186
+
1187
+ const registerPromise = map => {
1188
+ const id = create$n();
1189
+ const {
1190
+ promise,
1191
+ resolve
1192
+ } = Promise.withResolvers();
1193
+ map[id] = resolve;
1194
+ return {
1195
+ id,
1196
+ promise
1197
+ };
1198
+ };
1199
+
1200
+ // @ts-ignore
1201
+ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
1202
+ const {
1203
+ id,
1204
+ promise
1205
+ } = registerPromise(callbacks);
1206
+ const message = create$o(id, method, params);
1207
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
1208
+ ipc.sendAndTransfer(message);
1209
+ } else {
1210
+ ipc.send(message);
1211
+ }
1212
+ const responseMessage = await promise;
1213
+ return unwrapJsonRpcResult(responseMessage);
1214
+ };
973
1215
  const createRpc = ipc => {
1216
+ const callbacks = Object.create(null);
1217
+ ipc._resolve = (id, response) => {
1218
+ const fn = callbacks[id];
1219
+ if (!fn) {
1220
+ console.warn(`callback ${id} may already be disposed`);
1221
+ return;
1222
+ }
1223
+ fn(response);
1224
+ delete callbacks[id];
1225
+ };
974
1226
  const rpc = {
1227
+ async dispose() {
1228
+ await ipc?.dispose();
1229
+ },
1230
+ invoke(method, ...params) {
1231
+ return invokeHelper(callbacks, ipc, method, params, false);
1232
+ },
1233
+ invokeAndTransfer(method, ...params) {
1234
+ return invokeHelper(callbacks, ipc, method, params, true);
1235
+ },
975
1236
  // @ts-ignore
976
1237
  ipc,
977
1238
  /**
978
1239
  * @deprecated
979
1240
  */
980
1241
  send(method, ...params) {
981
- send(ipc, method, ...params);
982
- },
983
- invoke(method, ...params) {
984
- return invoke$4(ipc, method, ...params);
985
- },
986
- invokeAndTransfer(method, ...params) {
987
- return invokeAndTransfer$2(ipc, method, ...params);
988
- },
989
- async dispose() {
990
- await ipc?.dispose();
1242
+ const message = create$p(method, params);
1243
+ ipc.send(message);
991
1244
  }
992
1245
  };
993
1246
  return rpc;
@@ -1004,7 +1257,7 @@ const logError = () => {
1004
1257
  const handleMessage = event => {
1005
1258
  const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
1006
1259
  const actualExecute = event?.target?.execute || execute;
1007
- return handleJsonRpcMessage(event.target, event.data, actualExecute, resolve, preparePrettyError, logError, actualRequiresSocket);
1260
+ return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
1008
1261
  };
1009
1262
  const handleIpc = ipc => {
1010
1263
  if ('addEventListener' in ipc) {
@@ -1022,15 +1275,16 @@ const listen$1 = async (module, options) => {
1022
1275
  const ipc = module.wrap(rawIpc);
1023
1276
  return ipc;
1024
1277
  };
1025
- const create$5 = async ({
1278
+ const create$7 = async ({
1026
1279
  commandMap,
1280
+ isMessagePortOpen = true,
1027
1281
  messagePort
1028
1282
  }) => {
1029
1283
  // TODO create a commandMap per rpc instance
1030
1284
  register(commandMap);
1031
1285
  const rawIpc = await IpcParentWithMessagePort$1.create({
1032
- messagePort,
1033
- isMessagePortOpen: true
1286
+ isMessagePortOpen,
1287
+ messagePort
1034
1288
  });
1035
1289
  const ipc = IpcParentWithMessagePort$1.wrap(rawIpc);
1036
1290
  handleIpc(ipc);
@@ -1040,10 +1294,11 @@ const create$5 = async ({
1040
1294
  };
1041
1295
  const PlainMessagePortRpc = {
1042
1296
  __proto__: null,
1043
- create: create$5
1297
+ create: create$7
1044
1298
  };
1045
- const create$3 = async ({
1299
+ const create$5 = async ({
1046
1300
  commandMap,
1301
+ isMessagePortOpen,
1047
1302
  send
1048
1303
  }) => {
1049
1304
  const {
@@ -1051,16 +1306,17 @@ const create$3 = async ({
1051
1306
  port2
1052
1307
  } = new MessageChannel();
1053
1308
  await send(port1);
1054
- return create$5({
1309
+ return create$7({
1055
1310
  commandMap,
1311
+ isMessagePortOpen,
1056
1312
  messagePort: port2
1057
1313
  });
1058
1314
  };
1059
1315
  const TransferMessagePortRpcParent = {
1060
1316
  __proto__: null,
1061
- create: create$3
1317
+ create: create$5
1062
1318
  };
1063
- const create$2 = async ({
1319
+ const create$4 = async ({
1064
1320
  commandMap,
1065
1321
  webSocket
1066
1322
  }) => {
@@ -1090,7 +1346,7 @@ const getHost = () => {
1090
1346
  const getProtocol = () => {
1091
1347
  return location.protocol;
1092
1348
  };
1093
- const create$1 = async ({
1349
+ const create$3 = async ({
1094
1350
  commandMap,
1095
1351
  type
1096
1352
  }) => {
@@ -1098,17 +1354,17 @@ const create$1 = async ({
1098
1354
  const protocol = getProtocol();
1099
1355
  const wsUrl = getWebSocketUrl(type, host, protocol);
1100
1356
  const webSocket = new WebSocket(wsUrl);
1101
- const rpc = await create$2({
1102
- webSocket,
1103
- commandMap
1357
+ const rpc = await create$4({
1358
+ commandMap,
1359
+ webSocket
1104
1360
  });
1105
1361
  return rpc;
1106
1362
  };
1107
1363
  const WebSocketRpcParent2 = {
1108
1364
  __proto__: null,
1109
- create: create$1
1365
+ create: create$3
1110
1366
  };
1111
- const create$4 = async ({
1367
+ const create$2 = async ({
1112
1368
  commandMap
1113
1369
  }) => {
1114
1370
  // TODO create a commandMap per rpc instance
@@ -1120,77 +1376,84 @@ const create$4 = async ({
1120
1376
  };
1121
1377
  const WebWorkerRpcClient = {
1122
1378
  __proto__: null,
1123
- create: create$4
1379
+ create: create$2
1124
1380
  };
1125
- const createMockRpc = ({
1126
- commandMap
1381
+
1382
+ /* eslint-disable @typescript-eslint/no-misused-promises */
1383
+
1384
+ const create = async ({
1385
+ commandMap,
1386
+ isMessagePortOpen,
1387
+ send
1127
1388
  }) => {
1128
- const invocations = [];
1129
- const invoke = (method, ...params) => {
1130
- invocations.push([method, ...params]);
1131
- const command = commandMap[method];
1132
- if (!command) {
1133
- throw new Error(`command ${method} not found`);
1389
+ let rpcPromise;
1390
+ const getOrCreate = () => {
1391
+ if (!rpcPromise) {
1392
+ rpcPromise = create$5({
1393
+ commandMap,
1394
+ isMessagePortOpen,
1395
+ send
1396
+ });
1134
1397
  }
1135
- return command(...params);
1136
- };
1137
- const mockRpc = {
1138
- invoke,
1139
- invokeAndTransfer: invoke,
1140
- invocations
1398
+ return rpcPromise;
1141
1399
  };
1142
- return mockRpc;
1143
- };
1144
-
1145
- const DebugWorker = 55;
1146
- const ExtensionHostWorker = 44;
1147
- const FileSystemProcess$1 = 210;
1148
- const FileSystemWorker = 209;
1149
- const RendererProcess = 1670;
1150
- const RendererWorker = 1;
1151
-
1152
- const rpcs = Object.create(null);
1153
- const set$3 = (id, rpc) => {
1154
- rpcs[id] = rpc;
1155
- };
1156
- const get = id => {
1157
- return rpcs[id];
1158
- };
1159
-
1160
- const create = rpcId => {
1161
1400
  return {
1162
- // @ts-ignore
1163
- invoke(method, ...params) {
1164
- const rpc = get(rpcId);
1165
- // @ts-ignore
1401
+ async dispose() {
1402
+ const rpc = await getOrCreate();
1403
+ await rpc.dispose();
1404
+ },
1405
+ async invoke(method, ...params) {
1406
+ const rpc = await getOrCreate();
1166
1407
  return rpc.invoke(method, ...params);
1167
1408
  },
1168
- // @ts-ignore
1169
- invokeAndTransfer(method, ...params) {
1170
- const rpc = get(rpcId);
1171
- // @ts-ignore
1409
+ async invokeAndTransfer(method, ...params) {
1410
+ const rpc = await getOrCreate();
1172
1411
  return rpc.invokeAndTransfer(method, ...params);
1173
1412
  },
1174
- set(rpc) {
1175
- set$3(rpcId, rpc);
1176
- },
1177
- async dispose() {
1178
- const rpc = get(rpcId);
1179
- await rpc.dispose();
1413
+ async send(method, ...params) {
1414
+ const rpc = await getOrCreate();
1415
+ rpc.send(method, ...params);
1416
+ }
1417
+ };
1418
+ };
1419
+ const LazyTransferMessagePortRpcParent = {
1420
+ __proto__: null,
1421
+ create
1422
+ };
1423
+ const createMockRpc = ({
1424
+ commandMap
1425
+ }) => {
1426
+ const invocations = [];
1427
+ const invoke = (method, ...params) => {
1428
+ invocations.push([method, ...params]);
1429
+ const command = commandMap[method];
1430
+ if (!command) {
1431
+ throw new Error(`command ${method} not found`);
1180
1432
  }
1433
+ return command(...params);
1434
+ };
1435
+ const mockRpc = {
1436
+ invocations,
1437
+ invoke,
1438
+ invokeAndTransfer: invoke
1181
1439
  };
1440
+ return mockRpc;
1182
1441
  };
1183
1442
 
1184
1443
  const {
1444
+ set: set$4
1445
+ } = create$9(ExtensionHostWorker);
1446
+
1447
+ const {
1448
+ dispose,
1185
1449
  invoke: invoke$3,
1186
1450
  invokeAndTransfer: invokeAndTransfer$1,
1187
- set: set$2,
1188
- dispose
1189
- } = create(FileSystemProcess$1);
1451
+ set: set$3
1452
+ } = create$9(FileSystemProcess$1);
1190
1453
  const remove$3 = async uri => {
1191
1454
  return invoke$3('FileSystem.remove', uri);
1192
1455
  };
1193
- const readFile$4 = async uri => {
1456
+ const readFile$3 = async uri => {
1194
1457
  return invoke$3('FileSystem.readFile', uri);
1195
1458
  };
1196
1459
  const appendFile$2 = async (uri, text) => {
@@ -1204,7 +1467,7 @@ const getPathSeparator$2 = async root => {
1204
1467
  // @ts-ignore
1205
1468
  return invoke$3('FileSystem.getPathSeparator', root);
1206
1469
  };
1207
- const readJson$4 = async root => {
1470
+ const readJson$3 = async root => {
1208
1471
  // @ts-ignore
1209
1472
  return invoke$3('FileSystem.readJson', root);
1210
1473
  };
@@ -1236,7 +1499,7 @@ const getFolderSize$2 = async uri => {
1236
1499
  // @ts-ignore
1237
1500
  return invoke$3('FileSystem.getFolderSize', uri);
1238
1501
  };
1239
- const exists$3 = async uri => {
1502
+ const exists$2 = async uri => {
1240
1503
  // @ts-ignore
1241
1504
  return invoke$3('FileSystem.exists', uri);
1242
1505
  };
@@ -1244,36 +1507,41 @@ const registerMockRpc = commandMap => {
1244
1507
  const mockRpc = createMockRpc({
1245
1508
  commandMap
1246
1509
  });
1247
- set$2(mockRpc);
1510
+ set$3(mockRpc);
1248
1511
  return mockRpc;
1249
1512
  };
1250
1513
 
1251
1514
  const FileSystemProcess = {
1252
- __proto__: null,
1253
- appendFile: appendFile$2,
1254
- copy: copy$2,
1255
- dispose,
1256
- exists: exists$3,
1257
- getFolderSize: getFolderSize$2,
1258
- getPathSeparator: getPathSeparator$2,
1259
- getRealPath: getRealPath$2,
1260
- invoke: invoke$3,
1261
- invokeAndTransfer: invokeAndTransfer$1,
1262
- mkdir: mkdir$2,
1263
- readDirWithFileTypes: readDirWithFileTypes$2,
1264
- readFile: readFile$4,
1265
- readJson: readJson$4,
1266
- registerMockRpc,
1267
- remove: remove$3,
1268
- rename: rename$3,
1269
- set: set$2,
1270
- stat: stat$2,
1271
- writeFile: writeFile$3
1515
+ __proto__: null,
1516
+ appendFile: appendFile$2,
1517
+ copy: copy$2,
1518
+ dispose,
1519
+ exists: exists$2,
1520
+ getFolderSize: getFolderSize$2,
1521
+ getPathSeparator: getPathSeparator$2,
1522
+ getRealPath: getRealPath$2,
1523
+ invoke: invoke$3,
1524
+ invokeAndTransfer: invokeAndTransfer$1,
1525
+ mkdir: mkdir$2,
1526
+ readDirWithFileTypes: readDirWithFileTypes$2,
1527
+ readFile: readFile$3,
1528
+ readJson: readJson$3,
1529
+ registerMockRpc,
1530
+ remove: remove$3,
1531
+ rename: rename$3,
1532
+ set: set$3,
1533
+ stat: stat$2,
1534
+ writeFile: writeFile$3
1272
1535
  };
1273
1536
 
1537
+ const {
1538
+ set: set$2
1539
+ } = create$9(RendererProcess);
1540
+
1274
1541
  const {
1275
1542
  invokeAndTransfer,
1276
- set: set$1} = create(RendererWorker);
1543
+ set: set$1
1544
+ } = create$9(RendererWorker);
1277
1545
  const sendMessagePortToExtensionHostWorker = async (port, rpcId = 0) => {
1278
1546
  const command = 'HandleMessagePort.handleMessagePort2';
1279
1547
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, command, rpcId);
@@ -1289,7 +1557,7 @@ const createLazyRpc = rpcId => {
1289
1557
  let factory;
1290
1558
  const createRpc = async () => {
1291
1559
  const rpc = await factory();
1292
- set$3(rpcId, rpc);
1560
+ set$5(rpcId, rpc);
1293
1561
  };
1294
1562
  const ensureRpc = async () => {
1295
1563
  if (!rpcPromise) {
@@ -1298,237 +1566,24 @@ const createLazyRpc = rpcId => {
1298
1566
  await rpcPromise;
1299
1567
  };
1300
1568
  return {
1301
- setFactory(value) {
1302
- factory = value;
1303
- },
1304
1569
  async invoke(method, ...params) {
1305
1570
  await ensureRpc();
1306
- const rpc = get(rpcId);
1571
+ const rpc = get$1(rpcId);
1307
1572
  return rpc.invoke(method, ...params);
1573
+ },
1574
+ async invokeAndTransfer(method, ...params) {
1575
+ await ensureRpc();
1576
+ const rpc = get$1(rpcId);
1577
+ return rpc.invokeAndTransfer(method, ...params);
1578
+ },
1579
+ setFactory(value) {
1580
+ factory = value;
1308
1581
  }
1309
1582
  };
1310
1583
  };
1311
1584
 
1312
- // TODO: including these in blob-util.ts causes typedoc to generate docs for them,
1313
- // even with --excludePrivate ¯\_(ツ)_/¯
1314
- /** @private */
1315
-
1316
- /* global Promise, Image, Blob, FileReader, atob, btoa,
1317
- BlobBuilder, MSBlobBuilder, MozBlobBuilder, WebKitBlobBuilder, webkitURL */
1318
- /**
1319
- * Shim for
1320
- * [`new Blob()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob.Blob)
1321
- * to support
1322
- * [older browsers that use the deprecated `BlobBuilder` API](http://caniuse.com/blob).
1323
- *
1324
- * Example:
1325
- *
1326
- * ```js
1327
- * var myBlob = blobUtil.createBlob(['hello world'], {type: 'text/plain'});
1328
- * ```
1329
- *
1330
- * @param parts - content of the Blob
1331
- * @param properties - usually `{type: myContentType}`,
1332
- * you can also pass a string for the content type
1333
- * @returns Blob
1334
- */
1335
- function createBlob(parts, properties) {
1336
- parts = parts || [];
1337
- properties = properties || {};
1338
- if (typeof properties === 'string') {
1339
- properties = {
1340
- type: properties
1341
- }; // infer content type
1342
- }
1343
- try {
1344
- return new Blob(parts, properties);
1345
- } catch (e) {
1346
- if (e.name !== 'TypeError') {
1347
- throw e;
1348
- }
1349
- var Builder = typeof BlobBuilder !== 'undefined' ? BlobBuilder : typeof MSBlobBuilder !== 'undefined' ? MSBlobBuilder : typeof MozBlobBuilder !== 'undefined' ? MozBlobBuilder : WebKitBlobBuilder;
1350
- var builder = new Builder();
1351
- for (var i = 0; i < parts.length; i += 1) {
1352
- builder.append(parts[i]);
1353
- }
1354
- return builder.getBlob(properties.type);
1355
- }
1356
- }
1357
- /**
1358
- * Convert a `Blob` to a binary string.
1359
- *
1360
- * Example:
1361
- *
1362
- * ```js
1363
- * blobUtil.blobToBinaryString(blob).then(function (binaryString) {
1364
- * // success
1365
- * }).catch(function (err) {
1366
- * // error
1367
- * });
1368
- * ```
1369
- *
1370
- * @param blob
1371
- * @returns Promise that resolves with the binary string
1372
- */
1373
- function blobToBinaryString$1(blob) {
1374
- return new Promise(function (resolve, reject) {
1375
- var reader = new FileReader();
1376
- var hasBinaryString = typeof reader.readAsBinaryString === 'function';
1377
- reader.onloadend = function () {
1378
- var result = reader.result || '';
1379
- if (hasBinaryString) {
1380
- return resolve(result);
1381
- }
1382
- resolve(arrayBufferToBinaryString(result));
1383
- };
1384
- reader.onerror = reject;
1385
- if (hasBinaryString) {
1386
- reader.readAsBinaryString(blob);
1387
- } else {
1388
- reader.readAsArrayBuffer(blob);
1389
- }
1390
- });
1391
- }
1392
- /**
1393
- * Convert a base64-encoded string to a `Blob`.
1394
- *
1395
- * Example:
1396
- *
1397
- * ```js
1398
- * var blob = blobUtil.base64StringToBlob(base64String);
1399
- * ```
1400
- * @param base64 - base64-encoded string
1401
- * @param type - the content type (optional)
1402
- * @returns Blob
1403
- */
1404
- function base64StringToBlob(base64, type) {
1405
- var parts = [binaryStringToArrayBuffer(atob(base64))];
1406
- return type ? createBlob(parts, {
1407
- type: type
1408
- }) : createBlob(parts);
1409
- }
1410
- /**
1411
- * Convert a binary string to a `Blob`.
1412
- *
1413
- * Example:
1414
- *
1415
- * ```js
1416
- * var blob = blobUtil.binaryStringToBlob(binaryString);
1417
- * ```
1418
- *
1419
- * @param binary - binary string
1420
- * @param type - the content type (optional)
1421
- * @returns Blob
1422
- */
1423
- function binaryStringToBlob$1(binary, type) {
1424
- return base64StringToBlob(btoa(binary), type);
1425
- }
1426
- /**
1427
- * Convert an `ArrayBuffer` to a binary string.
1428
- *
1429
- * Example:
1430
- *
1431
- * ```js
1432
- * var myString = blobUtil.arrayBufferToBinaryString(arrayBuff)
1433
- * ```
1434
- *
1435
- * @param buffer - array buffer
1436
- * @returns binary string
1437
- */
1438
- function arrayBufferToBinaryString(buffer) {
1439
- var binary = '';
1440
- var bytes = new Uint8Array(buffer);
1441
- var length = bytes.byteLength;
1442
- var i = -1;
1443
- while (++i < length) {
1444
- binary += String.fromCharCode(bytes[i]);
1445
- }
1446
- return binary;
1447
- }
1448
- /**
1449
- * Convert a binary string to an `ArrayBuffer`.
1450
- *
1451
- * ```js
1452
- * var myBuffer = blobUtil.binaryStringToArrayBuffer(binaryString)
1453
- * ```
1454
- *
1455
- * @param binary - binary string
1456
- * @returns array buffer
1457
- */
1458
- function binaryStringToArrayBuffer(binary) {
1459
- var length = binary.length;
1460
- var buf = new ArrayBuffer(length);
1461
- var arr = new Uint8Array(buf);
1462
- var i = -1;
1463
- while (++i < length) {
1464
- arr[i] = binary.charCodeAt(i);
1465
- }
1466
- return buf;
1467
- }
1468
-
1469
- const normalizeBlobError = error => {
1470
- if (error && error instanceof ProgressEvent && error.target &&
1471
- // @ts-expect-error - target.error may not be in the type definition
1472
- error.target.error) {
1473
- // @ts-expect-error - target.error may not be in the type definition
1474
- return error.target.error;
1475
- }
1476
- return error;
1477
- };
1478
-
1479
- const binaryStringToBlob = async (string, type) => {
1480
- try {
1481
- return binaryStringToBlob$1(string, type);
1482
- } catch (error) {
1483
- const normalizedError = normalizeBlobError(error);
1484
- throw new VError(normalizedError, 'Failed to convert binary string to blob');
1485
- }
1486
- };
1487
- const blobToBinaryString = async blob => {
1488
- try {
1489
- return await blobToBinaryString$1(blob);
1490
- } catch (error) {
1491
- const normalizedError = normalizeBlobError(error);
1492
- throw new VError(normalizedError, 'Failed to convert blob to binary string');
1493
- }
1494
- };
1495
-
1496
- const readFile$3 = async uri => {
1497
- const response = await fetch(uri);
1498
- if (!response.ok) {
1499
- throw new Error(response.statusText);
1500
- }
1501
- const result = await response.text();
1502
- return result;
1503
- };
1504
- const readFileAsBlob$1 = async uri => {
1505
- const response = await fetch(uri);
1506
- if (!response.ok) {
1507
- throw new Error(response.statusText);
1508
- }
1509
- const result = await response.blob();
1510
- return result;
1511
- };
1512
- const exists$2 = async uri => {
1513
- const response = await fetch(uri);
1514
- if (response.ok) {
1515
- return true;
1516
- }
1517
- return false;
1518
- };
1519
- const readJson$3 = async uri => {
1520
- const response = await fetch(uri);
1521
- if (!response.ok) {
1522
- throw new Error(`${response.statusText}`);
1523
- }
1524
- const json = await response.json();
1525
- return json;
1526
- };
1527
-
1528
1585
  const {
1529
- invoke: invoke$2,
1530
- setFactory: setFactory$1
1531
- } = createLazyRpc(ExtensionHostWorker);
1586
+ invoke: invoke$2} = createLazyRpc(ExtensionHostWorker);
1532
1587
 
1533
1588
  const RE_PROTOCOL = /^([a-z-]+):\/\//;
1534
1589
  const assertUri = uri => {
@@ -1579,7 +1634,7 @@ const executeWatchCallBack = async id => {
1579
1634
  commandId,
1580
1635
  rpcId
1581
1636
  } = entry;
1582
- const rpc = get(rpcId);
1637
+ const rpc = get$1(rpcId);
1583
1638
  await rpc.invoke(commandId, id);
1584
1639
  };
1585
1640
  const unregisterWatchCallback = id => {
@@ -1670,7 +1725,7 @@ const remove = async dirent => {
1670
1725
  };
1671
1726
  const readFile = async uri => {
1672
1727
  if (isHttp(uri)) {
1673
- return readFile$3(uri);
1728
+ return readFile$4(uri);
1674
1729
  }
1675
1730
  if (isMemory(uri)) {
1676
1731
  return readFile$1(uri);
@@ -1688,7 +1743,7 @@ const getPathSeparator = async root => {
1688
1743
  };
1689
1744
  const readJson = async uri => {
1690
1745
  if (isHttp(uri)) {
1691
- return readJson$3(uri);
1746
+ return readJson$4(uri);
1692
1747
  }
1693
1748
  if (isMemory(uri)) {
1694
1749
  return readJson$1();
@@ -1719,7 +1774,7 @@ const stat = async dirent => {
1719
1774
  };
1720
1775
  const exists = async uri => {
1721
1776
  if (isHttp(uri)) {
1722
- return exists$2(uri);
1777
+ return exists$3(uri);
1723
1778
  }
1724
1779
  return exists$1(uri);
1725
1780
  };
@@ -1770,7 +1825,7 @@ const handleMessagePort = async (port, rpcId) => {
1770
1825
  messagePort: port
1771
1826
  });
1772
1827
  if (rpcId) {
1773
- set$3(rpcId, rpc);
1828
+ set$5(rpcId, rpc);
1774
1829
  }
1775
1830
  };
1776
1831
 
@@ -1841,9 +1896,7 @@ const Directory = 'directory';
1841
1896
  const File = 'file';
1842
1897
 
1843
1898
  const {
1844
- invoke,
1845
- setFactory
1846
- } = createLazyRpc(RendererProcess);
1899
+ invoke} = createLazyRpc(RendererProcess);
1847
1900
 
1848
1901
  /**
1849
1902
  * Do not use directly, use FileSystemHtml.getChildHandles
@@ -1952,40 +2005,42 @@ const commandMap = {
1952
2005
  'Initialize.initialize': initialize
1953
2006
  };
1954
2007
 
1955
- const createExtensionHostRpc = async () => {
2008
+ const initializeExtensionHostWorker = async () => {
1956
2009
  try {
1957
- const rpc = await TransferMessagePortRpcParent.create({
2010
+ const rpc = await LazyTransferMessagePortRpcParent.create({
1958
2011
  commandMap: {},
1959
2012
  send: sendMessagePortToExtensionHostWorker
1960
2013
  });
1961
- return rpc;
2014
+ set$4(rpc);
1962
2015
  } catch (error) {
1963
2016
  throw new VError(error, `Failed to create extension host rpc`);
1964
2017
  }
1965
2018
  };
1966
2019
 
1967
- const createRendererProcessRpc = async () => {
2020
+ const initializeRendererProcess = async () => {
1968
2021
  try {
1969
- const rpc = await TransferMessagePortRpcParent.create({
2022
+ const rpc = await LazyTransferMessagePortRpcParent.create({
1970
2023
  commandMap: {},
1971
2024
  send: sendMessagePortToRendererProcess
1972
2025
  });
1973
- return rpc;
2026
+ set$2(rpc);
1974
2027
  } catch (error) {
1975
2028
  throw new VError(error, `Failed to create renderer process rpc`);
1976
2029
  }
1977
2030
  };
1978
2031
 
1979
- const listen = async () => {
1980
- Object.assign(commandMapRef, commandMap);
1981
- setFactory$1(createExtensionHostRpc);
1982
- setFactory(createRendererProcessRpc);
2032
+ const initializeRendererWorker = async () => {
1983
2033
  const rpc = await WebWorkerRpcClient.create({
1984
2034
  commandMap: commandMap
1985
2035
  });
1986
2036
  set$1(rpc);
1987
2037
  };
1988
2038
 
2039
+ const listen = async () => {
2040
+ Object.assign(commandMapRef, commandMap);
2041
+ await Promise.all([initializeExtensionHostWorker(), initializeRendererProcess(), initializeRendererWorker()]);
2042
+ };
2043
+
1989
2044
  const main = async () => {
1990
2045
  await listen();
1991
2046
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-system-worker",
3
- "version": "4.2.0",
3
+ "version": "5.0.0",
4
4
  "description": "File System Worker",
5
5
  "keywords": [
6
6
  "Lvce Editor"