@jslop/node-adapter 0.1.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 p-arndt
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,85 @@
1
+ # @jslop/node-adapter
2
+
3
+ A Node HTTP wrapper around a built JSlop SSR `render(url)`. Serves static assets from `dist/client/` and falls through to the SSR entry for everything else.
4
+
5
+ ```bash
6
+ pnpm add @jslop/node-adapter
7
+ ```
8
+
9
+ ## Usage
10
+
11
+ Build your app first (see [`@jslop/vite`](../vite) docs):
12
+
13
+ ```bash
14
+ vite build # → dist/client/
15
+ vite build --ssr # → dist/server/entry-server.js
16
+ ```
17
+
18
+ Then drop in a serve script:
19
+
20
+ ```js
21
+ // serve.mjs
22
+ import { createServer } from "@jslop/node-adapter";
23
+ import { render } from "./dist/server/entry-server.js";
24
+ import { resolve, dirname } from "node:path";
25
+ import { fileURLToPath } from "node:url";
26
+
27
+ const here = dirname(fileURLToPath(import.meta.url));
28
+ const server = createServer({
29
+ render,
30
+ clientDir: resolve(here, "dist/client"),
31
+ });
32
+
33
+ server.listen(Number(process.env.PORT ?? 3000), () => {
34
+ console.log(`listening on http://localhost:${process.env.PORT ?? 3000}`);
35
+ });
36
+ ```
37
+
38
+ ```bash
39
+ node serve.mjs
40
+ ```
41
+
42
+ ## API
43
+
44
+ ### `createHandler(opts) → (req, res) => void`
45
+
46
+ Returns a Node HTTP request handler.
47
+
48
+ | Option | Type | Required | Notes |
49
+ |----------------------|-------------------------------------------------------------------|----------|----------------------------------------------------------------------|
50
+ | `render` | `RenderFn` | yes | The function exported by `dist/server/entry-server.js`. |
51
+ | `clientDir` | `string` | yes | Absolute path to the built client dir (typically `dist/client`). |
52
+ | `title` | `(url, params) => string` | no | Forwarded to `render` as `opts.title`. |
53
+ | `clientManifestPath` | `string` | no | Override path to `.vite/manifest.json`. Defaults to `<clientDir>/.vite/manifest.json`. |
54
+
55
+ Behavior:
56
+
57
+ - If the request path has a file extension (`.js`, `.css`, `.png`, ...) and the file exists under `clientDir`, it's served as a static asset.
58
+ - Files under `/assets/` get `cache-control: public, max-age=31536000, immutable` — safe because Vite hashes their filenames.
59
+ - Path traversal is blocked: anything that resolves outside `clientDir` falls through to `render`.
60
+ - All other requests call `render(url, { title, clientManifestPath })`. The result's `status`, `headers`, and `html` are written to the response.
61
+ - Render errors are logged and answered with `500 internal server error`.
62
+
63
+ ### `createServer(opts) → http.Server`
64
+
65
+ Same options as `createHandler`, plus passes the handler to `http.createServer`. Returns the unstarted server — call `.listen(port)` yourself.
66
+
67
+ ### Types
68
+
69
+ ```ts
70
+ type RenderFn = (
71
+ url: string,
72
+ opts?: {
73
+ appScriptUrl?: string;
74
+ stylesheets?: string[];
75
+ title?: (url: string, params: Record<string, string>) => string;
76
+ clientManifestPath?: string;
77
+ }
78
+ ) => Promise<{
79
+ status: number;
80
+ html: string;
81
+ headers: Record<string, string>;
82
+ }>;
83
+ ```
84
+
85
+ The `RenderFn` shape is intentionally request-agnostic — only a URL string in, a result object out — so Bun / edge / fetch-handler adapters can be written against the same contract.
@@ -0,0 +1,39 @@
1
+ import { type IncomingMessage, type ServerResponse, type Server } from "node:http";
2
+ /**
3
+ * Shape of `render` exported by `virtual:jslop-entry-server`.
4
+ */
5
+ export type RenderFn = (url: string, opts?: {
6
+ appScriptUrl?: string;
7
+ stylesheets?: string[];
8
+ title?: (url: string, params: Record<string, string>) => string;
9
+ clientManifestPath?: string;
10
+ }) => Promise<RenderResult>;
11
+ export interface RenderResult {
12
+ status: number;
13
+ html: string;
14
+ headers: Record<string, string>;
15
+ }
16
+ export interface NodeAdapterOptions {
17
+ /** The bundled `render` function from `dist/server/entry-server.js`. */
18
+ render: RenderFn;
19
+ /** Absolute path to the client build dir (typically `dist/client`). */
20
+ clientDir: string;
21
+ /** Optional title generator passed through to render(). */
22
+ title?: (url: string, params: Record<string, string>) => string;
23
+ /** Override path to `.vite/manifest.json`. Defaults to `<clientDir>/.vite/manifest.json`. */
24
+ clientManifestPath?: string;
25
+ }
26
+ /**
27
+ * Build a Node HTTP request handler from a JSlop SSR build.
28
+ *
29
+ * Static assets under `clientDir` are served with long cache headers when they
30
+ * include a hash in the filename (Vite default for `assets/`); other paths
31
+ * fall through to `render(url)`.
32
+ */
33
+ export declare function createHandler(opts: NodeAdapterOptions): (req: IncomingMessage, res: ServerResponse) => void;
34
+ /**
35
+ * Convenience: spin up an http.Server bound to a port.
36
+ */
37
+ export declare function createServer(opts: NodeAdapterOptions & {
38
+ port?: number;
39
+ }): Server;
package/dist/index.js ADDED
@@ -0,0 +1,100 @@
1
+ import { createServer as createHttpServer } from "node:http";
2
+ import { readFile, stat } from "node:fs/promises";
3
+ import { resolve, join, extname, normalize, sep } from "node:path";
4
+ const MIME = {
5
+ ".js": "application/javascript; charset=utf-8",
6
+ ".mjs": "application/javascript; charset=utf-8",
7
+ ".css": "text/css; charset=utf-8",
8
+ ".html": "text/html; charset=utf-8",
9
+ ".json": "application/json; charset=utf-8",
10
+ ".svg": "image/svg+xml",
11
+ ".png": "image/png",
12
+ ".jpg": "image/jpeg",
13
+ ".jpeg": "image/jpeg",
14
+ ".gif": "image/gif",
15
+ ".webp": "image/webp",
16
+ ".avif": "image/avif",
17
+ ".ico": "image/x-icon",
18
+ ".woff": "font/woff",
19
+ ".woff2": "font/woff2",
20
+ ".ttf": "font/ttf",
21
+ ".otf": "font/otf",
22
+ ".map": "application/json; charset=utf-8",
23
+ ".txt": "text/plain; charset=utf-8",
24
+ ".wasm": "application/wasm",
25
+ };
26
+ /**
27
+ * Build a Node HTTP request handler from a JSlop SSR build.
28
+ *
29
+ * Static assets under `clientDir` are served with long cache headers when they
30
+ * include a hash in the filename (Vite default for `assets/`); other paths
31
+ * fall through to `render(url)`.
32
+ */
33
+ export function createHandler(opts) {
34
+ const clientDir = resolve(opts.clientDir);
35
+ const manifestPath = opts.clientManifestPath ?? join(clientDir, ".vite", "manifest.json");
36
+ return async (req, res) => {
37
+ try {
38
+ const url = req.url ?? "/";
39
+ const pathname = url.split("?")[0] || "/";
40
+ // Try a static file under clientDir first if the path looks like an
41
+ // asset (has an extension, isn't a route).
42
+ const ext = extname(pathname);
43
+ if (ext) {
44
+ const served = await tryServeStatic(clientDir, pathname, ext, res);
45
+ if (served)
46
+ return;
47
+ }
48
+ const result = await opts.render(url, {
49
+ title: opts.title,
50
+ clientManifestPath: manifestPath,
51
+ });
52
+ res.statusCode = result.status;
53
+ for (const [k, v] of Object.entries(result.headers)) {
54
+ res.setHeader(k, v);
55
+ }
56
+ res.end(result.html);
57
+ }
58
+ catch (err) {
59
+ // eslint-disable-next-line no-console
60
+ console.error("[@jslop/node-adapter] render error:", err);
61
+ res.statusCode = 500;
62
+ res.setHeader("content-type", "text/plain; charset=utf-8");
63
+ res.end("internal server error");
64
+ }
65
+ };
66
+ }
67
+ async function tryServeStatic(clientDir, pathname, ext, res) {
68
+ // Resolve under clientDir and guard against path traversal.
69
+ const rel = pathname.replace(/^\/+/, "");
70
+ const target = normalize(join(clientDir, rel));
71
+ if (!target.startsWith(clientDir + sep) && target !== clientDir) {
72
+ return false;
73
+ }
74
+ try {
75
+ const st = await stat(target);
76
+ if (!st.isFile())
77
+ return false;
78
+ const buf = await readFile(target);
79
+ res.statusCode = 200;
80
+ res.setHeader("content-type", MIME[ext] ?? "application/octet-stream");
81
+ // Vite hashes filenames under /assets/, so those are safe to cache hard.
82
+ if (rel.startsWith("assets/")) {
83
+ res.setHeader("cache-control", "public, max-age=31536000, immutable");
84
+ }
85
+ res.end(buf);
86
+ return true;
87
+ }
88
+ catch {
89
+ return false;
90
+ }
91
+ }
92
+ /**
93
+ * Convenience: spin up an http.Server bound to a port.
94
+ */
95
+ export function createServer(opts) {
96
+ const handler = createHandler(opts);
97
+ const server = createHttpServer(handler);
98
+ return server;
99
+ }
100
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,IAAI,gBAAgB,EAA0D,MAAM,WAAW,CAAC;AACrH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,WAAW,CAAC;AAgCnE,MAAM,IAAI,GAA2B;IACnC,KAAK,EAAE,uCAAuC;IAC9C,MAAM,EAAE,uCAAuC;IAC/C,MAAM,EAAE,yBAAyB;IACjC,OAAO,EAAE,0BAA0B;IACnC,OAAO,EAAE,iCAAiC;IAC1C,MAAM,EAAE,eAAe;IACvB,MAAM,EAAE,WAAW;IACnB,MAAM,EAAE,YAAY;IACpB,OAAO,EAAE,YAAY;IACrB,MAAM,EAAE,WAAW;IACn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,CAAC,CAAC;QACvE,yEAAyE;QACzE,IAAI,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;YAC9B,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,qCAAqC,CAAC,CAAC;QACxE,CAAC;QACD,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACb,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,YAAY,CAAC,IAA4C;IACvE,MAAM,OAAO,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC;IACpC,MAAM,MAAM,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@jslop/node-adapter",
3
+ "version": "0.1.0",
4
+ "description": "Node.js adapter for JSlop production builds. Serves the SSR render() bundle plus static assets from dist/client with long-cache headers for hashed files.",
5
+ "keywords": [
6
+ "jslop",
7
+ "node",
8
+ "adapter",
9
+ "ssr",
10
+ "framework"
11
+ ],
12
+ "license": "MIT",
13
+ "author": "p-arndt",
14
+ "homepage": "https://github.com/p-arndt/jslop#readme",
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/p-arndt/jslop.git",
18
+ "directory": "packages/node-adapter"
19
+ },
20
+ "bugs": "https://github.com/p-arndt/jslop/issues",
21
+ "engines": {
22
+ "node": ">=20"
23
+ },
24
+ "type": "module",
25
+ "main": "./dist/index.js",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": {
29
+ "types": "./dist/index.d.ts",
30
+ "import": "./dist/index.js"
31
+ }
32
+ },
33
+ "files": [
34
+ "dist",
35
+ "README.md"
36
+ ],
37
+ "publishConfig": {
38
+ "access": "public"
39
+ },
40
+ "devDependencies": {
41
+ "@types/node": "^25.7.0",
42
+ "typescript": "^5.6.3"
43
+ },
44
+ "scripts": {
45
+ "build": "tsc -p tsconfig.json"
46
+ }
47
+ }