@lvce-editor/json-rpc 2.0.0 → 3.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.
Files changed (2) hide show
  1. package/dist/index.js +10 -66
  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,58 +275,6 @@ 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
- const isMessagePort = value => {
293
- return typeof MessagePort !== 'undefined' && value instanceof MessagePort;
294
- };
295
-
296
- const isMessagePortMain = value => {
297
- return isInstanceOf(value, 'MessagePortMain');
298
- };
299
-
300
- const isOffscreenCanvas = value => {
301
- return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
302
- };
303
-
304
- const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
305
-
306
- const isTransferrable = value => {
307
- for (const fn of transferrables) {
308
- if (fn(value)) {
309
- return true;
310
- }
311
- }
312
- return false;
313
- };
314
-
315
- const getTransferrableParams = value => {
316
- if (isTransferrable(value)) {
317
- return value;
318
- }
319
- if (Array.isArray(value)) {
320
- const result = value.filter(isTransferrable);
321
- if (result.length === 0) {
322
- return undefined;
323
- }
324
- if (result.length === 1 && isSingleTransferrable(result[0])) {
325
- return result[0];
326
- }
327
- return result;
328
- }
329
- return undefined;
330
- };
331
-
332
278
  const create$1 = (message, error) => {
333
279
  return {
334
280
  jsonrpc: Two,
@@ -462,17 +408,15 @@ const invoke = async (ipc, method, ...params) => {
462
408
  // TODO deprecated old typings,
463
409
  // always use automatic transferrable detection
464
410
  const invokeAndTransfer = async (ipc, handle, method, ...params) => {
465
- let transfer = handle;
466
411
  if (typeof handle === 'string') {
467
412
  params = [method, ...params];
468
413
  method = handle;
469
- transfer = getTransferrableParams(params);
470
414
  }
471
415
  const {
472
416
  message,
473
417
  promise
474
418
  } = create$2(method, params);
475
- ipc.sendAndTransfer(message, transfer);
419
+ ipc.sendAndTransfer(message);
476
420
  const responseMessage = await promise;
477
421
  const result = unwrapJsonRpcResult(responseMessage);
478
422
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/json-rpc",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "JsonRpc implementation",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",