@delight-rpc/child-process 0.6.1 → 0.6.3
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/lib/client.js +10 -10
- package/lib/client.js.map +1 -1
- package/package.json +4 -3
- package/src/client.ts +107 -0
- package/src/index.ts +2 -0
- package/src/server.ts +36 -0
package/lib/client.js
CHANGED
|
@@ -2,17 +2,17 @@ import * as DelightRPC from 'delight-rpc';
|
|
|
2
2
|
import { Deferred } from 'extra-promise';
|
|
3
3
|
import { CustomError } from '@blackglory/errors';
|
|
4
4
|
export function createClient(process, { parameterValidators, expectedVersion, channel } = {}) {
|
|
5
|
-
const pendings =
|
|
5
|
+
const pendings = new Map();
|
|
6
6
|
process.on('message', handler);
|
|
7
7
|
const client = DelightRPC.createClient(async function send(request) {
|
|
8
8
|
const res = new Deferred();
|
|
9
|
-
pendings
|
|
9
|
+
pendings.set(request.id, res);
|
|
10
10
|
try {
|
|
11
11
|
process.send(request);
|
|
12
12
|
return await res;
|
|
13
13
|
}
|
|
14
14
|
finally {
|
|
15
|
-
delete
|
|
15
|
+
pendings.delete(request.id);
|
|
16
16
|
}
|
|
17
17
|
}, {
|
|
18
18
|
parameterValidators,
|
|
@@ -24,28 +24,28 @@ export function createClient(process, { parameterValidators, expectedVersion, ch
|
|
|
24
24
|
process.off('message', handler);
|
|
25
25
|
for (const [key, deferred] of Object.entries(pendings)) {
|
|
26
26
|
deferred.reject(new ClientClosed());
|
|
27
|
-
delete
|
|
27
|
+
pendings.delete(key);
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
function handler(res) {
|
|
31
31
|
var _a;
|
|
32
32
|
if (DelightRPC.isResult(res) || DelightRPC.isError(res)) {
|
|
33
|
-
(_a = pendings
|
|
33
|
+
(_a = pendings.get(res.id)) === null || _a === void 0 ? void 0 : _a.resolve(res);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
export function createBatchClient(process, { expectedVersion, channel } = {}) {
|
|
38
|
-
const pendings =
|
|
38
|
+
const pendings = new Map();
|
|
39
39
|
process.on('message', handler);
|
|
40
40
|
const client = new DelightRPC.BatchClient(async function send(request) {
|
|
41
41
|
const res = new Deferred();
|
|
42
|
-
pendings
|
|
42
|
+
pendings.set(request.id, res);
|
|
43
43
|
try {
|
|
44
44
|
process.send(request);
|
|
45
45
|
return await res;
|
|
46
46
|
}
|
|
47
47
|
finally {
|
|
48
|
-
delete
|
|
48
|
+
pendings.delete(request.id);
|
|
49
49
|
}
|
|
50
50
|
}, {
|
|
51
51
|
expectedVersion,
|
|
@@ -56,13 +56,13 @@ export function createBatchClient(process, { expectedVersion, channel } = {}) {
|
|
|
56
56
|
process.off('message', handler);
|
|
57
57
|
for (const [key, deferred] of Object.entries(pendings)) {
|
|
58
58
|
deferred.reject(new ClientClosed());
|
|
59
|
-
delete
|
|
59
|
+
pendings.delete(key);
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
function handler(res) {
|
|
63
63
|
var _a;
|
|
64
64
|
if (DelightRPC.isError(res) || DelightRPC.isBatchResponse(res)) {
|
|
65
|
-
(_a = pendings
|
|
65
|
+
(_a = pendings.get(res.id)) === null || _a === void 0 ? void 0 : _a.resolve(res);
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
}
|
package/lib/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,aAAa,CAAA;AAEzC,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AACxC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAGhD,MAAM,UAAU,YAAY,CAC1B,OAAsC,EACtC,EAAE,mBAAmB,EAAE,eAAe,EAAE,OAAO,KAI3C,EAAE;IAEN,MAAM,QAAQ,
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources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C,OAAO,CAAC,GAAG,CAAC,IAAI,UAAU,CAAC,eAAe,CAAC,GAAG,CAAC,EAAE;YAC9D,MAAA,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,0CAAE,OAAO,CAAC,GAAG,CAAC,CAAA;SACnC;IACH,CAAC;AACH,CAAC;AAED,MAAM,OAAO,YAAa,SAAQ,WAAW;CAAG"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delight-rpc/child-process",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"files": [
|
|
7
|
-
"lib"
|
|
7
|
+
"lib",
|
|
8
|
+
"src"
|
|
8
9
|
],
|
|
9
10
|
"type": "module",
|
|
10
11
|
"main": "lib/index.js",
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"@commitlint/cli": "^17.4.4",
|
|
38
39
|
"@commitlint/config-conventional": "^17.4.4",
|
|
39
40
|
"@types/jest": "^29.5.0",
|
|
40
|
-
"@types/node": "
|
|
41
|
+
"@types/node": "16",
|
|
41
42
|
"@typescript-eslint/eslint-plugin": "^5.55.0",
|
|
42
43
|
"@typescript-eslint/parser": "^5.55.0",
|
|
43
44
|
"cross-env": "^7.0.3",
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import * as DelightRPC from 'delight-rpc'
|
|
2
|
+
import { ChildProcess } from 'child_process'
|
|
3
|
+
import { Deferred } from 'extra-promise'
|
|
4
|
+
import { CustomError } from '@blackglory/errors'
|
|
5
|
+
import { IRequest, IResponse, IError, IBatchRequest, IBatchResponse } from '@delight-rpc/protocol'
|
|
6
|
+
|
|
7
|
+
export function createClient<IAPI extends object>(
|
|
8
|
+
process: ChildProcess | NodeJS.Process
|
|
9
|
+
, { parameterValidators, expectedVersion, channel }: {
|
|
10
|
+
parameterValidators?: DelightRPC.ParameterValidators<IAPI>
|
|
11
|
+
expectedVersion?: string
|
|
12
|
+
channel?: string
|
|
13
|
+
} = {}
|
|
14
|
+
): [client: DelightRPC.ClientProxy<IAPI>, close: () => void] {
|
|
15
|
+
const pendings: Map<string, Deferred<IResponse<unknown>>> = new Map()
|
|
16
|
+
|
|
17
|
+
process.on('message', handler)
|
|
18
|
+
|
|
19
|
+
const client = DelightRPC.createClient<IAPI>(
|
|
20
|
+
async function send(request: IRequest<unknown>) {
|
|
21
|
+
const res = new Deferred<IResponse<unknown>>()
|
|
22
|
+
pendings.set(request.id, res)
|
|
23
|
+
try {
|
|
24
|
+
process.send!(request)
|
|
25
|
+
return await res
|
|
26
|
+
} finally {
|
|
27
|
+
pendings.delete(request.id)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
, {
|
|
31
|
+
parameterValidators
|
|
32
|
+
, expectedVersion
|
|
33
|
+
, channel
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
return [client, close]
|
|
38
|
+
|
|
39
|
+
function close() {
|
|
40
|
+
process.off('message', handler)
|
|
41
|
+
|
|
42
|
+
for (const [key, deferred] of Object.entries(pendings)) {
|
|
43
|
+
deferred.reject(new ClientClosed())
|
|
44
|
+
pendings.delete(key)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function handler(res: any): void {
|
|
49
|
+
if (DelightRPC.isResult(res) || DelightRPC.isError(res)) {
|
|
50
|
+
pendings.get(res.id)?.resolve(res)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function createBatchClient<DataType>(
|
|
56
|
+
process: ChildProcess | NodeJS.Process
|
|
57
|
+
, { expectedVersion, channel }: {
|
|
58
|
+
expectedVersion?: string
|
|
59
|
+
channel?: string
|
|
60
|
+
} = {}
|
|
61
|
+
): [client: DelightRPC.BatchClient<DataType>, close: () => void] {
|
|
62
|
+
const pendings: Map<
|
|
63
|
+
string
|
|
64
|
+
, Deferred<IError | IBatchResponse<unknown>>
|
|
65
|
+
> = new Map()
|
|
66
|
+
|
|
67
|
+
process.on('message', handler)
|
|
68
|
+
|
|
69
|
+
const client = new DelightRPC.BatchClient(
|
|
70
|
+
async function send(request: IBatchRequest<unknown>) {
|
|
71
|
+
const res = new Deferred<
|
|
72
|
+
| IError
|
|
73
|
+
| IBatchResponse<unknown>
|
|
74
|
+
>()
|
|
75
|
+
pendings.set(request.id, res)
|
|
76
|
+
try {
|
|
77
|
+
process.send!(request)
|
|
78
|
+
return await res
|
|
79
|
+
} finally {
|
|
80
|
+
pendings.delete(request.id)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
, {
|
|
84
|
+
expectedVersion
|
|
85
|
+
, channel
|
|
86
|
+
}
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
return [client, close]
|
|
90
|
+
|
|
91
|
+
function close() {
|
|
92
|
+
process.off('message', handler)
|
|
93
|
+
|
|
94
|
+
for (const [key, deferred] of Object.entries(pendings)) {
|
|
95
|
+
deferred.reject(new ClientClosed())
|
|
96
|
+
pendings.delete(key)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function handler(res: any): void {
|
|
101
|
+
if (DelightRPC.isError(res) || DelightRPC.isBatchResponse(res)) {
|
|
102
|
+
pendings.get(res.id)?.resolve(res)
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export class ClientClosed extends CustomError {}
|
package/src/index.ts
ADDED
package/src/server.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import * as DelightRPC from 'delight-rpc'
|
|
2
|
+
import { ChildProcess } from 'child_process'
|
|
3
|
+
import { isntNull } from '@blackglory/prelude'
|
|
4
|
+
|
|
5
|
+
export function createServer<IAPI extends object>(
|
|
6
|
+
api: DelightRPC.ImplementationOf<IAPI>
|
|
7
|
+
, process: ChildProcess | NodeJS.Process
|
|
8
|
+
, { parameterValidators, version, channel, ownPropsOnly }: {
|
|
9
|
+
parameterValidators?: DelightRPC.ParameterValidators<IAPI>
|
|
10
|
+
version?: `${number}.${number}.${number}`
|
|
11
|
+
channel?: string | RegExp | typeof DelightRPC.AnyChannel
|
|
12
|
+
ownPropsOnly?: boolean
|
|
13
|
+
} = {}
|
|
14
|
+
): () => void {
|
|
15
|
+
process.on('message', handler)
|
|
16
|
+
return () => process.off('message', handler)
|
|
17
|
+
|
|
18
|
+
async function handler(req: any): Promise<void> {
|
|
19
|
+
if (DelightRPC.isRequest(req) || DelightRPC.isBatchRequest(req)) {
|
|
20
|
+
const result = await DelightRPC.createResponse(
|
|
21
|
+
api
|
|
22
|
+
, req
|
|
23
|
+
, {
|
|
24
|
+
parameterValidators
|
|
25
|
+
, version
|
|
26
|
+
, channel
|
|
27
|
+
, ownPropsOnly
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
if (isntNull(result)) {
|
|
32
|
+
process.send!(result)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|