@ekrooh/bare 0.1.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.
@@ -0,0 +1,261 @@
1
+ //#region core/messages/constants.d.ts
2
+ declare const VERSION = 1;
3
+ declare const MessageType: {
4
+ readonly ENVELOPE: 1;
5
+ };
6
+ type MessageTypeValue = (typeof MessageType)[keyof typeof MessageType];
7
+ /** Canonical error codes. Everything the framework throws uses these; peers
8
+ * may carry arbitrary codes over the wire, and consumers may return app-scoped
9
+ * codes (e.g. `app.photos/not-found`) which are preserved verbatim. Keep
10
+ * `android/src/main/.../ErrorCodes.kt` in sync with the canonical union. */
11
+ declare const ErrorCode: {
12
+ readonly UNSUPPORTED_CAPABILITY: 'UNSUPPORTED_CAPABILITY';
13
+ readonly UNSUPPORTED_EVENT: 'UNSUPPORTED_EVENT';
14
+ readonly HOST_ERROR: 'HOST_ERROR';
15
+ readonly TRANSPORT_ERROR: 'TRANSPORT_ERROR';
16
+ readonly PLUGIN_ERROR: 'PLUGIN_ERROR';
17
+ readonly INVALID_RESPONSE: 'INVALID_RESPONSE';
18
+ readonly FRAME_TOO_LARGE: 'FRAME_TOO_LARGE';
19
+ readonly FRAME_INVALID: 'FRAME_INVALID';
20
+ readonly TIMEOUT: 'TIMEOUT';
21
+ };
22
+ type ErrorCode = (typeof ErrorCode)[keyof typeof ErrorCode];
23
+ /** A JSON header must fit a 16-bit big-endian length field. */
24
+ declare const MAX_HEADER_BYTES = 65535;
25
+ /** Upper bound on one encoded frame. Large binary transfers are a plugin-level
26
+ * concern (out-of-band handles), not an in-band payload. */
27
+ declare const MAX_FRAME_BYTES: number;
28
+ //#endregion
29
+ //#region core/messages/types.d.ts
30
+ /** Decoded binary frame (version byte stripped); used by WebSocket and other transports. */
31
+ interface WireMessage {
32
+ type: MessageTypeValue;
33
+ header: MessageHeader;
34
+ payload: Uint8Array;
35
+ }
36
+ type HeaderBase = {
37
+ type: string;
38
+ requestId?: string;
39
+ };
40
+ /** Canonical wire codes plus any app-scoped code consumers define (e.g.
41
+ * `app.photos/not-found`). Framework plugins use the canonical union;
42
+ * arbitrary codes ride the wire verbatim and are never flattened. */
43
+ type ErrorCodeOrString = ErrorCode | (string & {});
44
+ declare class CoreError extends Error {
45
+ code: ErrorCodeOrString;
46
+ constructor(code: ErrorCodeOrString, message: string);
47
+ }
48
+ type CoreErrorWire = {
49
+ code: string;
50
+ message: string;
51
+ };
52
+ type RuntimeTarget = 'web' | 'android' | 'ios' | 'bare';
53
+ type Either<E, A> = [E, null] | [null, A];
54
+ type DispatchEnvelope<TEvent extends string = string, TArgs extends Record<string, unknown> = Record<string, unknown>> = {
55
+ kind: 'dispatch';
56
+ pluginId: string;
57
+ event: TEvent;
58
+ args: TArgs;
59
+ payload?: Uint8Array | ArrayBuffer | string | null;
60
+ };
61
+ type InvokeEnvelope<TEvent extends string = string, TArgs extends Record<string, unknown> = Record<string, unknown>, TResult = unknown> = {
62
+ kind: 'invoke';
63
+ pluginId: string;
64
+ event: TEvent;
65
+ args: TArgs;
66
+ payload?: Uint8Array | ArrayBuffer | string | null;
67
+ timeoutMs?: number;
68
+ __result?: TResult;
69
+ };
70
+ type PluginInvokeRequestHeader = HeaderBase & {
71
+ type: 'INVOKE_REQUEST';
72
+ pluginId: string;
73
+ event: string;
74
+ args?: Record<string, unknown>;
75
+ };
76
+ type PluginInvokeResponseHeader = HeaderBase & {
77
+ type: 'INVOKE_RESPONSE';
78
+ pluginId: string;
79
+ event: string;
80
+ result?: unknown;
81
+ error?: CoreErrorWire;
82
+ };
83
+ type PluginDispatchHeader = HeaderBase & {
84
+ type: 'DISPATCH';
85
+ pluginId: string;
86
+ event: string;
87
+ args?: Record<string, unknown>;
88
+ };
89
+ type PluginHeader = PluginInvokeRequestHeader | PluginInvokeResponseHeader | PluginDispatchHeader;
90
+ /** Worklet → host: ask for merged capability list (host registry + static knowledge). */
91
+ type HostCapabilitiesQueryHeader = {
92
+ type: 'HOST_CAPABILITIES_QUERY';
93
+ requestId: string;
94
+ };
95
+ /** Host → worklet: capability rows contributed by the host. */
96
+ type HostCapabilitiesResponseHeader = {
97
+ type: 'HOST_CAPABILITIES_RESPONSE';
98
+ requestId: string;
99
+ capabilities: CapabilityDescriptor[];
100
+ };
101
+ /** Worklet → host: run a host-registered handler (same pluginId/event as frontend invokes). */
102
+ type HostInvokeRequestHeader = {
103
+ type: 'HOST_INVOKE_REQUEST';
104
+ requestId: string;
105
+ pluginId: string;
106
+ event: string;
107
+ args?: Record<string, unknown>;
108
+ };
109
+ /** Host → worklet: result for {@link HostInvokeRequestHeader}. */
110
+ type HostInvokeResponseHeader = {
111
+ type: 'HOST_INVOKE_RESPONSE';
112
+ requestId: string;
113
+ pluginId: string;
114
+ event: string;
115
+ result?: unknown;
116
+ error?: CoreErrorWire;
117
+ };
118
+ /** Serializable row for discovery and host registry listing. */
119
+ type CapabilityDescriptor = {
120
+ pluginId: string;
121
+ capabilities: string[];
122
+ events: string[];
123
+ runtimes: RuntimeTarget[];
124
+ };
125
+ type MessageHeader = PluginHeader | HostCapabilitiesQueryHeader | HostCapabilitiesResponseHeader | HostInvokeRequestHeader | HostInvokeResponseHeader;
126
+ interface PluginContext {
127
+ runtime: RuntimeTarget;
128
+ payload: Uint8Array;
129
+ }
130
+ interface PluginRuntimeAdapter {
131
+ dispatch?: (event: string, args: Record<string, unknown> | undefined, context: PluginContext) => void | Promise<void>;
132
+ invoke?: (event: string, args: Record<string, unknown> | undefined, context: PluginContext) => Either<CoreError, unknown> | Promise<Either<CoreError, unknown>>;
133
+ }
134
+ interface PluginManifest {
135
+ id: string;
136
+ version?: string;
137
+ capabilities?: string[];
138
+ events?: string[];
139
+ runtimes: Partial<Record<RuntimeTarget, PluginRuntimeAdapter>>;
140
+ }
141
+ //#endregion
142
+ //#region core/messages/errors.d.ts
143
+ /** Success half of an `Either` tuple. */
144
+ declare function ok<T>(result: T): [null, T];
145
+ /** Failure half of an `Either` tuple. Accepts a canonical error code or any
146
+ * app-scoped code (`app.photos/not-found`) — the code is preserved verbatim. */
147
+ declare function err(code: ErrorCodeOrString, message: string): [CoreError, null];
148
+ /** Convenience for constructing a `CoreError` from a possibly-non-canonical
149
+ * wire code: preserves known codes, maps anything else to `PLUGIN_ERROR`.
150
+ * Framework callers that want app codes preserved construct the error
151
+ * directly instead. */
152
+ declare function coerceErrorCode(code: string | undefined): ErrorCode;
153
+ type Ok<T> = [null, T];
154
+ type Err = [CoreError, null];
155
+ type PluginResult<T> = Either<CoreError, T>;
156
+ //#endregion
157
+ //#region core/messages/rpc-messenger.d.ts
158
+ type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
159
+ type InternalDispatchRequest = (PluginDispatchHeader & {
160
+ requestId: string;
161
+ }) | (PluginInvokeRequestHeader & {
162
+ requestId: string;
163
+ });
164
+ type DispatchRequest = DistributiveOmit<PluginDispatchHeader, 'requestId'>;
165
+ type InvokeRequest = DistributiveOmit<PluginInvokeRequestHeader, 'requestId'>;
166
+ interface ProtocolMessenger {
167
+ dispatch(request: DispatchRequest, payload?: Uint8Array | ArrayBuffer | string | null): string;
168
+ invoke(request: InvokeRequest, payload?: Uint8Array | ArrayBuffer | string | null, timeoutMs?: number): Promise<MessageHeader>;
169
+ handleIncoming(header: MessageHeader): void;
170
+ }
171
+ declare function createProtocolMessenger(send: (request: InternalDispatchRequest, payload?: Uint8Array | ArrayBuffer | string | null) => void): ProtocolMessenger;
172
+ //#endregion
173
+ //#region core/messages/plugin-kernel.d.ts
174
+ interface PluginRegistry {
175
+ register(plugin: PluginManifest): void;
176
+ resolve(pluginId: string, runtime: RuntimeTarget): PluginManifest['runtimes'][RuntimeTarget] | undefined;
177
+ get(pluginId: string): PluginManifest | undefined;
178
+ listCapabilities(runtime?: RuntimeTarget): Array<{
179
+ pluginId: string;
180
+ capabilities: string[];
181
+ events: string[];
182
+ runtimes: RuntimeTarget[];
183
+ }>;
184
+ }
185
+ declare function createPluginRegistry(): PluginRegistry;
186
+ interface PluginRouter {
187
+ route(header: MessageHeader, payload: Uint8Array): Promise<PluginInvokeResponseHeader | null>;
188
+ }
189
+ type PluginRouterOptions = {
190
+ delegateToHost?: (header: PluginInvokeRequestHeader, payload: Uint8Array) => Promise<PluginInvokeResponseHeader | null>;
191
+ logger?: Pick<Console, 'warn' | 'error'>;
192
+ };
193
+ declare function createPluginRouter(registry: PluginRegistry, runtime: RuntimeTarget, options?: PluginRouterOptions): PluginRouter;
194
+ interface PluginBus {
195
+ dispatch(envelope: DispatchEnvelope): string;
196
+ invoke<TResult>(envelope: InvokeEnvelope<string, Record<string, unknown>, TResult>): Promise<Either<CoreError, TResult>>;
197
+ }
198
+ declare function createPluginBus(messenger: ProtocolMessenger): PluginBus;
199
+ //#endregion
200
+ //#region core/messages/wire-codec.d.ts
201
+ type Encoder = (str: string) => Uint8Array;
202
+ type Decoder = (bytes: Uint8Array) => string;
203
+ interface ProtocolOptions {
204
+ encode?: Encoder;
205
+ decode?: Decoder;
206
+ allowUnknownTypes?: boolean;
207
+ /** Override the default {@link MAX_FRAME_BYTES} cap. */
208
+ maxFrameBytes?: number;
209
+ }
210
+ declare class MessageProtocol {
211
+ private encodeStr;
212
+ private decodeStr;
213
+ private allowUnknownTypes;
214
+ private maxFrameBytes;
215
+ constructor(options?: ProtocolOptions);
216
+ encode(type: MessageTypeValue, header: MessageHeader, payload?: Uint8Array | ArrayBuffer | string | null): Uint8Array;
217
+ decode(data: ArrayBuffer | Uint8Array): WireMessage;
218
+ }
219
+ //#endregion
220
+ //#region core/messages/define-plugin.d.ts
221
+ /**
222
+ * One event's wire identity and type shape — the single source of truth for
223
+ * the manifest `events` list, the generated handler table, and the typed
224
+ * builders in `events.ts`. `args`/`result` are phantom values that carry the
225
+ * handler and builder types so they can never drift apart.
226
+ */
227
+ interface EventSpec<TArgs extends Record<string, unknown> = Record<string, never>, TResult = unknown> {
228
+ readonly pluginId: string;
229
+ readonly name: string;
230
+ readonly args: TArgs;
231
+ readonly result: TResult;
232
+ }
233
+ type InvokeHandler<TArgs extends Record<string, unknown> = Record<string, never>, TResult = unknown> = (args: TArgs, context: PluginContext) => Either<CoreError, TResult> | Promise<Either<CoreError, TResult>>;
234
+ type DispatchHandler<TArgs extends Record<string, unknown> = Record<string, never>> = (args: TArgs, context: PluginContext) => void | Promise<void>;
235
+ type InvokeHandlers<TSpecs extends Record<string, EventSpec<any, any>>> = { [K in keyof TSpecs]?: InvokeHandler<TSpecs[K]['args'], TSpecs[K]['result']>; };
236
+ type DispatchHandlers<TSpecs extends Record<string, EventSpec<any, any>>> = { [K in keyof TSpecs]?: DispatchHandler<TSpecs[K]['args']>; };
237
+ /**
238
+ * Builds a {@link PluginManifest} from an event spec table. The `events` list
239
+ * is derived from the specs and handlers are dispatched by event name — no
240
+ * hand-written if-chains. Handlers receive typed args. Plugins with no local
241
+ * handlers (host-delegated, e.g. `core.permissions`) pass no `invoke`/`dispatch`.
242
+ */
243
+ declare function definePlugin<const TSpecs extends Record<string, EventSpec<any, any>>>(id: string, specs: TSpecs, options?: {
244
+ capabilities?: string[];
245
+ invoke?: InvokeHandlers<TSpecs>;
246
+ dispatch?: DispatchHandlers<TSpecs>;
247
+ }): PluginManifest;
248
+ /** Typed builder: an invoke envelope for a spec's event. */
249
+ declare function invokeEvent<TSpec extends EventSpec<any, any>>(spec: TSpec, args: TSpec['args'], payload?: Uint8Array | ArrayBuffer | string | null, timeoutMs?: number): InvokeEnvelope<TSpec['name'], TSpec['args'], TSpec['result']>;
250
+ /** Typed builder: a dispatch envelope for a spec's event. */
251
+ declare function dispatchEvent<TSpec extends EventSpec<any, any>>(spec: TSpec, args: TSpec['args'], payload?: Uint8Array | ArrayBuffer | string | null): DispatchEnvelope<TSpec['name'], TSpec['args']>;
252
+ //#endregion
253
+ //#region core/messages/create-bare-runtime-context.d.ts
254
+ interface BareRuntimeContext {
255
+ protocol: MessageProtocol;
256
+ pluginRegistry: PluginRegistry;
257
+ pluginRouter: PluginRouter;
258
+ }
259
+ declare function createBareRuntimeContext(plugins?: PluginManifest[], routerOptions?: PluginRouterOptions): BareRuntimeContext;
260
+ //#endregion
261
+ export { MAX_HEADER_BYTES as $, CapabilityDescriptor as A, InvokeEnvelope as B, createProtocolMessenger as C, coerceErrorCode as D, PluginResult as E, ErrorCodeOrString as F, PluginInvokeRequestHeader as G, PluginContext as H, HostCapabilitiesQueryHeader as I, PluginRuntimeAdapter as J, PluginInvokeResponseHeader as K, HostCapabilitiesResponseHeader as L, CoreErrorWire as M, DispatchEnvelope as N, err as O, Either as P, MAX_FRAME_BYTES as Q, HostInvokeRequestHeader as R, ProtocolMessenger as S, Ok as T, PluginDispatchHeader as U, MessageHeader as V, PluginHeader as W, WireMessage as X, RuntimeTarget as Y, ErrorCode as Z, createPluginBus as _, InvokeHandler as a, DispatchRequest as b, invokeEvent as c, MessageProtocol as d, MessageType as et, ProtocolOptions as f, PluginRouterOptions as g, PluginRouter as h, EventSpec as i, CoreError as j, ok as k, Decoder as l, PluginRegistry as m, createBareRuntimeContext as n, VERSION as nt, definePlugin as o, PluginBus as p, PluginManifest as q, DispatchHandler as r, dispatchEvent as s, BareRuntimeContext as t, MessageTypeValue as tt, Encoder as u, createPluginRegistry as v, Err as w, InvokeRequest as x, createPluginRouter as y, HostInvokeResponseHeader as z };
@@ -0,0 +1,2 @@
1
+ import { $ as MAX_HEADER_BYTES, A as CapabilityDescriptor, B as InvokeEnvelope, C as createProtocolMessenger, D as coerceErrorCode, E as PluginResult, F as ErrorCodeOrString, G as PluginInvokeRequestHeader, H as PluginContext, I as HostCapabilitiesQueryHeader, J as PluginRuntimeAdapter, K as PluginInvokeResponseHeader, L as HostCapabilitiesResponseHeader, M as CoreErrorWire, N as DispatchEnvelope, O as err, P as Either, Q as MAX_FRAME_BYTES, R as HostInvokeRequestHeader, S as ProtocolMessenger, T as Ok, U as PluginDispatchHeader, V as MessageHeader, W as PluginHeader, X as WireMessage, Y as RuntimeTarget, Z as ErrorCode, _ as createPluginBus, a as InvokeHandler, b as DispatchRequest, c as invokeEvent, d as MessageProtocol, et as MessageType, f as ProtocolOptions, g as PluginRouterOptions, h as PluginRouter, i as EventSpec, j as CoreError, k as ok, l as Decoder, m as PluginRegistry, n as createBareRuntimeContext, nt as VERSION, o as definePlugin, p as PluginBus, q as PluginManifest, r as DispatchHandler, s as dispatchEvent, t as BareRuntimeContext, tt as MessageTypeValue, u as Encoder, v as createPluginRegistry, w as Err, x as InvokeRequest, y as createPluginRouter, z as HostInvokeResponseHeader } from "./index-DjulYFJL.mjs";
2
+ export { BareRuntimeContext, CapabilityDescriptor, CoreError, CoreErrorWire, Decoder, DispatchEnvelope, DispatchHandler, DispatchRequest, Either, Encoder, Err, ErrorCode, ErrorCodeOrString, EventSpec, HostCapabilitiesQueryHeader, HostCapabilitiesResponseHeader, HostInvokeRequestHeader, HostInvokeResponseHeader, InvokeEnvelope, InvokeHandler, InvokeRequest, MAX_FRAME_BYTES, MAX_HEADER_BYTES, MessageHeader, MessageProtocol, MessageType, MessageTypeValue, Ok, PluginBus, PluginContext, PluginDispatchHeader, PluginHeader, PluginInvokeRequestHeader, PluginInvokeResponseHeader, PluginManifest, PluginRegistry, PluginResult, PluginRouter, PluginRouterOptions, PluginRuntimeAdapter, ProtocolMessenger, ProtocolOptions, RuntimeTarget, VERSION, WireMessage, coerceErrorCode, createBareRuntimeContext, createPluginBus, createPluginRegistry, createPluginRouter, createProtocolMessenger, definePlugin, dispatchEvent, err, invokeEvent, ok };