@lvce-editor/json-rpc 7.2.0 → 8.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.
package/dist/index.d.ts CHANGED
@@ -62,3 +62,7 @@ export const JsonRpcEvent: {
62
62
  export const JsonRequest: {
63
63
  create: (method: string, params: any) => any
64
64
  }
65
+
66
+ export const unwrapJsonRpcResult: any
67
+
68
+ export const restoreJsonRpcError: any
package/dist/index.js CHANGED
@@ -32,8 +32,8 @@ const create$3 = () => {
32
32
  const registerPromise = () => {
33
33
  const id = create$3();
34
34
  const {
35
- resolve,
36
- promise
35
+ promise,
36
+ resolve
37
37
  } = Promise.withResolvers();
38
38
  set(id, resolve);
39
39
  return {
@@ -48,10 +48,10 @@ const create$2 = (method, params) => {
48
48
  promise
49
49
  } = registerPromise();
50
50
  const message = {
51
+ id,
51
52
  jsonrpc: Two,
52
53
  method,
53
- params,
54
- id
54
+ params
55
55
  };
56
56
  return {
57
57
  message,
@@ -83,12 +83,12 @@ const getErrorConstructor = (message, type) => {
83
83
  switch (type) {
84
84
  case DomException:
85
85
  return DOMException;
86
- case TypeError$1:
87
- return TypeError;
88
- case SyntaxError$1:
89
- return SyntaxError;
90
86
  case ReferenceError$1:
91
87
  return ReferenceError;
88
+ case SyntaxError$1:
89
+ return SyntaxError;
90
+ case TypeError$1:
91
+ return TypeError;
92
92
  default:
93
93
  return Error;
94
94
  }
@@ -134,8 +134,10 @@ const getCurrentStack = () => {
134
134
  return currentStack;
135
135
  };
136
136
 
137
- const getNewLineIndex = (string, startIndex = undefined) => {
138
- return string.indexOf(NewLine, startIndex);
137
+ const getNewLineIndex = (string, startIndex) => {
138
+ {
139
+ return string.indexOf(NewLine);
140
+ }
139
141
  };
140
142
 
141
143
  const getParentStack = error => {
@@ -149,55 +151,77 @@ const getParentStack = error => {
149
151
  const MethodNotFound = -32601;
150
152
  const Custom = -32001;
151
153
 
154
+ const restoreExistingError = (error, currentStack) => {
155
+ if (typeof error.stack === 'string') {
156
+ error.stack = error.stack + NewLine + currentStack;
157
+ }
158
+ return error;
159
+ };
160
+ const restoreMethodNotFoundError = (error, currentStack) => {
161
+ const restoredError = new JsonRpcError(error.message);
162
+ const parentStack = getParentStack(error);
163
+ restoredError.stack = parentStack + NewLine + currentStack;
164
+ return restoredError;
165
+ };
166
+ const restoreStackFromData = (restoredError, error, currentStack) => {
167
+ if (error.data.stack && error.data.type && error.message) {
168
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
169
+ return;
170
+ }
171
+ if (error.data.stack) {
172
+ restoredError.stack = error.data.stack;
173
+ }
174
+ };
175
+ const applyDataProperties = (restoredError, error) => {
176
+ if (!error.data) {
177
+ return;
178
+ }
179
+ restoreStackFromData(restoredError, error, getCurrentStack());
180
+ if (error.data.codeFrame) {
181
+ // @ts-ignore
182
+ restoredError.codeFrame = error.data.codeFrame;
183
+ }
184
+ if (error.data.code) {
185
+ // @ts-ignore
186
+ restoredError.code = error.data.code;
187
+ }
188
+ if (error.data.type) {
189
+ // @ts-ignore
190
+ restoredError.name = error.data.type;
191
+ }
192
+ };
193
+ const applyDirectProperties = (restoredError, error) => {
194
+ if (error.stack) {
195
+ const lowerStack = restoredError.stack || '';
196
+ const indexNewLine = getNewLineIndex(lowerStack);
197
+ const parentStack = getParentStack(error);
198
+ // @ts-ignore
199
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
200
+ }
201
+ if (error.codeFrame) {
202
+ // @ts-ignore
203
+ restoredError.codeFrame = error.codeFrame;
204
+ }
205
+ };
206
+ const restoreMessageError = (error, _currentStack) => {
207
+ const restoredError = constructError(error.message, error.type, error.name);
208
+ if (error.data) {
209
+ applyDataProperties(restoredError, error);
210
+ } else {
211
+ applyDirectProperties(restoredError, error);
212
+ }
213
+ return restoredError;
214
+ };
152
215
  const restoreJsonRpcError = error => {
153
216
  const currentStack = getCurrentStack();
154
217
  if (error && error instanceof Error) {
155
- if (typeof error.stack === 'string') {
156
- error.stack = error.stack + NewLine + currentStack;
157
- }
158
- return error;
218
+ return restoreExistingError(error, currentStack);
159
219
  }
160
220
  if (error && error.code && error.code === MethodNotFound) {
161
- const restoredError = new JsonRpcError(error.message);
162
- const parentStack = getParentStack(error);
163
- restoredError.stack = parentStack + NewLine + currentStack;
164
- return restoredError;
221
+ return restoreMethodNotFoundError(error, currentStack);
165
222
  }
166
223
  if (error && error.message) {
167
- const restoredError = constructError(error.message, error.type, error.name);
168
- if (error.data) {
169
- if (error.data.stack && error.data.type && error.message) {
170
- restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
171
- } else if (error.data.stack) {
172
- restoredError.stack = error.data.stack;
173
- }
174
- if (error.data.codeFrame) {
175
- // @ts-ignore
176
- restoredError.codeFrame = error.data.codeFrame;
177
- }
178
- if (error.data.code) {
179
- // @ts-ignore
180
- restoredError.code = error.data.code;
181
- }
182
- if (error.data.type) {
183
- // @ts-ignore
184
- restoredError.name = error.data.type;
185
- }
186
- } else {
187
- if (error.stack) {
188
- const lowerStack = restoredError.stack || '';
189
- // @ts-ignore
190
- const indexNewLine = getNewLineIndex(lowerStack);
191
- const parentStack = getParentStack(error);
192
- // @ts-ignore
193
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
194
- }
195
- if (error.codeFrame) {
196
- // @ts-ignore
197
- restoredError.codeFrame = error.codeFrame;
198
- }
199
- }
200
- return restoredError;
224
+ return restoreMessageError(error);
201
225
  }
202
226
  if (typeof error === 'string') {
203
227
  return new Error(`JsonRpc Error: ${error}`);
@@ -258,28 +282,28 @@ const getErrorProperty = (error, prettyError) => {
258
282
  if (error && error.code === E_COMMAND_NOT_FOUND) {
259
283
  return {
260
284
  code: MethodNotFound,
261
- message: error.message,
262
- data: error.stack
285
+ data: error.stack,
286
+ message: error.message
263
287
  };
264
288
  }
265
289
  return {
266
290
  code: Custom,
267
- message: prettyError.message,
268
291
  data: {
269
- stack: getStack(prettyError),
270
- codeFrame: prettyError.codeFrame,
271
- type: getErrorType(prettyError),
272
292
  code: prettyError.code,
273
- name: prettyError.name
274
- }
293
+ codeFrame: prettyError.codeFrame,
294
+ name: prettyError.name,
295
+ stack: getStack(prettyError),
296
+ type: getErrorType(prettyError)
297
+ },
298
+ message: prettyError.message
275
299
  };
276
300
  };
277
301
 
278
302
  const create$1 = (id, error) => {
279
303
  return {
280
- jsonrpc: Two,
304
+ error,
281
305
  id,
282
- error
306
+ jsonrpc: Two
283
307
  };
284
308
  };
285
309
 
@@ -292,8 +316,8 @@ const getErrorResponse = (id, error, preparePrettyError, logError) => {
292
316
 
293
317
  const create = (message, result) => {
294
318
  return {
295
- jsonrpc: Two,
296
319
  id: message.id,
320
+ jsonrpc: Two,
297
321
  result: result ?? null
298
322
  };
299
323
  };
@@ -305,14 +329,14 @@ const getSuccessResponse = (message, result) => {
305
329
 
306
330
  const getErrorResponseSimple = (id, error) => {
307
331
  return {
308
- jsonrpc: Two,
309
- id,
310
332
  error: {
311
333
  code: Custom,
334
+ data: error,
312
335
  // @ts-ignore
313
- message: error.message,
314
- data: error
315
- }
336
+ message: error.message
337
+ },
338
+ id,
339
+ jsonrpc: Two
316
340
  };
317
341
  };
318
342
 
@@ -344,36 +368,36 @@ const normalizeParams = args => {
344
368
  if (args.length === 1) {
345
369
  const options = args[0];
346
370
  return {
371
+ execute: options.execute,
347
372
  ipc: options.ipc,
373
+ logError: options.logError || defaultLogError,
348
374
  message: options.message,
349
- execute: options.execute,
350
- resolve: options.resolve || defaultResolve,
351
375
  preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
352
- logError: options.logError || defaultLogError,
353
- requiresSocket: options.requiresSocket || defaultRequiresSocket
376
+ requiresSocket: options.requiresSocket || defaultRequiresSocket,
377
+ resolve: options.resolve || defaultResolve
354
378
  };
355
379
  }
356
380
  return {
381
+ execute: args[2],
357
382
  ipc: args[0],
383
+ logError: args[5],
358
384
  message: args[1],
359
- execute: args[2],
360
- resolve: args[3],
361
385
  preparePrettyError: args[4],
362
- logError: args[5],
363
- requiresSocket: args[6]
386
+ requiresSocket: args[6],
387
+ resolve: args[3]
364
388
  };
365
389
  };
366
390
 
367
391
  const handleJsonRpcMessage = async (...args) => {
368
392
  const options = normalizeParams(args);
369
393
  const {
370
- message,
371
- ipc,
372
394
  execute,
373
- resolve,
374
- preparePrettyError,
395
+ ipc,
375
396
  logError,
376
- requiresSocket
397
+ message,
398
+ preparePrettyError,
399
+ requiresSocket,
400
+ resolve
377
401
  } = options;
378
402
  if ('id' in message) {
379
403
  if ('method' in message) {
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@lvce-editor/json-rpc",
3
- "version": "7.2.0",
3
+ "version": "8.1.0",
4
4
  "description": "JsonRpc implementation",
5
5
  "repository": {
6
+ "type": "git",
6
7
  "url": "https://github.com/lvce-editor/json-rpc"
7
8
  },
8
9
  "main": "dist/index.js",
@@ -12,5 +13,6 @@
12
13
  "json-rpc"
13
14
  ],
14
15
  "author": "Lvce Editor",
15
- "license": "MIT"
16
+ "license": "MIT",
17
+ "exports": "./dist/index.js"
16
18
  }