Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940// What survives a postMessage, what refuses to clone, and the module's small helpers.const wt = require("node:worker_threads");const { MessageChannel, receiveMessageOnPort } = wt;
const { port1, port2 } = new MessageChannel();const tryPost = (label, value) => { try { port1.postMessage(value); console.log(label, "posted"); } catch (e) { console.log(label, e.name, e.constructor.name, e.code, e.message); }};tryPost("fn", { f() {} });tryPost("arrow", () => 1);tryPost("symbol", Symbol("s"));class Point { constructor() { this.x = 1; } get y() { return 2; } }const cyclic = { name: "c" };cyclic.self = cyclic;const err = new RangeError("r");err.extra = 1;port1.postMessage([new Point(), cyclic, err, new Error("plain", { cause: "why" }), undefined, null, -0, NaN, [1, , 3], Buffer.from("hi"), new Float64Array([1.5]), new DataView(new ArrayBuffer(2)), Object(1), Object("s"), Object(false)]);const [pt, cyc, re, plain, u, n, negz, nan, holes, buf, f64, dv, num, str, bool] = receiveMessageOnPort(port2).message;console.log("point", pt.constructor.name, pt.x, pt.y);console.log("cyclic", cyc.self === cyc, cyc.name);console.log("error", re.constructor.name, re.message, re.extra, typeof re.stack);console.log("cause", plain.cause);console.log("prims", u, n, Object.is(negz, -0), Number.isNaN(nan), 1 in holes, holes.length);console.log("buf", buf.constructor.name, Buffer.from(buf).toString(), f64[0], dv.byteLength);console.log("boxed", typeof num, num.valueOf(), str.valueOf(), bool.valueOf());
wt.setEnvironmentData("k", { v: 1 });console.log("envdata", JSON.stringify(wt.getEnvironmentData("k")), wt.getEnvironmentData("missing"));const ab = new ArrayBuffer(1);console.log("untransferable", wt.isMarkedAsUntransferable(ab));wt.markAsUntransferable(ab);console.log("marked", wt.isMarkedAsUntransferable(ab));console.log("share env", typeof wt.SHARE_ENV, "resourceLimits", JSON.stringify(wt.resourceLimits));port1.close();