@isoloop/runtime 0.0.0-next.69da74b
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/LICENSE +21 -0
- package/dist/bootstrap.js +1876 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +26 -0
- package/dist/testing.d.ts +33 -0
- package/dist/testing.js +97 -0
- package/dist/vanilla-NeyOhMib.d.ts +136 -0
- package/package.json +40 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { a as defineIsolate, c as ScrollIntoViewOptionsLike, i as REQUIRES, l as BusClient, n as IsolateDefinition, o as HostApi, r as IsolateRoot, s as HostCallError, t as IsolateContext, u as SubscribeOptions } from "./vanilla-NeyOhMib.js";
|
|
2
|
+
import { BusError } from "@isoloop/protocol";
|
|
3
|
+
export { type BusClient, BusError, type HostApi, HostCallError, type IsolateContext, type IsolateDefinition, type IsolateRoot, REQUIRES, type ScrollIntoViewOptionsLike, type SubscribeOptions, defineIsolate };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { BusError, majorMinor } from "@isoloop/protocol";
|
|
2
|
+
import "@isoloop/dom/worker";
|
|
3
|
+
// src/host-calls.ts
|
|
4
|
+
/** A host call that failed, with the protocol's error code. */
|
|
5
|
+
var HostCallError = class extends Error {
|
|
6
|
+
code;
|
|
7
|
+
constructor(code, message) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "HostCallError";
|
|
10
|
+
this.code = code;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
// src/vanilla.ts
|
|
14
|
+
/** This build's stamp: `major.minor` of the package version, inlined when tsdown bundles it. */
|
|
15
|
+
const REQUIRES = majorMinor("0.0.0") ?? "0.0";
|
|
16
|
+
/**
|
|
17
|
+
* Types an isolate module's default export and stamps it with the version
|
|
18
|
+
* it was built against. `Root` defaults to the structural `IsolateRoot`.
|
|
19
|
+
*/
|
|
20
|
+
function defineIsolate(def) {
|
|
21
|
+
return {
|
|
22
|
+
requires: REQUIRES,
|
|
23
|
+
...def
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export { BusError, HostCallError, REQUIRES, defineIsolate };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { o as HostApi, t as IsolateContext } from "./vanilla-NeyOhMib.js";
|
|
2
|
+
import { ShutdownReason } from "@isoloop/protocol";
|
|
3
|
+
export type TestContextOptions = {
|
|
4
|
+
id?: string;
|
|
5
|
+
config?: unknown;
|
|
6
|
+
/** The render root, e.g. an element on the test page; omit for a headless context. */
|
|
7
|
+
root?: object;
|
|
8
|
+
/** Answers `bus.request`; default: reject with `timeout` after the request's `timeoutMs`. */
|
|
9
|
+
respond?: (topic: string, value: unknown) => unknown;
|
|
10
|
+
/** Answers host calls; default: `measure` gives a zero rect, the rest resolve. */
|
|
11
|
+
host?: Partial<HostApi>;
|
|
12
|
+
};
|
|
13
|
+
export type TestContext = {
|
|
14
|
+
readonly ctx: IsolateContext;
|
|
15
|
+
/** Delivers a record to every subscription on `topic`, in order. */
|
|
16
|
+
emit: (topic: string, value: unknown, key?: string) => void;
|
|
17
|
+
/** Runs `onReset` of every subscription on `topic` (a bus restart lost it, ADR 0007). */
|
|
18
|
+
reset: (topic: string) => void;
|
|
19
|
+
/** Every `publish`, in order. */
|
|
20
|
+
readonly published: {
|
|
21
|
+
topic: string;
|
|
22
|
+
key?: string;
|
|
23
|
+
value: unknown;
|
|
24
|
+
}[];
|
|
25
|
+
/** Open subscriptions, by topic and start position. */
|
|
26
|
+
subscriptions: () => {
|
|
27
|
+
topic: string;
|
|
28
|
+
from: string;
|
|
29
|
+
}[];
|
|
30
|
+
/** Runs the `onShutdown` handlers, as the runtime does on `stop`. */
|
|
31
|
+
shutdown: (reason?: ShutdownReason) => Promise<void>;
|
|
32
|
+
};
|
|
33
|
+
export declare function createTestContext(o?: TestContextOptions): TestContext;
|
package/dist/testing.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { BusError } from "@isoloop/protocol";
|
|
2
|
+
// src/testing.ts
|
|
3
|
+
const ZERO = {
|
|
4
|
+
x: 0,
|
|
5
|
+
y: 0,
|
|
6
|
+
width: 0,
|
|
7
|
+
height: 0
|
|
8
|
+
};
|
|
9
|
+
function createTestContext(o = {}) {
|
|
10
|
+
const subs = /* @__PURE__ */ new Set();
|
|
11
|
+
const published = [];
|
|
12
|
+
const shutdownFns = [];
|
|
13
|
+
let offset = 0;
|
|
14
|
+
const bus = {
|
|
15
|
+
publish(topic, rec) {
|
|
16
|
+
published.push(rec.key === void 0 ? {
|
|
17
|
+
topic: topic.name,
|
|
18
|
+
value: rec.value
|
|
19
|
+
} : {
|
|
20
|
+
topic: topic.name,
|
|
21
|
+
key: rec.key,
|
|
22
|
+
value: rec.value
|
|
23
|
+
});
|
|
24
|
+
return Promise.resolve();
|
|
25
|
+
},
|
|
26
|
+
subscribe(topic, fn, opts = {}) {
|
|
27
|
+
const sub = {
|
|
28
|
+
topic: topic.name,
|
|
29
|
+
fn,
|
|
30
|
+
opts
|
|
31
|
+
};
|
|
32
|
+
subs.add(sub);
|
|
33
|
+
return () => {
|
|
34
|
+
subs.delete(sub);
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
async request(topic, value, ro = {}) {
|
|
38
|
+
if (o.respond) return o.respond(topic.name, value);
|
|
39
|
+
const timeoutMs = ro.timeoutMs ?? 3e4;
|
|
40
|
+
return new Promise((_resolve, reject) => {
|
|
41
|
+
setTimeout(() => {
|
|
42
|
+
reject(new BusError({
|
|
43
|
+
code: "timeout",
|
|
44
|
+
message: `No reply on ${topic.name} within ${timeoutMs} ms`
|
|
45
|
+
}));
|
|
46
|
+
}, timeoutMs);
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
reply: () => Promise.resolve(),
|
|
50
|
+
commit: () => {}
|
|
51
|
+
};
|
|
52
|
+
const host = {
|
|
53
|
+
navigate: () => Promise.resolve(true),
|
|
54
|
+
measure: () => Promise.resolve(ZERO),
|
|
55
|
+
focus: () => Promise.resolve(),
|
|
56
|
+
scrollIntoView: () => Promise.resolve(),
|
|
57
|
+
requestCanvas: () => Promise.resolve(new OffscreenCanvas(1, 1)),
|
|
58
|
+
...o.host
|
|
59
|
+
};
|
|
60
|
+
return {
|
|
61
|
+
ctx: {
|
|
62
|
+
id: o.id ?? "test",
|
|
63
|
+
config: o.config ?? null,
|
|
64
|
+
root: o.root,
|
|
65
|
+
host,
|
|
66
|
+
bus,
|
|
67
|
+
styles: () => {},
|
|
68
|
+
setValue: () => {},
|
|
69
|
+
onShutdown: (fn) => {
|
|
70
|
+
shutdownFns.push(fn);
|
|
71
|
+
},
|
|
72
|
+
keepAwake: () => () => {}
|
|
73
|
+
},
|
|
74
|
+
published,
|
|
75
|
+
emit(topic, value, key) {
|
|
76
|
+
const rec = {
|
|
77
|
+
offset: offset++,
|
|
78
|
+
value,
|
|
79
|
+
ts: Date.now(),
|
|
80
|
+
producer: "test",
|
|
81
|
+
...key === void 0 ? {} : { key }
|
|
82
|
+
};
|
|
83
|
+
for (const s of subs) if (s.topic === topic) s.fn(rec);
|
|
84
|
+
},
|
|
85
|
+
reset(topic) {
|
|
86
|
+
for (const s of subs) if (s.topic === topic) s.opts.onReset?.();
|
|
87
|
+
},
|
|
88
|
+
subscriptions: () => [...subs].map((s) => ({
|
|
89
|
+
topic: s.topic,
|
|
90
|
+
from: s.opts.from ?? "latest"
|
|
91
|
+
})),
|
|
92
|
+
async shutdown(reason = "stop") {
|
|
93
|
+
await Promise.allSettled(shutdownFns.map(async (fn) => fn(reason)));
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
export { createTestContext };
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { ErrorCode, Rect, ShutdownReason, StartPosition, TopicRef, TypedRecord } from "@isoloop/protocol";
|
|
2
|
+
type SubscribeOptions = {
|
|
3
|
+
/** Default `latest`. */
|
|
4
|
+
from?: StartPosition;
|
|
5
|
+
/** Share the topic with other subscribers of the same group: each record goes to one of them. */
|
|
6
|
+
group?: string;
|
|
7
|
+
/** Default true: commit each record once its handler settles. */
|
|
8
|
+
autoCommit?: boolean;
|
|
9
|
+
/**
|
|
10
|
+
* A bus restart lost this memory topic (ADR 0007). Clear what was built
|
|
11
|
+
* from it; records from the new bus follow.
|
|
12
|
+
*/
|
|
13
|
+
onReset?: () => void;
|
|
14
|
+
/**
|
|
15
|
+
* Records in `[from, to]` were dropped by retention before they reached
|
|
16
|
+
* this subscription (design §6.3), e.g. while the isolate was suspended.
|
|
17
|
+
*/
|
|
18
|
+
onGap?: (gap: {
|
|
19
|
+
from: number;
|
|
20
|
+
to: number;
|
|
21
|
+
}) => void;
|
|
22
|
+
};
|
|
23
|
+
interface BusClient {
|
|
24
|
+
/** Resolves once the broker appended the record; rejects with a `BusError`. */
|
|
25
|
+
publish<In>(topic: TopicRef<In, any, any, any>, rec: {
|
|
26
|
+
key?: string;
|
|
27
|
+
value: In;
|
|
28
|
+
}): Promise<void>;
|
|
29
|
+
/** Returns the unsubscribe function. */
|
|
30
|
+
subscribe<Out>(topic: TopicRef<any, Out, any, any>, fn: (r: TypedRecord<Out>) => void | Promise<void>, opts?: SubscribeOptions): () => void;
|
|
31
|
+
/** Topics with a `reply` schema only. Rejects with `timeout` after `timeoutMs` (default 30 s). */
|
|
32
|
+
request<In, ROut>(topic: TopicRef<In, any, any, ROut>, value: In, opts?: {
|
|
33
|
+
key?: string;
|
|
34
|
+
timeoutMs?: number;
|
|
35
|
+
}): Promise<ROut>;
|
|
36
|
+
/** Answers a request record delivered to this isolate; the broker checks `value` against the topic's `reply` schema. */
|
|
37
|
+
reply(record: TypedRecord<unknown>, value: unknown): Promise<void>;
|
|
38
|
+
/** Commits a record by hand, for `autoCommit: false`. */
|
|
39
|
+
commit(record: TypedRecord<unknown>): void;
|
|
40
|
+
}
|
|
41
|
+
/** A host call that failed, with the protocol's error code. */
|
|
42
|
+
declare class HostCallError extends Error {
|
|
43
|
+
readonly code: ErrorCode;
|
|
44
|
+
constructor(code: ErrorCode, message: string);
|
|
45
|
+
}
|
|
46
|
+
/** What `ctx.host` offers (design §4.7). */
|
|
47
|
+
type HostApi = {
|
|
48
|
+
/**
|
|
49
|
+
* Asks the host to navigate (ADR 0002). Needs `grants.navigate`, else
|
|
50
|
+
* `forbidden`. `/x` resolves against the host's `basePath`. Resolves with
|
|
51
|
+
* the host's answer: `true` accepted, `false` refused.
|
|
52
|
+
*/
|
|
53
|
+
navigate(href: string, o?: {
|
|
54
|
+
replace?: boolean;
|
|
55
|
+
state?: unknown;
|
|
56
|
+
}): Promise<boolean>;
|
|
57
|
+
/** The node's box relative to the surface, after pending patches apply. */
|
|
58
|
+
measure(node: unknown): Promise<Rect>;
|
|
59
|
+
focus(node: unknown): Promise<void>;
|
|
60
|
+
scrollIntoView(node: unknown, opts?: ScrollIntoViewOptionsLike): Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Hands a rendered `<canvas>` to the isolate as an `OffscreenCanvas`
|
|
63
|
+
* (design §4.7), at `size` in canvas pixels if given. Draw on it from the
|
|
64
|
+
* worker. The element then gets `resize` events with its CSS `width` and
|
|
65
|
+
* `height`, the first at once. Each canvas goes once; a second request
|
|
66
|
+
* rejects with `invalid`, a headless isolate's with `no_surface`.
|
|
67
|
+
*/
|
|
68
|
+
requestCanvas(node: unknown, size?: {
|
|
69
|
+
width?: number;
|
|
70
|
+
height?: number;
|
|
71
|
+
}): Promise<OffscreenCanvas>;
|
|
72
|
+
};
|
|
73
|
+
type ScrollIntoViewOptionsLike = {
|
|
74
|
+
behavior?: "auto" | "instant" | "smooth";
|
|
75
|
+
block?: "start" | "center" | "end" | "nearest";
|
|
76
|
+
inline?: "start" | "center" | "end" | "nearest";
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* What a render root offers, structurally, so DOM-typed isolate code can pass
|
|
80
|
+
* its own nodes. Use `defineIsolate<HTMLElement>(…)` to get the DOM's types.
|
|
81
|
+
*/
|
|
82
|
+
interface IsolateRoot {
|
|
83
|
+
append(...nodes: (object | string)[]): void;
|
|
84
|
+
replaceChildren(...nodes: (object | string)[]): void;
|
|
85
|
+
appendChild<T extends object>(node: T): T;
|
|
86
|
+
insertBefore<T extends object>(node: T, ref: object | null): T;
|
|
87
|
+
removeChild<T extends object>(node: T): T;
|
|
88
|
+
readonly childNodes: ArrayLike<object>;
|
|
89
|
+
textContent: string | null;
|
|
90
|
+
addEventListener(type: string, fn: (event: never) => void, options?: unknown): void;
|
|
91
|
+
}
|
|
92
|
+
type IsolateContext<Root = IsolateRoot> = {
|
|
93
|
+
readonly id: string;
|
|
94
|
+
/** `manifest.config`, passed through. */
|
|
95
|
+
readonly config: unknown;
|
|
96
|
+
/** The render target; `undefined` for a headless isolate (ADR 0008). */
|
|
97
|
+
readonly root: Root | undefined;
|
|
98
|
+
readonly host: HostApi;
|
|
99
|
+
/** The log bus, on its own port to the bus worker (design §5). */
|
|
100
|
+
readonly bus: BusClient;
|
|
101
|
+
/** Adds stylesheets to the surface (design §4.8); a no-op when headless. */
|
|
102
|
+
styles(css: string | readonly string[]): void;
|
|
103
|
+
/**
|
|
104
|
+
* Sets an `<input>`'s, `<textarea>`'s or `<select>`'s value. The host owns
|
|
105
|
+
* a focused input's value (design §4.6), so a plain `input.value = …` waits
|
|
106
|
+
* until the user leaves it; with `force` the host applies it at once and
|
|
107
|
+
* keeps the caret at the end, e.g. to clear a chat box after sending.
|
|
108
|
+
*/
|
|
109
|
+
setValue(node: unknown, value: string, opts?: {
|
|
110
|
+
force?: boolean;
|
|
111
|
+
}): void;
|
|
112
|
+
/** Runs on stop, within the host's grace period, before the worker goes. */
|
|
113
|
+
onShutdown(fn: (reason: ShutdownReason) => void | Promise<void>): void;
|
|
114
|
+
/**
|
|
115
|
+
* Holds the isolate against suspension (ADR 0001); call the result to
|
|
116
|
+
* release. Only matters for isolates spawned with `suspend`.
|
|
117
|
+
*/
|
|
118
|
+
keepAwake(): () => void;
|
|
119
|
+
};
|
|
120
|
+
type IsolateDefinition<Root = IsolateRoot> = {
|
|
121
|
+
start(ctx: IsolateContext<Root>): void | Promise<void>;
|
|
122
|
+
/**
|
|
123
|
+
* The Isoloop `major.minor` it was built against, stamped by
|
|
124
|
+
* `defineIsolate` (ADR 0012). The host fails an isolate outside its
|
|
125
|
+
* `[FLOOR, VERSION]`; a definition without one isn't checked.
|
|
126
|
+
*/
|
|
127
|
+
requires?: string;
|
|
128
|
+
};
|
|
129
|
+
/** This build's stamp: `major.minor` of the package version, inlined when tsdown bundles it. */
|
|
130
|
+
declare const REQUIRES: string;
|
|
131
|
+
/**
|
|
132
|
+
* Types an isolate module's default export and stamps it with the version
|
|
133
|
+
* it was built against. `Root` defaults to the structural `IsolateRoot`.
|
|
134
|
+
*/
|
|
135
|
+
declare function defineIsolate<Root = IsolateRoot>(def: IsolateDefinition<Root>): IsolateDefinition<Root>;
|
|
136
|
+
export { defineIsolate as a, ScrollIntoViewOptionsLike as c, REQUIRES as i, BusClient as l, IsolateDefinition as n, HostApi as o, IsolateRoot as r, HostCallError as s, IsolateContext as t, SubscribeOptions as u };
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@isoloop/runtime",
|
|
3
|
+
"version": "0.0.0-next.69da74b",
|
|
4
|
+
"description": "Isolate-side runtime that runs inside each worker.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/isoloop/isoloop.git",
|
|
11
|
+
"directory": "packages/runtime"
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./bootstrap": {
|
|
22
|
+
"import": "./dist/bootstrap.js"
|
|
23
|
+
},
|
|
24
|
+
"./testing": {
|
|
25
|
+
"types": "./dist/testing.d.ts",
|
|
26
|
+
"import": "./dist/testing.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@isoloop/dom": "0.0.0-next.69da74b",
|
|
31
|
+
"@isoloop/protocol": "0.0.0-next.69da74b"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"typescript": "7.0.2"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "tsdown",
|
|
38
|
+
"typecheck": "tsc --noEmit"
|
|
39
|
+
}
|
|
40
|
+
}
|