@isoloop/runtime 0.0.0-next.4da23df → 0.0.0-next.5aa1a15
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/bootstrap.js +212 -53
- package/package.json +3 -3
package/dist/bootstrap.js
CHANGED
|
@@ -20,11 +20,12 @@ var __exportAll = (all, no_symbols) => {
|
|
|
20
20
|
// ../protocol/dist/index.js
|
|
21
21
|
/**
|
|
22
22
|
* True for a well-formed envelope this build can read: `v` is a supported
|
|
23
|
-
* version, `t` is a string, `id`/`re` are absent or strings, and `p`
|
|
23
|
+
* version, `t` is a string, `id`/`re`/`isolate` are absent or strings, and `p`
|
|
24
|
+
* is present.
|
|
24
25
|
* Says nothing about whether `t` is known; see the per-port guards.
|
|
25
26
|
*/
|
|
26
27
|
function isEnvelope(x) {
|
|
27
|
-
return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]);
|
|
28
|
+
return isRecord(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]) && isOptionalString(x["isolate"]);
|
|
28
29
|
}
|
|
29
30
|
/**
|
|
30
31
|
* Builds a guard for one port's union from its message types. Each side drops
|
|
@@ -151,6 +152,117 @@ var init_dist = __esmMin((() => {
|
|
|
151
152
|
retention: { kind: "compacted" }
|
|
152
153
|
});
|
|
153
154
|
}));
|
|
155
|
+
// src/freeze.ts
|
|
156
|
+
init_dist();
|
|
157
|
+
/** Constructors whose `prototype` and statics are frozen, if this engine has them. */
|
|
158
|
+
const NAMES = [
|
|
159
|
+
"Object",
|
|
160
|
+
"Function",
|
|
161
|
+
"Array",
|
|
162
|
+
"String",
|
|
163
|
+
"Number",
|
|
164
|
+
"Boolean",
|
|
165
|
+
"Symbol",
|
|
166
|
+
"BigInt",
|
|
167
|
+
"Date",
|
|
168
|
+
"RegExp",
|
|
169
|
+
"Error",
|
|
170
|
+
"EvalError",
|
|
171
|
+
"RangeError",
|
|
172
|
+
"ReferenceError",
|
|
173
|
+
"SyntaxError",
|
|
174
|
+
"TypeError",
|
|
175
|
+
"URIError",
|
|
176
|
+
"AggregateError",
|
|
177
|
+
"Promise",
|
|
178
|
+
"Map",
|
|
179
|
+
"Set",
|
|
180
|
+
"WeakMap",
|
|
181
|
+
"WeakSet",
|
|
182
|
+
"WeakRef",
|
|
183
|
+
"FinalizationRegistry",
|
|
184
|
+
"ArrayBuffer",
|
|
185
|
+
"DataView",
|
|
186
|
+
"Int8Array",
|
|
187
|
+
"Uint8Array",
|
|
188
|
+
"Uint8ClampedArray",
|
|
189
|
+
"Int16Array",
|
|
190
|
+
"Uint16Array",
|
|
191
|
+
"Int32Array",
|
|
192
|
+
"Uint32Array",
|
|
193
|
+
"Float32Array",
|
|
194
|
+
"Float64Array",
|
|
195
|
+
"BigInt64Array",
|
|
196
|
+
"BigUint64Array"
|
|
197
|
+
];
|
|
198
|
+
/**
|
|
199
|
+
* Freezing a prototype makes its data properties read-only, and in strict
|
|
200
|
+
* code assigning a property of the same name on an instance then throws (the
|
|
201
|
+
* "override mistake": `this.name = 'MyError'`, `Foo.prototype.toString = …`).
|
|
202
|
+
* As Hardened JS does, each data property becomes an accessor whose setter
|
|
203
|
+
* defines the property on the instance instead, so overriding still works
|
|
204
|
+
* while the prototype itself can't change.
|
|
205
|
+
*/
|
|
206
|
+
function tameOverrides(proto) {
|
|
207
|
+
for (const key of Reflect.ownKeys(proto)) {
|
|
208
|
+
const d = Object.getOwnPropertyDescriptor(proto, key);
|
|
209
|
+
if (!d || !("value" in d) || !d.configurable) continue;
|
|
210
|
+
const value = d.value;
|
|
211
|
+
Object.defineProperty(proto, key, {
|
|
212
|
+
get() {
|
|
213
|
+
return value;
|
|
214
|
+
},
|
|
215
|
+
set(v) {
|
|
216
|
+
if (this === proto) throw new TypeError(`Cannot assign to read only property '${String(key)}' of a frozen built-in`);
|
|
217
|
+
if (typeof this !== "object" && typeof this !== "function") return;
|
|
218
|
+
if (this === null) return;
|
|
219
|
+
Object.defineProperty(this, key, {
|
|
220
|
+
value: v,
|
|
221
|
+
writable: true,
|
|
222
|
+
enumerable: true,
|
|
223
|
+
configurable: true
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
enumerable: d.enumerable ?? false,
|
|
227
|
+
configurable: false
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function hardenProto(proto) {
|
|
232
|
+
if (typeof proto !== "object" && typeof proto !== "function" || proto === null) return;
|
|
233
|
+
if (Object.isFrozen(proto)) return;
|
|
234
|
+
tameOverrides(proto);
|
|
235
|
+
Object.freeze(proto);
|
|
236
|
+
}
|
|
237
|
+
function freezeWithPrototype(c) {
|
|
238
|
+
if (typeof c !== "function") return;
|
|
239
|
+
hardenProto(Reflect.get(c, "prototype"));
|
|
240
|
+
if (c !== Error) Object.freeze(c);
|
|
241
|
+
}
|
|
242
|
+
/** Freezes the shared intrinsics. Call once, before any member module loads. */
|
|
243
|
+
function freezeBuiltins() {
|
|
244
|
+
const g = globalThis;
|
|
245
|
+
for (const name of NAMES) freezeWithPrototype(Reflect.get(g, name));
|
|
246
|
+
freezeWithPrototype(Object.getPrototypeOf(Int8Array));
|
|
247
|
+
const protos = [
|
|
248
|
+
Object.getPrototypeOf([][Symbol.iterator]()),
|
|
249
|
+
Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]())),
|
|
250
|
+
Object.getPrototypeOf((/* @__PURE__ */ new Map())[Symbol.iterator]()),
|
|
251
|
+
Object.getPrototypeOf((/* @__PURE__ */ new Set())[Symbol.iterator]()),
|
|
252
|
+
Object.getPrototypeOf(""[Symbol.iterator]()),
|
|
253
|
+
Object.getPrototypeOf(function* () {}),
|
|
254
|
+
Object.getPrototypeOf(async function() {}),
|
|
255
|
+
Object.getPrototypeOf(async function* () {})
|
|
256
|
+
];
|
|
257
|
+
for (const p of protos) {
|
|
258
|
+
if (typeof p !== "object" && typeof p !== "function" || p === null) continue;
|
|
259
|
+
hardenProto(Reflect.get(p, "prototype"));
|
|
260
|
+
hardenProto(p);
|
|
261
|
+
}
|
|
262
|
+
Object.freeze(Math);
|
|
263
|
+
Object.freeze(JSON);
|
|
264
|
+
Object.freeze(Reflect);
|
|
265
|
+
}
|
|
154
266
|
// src/bus-client.ts
|
|
155
267
|
function createBusClient(first, o = {}) {
|
|
156
268
|
const limit = o.inboxLimit ?? 1e3;
|
|
@@ -391,7 +503,6 @@ function createBusClient(first, o = {}) {
|
|
|
391
503
|
}
|
|
392
504
|
var randomEpoch, check, REQUEST_TIMEOUT_MS;
|
|
393
505
|
var init_bus_client = __esmMin((() => {
|
|
394
|
-
init_dist();
|
|
395
506
|
randomEpoch = () => crypto.getRandomValues(/* @__PURE__ */ new Uint32Array(1))[0] ?? 0;
|
|
396
507
|
check = (r) => {
|
|
397
508
|
if (!r.ok) throw new BusError(r.error);
|
|
@@ -1780,11 +1891,19 @@ var init_vanilla = __esmMin((() => {
|
|
|
1780
1891
|
}));
|
|
1781
1892
|
// src/bootstrap.ts
|
|
1782
1893
|
init_dist();
|
|
1783
|
-
|
|
1894
|
+
let mode = null;
|
|
1895
|
+
const members = /* @__PURE__ */ new Map();
|
|
1896
|
+
const postWorker = (msg) => {
|
|
1784
1897
|
self.postMessage(msg);
|
|
1785
1898
|
};
|
|
1786
|
-
const
|
|
1787
|
-
|
|
1899
|
+
const memberPost = (name) => (msg) => {
|
|
1900
|
+
self.postMessage(name === void 0 ? msg : {
|
|
1901
|
+
...msg,
|
|
1902
|
+
isolate: name
|
|
1903
|
+
});
|
|
1904
|
+
};
|
|
1905
|
+
const fatal = (m, message, code) => {
|
|
1906
|
+
m.post({
|
|
1788
1907
|
v: 1,
|
|
1789
1908
|
t: "error",
|
|
1790
1909
|
p: code ? {
|
|
@@ -1797,46 +1916,70 @@ const fatal = (message, code) => {
|
|
|
1797
1916
|
}
|
|
1798
1917
|
});
|
|
1799
1918
|
};
|
|
1800
|
-
|
|
1801
|
-
|
|
1919
|
+
let beating = false;
|
|
1920
|
+
/** Once per worker, after the first isolate's protocol check. */
|
|
1802
1921
|
function startHeartbeat(everyMs) {
|
|
1922
|
+
if (beating) return;
|
|
1923
|
+
beating = true;
|
|
1803
1924
|
let last = performance.now();
|
|
1804
1925
|
setInterval(() => {
|
|
1805
1926
|
const now = performance.now();
|
|
1806
1927
|
const lagMs = Math.max(0, now - last - everyMs);
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
|
|
1928
|
+
if (mode === "pool") {
|
|
1929
|
+
const reports = {};
|
|
1930
|
+
for (const m of members.values()) if (m.name !== void 0 && m.idleReport) reports[m.name] = m.idleReport();
|
|
1931
|
+
postWorker({
|
|
1932
|
+
v: 1,
|
|
1933
|
+
t: "beat",
|
|
1934
|
+
p: {
|
|
1935
|
+
lagMs,
|
|
1936
|
+
members: reports
|
|
1937
|
+
}
|
|
1938
|
+
});
|
|
1939
|
+
} else {
|
|
1940
|
+
const idle = members.get(void 0)?.idleReport?.();
|
|
1941
|
+
postWorker({
|
|
1942
|
+
v: 1,
|
|
1943
|
+
t: "beat",
|
|
1944
|
+
p: idle ? {
|
|
1945
|
+
lagMs,
|
|
1946
|
+
idle
|
|
1947
|
+
} : { lagMs }
|
|
1948
|
+
});
|
|
1949
|
+
}
|
|
1815
1950
|
last = now;
|
|
1816
1951
|
}, everyMs);
|
|
1817
1952
|
}
|
|
1818
|
-
|
|
1953
|
+
/** `shutdown` for a member: `bye` once its handlers settle or the grace runs out. */
|
|
1954
|
+
function shutdownMember(m, graceMs, reason) {
|
|
1955
|
+
if (members.get(m.name) === m) members.delete(m.name);
|
|
1956
|
+
m.receive = null;
|
|
1957
|
+
const done = m.shutdownHook?.(reason) ?? Promise.resolve();
|
|
1958
|
+
const grace = new Promise((r) => setTimeout(r, Math.max(0, graceMs - 50)));
|
|
1959
|
+
Promise.race([done, grace]).finally(() => {
|
|
1960
|
+
m.post({
|
|
1961
|
+
v: 1,
|
|
1962
|
+
t: "bye",
|
|
1963
|
+
p: {}
|
|
1964
|
+
});
|
|
1965
|
+
if (members.size === 0) self.close();
|
|
1966
|
+
});
|
|
1967
|
+
}
|
|
1968
|
+
/** Routes one message from the host to the member it is for. */
|
|
1969
|
+
function route(msg) {
|
|
1970
|
+
const m = members.get(mode === "pool" ? msg.isolate : void 0);
|
|
1971
|
+
if (!m) return;
|
|
1972
|
+
if (msg.t === "shutdown") shutdownMember(m, msg.p.graceMs, msg.p.reason);
|
|
1973
|
+
else m.receive?.(msg);
|
|
1974
|
+
}
|
|
1975
|
+
async function boot(m, init) {
|
|
1819
1976
|
if (init.protocol.min > PROTOCOL.v || init.protocol.max < PROTOCOL.min) {
|
|
1820
|
-
fatal(`Host speaks protocol ${init.protocol.min}–${init.protocol.max}, this runtime ${PROTOCOL.min}–${PROTOCOL.v}`, "protocol_mismatch");
|
|
1977
|
+
fatal(m, `Host speaks protocol ${init.protocol.min}–${init.protocol.max}, this runtime ${PROTOCOL.min}–${PROTOCOL.v}`, "protocol_mismatch");
|
|
1821
1978
|
return;
|
|
1822
1979
|
}
|
|
1823
|
-
self.addEventListener("message", (e) => {
|
|
1824
|
-
const msg = e.data;
|
|
1825
|
-
if (!isHostToIsolate(msg) || msg.t !== "shutdown") return;
|
|
1826
|
-
const done = shutdownHook?.(msg.p.reason) ?? Promise.resolve();
|
|
1827
|
-
const grace = new Promise((r) => setTimeout(r, Math.max(0, msg.p.graceMs - 50)));
|
|
1828
|
-
Promise.race([done, grace]).finally(() => {
|
|
1829
|
-
post({
|
|
1830
|
-
v: 1,
|
|
1831
|
-
t: "bye",
|
|
1832
|
-
p: {}
|
|
1833
|
-
});
|
|
1834
|
-
self.close();
|
|
1835
|
-
});
|
|
1836
|
-
});
|
|
1837
1980
|
startHeartbeat(init.heartbeatMs);
|
|
1838
1981
|
const { createVanillaIsolate } = await Promise.resolve().then(() => (init_vanilla(), vanilla_exports));
|
|
1839
|
-
const runtime = createVanillaIsolate(init, post, self);
|
|
1982
|
+
const runtime = createVanillaIsolate(init, m.post, self);
|
|
1840
1983
|
let mod;
|
|
1841
1984
|
try {
|
|
1842
1985
|
mod = await import(
|
|
@@ -1846,7 +1989,7 @@ async function boot(init) {
|
|
|
1846
1989
|
} catch (err) {
|
|
1847
1990
|
const why = err instanceof Error ? err.message : String(err);
|
|
1848
1991
|
const cross = new URL(init.src).origin !== self.location.origin;
|
|
1849
|
-
fatal(`Could not import ${init.src}: ${why}${cross ? " (a cross-origin isolate needs CORS headers; see docs/guide/hosting.md)" : ""}`);
|
|
1992
|
+
fatal(m, `Could not import ${init.src}: ${why}${cross ? " (a cross-origin isolate needs CORS headers; see docs/guide/hosting.md)" : ""}`);
|
|
1850
1993
|
return;
|
|
1851
1994
|
}
|
|
1852
1995
|
const main = typeof mod === "object" && mod !== null && "default" in mod ? mod.default : void 0;
|
|
@@ -1854,21 +1997,19 @@ async function boot(init) {
|
|
|
1854
1997
|
const start = main.start;
|
|
1855
1998
|
if (typeof start === "function") {
|
|
1856
1999
|
const requires = "requires" in main ? main.requires : void 0;
|
|
1857
|
-
await startIsolate(runtime, (ctx) => Reflect.apply(start, main, [ctx]), typeof requires === "string" ? requires : void 0);
|
|
2000
|
+
await startIsolate(m, runtime, (ctx) => Reflect.apply(start, main, [ctx]), typeof requires === "string" ? requires : void 0);
|
|
1858
2001
|
return;
|
|
1859
2002
|
}
|
|
1860
2003
|
}
|
|
1861
|
-
fatal(`${init.src} has no default export with start(ctx)`);
|
|
2004
|
+
fatal(m, `${init.src} has no default export with start(ctx)`);
|
|
1862
2005
|
}
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
idleReport = () => runtime.idle();
|
|
1871
|
-
post({
|
|
2006
|
+
async function startIsolate(m, runtime, start, requires) {
|
|
2007
|
+
m.receive = (msg) => {
|
|
2008
|
+
runtime.receive(msg);
|
|
2009
|
+
};
|
|
2010
|
+
m.shutdownHook = (reason) => runtime.shutdown(reason);
|
|
2011
|
+
m.idleReport = () => runtime.idle();
|
|
2012
|
+
m.post({
|
|
1872
2013
|
v: 1,
|
|
1873
2014
|
t: "ready",
|
|
1874
2015
|
p: {
|
|
@@ -1881,15 +2022,33 @@ async function startIsolate(runtime, start, requires) {
|
|
|
1881
2022
|
try {
|
|
1882
2023
|
await start(runtime.ctx);
|
|
1883
2024
|
} catch (err) {
|
|
1884
|
-
fatal(`start() threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
2025
|
+
fatal(m, `start() threw: ${err instanceof Error ? err.message : String(err)}`);
|
|
1885
2026
|
}
|
|
1886
2027
|
}
|
|
1887
|
-
|
|
2028
|
+
self.addEventListener("message", (e) => {
|
|
1888
2029
|
const msg = e.data;
|
|
1889
|
-
if (!isHostToIsolate(msg)
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
2030
|
+
if (!isHostToIsolate(msg)) return;
|
|
2031
|
+
if (msg.t !== "init") {
|
|
2032
|
+
route(msg);
|
|
2033
|
+
return;
|
|
2034
|
+
}
|
|
2035
|
+
const name = msg.isolate;
|
|
2036
|
+
const wants = name === void 0 ? "own" : "pool";
|
|
2037
|
+
if (mode === "own" || mode === "pool" && wants === "own") return;
|
|
2038
|
+
if (mode === null) {
|
|
2039
|
+
mode = wants;
|
|
2040
|
+
if (mode === "pool") freezeBuiltins();
|
|
2041
|
+
}
|
|
2042
|
+
if (members.has(name)) return;
|
|
2043
|
+
const m = {
|
|
2044
|
+
name,
|
|
2045
|
+
post: memberPost(name),
|
|
2046
|
+
shutdownHook: null,
|
|
2047
|
+
idleReport: null,
|
|
2048
|
+
receive: null
|
|
2049
|
+
};
|
|
2050
|
+
members.set(name, m);
|
|
2051
|
+
boot(m, msg.p).catch((err) => {
|
|
2052
|
+
fatal(m, err instanceof Error ? err.message : String(err));
|
|
1893
2053
|
});
|
|
1894
|
-
};
|
|
1895
|
-
self.addEventListener("message", onInit);
|
|
2054
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@isoloop/runtime",
|
|
3
|
-
"version": "0.0.0-next.
|
|
3
|
+
"version": "0.0.0-next.5aa1a15",
|
|
4
4
|
"description": "Isolate-side runtime that runs inside each worker.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@isoloop/dom": "0.0.0-next.
|
|
31
|
-
"@isoloop/protocol": "0.0.0-next.
|
|
30
|
+
"@isoloop/dom": "0.0.0-next.5aa1a15",
|
|
31
|
+
"@isoloop/protocol": "0.0.0-next.5aa1a15"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"typescript": "7.0.2"
|