@isoloop/protocol 0.0.0-next.69da74b → 0.0.0-next.800569e
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 +72 -6
- package/dist/index.js +80 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -13,13 +13,20 @@ export type Envelope<T extends string, P> = {
|
|
|
13
13
|
id?: string;
|
|
14
14
|
/** Id of the message this replies to. */
|
|
15
15
|
re?: string;
|
|
16
|
+
/**
|
|
17
|
+
* The pool member a message is for (host → worker) or from (worker → host),
|
|
18
|
+
* on a pooled worker's isolate port (M2c, ADR 0001). Absent on an own
|
|
19
|
+
* worker's port, and on messages about the whole worker (`beat`).
|
|
20
|
+
*/
|
|
21
|
+
isolate?: string;
|
|
16
22
|
p: P;
|
|
17
23
|
};
|
|
18
24
|
/** Any envelope, before its type is known. */
|
|
19
25
|
export type AnyEnvelope = Envelope<string, unknown>;
|
|
20
26
|
/**
|
|
21
27
|
* True for a well-formed envelope this build can read: `v` is a supported
|
|
22
|
-
* version, `t` is a string, `id`/`re` are absent or strings, and `p`
|
|
28
|
+
* version, `t` is a string, `id`/`re`/`isolate` are absent or strings, and `p`
|
|
29
|
+
* is present.
|
|
23
30
|
* Says nothing about whether `t` is known; see the per-port guards.
|
|
24
31
|
*/
|
|
25
32
|
export declare function isEnvelope(x: unknown): x is AnyEnvelope;
|
|
@@ -92,8 +99,12 @@ export type Mods = {
|
|
|
92
99
|
meta: boolean;
|
|
93
100
|
shift: boolean;
|
|
94
101
|
};
|
|
95
|
-
/**
|
|
96
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Event types the host forwards (design §4.6), plus `resize` for transferred
|
|
104
|
+
* canvases (§4.7). `pointermove` and `scroll` are opt-in per node and come at
|
|
105
|
+
* most once a frame.
|
|
106
|
+
*/
|
|
107
|
+
export type EventType = "click" | "dblclick" | "input" | "change" | "keydown" | "keyup" | "submit" | "focusin" | "focusout" | "pointerdown" | "pointerup" | "pointermove" | "scroll" | "resize";
|
|
97
108
|
/** A DOM event as it reaches an isolate (design §4.6). Node ids are the isolate's own. */
|
|
98
109
|
export type SerializedEvent = {
|
|
99
110
|
type: EventType;
|
|
@@ -111,6 +122,9 @@ export type SerializedEvent = {
|
|
|
111
122
|
/** For `resize`. */
|
|
112
123
|
width?: number;
|
|
113
124
|
height?: number;
|
|
125
|
+
/** For `scroll`: the scrolled element's offsets (M3b). */
|
|
126
|
+
scrollTop?: number;
|
|
127
|
+
scrollLeft?: number;
|
|
114
128
|
};
|
|
115
129
|
/** Why the host asks an isolate to shut down. `suspend` keeps its offsets (M2, ADR 0001). */
|
|
116
130
|
export type ShutdownReason = "stop" | "restart" | "suspend";
|
|
@@ -146,6 +160,8 @@ export type InitPayload = {
|
|
|
146
160
|
* subscriptions start from `committed` instead, so it replays what it missed.
|
|
147
161
|
*/
|
|
148
162
|
wake?: boolean;
|
|
163
|
+
/** `KernelConfig.bus.commitIntervalMs` (ADR 0016, decision 1). Default 100. */
|
|
164
|
+
commitIntervalMs?: number;
|
|
149
165
|
};
|
|
150
166
|
/** What a beat says about whether the isolate could be suspended (ADR 0001). */
|
|
151
167
|
export type IdleReport = {
|
|
@@ -158,6 +174,15 @@ export type IdleReport = {
|
|
|
158
174
|
ageMs: number;
|
|
159
175
|
stack?: string;
|
|
160
176
|
}[];
|
|
177
|
+
/**
|
|
178
|
+
* Milliseconds the worker spent running this isolate's code since the last
|
|
179
|
+
* beat (ADR 0017): what the runtime dispatched to it and the callbacks it
|
|
180
|
+
* registered. A pool's shared framework render is split equally among the
|
|
181
|
+
* members whose roots it touched. Promise continuations, other schedulers
|
|
182
|
+
* and the worker's shared work (garbage collection) aren't counted; they
|
|
183
|
+
* show only in `lagMs`. Absent from runtimes before it.
|
|
184
|
+
*/
|
|
185
|
+
busyMs?: number;
|
|
161
186
|
};
|
|
162
187
|
/** Isolate port: host → isolate. */
|
|
163
188
|
export type HostToIsolate = Envelope<"init", InitPayload> | Envelope<"event", SerializedEvent> | Envelope<"result", CallResult> | Envelope<"shutdown", {
|
|
@@ -185,6 +210,7 @@ export type IsolateToHost = Envelope<"ready", {
|
|
|
185
210
|
lagMs: number;
|
|
186
211
|
heapBytes?: number;
|
|
187
212
|
idle?: IdleReport;
|
|
213
|
+
members?: Record<string, IdleReport>;
|
|
188
214
|
}> | Envelope<"error", ErrorPayload> | Envelope<"bye", Record<string, never>>;
|
|
189
215
|
export declare const HOST_TO_ISOLATE: MessageTypes<HostToIsolate>;
|
|
190
216
|
export declare const ISOLATE_TO_HOST: MessageTypes<IsolateToHost>;
|
|
@@ -232,9 +258,11 @@ export type IsolateToBus = Envelope<"publish", {
|
|
|
232
258
|
group?: string;
|
|
233
259
|
}> | Envelope<"unsubscribe", {
|
|
234
260
|
topic: string;
|
|
235
|
-
}> | Envelope<"
|
|
236
|
-
|
|
237
|
-
|
|
261
|
+
}> | Envelope<"commits", {
|
|
262
|
+
offsets: {
|
|
263
|
+
topic: string;
|
|
264
|
+
offset: number;
|
|
265
|
+
}[];
|
|
238
266
|
}> | Envelope<"request", {
|
|
239
267
|
topic: string;
|
|
240
268
|
key?: string;
|
|
@@ -261,6 +289,44 @@ export declare const ISOLATE_TO_BUS: MessageTypes<IsolateToBus>;
|
|
|
261
289
|
export declare const BUS_TO_ISOLATE: MessageTypes<BusToIsolate>;
|
|
262
290
|
export declare const isIsolateToBus: (x: unknown) => x is IsolateToBus;
|
|
263
291
|
export declare const isBusToIsolate: (x: unknown) => x is BusToIsolate;
|
|
292
|
+
/** `KernelConfig.bus.commitIntervalMs` when unset (ADR 0016, decision 1, point 7). */
|
|
293
|
+
export declare const DEFAULT_COMMIT_INTERVAL_MS = 100;
|
|
294
|
+
/** The highest allowed interval: keeps the redelivery window after a crash under a second. */
|
|
295
|
+
export declare const MAX_COMMIT_INTERVAL_MS = 1e3;
|
|
296
|
+
/** A subscription's credit at the broker: records delivered and not yet committed. */
|
|
297
|
+
export declare const DEFAULT_CREDIT = 1e3;
|
|
298
|
+
/** Why an interval is invalid, or `undefined` when it's an integer from 0 to 1,000. */
|
|
299
|
+
export declare function commitIntervalProblem(ms: unknown): string | undefined;
|
|
300
|
+
export type CommitOffsets = {
|
|
301
|
+
topic: string;
|
|
302
|
+
offset: number;
|
|
303
|
+
}[];
|
|
304
|
+
export type CommitterOptions = {
|
|
305
|
+
/** Sends one `commits` message. Called with at least one entry. */
|
|
306
|
+
send: (offsets: CommitOffsets) => void;
|
|
307
|
+
/** 0: at the end of each task that settled a record. Default 100. */
|
|
308
|
+
intervalMs?: number;
|
|
309
|
+
/** The broker's credit per subscription; a subscription sends early at half of it. Default 1,000. */
|
|
310
|
+
credit?: number;
|
|
311
|
+
};
|
|
312
|
+
export type Committer = {
|
|
313
|
+
/** `records` records of `topic`, up to `offset`, are settled. Default one. */
|
|
314
|
+
settle(topic: string, offset: number, records?: number): void;
|
|
315
|
+
/** Sends what's pending now: an unsubscribe, and a new port after a bus restart. */
|
|
316
|
+
flush(): void;
|
|
317
|
+
/**
|
|
318
|
+
* Returns what's pending and clears it, without sending: for a sender that
|
|
319
|
+
* must wait for the broker's answer (a shutdown, before `bye`).
|
|
320
|
+
*/
|
|
321
|
+
take(): CommitOffsets | undefined;
|
|
322
|
+
/** Forgets a topic's pending offset (an unsubscribe or a lost memory topic). */
|
|
323
|
+
drop(topic: string): void;
|
|
324
|
+
/** Settled offsets not yet sent, by topic. For tests and reports. */
|
|
325
|
+
pending(): ReadonlyMap<string, number>;
|
|
326
|
+
/** Stops the timer without sending. */
|
|
327
|
+
dispose(): void;
|
|
328
|
+
};
|
|
329
|
+
export declare function createCommitter(o: CommitterOptions): Committer;
|
|
264
330
|
/**
|
|
265
331
|
* A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
|
|
266
332
|
* `reply`): the broker's error code, and the schema issues for `invalid`.
|
package/dist/index.js
CHANGED
|
@@ -8,11 +8,12 @@ const isRecord = (x) => typeof x === "object" && x !== null && !Array.isArray(x)
|
|
|
8
8
|
const isOptionalString = (x) => x === void 0 || typeof x === "string";
|
|
9
9
|
/**
|
|
10
10
|
* True for a well-formed envelope this build can read: `v` is a supported
|
|
11
|
-
* version, `t` is a string, `id`/`re` are absent or strings, and `p`
|
|
11
|
+
* version, `t` is a string, `id`/`re`/`isolate` are absent or strings, and `p`
|
|
12
|
+
* is present.
|
|
12
13
|
* Says nothing about whether `t` is known; see the per-port guards.
|
|
13
14
|
*/
|
|
14
15
|
function isEnvelope(x) {
|
|
15
|
-
return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]);
|
|
16
|
+
return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]) && isOptionalString(x["isolate"]);
|
|
16
17
|
}
|
|
17
18
|
/**
|
|
18
19
|
* Builds a guard for one port's union from its message types. Each side drops
|
|
@@ -107,7 +108,7 @@ const ISOLATE_TO_BUS = {
|
|
|
107
108
|
publish: true,
|
|
108
109
|
subscribe: true,
|
|
109
110
|
unsubscribe: true,
|
|
110
|
-
|
|
111
|
+
commits: true,
|
|
111
112
|
request: true,
|
|
112
113
|
reply: true
|
|
113
114
|
};
|
|
@@ -118,6 +119,81 @@ const BUS_TO_ISOLATE = {
|
|
|
118
119
|
};
|
|
119
120
|
const isIsolateToBus = envelopeGuard(ISOLATE_TO_BUS);
|
|
120
121
|
const isBusToIsolate = envelopeGuard(BUS_TO_ISOLATE);
|
|
122
|
+
// src/commits.ts
|
|
123
|
+
/** `KernelConfig.bus.commitIntervalMs` when unset (ADR 0016, decision 1, point 7). */
|
|
124
|
+
const DEFAULT_COMMIT_INTERVAL_MS = 100;
|
|
125
|
+
/** The highest allowed interval: keeps the redelivery window after a crash under a second. */
|
|
126
|
+
const MAX_COMMIT_INTERVAL_MS = 1e3;
|
|
127
|
+
/** A subscription's credit at the broker: records delivered and not yet committed. */
|
|
128
|
+
const DEFAULT_CREDIT = 1e3;
|
|
129
|
+
/** Why an interval is invalid, or `undefined` when it's an integer from 0 to 1,000. */
|
|
130
|
+
function commitIntervalProblem(ms) {
|
|
131
|
+
if (typeof ms === "number" && Number.isInteger(ms) && ms >= 0 && ms <= 1e3) return void 0;
|
|
132
|
+
return `bus.commitIntervalMs must be an integer from 0 to ${MAX_COMMIT_INTERVAL_MS}, got ${String(ms)}`;
|
|
133
|
+
}
|
|
134
|
+
function createCommitter(o) {
|
|
135
|
+
const intervalMs = o.intervalMs ?? 100;
|
|
136
|
+
const half = Math.max(1, Math.floor((o.credit ?? 1e3) / 2));
|
|
137
|
+
/** Highest settled offset per topic, not yet sent. */
|
|
138
|
+
const offsets = /* @__PURE__ */ new Map();
|
|
139
|
+
/** Records settled per topic since its last send: the early send at half the credit. */
|
|
140
|
+
const counts = /* @__PURE__ */ new Map();
|
|
141
|
+
let timer;
|
|
142
|
+
let queued = false;
|
|
143
|
+
const take = () => {
|
|
144
|
+
if (timer !== void 0) {
|
|
145
|
+
clearTimeout(timer);
|
|
146
|
+
timer = void 0;
|
|
147
|
+
}
|
|
148
|
+
queued = false;
|
|
149
|
+
if (offsets.size === 0) return void 0;
|
|
150
|
+
const out = [];
|
|
151
|
+
for (const [topic, offset] of offsets) out.push({
|
|
152
|
+
topic,
|
|
153
|
+
offset
|
|
154
|
+
});
|
|
155
|
+
offsets.clear();
|
|
156
|
+
counts.clear();
|
|
157
|
+
return out;
|
|
158
|
+
};
|
|
159
|
+
const flush = () => {
|
|
160
|
+
const out = take();
|
|
161
|
+
if (out) o.send(out);
|
|
162
|
+
};
|
|
163
|
+
const schedule = () => {
|
|
164
|
+
if (intervalMs === 0) {
|
|
165
|
+
if (queued) return;
|
|
166
|
+
queued = true;
|
|
167
|
+
queueMicrotask(() => {
|
|
168
|
+
if (queued) flush();
|
|
169
|
+
});
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
timer ??= setTimeout(flush, intervalMs);
|
|
173
|
+
};
|
|
174
|
+
return {
|
|
175
|
+
settle(topic, offset, records = 1) {
|
|
176
|
+
const prev = offsets.get(topic);
|
|
177
|
+
if (prev === void 0 || offset > prev) offsets.set(topic, offset);
|
|
178
|
+
const n = (counts.get(topic) ?? 0) + records;
|
|
179
|
+
counts.set(topic, n);
|
|
180
|
+
if (n >= half) flush();
|
|
181
|
+
else schedule();
|
|
182
|
+
},
|
|
183
|
+
flush,
|
|
184
|
+
take,
|
|
185
|
+
drop(topic) {
|
|
186
|
+
offsets.delete(topic);
|
|
187
|
+
counts.delete(topic);
|
|
188
|
+
},
|
|
189
|
+
pending: () => offsets,
|
|
190
|
+
dispose() {
|
|
191
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
192
|
+
timer = void 0;
|
|
193
|
+
queued = false;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
121
197
|
// src/bus-error.ts
|
|
122
198
|
/**
|
|
123
199
|
* A rejected bus call (`kernel.publish`, `ctx.bus.publish`, `request`,
|
|
@@ -252,4 +328,4 @@ function compareVersions(a, b) {
|
|
|
252
328
|
const [bm, bn] = parse(b);
|
|
253
329
|
return am - bm || an - bn;
|
|
254
330
|
}
|
|
255
|
-
export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, compareVersions, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
|
|
331
|
+
export { BUS_TO_HOST, BUS_TO_ISOLATE, BusError, DEFAULT_COMMIT_INTERVAL_MS, DEFAULT_CREDIT, ERROR_CODES, HOST_TO_BUS, HOST_TO_ISOLATE, ISOLATE_TO_BUS, ISOLATE_TO_HOST, MAX_COMMIT_INTERVAL_MS, PROTOCOL, ROUTE_KEY, SYSTEM_TOPICS, SYSTEM_TOPIC_PREFIX, assertNever, checks, commitIntervalProblem, compareVersions, createCommitter, envelopeGuard, isBusToHost, isBusToIsolate, isEnvelope, isErrorCode, isHostToBus, isHostToIsolate, isIsolateToBus, isIsolateToHost, isSystemTopic, majorMinor, matchAny, matchTopic, routeTopic, systemSchema, systemTopic, topicConfigOf };
|