@webpieces/ipc-bridge 0.4.751
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/README.md +53 -0
- package/package.json +27 -0
- package/src/IpcClientFactory.d.ts +11 -0
- package/src/IpcClientFactory.js +60 -0
- package/src/IpcClientFactory.js.map +1 -0
- package/src/IpcServerFactory.d.ts +16 -0
- package/src/IpcServerFactory.js +68 -0
- package/src/IpcServerFactory.js.map +1 -0
- package/src/index.d.ts +3 -0
- package/src/index.js +8 -0
- package/src/index.js.map +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Portable IPC bridge
|
|
2
|
+
|
|
3
|
+
`@webpieces/ipc-bridge` exports both `IpcClientFactory` and `IpcServerFactory` for browser, React Native and Node. Install one package on each side of a trusted JSON transport connection. Either side can create client proxies and register server implementations.
|
|
4
|
+
|
|
5
|
+
```typescript
|
|
6
|
+
import { IpcClientFactory, IpcServerFactory } from '@webpieces/ipc-bridge';
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Declare a shared abstract API and an `IpcContract` from `@webpieces/core-util/ipc` beside it. Its `IpcMethods<Api>` mapping requires one `IpcMethod` per API member. Each method declares an explicit stable wire ID, request schema, response schema, masking spec and request/notification kind. Schemas implement `parse(unknown)` and validate/reconstruct a DTO. Methods accept exactly one non-null DTO and return a Promise. Use `IpcVoidSchema` for acknowledged void. Notifications also acknowledge failures; they never silently swallow them.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
abstract class PlaybackApi {
|
|
13
|
+
abstract play(request: PlayRequest): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
class PlaybackMethods {
|
|
16
|
+
readonly play = new IpcMethod('play.v1', new PlayRequestSchema(), new IpcVoidSchema(), new MaskSpec({}));
|
|
17
|
+
}
|
|
18
|
+
const playbackContract = new IpcContract('playback.v1', PlaybackApi, new PlaybackMethods());
|
|
19
|
+
const clients = new IpcClientFactory(connection, logging);
|
|
20
|
+
const playback = clients.createClient(playbackContract);
|
|
21
|
+
await playback.play(new PlayRequest('track-id'));
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The schema, DTO, IDs and metadata live in the shared contract package. The names of the abstract class and controller are never sent on the wire. Proxy inspection, symbols and `then` do not send requests.
|
|
25
|
+
|
|
26
|
+
Construct `IpcConnection` with an `IpcTransport` adapter and `IpcConnectionOptions`: a positive timeout, a connection-wide unique ID generator, a scheduler returning timer cancellation functions, and an error owner. The adapter sends strings and subscribes once to incoming strings, transport errors and close events. Its send Promise must reject on send failure; parse/message-error callbacks go to the failure subscriber. A vendor bus adapter must not expose vendor Node declarations through its public contract. Do not install a second vendor handler for every API.
|
|
27
|
+
|
|
28
|
+
`IpcLogging.context(call)` is required and supplies an active per-call `ApiCallContext` carrying the transaction/call/parent IDs in the host logger. Both factories invoke framework `LogApiCallImpl`; receiver logging happens before encoding failures and sender completion logging happens after decoding. `UserError` is rethrown but remains a successful monitoring outcome. Configure the normal framework logging backend at bootstrap. Masking affects logs, never wire DTOs.
|
|
29
|
+
|
|
30
|
+
Use `clients.withContext(inboundContext)` when making nested calls; this copies the transaction and makes the inbound call the parent. No ambient mutable state is held across awaits. `createScoped` on the server supports constructing a controller with a scoped client factory.
|
|
31
|
+
|
|
32
|
+
Timeout rejects with the existing `TimeoutError`. Send/close/disposal rejects with local `IpcTransportError`, preserving the original failure as standard `cause`. This extends `Error`, not semantic `ApiError`, and is never a remote wire category. A receiver throwing `ServiceUnavailableError` still arrives as that canonical semantic error, so callers can distinguish a failed local bridge from an unavailable remote implementation. A timeout does not cancel remote side effects, and the framework never replays requests. `dispose()` rejects pending requests and closes the adapter. Late replies are reported and ignored. Malformed envelopes and correlation mismatches fail the connection, including its pending calls. Observe all OS-callback send Promises or route rejections to the application's explicit error owner.
|
|
33
|
+
|
|
34
|
+
Host responsibilities: validate WebView origins/navigation/session identity and expose only authorized APIs. Suspension/resume, coalescing native state, durable usage journals and installed shell protocol minimums are application policies. Generic IPC cannot make suspended website JavaScript execute. Android/iOS lifecycle and locked-screen tests remain downstream integration work.
|
|
35
|
+
|
|
36
|
+
The required compatibility build checks published declarations without Node/DOM ambient types, scans transitive runtime dependencies, bundles Android/iOS with Metro and compiles Hermes bytecode. Hermes/device execution is a separate verification claim and must only be made when actually run.
|
|
37
|
+
|
|
38
|
+
## Receiver registration
|
|
39
|
+
|
|
40
|
+
`IpcServerFactory` registers explicit shared contracts and already-constructed implementations. It does not create a bus or import an HTTP server, DI container, React Native runtime or Node request context.
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
const server = new IpcServerFactory(logging);
|
|
44
|
+
server.create(playbackContract, new NativePlaybackController(player));
|
|
45
|
+
server.create(navigationContract, new NavigationController(router));
|
|
46
|
+
connection.setHandler(server.handle); // exactly once, before sending any calls
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
One connection has one dispatcher containing multiple APIs. Duplicate registrations fail. Unknown API/method IDs return canonical `EndpointNotFoundError`, distinct from a missing domain entity. Schemas validate both request and reply; successful void is encoded explicitly as null. Receiver exceptions are logged then encoded with the same `ApiErrorCodec` used by the sender to reconstruct canonical errors.
|
|
50
|
+
|
|
51
|
+
`createScoped(contract, scope)` accepts an `IpcControllerScope<T>` whose `create(context)` supplies the implementation for that invocation. Inject a `clients.withContext(context)` proxy into this controller when it calls back or invokes another API. Concurrent calls have separate context objects and do not overwrite a global async context.
|
|
52
|
+
|
|
53
|
+
A host may register receivers and create client proxies on both ends of the same connection. `IpcConnection` owns transport parse/send failures and disposal; see the connection and logging contracts above for the transport and logging contracts, error ownership, security responsibilities and downstream native lifecycle tests.
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webpieces/ipc-bridge",
|
|
3
|
+
"version": "0.4.751",
|
|
4
|
+
"description": "Portable typed duplex IPC client and server factories for browser, React Native and Node",
|
|
5
|
+
"type": "commonjs",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./src/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.d.ts",
|
|
11
|
+
"default": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"author": "Dean Hiller",
|
|
15
|
+
"license": "Apache-2.0",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/deanhiller/webpieces-ts.git",
|
|
19
|
+
"directory": "packages/core/ipc-bridge"
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@webpieces/core-util": "0.4.751"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IpcCallContext, IpcConnection, IpcContract, IpcLogging } from '@webpieces/core-util/ipc';
|
|
2
|
+
/** Typed proxies on one trusted duplex connection; no HTTP decorators or container required. */
|
|
3
|
+
export declare class IpcClientFactory {
|
|
4
|
+
private readonly connection;
|
|
5
|
+
private readonly logging;
|
|
6
|
+
private readonly parent?;
|
|
7
|
+
constructor(connection: IpcConnection, logging: IpcLogging, parent?: IpcCallContext | undefined);
|
|
8
|
+
/** Explicit scope propagation is safe across concurrent async calls in browser and RN. */
|
|
9
|
+
withContext(context: IpcCallContext): IpcClientFactory;
|
|
10
|
+
createClient<T extends object>(contract: IpcContract<T>): T;
|
|
11
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IpcClientFactory = void 0;
|
|
4
|
+
const errors_1 = require("@webpieces/core-util/errors");
|
|
5
|
+
const ipc_1 = require("@webpieces/core-util/ipc");
|
|
6
|
+
/** Typed proxies on one trusted duplex connection; no HTTP decorators or container required. */
|
|
7
|
+
class IpcClientFactory {
|
|
8
|
+
connection;
|
|
9
|
+
logging;
|
|
10
|
+
parent;
|
|
11
|
+
constructor(connection, logging, parent) {
|
|
12
|
+
this.connection = connection;
|
|
13
|
+
this.logging = logging;
|
|
14
|
+
this.parent = parent;
|
|
15
|
+
}
|
|
16
|
+
/** Explicit scope propagation is safe across concurrent async calls in browser and RN. */
|
|
17
|
+
withContext(context) {
|
|
18
|
+
return new IpcClientFactory(this.connection, this.logging, context);
|
|
19
|
+
}
|
|
20
|
+
createClient(contract) {
|
|
21
|
+
// webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
22
|
+
const methods = new Map();
|
|
23
|
+
for (const key of Object.keys(contract.methods)) {
|
|
24
|
+
// webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
25
|
+
const method = contract.methods[key];
|
|
26
|
+
// webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
27
|
+
methods.set(key, async (request) => {
|
|
28
|
+
const context = this.connection.newContext(this.parent);
|
|
29
|
+
return ipc_1.IpcCallLogger.execute(this.logging, context, 'client', contract.id, method.id, method.mask, request, async () => {
|
|
30
|
+
const body = method.request.parse(request);
|
|
31
|
+
if (body === null || body === undefined)
|
|
32
|
+
throw new errors_1.InternalError('IPC requests require one non-null DTO');
|
|
33
|
+
const reply = await this.connection.request(new ipc_1.IpcRequest(contract.id, method.id, context, body));
|
|
34
|
+
if (reply.type === 'failure')
|
|
35
|
+
throw errors_1.ApiErrorCodec.decode(reply.error);
|
|
36
|
+
return method.response.parse(reply.body);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
return new Proxy(Object.create(null), new IpcProxyHandler(methods));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.IpcClientFactory = IpcClientFactory;
|
|
44
|
+
class IpcProxyHandler {
|
|
45
|
+
methods;
|
|
46
|
+
constructor(
|
|
47
|
+
// webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
48
|
+
methods) {
|
|
49
|
+
this.methods = methods;
|
|
50
|
+
}
|
|
51
|
+
// webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
52
|
+
get(_target, key) {
|
|
53
|
+
// Promise assimilation, inspection, symbols and framework instrumentation aren't RPC methods.
|
|
54
|
+
return this.methods.get(key);
|
|
55
|
+
}
|
|
56
|
+
has(_target, key) {
|
|
57
|
+
return this.methods.has(key);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=IpcClientFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IpcClientFactory.js","sourceRoot":"","sources":["../../../../../packages/core/ipc-bridge/src/IpcClientFactory.ts"],"names":[],"mappings":";;;AAAA,wDAA2E;AAC3E,kDAQkC;AAElC,gGAAgG;AAChG,MAAa,gBAAgB;IAEJ;IACA;IACA;IAHrB,YACqB,UAAyB,EACzB,OAAmB,EACnB,MAAuB;QAFvB,eAAU,GAAV,UAAU,CAAe;QACzB,YAAO,GAAP,OAAO,CAAY;QACnB,WAAM,GAAN,MAAM,CAAiB;IACzC,CAAC;IAEJ,0FAA0F;IAC1F,WAAW,CAAC,OAAuB;QAC/B,OAAO,IAAI,gBAAgB,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACxE,CAAC;IAED,YAAY,CAAmB,QAAwB;QACnD,0IAA0I;QAC1I,MAAM,OAAO,GAAG,IAAI,GAAG,EAAuD,CAAC;QAC/E,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;YAC9C,0IAA0I;YAC1I,MAAM,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAc,CAAgC,CAAC;YAC/E,0IAA0I;YA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,GAAG,CAAC,OAAU,EAAE,GAAoB;QAChC,8FAA8F;QAC9F,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;IACD,GAAG,CAAC,OAAU,EAAE,GAAoB;QAChC,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC;CACJ","sourcesContent":["import { ApiErrorCodec, InternalError } from '@webpieces/core-util/errors';\nimport {\n IpcCallContext,\n IpcConnection,\n IpcContract,\n IpcLogging,\n IpcMethod,\n IpcRequest,\n IpcCallLogger,\n} from '@webpieces/core-util/ipc';\n\n/** Typed proxies on one trusted duplex connection; no HTTP decorators or container required. */\nexport class IpcClientFactory {\n constructor(\n private readonly connection: IpcConnection,\n private readonly logging: IpcLogging,\n private readonly parent?: IpcCallContext,\n ) {}\n\n /** Explicit scope propagation is safe across concurrent async calls in browser and RN. */\n withContext(context: IpcCallContext): IpcClientFactory {\n return new IpcClientFactory(this.connection, this.logging, context);\n }\n\n createClient<T extends object>(contract: IpcContract<T>): T {\n // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n const methods = new Map<PropertyKey, (request: unknown) => Promise<unknown>>();\n for (const key of Object.keys(contract.methods)) {\n // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n const method = contract.methods[key as keyof T] as IpcMethod<unknown, unknown>;\n // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n methods.set(key, async (request: unknown): Promise<unknown> => {\n const context = this.connection.newContext(this.parent);\n return IpcCallLogger.execute(\n this.logging,\n context,\n 'client',\n contract.id,\n method.id,\n method.mask,\n request,\n async () => {\n const body = method.request.parse(request);\n if (body === null || body === undefined)\n throw new InternalError('IPC requests require one non-null DTO');\n const reply = await this.connection.request(\n new IpcRequest(contract.id, method.id, context, body),\n );\n if (reply.type === 'failure') throw ApiErrorCodec.decode(reply.error);\n return method.response.parse(reply.body);\n },\n );\n });\n }\n return new Proxy(Object.create(null) as T, new IpcProxyHandler(methods));\n }\n}\n\nclass IpcProxyHandler<T extends object> implements ProxyHandler<T> {\n constructor(\n // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n private readonly methods: ReadonlyMap<PropertyKey, (request: unknown) => Promise<unknown>>,\n ) {}\n // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n get(_target: T, key: string | symbol): unknown {\n // Promise assimilation, inspection, symbols and framework instrumentation aren't RPC methods.\n return this.methods.get(key);\n }\n has(_target: T, key: string | symbol): boolean {\n return this.methods.has(key);\n }\n}\n"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { IpcCallContext, IpcContract, IpcLogging, IpcReply, IpcRequest } from '@webpieces/core-util/ipc';
|
|
2
|
+
/** Construct a receiver using explicit call context when it needs to make nested outbound calls. */
|
|
3
|
+
export interface IpcControllerScope<T> {
|
|
4
|
+
create(context: IpcCallContext): T;
|
|
5
|
+
}
|
|
6
|
+
/** One dispatcher hosts multiple explicitly registered APIs and never enumerates controller members. */
|
|
7
|
+
export declare class IpcServerFactory {
|
|
8
|
+
private readonly logging;
|
|
9
|
+
private readonly registrations;
|
|
10
|
+
constructor(logging: IpcLogging);
|
|
11
|
+
create<T extends object>(contract: IpcContract<T>, controller: T): void;
|
|
12
|
+
createScoped<T extends object>(contract: IpcContract<T>, scope: IpcControllerScope<T>): void;
|
|
13
|
+
private register;
|
|
14
|
+
/** Bootstrap passes this function to the ONE connection's setHandler before sending calls. */
|
|
15
|
+
readonly handle: (request: IpcRequest) => Promise<IpcReply>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IpcServerFactory = void 0;
|
|
4
|
+
const errors_1 = require("@webpieces/core-util/errors");
|
|
5
|
+
const ipc_1 = require("@webpieces/core-util/ipc");
|
|
6
|
+
class IpcRegistration {
|
|
7
|
+
invoke;
|
|
8
|
+
// webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
9
|
+
constructor(invoke) {
|
|
10
|
+
this.invoke = invoke;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
/** One dispatcher hosts multiple explicitly registered APIs and never enumerates controller members. */
|
|
14
|
+
class IpcServerFactory {
|
|
15
|
+
logging;
|
|
16
|
+
registrations = new Map();
|
|
17
|
+
constructor(logging) {
|
|
18
|
+
this.logging = logging;
|
|
19
|
+
}
|
|
20
|
+
create(contract, controller) {
|
|
21
|
+
this.register(contract, () => controller);
|
|
22
|
+
}
|
|
23
|
+
createScoped(contract, scope) {
|
|
24
|
+
this.register(contract, (context) => scope.create(context));
|
|
25
|
+
}
|
|
26
|
+
register(contract, controller) {
|
|
27
|
+
if (this.registrations.has(contract.id))
|
|
28
|
+
throw new errors_1.InternalError(`Duplicate IPC API registration: ${contract.id}`);
|
|
29
|
+
const methods = new Map();
|
|
30
|
+
for (const key of Object.keys(contract.methods)) {
|
|
31
|
+
// webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
32
|
+
const method = contract.methods[key];
|
|
33
|
+
methods.set(method.id, new IpcRegistration(async (request) => {
|
|
34
|
+
// Logging wraps validation and invocation; only handle() below encodes exceptions.
|
|
35
|
+
return ipc_1.IpcCallLogger.execute(this.logging, request.context, 'server', contract.id, method.id, method.mask, request.body, async () => {
|
|
36
|
+
const body = method.request.parse(request.body);
|
|
37
|
+
if (body === null || body === undefined)
|
|
38
|
+
throw new errors_1.InternalError('IPC requests require one non-null DTO');
|
|
39
|
+
const instance = controller(request.context);
|
|
40
|
+
const invoke = instance[key];
|
|
41
|
+
if (typeof invoke !== 'function')
|
|
42
|
+
throw new errors_1.InternalError(`IPC implementation is missing ${contract.id}.${method.id}`);
|
|
43
|
+
const result = await // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation
|
|
44
|
+
invoke.call(instance, body);
|
|
45
|
+
return method.response.parse(result);
|
|
46
|
+
});
|
|
47
|
+
}));
|
|
48
|
+
}
|
|
49
|
+
this.registrations.set(contract.id, methods);
|
|
50
|
+
}
|
|
51
|
+
/** Bootstrap passes this function to the ONE connection's setHandler before sending calls. */
|
|
52
|
+
handle = async (request) => {
|
|
53
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- receiver translates to a failure reply; the generated client reconstructs and throws it
|
|
54
|
+
try {
|
|
55
|
+
const method = this.registrations.get(request.apiId)?.get(request.methodId);
|
|
56
|
+
if (!method)
|
|
57
|
+
throw new errors_1.EndpointNotFoundError('Unknown IPC API or method');
|
|
58
|
+
const body = await method.invoke(request);
|
|
59
|
+
return new ipc_1.IpcSuccess(request.context, body === undefined ? null : body);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
const error = (0, ipc_1.toError)(err);
|
|
63
|
+
return new ipc_1.IpcFailure(request.context, errors_1.ApiErrorCodec.encode(error));
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
exports.IpcServerFactory = IpcServerFactory;
|
|
68
|
+
//# sourceMappingURL=IpcServerFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IpcServerFactory.js","sourceRoot":"","sources":["../../../../../packages/core/ipc-bridge/src/IpcServerFactory.ts"],"names":[],"mappings":";;;AAAA,wDAAkG;AAClG,kDAWkC;AAMlC,MAAM,eAAe;IAEI;IADrB,0IAA0I;IAC1I,YAAqB,MAAiD;QAAjD,WAAM,GAAN,MAAM,CAA2C;IAAG,CAAC;CAC7E;AAED,wGAAwG;AACxG,MAAa,gBAAgB;IAEI;IADZ,aAAa,GAAG,IAAI,GAAG,EAAwC,CAAC;I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E,CAAC;YACpB,MAAM,KAAK,GAAG,IAAA,aAAO,EAAC,GAAG,CAAC,CAAC;YAC3B,OAAO,IAAI,gBAAU,CAAC,OAAO,CAAC,OAAO,EAAE,sBAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;QACxE,CAAC;IACL,CAAC,CAAC;CACL;AAxED,4CAwEC","sourcesContent":["import { ApiErrorCodec, EndpointNotFoundError, InternalError } from '@webpieces/core-util/errors';\nimport {\n IpcCallContext,\n IpcContract,\n IpcFailure,\n IpcLogging,\n IpcMethod,\n IpcReply,\n IpcRequest,\n IpcSuccess,\n IpcCallLogger,\n toError,\n} from '@webpieces/core-util/ipc';\n\n/** Construct a receiver using explicit call context when it needs to make nested outbound calls. */\nexport interface IpcControllerScope<T> {\n create(context: IpcCallContext): T;\n}\nclass IpcRegistration {\n // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n constructor(readonly invoke: (request: IpcRequest) => Promise<unknown>) {}\n}\n\n/** One dispatcher hosts multiple explicitly registered APIs and never enumerates controller members. */\nexport class IpcServerFactory {\n private readonly registrations = new Map<string, Map<string, IpcRegistration>>();\n constructor(private readonly logging: IpcLogging) {}\n\n create<T extends object>(contract: IpcContract<T>, controller: T): void {\n this.register(contract, () => controller);\n }\n\n createScoped<T extends object>(contract: IpcContract<T>, scope: IpcControllerScope<T>): void {\n this.register(contract, (context) => scope.create(context));\n }\n\n private register<T extends object>(\n contract: IpcContract<T>,\n controller: (context: IpcCallContext) => T,\n ): void {\n if (this.registrations.has(contract.id))\n throw new InternalError(`Duplicate IPC API registration: ${contract.id}`);\n const methods = new Map<string, IpcRegistration>();\n for (const key of Object.keys(contract.methods)) {\n // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n const method = contract.methods[key as keyof T] as IpcMethod<unknown, unknown>;\n methods.set(\n method.id,\n new IpcRegistration(async (request) => {\n // Logging wraps validation and invocation; only handle() below encodes exceptions.\n return IpcCallLogger.execute(\n this.logging,\n request.context,\n 'server',\n contract.id,\n method.id,\n method.mask,\n request.body,\n async () => {\n const body = method.request.parse(request.body);\n if (body === null || body === undefined)\n throw new InternalError('IPC requests require one non-null DTO');\n const instance = controller(request.context);\n const invoke = instance[key as keyof T];\n if (typeof invoke !== 'function')\n throw new InternalError(\n `IPC implementation is missing ${contract.id}.${method.id}`,\n );\n const result =\n await // webpieces-disable no-any-unknown -- untrusted IPC data is schema-validated; generic dispatch cannot assume a DTO type before validation\n (invoke as (request: unknown) => Promise<unknown>).call(\n instance,\n body,\n );\n return method.response.parse(result);\n },\n );\n }),\n );\n }\n this.registrations.set(contract.id, methods);\n }\n\n /** Bootstrap passes this function to the ONE connection's setHandler before sending calls. */\n readonly handle = async (request: IpcRequest): Promise<IpcReply> => {\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions -- receiver translates to a failure reply; the generated client reconstructs and throws it\n try {\n const method = this.registrations.get(request.apiId)?.get(request.methodId);\n if (!method) throw new EndpointNotFoundError('Unknown IPC API or method');\n const body = await method.invoke(request);\n return new IpcSuccess(request.context, body === undefined ? null : body);\n } catch (err: unknown) {\n const error = toError(err);\n return new IpcFailure(request.context, ApiErrorCodec.encode(error));\n }\n };\n}\n"]}
|
package/src/index.d.ts
ADDED
package/src/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IpcServerFactory = exports.IpcClientFactory = void 0;
|
|
4
|
+
var IpcClientFactory_1 = require("./IpcClientFactory");
|
|
5
|
+
Object.defineProperty(exports, "IpcClientFactory", { enumerable: true, get: function () { return IpcClientFactory_1.IpcClientFactory; } });
|
|
6
|
+
var IpcServerFactory_1 = require("./IpcServerFactory");
|
|
7
|
+
Object.defineProperty(exports, "IpcServerFactory", { enumerable: true, get: function () { return IpcServerFactory_1.IpcServerFactory; } });
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/core/ipc-bridge/src/index.ts"],"names":[],"mappings":";;;AAAA,uDAAsD;AAA7C,oHAAA,gBAAgB,OAAA;AACzB,uDAAsD;AAA7C,oHAAA,gBAAgB,OAAA","sourcesContent":["export { IpcClientFactory } from './IpcClientFactory';\nexport { IpcServerFactory } from './IpcServerFactory';\nexport type { IpcControllerScope } from './IpcServerFactory';\n"]}
|