@ekrooh/bare 0.6.2 → 0.7.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/README.md +3 -2
- package/dist/errors-Bl5w-m62.mjs +56 -0
- package/dist/events-DUGMT3B9.mjs +40 -0
- package/dist/{index-Do-kTNXw.d.mts → index-B3AmX7mC.d.mts} +35 -146
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +3 -1
- package/dist/{messages-CMNIFMFY.mjs → messages-BxviiFuh.mjs} +182 -73
- package/dist/plugin-BnDh08L_.mjs +111 -0
- package/dist/plugins-discovery-events.d.mts +1 -1
- package/dist/plugins-discovery-events.mjs +1 -1
- package/dist/plugins-health-events.d.mts +1 -1
- package/dist/plugins-health-events.mjs +1 -1
- package/dist/plugins-logs-events.d.mts +2 -1
- package/dist/plugins-logs-events.mjs +1 -1
- package/dist/plugins-media-events.d.mts +1 -1
- package/dist/plugins-media-events.mjs +1 -26
- package/dist/plugins-permissions-events.d.mts +2 -1
- package/dist/plugins-permissions-events.mjs +11 -3
- package/dist/plugins-q6Wno9wN.mjs +77 -0
- package/dist/plugins.d.mts +7 -4
- package/dist/plugins.mjs +1 -1
- package/dist/runtime.d.mts +31 -7
- package/dist/runtime.mjs +657 -124
- package/dist/{static-file-server-COxd-PFR.d.mts → static-file-server-drT1AHqV.d.mts} +26 -4
- package/dist/{store-DTg1jN5E.mjs → store-Bd2wL58k.mjs} +11 -2
- package/dist/sync.d.mts +36 -0
- package/dist/sync.mjs +323 -0
- package/dist/transports.d.mts +28 -1
- package/dist/transports.mjs +332 -168
- package/dist/types-BC39OoZB.d.mts +152 -0
- package/package.json +10 -5
- package/dist/plugins-BnpdCi3A.mjs +0 -156
|
@@ -1,11 +1,19 @@
|
|
|
1
|
-
import { a as invokeEvent } from "./messages-
|
|
1
|
+
import { a as invokeEvent } from "./messages-BxviiFuh.mjs";
|
|
2
2
|
// plugins/permissions/events.ts
|
|
3
|
+
/**
|
|
4
|
+
* A permission request shows a system prompt the user answers at their own pace
|
|
5
|
+
* (R-04). The page waits five minutes; the worklet waits slightly less for the
|
|
6
|
+
* host, so a host that never answers fails the call with a `TIMEOUT` the page can
|
|
7
|
+
* read before the page gives up on its own. Mirrors `vendor.media`.
|
|
8
|
+
*/
|
|
9
|
+
const PERMISSION_REQUEST_TIMEOUT_MS = 3e5;
|
|
3
10
|
const permissionSpecs = {
|
|
4
11
|
request: {
|
|
5
12
|
pluginId: "core.permissions",
|
|
6
13
|
name: "permissions.request",
|
|
7
14
|
args: {},
|
|
8
|
-
result: {}
|
|
15
|
+
result: {},
|
|
16
|
+
hostTimeoutMs: 295e3
|
|
9
17
|
},
|
|
10
18
|
status: {
|
|
11
19
|
pluginId: "core.permissions",
|
|
@@ -16,7 +24,7 @@ const permissionSpecs = {
|
|
|
16
24
|
};
|
|
17
25
|
const permissionEvents = { permissions: {
|
|
18
26
|
request(permission) {
|
|
19
|
-
return invokeEvent(permissionSpecs.request, { permission });
|
|
27
|
+
return invokeEvent(permissionSpecs.request, { permission }, null, PERMISSION_REQUEST_TIMEOUT_MS);
|
|
20
28
|
},
|
|
21
29
|
status(permission) {
|
|
22
30
|
return invokeEvent(permissionSpecs.status, { permission });
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { n as err, o as ErrorCode, r as ok } from "./errors-Bl5w-m62.mjs";
|
|
2
|
+
import { r as definePlugin } from "./messages-BxviiFuh.mjs";
|
|
3
|
+
import { i as createDiscoveryPlugin, n as createLogsPlugin, r as createHealthPlugin, t as createPermissionsPluginStub } from "./plugin-BnDh08L_.mjs";
|
|
4
|
+
import { i as isMediaKind, n as mediaSpecs, r as MEDIA_HOST_TIMEOUT_MS } from "./events-DUGMT3B9.mjs";
|
|
5
|
+
import crypto from "bare-crypto";
|
|
6
|
+
// plugins/media/plugin.ts
|
|
7
|
+
/** Most media mounts are consumed once and then linger. Cap how many stay
|
|
8
|
+
* mounted at once and evict the least-recently-used so picked/captured files
|
|
9
|
+
* do not accumulate on the loopback server for the whole session. */
|
|
10
|
+
const MEDIA_MOUNT_CAP = 4;
|
|
11
|
+
/** Cap on a host error message passed on to the page. */
|
|
12
|
+
const MAX_HOST_ERROR_MESSAGE_CHARS = 500;
|
|
13
|
+
function newRequestId() {
|
|
14
|
+
const rand = crypto.randomBytes(16).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
15
|
+
return `${Date.now().toString(36)}-${rand}`;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* `vendor.media` — reference plugin demonstrating out-of-band binary transfer.
|
|
19
|
+
* The native host picks/captures a file (returns its path); the worklet mounts
|
|
20
|
+
* it on its loopback HTTP server and hands the web layer a plain URL. No media
|
|
21
|
+
* bytes ever cross the wire protocol or a WebView bridge, and the serving code
|
|
22
|
+
* is identical on every runtime.
|
|
23
|
+
*/
|
|
24
|
+
function createMediaPlugin(deps) {
|
|
25
|
+
const mounted = /* @__PURE__ */ new Map();
|
|
26
|
+
const mountMedia = (id, path) => {
|
|
27
|
+
deps.staticServer.mount(`/media/${id}`, path);
|
|
28
|
+
mounted.set(id, path);
|
|
29
|
+
while (mounted.size > MEDIA_MOUNT_CAP) {
|
|
30
|
+
const oldestId = mounted.keys().next().value;
|
|
31
|
+
const oldestPath = mounted.get(oldestId);
|
|
32
|
+
mounted.delete(oldestId);
|
|
33
|
+
if (oldestPath !== void 0) deps.staticServer.unmount(`/media/${oldestId}`);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const serve = async (event, args) => {
|
|
37
|
+
const kind = args?.kind;
|
|
38
|
+
if (kind !== void 0 && !isMediaKind(kind)) return err(ErrorCode.PLUGIN_ERROR, `Unknown media kind for ${event}`);
|
|
39
|
+
const host = await deps.invokeOnHost({
|
|
40
|
+
type: "INVOKE_REQUEST",
|
|
41
|
+
pluginId: "vendor.media",
|
|
42
|
+
event,
|
|
43
|
+
requestId: newRequestId(),
|
|
44
|
+
args: kind === void 0 ? {} : { kind }
|
|
45
|
+
}, /* @__PURE__ */ new Uint8Array(0), MEDIA_HOST_TIMEOUT_MS);
|
|
46
|
+
if (!host) return err(ErrorCode.HOST_ERROR, `Host did not answer ${event}`);
|
|
47
|
+
if (host.error) {
|
|
48
|
+
const code = typeof host.error.code === "string" && host.error.code.length > 0 ? host.error.code : ErrorCode.HOST_ERROR;
|
|
49
|
+
const message = typeof host.error.message === "string" && host.error.message.length > 0 ? host.error.message.slice(0, MAX_HOST_ERROR_MESSAGE_CHARS) : `Host failed ${event}`;
|
|
50
|
+
return err(code, message);
|
|
51
|
+
}
|
|
52
|
+
const path = host.result?.path;
|
|
53
|
+
if (!path) return err(ErrorCode.HOST_ERROR, `Host returned no path for ${event}`);
|
|
54
|
+
const id = `${kind ?? "media"}-${newRequestId()}`;
|
|
55
|
+
mountMedia(id, path);
|
|
56
|
+
const url = await deps.staticServer.url(`/media/${id}`);
|
|
57
|
+
return ok({ url });
|
|
58
|
+
};
|
|
59
|
+
return definePlugin("vendor.media", mediaSpecs, {
|
|
60
|
+
capabilities: ["media"],
|
|
61
|
+
invoke: {
|
|
62
|
+
pick: async (args) => serve("media.pick", args),
|
|
63
|
+
capture: async (args) => serve("media.capture", args)
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// plugins/index.ts
|
|
68
|
+
function createDefaultPlugins(deps) {
|
|
69
|
+
return [
|
|
70
|
+
createHealthPlugin(),
|
|
71
|
+
createDiscoveryPlugin(deps),
|
|
72
|
+
createPermissionsPluginStub(),
|
|
73
|
+
createMediaPlugin(deps),
|
|
74
|
+
createLogsPlugin(deps)
|
|
75
|
+
];
|
|
76
|
+
}
|
|
77
|
+
export { createDefaultPlugins as t };
|
package/dist/plugins.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { _ as PluginInvokeResponseHeader, b as RuntimeTarget, g as PluginInvokeRequestHeader, t as CapabilityDescriptor, v as PluginManifest } from "./types-BC39OoZB.mjs";
|
|
2
|
+
import { a as LogStore } from "./index-B3AmX7mC.mjs";
|
|
3
|
+
import { n as LoopbackServer } from "./static-file-server-drT1AHqV.mjs";
|
|
3
4
|
type DiscoveryPluginDeps = {
|
|
4
5
|
listBareCapabilities: () => Array<{
|
|
5
6
|
pluginId: string;
|
|
@@ -16,8 +17,10 @@ type LogsPluginDeps = {
|
|
|
16
17
|
type MediaPluginDeps = {
|
|
17
18
|
/** Worklet-side loopback server exposing picked files to the web layer. */
|
|
18
19
|
staticServer: LoopbackServer;
|
|
19
|
-
/** Delegates the native pick/capture to the host (`HOST_INVOKE_REQUEST`).
|
|
20
|
-
|
|
20
|
+
/** Delegates the native pick/capture to the host (`HOST_INVOKE_REQUEST`).
|
|
21
|
+
* `timeoutMs` is how long to wait for the host; the media plugin passes a
|
|
22
|
+
* deadline just under the page's 5-minute invoke timeout. */
|
|
23
|
+
invokeOnHost: (header: PluginInvokeRequestHeader, payload: Uint8Array, timeoutMs?: number) => Promise<PluginInvokeResponseHeader | null>;
|
|
21
24
|
};
|
|
22
25
|
export type DefaultPluginsDeps = DiscoveryPluginDeps & MediaPluginDeps & LogsPluginDeps;
|
|
23
26
|
export declare function createDefaultPlugins(deps: DefaultPluginsDeps): PluginManifest[];
|
package/dist/plugins.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as createDefaultPlugins } from "./plugins-
|
|
1
|
+
export { t as createDefaultPlugins } from "./plugins-q6Wno9wN.mjs";
|
package/dist/runtime.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { _ as PluginInvokeResponseHeader, f as MessageHeader, g as PluginInvokeRequestHeader, t as CapabilityDescriptor, v as PluginManifest, x as WireMessage } from "./types-BC39OoZB.mjs";
|
|
2
|
+
import { C as createPluginRegistry, g as MessageProtocol, k as Platform, o as BareRuntimeContext, w as createPluginRouter } from "./index-B3AmX7mC.mjs";
|
|
3
|
+
import { i as createLoopbackServer, n as LoopbackServer, r as collectRequestBody, t as LoopbackRouteHandler } from "./static-file-server-drT1AHqV.mjs";
|
|
3
4
|
type BareIpcLike = {
|
|
4
5
|
write(data: Uint8Array | string): boolean;
|
|
5
6
|
};
|
|
@@ -9,9 +10,16 @@ declare function createHostIpcBridge(config: {
|
|
|
9
10
|
/** Worklet entry points pass `barePlatform`; Bare has no Web Crypto global. */
|
|
10
11
|
platform: Platform;
|
|
11
12
|
}): {
|
|
13
|
+
/** Stops the bridge (the runtime is closing): clears every pending timer,
|
|
14
|
+
* rejects every pending call with `reason`, and makes later calls reject
|
|
15
|
+
* at once instead of writing to a dead channel. */
|
|
16
|
+
close(reason?: string): void;
|
|
12
17
|
tryConsumeDownstream: (parsed: WireMessage) => boolean;
|
|
13
18
|
tryConsumeDownstreamFromHost(raw: Uint8Array | ArrayBuffer): boolean;
|
|
14
19
|
queryCapabilities(timeoutMs?: number): Promise<CapabilityDescriptor[]>;
|
|
20
|
+
/** Runs `header` on the host. `timeoutMs` bounds the wait (default
|
|
21
|
+
* 30 s); a user-mediated call (picker, camera, permission prompt) passes
|
|
22
|
+
* its own, longer deadline. */
|
|
15
23
|
invokeOnHost(header: PluginInvokeRequestHeader, payload: Uint8Array, timeoutMs?: number): Promise<PluginInvokeResponseHeader | null>;
|
|
16
24
|
};
|
|
17
25
|
/** Envelope encoder bound to a loopback server's connected protocol socket —
|
|
@@ -79,7 +87,8 @@ export type WorkletRuntimeOptions = {
|
|
|
79
87
|
* ephemeral port in device mode, 8080 in dev mode. */
|
|
80
88
|
port?: number;
|
|
81
89
|
/** Require the per-session token/cookie. Defaults to on in device mode and
|
|
82
|
-
* off in dev mode. The origin check applies either way.
|
|
90
|
+
* off in dev mode. The origin check applies either way. `false` is only
|
|
91
|
+
* accepted together with `deviceMode: false`. */
|
|
83
92
|
auth?: boolean;
|
|
84
93
|
/** Extra page origins allowed to call the loopback server, e.g. the Vite dev
|
|
85
94
|
* server (`http://localhost:5173`). The bound origin and its loopback
|
|
@@ -92,7 +101,8 @@ export type WorkletRuntimeOptions = {
|
|
|
92
101
|
handoffFile?: boolean;
|
|
93
102
|
/** Device mode (the default): auth on, an ephemeral port, and the handoff
|
|
94
103
|
* file when `storage` is set. Pass `false` (or the `mode=dev` CLI token) for
|
|
95
|
-
* the dev backend: auth off and port 8080 unless set explicitly.
|
|
104
|
+
* the dev backend: auth off and port 8080 unless set explicitly. Device mode
|
|
105
|
+
* cannot run with `auth: false`: that combination throws. */
|
|
96
106
|
deviceMode?: boolean;
|
|
97
107
|
/** Additional plugin manifests to register after the canonical defaults
|
|
98
108
|
* (consumer product plugins, e.g. `app.photos`). */
|
|
@@ -115,6 +125,13 @@ export type WorkletRuntimeOptions = {
|
|
|
115
125
|
/** Global cap on calls routed at once; calls over it fail with a
|
|
116
126
|
* `TRANSPORT_ERROR`. Defaults to 64. */
|
|
117
127
|
maxInflight?: number;
|
|
128
|
+
/** Cap on calls routed at once from one channel (the page socket, the host
|
|
129
|
+
* IPC), so one peer cannot take every slot. Defaults to three quarters of
|
|
130
|
+
* `maxInflight`. */
|
|
131
|
+
maxInflightPerChannel?: number;
|
|
132
|
+
/** Cap on calls routed at once to one plugin, so a slow plugin cannot take
|
|
133
|
+
* every slot. Defaults to `maxInflight` (no per-plugin cap). */
|
|
134
|
+
maxInflightPerPlugin?: number;
|
|
118
135
|
/** Plugin ids whose calls must run one at a time, in arrival order. All
|
|
119
136
|
* other plugins run concurrently, so one slow call never blocks the rest. */
|
|
120
137
|
serialPlugins?: readonly string[];
|
|
@@ -138,18 +155,25 @@ export interface WorkletRuntime {
|
|
|
138
155
|
hostBridge: ReturnType<typeof createHostIpcBridge> | null;
|
|
139
156
|
config: WorkletRuntimeConfig;
|
|
140
157
|
/** Binds the server, mounts the web app, attaches the protocol socket and
|
|
141
|
-
* (in device mode) writes the handoff file. Resolves to the credentials.
|
|
158
|
+
* (in device mode) writes the handoff file. Resolves to the credentials.
|
|
159
|
+
* Idempotent: later calls return the same promise. Rejects when the device
|
|
160
|
+
* handoff file cannot be written (a host would never find the origin) and
|
|
161
|
+
* after `close()`. */
|
|
142
162
|
start(): Promise<{
|
|
143
163
|
origin: string;
|
|
144
164
|
port: number;
|
|
145
165
|
token: string;
|
|
146
166
|
bootstrap: string;
|
|
147
167
|
}>;
|
|
168
|
+
/** Closes the server, detaches the host IPC listener, fails pending host
|
|
169
|
+
* calls and stops feeding this runtime's log store from the console.
|
|
170
|
+
* Idempotent. */
|
|
148
171
|
close(cb?: (err?: Error | null) => void): void;
|
|
149
172
|
}
|
|
150
173
|
/**
|
|
151
|
-
* Creates one worklet runtime — a per-instance
|
|
152
|
-
*
|
|
174
|
+
* Creates one worklet runtime — a per-instance factory (no hardcoded port;
|
|
175
|
+
* the only process-wide state is the shared console capture, which fans out
|
|
176
|
+
* to every live runtime's log store) so the dev/test backend can host
|
|
153
177
|
* N of them and a consumer worklet entry can be just "register plugins +
|
|
154
178
|
* wire p2p". Mirrors the reference wiring in `core/main.core.ts`.
|
|
155
179
|
*/
|