@onting/rpc 0.1.1-main.202606281939.15cafe0 → 0.2.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/dist/index.d.ts +2 -2
- package/dist/index.js.map +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/server.js +2 -2
- package/dist/server.js.map +1 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -24,12 +24,12 @@ type StubEnvironment = {
|
|
|
24
24
|
};
|
|
25
25
|
|
|
26
26
|
type StubImplementation<T extends Stub> = StubContract<T> & {
|
|
27
|
-
implement(environment: StubEnvironment): T
|
|
27
|
+
implement(environment: StubEnvironment): Promise<T>;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
30
|
declare function defineImplementation<T extends StubContract<// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
31
|
any>>(contract: T, implementation: {
|
|
32
|
-
implement(environment: StubEnvironment): InferStub<T
|
|
32
|
+
implement(environment: StubEnvironment): Promise<InferStub<T>>;
|
|
33
33
|
}): StubImplementation<InferStub<T>>;
|
|
34
34
|
|
|
35
35
|
type Promisify<T> = T extends Promise<unknown> ? T : Promise<T>;
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types/defineContract.ts","../src/types/defineImplementation.ts"],"sourcesContent":["import type { Stub } from './Stub.ts';\nimport type { StubContract } from './StubContract.ts';\n\nexport default function defineContract<S extends Stub>(contract: { get keys(): Iterable<keyof S> }): 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): InferStub<T
|
|
1
|
+
{"version":3,"sources":["../src/types/defineContract.ts","../src/types/defineImplementation.ts"],"sourcesContent":["import type { Stub } from './Stub.ts';\nimport type { StubContract } from './StubContract.ts';\n\nexport default function defineContract<S extends Stub>(contract: { get keys(): Iterable<keyof S> }): 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,eAAgD,UAA8D;AACnH,SAAO;AAAA,IACL,MAAM,OAAO,OAAO,CAAC,GAAG,SAAS,IAAI,CAAC;AAAA,IACtC,UAAU;AAAA,MACR,MAAM;AAAA,IACR;AAAA,EACF;AACF;;;ACLe,SAAR,qBAIL,UACA,gBAGkC;AAClC,SAAO;AAAA,IACL,GAAG;AAAA,IACH,WAAW,eAAe;AAAA,EAC5B;AACF;","names":[]}
|
package/dist/server.d.ts
CHANGED
|
@@ -17,13 +17,13 @@ type StubContract<T extends Stub> = {
|
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
type StubImplementation<T extends Stub> = StubContract<T> & {
|
|
20
|
-
implement(environment: StubEnvironment): T
|
|
20
|
+
implement(environment: StubEnvironment): Promise<T>;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
23
|
declare function listen<T extends StubImplementation<// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
24
24
|
any>>(implementation: T, environment: StubEnvironment, messagePort: MessagePort, init?: {
|
|
25
25
|
readonly marshal?: ((value: unknown) => unknown) | undefined;
|
|
26
26
|
readonly unmarshal?: ((value: unknown) => unknown) | undefined;
|
|
27
|
-
} | undefined): () => void
|
|
27
|
+
} | undefined): Promise<() => void>;
|
|
28
28
|
|
|
29
29
|
export { listen };
|
package/dist/server.js
CHANGED
|
@@ -44,10 +44,10 @@ var verifyInstance_default = verifyInstance;
|
|
|
44
44
|
|
|
45
45
|
// src/server/listen.ts
|
|
46
46
|
var PASSTHRU_FN = (value) => value;
|
|
47
|
-
function listen(implementation, environment, messagePort, init) {
|
|
47
|
+
async function listen(implementation, environment, messagePort, init) {
|
|
48
48
|
const marshal = init?.marshal ?? PASSTHRU_FN;
|
|
49
49
|
const unmarshal = init?.unmarshal ?? PASSTHRU_FN;
|
|
50
|
-
const instance = implementation.implement(environment);
|
|
50
|
+
const instance = await implementation.implement(environment);
|
|
51
51
|
verifyInstance_default(implementation, instance);
|
|
52
52
|
const { fn, teardown } = createHandshakeStub_default(implementation, instance, {
|
|
53
53
|
marshal,
|
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\
|
|
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 { InferStub } from '../../types/InferStub.ts';\nimport type { InferHandshake } from '../../types/internal/InferHandshake.ts';\nimport type { Stub } from '../../types/Stub.ts';\nimport type { StubContract } from '../../types/StubContract.ts';\n\nfunction createHandshakeStub<T extends StubContract<S>, S extends Stub>(\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 { Stub } from '../../types/Stub';\nimport type { StubContract } from '../../types/StubContract';\n\nfunction verifyInstance<T extends StubContract<S>, S extends Stub>(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,eAA0D,UAAa,UAAa;AAC3F,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.
|
|
3
|
+
"version": "0.2.0",
|
|
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.
|
|
83
|
+
"@onting/rpc": "^0.2.0",
|
|
84
84
|
"message-port-rpc": "^3.0.1",
|
|
85
85
|
"workthru": "^0.1.0"
|
|
86
86
|
}
|