js-rpc2 2.5.1 → 2.6.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/package.json +1 -1
- package/src/lib.js +16 -29
- package/src/lib.test.js +16 -1
- package/src/test-worker.js +13 -0
package/package.json
CHANGED
package/src/lib.js
CHANGED
|
@@ -893,7 +893,7 @@ export function base64encode(buffer) {
|
|
|
893
893
|
const chars = []
|
|
894
894
|
for (let i = 0; i < buffer.length; i += CHUNK_SZ) {
|
|
895
895
|
const chunk = buffer.subarray(i, i + CHUNK_SZ)
|
|
896
|
-
chars.push(String.fromCharCode.apply(null, chunk))
|
|
896
|
+
chars.push(String.fromCharCode.apply(null, Array.from(chunk)))
|
|
897
897
|
}
|
|
898
898
|
return btoa(chars.join(''))
|
|
899
899
|
}
|
|
@@ -993,42 +993,29 @@ export function createRpcClientChromeExtensions(param) {
|
|
|
993
993
|
|
|
994
994
|
/**
|
|
995
995
|
* @param {{
|
|
996
|
-
* parentPort: NodeJSMessagePort;
|
|
996
|
+
* parentPort: MessagePort|NodeJSMessagePort;
|
|
997
997
|
* extension: Object;
|
|
998
998
|
* logger?:(msg:string)=>void;
|
|
999
999
|
* }} param
|
|
1000
1000
|
*/
|
|
1001
|
-
export function
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
})
|
|
1010
|
-
helper.readable.pipeTo(new WritableStream({
|
|
1011
|
-
async write(chunk) {
|
|
1012
|
-
port.postMessage(chunk)
|
|
1013
|
-
}
|
|
1014
|
-
}))
|
|
1001
|
+
export function createRpcServerWorker(param) {
|
|
1002
|
+
/** @type{any} */
|
|
1003
|
+
const parentPort = param.parentPort
|
|
1004
|
+
parentPort.onmessage = (/** @type {{ ports: [any]; }} */ event) => {
|
|
1005
|
+
parentPort.onmessage = null
|
|
1006
|
+
let [port] = event.ports
|
|
1007
|
+
createRpcServerMessagePort({ port, rpcKey: '', extension: param.extension, logger: param.logger })
|
|
1008
|
+
}
|
|
1015
1009
|
}
|
|
1016
1010
|
|
|
1017
1011
|
/**
|
|
1018
1012
|
* @param {{
|
|
1019
|
-
* worker:NodeJSWorker;
|
|
1013
|
+
* worker:Worker|NodeJSWorker;
|
|
1020
1014
|
* }} param
|
|
1021
1015
|
*/
|
|
1022
|
-
export function
|
|
1023
|
-
let
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
param.worker.postMessage(chunk)
|
|
1028
|
-
}
|
|
1029
|
-
}))
|
|
1030
|
-
param.worker.on('message', async (data) => {
|
|
1031
|
-
await writer.write(data)
|
|
1032
|
-
})
|
|
1033
|
-
return createRPCProxy(helper.apiInvoke)
|
|
1016
|
+
export function createRpcClientWorker(param) {
|
|
1017
|
+
let channel = new MessageChannel()
|
|
1018
|
+
const client = createRpcClientMessagePort({ port: channel.port1, rpcKey: '' })
|
|
1019
|
+
param.worker.postMessage(channel.port2, [/** @type{any} */(channel.port2)])
|
|
1020
|
+
return client
|
|
1034
1021
|
}
|
package/src/lib.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { test } from 'node:test'
|
|
2
2
|
import { deepStrictEqual, fail, ok, strictEqual } from 'node:assert'
|
|
3
|
-
import { _testCreateRpcClientHttp, createRpcClientHttp, createRpcClientWebSocket, sleep, Uint8Array_from } from './lib.js'
|
|
3
|
+
import { _testCreateRpcClientHttp, createRpcClientHttp, createRpcClientWorker, createRpcClientWebSocket, sleep, Uint8Array_from } from './lib.js'
|
|
4
4
|
import { createServer } from 'node:http'
|
|
5
5
|
import { WebSocketServer } from 'ws'
|
|
6
6
|
import Koa from 'koa'
|
|
@@ -10,6 +10,11 @@ import { AsyncLocalStorage } from 'node:async_hooks'
|
|
|
10
10
|
import { Readable, Transform } from 'node:stream'
|
|
11
11
|
import { pipeline } from 'node:stream/promises'
|
|
12
12
|
import { Packr } from 'msgpackr'
|
|
13
|
+
import { Worker } from 'node:worker_threads'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @import {ExtensionApi} from './test-worker.js'
|
|
17
|
+
*/
|
|
13
18
|
|
|
14
19
|
test('basic', async () => {
|
|
15
20
|
// node --test-name-pattern="^basic$" src/lib.test.js
|
|
@@ -586,4 +591,14 @@ test('msgpackr', async () => {
|
|
|
586
591
|
delete data['error']
|
|
587
592
|
delete value['error']
|
|
588
593
|
deepStrictEqual(data, value)
|
|
594
|
+
})
|
|
595
|
+
|
|
596
|
+
test('test-rpc-NodeJSWorker', async () => {
|
|
597
|
+
// node --test --test-name-pattern="^test-rpc-NodeJSWorker$" src/lib.test.js
|
|
598
|
+
const worker = new Worker(new URL('./test-worker.js', import.meta.url))
|
|
599
|
+
/** @type{ExtensionApi} */
|
|
600
|
+
const rpc = createRpcClientWorker({ worker: worker })
|
|
601
|
+
console.info(await rpc.hello('123'))
|
|
602
|
+
deepStrictEqual(await rpc.hello('123'), 'hello 123')
|
|
603
|
+
await worker.terminate()
|
|
589
604
|
})
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { parentPort } from 'node:worker_threads'
|
|
2
|
+
import { createRpcServerWorker } from './lib.js'
|
|
3
|
+
|
|
4
|
+
export const ExtensionApi = {
|
|
5
|
+
/**
|
|
6
|
+
* @param {any} name
|
|
7
|
+
*/
|
|
8
|
+
async hello(name) {
|
|
9
|
+
return `hello ${name}`
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
createRpcServerWorker({ parentPort, extension: ExtensionApi, })
|