js-rpc2 1.0.3 → 1.0.5
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/package.json +2 -2
- package/src/lib.js +28 -8
- package/src/lib.test.js +45 -1
- package/src/server.js +6 -2
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "js-rpc2",
|
3
|
-
"version": "1.0.
|
3
|
+
"version": "1.0.5",
|
4
4
|
"description": "js web websocket http rpc",
|
5
5
|
"main": "index.js",
|
6
6
|
"type": "module",
|
@@ -26,7 +26,7 @@
|
|
26
26
|
"author": "yuanliwei",
|
27
27
|
"license": "MIT",
|
28
28
|
"devDependencies": {
|
29
|
-
"@types/node": "^22.
|
29
|
+
"@types/node": "^22.13.14",
|
30
30
|
"koa": "^2.15.3",
|
31
31
|
"koa-router": "^13.0.1",
|
32
32
|
"ws": "^8.18.0"
|
package/src/lib.js
CHANGED
@@ -88,11 +88,11 @@ export async function buildBufferData(queue, key, iv) {
|
|
88
88
|
/**
|
89
89
|
* @param {Uint8Array<ArrayBuffer>} buffer
|
90
90
|
* @param {CryptoKey} key
|
91
|
-
* @param {Uint8Array} iv
|
92
|
-
* @returns {Promise<[Uint8Array[],Uint8Array<ArrayBuffer>]>}
|
91
|
+
* @param {Uint8Array<ArrayBuffer>} iv
|
92
|
+
* @returns {Promise<[Uint8Array<ArrayBuffer>[],Uint8Array<ArrayBuffer>]>}
|
93
93
|
*/
|
94
94
|
export async function parseBufferData(buffer, key, iv) {
|
95
|
-
/** @type{Uint8Array[]} */
|
95
|
+
/** @type{Uint8Array<ArrayBuffer>[]} */
|
96
96
|
let queue = []
|
97
97
|
let offset = 0
|
98
98
|
let remain = new Uint8Array(0)
|
@@ -310,7 +310,7 @@ export async function encrypt(data, key, iv) {
|
|
310
310
|
}
|
311
311
|
|
312
312
|
/**
|
313
|
-
* @param {Uint8Array} data
|
313
|
+
* @param {Uint8Array<ArrayBuffer>} data
|
314
314
|
* @param {CryptoKey} key
|
315
315
|
* @param {Uint8Array} iv
|
316
316
|
* @returns
|
@@ -574,7 +574,7 @@ export async function rpcRunServerDecodeBuffer(extension, writer, buffer) {
|
|
574
574
|
box = {
|
575
575
|
id: dataId,
|
576
576
|
type: RPC_TYPE_ERROR,
|
577
|
-
data: buildRpcItemData([
|
577
|
+
data: buildRpcItemData([error.message, error.stack]),
|
578
578
|
}
|
579
579
|
}
|
580
580
|
await writer.write(buildRpcData(box))
|
@@ -593,7 +593,11 @@ export function createRPCProxy(apiInvoke) {
|
|
593
593
|
}
|
594
594
|
proxy = new Proxy(Function, {
|
595
595
|
async apply(_target, _thisArg, argArray) {
|
596
|
-
|
596
|
+
try {
|
597
|
+
return await apiInvoke(String(p), argArray)
|
598
|
+
} catch (error) {
|
599
|
+
throw new Error(error.message, { cause: error })
|
600
|
+
}
|
597
601
|
}
|
598
602
|
})
|
599
603
|
map.set(p, proxy)
|
@@ -628,7 +632,9 @@ export function createRpcServerHelper(param) {
|
|
628
632
|
async close() {
|
629
633
|
await writer.close()
|
630
634
|
}
|
631
|
-
}))
|
635
|
+
})).catch(e => {
|
636
|
+
console.error(e)
|
637
|
+
})
|
632
638
|
|
633
639
|
/** @type{RPC_HELPER_SERVER} */
|
634
640
|
let ret = { writable: decode.writable, readable: encode.readable }
|
@@ -644,6 +650,20 @@ export function createRpcServerHelper(param) {
|
|
644
650
|
* }} RPC_HELPER_CLIENT
|
645
651
|
*/
|
646
652
|
|
653
|
+
class RPCError extends Error {
|
654
|
+
/**
|
655
|
+
* @param {string} message
|
656
|
+
* @param {string} stack
|
657
|
+
* @param {ErrorOptions} [option]
|
658
|
+
*/
|
659
|
+
constructor(message, stack, option) {
|
660
|
+
super(message, option)
|
661
|
+
if (stack) {
|
662
|
+
this.stack = stack
|
663
|
+
}
|
664
|
+
}
|
665
|
+
}
|
666
|
+
|
647
667
|
/**
|
648
668
|
* @param {{ rpcKey: string; }} param
|
649
669
|
*/
|
@@ -666,7 +686,7 @@ export function createRpcClientHelper(param) {
|
|
666
686
|
if (callbackFunctionMap.has(data.id)) {
|
667
687
|
let o = callbackFunctionMap.get(data.id)
|
668
688
|
if (data.type == RPC_TYPE_ERROR) {
|
669
|
-
o.promise.reject(new
|
689
|
+
o.promise.reject(new RPCError(items.at(0).data, items.at(1).data))
|
670
690
|
}
|
671
691
|
if (data.type == RPC_TYPE_RETURN) {
|
672
692
|
callbackFunctionMap.delete(data.id)
|
package/src/lib.test.js
CHANGED
@@ -282,4 +282,48 @@ export async function runWithAbortController(func) {
|
|
282
282
|
await func(ac)
|
283
283
|
await sleep(1000)
|
284
284
|
} finally { ac.abort() }
|
285
|
-
}
|
285
|
+
}
|
286
|
+
|
287
|
+
|
288
|
+
test('error-stack', async () => {
|
289
|
+
// node --test-name-pattern="^error-stack$" src/lib.test.js
|
290
|
+
|
291
|
+
// server
|
292
|
+
const app = new Koa()
|
293
|
+
const router = new Router()
|
294
|
+
app.use(router.routes())
|
295
|
+
app.use(router.allowedMethods())
|
296
|
+
let server = createServer(app.callback()).listen(9000)
|
297
|
+
|
298
|
+
class RpcApi {
|
299
|
+
async hello() {
|
300
|
+
new URL('/')
|
301
|
+
}
|
302
|
+
}
|
303
|
+
|
304
|
+
const extension = new RpcApi()
|
305
|
+
|
306
|
+
createRpcServerKoaRouter({
|
307
|
+
path: '/rpc/abc',
|
308
|
+
router: router,
|
309
|
+
extension: extension,
|
310
|
+
})
|
311
|
+
|
312
|
+
|
313
|
+
// client
|
314
|
+
|
315
|
+
/** @type{RpcApi} */
|
316
|
+
const rpc = createRpcClientHttp({
|
317
|
+
// url: `/rpc/abc`, // in same site html page
|
318
|
+
url: `http://127.0.0.1:9000/rpc/abc`, // others
|
319
|
+
})
|
320
|
+
|
321
|
+
try {
|
322
|
+
let ret = await rpc.hello()
|
323
|
+
console.info(ret)
|
324
|
+
} catch (error) {
|
325
|
+
console.error(error)
|
326
|
+
}
|
327
|
+
server.close()
|
328
|
+
|
329
|
+
})
|
package/src/server.js
CHANGED
@@ -64,13 +64,17 @@ export function createRpcServerWebSocket(param) {
|
|
64
64
|
export function createRpcServerKoaRouter(param) {
|
65
65
|
param.router.post(param.path, async (ctx) => {
|
66
66
|
let helper = createRpcServerHelper({ rpcKey: param.rpcKey, extension: param.extension })
|
67
|
-
|
67
|
+
/** @type{ReadableStream} */
|
68
|
+
let a = Readable.toWeb(ctx.req)
|
69
|
+
await a.pipeTo(helper.writable)
|
68
70
|
ctx.status = 200
|
69
71
|
ctx.response.set({
|
70
72
|
'Connection': 'keep-alive',
|
71
73
|
'Cache-Control': 'no-cache',
|
72
74
|
'Content-Type': 'application/octet-stream'
|
73
75
|
})
|
74
|
-
|
76
|
+
/** @type{object} */
|
77
|
+
let b = helper.readable
|
78
|
+
ctx.body = Readable.fromWeb(b)
|
75
79
|
})
|
76
80
|
}
|