js-rpc2 1.0.7 → 1.1.1
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 +1 -1
- package/src/lib.js +10 -1
- package/src/lib.test.js +7 -4
package/package.json
CHANGED
package/src/lib.js
CHANGED
@@ -589,7 +589,8 @@ export async function rpcRunServerDecodeBuffer(extension, writer, buffer, logger
|
|
589
589
|
}
|
590
590
|
if (logger) {
|
591
591
|
logger(`time: ${Date.now() - time}ms ${fnName}(${params.map(o => {
|
592
|
-
if (typeof o == 'function') { return Function }
|
592
|
+
if (typeof o == 'function') { return `Function()` }
|
593
|
+
if (o instanceof Uint8Array) { return `Uint8Array(${o.length})` }
|
593
594
|
return o
|
594
595
|
}).join(', ')})`)
|
595
596
|
}
|
@@ -744,6 +745,14 @@ export function createRpcClientHelper(param) {
|
|
744
745
|
argArray.push(fnName)
|
745
746
|
for (const arg of args) {
|
746
747
|
if (arg instanceof Function) {
|
748
|
+
const isAsyncFunction = arg.constructor?.name === 'AsyncFunction'
|
749
|
+
if (!isAsyncFunction) {
|
750
|
+
const receivedType = arg.constructor?.name || 'regular function'
|
751
|
+
throw new Error(
|
752
|
+
`Expected an AsyncFunction as the callback, but received a ${receivedType}. ` +
|
753
|
+
'Ensure the callback is declared with "async function" or an arrow function using "async".'
|
754
|
+
)
|
755
|
+
}
|
747
756
|
const key = uniqueKeyID++
|
748
757
|
keys.push(key)
|
749
758
|
callbackFunctionMap.set(key, { id: key, type: RPC_TYPE_CALLBACK, callback: arg })
|
package/src/lib.test.js
CHANGED
@@ -133,13 +133,13 @@ test('测试RPC调用-WebSocket', async () => {
|
|
133
133
|
console.info(string)
|
134
134
|
strictEqual(string, 'hello asdfghjkl')
|
135
135
|
|
136
|
-
let stringcallback = await client.callback('asdfghjkl', (progress) => {
|
136
|
+
let stringcallback = await client.callback('asdfghjkl', async (progress) => {
|
137
137
|
console.info(`client : ${progress}`)
|
138
138
|
})
|
139
139
|
strictEqual(stringcallback, 'hello callback asdfghjkl')
|
140
140
|
|
141
141
|
let callbackCount = 0
|
142
|
-
let stringcallback2 = await client.callback2('asdfghjkl', (progress, buffer) => {
|
142
|
+
let stringcallback2 = await client.callback2('asdfghjkl', async (progress, buffer) => {
|
143
143
|
console.info(`client : ${progress}`, buffer)
|
144
144
|
callbackCount++
|
145
145
|
})
|
@@ -211,6 +211,9 @@ test('测试RPC调用-KoaRouter', async () => {
|
|
211
211
|
router: router,
|
212
212
|
rpcKey: '11474f3dfbb861700cb6c3864b328311',
|
213
213
|
extension: extension,
|
214
|
+
logger(msg) {
|
215
|
+
console.info(msg)
|
216
|
+
},
|
214
217
|
})
|
215
218
|
|
216
219
|
server.addListener('request', app.callback())
|
@@ -233,7 +236,7 @@ test('测试RPC调用-KoaRouter', async () => {
|
|
233
236
|
strictEqual(string, 'hello asdfghjkl')
|
234
237
|
|
235
238
|
let callbackCount = 0
|
236
|
-
let stringcallback = await client.callback('asdfghjkl', (progress) => {
|
239
|
+
let stringcallback = await client.callback('asdfghjkl', async (progress) => {
|
237
240
|
console.info(`client : ${progress}`)
|
238
241
|
callbackCount++
|
239
242
|
})
|
@@ -241,7 +244,7 @@ test('测试RPC调用-KoaRouter', async () => {
|
|
241
244
|
strictEqual(3, callbackCount)
|
242
245
|
strictEqual(stringcallback, 'hello callback asdfghjkl')
|
243
246
|
|
244
|
-
let stringcallback2 = await client.callback2('asdfghjkl', (progress, buffer) => {
|
247
|
+
let stringcallback2 = await client.callback2('asdfghjkl', async (progress, buffer) => {
|
245
248
|
console.info(`client : ${progress}`, buffer)
|
246
249
|
})
|
247
250
|
strictEqual(stringcallback2, 'hello callback asdfghjkl')
|