@lvce-editor/json-rpc 2.0.0 → 2.1.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.
Files changed (2) hide show
  1. package/dist/index.js +50 -32
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -150,7 +150,6 @@ const getErrorConstructor = (message, type) => {
150
150
  if (type) {
151
151
  switch (type) {
152
152
  case DomException:
153
- // @ts-ignore
154
153
  return DOMException;
155
154
  case TypeError$1:
156
155
  return TypeError;
@@ -176,7 +175,6 @@ const getErrorConstructor = (message, type) => {
176
175
 
177
176
  const constructError = (message, type, name) => {
178
177
  const ErrorConstructor = getErrorConstructor(message, type);
179
- // @ts-ignore
180
178
  if (ErrorConstructor === DOMException && name) {
181
179
  return new ErrorConstructor(message, name);
182
180
  }
@@ -194,6 +192,14 @@ const getNewLineIndex = (string, startIndex = undefined) => {
194
192
  return string.indexOf(NewLine, startIndex);
195
193
  };
196
194
 
195
+ const getParentStack = error => {
196
+ let parentStack = error.stack || error.data || error.message || '';
197
+ if (parentStack.startsWith(' at')) {
198
+ parentStack = error.message + NewLine + parentStack;
199
+ }
200
+ return parentStack;
201
+ };
202
+
197
203
  const joinLines = lines => {
198
204
  return lines.join(NewLine);
199
205
  };
@@ -205,18 +211,11 @@ const splitLines = lines => {
205
211
  return lines.split(NewLine);
206
212
  };
207
213
 
208
- const getParentStack = error => {
209
- let parentStack = error.stack || error.data || error.message || '';
210
- if (parentStack.startsWith(' at')) {
211
- parentStack = error.message + NewLine + parentStack;
212
- }
213
- return parentStack;
214
- };
215
214
  const restoreJsonRpcError = error => {
216
215
  if (error && error instanceof Error) {
217
216
  return error;
218
217
  }
219
- const currentStack = joinLines(splitLines(new Error().stack).slice(1));
218
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
220
219
  if (error && error.code && error.code === MethodNotFound) {
221
220
  const restoredError = new JsonRpcError(error.message);
222
221
  const parentStack = getParentStack(error);
@@ -245,7 +244,6 @@ const restoreJsonRpcError = error => {
245
244
  }
246
245
  } else {
247
246
  if (error.stack) {
248
- // TODO accessing stack might be slow
249
247
  const lowerStack = restoredError.stack || '';
250
248
  // @ts-ignore
251
249
  const indexNewLine = getNewLineIndex(lowerStack);
@@ -277,22 +275,14 @@ const unwrapJsonRpcResult = responseMessage => {
277
275
  throw new JsonRpcError('unexpected response message');
278
276
  };
279
277
 
280
- const isInstanceOf = (value, constructorName) => {
281
- return value?.constructor?.name === constructorName;
282
- };
283
-
284
- const isSocket = value => {
285
- return isInstanceOf(value, 'Socket');
286
- };
287
-
288
- const isSingleTransferrable = value => {
289
- return isSocket(value);
290
- };
291
-
292
278
  const isMessagePort = value => {
293
279
  return typeof MessagePort !== 'undefined' && value instanceof MessagePort;
294
280
  };
295
281
 
282
+ const isInstanceOf = (value, constructorName) => {
283
+ return value?.constructor?.name === constructorName;
284
+ };
285
+
296
286
  const isMessagePortMain = value => {
297
287
  return isInstanceOf(value, 'MessagePortMain');
298
288
  };
@@ -301,6 +291,10 @@ const isOffscreenCanvas = value => {
301
291
  return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
302
292
  };
303
293
 
294
+ const isSocket = value => {
295
+ return isInstanceOf(value, 'Socket');
296
+ };
297
+
304
298
  const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
305
299
 
306
300
  const isTransferrable = value => {
@@ -312,21 +306,45 @@ const isTransferrable = value => {
312
306
  return false;
313
307
  };
314
308
 
315
- const getTransferrableParams = value => {
309
+ const walkValue = (value, transferrables) => {
310
+ if (!value) {
311
+ return;
312
+ }
316
313
  if (isTransferrable(value)) {
317
- return value;
314
+ transferrables.push(value);
318
315
  }
319
316
  if (Array.isArray(value)) {
320
- const result = value.filter(isTransferrable);
321
- if (result.length === 0) {
322
- return undefined;
317
+ for (const item of value) {
318
+ walkValue(item, transferrables);
323
319
  }
324
- if (result.length === 1 && isSingleTransferrable(result[0])) {
325
- return result[0];
320
+ return;
321
+ }
322
+ if (typeof value === 'object') {
323
+ for (const property of Object.values(value)) {
324
+ walkValue(property, transferrables);
326
325
  }
327
- return result;
328
326
  }
329
- return undefined;
327
+ };
328
+
329
+ const getTransferrables = value => {
330
+ const transferrables = [];
331
+ walkValue(value, transferrables);
332
+ return transferrables;
333
+ };
334
+
335
+ const isSingleTransferrable = value => {
336
+ return isSocket(value);
337
+ };
338
+
339
+ const getTransferrableParams = value => {
340
+ const transferrables = getTransferrables(value);
341
+ if (transferrables.length === 0) {
342
+ return undefined;
343
+ }
344
+ if (isSingleTransferrable(transferrables[0])) {
345
+ return transferrables[0];
346
+ }
347
+ return transferrables;
330
348
  };
331
349
 
332
350
  const create$1 = (message, error) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/json-rpc",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "JsonRpc implementation",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",