@tschk/moonshine-deploy-node 0.4.4 → 0.4.80

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 (3) hide show
  1. package/README.md +11 -0
  2. package/package.json +5 -5
  3. package/src/index.ts +35 -15
package/README.md CHANGED
@@ -11,6 +11,10 @@ await nodeAdapter.build(manifest, ".moonshine");
11
11
  - Target runtime: `node`.
12
12
  - Capabilities: `streaming`, `islands`, `revalidation`.
13
13
  - Output: `server.ts` using `node:http` and a `Request` / `Response` bridge.
14
+ - React builds with a compiler server bundle also emit a self-contained
15
+ `server.mjs`. Deploy it beside `public/` and run `node server.mjs` on Node 22;
16
+ neither source files nor runtime packages are needed. Building requires Bun.
17
+ Manifests without a compiler server bundle retain the legacy `server.ts` output.
14
18
  - Native builds also emit a bundled `server.js`; deploy it beside `public/` and run
15
19
  `node server.js`. The bundle includes its manifest and needs no runtime packages.
16
20
  This path is verified on Node 22. Building still requires Bun.
@@ -21,3 +25,10 @@ static rendering, and explicit SPA clients; hydration and streaming are unfinish
21
25
 
22
26
  See [docs/DEPLOYMENT.md](../../docs/DEPLOYMENT.md) and
23
27
  [docs/MANIFEST.md](../../docs/MANIFEST.md).
28
+
29
+ The Node adapter targets Node's HTTP server. Its regression suite also checks
30
+ normal body completion and partial-upload cancellation in Bun. Bun 1.3.10's
31
+ `node:http` emulation clears abort callbacks after body EOF, so disconnect cancellation
32
+ following a completed upload is unsupported in that combination. Use the native
33
+ Bun adapter for Bun deployments; completed-upload disconnects are checked in
34
+ actual Node. See the [pinned Bun implementation](https://github.com/oven-sh/bun/blob/bun-v1.3.10/src/js/node/_http_incoming.ts).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tschk/moonshine-deploy-node",
3
- "version": "0.4.4",
3
+ "version": "0.4.80",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
@@ -15,20 +15,20 @@
15
15
  "test": "bun test"
16
16
  },
17
17
  "dependencies": {
18
- "@tschk/moonshine-framework": "^0.4.4",
19
- "@tschk/moonshine-server": "^0.4.4"
18
+ "@tschk/moonshine-framework": "^0.4.80",
19
+ "@tschk/moonshine-server": "^0.4.80"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@types/bun": "1.3.14",
23
23
  "@types/node": "^22.0.0",
24
24
  "typescript": "7.0.2",
25
- "@tschk/moonshine-adapter-conformance": "^0.4.4"
25
+ "@tschk/moonshine-adapter-conformance": "^0.4.80"
26
26
  },
27
27
  "files": [
28
28
  "src"
29
29
  ],
30
30
  "peerDependencies": {
31
- "@tschk/moonshine-react": "^0.4.4"
31
+ "@tschk/moonshine-react": "^0.4.80"
32
32
  },
33
33
  "peerDependenciesMeta": {
34
34
  "@tschk/moonshine-react": {
package/src/index.ts CHANGED
@@ -1,11 +1,13 @@
1
- import { mkdir, writeFile } from "node:fs/promises";
1
+ import { mkdir, rm, writeFile } from "node:fs/promises";
2
2
  import {
3
3
  createServer,
4
4
  type IncomingMessage,
5
5
  type ServerResponse,
6
6
  } from "node:http";
7
+ import { existsSync } from "node:fs";
7
8
  import { resolve } from "node:path";
8
9
  import { Readable } from "node:stream";
10
+ import { fileURLToPath } from "node:url";
9
11
  import {
10
12
  type DeploymentAdapter,
11
13
  type MoonshineManifest,
@@ -108,7 +110,12 @@ export function createNodeHandler(
108
110
  closed = true;
109
111
  controller.abort();
110
112
  };
111
- req.once("close", onClose);
113
+ // IncomingMessage also closes after a normal body read; only an
114
+ // incomplete message means the request was interrupted.
115
+ const onRequestClose = () => {
116
+ if (!req.complete) onClose();
117
+ };
118
+ req.once("close", onRequestClose);
112
119
  res.once("close", onClose);
113
120
 
114
121
  const headers = new Headers();
@@ -146,7 +153,7 @@ export function createNodeHandler(
146
153
  res.end(String(error));
147
154
  return;
148
155
  } finally {
149
- req.off("close", onClose);
156
+ req.off("close", onRequestClose);
150
157
  res.off("close", onClose);
151
158
  }
152
159
 
@@ -183,6 +190,7 @@ export const nodeAdapter: DeploymentAdapter = {
183
190
  build: async (manifest, outDir) => {
184
191
  validateRuntime(nodeAdapter, manifest);
185
192
  await mkdir(outDir, { recursive: true });
193
+ await rm(resolve(outDir, "server.mjs"), { force: true });
186
194
  const assets = await Promise.all(
187
195
  manifest.assets.map(async (asset) => {
188
196
  const file = await copyAsset(asset, outDir);
@@ -195,17 +203,28 @@ export const nodeAdapter: DeploymentAdapter = {
195
203
  JSON.stringify(builtManifest, null, 2) + "\n",
196
204
  );
197
205
  const native = manifest.renderer === "native";
198
- const rendererImport = native
199
- ? 'import { modules, renderer } from "./dist/server.js";'
200
- : 'import { modules } from "./dist/server.js";\nimport { reactRenderer as renderer } from "@tschk/moonshine-react";';
206
+ const bundled =
207
+ native ||
208
+ ((manifest.renderer ?? "react") === "react" &&
209
+ Boolean(
210
+ manifest.entries.server &&
211
+ existsSync(resolve(outDir, manifest.entries.server)),
212
+ ));
213
+ const serverEntry = manifest.entries.server ?? "dist/server.js";
214
+ // The bundle exports the renderer bound to its own route modules (and, for
215
+ // React, its own React instance).
216
+ const rendererImport =
217
+ native || (manifest.renderer ?? "react") === "react"
218
+ ? `import { modules, renderer } from ${JSON.stringify(`./${serverEntry}`)};`
219
+ : 'import { modules } from "./dist/server.js";\nimport { reactRenderer as renderer } from "@tschk/moonshine-react";';
201
220
  const entry = `import { createServer } from "node:http";
202
- import { createRequestHandler } from "@tschk/moonshine-server";
203
- import { createNodeHandler } from "@tschk/moonshine-deploy-node";
221
+ import { createRequestHandler } from ${JSON.stringify(bundled ? fileURLToPath(import.meta.resolve(`@tschk/moonshine-server${manifest.dataRequests ? "/data-http" : ""}`)) : `@tschk/moonshine-server${manifest.dataRequests ? "/data-http" : ""}`)};
222
+ import { createNodeHandler } from ${JSON.stringify(bundled ? fileURLToPath(import.meta.resolve("@tschk/moonshine-deploy-node")) : "@tschk/moonshine-deploy-node")};
204
223
  import { dirname, resolve } from "node:path";
205
224
  import { fileURLToPath } from "node:url";
206
225
  import manifest from "./manifest.json" with { type: "json" };
207
226
  ${rendererImport}
208
- ${manifest.requestContext ? 'import { requestScope } from "./dist/server.js";' : ""}
227
+ ${manifest.requestContext ? `import { requestScope } from ${JSON.stringify(`./${serverEntry}`)};` : ""}
209
228
 
210
229
  const artifactDir = dirname(fileURLToPath(import.meta.url));
211
230
  const projectDir = resolve(artifactDir, "..");
@@ -223,7 +242,7 @@ const resolvedManifest = {
223
242
  };
224
243
 
225
244
  const fetch = createRequestHandler({
226
- manifest: ${native ? "manifest" : "resolvedManifest"},
245
+ manifest: ${bundled ? "manifest" : "resolvedManifest"},
227
246
  modules,
228
247
  renderer,${manifest.requestContext ? "\n requestScope," : ""}
229
248
  staticDir: artifactDir + "/public",
@@ -238,18 +257,19 @@ server.listen(Number.isInteger(envPort) && envPort >= 0 && envPort <= 65535 ? en
238
257
  `;
239
258
  const entryPath = resolve(outDir, "server.ts");
240
259
  await writeFile(entryPath, entry);
241
- if (native) {
260
+ if (bundled) {
242
261
  const result = await Bun.build({
243
262
  entrypoints: [entryPath],
244
263
  outdir: outDir,
264
+ naming: native ? "server.js" : "server.mjs",
245
265
  target: "node",
246
266
  packages: "bundle",
267
+ ...(native
268
+ ? {}
269
+ : { define: { "process.env.NODE_ENV": '"production"' } }),
247
270
  });
248
271
  if (!result.success)
249
- throw new AggregateError(
250
- result.logs,
251
- "Native Node server bundle failed",
252
- );
272
+ throw new AggregateError(result.logs, "Node server bundle failed");
253
273
  }
254
274
  },
255
275
  };