js-rpc2 1.0.1 → 1.0.2
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 +27 -13
- package/src/lib.test.js +19 -1
package/package.json
CHANGED
package/src/lib.js
CHANGED
@@ -86,10 +86,10 @@ export async function buildBufferData(queue, key, iv) {
|
|
86
86
|
}
|
87
87
|
|
88
88
|
/**
|
89
|
-
* @param {Uint8Array} buffer
|
89
|
+
* @param {Uint8Array<ArrayBuffer>} buffer
|
90
90
|
* @param {CryptoKey} key
|
91
91
|
* @param {Uint8Array} iv
|
92
|
-
* @returns {Promise<[Uint8Array[],Uint8Array]>}
|
92
|
+
* @returns {Promise<[Uint8Array[],Uint8Array<ArrayBuffer>]>}
|
93
93
|
*/
|
94
94
|
export async function parseBufferData(buffer, key, iv) {
|
95
95
|
/** @type{Uint8Array[]} */
|
@@ -627,6 +627,10 @@ export function createRpcServerHelper(param) {
|
|
627
627
|
* writable: WritableStream<Uint8Array>;
|
628
628
|
* readable: ReadableStream<Uint8Array>;
|
629
629
|
* apiInvoke: (fnName: string, args: object[]) => Promise<object>;
|
630
|
+
* transform:()=>{
|
631
|
+
* readable: ReadableStream<Uint8Array<ArrayBufferLike>>;
|
632
|
+
* witeable: WritableStream<Uint8Array<ArrayBufferLike>>;
|
633
|
+
* }
|
630
634
|
* }} RPC_HELPER_CLIENT
|
631
635
|
*/
|
632
636
|
|
@@ -710,8 +714,15 @@ export function createRpcClientHelper(param) {
|
|
710
714
|
}
|
711
715
|
}
|
712
716
|
|
717
|
+
function transform() {
|
718
|
+
let decode = createDecodeStream(rpc_key_iv)
|
719
|
+
let encode = createEncodeStream(rpc_key_iv)
|
720
|
+
decode.readable.pipeTo(encode.writable)
|
721
|
+
return { readable: encode.readable, witeable: decode.writable }
|
722
|
+
}
|
723
|
+
|
713
724
|
/** @type{RPC_HELPER_CLIENT} */
|
714
|
-
let ret = { writable: decode.writable, readable: encode.readable, apiInvoke }
|
725
|
+
let ret = { writable: decode.writable, readable: encode.readable, apiInvoke, transform }
|
715
726
|
return ret
|
716
727
|
}
|
717
728
|
|
@@ -790,20 +801,23 @@ export function createRpcClientHttp(param) {
|
|
790
801
|
let helper = createRpcClientHelper({ rpcKey: param.rpcKey })
|
791
802
|
let writer = helper.writable.getWriter()
|
792
803
|
helper.readable.pipeTo(new WritableStream({
|
793
|
-
|
794
|
-
|
804
|
+
write(chunk) {
|
805
|
+
fetch(param.url, {
|
795
806
|
method: 'POST',
|
796
807
|
signal: param.signal,
|
797
808
|
body: chunk,
|
798
|
-
})
|
799
|
-
|
800
|
-
|
801
|
-
}
|
802
|
-
res.body.pipeTo(new WritableStream({
|
803
|
-
async write(chunk) {
|
804
|
-
await writer.write(chunk)
|
809
|
+
}).then(res => {
|
810
|
+
if (param.intercept) {
|
811
|
+
param.intercept(res)
|
805
812
|
}
|
806
|
-
|
813
|
+
let transform = helper.transform()
|
814
|
+
res.body.pipeTo(transform.witeable).catch(e => console.error(e))
|
815
|
+
transform.readable.pipeTo(new WritableStream({
|
816
|
+
async write(chunk) {
|
817
|
+
await writer.write(chunk)
|
818
|
+
}
|
819
|
+
})).catch((e) => console.error(e))
|
820
|
+
}).catch(e => console.error(e))
|
807
821
|
}
|
808
822
|
})).catch((err) => console.error('createRpcClientHttp', err.message))
|
809
823
|
return createRPCProxy(helper.apiInvoke)
|
package/src/lib.test.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { test } from 'node:test'
|
2
|
-
import { deepStrictEqual, strictEqual } from 'node:assert'
|
2
|
+
import { deepStrictEqual, ok, strictEqual } from 'node:assert'
|
3
3
|
import { createRpcClientHttp, createRpcClientWebSocket, sleep, Uint8Array_from } from './lib.js'
|
4
4
|
import { createServer } from 'node:http'
|
5
5
|
import { WebSocketServer } from 'ws'
|
@@ -193,6 +193,10 @@ test('测试RPC调用-KoaRouter', async () => {
|
|
193
193
|
},
|
194
194
|
void: async function (/** @type {string} */ name,/** @type {Uint8Array} */ buffer) {
|
195
195
|
console.info('call void')
|
196
|
+
},
|
197
|
+
longTimeBlock: async function () {
|
198
|
+
await sleep(1000)
|
199
|
+
return 'finished'
|
196
200
|
}
|
197
201
|
}
|
198
202
|
|
@@ -251,6 +255,20 @@ test('测试RPC调用-KoaRouter', async () => {
|
|
251
255
|
|
252
256
|
let retvoid = await client.void('asdfghjkl', buffer)
|
253
257
|
strictEqual(retvoid, undefined)
|
258
|
+
|
259
|
+
let startTime = performance.now()
|
260
|
+
let [time1, time2] = await Promise.all([
|
261
|
+
client.longTimeBlock().then(v => {
|
262
|
+
console.info('longTimeBlock', v)
|
263
|
+
return performance.now() - startTime
|
264
|
+
}),
|
265
|
+
client.hello('欣瑶').then(v => {
|
266
|
+
console.info('hello', v)
|
267
|
+
return performance.now() - startTime
|
268
|
+
})
|
269
|
+
])
|
270
|
+
ok(time1 > 1000, `time1:${time1}`)
|
271
|
+
ok(time2 < 10, `time2:${time2}`)
|
254
272
|
})
|
255
273
|
console.info('over!')
|
256
274
|
})
|