@jxsuite/compiler 6.1.0 → 7.0.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.
@@ -1,65 +1,30 @@
1
1
  /**
2
- * Compile-server.js — Hono server handler compilation
2
+ * Compile-server.js — the site worker
3
3
  *
4
- * Compiles `timing: "server"` entries from a Jx document into a deployable Hono server handler
5
- * file.
4
+ * Emits the one Hono worker a site deploys beside its static output: every `timing: "server"` entry
5
+ * the build collected as a `POST /_jx/server/<export>` route, the extension server mounts, and the
6
+ * adapter's fallthrough to static assets (compiler.md §6.3).
6
7
  */
7
8
 
8
9
  import { withBase } from "@jxsuite/schema/asset-paths";
9
- import { dereference } from "@apidevtools/json-schema-ref-parser";
10
- import { collectServerEntries } from "../shared.ts";
11
10
  import { localeNegotiationMiddleware } from "../site/locale-negotiation.ts";
12
11
  import type { ResolvedI18n } from "../site/i18n.ts";
13
- import type { JxDocumentValue, JxElement } from "@jxsuite/schema/types";
12
+ import type { JxDocumentValue } from "@jxsuite/schema/types";
14
13
 
15
14
  /**
16
- * Compile a Jx document to a Hono server handler file. The handler exposes each `timing: "server"`
17
- * entry as a POST endpoint under `/_jx/server/$export`. Returns null if no server entries are
18
- * found.
15
+ * The route prefix the site worker registers its server functions under, and a compiled page or
16
+ * component posts to: the deployment base (site-architecture.md §14.7) in front of `baseUrl`.
19
17
  *
20
- * @example
21
- * const server = await compileServer("./dashboard.json");
22
- * if (server) fs.writeFileSync("dist/server.js", server);
18
+ * One function builds it for both sides. The worker IS the origin, so a route left at the bare path
19
+ * never matches on a site served from a subfolder, and no pass rewrites a module after it is
20
+ * compiled, so the caller has to be given the same prefix when the module is.
23
21
  *
24
- * @param {string | JxElement} sourcePath - Path to .json file, URL, or raw document
25
- * @param {object} [opts]
26
- * @param {string} [opts.baseUrl] - Base path for server endpoints. Default is `'/_jx/server'`
27
- * @returns {Promise<string | null>} Hono server handler source string, or null
22
+ * @param {string} base - The deployment base, `""` for a site at an origin root
23
+ * @param {string} [baseUrl] - Route prefix before the base is applied, `"/_jx/server"` by default
24
+ * @returns {string}
28
25
  */
29
- export async function compileServer(
30
- sourcePath: string | JxElement,
31
- opts: {
32
- baseUrl?: string;
33
- } = {},
34
- ) {
35
- const { baseUrl = "/_jx/server" } = opts;
36
- /* SAFETY: the parameter is a Jx element document or the path of one, and `dereference` only
37
- replaces each `$ref` node with the value it points to, so the result is that same document
38
- with its references inlined. A file read from disk is not re-validated here. */
39
- const doc = (await dereference(sourcePath)) as JxElement;
40
- const entries = collectServerEntries(doc);
41
- if (entries.length === 0) {
42
- return null;
43
- }
44
-
45
- const imports = entries
46
- .map(({ exportName, src }) => `import { ${exportName} } from '${src}'`)
47
- .join("\n");
48
-
49
- const routes = entries.map(({ exportName }) => buildRoute(exportName, baseUrl)).join("\n");
50
-
51
- return `// Generated by @jxsuite/compiler — do not edit manually
52
- // Deploy as a Cloudflare Worker, Node server, or Bun process.
53
- // Requires: npm install hono
54
-
55
- import { Hono } from 'hono'
56
- ${imports}
57
-
58
- const app = new Hono()
59
- ${routes}
60
-
61
- export default app
62
- `;
26
+ export function serverRoutePrefix(base: string, baseUrl = "/_jx/server"): string {
27
+ return withBase(base, baseUrl);
63
28
  }
64
29
 
65
30
  /**
@@ -131,7 +96,7 @@ export function compileSiteServer(
131
96
  * everything behind it routes on the bare path. Each is internally consistent: production serves
132
97
  * exactly one URL space, while `jx dev` deliberately answers both.
133
98
  */
134
- const baseUrl = withBase(base, opts.baseUrl ?? "/_jx/server");
99
+ const baseUrl = serverRoutePrefix(base, opts.baseUrl);
135
100
  if (entries.length === 0 && mounts.length === 0 && !adapter) {
136
101
  return null;
137
102
  }
@@ -217,7 +182,7 @@ function buildRoute(exportName: string, baseUrl: string) {
217
182
  app.post('${baseUrl}/${exportName}', async (c) => {
218
183
  const args = await c.req.json().catch(() => ({}))
219
184
  try {
220
- return c.json(await ${exportName}(args, c.env))
185
+ return c.json((await ${exportName}(args, c.env)) ?? null)
221
186
  } catch (e) {
222
187
  return c.json({ ok: false, error: e?.message ?? 'Server error' }, 500)
223
188
  }