@delight-rpc/piscina 0.5.0 → 0.5.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 CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@delight-rpc/piscina",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
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",
@@ -62,7 +63,7 @@
62
63
  },
63
64
  "dependencies": {
64
65
  "@blackglory/prelude": "^0.3.1",
65
- "@delight-rpc/protocol": "^3.0.1"
66
+ "@delight-rpc/protocol": "^4.0.0"
66
67
  },
67
68
  "peerDependencies": {
68
69
  "delight-rpc": "^5.0.0 || ^6.0.0",
package/src/client.ts ADDED
@@ -0,0 +1,47 @@
1
+ import * as DelightRPC from 'delight-rpc'
2
+ import Piscina from 'piscina'
3
+ import { IRequest, IBatchRequest } from '@delight-rpc/protocol'
4
+
5
+ export function createClient<IAPI extends object>(
6
+ piscina: Piscina
7
+ , { parameterValidators, expectedVersion, channel }: {
8
+ parameterValidators?: DelightRPC.ParameterValidators<IAPI>
9
+ expectedVersion?: string
10
+ channel?: string
11
+ } = {}
12
+ ): DelightRPC.ClientProxy<IAPI> {
13
+ const client = DelightRPC.createClient<IAPI>(
14
+ createSend(piscina)
15
+ , {
16
+ parameterValidators
17
+ , expectedVersion
18
+ , channel
19
+ }
20
+ )
21
+
22
+ return client
23
+ }
24
+
25
+ export function createBatchClient<DataType>(
26
+ piscina: Piscina
27
+ , { expectedVersion, channel }: {
28
+ expectedVersion?: string
29
+ channel?: string
30
+ } = {}
31
+ ): DelightRPC.BatchClient {
32
+ const client = new DelightRPC.BatchClient<DataType>(
33
+ createSend(piscina)
34
+ , {
35
+ expectedVersion
36
+ , channel
37
+ }
38
+ )
39
+
40
+ return client
41
+ }
42
+
43
+ function createSend<T>(
44
+ piscina: Piscina
45
+ ): (request: IRequest<unknown> | IBatchRequest<unknown>) => Promise<T> {
46
+ return async request => await piscina.run(request)
47
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './client.js'
2
+ export * from './server.js'
package/src/server.ts ADDED
@@ -0,0 +1,31 @@
1
+ import * as DelightRPC from 'delight-rpc'
2
+ import { isntNull } from '@blackglory/prelude'
3
+
4
+ export function createServer<IAPI extends object>(
5
+ api: DelightRPC.ImplementationOf<IAPI>
6
+ , { parameterValidators, version, channel, ownPropsOnly }: {
7
+ parameterValidators?: DelightRPC.ParameterValidators<IAPI>
8
+ version?: `${number}.${number}.${number}`
9
+ channel?: string | RegExp | typeof DelightRPC.AnyChannel
10
+ ownPropsOnly?: boolean
11
+ } = {}
12
+ ): (req: unknown) => Promise<unknown> {
13
+ return async function handler(req: unknown): Promise<unknown> {
14
+ if (DelightRPC.isRequest(req) || DelightRPC.isBatchRequest(req)) {
15
+ const response = await DelightRPC.createResponse(
16
+ api
17
+ , req
18
+ , {
19
+ parameterValidators
20
+ , version
21
+ , channel
22
+ , ownPropsOnly
23
+ }
24
+ )
25
+
26
+ if (isntNull(response)) {
27
+ return response
28
+ }
29
+ }
30
+ }
31
+ }