@onting/rpc 0.2.0 → 0.2.1-main.202607160257.a366cf0
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/dist/client.d.ts +5 -3
- package/dist/index.d.ts +8 -6
- package/dist/index.js.map +1 -1
- package/dist/server.d.ts +3 -3
- package/dist/server.js.map +1 -1
- package/package.json +2 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
type
|
|
1
|
+
type AnyStub = Record<string, (...args: any[]) => any>;
|
|
2
2
|
|
|
3
|
-
type StubContract<T extends
|
|
3
|
+
type StubContract<T extends AnyStub> = {
|
|
4
4
|
readonly keys: readonly (keyof T)[];
|
|
5
5
|
['~types']: {
|
|
6
6
|
readonly Stub: T;
|
|
@@ -12,9 +12,11 @@ any> ? T['~types']['Stub'] : never;
|
|
|
12
12
|
|
|
13
13
|
type Promisify<T> = T extends Promise<unknown> ? T : Promise<T>;
|
|
14
14
|
|
|
15
|
+
type PromisifyFunction<T extends (...args: readonly unknown[]) => unknown> = (...args: Parameters<T>) => Promisify<ReturnType<T>>;
|
|
16
|
+
|
|
15
17
|
type InferClient<T extends StubContract<// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
18
|
any>> = {
|
|
17
|
-
readonly [P in keyof InferStub<T>]:
|
|
19
|
+
readonly [P in keyof InferStub<T>]: PromisifyFunction<InferStub<T>[P]>;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
declare function createClientStub<S extends StubContract<// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import { BrowsingContext as BrowsingContext$1, WebDriver } from 'selenium-webdriver';
|
|
2
2
|
|
|
3
|
-
type
|
|
3
|
+
type AnyStub = Record<string, (...args: any[]) => any>;
|
|
4
4
|
|
|
5
|
-
type StubContract<T extends
|
|
5
|
+
type StubContract<T extends AnyStub> = {
|
|
6
6
|
readonly keys: readonly (keyof T)[];
|
|
7
7
|
['~types']: {
|
|
8
8
|
readonly Stub: T;
|
|
9
9
|
};
|
|
10
10
|
};
|
|
11
11
|
|
|
12
|
-
declare function defineContract<S extends
|
|
12
|
+
declare function defineContract<S extends AnyStub>(contract: {
|
|
13
13
|
get keys(): Iterable<keyof S>;
|
|
14
14
|
}): StubContract<S>;
|
|
15
15
|
|
|
@@ -23,7 +23,7 @@ type StubEnvironment = {
|
|
|
23
23
|
readonly webDriver: WebDriver;
|
|
24
24
|
};
|
|
25
25
|
|
|
26
|
-
type StubImplementation<T extends
|
|
26
|
+
type StubImplementation<T extends AnyStub> = StubContract<T> & {
|
|
27
27
|
implement(environment: StubEnvironment): Promise<T>;
|
|
28
28
|
};
|
|
29
29
|
|
|
@@ -34,9 +34,11 @@ any>>(contract: T, implementation: {
|
|
|
34
34
|
|
|
35
35
|
type Promisify<T> = T extends Promise<unknown> ? T : Promise<T>;
|
|
36
36
|
|
|
37
|
+
type PromisifyFunction<T extends (...args: readonly unknown[]) => unknown> = (...args: Parameters<T>) => Promisify<ReturnType<T>>;
|
|
38
|
+
|
|
37
39
|
type InferClient<T extends StubContract<// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
40
|
any>> = {
|
|
39
|
-
readonly [P in keyof InferStub<T>]:
|
|
41
|
+
readonly [P in keyof InferStub<T>]: PromisifyFunction<InferStub<T>[P]>;
|
|
40
42
|
};
|
|
41
43
|
|
|
42
|
-
export { type
|
|
44
|
+
export { type AnyStub, type BrowsingContext, type InferClient, type InferStub, type StubContract, type StubEnvironment, type StubImplementation, defineContract, defineImplementation };
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types/defineContract.ts","../src/types/defineImplementation.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/types/defineContract.ts","../src/types/defineImplementation.ts"],"sourcesContent":["import type { AnyStub } from './AnyStub.ts';\nimport type { StubContract } from './StubContract.ts';\n\nexport default function defineContract<S extends AnyStub>(contract: {\n get keys(): Iterable<keyof S>;\n}): StubContract<S> {\n return {\n keys: Object.freeze([...contract.keys]),\n '~types': {\n Stub: undefined as unknown as S\n }\n };\n}\n","import type { InferStub } from './InferStub.ts';\nimport type { StubContract } from './StubContract.ts';\nimport type { StubEnvironment } from './StubEnvironment.ts';\nimport type { StubImplementation } from './StubImplementation.ts';\n\nexport default function defineImplementation<\n T extends StubContract<// eslint-disable-next-line @typescript-eslint/no-explicit-any\n any>\n>(\n contract: T,\n implementation: {\n implement(environment: StubEnvironment): Promise<InferStub<T>>;\n }\n): StubImplementation<InferStub<T>> {\n return {\n ...contract,\n implement: implementation.implement\n };\n}\n"],"mappings":";AAGe,SAAR,eAAmD,UAEtC;AAClB,SAAO;AAAA,IACL,MAAM,OAAO,OAAO,CAAC,GAAG,SAAS,IAAI,CAAC;AAAA,IACtC,UAAU;AAAA,MACR,MAAM;AAAA,IACR;AAAA,EACF;AACF;;;ACPe,SAAR,qBAIL,UACA,gBAGkC;AAClC,SAAO;AAAA,IACL,GAAG;AAAA,IACH,WAAW,eAAe;AAAA,EAC5B;AACF;","names":[]}
|
package/dist/server.d.ts
CHANGED
|
@@ -7,16 +7,16 @@ type StubEnvironment = {
|
|
|
7
7
|
readonly webDriver: WebDriver;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
-
type
|
|
10
|
+
type AnyStub = Record<string, (...args: any[]) => any>;
|
|
11
11
|
|
|
12
|
-
type StubContract<T extends
|
|
12
|
+
type StubContract<T extends AnyStub> = {
|
|
13
13
|
readonly keys: readonly (keyof T)[];
|
|
14
14
|
['~types']: {
|
|
15
15
|
readonly Stub: T;
|
|
16
16
|
};
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
type StubImplementation<T extends
|
|
19
|
+
type StubImplementation<T extends AnyStub> = StubContract<T> & {
|
|
20
20
|
implement(environment: StubEnvironment): Promise<T>;
|
|
21
21
|
};
|
|
22
22
|
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/server/listen.ts","../src/server/private/createHandshakeStub.ts","../src/server/private/verifyInstance.ts"],"sourcesContent":["import { messagePortRPC as rpc } from 'message-port-rpc';\nimport type { InferStub } from '../types/InferStub.ts';\nimport type { StubEnvironment } from '../types/StubEnvironment.ts';\nimport type { StubImplementation } from '../types/StubImplementation.ts';\nimport createHandshakeStub from './private/createHandshakeStub.ts';\nimport verifyInstance from './private/verifyInstance.ts';\n\nconst PASSTHRU_FN = (value: unknown): unknown => value;\n\nasync function listen<\n T extends StubImplementation<// eslint-disable-next-line @typescript-eslint/no-explicit-any\n any>\n>(\n implementation: T,\n environment: StubEnvironment,\n messagePort: MessagePort,\n init?:\n | {\n readonly marshal?: ((value: unknown) => unknown) | undefined;\n readonly unmarshal?: ((value: unknown) => unknown) | undefined;\n }\n | undefined\n): Promise<() => void> {\n const marshal = init?.marshal ?? PASSTHRU_FN;\n const unmarshal = init?.unmarshal ?? PASSTHRU_FN;\n const instance: InferStub<T> = await implementation.implement(environment);\n\n verifyInstance(implementation, instance);\n\n const { fn, teardown } = createHandshakeStub(implementation, instance, {\n marshal,\n unmarshal\n });\n\n rpc(messagePort, fn);\n\n return teardown;\n}\n\nexport default listen;\n","import { messagePortRPC as rpc } from 'message-port-rpc';\nimport type {
|
|
1
|
+
{"version":3,"sources":["../src/server/listen.ts","../src/server/private/createHandshakeStub.ts","../src/server/private/verifyInstance.ts"],"sourcesContent":["import { messagePortRPC as rpc } from 'message-port-rpc';\nimport type { InferStub } from '../types/InferStub.ts';\nimport type { StubEnvironment } from '../types/StubEnvironment.ts';\nimport type { StubImplementation } from '../types/StubImplementation.ts';\nimport createHandshakeStub from './private/createHandshakeStub.ts';\nimport verifyInstance from './private/verifyInstance.ts';\n\nconst PASSTHRU_FN = (value: unknown): unknown => value;\n\nasync function listen<\n T extends StubImplementation<// eslint-disable-next-line @typescript-eslint/no-explicit-any\n any>\n>(\n implementation: T,\n environment: StubEnvironment,\n messagePort: MessagePort,\n init?:\n | {\n readonly marshal?: ((value: unknown) => unknown) | undefined;\n readonly unmarshal?: ((value: unknown) => unknown) | undefined;\n }\n | undefined\n): Promise<() => void> {\n const marshal = init?.marshal ?? PASSTHRU_FN;\n const unmarshal = init?.unmarshal ?? PASSTHRU_FN;\n const instance: InferStub<T> = await implementation.implement(environment);\n\n verifyInstance(implementation, instance);\n\n const { fn, teardown } = createHandshakeStub(implementation, instance, {\n marshal,\n unmarshal\n });\n\n rpc(messagePort, fn);\n\n return teardown;\n}\n\nexport default listen;\n","import { messagePortRPC as rpc } from 'message-port-rpc';\nimport type { AnyStub } from '../../types/AnyStub.ts';\nimport type { InferStub } from '../../types/InferStub.ts';\nimport type { InferHandshake } from '../../types/internal/InferHandshake.ts';\nimport type { StubContract } from '../../types/StubContract.ts';\n\nfunction createHandshakeStub<T extends StubContract<S>, S extends AnyStub>(\n contract: T,\n instance: S,\n {\n marshal,\n unmarshal\n }: {\n readonly marshal: (value: unknown) => unknown;\n readonly unmarshal: (value: unknown) => unknown;\n }\n): {\n readonly fn: () => InferHandshake<T>;\n readonly teardown: () => void;\n} {\n const openedPorts = new Set<MessagePort>();\n\n return {\n fn() {\n const handshakeResultMap = new Map<keyof InferStub<T>, MessagePort>();\n\n // Prefer StubContract.keys over Object.getOwnPropertyNames(stubInstance).\n for (const key of contract.keys) {\n const value = instance[key];\n\n // We already verified the implementation in `listen()`.\n if (value) {\n const { port1, port2 } = new MessageChannel();\n\n rpc(port1, async (...args) => await marshal(await value(...(await Promise.all(args.map(unmarshal))))));\n handshakeResultMap.set(key, port2);\n\n openedPorts.add(port1);\n }\n }\n\n return Object.fromEntries(handshakeResultMap.entries()) satisfies Record<\n string,\n MessagePort\n > as InferHandshake<T>;\n },\n teardown() {\n for (const port of openedPorts) {\n port.close();\n }\n }\n };\n}\n\nexport default createHandshakeStub;\n","import type { AnyStub } from '../../types/AnyStub';\nimport type { StubContract } from '../../types/StubContract';\n\nfunction verifyInstance<T extends StubContract<S>, S extends AnyStub>(contract: T, instance: S) {\n const declaredKeySet = new Set(contract.keys);\n const implementedKeySet = new Set(Object.getOwnPropertyNames(instance));\n\n const differences = declaredKeySet.symmetricDifference(implementedKeySet);\n\n if (differences.size) {\n throw new Error(`Keys in stub contract does not match implementation: ${Array.from(differences).join(', ')}`);\n }\n}\n\nexport default verifyInstance;\n"],"mappings":";AAAA,SAAS,kBAAkBA,YAAW;;;ACAtC,SAAS,kBAAkB,WAAW;AAMtC,SAAS,oBACP,UACA,UACA;AAAA,EACE;AAAA,EACA;AACF,GAOA;AACA,QAAM,cAAc,oBAAI,IAAiB;AAEzC,SAAO;AAAA,IACL,KAAK;AACH,YAAM,qBAAqB,oBAAI,IAAqC;AAGpE,iBAAW,OAAO,SAAS,MAAM;AAC/B,cAAM,QAAQ,SAAS,GAAG;AAG1B,YAAI,OAAO;AACT,gBAAM,EAAE,OAAO,MAAM,IAAI,IAAI,eAAe;AAE5C,cAAI,OAAO,UAAU,SAAS,MAAM,QAAQ,MAAM,MAAM,GAAI,MAAM,QAAQ,IAAI,KAAK,IAAI,SAAS,CAAC,CAAE,CAAC,CAAC;AACrG,6BAAmB,IAAI,KAAK,KAAK;AAEjC,sBAAY,IAAI,KAAK;AAAA,QACvB;AAAA,MACF;AAEA,aAAO,OAAO,YAAY,mBAAmB,QAAQ,CAAC;AAAA,IAIxD;AAAA,IACA,WAAW;AACT,iBAAW,QAAQ,aAAa;AAC9B,aAAK,MAAM;AAAA,MACb;AAAA,IACF;AAAA,EACF;AACF;AAEA,IAAO,8BAAQ;;;ACnDf,SAAS,eAA6D,UAAa,UAAa;AAC9F,QAAM,iBAAiB,IAAI,IAAI,SAAS,IAAI;AAC5C,QAAM,oBAAoB,IAAI,IAAI,OAAO,oBAAoB,QAAQ,CAAC;AAEtE,QAAM,cAAc,eAAe,oBAAoB,iBAAiB;AAExE,MAAI,YAAY,MAAM;AACpB,UAAM,IAAI,MAAM,wDAAwD,MAAM,KAAK,WAAW,EAAE,KAAK,IAAI,CAAC,EAAE;AAAA,EAC9G;AACF;AAEA,IAAO,yBAAQ;;;AFPf,IAAM,cAAc,CAAC,UAA4B;AAEjD,eAAe,OAIb,gBACA,aACA,aACA,MAMqB;AACrB,QAAM,UAAU,MAAM,WAAW;AACjC,QAAM,YAAY,MAAM,aAAa;AACrC,QAAM,WAAyB,MAAM,eAAe,UAAU,WAAW;AAEzE,yBAAe,gBAAgB,QAAQ;AAEvC,QAAM,EAAE,IAAI,SAAS,IAAI,4BAAoB,gBAAgB,UAAU;AAAA,IACrE;AAAA,IACA;AAAA,EACF,CAAC;AAED,EAAAC,KAAI,aAAa,EAAE;AAEnB,SAAO;AACT;AAEA,IAAO,iBAAQ;","names":["rpc","rpc"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onting/rpc",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1-main.202607160257.a366cf0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"license": "MIT",
|
|
81
81
|
"pinDependencies": {},
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"@onting/rpc": "^0.2.
|
|
83
|
+
"@onting/rpc": "^0.2.1-main.202607160257.a366cf0",
|
|
84
84
|
"message-port-rpc": "^3.0.1",
|
|
85
85
|
"workthru": "^0.1.0"
|
|
86
86
|
}
|