@use-worker/node 0.1.0-beta.0

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Andrew Ingram
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # @use-worker/node
2
+
3
+ ```sh
4
+ node --enable-source-maps --import @use-worker/node/register ./src/main.ts
5
+ ```
6
+
7
+ Requires Node 24 or later. Local ESM source and its Worker view are compiled in
8
+ memory through the packaged native SWC plugin. No application codegen step is
9
+ required. This package installs `@swc/core`; it does not install Vite or Oxc.
10
+
11
+ The root exports async `register()` and `disposeClients()`. Register before
12
+ loading application modules with dynamic imports. Idle pools allow process exit;
13
+ active calls and iterators retain their Workers. CommonJS and dependency source
14
+ under `node_modules` are outside the transform contract.
15
+
16
+ The native transform implements a subset: anonymous async arrow/function
17
+ expressions and whole modules of named async function/generator declarations.
18
+ Inline declarations, methods and complete capture validation remain unfinished.
19
+ Use the SWC package's compatibility guidance before adopting it.
20
+
21
+ Experimental, unpublished `0.0.0` packages.
22
+
23
+ See the [telemetry guide](../../docs/telemetry.md) for optional `otel: true`,
24
+ Worker setup, and graceful shutdown.
@@ -0,0 +1,63 @@
1
+ # Third-party notices
2
+
3
+ Parts of the scope analysis and function-hoisting approach in
4
+ `src/compiler/closure-scope.ts` and `src/compiler/hoist.ts` are adapted from the official Vite
5
+ React Server Components transform:
6
+
7
+ https://github.com/vitejs/vite-plugin-react/tree/11ad0af/packages/plugin-rsc/src/transforms
8
+
9
+ Vite is distributed under the following license:
10
+
11
+ > MIT License
12
+ >
13
+ > Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
14
+ >
15
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ > of this software and associated documentation files (the "Software"), to deal
17
+ > in the Software without restriction, including without limitation the rights
18
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ > copies of the Software, and to permit persons to whom the Software is
20
+ > furnished to do so, subject to the following conditions:
21
+ >
22
+ > The above copyright notice and this permission notice shall be included in all
23
+ > copies or substantial portions of the Software.
24
+ >
25
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
+ > SOFTWARE.
32
+
33
+ The directive typo-detection algorithm in `src/compiler/transform.ts` and capture-path
34
+ coalescing in `src/compiler/hoist.ts` are adapted from the Next.js Server Actions
35
+ transform:
36
+
37
+ https://github.com/vercel/next.js/blob/090f1b72753b2d0094235d27d46ffa0da5fc7d35/crates/next-custom-transforms/src/transforms/server_actions.rs#L3231-L3285
38
+
39
+ https://github.com/vercel/next.js/blob/090f1b72753b2d0094235d27d46ffa0da5fc7d35/crates/next-custom-transforms/src/transforms/server_actions.rs#L2920-L2966
40
+
41
+ Next.js is distributed under the following license:
42
+
43
+ > The MIT License (MIT)
44
+ >
45
+ > Copyright (c) 2025 Vercel, Inc.
46
+ >
47
+ > Permission is hereby granted, free of charge, to any person obtaining a copy
48
+ > of this software and associated documentation files (the "Software"), to deal
49
+ > in the Software without restriction, including without limitation the rights
50
+ > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
51
+ > copies of the Software, and to permit persons to whom the Software is
52
+ > furnished to do so, subject to the following conditions:
53
+ >
54
+ > The above copyright notice and this permission notice shall be included in all
55
+ > copies or substantial portions of the Software.
56
+ >
57
+ > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
58
+ > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
59
+ > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
60
+ > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
61
+ > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
62
+ > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
63
+ > SOFTWARE.
@@ -0,0 +1,9 @@
1
+ import { type WorkerLike } from "@use-worker/core/internal/client-runtime";
2
+ export * from "@use-worker/core/internal/client-runtime";
3
+ export declare const createWorkerClient: (createWorker: () => WorkerLike, options?: {
4
+ poolSize?: number;
5
+ instrumentation?: import("@use-worker/core/internal/instrumentation").Instrumentation;
6
+ hooks?: import("@use-worker/core/internal/instrumentation").InvocationHooks;
7
+ gracefulShutdown?: boolean;
8
+ } | undefined) => import("@use-worker/core/internal/client-runtime").WorkerRuntimeClient, disposeClients: () => Promise<void>;
9
+ export declare function createNodeWorker(url: URL, execArgv?: string[]): WorkerLike;
package/dist/client.js ADDED
@@ -0,0 +1,8 @@
1
+ import { Worker } from "node:worker_threads";
2
+ import { adaptNodeWorker } from "@use-worker/core/internal/node-client";
3
+ import { createWorkerClientRegistry, } from "@use-worker/core/internal/client-runtime";
4
+ export * from "@use-worker/core/internal/client-runtime";
5
+ export const { createWorkerClient, disposeClients } = createWorkerClientRegistry();
6
+ export function createNodeWorker(url, execArgv = ["--enable-source-maps"]) {
7
+ return adaptNodeWorker(new Worker(url, { execArgv }));
8
+ }
@@ -0,0 +1,7 @@
1
+ export declare const transformCounts: Map<string, number>;
2
+ export interface RegisterOptions {
3
+ otel?: boolean | "forward";
4
+ instrumentation?: string;
5
+ workerSetup?: string;
6
+ }
7
+ export declare function register(options?: RegisterOptions): import("module").ModuleHooks;
package/dist/hooks.js ADDED
@@ -0,0 +1,89 @@
1
+ // Node ESM source views are compiled on demand and returned in memory.
2
+ import { existsSync, readFileSync } from "node:fs";
3
+ import { registerHooks } from "node:module";
4
+ import { fileURLToPath, pathToFileURL } from "node:url";
5
+ import { transformSync } from "@use-worker/swc/compiler";
6
+ import { pluginPath as wasm } from "@use-worker/swc";
7
+ import { tmpdir } from "node:os";
8
+ import { join, resolve } from "node:path";
9
+ // Per-realm instrumentation for the integration test; not a transform cache.
10
+ export const transformCounts = new Map();
11
+ export function register(options = {}) {
12
+ return registerHooks({
13
+ resolve(specifier, context, nextResolve) {
14
+ try {
15
+ return nextResolve(specifier, context);
16
+ }
17
+ catch (error) {
18
+ // Match TypeScript's .js source imports, without overriding existing JS
19
+ // files or changing package resolution.
20
+ if (!(error instanceof Error) || !("code" in error) ||
21
+ error.code !== "ERR_MODULE_NOT_FOUND" ||
22
+ !(specifier.startsWith("./") || specifier.startsWith("../")) ||
23
+ !context.parentURL)
24
+ throw error;
25
+ const source = new URL(specifier, context.parentURL);
26
+ if (source.protocol !== "file:" || !source.pathname.endsWith(".js"))
27
+ throw error;
28
+ source.pathname = source.pathname.slice(0, -3) + ".ts";
29
+ if (!existsSync(source))
30
+ throw error;
31
+ return nextResolve(source.href, context);
32
+ }
33
+ },
34
+ load(url, context, nextLoad) {
35
+ const source = new URL(url);
36
+ if (source.protocol !== "file:" || !/\.(?:ts|mts|js|mjs|tsx|jsx)$/.test(source.pathname) ||
37
+ source.pathname.includes("/node_modules/"))
38
+ return nextLoad(url, context);
39
+ if (context.format === "commonjs" || context.format === "commonjs-typescript") {
40
+ if (readFileSync(source, "utf8").includes("use worker")) {
41
+ throw new Error("use-worker Node hooks require an ESM source module");
42
+ }
43
+ return nextLoad(url, context);
44
+ }
45
+ const mode = source.searchParams.get("use-worker") === "entry" ? "worker" : "main";
46
+ const encoded = source.searchParams.get("use-worker-options");
47
+ const telemetry = encoded ? JSON.parse(encoded) : options;
48
+ const workerSetup = telemetry.workerSetup ? pathToFileURL(resolve(telemetry.workerSetup)).href : undefined;
49
+ const instrumentation = telemetry.instrumentation ? pathToFileURL(resolve(telemetry.instrumentation)).href : undefined;
50
+ const workerEntry = new URL(source);
51
+ workerEntry.searchParams.set("use-worker", "entry");
52
+ if (telemetry.otel || telemetry.workerSetup || telemetry.instrumentation) {
53
+ workerEntry.searchParams.set("use-worker-options", JSON.stringify({ ...telemetry,
54
+ instrumentation: telemetry.instrumentation ? resolve(telemetry.instrumentation) : undefined,
55
+ workerSetup: telemetry.workerSetup ? resolve(telemetry.workerSetup) : undefined }));
56
+ }
57
+ const filename = fileURLToPath(source);
58
+ const result = transformSync(readFileSync(source, "utf8"), {
59
+ filename,
60
+ swcrc: false,
61
+ configFile: false,
62
+ sourceMaps: "inline",
63
+ jsc: {
64
+ parser: /\.(?:ts|mts|tsx)$/.test(source.pathname)
65
+ ? { syntax: "typescript", tsx: source.pathname.endsWith(".tsx") }
66
+ : { syntax: "ecmascript", jsx: source.pathname.endsWith(".jsx") },
67
+ target: "es2022",
68
+ experimental: {
69
+ plugins: [[wasm, {
70
+ mode,
71
+ otel: telemetry.otel ?? false, workerSetup, instrumentation,
72
+ // Resolve optional forwarding imports from the application's source module.
73
+ otelRuntime: telemetry.otel === "forward"
74
+ ? mode === "worker" ? "@use-worker/otel/worker" : "@use-worker/otel"
75
+ : import.meta.resolve("@use-worker/core/otel"),
76
+ clientRuntime: new URL("./loader-client.js", import.meta.url).href,
77
+ workerRuntime: import.meta.resolve("@use-worker/core/internal/node-worker"),
78
+ workerEntry: workerEntry.href,
79
+ }]],
80
+ cacheRoot: join(tmpdir(), "use-worker-swc-cache"),
81
+ },
82
+ },
83
+ module: { type: "es6" },
84
+ });
85
+ transformCounts.set(url, (transformCounts.get(url) ?? 0) + 1);
86
+ return { format: "module", shortCircuit: true, source: result.code };
87
+ },
88
+ });
89
+ }
@@ -0,0 +1,3 @@
1
+ export { disposeClients } from "./client.js";
2
+ /** Install source hooks before dynamically importing application modules. */
3
+ export declare function register(options?: import("./hooks.js").RegisterOptions): Promise<import("module").ModuleHooks>;
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export { disposeClients } from "./client.js";
2
+ /** Install source hooks before dynamically importing application modules. */
3
+ export async function register(options = {}) {
4
+ return (await import("./hooks.js")).register(options);
5
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./client.js";
2
+ export declare function createNodeWorker(url: URL): import("@use-worker/core/internal/client-runtime").WorkerLike;
@@ -0,0 +1,15 @@
1
+ import { createNodeWorker as createTransport } from "./client.js";
2
+ export * from "./client.js";
3
+ export function createNodeWorker(url) {
4
+ // Import from a data entry so the worker variant's URL query survives worker
5
+ // startup. The actual transformed module retains its original file URL.
6
+ const registration = new URL("./register.js", import.meta.url);
7
+ const options = url.searchParams.get("use-worker-options");
8
+ if (options)
9
+ registration.searchParams.set("use-worker-options", options);
10
+ const bootstrap = new URL(`data:text/javascript,${encodeURIComponent(`await import(${JSON.stringify(url.href)})`)}`);
11
+ return createTransport(bootstrap, [
12
+ "--enable-source-maps",
13
+ "--import", registration.href,
14
+ ]);
15
+ }
@@ -0,0 +1 @@
1
+ export { transformCounts } from "./hooks.js";
@@ -0,0 +1,4 @@
1
+ import { register } from "./hooks.js";
2
+ export { transformCounts } from "./hooks.js";
3
+ const options = new URL(import.meta.url).searchParams.get("use-worker-options");
4
+ register(options ? JSON.parse(options) : {});
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@use-worker/node",
3
+ "version": "0.1.0-beta.0",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "files": [
7
+ "dist",
8
+ "THIRD_PARTY_NOTICES.md",
9
+ "LICENSE"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/index.d.ts",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./register": "./dist/register.js",
17
+ "./internal/client": {
18
+ "types": "./dist/client.d.ts",
19
+ "default": "./dist/client.js"
20
+ },
21
+ "./internal/loader-client": {
22
+ "types": "./dist/loader-client.d.ts",
23
+ "default": "./dist/loader-client.js"
24
+ }
25
+ },
26
+ "dependencies": {
27
+ "@swc/core": "1.16.2",
28
+ "@use-worker/core": "0.1.0-beta.0",
29
+ "@use-worker/swc": "0.1.0-beta.0"
30
+ },
31
+ "engines": {
32
+ "node": ">=24.0.0"
33
+ },
34
+ "description": "Node ESM runtime loading for use-worker through native SWC.",
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "build": "node ../../scripts/build-package.mjs",
40
+ "typecheck": "tsc -p tsconfig.json --noEmit"
41
+ }
42
+ }