@nubjs/nub-darwin-arm64 0.0.1

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.
Files changed (31) hide show
  1. package/bin/nub +0 -0
  2. package/package.json +17 -0
  3. package/runtime/addons/nub-native.node +0 -0
  4. package/runtime/navigator-locks.mjs +135 -0
  5. package/runtime/node_modules/@oxc-transform/binding-darwin-arm64/README.md +3 -0
  6. package/runtime/node_modules/@oxc-transform/binding-darwin-arm64/package.json +41 -0
  7. package/runtime/node_modules/@oxc-transform/binding-darwin-arm64/transform.darwin-arm64.node +0 -0
  8. package/runtime/node_modules/get-tsconfig/LICENSE +21 -0
  9. package/runtime/node_modules/get-tsconfig/README.md +268 -0
  10. package/runtime/node_modules/get-tsconfig/dist/index.cjs +7 -0
  11. package/runtime/node_modules/get-tsconfig/dist/index.d.cts +2116 -0
  12. package/runtime/node_modules/get-tsconfig/dist/index.d.mts +2116 -0
  13. package/runtime/node_modules/get-tsconfig/dist/index.mjs +7 -0
  14. package/runtime/node_modules/get-tsconfig/package.json +46 -0
  15. package/runtime/node_modules/oxc-transform/LICENSE +22 -0
  16. package/runtime/node_modules/oxc-transform/README.md +84 -0
  17. package/runtime/node_modules/oxc-transform/browser.js +1 -0
  18. package/runtime/node_modules/oxc-transform/index.d.ts +658 -0
  19. package/runtime/node_modules/oxc-transform/index.js +598 -0
  20. package/runtime/node_modules/oxc-transform/package.json +114 -0
  21. package/runtime/node_modules/oxc-transform/webcontainer-fallback.cjs +21 -0
  22. package/runtime/node_modules/resolve-pkg-maps/LICENSE +21 -0
  23. package/runtime/node_modules/resolve-pkg-maps/README.md +216 -0
  24. package/runtime/node_modules/resolve-pkg-maps/dist/index.cjs +1 -0
  25. package/runtime/node_modules/resolve-pkg-maps/dist/index.d.cts +11 -0
  26. package/runtime/node_modules/resolve-pkg-maps/dist/index.d.mts +11 -0
  27. package/runtime/node_modules/resolve-pkg-maps/dist/index.mjs +1 -0
  28. package/runtime/node_modules/resolve-pkg-maps/package.json +42 -0
  29. package/runtime/polyfills.mjs +160 -0
  30. package/runtime/preload.mjs +355 -0
  31. package/runtime/worker-polyfill.mjs +91 -0
@@ -0,0 +1,355 @@
1
+ import module from "node:module";
2
+ import { readFileSync, writeFileSync, mkdirSync, statSync } from "node:fs";
3
+ import { fileURLToPath, pathToFileURL } from "node:url";
4
+ import { createRequire } from "node:module";
5
+ import { createHash } from "node:crypto";
6
+ import { join, dirname, resolve as pathResolve, extname as pathExtname } from "node:path";
7
+ import { transformSync } from "oxc-transform";
8
+ import { getTsconfig, createPathsMatcher } from "get-tsconfig";
9
+
10
+ const __require = createRequire(import.meta.url);
11
+
12
+ // ── Constants ───────────────────────────────────────────────────────
13
+ const TRANSPILE_EXTS = new Set([".ts", ".tsx", ".mts", ".cts", ".jsx"]);
14
+ const DATA_EXTS = { ".jsonc": "jsonc", ".json5": "json5", ".toml": "toml", ".yaml": "yaml", ".yml": "yaml", ".txt": "txt" };
15
+ const TS_PARENT_EXTS = new Set([".ts", ".tsx", ".mts", ".cts"]);
16
+
17
+ // Nub's N-API addon for data-format parsing (Rust-native YAML/TOML/JSON5/JSONC).
18
+ let nubNative = null;
19
+ for (const rel of ["./addons/nub-native.node", "../runtime/addons/nub-native.node"]) {
20
+ try { nubNative = __require(fileURLToPath(new URL(rel, import.meta.url))); break; } catch {}
21
+ }
22
+
23
+ // Packages resolved from Nub's distribution, not the user's.
24
+ const VENDORED_PACKAGES = new Set(["@oxc-project/runtime"]);
25
+
26
+ // Built-in modules provided by Nub (resolved to files in this distribution).
27
+ // connect() sockets deferred per design decision — "sockets" specifier not clobbered.
28
+ const BUILTIN_MODULES = new Map();
29
+
30
+ // Package clobbering: specifiers that resolve to a synthetic module
31
+ // re-exporting the native global instead of the userland package.
32
+ const CLOBBER_MAP = new Map([
33
+ ["@js-temporal/polyfill", () => `const T = globalThis.Temporal; export default T; export const Temporal = T;`],
34
+ ["urlpattern-polyfill", () => `export const URLPattern = globalThis.URLPattern;`],
35
+ ["abort-controller", () => `export const AbortController = globalThis.AbortController; export const AbortSignal = globalThis.AbortSignal; export default globalThis.AbortController;`],
36
+ ]);
37
+
38
+ // ── tsconfig cache ──────────────────────────────────────────────────
39
+ const tsconfigCache = new Map();
40
+
41
+ function getTsconfigForDir(dir) {
42
+ if (tsconfigCache.has(dir)) return tsconfigCache.get(dir);
43
+ const result = getTsconfig(dir);
44
+ const matcher = result ? createPathsMatcher(result) : null;
45
+ const entry = { tsconfig: result, matcher };
46
+ tsconfigCache.set(dir, entry);
47
+ return entry;
48
+ }
49
+
50
+ // ── Transpile cache ─────────────────────────────────────────────────
51
+ const NUB_VERSION = "0.1.0";
52
+ const CACHE_DISABLED = process.permission?.has !== undefined;
53
+ let cacheDir = null;
54
+
55
+ if (!CACHE_DISABLED) {
56
+ const base = process.env.XDG_CACHE_HOME || (process.env.HOME ? join(process.env.HOME, ".cache") : null);
57
+ if (base) {
58
+ cacheDir = join(base, "nub", "transpile");
59
+ try { mkdirSync(cacheDir, { recursive: true }); } catch { cacheDir = null; }
60
+ }
61
+ }
62
+
63
+ function cacheKey(source) {
64
+ return createHash("sha256").update(NUB_VERSION).update("\0").update(source).digest("hex");
65
+ }
66
+ function cacheGet(key) {
67
+ if (!cacheDir) return null;
68
+ const path = join(cacheDir, key);
69
+ const s = statSync(path, { throwIfNoEntry: false });
70
+ if (!s || !s.isFile()) return null;
71
+ return readFileSync(path, "utf8");
72
+ }
73
+ function cacheSet(key, value) {
74
+ if (!cacheDir) return;
75
+ try { writeFileSync(join(cacheDir, key), value); } catch {}
76
+ }
77
+
78
+ // ── Helpers ─────────────────────────────────────────────────────────
79
+ function extname(url) {
80
+ const path = url.includes("?") ? url.slice(0, url.indexOf("?")) : url;
81
+ const dot = path.lastIndexOf(".");
82
+ return dot === -1 ? "" : path.slice(dot);
83
+ }
84
+
85
+ function isNodeModules(url) {
86
+ return url.includes("/node_modules/") || url.includes("\\node_modules\\");
87
+ }
88
+
89
+ function fileExists(filePath) {
90
+ const s = statSync(filePath, { throwIfNoEntry: false });
91
+ return s !== undefined && s.isFile();
92
+ }
93
+
94
+ function dirExists(filePath) {
95
+ const s = statSync(filePath, { throwIfNoEntry: false });
96
+ return s !== undefined && s.isDirectory();
97
+ }
98
+
99
+ function safeRequireResolve(specifier) {
100
+ try { return __require.resolve(specifier); } catch { return null; }
101
+ }
102
+
103
+ function barePkg(specifier) {
104
+ return specifier.startsWith("@")
105
+ ? specifier.split("/").slice(0, 2).join("/")
106
+ : specifier.split("/")[0];
107
+ }
108
+
109
+ // ── Resolve hook ────────────────────────────────────────────────────
110
+ function resolve(specifier, context, nextResolve) {
111
+ // Skip node: and data: protocols.
112
+ if (specifier.startsWith("node:") || specifier.startsWith("data:")) {
113
+ return nextResolve(specifier, context);
114
+ }
115
+
116
+ // Skip Node built-in module names (without node: prefix).
117
+ if (module.builtinModules.includes(specifier)) {
118
+ return nextResolve(specifier, context);
119
+ }
120
+
121
+ // 1. Built-in modules provided by Nub.
122
+ if (BUILTIN_MODULES.has(specifier)) {
123
+ return { url: BUILTIN_MODULES.get(specifier), shortCircuit: true };
124
+ }
125
+
126
+ // 2. Vendored packages (e.g. @oxc-project/runtime).
127
+ const bare = barePkg(specifier);
128
+ if (VENDORED_PACKAGES.has(bare)) {
129
+ const resolved = safeRequireResolve(specifier);
130
+ if (resolved) return { url: pathToFileURL(resolved).href, shortCircuit: true };
131
+ }
132
+
133
+ // 2. Package clobbering.
134
+ if (CLOBBER_MAP.has(bare) && !isNodeModules(context.parentURL || "")) {
135
+ return { url: `data:text/javascript,${encodeURIComponent(CLOBBER_MAP.get(bare)())}`, shortCircuit: true };
136
+ }
137
+
138
+ // 3. tsconfig-paths (only for bare/aliased specifiers, not relative).
139
+ const parentURL = String(context.parentURL || "");
140
+ const parentExt = extname(parentURL);
141
+ if (!specifier.startsWith(".") && !specifier.startsWith("/") && !specifier.startsWith("file:") && !isNodeModules(parentURL)) {
142
+ const parentDir = parentURL.startsWith("file:") ? dirname(fileURLToPath(parentURL)) : process.cwd();
143
+ const { matcher } = getTsconfigForDir(parentDir);
144
+ if (matcher) {
145
+ const mapped = matcher(specifier);
146
+ if (mapped && mapped.length > 0) {
147
+ for (const candidate of mapped) {
148
+ const resolved = tryResolveFile(candidate, parentExt);
149
+ if (resolved) {
150
+ return { url: pathToFileURL(resolved).href, shortCircuit: true };
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+
157
+ // 4. Extensionless probing (only when parent is a TS file).
158
+ if (TS_PARENT_EXTS.has(parentExt) && (specifier.startsWith("./") || specifier.startsWith("../"))) {
159
+ const parentDir = dirname(fileURLToPath(parentURL));
160
+ const target = pathResolve(parentDir, specifier);
161
+ const resolved = tryResolveFile(target, parentExt);
162
+ if (resolved) {
163
+ return { url: pathToFileURL(resolved).href, shortCircuit: true };
164
+ }
165
+ }
166
+
167
+ return nextResolve(specifier, context);
168
+ }
169
+
170
+ // Try to resolve a file path with extensionless probing + .js→.ts swap.
171
+ function tryResolveFile(target, parentExt) {
172
+ // If the target already has an extension and exists, use it.
173
+ const existingExt = pathExtname(target);
174
+ if (existingExt && fileExists(target)) return target;
175
+
176
+ // .js → .ts swap (tsc emit convention reversal).
177
+ if (existingExt === ".js") {
178
+ const tsSwap = target.slice(0, -3) + ".ts";
179
+ if (fileExists(tsSwap)) return tsSwap;
180
+ const tsxSwap = target.slice(0, -3) + ".tsx";
181
+ if (fileExists(tsxSwap)) return tsxSwap;
182
+ }
183
+ if (existingExt === ".jsx") {
184
+ const tsxSwap = target.slice(0, -4) + ".tsx";
185
+ if (fileExists(tsxSwap)) return tsxSwap;
186
+ }
187
+
188
+ // Extensionless: probe in parent-ext-aware order.
189
+ if (!existingExt) {
190
+ const probeOrder = getProbeOrder(parentExt);
191
+ for (const ext of probeOrder) {
192
+ if (fileExists(target + ext)) return target + ext;
193
+ }
194
+ // Directory index probing.
195
+ if (dirExists(target)) {
196
+ for (const ext of probeOrder) {
197
+ const idx = join(target, "index" + ext);
198
+ if (fileExists(idx)) return idx;
199
+ }
200
+ }
201
+ }
202
+
203
+ return null;
204
+ }
205
+
206
+ function getProbeOrder(parentExt) {
207
+ switch (parentExt) {
208
+ case ".tsx": return [".tsx", ".ts", ".jsx", ".js", ".json"];
209
+ case ".mts": return [".mts", ".mjs"];
210
+ case ".cts": return [".cts", ".cjs"];
211
+ default: return [".ts", ".tsx", ".js", ".jsx", ".json"];
212
+ }
213
+ }
214
+
215
+ // ── Load hook ───────────────────────────────────────────────────────
216
+ function load(url, context, nextLoad) {
217
+ const ext = extname(url);
218
+
219
+ if (TRANSPILE_EXTS.has(ext)) return loadTranspile(url, ext);
220
+ if (ext in DATA_EXTS) return loadData(url, ext);
221
+
222
+ return nextLoad(url, context);
223
+ }
224
+
225
+ function loadTranspile(url, ext) {
226
+ const filePath = fileURLToPath(url);
227
+ const source = readFileSync(filePath, "utf8");
228
+ const dir = dirname(filePath);
229
+ const { tsconfig } = getTsconfigForDir(dir);
230
+ const co = tsconfig?.config?.compilerOptions;
231
+
232
+ // Cache key includes tsconfig compilerOptions so tsconfig changes invalidate.
233
+ const tsconfigHash = co ? JSON.stringify(co) : "";
234
+ const key = cacheKey(source + "\0" + ext + "\0" + tsconfigHash);
235
+ const cached = cacheGet(key);
236
+ if (cached) {
237
+ return { format: ext === ".cts" ? "commonjs" : "module", source: cached, shortCircuit: true };
238
+ }
239
+
240
+ const lang = ext === ".tsx" ? "tsx" : ext === ".jsx" ? "jsx" : "ts";
241
+ const opts = {
242
+ lang,
243
+ sourceType: ext === ".cts" ? "commonjs" : "module",
244
+ sourcemap: true,
245
+ typescript: {},
246
+ decorator: co?.experimentalDecorators !== false
247
+ ? { legacy: true, emitDecoratorMetadata: co?.emitDecoratorMetadata !== false }
248
+ : undefined,
249
+ };
250
+ if (ext === ".tsx" || ext === ".jsx") {
251
+ opts.jsx = {
252
+ runtime: co?.jsx === "react" ? "classic" : "automatic",
253
+ importSource: co?.jsxImportSource || "react",
254
+ };
255
+ }
256
+
257
+ const result = transformSync(filePath, source, opts);
258
+ if (result.errors.length > 0) {
259
+ throw new Error(`Transpile error in ${filePath}:\n${result.errors.map((e) => e.message).join("\n")}`);
260
+ }
261
+
262
+ let code = result.code;
263
+ if (result.map) {
264
+ const map = typeof result.map === "string" ? JSON.parse(result.map) : result.map;
265
+ map.sourcesContent = [source];
266
+ code += `\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(JSON.stringify(map)).toString("base64")}\n`;
267
+ }
268
+
269
+ cacheSet(key, code);
270
+ return { format: ext === ".cts" ? "commonjs" : "module", source: code, shortCircuit: true };
271
+ }
272
+
273
+ function loadData(url, ext) {
274
+ const filePath = fileURLToPath(url);
275
+ const raw = readFileSync(filePath, "utf8");
276
+ const kind = DATA_EXTS[ext];
277
+
278
+ if (kind === "txt") {
279
+ return { format: "module", source: `export default ${JSON.stringify(raw)};\n`, shortCircuit: true };
280
+ }
281
+
282
+ let parsed;
283
+ if (nubNative) {
284
+ if (kind === "yaml") parsed = nubNative.parseYaml(raw);
285
+ else if (kind === "toml") parsed = nubNative.parseToml(raw);
286
+ else if (kind === "json5") parsed = nubNative.parseJson5(raw);
287
+ else if (kind === "jsonc") parsed = nubNative.parseJsonc(raw);
288
+ } else {
289
+ if (kind === "yaml") parsed = lazyRequire("yaml").parse(raw);
290
+ else if (kind === "toml") parsed = lazyRequire("@iarna/toml").parse(raw);
291
+ else if (kind === "json5") parsed = lazyRequire("json5").parse(raw);
292
+ else if (kind === "jsonc") parsed = JSON.parse(stripJsonComments(raw));
293
+ }
294
+
295
+ if (parsed == null) {
296
+ return { format: "module", source: "export default undefined;\n", shortCircuit: true };
297
+ }
298
+
299
+ let code = `const _data = ${JSON.stringify(parsed)};\nexport default _data;\n`;
300
+ if (typeof parsed === "object" && !Array.isArray(parsed)) {
301
+ for (const key of Object.keys(parsed)) {
302
+ if (/^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(key)) {
303
+ code += `export const ${key} = _data[${JSON.stringify(key)}];\n`;
304
+ }
305
+ }
306
+ }
307
+ return { format: "module", source: code, shortCircuit: true };
308
+ }
309
+
310
+ function lazyRequire(pkg) {
311
+ try { return __require(pkg); } catch {
312
+ throw new Error(`Nub: importing this file requires the "${pkg}" package.\nInstall it: npm install ${pkg}`);
313
+ }
314
+ }
315
+
316
+ function stripJsonComments(text) {
317
+ let result = "", i = 0, inString = false, escape = false;
318
+ while (i < text.length) {
319
+ const ch = text[i];
320
+ if (escape) { result += ch; escape = false; i++; continue; }
321
+ if (inString) { if (ch === "\\") escape = true; if (ch === '"') inString = false; result += ch; i++; continue; }
322
+ if (ch === '"') { inString = true; result += ch; i++; continue; }
323
+ if (ch === "/" && text[i + 1] === "/") { while (i < text.length && text[i] !== "\n") i++; continue; }
324
+ if (ch === "/" && text[i + 1] === "*") { i += 2; while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++; i += 2; continue; }
325
+ result += ch; i++;
326
+ }
327
+ return result;
328
+ }
329
+
330
+ // ── Pre-load clobbered polyfill packages BEFORE hooks register ──────
331
+ // Packages in CLOBBER_MAP can't be imported after hooks register because
332
+ // the resolve hook returns a synthetic module instead of the real package.
333
+ // Load them here via CJS require (which isn't yet hooked) and stash them.
334
+ const __preloadedPolyfills = {};
335
+ try { __preloadedPolyfills.temporal = __require("@js-temporal/polyfill"); } catch {}
336
+ try { __preloadedPolyfills.urlpattern = __require("urlpattern-polyfill"); } catch {}
337
+
338
+ // ── Register hooks ──────────────────────────────────────────────────
339
+ module.registerHooks({ resolve, load });
340
+
341
+ // ── Polyfill preloads ───────────────────────────────────────────────
342
+ globalThis.__nubPreloaded = __preloadedPolyfills;
343
+ await import("./polyfills.mjs");
344
+ delete globalThis.__nubPreloaded;
345
+ delete globalThis.__nubRequire;
346
+ delete globalThis.__nubRequire;
347
+
348
+ // ── Watch-mode dependency reporting ─────────────────────────────────
349
+ if (process.env.WATCH_REPORT_DEPENDENCIES === "1" && process.send) {
350
+ const deps = [];
351
+ for (const [, entry] of tsconfigCache) {
352
+ if (entry.tsconfig?.path) deps.push(entry.tsconfig.path);
353
+ }
354
+ if (deps.length > 0) process.send({ "watch:require": deps });
355
+ }
@@ -0,0 +1,91 @@
1
+ // Browser-shape Worker global polyfill for Node.js.
2
+ // Wraps node:worker_threads.Worker with EventTarget inheritance,
3
+ // real MessageEvent/ErrorEvent, and URL-only constructor (Deno shape).
4
+
5
+ import { Worker as NodeWorker, parentPort, isMainThread } from "node:worker_threads";
6
+ import { fileURLToPath } from "node:url";
7
+
8
+ if (typeof globalThis.Worker === "undefined") {
9
+ class Worker extends EventTarget {
10
+ #worker;
11
+
12
+ constructor(url, options = {}) {
13
+ super();
14
+
15
+ let workerPath;
16
+ if (url instanceof URL) {
17
+ workerPath = fileURLToPath(url);
18
+ } else if (typeof url === "string") {
19
+ if (url.startsWith("file://")) {
20
+ workerPath = fileURLToPath(url);
21
+ } else {
22
+ workerPath = url;
23
+ }
24
+ } else {
25
+ throw new TypeError("Worker constructor: url must be a string or URL");
26
+ }
27
+
28
+ const type = options.type || "module";
29
+ this.#worker = new NodeWorker(workerPath, {
30
+ ...options,
31
+ eval: false,
32
+ execArgv: process.execArgv,
33
+ });
34
+
35
+ this.#worker.on("message", (data) => {
36
+ this.dispatchEvent(new MessageEvent("message", { data }));
37
+ });
38
+
39
+ this.#worker.on("messageerror", (err) => {
40
+ this.dispatchEvent(new MessageEvent("messageerror", { data: err }));
41
+ });
42
+
43
+ this.#worker.on("error", (err) => {
44
+ this.dispatchEvent(new ErrorEvent("error", { error: err, message: err.message }));
45
+ });
46
+
47
+ this.#worker.on("exit", (code) => {
48
+ this.dispatchEvent(new Event("exit"));
49
+ });
50
+ }
51
+
52
+ postMessage(data, transfer) {
53
+ this.#worker.postMessage(data, transfer);
54
+ }
55
+
56
+ terminate() {
57
+ return this.#worker.terminate();
58
+ }
59
+
60
+ #onmessageHandler = null;
61
+ get onmessage() { return this.#onmessageHandler; }
62
+ set onmessage(fn) {
63
+ if (this.#onmessageHandler) this.removeEventListener("message", this.#onmessageHandler);
64
+ this.#onmessageHandler = fn;
65
+ if (fn) this.addEventListener("message", fn);
66
+ }
67
+
68
+ #onerrorHandler = null;
69
+ get onerror() { return this.#onerrorHandler; }
70
+ set onerror(fn) {
71
+ if (this.#onerrorHandler) this.removeEventListener("error", this.#onerrorHandler);
72
+ this.#onerrorHandler = fn;
73
+ if (fn) this.addEventListener("error", fn);
74
+ }
75
+ }
76
+
77
+ globalThis.Worker = Worker;
78
+ }
79
+
80
+ // Worker-side bootstrap: set up self, postMessage, close.
81
+ if (!isMainThread && parentPort) {
82
+ if (typeof globalThis.self === "undefined") {
83
+ globalThis.self = globalThis;
84
+ }
85
+ if (typeof globalThis.postMessage === "undefined") {
86
+ globalThis.postMessage = (data, transfer) => parentPort.postMessage(data, transfer);
87
+ }
88
+ if (typeof globalThis.close === "undefined") {
89
+ globalThis.close = () => process.exit(0);
90
+ }
91
+ }