@orunelle/theme-runtime 0.0.0-stage → 2.0.0-alpha.14
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/dist/application.d.ts +7 -0
- package/dist/application.d.ts.map +1 -0
- package/dist/application.js +58 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +429 -0
- package/dist/commerce.d.ts +235 -0
- package/dist/commerce.d.ts.map +1 -0
- package/dist/commerce.js +316 -0
- package/dist/credentials.d.ts +11 -0
- package/dist/credentials.d.ts.map +1 -0
- package/dist/credentials.js +79 -0
- package/dist/fixtures.d.ts +23 -0
- package/dist/fixtures.d.ts.map +1 -0
- package/dist/fixtures.js +131 -0
- package/dist/host/package.json +1 -0
- package/dist/host/preview/editor.css +116 -0
- package/dist/host/preview/editor.tsx +193 -0
- package/dist/host/preview/src/bridge.tsx +371 -0
- package/dist/host/preview/src/client.tsx +15 -0
- package/dist/host/preview/src/env.d.ts +5 -0
- package/dist/host/preview/src/preview.css +40 -0
- package/dist/host/preview/src/server.tsx +95 -0
- package/dist/host/preview/src/surface.tsx +264 -0
- package/dist/host/serve-node.mjs +133 -0
- package/dist/host/storefront/public/fonts/9da48a48bf6500f8-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/e4af272ccee01ff0-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/e4efb0298547fad0-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/f022b1629605b85c-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts/fb66eb17bf011c3b-s.p.woff2 +0 -0
- package/dist/host/storefront/public/fonts.css +7 -0
- package/dist/host/storefront/public/samples/pouch-navy.svg +4 -0
- package/dist/host/storefront/public/samples/throw-oat.svg +2 -0
- package/dist/host/storefront/public/samples/throw-sage.svg +2 -0
- package/dist/host/storefront/public/samples/tote-charcoal.svg +5 -0
- package/dist/host/storefront/public/samples/tote-detail.svg +1 -0
- package/dist/host/storefront/public/samples/tote-sand.svg +5 -0
- package/dist/host/storefront/src/binding.server.ts +1 -0
- package/dist/host/storefront/src/data.ts +2 -0
- package/dist/host/storefront/src/env.d.ts +5 -0
- package/dist/host/storefront/src/queries.ts +1 -0
- package/dist/host/storefront/src/routeTree.gen.ts +155 -0
- package/dist/host/storefront/src/router.tsx +31 -0
- package/dist/host/storefront/src/routes/$locale.$.tsx +135 -0
- package/dist/host/storefront/src/routes/$locale.index.tsx +19 -0
- package/dist/host/storefront/src/routes/$locale.products.$handle.tsx +57 -0
- package/dist/host/storefront/src/routes/$locale.tsx +36 -0
- package/dist/host/storefront/src/routes/__root.tsx +103 -0
- package/dist/host/storefront/src/routes/index.tsx +6 -0
- package/dist/host/storefront/src/server.ts +1 -0
- package/dist/host.d.ts +45 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +380 -0
- package/dist/local-editor.d.ts +2 -0
- package/dist/local-editor.d.ts.map +1 -0
- package/dist/local-editor.js +111 -0
- package/dist/managed-worker.d.ts +2 -0
- package/dist/managed-worker.d.ts.map +1 -0
- package/dist/managed-worker.js +22 -0
- package/dist/server.d.ts +5 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +98 -0
- package/dist/source.d.ts +19 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +131 -0
- package/dist/tanstack/binding.server.d.ts +4 -0
- package/dist/tanstack/binding.server.d.ts.map +1 -0
- package/dist/tanstack/binding.server.js +9 -0
- package/dist/tanstack/data.d.ts +44 -0
- package/dist/tanstack/data.d.ts.map +1 -0
- package/dist/tanstack/data.js +320 -0
- package/dist/tanstack/index.d.ts +4 -0
- package/dist/tanstack/index.d.ts.map +1 -0
- package/dist/tanstack/index.js +2 -0
- package/dist/tanstack/queries.d.ts +6 -0
- package/dist/tanstack/queries.d.ts.map +1 -0
- package/dist/tanstack/queries.js +5 -0
- package/dist/template/app/client.tsx +3 -0
- package/dist/template/app/env.d.ts +1 -0
- package/dist/template/app/router.tsx +40 -0
- package/dist/template/app/routes/$locale.$.tsx +135 -0
- package/dist/template/app/routes/$locale.index.tsx +19 -0
- package/dist/template/app/routes/$locale.products.$handle.tsx +57 -0
- package/dist/template/app/routes/$locale.tsx +46 -0
- package/dist/template/app/routes/__root.tsx +103 -0
- package/dist/template/app/routes/index.tsx +6 -0
- package/dist/template/app/server.ts +1 -0
- package/dist/template/app/start.ts +2 -0
- package/dist/template/app/theme.ts +12 -0
- package/dist/template/tests/router-isolation.test.ts +14 -0
- package/dist/template/vite.config.ts +5 -0
- package/dist/template/vitest.config.ts +9 -0
- package/dist/vite.d.ts +7 -0
- package/dist/vite.d.ts.map +1 -0
- package/dist/vite.js +22 -0
- package/package.json +88 -3
- package/README.md +0 -3
package/dist/host.js
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { cp, lstat, mkdir, readFile, readdir, realpath, symlink, writeFile, } from "node:fs/promises";
|
|
2
|
+
import { resolve, relative, sep, dirname } from "node:path";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { build, createBuilder, createServer, } from "vite";
|
|
5
|
+
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
|
|
6
|
+
import react from "@vitejs/plugin-react";
|
|
7
|
+
import { orunelleTheme } from "./vite.js";
|
|
8
|
+
import { readSource, sha256, validateSource, } from "./source.js";
|
|
9
|
+
const distribution = fileURLToPath(new URL("./host/", import.meta.url));
|
|
10
|
+
const runtimeRoot = resolve(fileURLToPath(new URL("../", import.meta.url)));
|
|
11
|
+
const versions = {
|
|
12
|
+
policy: "orunelle-managed-compile-v1",
|
|
13
|
+
node: process.versions.node,
|
|
14
|
+
vite: "8.3.6",
|
|
15
|
+
start: "1.169.4",
|
|
16
|
+
react: "19.3.1",
|
|
17
|
+
runtimeAbi: "orunelle.react.v2/1",
|
|
18
|
+
};
|
|
19
|
+
async function inventory(root, trustedPackage = false) {
|
|
20
|
+
const out = {};
|
|
21
|
+
async function walk(directory) {
|
|
22
|
+
for (const name of (await readdir(directory)).sort()) {
|
|
23
|
+
const path = resolve(directory, name), info = await lstat(path);
|
|
24
|
+
if (info.isDirectory())
|
|
25
|
+
await walk(path);
|
|
26
|
+
else {
|
|
27
|
+
if (!info.isFile() ||
|
|
28
|
+
(!trustedPackage && info.nlink !== 1) ||
|
|
29
|
+
info.size > 16 * 1024 * 1024)
|
|
30
|
+
throw new Error("Invalid output file.");
|
|
31
|
+
out[relative(root, path).split(sep).join("/")] = sha256(await readFile(path));
|
|
32
|
+
if (Object.keys(out).length > 1024)
|
|
33
|
+
throw new Error("Build exceeds 1024 output files.");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
await walk(root);
|
|
38
|
+
return Object.fromEntries(Object.entries(out).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
39
|
+
}
|
|
40
|
+
export async function toolchainIdentity() {
|
|
41
|
+
const files = await inventory(distribution, true);
|
|
42
|
+
// Alpha package versions alone do not identify locally rebuilt framework
|
|
43
|
+
// code. Hash the actual installed exports too, with location-free keys so
|
|
44
|
+
// a packed external project and the operator get the same identity.
|
|
45
|
+
for (const name of ["contract", "sdk", "engine", "editor"]) {
|
|
46
|
+
const entry = fileURLToPath(import.meta.resolve(`@orunelle/theme-${name}`));
|
|
47
|
+
for (const [path, digest] of Object.entries(await inventory(dirname(entry), true)))
|
|
48
|
+
files[`packages/${name}/${path}`] = digest;
|
|
49
|
+
}
|
|
50
|
+
for (const name of [
|
|
51
|
+
"host.js",
|
|
52
|
+
"source.js",
|
|
53
|
+
"vite.js",
|
|
54
|
+
"application.js",
|
|
55
|
+
"server.js",
|
|
56
|
+
"commerce.js",
|
|
57
|
+
"fixtures.js",
|
|
58
|
+
"tanstack/index.js",
|
|
59
|
+
"tanstack/data.js",
|
|
60
|
+
"tanstack/queries.js",
|
|
61
|
+
"tanstack/binding.server.js",
|
|
62
|
+
])
|
|
63
|
+
files[`runtime/${name}`] = sha256(await readFile(new URL(name, import.meta.url)));
|
|
64
|
+
return {
|
|
65
|
+
...versions,
|
|
66
|
+
toolchainDigest: sha256(JSON.stringify({ versions, files })),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/** Author files are input modules, never config/plugins. Packages and builtins
|
|
70
|
+
* are resolved only from this fixed platform dependency policy. */
|
|
71
|
+
function authorImports(sourceRoot) {
|
|
72
|
+
let development = false;
|
|
73
|
+
const allowed = new Set([
|
|
74
|
+
"sanitize-html",
|
|
75
|
+
"react",
|
|
76
|
+
"react/jsx-runtime",
|
|
77
|
+
"react-dom",
|
|
78
|
+
"react-dom/client",
|
|
79
|
+
"@orunelle/theme-sdk",
|
|
80
|
+
"@orunelle/theme-sdk/commerce",
|
|
81
|
+
"@orunelle/theme-sdk/query",
|
|
82
|
+
"@orunelle/theme-engine",
|
|
83
|
+
"@orunelle/theme-engine/styles.css",
|
|
84
|
+
"@orunelle/theme-engine/sections.css",
|
|
85
|
+
"@orunelle/theme-contract",
|
|
86
|
+
"@tanstack/react-router",
|
|
87
|
+
"@tanstack/react-query",
|
|
88
|
+
"@tanstack/react-router-ssr-query",
|
|
89
|
+
"@tanstack/react-start",
|
|
90
|
+
"@tanstack/react-start/client",
|
|
91
|
+
"@tanstack/react-start/server-entry",
|
|
92
|
+
"@orunelle/theme-runtime/tanstack",
|
|
93
|
+
"@orunelle/theme-runtime/server",
|
|
94
|
+
]);
|
|
95
|
+
const resolvedSDK = new Set([...allowed].map((name) => fileURLToPath(import.meta.resolve(name))));
|
|
96
|
+
return {
|
|
97
|
+
name: "orunelle-author-import-policy",
|
|
98
|
+
enforce: "pre",
|
|
99
|
+
configResolved(config) {
|
|
100
|
+
development = config.command === "serve";
|
|
101
|
+
},
|
|
102
|
+
resolveId(id, importer, options) {
|
|
103
|
+
if (!importer?.split("?")[0]?.startsWith(sourceRoot + sep))
|
|
104
|
+
return;
|
|
105
|
+
if (id === "\0vite/preload-helper.js")
|
|
106
|
+
return;
|
|
107
|
+
const [file, query] = id.split("?");
|
|
108
|
+
const generatedQuery = !query ||
|
|
109
|
+
[...new URLSearchParams(query)].every(([key, value]) => (key === "tsr-shared" && value === "1") ||
|
|
110
|
+
(key === "tsr-split" && /^[a-zA-Z,]+$/.test(value)) ||
|
|
111
|
+
(key === "tss-serverfn-split" && value === ""));
|
|
112
|
+
if (file?.startsWith(sourceRoot + sep) &&
|
|
113
|
+
resolve(file) === file &&
|
|
114
|
+
!/[\\\u0000]/.test(file) &&
|
|
115
|
+
generatedQuery)
|
|
116
|
+
return { id };
|
|
117
|
+
// Vite's alias plugin runs before user resolution hooks. Accept only the
|
|
118
|
+
// exact pinned public exports when it has already resolved a package.
|
|
119
|
+
if (resolvedSDK.has(id.split("?")[0]) &&
|
|
120
|
+
(!id.includes("?") || id.endsWith(".css?url")))
|
|
121
|
+
return { id };
|
|
122
|
+
if (id.startsWith(".")) {
|
|
123
|
+
const [name, query] = id.split("?");
|
|
124
|
+
const target = resolve(dirname(importer.split("?")[0]), name);
|
|
125
|
+
if (target.startsWith(sourceRoot + sep) &&
|
|
126
|
+
!/[\\\u0000]/.test(id) &&
|
|
127
|
+
(!query || query === "url"))
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
else if (allowed.has(id) ||
|
|
131
|
+
(id.endsWith(".css?url") && allowed.has(id.slice(0, -4))))
|
|
132
|
+
return {
|
|
133
|
+
id: fileURLToPath(import.meta.resolve(id.endsWith("?url") ? id.slice(0, -4) : id)) + (id.endsWith("?url") ? "?url" : ""),
|
|
134
|
+
external: development &&
|
|
135
|
+
options.ssr === true &&
|
|
136
|
+
(id === "react" ||
|
|
137
|
+
id.startsWith("react/") ||
|
|
138
|
+
id === "react-dom" ||
|
|
139
|
+
id.startsWith("react-dom/") ||
|
|
140
|
+
id === "@tanstack/react-query" ||
|
|
141
|
+
id === "@tanstack/react-router" ||
|
|
142
|
+
id === "@tanstack/react-router-ssr-query"),
|
|
143
|
+
};
|
|
144
|
+
throw new Error(`Unsupported author import ${JSON.stringify(id)}. Use relative source files or a supported SDK/React import.`);
|
|
145
|
+
},
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
export async function prepare(project, source, work) {
|
|
149
|
+
const parsed = validateSource(source);
|
|
150
|
+
await mkdir(work, { recursive: false, mode: 0o700 });
|
|
151
|
+
await cp(distribution, resolve(work, "host"), { recursive: true });
|
|
152
|
+
await cp(resolve(work, "host/storefront/public"), resolve(work, "public"), {
|
|
153
|
+
recursive: true,
|
|
154
|
+
});
|
|
155
|
+
const sourceRoot = resolve(work, "source");
|
|
156
|
+
for (const [path, bytes] of parsed.files) {
|
|
157
|
+
await mkdir(dirname(resolve(sourceRoot, path)), { recursive: true });
|
|
158
|
+
await writeFile(resolve(sourceRoot, path), bytes, { flag: "wx" });
|
|
159
|
+
}
|
|
160
|
+
if (await lstat(resolve(sourceRoot, "public")).catch(() => null))
|
|
161
|
+
await cp(resolve(sourceRoot, "public"), resolve(work, "public"), {
|
|
162
|
+
recursive: true,
|
|
163
|
+
});
|
|
164
|
+
// Node resolution uses the package's pinned dependency tree, independent of
|
|
165
|
+
// any author package.json, Vite config or node_modules hooks.
|
|
166
|
+
await symlink((await lstat(resolve(runtimeRoot, "node_modules/zod")).catch(() => null)) !== null
|
|
167
|
+
? resolve(runtimeRoot, "node_modules")
|
|
168
|
+
: resolve(runtimeRoot, "../.."), resolve(work, "node_modules"), "dir");
|
|
169
|
+
const entry = resolve(work, "entry.tsx");
|
|
170
|
+
const themeEntry = parsed.files.has("app/theme.ts")
|
|
171
|
+
? "app/theme.ts"
|
|
172
|
+
: "app/theme.tsx";
|
|
173
|
+
await writeFile(entry, `import author from './source/${themeEntry}';\nimport manifest from './source/orunelle.theme.json';\nimport { defineTheme } from '@orunelle/theme-sdk';\n${parsed.defaults ? "import defaults from './source/defaults.json';" : "const defaults = undefined;"}\nexport default defineTheme({...author,manifest,defaults});\n`);
|
|
174
|
+
const serverEntry = resolve(work, "server.ts");
|
|
175
|
+
await writeFile(serverEntry, parsed.application && parsed.files.has("app/server.ts")
|
|
176
|
+
? `import handler from './source/app/server.ts';\nimport { createThemeServer } from '@orunelle/theme-runtime/server';\nexport default createThemeServer(handler);\n`
|
|
177
|
+
: `export {default} from '@orunelle/theme-runtime/server';\n`);
|
|
178
|
+
return {
|
|
179
|
+
...parsed,
|
|
180
|
+
sourceRoot,
|
|
181
|
+
entry,
|
|
182
|
+
work,
|
|
183
|
+
project,
|
|
184
|
+
themeEntry,
|
|
185
|
+
serverEntry,
|
|
186
|
+
applicationRoot: resolve(sourceRoot, "app"),
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
export function managedConfig(state, preview, output) {
|
|
190
|
+
const root = resolve(state.work, "host", preview ? "preview" : "storefront");
|
|
191
|
+
return {
|
|
192
|
+
configFile: false,
|
|
193
|
+
envDir: false,
|
|
194
|
+
root,
|
|
195
|
+
cacheDir: resolve(state.work, preview ? "cache-preview" : "cache-storefront"),
|
|
196
|
+
logLevel: "warn",
|
|
197
|
+
plugins: [
|
|
198
|
+
authorImports(state.sourceRoot),
|
|
199
|
+
orunelleTheme({ entry: state.entry }),
|
|
200
|
+
...(preview
|
|
201
|
+
? []
|
|
202
|
+
: [
|
|
203
|
+
tanstackStart({
|
|
204
|
+
...(state.application
|
|
205
|
+
? {
|
|
206
|
+
srcDirectory: relative(root, state.applicationRoot),
|
|
207
|
+
server: {
|
|
208
|
+
entry: relative(state.applicationRoot, state.serverEntry),
|
|
209
|
+
},
|
|
210
|
+
}
|
|
211
|
+
: {}),
|
|
212
|
+
serverFns: {
|
|
213
|
+
generateFunctionId({ filename, functionName }) {
|
|
214
|
+
const absolute = resolve(root, filename);
|
|
215
|
+
for (const [directory, label] of [
|
|
216
|
+
[state.applicationRoot, "theme/app"],
|
|
217
|
+
[state.sourceRoot, "theme"],
|
|
218
|
+
[runtimeRoot, "framework"],
|
|
219
|
+
[root, "host"],
|
|
220
|
+
[state.work, "build"],
|
|
221
|
+
])
|
|
222
|
+
if (absolute.startsWith(directory + sep))
|
|
223
|
+
return sha256(label +
|
|
224
|
+
"/" +
|
|
225
|
+
relative(directory, absolute).split(sep).join("/") +
|
|
226
|
+
"--" +
|
|
227
|
+
functionName);
|
|
228
|
+
return undefined;
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
}),
|
|
232
|
+
]),
|
|
233
|
+
{
|
|
234
|
+
name: "orunelle-stable-route-manifest",
|
|
235
|
+
// Start retains source filenames in its server route manifest. Replace
|
|
236
|
+
// only this platform-generated module before chunk hashing; never
|
|
237
|
+
// rewrite author strings or already-hashed output files.
|
|
238
|
+
transform(code, id) {
|
|
239
|
+
if (!id.includes("tanstack-start-manifest"))
|
|
240
|
+
return;
|
|
241
|
+
return code
|
|
242
|
+
.split(resolve(runtimeRoot, "dist").split(sep).join("/") + "/")
|
|
243
|
+
.join("/orunelle/framework/")
|
|
244
|
+
.split(state.applicationRoot.split(sep).join("/") + "/")
|
|
245
|
+
.join("/orunelle/theme/app/")
|
|
246
|
+
.split(root.split(sep).join("/") + "/")
|
|
247
|
+
.join("/orunelle/storefront/");
|
|
248
|
+
},
|
|
249
|
+
},
|
|
250
|
+
react(),
|
|
251
|
+
],
|
|
252
|
+
publicDir: resolve(state.work, "public"),
|
|
253
|
+
resolve: {
|
|
254
|
+
alias: [
|
|
255
|
+
{
|
|
256
|
+
find: "@orunelle/theme-runtime/tanstack",
|
|
257
|
+
replacement: resolve(runtimeRoot, "dist/tanstack/index.js"),
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
find: "@orunelle/theme-runtime/bindings",
|
|
261
|
+
replacement: resolve(runtimeRoot, "dist/tanstack/binding.server.js"),
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
find: /^@orunelle\/theme-runtime\/(.+)$/,
|
|
265
|
+
replacement: resolve(runtimeRoot, "dist/$1.js"),
|
|
266
|
+
},
|
|
267
|
+
],
|
|
268
|
+
dedupe: [
|
|
269
|
+
"react",
|
|
270
|
+
"react-dom",
|
|
271
|
+
"@tanstack/react-router",
|
|
272
|
+
"@tanstack/react-query",
|
|
273
|
+
],
|
|
274
|
+
},
|
|
275
|
+
server: {
|
|
276
|
+
host: "127.0.0.1",
|
|
277
|
+
strictPort: true,
|
|
278
|
+
allowedHosts: ["127.0.0.1", "localhost"],
|
|
279
|
+
fs: {
|
|
280
|
+
allow: [
|
|
281
|
+
state.work,
|
|
282
|
+
state.project,
|
|
283
|
+
runtimeRoot,
|
|
284
|
+
fileURLToPath(new URL("../..", import.meta.url)),
|
|
285
|
+
],
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
// Author imports and host imports must share one router/React context.
|
|
289
|
+
// Bundle the pinned framework graph instead of mixing an inlined author
|
|
290
|
+
// router with an external host router in the emitted server.
|
|
291
|
+
...(preview || output ? { ssr: { noExternal: true } } : {}),
|
|
292
|
+
...(preview ? { base: "./" } : {}),
|
|
293
|
+
build: {
|
|
294
|
+
minify: "oxc",
|
|
295
|
+
sourcemap: false,
|
|
296
|
+
...(output ? { outDir: output } : {}),
|
|
297
|
+
...(preview
|
|
298
|
+
? {
|
|
299
|
+
manifest: true,
|
|
300
|
+
rollupOptions: { input: resolve(root, "src/client.tsx") },
|
|
301
|
+
}
|
|
302
|
+
: {}),
|
|
303
|
+
},
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
export async function buildTheme(project, output, supplied) {
|
|
307
|
+
const source = supplied ?? (await readSource(project));
|
|
308
|
+
const parent = resolve(project, ".orunelle");
|
|
309
|
+
await mkdir(parent, { recursive: true, mode: 0o700 });
|
|
310
|
+
const state = await prepare(project, source, resolve(parent, "build-" + crypto.randomUUID()));
|
|
311
|
+
// Create output exclusively: never empty an author-owned or existing directory.
|
|
312
|
+
await mkdir(output, { recursive: false, mode: 0o700 });
|
|
313
|
+
const storefrontBuilder = await createBuilder(managedConfig(state, false, resolve(output, "storefront")), null);
|
|
314
|
+
await storefrontBuilder.buildApp();
|
|
315
|
+
await build(managedConfig(state, true, resolve(output, "preview/client")));
|
|
316
|
+
const serverConfig = managedConfig(state, true, resolve(output, "preview/server"));
|
|
317
|
+
delete serverConfig.build.rollupOptions;
|
|
318
|
+
serverConfig.build = {
|
|
319
|
+
...serverConfig.build,
|
|
320
|
+
ssr: resolve(state.work, "host/preview/src/server.tsx"),
|
|
321
|
+
};
|
|
322
|
+
await build(serverConfig);
|
|
323
|
+
await cp(resolve(distribution, "serve-node.mjs"), resolve(output, "serve-node.mjs"));
|
|
324
|
+
let hashes = await inventory(output);
|
|
325
|
+
const total = (await Promise.all(Object.keys(hashes).map((path) => lstat(resolve(output, path))))).reduce((sum, file) => sum + file.size, 0);
|
|
326
|
+
if (total > 64 * 1024 * 1024)
|
|
327
|
+
throw new Error("Build exceeds 64 MiB.");
|
|
328
|
+
const browserManifest = JSON.parse(await readFile(resolve(output, "preview/client/.vite/manifest.json"), "utf8"));
|
|
329
|
+
const entry = Object.values(browserManifest).find((item) => item.isEntry);
|
|
330
|
+
if (!entry)
|
|
331
|
+
throw new Error("Missing preview browser entry.");
|
|
332
|
+
const toolchain = await toolchainIdentity();
|
|
333
|
+
const descriptor = {
|
|
334
|
+
schemaVersion: 2,
|
|
335
|
+
runtimeAbi: versions.runtimeAbi,
|
|
336
|
+
manifest: state.manifest,
|
|
337
|
+
defaults: state.defaults,
|
|
338
|
+
sourceDigest: state.sourceDigest,
|
|
339
|
+
toolchainDigest: toolchain.toolchainDigest,
|
|
340
|
+
entry: entry.file,
|
|
341
|
+
styles: entry.css ?? [],
|
|
342
|
+
assets: Object.keys(hashes)
|
|
343
|
+
.filter((path) => path.startsWith("preview/client/"))
|
|
344
|
+
.map((path) => path.slice(15)),
|
|
345
|
+
};
|
|
346
|
+
// Put the descriptor inside the hashed inventory so defaults, entry points
|
|
347
|
+
// and asset allowlists cannot change while keeping an artifact digest.
|
|
348
|
+
await writeFile(resolve(output, "descriptor.json"), JSON.stringify(descriptor) + "\n", { flag: "wx" });
|
|
349
|
+
hashes = await inventory(output);
|
|
350
|
+
const metadata = {
|
|
351
|
+
...descriptor,
|
|
352
|
+
artifactDigest: sha256(JSON.stringify(hashes)),
|
|
353
|
+
sha256: hashes,
|
|
354
|
+
};
|
|
355
|
+
await writeFile(resolve(output, "artifact.json"), JSON.stringify(metadata, null, 2) + "\n", { flag: "wx" });
|
|
356
|
+
return metadata;
|
|
357
|
+
}
|
|
358
|
+
export async function devTheme(project, port) {
|
|
359
|
+
delete process.env.ORUNELLE_STOREFRONT_BINDINGS;
|
|
360
|
+
delete process.env.ORUNELLE_PREVIEW_TOKEN;
|
|
361
|
+
const source = await readSource(project), parent = resolve(project, ".orunelle");
|
|
362
|
+
await mkdir(parent, { recursive: true, mode: 0o700 });
|
|
363
|
+
const state = await prepare(project, source, resolve(parent, "dev-" + crypto.randomUUID()));
|
|
364
|
+
await useLocalAuthorSource(state);
|
|
365
|
+
const options = managedConfig(state, false);
|
|
366
|
+
options.server = { ...options.server, port };
|
|
367
|
+
const server = await createServer(options);
|
|
368
|
+
await server.listen();
|
|
369
|
+
return { server, origin: `http://127.0.0.1:${port}` };
|
|
370
|
+
}
|
|
371
|
+
export async function useLocalAuthorSource(state) {
|
|
372
|
+
// Local author development deliberately imports the author's own source so
|
|
373
|
+
// React HMR sees edits. Remote managed builds only use the validated copy.
|
|
374
|
+
await writeFile(state.entry, `import author from ${JSON.stringify(resolve(state.project, state.themeEntry))};\nimport manifest from ${JSON.stringify(resolve(state.project, "orunelle.theme.json"))};\nimport {defineTheme} from '@orunelle/theme-sdk';\n${state.defaults ? `import defaults from ${JSON.stringify(resolve(state.project, "defaults.json"))};` : "const defaults=undefined;"}\nexport default defineTheme({...author,manifest,defaults});`);
|
|
375
|
+
if (state.application) {
|
|
376
|
+
state.applicationRoot = resolve(state.project, "app");
|
|
377
|
+
if (state.files.has("app/server.ts"))
|
|
378
|
+
await writeFile(state.serverEntry, `import handler from ${JSON.stringify(resolve(state.project, "app/server.ts"))};\nimport {createThemeServer} from '@orunelle/theme-runtime/server';\nexport default createThemeServer(handler);\n`);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-editor.d.ts","sourceRoot":"","sources":["../src/local-editor.ts"],"names":[],"mappings":"AAWA,wBAAsB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,mBAqH5D"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { mkdir, writeFile, readFile } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { randomBytes, randomUUID } from "node:crypto";
|
|
4
|
+
import { createServer } from "vite";
|
|
5
|
+
import { prepare, managedConfig, useLocalAuthorSource } from "./host.js";
|
|
6
|
+
import { readSource } from "./source.js";
|
|
7
|
+
import { fixtureProducts, resolveFixtureContext } from "./fixtures.js";
|
|
8
|
+
import { loadCredential } from "./credentials.js";
|
|
9
|
+
export async function devEditor(project, port) {
|
|
10
|
+
if (port > 65535)
|
|
11
|
+
throw new Error("Editor port exceeds 65535.");
|
|
12
|
+
const parent = resolve(project, ".orunelle");
|
|
13
|
+
await mkdir(parent, { recursive: true, mode: 0o700 });
|
|
14
|
+
const state = await prepare(project, await readSource(project), resolve(parent, "editor-" + randomUUID()));
|
|
15
|
+
await useLocalAuthorSource(state);
|
|
16
|
+
const origin = `http://127.0.0.1:${port}`, installationId = randomUUID();
|
|
17
|
+
const context = resolveFixtureContext(new Request(origin));
|
|
18
|
+
let products = [...fixtureProducts(context)];
|
|
19
|
+
let resources = [];
|
|
20
|
+
let store = context.store;
|
|
21
|
+
// A linked-store credential stays in this Node process. The generated editor
|
|
22
|
+
// and author iframe receive only the public, settings-authorized sample.
|
|
23
|
+
const linkPath = resolve(project, ".orunelle/link.json");
|
|
24
|
+
const linkText = await readFile(linkPath, "utf8").catch((e) => {
|
|
25
|
+
if (e.code !== "ENOENT")
|
|
26
|
+
throw e;
|
|
27
|
+
return null;
|
|
28
|
+
});
|
|
29
|
+
if (linkText) {
|
|
30
|
+
const link = JSON.parse(linkText), credential = await loadCredential(link.apiOrigin);
|
|
31
|
+
if (credential.developerId !== link.developerId)
|
|
32
|
+
throw new Error("Linked store belongs to another developer.");
|
|
33
|
+
const response = await fetch(`${credential.apiOrigin}/api/theme-author/stores/${encodeURIComponent(link.store.id)}/resources`, {
|
|
34
|
+
redirect: "error",
|
|
35
|
+
signal: AbortSignal.timeout(15000),
|
|
36
|
+
headers: { Authorization: `Bearer ${credential.token}` },
|
|
37
|
+
});
|
|
38
|
+
if (!response.ok)
|
|
39
|
+
throw new Error("Linked preview access is unavailable. Relink the store or sign in again.");
|
|
40
|
+
const data = (await response.json());
|
|
41
|
+
products = data.data.products;
|
|
42
|
+
resources = data.data.resources;
|
|
43
|
+
store = {
|
|
44
|
+
id: link.store.id,
|
|
45
|
+
slug: link.store.slug,
|
|
46
|
+
name: { en: link.store.name, ar: link.store.name },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const bootstrap = {
|
|
50
|
+
identity: {
|
|
51
|
+
protocol: 1,
|
|
52
|
+
sessionId: randomUUID(),
|
|
53
|
+
nonce: randomBytes(32).toString("base64url"),
|
|
54
|
+
storeId: linkText ? store.id : randomUUID(),
|
|
55
|
+
installationId,
|
|
56
|
+
releaseId: randomUUID(),
|
|
57
|
+
artifactDigest: state.sourceDigest,
|
|
58
|
+
expiresAt: Date.now() + 5 * 60 * 1000,
|
|
59
|
+
editorOrigin: origin,
|
|
60
|
+
},
|
|
61
|
+
manifest: state.manifest,
|
|
62
|
+
configuration: state.defaults
|
|
63
|
+
? { ...state.defaults, installationId }
|
|
64
|
+
: {
|
|
65
|
+
schemaVersion: 2,
|
|
66
|
+
configurationVersion: 1,
|
|
67
|
+
themeId: state.manifest.id,
|
|
68
|
+
installationId,
|
|
69
|
+
settings: {},
|
|
70
|
+
groups: {},
|
|
71
|
+
templates: {},
|
|
72
|
+
nodes: [],
|
|
73
|
+
assignments: [],
|
|
74
|
+
},
|
|
75
|
+
storefront: {
|
|
76
|
+
...context,
|
|
77
|
+
store,
|
|
78
|
+
origin,
|
|
79
|
+
mode: "preview",
|
|
80
|
+
commerce: "fixture",
|
|
81
|
+
currency: "EGP",
|
|
82
|
+
market: "EG",
|
|
83
|
+
},
|
|
84
|
+
products,
|
|
85
|
+
resources,
|
|
86
|
+
media: [],
|
|
87
|
+
assetBase: "/",
|
|
88
|
+
};
|
|
89
|
+
const root = resolve(state.work, "host/preview");
|
|
90
|
+
await writeFile(resolve(root, "bootstrap.json"), JSON.stringify(bootstrap));
|
|
91
|
+
await writeFile(resolve(root, "frame.tsx"), `import {createRoot} from 'react-dom/client';import {makePreviewRouter,PreviewDocument} from './src/surface';import bootstrap from './bootstrap.json';import '@orunelle/theme-engine/styles.css';import './src/preview.css';const router=makePreviewRouter(bootstrap as any);await router.load();createRoot(document.getElementById('root')!).render(<PreviewDocument router={router} bootstrap={bootstrap as any}/>);`);
|
|
92
|
+
const options = managedConfig(state, true);
|
|
93
|
+
options.server = { ...options.server, port, cors: { origin: "*" } };
|
|
94
|
+
options.plugins.push({
|
|
95
|
+
name: "orunelle-local-editor",
|
|
96
|
+
configureServer(server) {
|
|
97
|
+
server.middlewares.use(async (req, res, next) => {
|
|
98
|
+
if (req.url !== "/" && req.url !== "/frame")
|
|
99
|
+
return next();
|
|
100
|
+
const frame = req.url === "/frame";
|
|
101
|
+
const html = `<!doctype html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Local theme editor</title></head><body><div id="root"></div><script type="module" src="/${frame ? "frame.tsx" : "editor.tsx"}"></script></body></html>`;
|
|
102
|
+
res.setHeader("Content-Type", "text/html");
|
|
103
|
+
res.setHeader("Cache-Control", "no-store");
|
|
104
|
+
res.end(await server.transformIndexHtml(req.url, html));
|
|
105
|
+
});
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
const server = await createServer(options);
|
|
109
|
+
await server.listen();
|
|
110
|
+
return origin;
|
|
111
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"managed-worker.d.ts","sourceRoot":"","sources":["../src/managed-worker.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Fixed entry for a separately operated compiler process. No author config,
|
|
2
|
+
// package manager, lifecycle script or author server module is executed here.
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { buildTheme, toolchainIdentity } from "./host.js";
|
|
6
|
+
import { validateSource } from "./source.js";
|
|
7
|
+
const [sourcePath, output, expectedToolchain] = process.argv.slice(2);
|
|
8
|
+
try {
|
|
9
|
+
if (!sourcePath || !output || !expectedToolchain)
|
|
10
|
+
throw new Error("Expected source, output and toolchain digest.");
|
|
11
|
+
if ((await toolchainIdentity()).toolchainDigest !== expectedToolchain)
|
|
12
|
+
throw new Error("Toolchain identity changed; run doctor and pin a new candidate.");
|
|
13
|
+
const source = JSON.parse(await readFile(sourcePath, "utf8"));
|
|
14
|
+
validateSource(source);
|
|
15
|
+
await buildTheme(resolve(sourcePath, ".."), resolve(output), source);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
console.error(error instanceof Error
|
|
19
|
+
? error.message.slice(0, 2000)
|
|
20
|
+
: "Managed compilation failed.");
|
|
21
|
+
process.exitCode = 1;
|
|
22
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import defaultHandler from "@tanstack/react-start/server-entry";
|
|
2
|
+
export declare function createThemeServer(handler: typeof defaultHandler): import("@tanstack/react-start/server-entry").ServerEntry;
|
|
3
|
+
declare const _default: import("@tanstack/react-start/server-entry").ServerEntry;
|
|
4
|
+
export default _default;
|
|
5
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,cAEN,MAAM,oCAAoC,CAAC;AAa5C,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,cAAc,4DAsG/D;;AACD,wBAAiD"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import defaultHandler, { createServerEntry, } from "@tanstack/react-start/server-entry";
|
|
2
|
+
import { timingSafeEqual } from "node:crypto";
|
|
3
|
+
import { validateConfiguration, validateManifest, } from "@orunelle/theme-contract";
|
|
4
|
+
import { withBinding } from "./tanstack/binding.server.js";
|
|
5
|
+
import { brandBinding, proxyCommerce, resolveCommerceBinding, } from "./commerce.js";
|
|
6
|
+
export function createThemeServer(handler) {
|
|
7
|
+
return createServerEntry({
|
|
8
|
+
async fetch(request, options) {
|
|
9
|
+
if (process.env.ORUNELLE_PUBLIC_ARTIFACT_DIGEST) {
|
|
10
|
+
const key = process.env.ORUNELLE_PUBLIC_RENDER_KEY ?? "";
|
|
11
|
+
const supplied = request.headers.get("x-orunelle-render-key") ?? "";
|
|
12
|
+
if (key.length < 32 ||
|
|
13
|
+
key.length !== supplied.length ||
|
|
14
|
+
!timingSafeEqual(Buffer.from(key), Buffer.from(supplied)) ||
|
|
15
|
+
request.method !== "POST" ||
|
|
16
|
+
new URL(request.url).pathname !== "/__theme-storefront/render")
|
|
17
|
+
return new Response("Not found", { status: 404 });
|
|
18
|
+
const text = await request.text();
|
|
19
|
+
if (text.length > 1048576)
|
|
20
|
+
return new Response("Payload too large", { status: 413 });
|
|
21
|
+
let payload;
|
|
22
|
+
try {
|
|
23
|
+
payload = JSON.parse(text);
|
|
24
|
+
const url = new URL(payload.url);
|
|
25
|
+
if (url.origin !== payload.binding.origin ||
|
|
26
|
+
url.username ||
|
|
27
|
+
url.password ||
|
|
28
|
+
payload.artifactDigest !==
|
|
29
|
+
process.env.ORUNELLE_PUBLIC_ARTIFACT_DIGEST ||
|
|
30
|
+
validateManifest(payload.manifest) ||
|
|
31
|
+
validateConfiguration(payload.manifest, payload.configuration))
|
|
32
|
+
throw new Error("Invalid published configuration");
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return new Response("Invalid render request", { status: 400 });
|
|
36
|
+
}
|
|
37
|
+
// The render's data capability travels with the server-side binding so
|
|
38
|
+
// data requests can carry it explicitly (needed where fetch cannot be
|
|
39
|
+
// intercepted per request, such as the isolate pool).
|
|
40
|
+
const renderBinding = brandBinding(typeof payload.brokerToken === "string"
|
|
41
|
+
? { ...payload.binding, brokerToken: payload.brokerToken }
|
|
42
|
+
: payload.binding, payload.brand);
|
|
43
|
+
const anonymous = new Request(payload.url, {
|
|
44
|
+
signal: request.signal,
|
|
45
|
+
headers: {
|
|
46
|
+
accept: "text/html, application/json",
|
|
47
|
+
"x-tsr-serverfn": "true",
|
|
48
|
+
"sec-fetch-site": "same-origin",
|
|
49
|
+
origin: new URL(payload.url).origin,
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
return withBinding({ ORUNELLE_STOREFRONT_BINDINGS: JSON.stringify([renderBinding]) }, async () => {
|
|
53
|
+
if (!resolveCommerceBinding(anonymous, {
|
|
54
|
+
ORUNELLE_STOREFRONT_BINDINGS: JSON.stringify([renderBinding]),
|
|
55
|
+
}))
|
|
56
|
+
return new Response("Invalid binding", { status: 400 });
|
|
57
|
+
const response = await handler.fetch(anonymous, options);
|
|
58
|
+
const headers = new Headers(response.headers);
|
|
59
|
+
headers.delete("set-cookie");
|
|
60
|
+
headers.set("x-orunelle-artifact-digest", payload.artifactDigest);
|
|
61
|
+
headers.set("cache-control", "private, no-store");
|
|
62
|
+
return new Response(response.body, {
|
|
63
|
+
status: response.status,
|
|
64
|
+
headers,
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
const context = resolveCommerceBinding(request, process.env);
|
|
69
|
+
if (!context)
|
|
70
|
+
return new Response("Storefront not found", {
|
|
71
|
+
status: 404,
|
|
72
|
+
headers: { "cache-control": "private, no-store" },
|
|
73
|
+
});
|
|
74
|
+
const path = new URL(request.url).pathname;
|
|
75
|
+
if (path === "/__commerce" || path.startsWith("/__commerce/"))
|
|
76
|
+
return proxyCommerce(request, process.env);
|
|
77
|
+
if (path === "/robots.txt")
|
|
78
|
+
return new Response("User-agent: *\nDisallow: /\n", {
|
|
79
|
+
headers: {
|
|
80
|
+
"content-type": "text/plain",
|
|
81
|
+
"cache-control": "private, no-store",
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
const response = await handler.fetch(request, options);
|
|
85
|
+
const headers = new Headers(response.headers);
|
|
86
|
+
headers.set("cache-control", "private, no-store");
|
|
87
|
+
headers.set("x-robots-tag", "noindex, nofollow");
|
|
88
|
+
headers.set("x-content-type-options", "nosniff");
|
|
89
|
+
headers.set("referrer-policy", "no-referrer");
|
|
90
|
+
return new Response(response.body, {
|
|
91
|
+
status: response.status,
|
|
92
|
+
statusText: response.statusText,
|
|
93
|
+
headers,
|
|
94
|
+
});
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
export default createThemeServer(defaultHandler);
|
package/dist/source.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ThemeSourceFile {
|
|
2
|
+
path: string;
|
|
3
|
+
content: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ThemeSource {
|
|
6
|
+
schemaVersion: 2;
|
|
7
|
+
files: ThemeSourceFile[];
|
|
8
|
+
}
|
|
9
|
+
export declare const sha256: (bytes: string | Uint8Array) => string;
|
|
10
|
+
export declare function validateSource(source: ThemeSource): {
|
|
11
|
+
manifest: import("@orunelle/theme-contract").MerchantManifest;
|
|
12
|
+
defaults: any;
|
|
13
|
+
sourceDigest: string;
|
|
14
|
+
files: Map<string, Buffer<ArrayBufferLike>>;
|
|
15
|
+
application: boolean;
|
|
16
|
+
};
|
|
17
|
+
/** Never follows a source symlink or reads credentials/package lifecycle hooks. */
|
|
18
|
+
export declare function readSource(directory: string): Promise<ThemeSource>;
|
|
19
|
+
//# sourceMappingURL=source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"source.d.ts","sourceRoot":"","sources":["../src/source.ts"],"names":[],"mappings":"AASA,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;CACjB;AACD,MAAM,WAAW,WAAW;IAC1B,aAAa,EAAE,CAAC,CAAC;IACjB,KAAK,EAAE,eAAe,EAAE,CAAC;CAC1B;AACD,eAAO,MAAM,MAAM,GAAI,OAAO,MAAM,GAAG,UAAU,WACC,CAAC;AACnD,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW;;;;;;EA2EjD;AAED,mFAAmF;AACnF,wBAAsB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CA0ExE"}
|