@fluixi/start 0.1.0-alpha.72 → 0.1.0-alpha.74

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 (64) hide show
  1. package/dist/adapter.cjs +748 -0
  2. package/dist/adapter.mjs +717 -0
  3. package/dist/adapters/entry.cjs +122 -0
  4. package/dist/adapters/entry.mjs +95 -0
  5. package/dist/adapters/platforms.cjs +311 -0
  6. package/dist/adapters/platforms.mjs +284 -0
  7. package/dist/api-RH6E5UTH.mjs +114 -0
  8. package/dist/api.cjs +145 -0
  9. package/dist/api.mjs +116 -0
  10. package/dist/commands/api-23MJFSYL.mjs +17 -0
  11. package/dist/commands/api-4IYNQRKG.mjs +16 -0
  12. package/dist/commands/api-RH6E5UTH.mjs +114 -0
  13. package/dist/commands/build.cjs +870 -0
  14. package/dist/commands/build.mjs +710 -0
  15. package/dist/commands/chunk-NS5GR2GX.mjs +115 -0
  16. package/dist/commands/chunk-OCPCK4ZJ.mjs +117 -0
  17. package/dist/commands/dev.cjs +663 -0
  18. package/dist/commands/dev.mjs +505 -0
  19. package/dist/commands/index.cjs +1138 -0
  20. package/dist/commands/index.mjs +975 -0
  21. package/dist/commands/prerender.cjs +402 -0
  22. package/dist/commands/prerender.mjs +375 -0
  23. package/dist/commands/start.cjs +461 -0
  24. package/dist/commands/start.mjs +426 -0
  25. package/dist/config.cjs +144 -0
  26. package/dist/config.mjs +108 -0
  27. package/dist/di.cjs +200 -0
  28. package/dist/di.mjs +169 -0
  29. package/dist/document.cjs +115 -0
  30. package/dist/document.mjs +86 -0
  31. package/dist/generated-api.cjs +43 -0
  32. package/dist/generated-api.mjs +18 -0
  33. package/dist/generated-server-fns.cjs +43 -0
  34. package/dist/generated-server-fns.mjs +18 -0
  35. package/dist/handler-core.cjs +211 -0
  36. package/dist/handler-core.mjs +185 -0
  37. package/dist/handler.cjs +181 -0
  38. package/dist/handler.mjs +150 -0
  39. package/dist/head.cjs +25 -0
  40. package/dist/head.mjs +4 -0
  41. package/dist/image.cjs +146 -0
  42. package/dist/image.mjs +120 -0
  43. package/dist/index.cjs +1036 -0
  44. package/dist/index.mjs +968 -0
  45. package/dist/interceptors.cjs +71 -0
  46. package/dist/interceptors.mjs +42 -0
  47. package/dist/internal.cjs +388 -0
  48. package/dist/internal.d.ts.map +1 -1
  49. package/dist/internal.js +6 -2
  50. package/dist/internal.mjs +218 -0
  51. package/dist/middleware.cjs +58 -0
  52. package/dist/middleware.mjs +31 -0
  53. package/dist/preload.cjs +42 -0
  54. package/dist/preload.mjs +18 -0
  55. package/dist/router.cjs +25 -0
  56. package/dist/router.mjs +4 -0
  57. package/dist/server-fn-setup.cjs +46 -0
  58. package/dist/server-fn-setup.mjs +22 -0
  59. package/dist/server-fn.cjs +109 -0
  60. package/dist/server-fn.mjs +79 -0
  61. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  62. package/dist/ui.cjs +123 -0
  63. package/dist/ui.mjs +93 -0
  64. package/package.json +45 -25
@@ -0,0 +1,218 @@
1
+ // src/internal.ts
2
+ import { readdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
3
+ import { resolve as resolvePath, relative as relativePath } from "node:path";
4
+
5
+ // src/document.ts
6
+ import { extractHeadMarker } from "@fluixi/head";
7
+ function injectApp(template, appHtml, mountId = "root") {
8
+ const mount = new RegExp(`<div id=["']${mountId}["']\\s*>\\s*</div>`);
9
+ if (mount.test(template)) {
10
+ return template.replace(mount, `<div id="${mountId}">${appHtml}</div>`);
11
+ }
12
+ if (template.includes("<!--ssr-outlet-->")) {
13
+ return template.replace("<!--ssr-outlet-->", appHtml);
14
+ }
15
+ return template.replace("</body>", `<div id="${mountId}">${appHtml}</div></body>`);
16
+ }
17
+ function splitTemplate(template, mountId = "root") {
18
+ const mount = new RegExp(`<div id=["']${mountId}["']\\s*>\\s*</div>`);
19
+ const m = template.match(mount);
20
+ if (m && m.index !== void 0) {
21
+ return {
22
+ head: template.slice(0, m.index) + `<div id="${mountId}">`,
23
+ tail: `</div>` + template.slice(m.index + m[0].length)
24
+ };
25
+ }
26
+ const outlet = template.indexOf("<!--ssr-outlet-->");
27
+ if (outlet !== -1) {
28
+ return {
29
+ head: template.slice(0, outlet),
30
+ tail: template.slice(outlet + "<!--ssr-outlet-->".length)
31
+ };
32
+ }
33
+ const body = template.indexOf("</body>");
34
+ if (body !== -1) {
35
+ return {
36
+ head: template.slice(0, body) + `<div id="${mountId}">`,
37
+ tail: `</div>` + template.slice(body)
38
+ };
39
+ }
40
+ return { head: template, tail: "" };
41
+ }
42
+ function setHtmlAttr(html, name, value) {
43
+ const existing = new RegExp(`(<html\\b[^>]*?)\\s${name}="[^"]*"`, "i");
44
+ if (existing.test(html)) return html.replace(existing, `$1 ${name}="${value}"`);
45
+ return html.replace(/<html\b/i, `<html ${name}="${value}"`);
46
+ }
47
+ function injectAppAndHead(template, rendered, mountId = "root") {
48
+ const { head, body } = extractHeadMarker(rendered);
49
+ let html = injectApp(template, body, mountId);
50
+ if (head) {
51
+ if (head.headHtml) {
52
+ if (/<title[\s>]/i.test(head.headHtml)) html = html.replace(/<title>[\s\S]*?<\/title>/i, "");
53
+ html = html.replace("</head>", `${head.headHtml}</head>`);
54
+ }
55
+ for (const [k, v] of Object.entries(head.htmlAttrs)) html = setHtmlAttr(html, k, v);
56
+ }
57
+ return html;
58
+ }
59
+ function guardHandler(handler, onError) {
60
+ return async (request) => {
61
+ try {
62
+ return await handler(request);
63
+ } catch (e) {
64
+ onError?.(e);
65
+ return new Response(String(e?.stack || e), {
66
+ status: 500,
67
+ headers: { "content-type": "text/plain; charset=utf-8" }
68
+ });
69
+ }
70
+ };
71
+ }
72
+
73
+ // src/internal.ts
74
+ var SERVER_FNS_VMOD = "virtual:fluixi-server-fns";
75
+ var I18N_VMOD = "virtual:fluixi-i18n";
76
+ function i18nVitePlugin(opts) {
77
+ let root = process.cwd();
78
+ const id = "\0" + I18N_VMOD;
79
+ return {
80
+ name: "fluixi-i18n",
81
+ configResolved(config) {
82
+ root = config.root || root;
83
+ },
84
+ resolveId(source) {
85
+ return source === I18N_VMOD ? id : null;
86
+ },
87
+ load(source) {
88
+ if (source !== id) return null;
89
+ const dir = resolvePath(root, opts.dir);
90
+ let files = [];
91
+ try {
92
+ files = readdirSync(dir).filter((f) => f.endsWith(".json"));
93
+ } catch {
94
+ }
95
+ const discovered = files.map((f) => f.replace(/\.json$/, "")).sort();
96
+ const locales = opts.locales?.length ? opts.locales : discovered;
97
+ const defaultLocale = opts.defaultLocale ?? locales[0] ?? "en";
98
+ const imports = locales.map((l, i) => `import _${i} from ${JSON.stringify(resolvePath(dir, l + ".json"))};`).join("\n");
99
+ const messages = `export const messages = {${locales.map((l, i) => `${JSON.stringify(l)}:_${i}`).join(",")}};`;
100
+ return `${imports}
101
+ export const locales = ${JSON.stringify(locales)};
102
+ export const defaultLocale = ${JSON.stringify(defaultLocale)};
103
+ ${messages}
104
+ `;
105
+ }
106
+ };
107
+ }
108
+ async function loadServerEnv(root, mode) {
109
+ const { loadEnv } = await import("vite");
110
+ const env = loadEnv(mode, root, "");
111
+ for (const [k, v] of Object.entries(env)) {
112
+ if (process.env[k] === void 0) process.env[k] = v;
113
+ }
114
+ }
115
+ function i18nTypeBlock(root, i18n) {
116
+ const dir = resolvePath(root, i18n.dir);
117
+ let discovered = [];
118
+ try {
119
+ discovered = readdirSync(dir).filter((f) => f.endsWith(".json")).map((f) => f.replace(/\.json$/, "")).sort();
120
+ } catch {
121
+ }
122
+ const locales = i18n.locales?.length ? i18n.locales : discovered;
123
+ const defaultLocale = i18n.defaultLocale ?? locales[0] ?? "en";
124
+ const typed = locales.length > 0 && locales.every((l) => discovered.includes(l));
125
+ if (!typed) {
126
+ return [
127
+ `declare module 'virtual:fluixi-i18n' {`,
128
+ ` export const messages: Record<string, Record<string, unknown>>;`,
129
+ ` export const locales: string[];`,
130
+ ` export const defaultLocale: string;`,
131
+ `}`
132
+ ];
133
+ }
134
+ const srcDir = resolvePath(root, "src");
135
+ const toSpec = (l) => {
136
+ const rel = relativePath(srcDir, resolvePath(dir, l + ".json")).split(/[\\/]/).join("/");
137
+ return rel.startsWith(".") ? rel : "./" + rel;
138
+ };
139
+ const entries = locales.map((l) => ` ${JSON.stringify(l)}: (typeof import(${JSON.stringify(toSpec(l))}))['default'];`);
140
+ const tuple = locales.map((l) => JSON.stringify(l)).join(", ");
141
+ return [
142
+ `declare module 'virtual:fluixi-i18n' {`,
143
+ ` export const messages: {`,
144
+ ...entries,
145
+ ` };`,
146
+ ` export const locales: readonly [${tuple}];`,
147
+ ` export const defaultLocale: ${JSON.stringify(defaultLocale)};`,
148
+ `}`
149
+ ];
150
+ }
151
+ function startTypesPlugin(cfg) {
152
+ let root = process.cwd();
153
+ return {
154
+ name: "fluixi-start-types",
155
+ configResolved(config) {
156
+ root = config.root || root;
157
+ },
158
+ buildStart() {
159
+ const blocks = [];
160
+ if (cfg.i18n) blocks.push(...i18nTypeBlock(root, cfg.i18n));
161
+ if (blocks.length === 0) return;
162
+ const content = `// Auto-generated by @fluixi/start — do not edit.
163
+ ${blocks.join("\n")}
164
+ `;
165
+ const file = resolvePath(root, "src/fluixi-start-env.d.ts");
166
+ try {
167
+ if (!existsSync(file) || readFileSync(file, "utf8") !== content) writeFileSync(file, content);
168
+ } catch {
169
+ }
170
+ }
171
+ };
172
+ }
173
+ async function fluixiPlugins(cfg) {
174
+ const { fluixi, fluixiRoutesPlugin } = await import("@fluixi/core/plugins");
175
+ const { serverFunctionsVitePlugin } = await import("@fluixi/compiler/integrations");
176
+ const plugins = [
177
+ serverFunctionsVitePlugin(),
178
+ fluixiRoutesPlugin({ routesDir: cfg.routesDir, polyfills: false }),
179
+ // The app's component-resolution rules reach the compiler here; without this an SSR
180
+ // app has no way to say where an unimported tag comes from.
181
+ //
182
+ // `fluixi()` and not `createVitePlugin()`: the latter is the bare compiler integration,
183
+ // and the reactive graph — agent, source marks, /__fluixi/graph — is composed on top of
184
+ // it by the former. A start app got no graph at all until this changed.
185
+ fluixi({ resolve: cfg.resolve })
186
+ ];
187
+ if (cfg.i18n) plugins.push(i18nVitePlugin(cfg.i18n));
188
+ const { apiRoutesVitePlugin } = await import("./api-RH6E5UTH.mjs");
189
+ plugins.push(apiRoutesVitePlugin({ dir: cfg.apiDir }));
190
+ plugins.push(startTypesPlugin(cfg));
191
+ return plugins;
192
+ }
193
+ var _guardsInstalled = false;
194
+ function installCrashGuards() {
195
+ if (_guardsInstalled) return;
196
+ _guardsInstalled = true;
197
+ process.on(
198
+ "unhandledRejection",
199
+ (reason) => console.warn("[fluixi] unhandled rejection (backend likely down) —", reason?.message || reason)
200
+ );
201
+ process.on(
202
+ "uncaughtException",
203
+ (err) => console.warn("[fluixi] uncaught exception —", err?.message || err)
204
+ );
205
+ }
206
+ export {
207
+ I18N_VMOD,
208
+ SERVER_FNS_VMOD,
209
+ fluixiPlugins,
210
+ guardHandler,
211
+ i18nVitePlugin,
212
+ injectApp,
213
+ injectAppAndHead,
214
+ installCrashGuards,
215
+ loadServerEnv,
216
+ splitTemplate,
217
+ startTypesPlugin
218
+ };
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/middleware.ts
21
+ var middleware_exports = {};
22
+ __export(middleware_exports, {
23
+ composeMiddleware: () => composeMiddleware,
24
+ defineMiddleware: () => defineMiddleware,
25
+ normalizeMiddleware: () => normalizeMiddleware
26
+ });
27
+ module.exports = __toCommonJS(middleware_exports);
28
+ function defineMiddleware(mw) {
29
+ return Array.isArray(mw) ? mw.slice() : [mw];
30
+ }
31
+ function normalizeMiddleware(mod) {
32
+ const def = mod?.default ?? mod;
33
+ if (def == null) return [];
34
+ if (Array.isArray(def)) return def.filter((m) => typeof m === "function");
35
+ return typeof def === "function" ? [def] : [];
36
+ }
37
+ function composeMiddleware(middlewares, core) {
38
+ if (middlewares.length === 0) return core;
39
+ return (request) => {
40
+ let lastCalled = -1;
41
+ const dispatch = (i) => {
42
+ if (i <= lastCalled) {
43
+ return Promise.reject(new Error("middleware called next() more than once"));
44
+ }
45
+ lastCalled = i;
46
+ const mw = middlewares[i];
47
+ if (!mw) return Promise.resolve(core(request));
48
+ return Promise.resolve(mw(request, () => dispatch(i + 1)));
49
+ };
50
+ return dispatch(0);
51
+ };
52
+ }
53
+ // Annotate the CommonJS export names for ESM import in node:
54
+ 0 && (module.exports = {
55
+ composeMiddleware,
56
+ defineMiddleware,
57
+ normalizeMiddleware
58
+ });
@@ -0,0 +1,31 @@
1
+ // src/middleware.ts
2
+ function defineMiddleware(mw) {
3
+ return Array.isArray(mw) ? mw.slice() : [mw];
4
+ }
5
+ function normalizeMiddleware(mod) {
6
+ const def = mod?.default ?? mod;
7
+ if (def == null) return [];
8
+ if (Array.isArray(def)) return def.filter((m) => typeof m === "function");
9
+ return typeof def === "function" ? [def] : [];
10
+ }
11
+ function composeMiddleware(middlewares, core) {
12
+ if (middlewares.length === 0) return core;
13
+ return (request) => {
14
+ let lastCalled = -1;
15
+ const dispatch = (i) => {
16
+ if (i <= lastCalled) {
17
+ return Promise.reject(new Error("middleware called next() more than once"));
18
+ }
19
+ lastCalled = i;
20
+ const mw = middlewares[i];
21
+ if (!mw) return Promise.resolve(core(request));
22
+ return Promise.resolve(mw(request, () => dispatch(i + 1)));
23
+ };
24
+ return dispatch(0);
25
+ };
26
+ }
27
+ export {
28
+ composeMiddleware,
29
+ defineMiddleware,
30
+ normalizeMiddleware
31
+ };
@@ -0,0 +1,42 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/preload.ts
22
+ var preload_exports = {};
23
+ __export(preload_exports, {
24
+ createPreloader: () => createPreloader
25
+ });
26
+ module.exports = __toCommonJS(preload_exports);
27
+ var import_router = require("@fluixi/core/router");
28
+ function createPreloader(assets) {
29
+ const patterns = Object.keys(assets ?? {});
30
+ if (!patterns.length) return () => [];
31
+ const routes = patterns.map((path) => ({ path, meta: { path } }));
32
+ return (pathname) => {
33
+ const matched = (0, import_router.matchRoutes)(routes, pathname)?.matched;
34
+ const leaf = matched?.[matched.length - 1];
35
+ const pattern = leaf?.route.meta?.path;
36
+ return pattern ? assets[pattern] ?? [] : [];
37
+ };
38
+ }
39
+ // Annotate the CommonJS export names for ESM import in node:
40
+ 0 && (module.exports = {
41
+ createPreloader
42
+ });
@@ -0,0 +1,18 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+
3
+ // src/preload.ts
4
+ import { matchRoutes } from "@fluixi/core/router";
5
+ function createPreloader(assets) {
6
+ const patterns = Object.keys(assets ?? {});
7
+ if (!patterns.length) return () => [];
8
+ const routes = patterns.map((path) => ({ path, meta: { path } }));
9
+ return (pathname) => {
10
+ const matched = matchRoutes(routes, pathname)?.matched;
11
+ const leaf = matched?.[matched.length - 1];
12
+ const pattern = leaf?.route.meta?.path;
13
+ return pattern ? assets[pattern] ?? [] : [];
14
+ };
15
+ }
16
+ export {
17
+ createPreloader
18
+ };
@@ -0,0 +1,25 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __copyProps = (to, from, except, desc) => {
8
+ if (from && typeof from === "object" || typeof from === "function") {
9
+ for (let key of __getOwnPropNames(from))
10
+ if (!__hasOwnProp.call(to, key) && key !== except)
11
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
12
+ }
13
+ return to;
14
+ };
15
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
16
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
17
+
18
+ // src/router.ts
19
+ var router_exports = {};
20
+ module.exports = __toCommonJS(router_exports);
21
+ __reExport(router_exports, require("@fluixi/core/router"), module.exports);
22
+ // Annotate the CommonJS export names for ESM import in node:
23
+ 0 && (module.exports = {
24
+ ...require("@fluixi/core/router")
25
+ });
@@ -0,0 +1,4 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+
3
+ // src/router.ts
4
+ export * from "@fluixi/core/router";
@@ -0,0 +1,46 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/server-fn-setup.ts
22
+ var server_fn_setup_exports = {};
23
+ __export(server_fn_setup_exports, {
24
+ getRequestEvent: () => import_server.getRequestEvent
25
+ });
26
+ module.exports = __toCommonJS(server_fn_setup_exports);
27
+ var import_server = require("@fluixi/server");
28
+
29
+ // src/server-fn.ts
30
+ var contextRunner = null;
31
+ function setServerFnContextRunner(runner) {
32
+ contextRunner = runner;
33
+ }
34
+
35
+ // src/server-fn-setup.ts
36
+ setServerFnContextRunner((request, run) => {
37
+ const responseHeaders = {};
38
+ const event = { request, url: new URL(request.url).pathname, responseHeaders };
39
+ const ctx = (0, import_server.createRequestContext)(event);
40
+ const result = Promise.resolve((0, import_server.runWithRequestContext)(ctx, run));
41
+ return { result, responseHeaders };
42
+ });
43
+ // Annotate the CommonJS export names for ESM import in node:
44
+ 0 && (module.exports = {
45
+ getRequestEvent
46
+ });
@@ -0,0 +1,22 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+
3
+ // src/server-fn-setup.ts
4
+ import { createRequestContext, runWithRequestContext, getRequestEvent } from "@fluixi/server";
5
+
6
+ // src/server-fn.ts
7
+ var contextRunner = null;
8
+ function setServerFnContextRunner(runner) {
9
+ contextRunner = runner;
10
+ }
11
+
12
+ // src/server-fn-setup.ts
13
+ setServerFnContextRunner((request, run) => {
14
+ const responseHeaders = {};
15
+ const event = { request, url: new URL(request.url).pathname, responseHeaders };
16
+ const ctx = createRequestContext(event);
17
+ const result = Promise.resolve(runWithRequestContext(ctx, run));
18
+ return { result, responseHeaders };
19
+ });
20
+ export {
21
+ getRequestEvent
22
+ };
@@ -0,0 +1,109 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/server-fn.ts
22
+ var server_fn_exports = {};
23
+ __export(server_fn_exports, {
24
+ $$createServerReference: () => $$createServerReference,
25
+ $$registerServerFn: () => $$registerServerFn,
26
+ SERVER_FN_ENDPOINT: () => SERVER_FN_ENDPOINT,
27
+ handleServerFn: () => handleServerFn,
28
+ isServerFnRequest: () => isServerFnRequest,
29
+ setServerFnContextRunner: () => setServerFnContextRunner,
30
+ setServerFnFetch: () => setServerFnFetch
31
+ });
32
+ module.exports = __toCommonJS(server_fn_exports);
33
+ var SERVER_FN_ENDPOINT = "/_server";
34
+ var HEADER = "x-fluixi-server-fn";
35
+ var registry = /* @__PURE__ */ new Map();
36
+ function $$registerServerFn(id, fn) {
37
+ registry.set(id, fn);
38
+ return fn;
39
+ }
40
+ function $$createServerReference(id) {
41
+ return (...args) => callServer(id, args);
42
+ }
43
+ var contextRunner = null;
44
+ function setServerFnContextRunner(runner) {
45
+ contextRunner = runner;
46
+ }
47
+ var _fetch = (...a) => fetch(...a);
48
+ function setServerFnFetch(f) {
49
+ _fetch = f;
50
+ }
51
+ async function callServer(id, args) {
52
+ const res = await _fetch(SERVER_FN_ENDPOINT, {
53
+ method: "POST",
54
+ headers: { "content-type": "application/json", [HEADER]: id },
55
+ body: JSON.stringify(args)
56
+ });
57
+ if (!res.ok) {
58
+ throw new Error(`server function "${id}" failed: ${res.status} ${await res.text()}`);
59
+ }
60
+ return (res.headers.get("content-type") || "").includes("application/json") ? res.json() : res.text();
61
+ }
62
+ function isServerFnRequest(request) {
63
+ return request.method === "POST" && request.headers.has(HEADER);
64
+ }
65
+ async function handleServerFn(request) {
66
+ const origin = request.headers.get("origin");
67
+ if (origin) {
68
+ try {
69
+ if (new URL(origin).host !== new URL(request.url).host) {
70
+ return new Response("forbidden", { status: 403 });
71
+ }
72
+ } catch {
73
+ return new Response("forbidden", { status: 403 });
74
+ }
75
+ }
76
+ const id = request.headers.get(HEADER);
77
+ const fn = registry.get(id);
78
+ if (!fn) return new Response(`unknown server function: ${id}`, { status: 404 });
79
+ let args = [];
80
+ try {
81
+ const parsed = await request.json();
82
+ args = Array.isArray(parsed) ? parsed : [parsed];
83
+ } catch {
84
+ }
85
+ try {
86
+ const headers = { "content-type": "application/json" };
87
+ let result;
88
+ if (contextRunner) {
89
+ const ran = contextRunner(request, () => fn(...args));
90
+ result = await ran.result;
91
+ Object.assign(headers, ran.responseHeaders || {});
92
+ } else {
93
+ result = await fn(...args);
94
+ }
95
+ return new Response(JSON.stringify(result ?? null), { status: 200, headers });
96
+ } catch (e) {
97
+ return new Response(String(e?.message || e), { status: 500 });
98
+ }
99
+ }
100
+ // Annotate the CommonJS export names for ESM import in node:
101
+ 0 && (module.exports = {
102
+ $$createServerReference,
103
+ $$registerServerFn,
104
+ SERVER_FN_ENDPOINT,
105
+ handleServerFn,
106
+ isServerFnRequest,
107
+ setServerFnContextRunner,
108
+ setServerFnFetch
109
+ });
@@ -0,0 +1,79 @@
1
+ /*! @fluixi/start v0.1.0-alpha.74 | (c) 2026 Ibrahima Touré and Fluixi contributors | MIT */
2
+
3
+ // src/server-fn.ts
4
+ var SERVER_FN_ENDPOINT = "/_server";
5
+ var HEADER = "x-fluixi-server-fn";
6
+ var registry = /* @__PURE__ */ new Map();
7
+ function $$registerServerFn(id, fn) {
8
+ registry.set(id, fn);
9
+ return fn;
10
+ }
11
+ function $$createServerReference(id) {
12
+ return (...args) => callServer(id, args);
13
+ }
14
+ var contextRunner = null;
15
+ function setServerFnContextRunner(runner) {
16
+ contextRunner = runner;
17
+ }
18
+ var _fetch = (...a) => fetch(...a);
19
+ function setServerFnFetch(f) {
20
+ _fetch = f;
21
+ }
22
+ async function callServer(id, args) {
23
+ const res = await _fetch(SERVER_FN_ENDPOINT, {
24
+ method: "POST",
25
+ headers: { "content-type": "application/json", [HEADER]: id },
26
+ body: JSON.stringify(args)
27
+ });
28
+ if (!res.ok) {
29
+ throw new Error(`server function "${id}" failed: ${res.status} ${await res.text()}`);
30
+ }
31
+ return (res.headers.get("content-type") || "").includes("application/json") ? res.json() : res.text();
32
+ }
33
+ function isServerFnRequest(request) {
34
+ return request.method === "POST" && request.headers.has(HEADER);
35
+ }
36
+ async function handleServerFn(request) {
37
+ const origin = request.headers.get("origin");
38
+ if (origin) {
39
+ try {
40
+ if (new URL(origin).host !== new URL(request.url).host) {
41
+ return new Response("forbidden", { status: 403 });
42
+ }
43
+ } catch {
44
+ return new Response("forbidden", { status: 403 });
45
+ }
46
+ }
47
+ const id = request.headers.get(HEADER);
48
+ const fn = registry.get(id);
49
+ if (!fn) return new Response(`unknown server function: ${id}`, { status: 404 });
50
+ let args = [];
51
+ try {
52
+ const parsed = await request.json();
53
+ args = Array.isArray(parsed) ? parsed : [parsed];
54
+ } catch {
55
+ }
56
+ try {
57
+ const headers = { "content-type": "application/json" };
58
+ let result;
59
+ if (contextRunner) {
60
+ const ran = contextRunner(request, () => fn(...args));
61
+ result = await ran.result;
62
+ Object.assign(headers, ran.responseHeaders || {});
63
+ } else {
64
+ result = await fn(...args);
65
+ }
66
+ return new Response(JSON.stringify(result ?? null), { status: 200, headers });
67
+ } catch (e) {
68
+ return new Response(String(e?.message || e), { status: 500 });
69
+ }
70
+ }
71
+ export {
72
+ $$createServerReference,
73
+ $$registerServerFn,
74
+ SERVER_FN_ENDPOINT,
75
+ handleServerFn,
76
+ isServerFnRequest,
77
+ setServerFnContextRunner,
78
+ setServerFnFetch
79
+ };