@sbuilder/render 0.0.0-stage → 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 Store Builder
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 CHANGED
@@ -1,3 +1,22 @@
1
- # Temporary Holding Version
1
+ # @sbuilder/render
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Renders a Store Builder page **exactly** as the storefront serves it, inside Node.js — the
4
+ platform's own Go renderer (`server/cmd/renderpage`: the real publish + serve path) compiled to
5
+ WebAssembly. Not a re-implementation: the same code, byte for byte (proven by the platform's
6
+ parity tests against its real router, and by a wasm-vs-native test).
7
+
8
+ ```js
9
+ import { renderPage } from '@sbuilder/render';
10
+
11
+ const { status, headers, html } = await renderPage(envelope);
12
+ ```
13
+
14
+ `envelope` is one page document plus the site data it reads. Get it ready-made from
15
+ `POST /api/v1/pages/{id}/render-context` (send the document; the platform answers with the
16
+ envelope) — you never assemble it by hand. `@sbuilder/cli/host` does both steps for a site repo,
17
+ and `sb site next` wires that into a Next.js app.
18
+
19
+ - Server-side only. The module is compiled once per process; each render runs in a fresh
20
+ instance with no filesystem, network or environment (stdin in, stdout out). ~25–60 ms a page.
21
+ - `RenderError` carries the renderer's own sentence for a bad envelope.
22
+ - Node ≥ 20. `node:wasi` prints an ExperimentalWarning once.
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export interface RenderResult {
2
+ status: number;
3
+ headers: { etag?: string; cacheControl?: string; contentType?: string; location?: string };
4
+ html: string;
5
+ /** Empty in inline mode; otherwise serve each lane's `css` at its `url`. */
6
+ cssLanes: { lane: string; hash: string; media: string; url: string; css: string }[];
7
+ }
8
+ export class RenderError extends Error {}
9
+ /** The cmd/renderpage envelope: { site, page:{ document, … }, siteSettings, theme, data, runtime, request, … }. */
10
+ export function renderPage(envelope: Record<string, unknown>): Promise<RenderResult>;
package/index.js ADDED
@@ -0,0 +1,64 @@
1
+ import { mkdtempSync, openSync, closeSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
2
+ import { tmpdir } from 'node:os';
3
+ import { join } from 'node:path';
4
+ import { WASI } from 'node:wasi';
5
+
6
+ /**
7
+ * The platform's page renderer (server/cmd/renderpage) compiled to WebAssembly.
8
+ * One envelope in → the HTML a storefront visitor receives out, byte-identical
9
+ * to the platform (proven in Go: cmd/renderpage parity + wasm-vs-native tests).
10
+ *
11
+ * The module is compiled ONCE per process and a fresh instance runs each render,
12
+ * so no state leaks from one page to the next. It gets no filesystem, no network
13
+ * and no environment: input in on stdin, output out on stdout.
14
+ */
15
+
16
+ let compiled;
17
+ const wasmURL = new URL('./renderpage.wasm', import.meta.url);
18
+
19
+ function module_() {
20
+ compiled ??= WebAssembly.compile(readFileSync(wasmURL));
21
+ return compiled;
22
+ }
23
+
24
+ export class RenderError extends Error {}
25
+
26
+ /** Renders one page. `envelope` is the cmd/renderpage input (see README). */
27
+ export async function renderPage(envelope) {
28
+ const mod = await module_();
29
+ // node:wasi takes file descriptors, so stdin/stdout/stderr go through a private temp dir.
30
+ const dir = mkdtempSync(join(tmpdir(), 'sb-render-'));
31
+ const files = { in: join(dir, 'in'), out: join(dir, 'out'), err: join(dir, 'err') };
32
+ writeFileSync(files.in, JSON.stringify(envelope));
33
+ const fds = {
34
+ in: openSync(files.in, 'r'),
35
+ out: openSync(files.out, 'w'),
36
+ err: openSync(files.err, 'w'),
37
+ };
38
+ try {
39
+ const wasi = new WASI({
40
+ version: 'preview1',
41
+ args: ['renderpage'],
42
+ env: {},
43
+ stdin: fds.in,
44
+ stdout: fds.out,
45
+ stderr: fds.err,
46
+ returnOnExit: true,
47
+ });
48
+ const instance = await WebAssembly.instantiate(mod, wasi.getImportObject());
49
+ const code = wasi.start(instance);
50
+ for (const fd of Object.values(fds)) closeSync(fd);
51
+ if (code !== 0)
52
+ throw new RenderError(readFileSync(files.err, 'utf8').trim() || `renderpage exited ${code}`);
53
+ return JSON.parse(readFileSync(files.out, 'utf8'));
54
+ } finally {
55
+ for (const fd of Object.values(fds)) {
56
+ try {
57
+ closeSync(fd);
58
+ } catch {
59
+ // already closed
60
+ }
61
+ }
62
+ rmSync(dir, { recursive: true, force: true });
63
+ }
64
+ }
package/package.json CHANGED
@@ -1,6 +1,46 @@
1
1
  {
2
2
  "name": "@sbuilder/render",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Render a Store Builder page exactly as the storefront serves it — the platform's own Go renderer, as WebAssembly, for Node.js hosts such as Next.js.",
5
+ "keywords": [
6
+ "sbuilder",
7
+ "store-builder",
8
+ "render",
9
+ "webassembly",
10
+ "nextjs"
11
+ ],
12
+ "homepage": "https://github.com/vuluu2k/web_builder/tree/main/cli/render#readme",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/vuluu2k/web_builder.git",
16
+ "directory": "cli/render"
17
+ },
18
+ "license": "MIT",
19
+ "author": "Store Builder",
20
+ "type": "module",
21
+ "main": "index.js",
22
+ "types": "index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./index.d.ts",
26
+ "default": "./index.js"
27
+ }
28
+ },
29
+ "files": [
30
+ "index.js",
31
+ "index.d.ts",
32
+ "renderpage.wasm",
33
+ "README.md",
34
+ "LICENSE"
35
+ ],
36
+ "engines": {
37
+ "node": ">=20"
38
+ },
39
+ "scripts": {
40
+ "prepack": "node build.mjs",
41
+ "test": "node build.mjs && node --test test.mjs"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ }
46
+ }
Binary file