@farm.js/plugin 0.1.0-beta.10 → 0.1.0-beta.100
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/api/index.d.ts +44 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +497 -0
- package/dist/context/index.d.ts +61 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +75 -0
- package/dist/dev-static.d.ts +8 -0
- package/dist/dev-static.d.ts.map +1 -0
- package/dist/dev-static.js +37 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/middleware/index.d.ts +97 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/index.js +444 -0
- package/dist/middleware/matching.d.ts +29 -0
- package/dist/middleware/matching.d.ts.map +1 -0
- package/dist/middleware/matching.js +79 -0
- package/dist/observability/index.d.ts +190 -0
- package/dist/observability/index.d.ts.map +1 -0
- package/dist/observability/index.js +399 -0
- package/dist/rsc/automatic-optimized-boundary.d.ts +11 -0
- package/dist/rsc/automatic-optimized-boundary.d.ts.map +1 -0
- package/dist/rsc/automatic-optimized-boundary.js +162 -0
- package/dist/rsc/build-paths.d.ts +3 -0
- package/dist/rsc/build-paths.d.ts.map +1 -0
- package/dist/rsc/build-paths.js +8 -0
- package/dist/rsc/compatibility.d.ts +8 -0
- package/dist/rsc/compatibility.d.ts.map +1 -0
- package/dist/rsc/compatibility.js +46 -0
- package/dist/rsc/dev-response.d.ts +3 -0
- package/dist/rsc/dev-response.d.ts.map +1 -0
- package/dist/rsc/dev-response.js +10 -0
- package/dist/rsc/entries/client.d.ts +15 -0
- package/dist/rsc/entries/client.d.ts.map +1 -0
- package/dist/rsc/entries/client.js +320 -0
- package/dist/rsc/entries/error-fallback.d.ts +3 -0
- package/dist/rsc/entries/error-fallback.d.ts.map +1 -0
- package/dist/rsc/entries/error-fallback.js +12 -0
- package/dist/rsc/entries/rsc.d.ts +13 -0
- package/dist/rsc/entries/rsc.d.ts.map +1 -0
- package/dist/rsc/entries/rsc.js +930 -0
- package/dist/rsc/entries/ssr.d.ts +13 -0
- package/dist/rsc/entries/ssr.d.ts.map +1 -0
- package/dist/rsc/entries/ssr.js +215 -0
- package/dist/rsc/index.d.ts +91 -0
- package/dist/rsc/index.d.ts.map +1 -0
- package/dist/rsc/index.js +1466 -0
- package/dist/rsc/nitro-build.d.ts +36 -0
- package/dist/rsc/nitro-build.d.ts.map +1 -0
- package/dist/rsc/nitro-build.js +416 -0
- package/dist/rsc/optimized-boundary.d.ts +29 -0
- package/dist/rsc/optimized-boundary.d.ts.map +1 -0
- package/dist/rsc/optimized-boundary.js +243 -0
- package/dist/rsc/route-action-transform.d.ts +11 -0
- package/dist/rsc/route-action-transform.d.ts.map +1 -0
- package/dist/rsc/route-action-transform.js +369 -0
- package/dist/rsc/server-fn-transform.d.ts +6 -0
- package/dist/rsc/server-fn-transform.d.ts.map +1 -0
- package/dist/rsc/server-fn-transform.js +152 -0
- package/dist/rsc/test-fixture-dependencies.d.ts +2 -0
- package/dist/rsc/test-fixture-dependencies.d.ts.map +1 -0
- package/dist/rsc/test-fixture-dependencies.js +23 -0
- package/dist/rsc/types.d.ts +132 -0
- package/dist/rsc/types.d.ts.map +1 -0
- package/dist/rsc/types.js +1 -0
- package/dist/rsc/vite-plugin-nitro.d.ts +46 -0
- package/dist/rsc/vite-plugin-nitro.d.ts.map +1 -0
- package/dist/rsc/vite-plugin-nitro.js +148 -0
- package/package.json +4 -4
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { render, } from "@farming-labs/strata";
|
|
2
|
+
import { StaticFragment, } from "@farming-labs/strata/react-server";
|
|
3
|
+
import { createElement, Fragment, isValidElement } from "react";
|
|
4
|
+
const AUTOMATIC_MIN_NODES = 8;
|
|
5
|
+
const AUTOMATIC_MIN_BYTES = 256;
|
|
6
|
+
const AUTOMATIC_BOUNDARIES = new Set([
|
|
7
|
+
"article",
|
|
8
|
+
"aside",
|
|
9
|
+
"div",
|
|
10
|
+
"footer",
|
|
11
|
+
"header",
|
|
12
|
+
"main",
|
|
13
|
+
"nav",
|
|
14
|
+
"section",
|
|
15
|
+
"span",
|
|
16
|
+
]);
|
|
17
|
+
const STRATA_TAGS = new Set([
|
|
18
|
+
"a",
|
|
19
|
+
"abbr",
|
|
20
|
+
"address",
|
|
21
|
+
"article",
|
|
22
|
+
"aside",
|
|
23
|
+
"b",
|
|
24
|
+
"blockquote",
|
|
25
|
+
"br",
|
|
26
|
+
"caption",
|
|
27
|
+
"cite",
|
|
28
|
+
"code",
|
|
29
|
+
"col",
|
|
30
|
+
"colgroup",
|
|
31
|
+
"dd",
|
|
32
|
+
"del",
|
|
33
|
+
"details",
|
|
34
|
+
"dfn",
|
|
35
|
+
"div",
|
|
36
|
+
"dl",
|
|
37
|
+
"dt",
|
|
38
|
+
"em",
|
|
39
|
+
"figcaption",
|
|
40
|
+
"figure",
|
|
41
|
+
"footer",
|
|
42
|
+
"h1",
|
|
43
|
+
"h2",
|
|
44
|
+
"h3",
|
|
45
|
+
"h4",
|
|
46
|
+
"h5",
|
|
47
|
+
"h6",
|
|
48
|
+
"header",
|
|
49
|
+
"hr",
|
|
50
|
+
"i",
|
|
51
|
+
"img",
|
|
52
|
+
"ins",
|
|
53
|
+
"kbd",
|
|
54
|
+
"li",
|
|
55
|
+
"main",
|
|
56
|
+
"mark",
|
|
57
|
+
"nav",
|
|
58
|
+
"ol",
|
|
59
|
+
"p",
|
|
60
|
+
"pre",
|
|
61
|
+
"q",
|
|
62
|
+
"s",
|
|
63
|
+
"samp",
|
|
64
|
+
"section",
|
|
65
|
+
"small",
|
|
66
|
+
"span",
|
|
67
|
+
"strong",
|
|
68
|
+
"sub",
|
|
69
|
+
"summary",
|
|
70
|
+
"sup",
|
|
71
|
+
"table",
|
|
72
|
+
"tbody",
|
|
73
|
+
"td",
|
|
74
|
+
"tfoot",
|
|
75
|
+
"th",
|
|
76
|
+
"thead",
|
|
77
|
+
"time",
|
|
78
|
+
"tr",
|
|
79
|
+
"u",
|
|
80
|
+
"ul",
|
|
81
|
+
"var",
|
|
82
|
+
"wbr",
|
|
83
|
+
]);
|
|
84
|
+
const BOOLEAN_ATTRIBUTES = new Set(["hidden", "reversed"]);
|
|
85
|
+
const ATTRIBUTE_NAMES = {
|
|
86
|
+
className: "class",
|
|
87
|
+
colSpan: "colspan",
|
|
88
|
+
dateTime: "datetime",
|
|
89
|
+
rowSpan: "rowspan",
|
|
90
|
+
};
|
|
91
|
+
function AutomaticOptimizedBoundary({ document, fallback, ...props }) {
|
|
92
|
+
let content;
|
|
93
|
+
try {
|
|
94
|
+
content = render(document);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
return fallback;
|
|
98
|
+
}
|
|
99
|
+
if (content.nodeCount < AUTOMATIC_MIN_NODES && content.bytes < AUTOMATIC_MIN_BYTES) {
|
|
100
|
+
return fallback;
|
|
101
|
+
}
|
|
102
|
+
return StaticFragment({ ...props, content });
|
|
103
|
+
}
|
|
104
|
+
function attributeValue(name, value) {
|
|
105
|
+
if (value === null || value === undefined)
|
|
106
|
+
return undefined;
|
|
107
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "bigint") {
|
|
108
|
+
return String(value);
|
|
109
|
+
}
|
|
110
|
+
if (typeof value !== "boolean")
|
|
111
|
+
return null;
|
|
112
|
+
if (name.startsWith("aria-") || name.startsWith("data-"))
|
|
113
|
+
return String(value);
|
|
114
|
+
if (BOOLEAN_ATTRIBUTES.has(name))
|
|
115
|
+
return value ? "" : undefined;
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
function convertAttributes(tag, props) {
|
|
119
|
+
const attributes = {};
|
|
120
|
+
for (const [reactName, rawValue] of Object.entries(props)) {
|
|
121
|
+
if (reactName === "children")
|
|
122
|
+
continue;
|
|
123
|
+
if (reactName === "dangerouslySetInnerHTML" ||
|
|
124
|
+
reactName === "ref" ||
|
|
125
|
+
reactName === "style" ||
|
|
126
|
+
reactName === "suppressHydrationWarning" ||
|
|
127
|
+
/^on[A-Z]/.test(reactName)) {
|
|
128
|
+
return null;
|
|
129
|
+
}
|
|
130
|
+
const name = ATTRIBUTE_NAMES[reactName] || reactName;
|
|
131
|
+
const value = attributeValue(name, rawValue);
|
|
132
|
+
if (value === null)
|
|
133
|
+
return null;
|
|
134
|
+
if (value !== undefined)
|
|
135
|
+
attributes[name] = value;
|
|
136
|
+
}
|
|
137
|
+
return Object.keys(attributes).length > 0 ? attributes : {};
|
|
138
|
+
}
|
|
139
|
+
function automaticDocumentFromElement(element) {
|
|
140
|
+
const rootProps = element.props;
|
|
141
|
+
const children = [];
|
|
142
|
+
if (!appendStrataNodes(rootProps.children, children))
|
|
143
|
+
return null;
|
|
144
|
+
return { type: "document", ...(children.length > 0 ? { children } : {}) };
|
|
145
|
+
}
|
|
146
|
+
function appendStrataNodes(value, output) {
|
|
147
|
+
if (value === null || value === undefined || typeof value === "boolean")
|
|
148
|
+
return true;
|
|
149
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "bigint") {
|
|
150
|
+
output.push({ type: "text", value: String(value) });
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
if (Array.isArray(value)) {
|
|
154
|
+
for (const child of value) {
|
|
155
|
+
if (!appendStrataNodes(child, output))
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
if (!isValidElement(value))
|
|
161
|
+
return false;
|
|
162
|
+
const element = value;
|
|
163
|
+
const props = element.props;
|
|
164
|
+
if (element.type === Fragment)
|
|
165
|
+
return appendStrataNodes(props.children, output);
|
|
166
|
+
if (element.type === OptimizedBoundary || element.type === AutomaticOptimizedBoundary) {
|
|
167
|
+
const boundary = props.as ?? "div";
|
|
168
|
+
if (typeof boundary !== "string" || !STRATA_TAGS.has(boundary))
|
|
169
|
+
return false;
|
|
170
|
+
const { as: _as, document: _document, fallback: _fallback, ...boundaryProps } = props;
|
|
171
|
+
const attributes = convertAttributes(boundary, boundaryProps);
|
|
172
|
+
if (!attributes)
|
|
173
|
+
return false;
|
|
174
|
+
const document = props.document;
|
|
175
|
+
if (!document || document.type !== "document")
|
|
176
|
+
return false;
|
|
177
|
+
output.push({
|
|
178
|
+
type: "element",
|
|
179
|
+
tag: boundary,
|
|
180
|
+
...(Object.keys(attributes).length > 0 ? { attributes } : {}),
|
|
181
|
+
...(document.children?.length ? { children: document.children } : {}),
|
|
182
|
+
});
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
if (typeof element.type !== "string" || !STRATA_TAGS.has(element.type)) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
const tag = element.type;
|
|
189
|
+
const attributes = convertAttributes(tag, props);
|
|
190
|
+
if (!attributes)
|
|
191
|
+
return false;
|
|
192
|
+
const children = [];
|
|
193
|
+
if (!appendStrataNodes(props.children, children))
|
|
194
|
+
return false;
|
|
195
|
+
output.push({
|
|
196
|
+
type: "element",
|
|
197
|
+
tag,
|
|
198
|
+
...(Object.keys(attributes).length > 0 ? { attributes } : {}),
|
|
199
|
+
...(children.length > 0 ? { children } : {}),
|
|
200
|
+
});
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Internal compiler target for automatic optimized boundaries. This helper is
|
|
205
|
+
* deliberately fail-open: any unsupported or unsafe tree returns the original
|
|
206
|
+
* React element unchanged.
|
|
207
|
+
*
|
|
208
|
+
* @internal
|
|
209
|
+
*/
|
|
210
|
+
export function _optimizeBoundary(element) {
|
|
211
|
+
if (!isValidElement(element) ||
|
|
212
|
+
typeof element.type !== "string" ||
|
|
213
|
+
!AUTOMATIC_BOUNDARIES.has(element.type)) {
|
|
214
|
+
return element;
|
|
215
|
+
}
|
|
216
|
+
const props = element.props;
|
|
217
|
+
if (props.dangerouslySetInnerHTML !== undefined || props.ref !== undefined)
|
|
218
|
+
return element;
|
|
219
|
+
const document = automaticDocumentFromElement(element);
|
|
220
|
+
if (!document)
|
|
221
|
+
return element;
|
|
222
|
+
const { children: _children, ...boundaryProps } = props;
|
|
223
|
+
return createElement(AutomaticOptimizedBoundary, {
|
|
224
|
+
...boundaryProps,
|
|
225
|
+
...(element.key !== null ? { key: element.key } : {}),
|
|
226
|
+
as: element.type,
|
|
227
|
+
document,
|
|
228
|
+
fallback: element,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Render a typed host-only document with the optimized native runtime and
|
|
233
|
+
* place it inside an opaque React-owned boundary.
|
|
234
|
+
*
|
|
235
|
+
* Enable `experimental.optimizedBoundary` before importing this component.
|
|
236
|
+
*/
|
|
237
|
+
export function OptimizedBoundary({ document, renderOptions, ...props }) {
|
|
238
|
+
const content = render(document, renderOptions);
|
|
239
|
+
return StaticFragment({
|
|
240
|
+
...props,
|
|
241
|
+
content,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type FarmRouteActionClientTransformResult = {
|
|
2
|
+
code: string;
|
|
3
|
+
routes: string[];
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Replace a programmatic route imported by the browser with a small action-only
|
|
7
|
+
* proxy. Route loaders, components, database imports, and other server code do
|
|
8
|
+
* not enter the client graph.
|
|
9
|
+
*/
|
|
10
|
+
export declare function transformFarmRouteActionClients(code: string, id: string): FarmRouteActionClientTransformResult | null;
|
|
11
|
+
//# sourceMappingURL=route-action-transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route-action-transform.d.ts","sourceRoot":"","sources":["../../src/rsc/route-action-transform.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,oCAAoC,GAAG;IACjD,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,EAAE,CAAC;CAClB,CAAC;AAcF;;;;GAIG;AACH,wBAAgB,+BAA+B,CAC7C,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,oCAAoC,GAAG,IAAI,CAqC7C"}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
const ROUTE_FACTORY_IMPORT_RE = /\bimport\s+(?:type\s+)?(?:[A-Za-z_$][\w$]*\s*,\s*)?(\{[^}]*\})\s+from\s*["']@farm.js\/core(?:\/routes)?["']\s*;?/g;
|
|
2
|
+
const EXPORTED_ROUTE_RE = /\bexport\s+(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*([A-Za-z_$][\w$]*)\s*\(/g;
|
|
3
|
+
const IMPORT_RE = /\bimport\s+(?!type\b)([\s\S]*?)\s+from\s*(["'][^"']+["'])\s*;?/g;
|
|
4
|
+
const IDENTIFIER_RE = /^[A-Za-z_$][\w$]*$/;
|
|
5
|
+
const MODULE_EXT_RE = /\.[cm]?[jt]sx?$/;
|
|
6
|
+
/**
|
|
7
|
+
* Replace a programmatic route imported by the browser with a small action-only
|
|
8
|
+
* proxy. Route loaders, components, database imports, and other server code do
|
|
9
|
+
* not enter the client graph.
|
|
10
|
+
*/
|
|
11
|
+
export function transformFarmRouteActionClients(code, id) {
|
|
12
|
+
if (!isTransformableModule(id) || !code.includes("createRoute") || !code.includes("actions")) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const routeFactories = findRouteFactoryImportNames(code);
|
|
16
|
+
if (routeFactories.size === 0)
|
|
17
|
+
return null;
|
|
18
|
+
const routes = findActionRoutes(code, routeFactories, id);
|
|
19
|
+
if (routes.length === 0)
|
|
20
|
+
return null;
|
|
21
|
+
const importStatements = findRuntimeImports(code);
|
|
22
|
+
const requiredImports = new Set();
|
|
23
|
+
for (const route of routes) {
|
|
24
|
+
for (const action of route.actions) {
|
|
25
|
+
const statement = importStatements.get(action.local);
|
|
26
|
+
if (!statement) {
|
|
27
|
+
throw new Error(`[Farm.js] Route "${route.exportName}" action "${action.name}" in ${id} must reference ` +
|
|
28
|
+
"a server function imported from another module. Define and export createServerFn in an actions module, then import it into the route.");
|
|
29
|
+
}
|
|
30
|
+
requiredImports.add(statement);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const output = [
|
|
34
|
+
...requiredImports,
|
|
35
|
+
...routes.map((route) => generateRouteActionProxy(route)),
|
|
36
|
+
...findRouteDefaultExports(code, routes),
|
|
37
|
+
].join("\n");
|
|
38
|
+
return {
|
|
39
|
+
code: `${output}\n`,
|
|
40
|
+
routes: routes.map((route) => route.exportName),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function findRouteFactoryImportNames(code) {
|
|
44
|
+
const names = new Set();
|
|
45
|
+
for (const match of code.matchAll(ROUTE_FACTORY_IMPORT_RE)) {
|
|
46
|
+
const namedImports = (match[1] ?? "").slice(1, -1).split(",");
|
|
47
|
+
for (const specifier of namedImports) {
|
|
48
|
+
const normalized = specifier.trim().replace(/^type\s+/, "");
|
|
49
|
+
const specifierMatch = normalized.match(/^createRoute(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
|
|
50
|
+
if (specifierMatch)
|
|
51
|
+
names.add(specifierMatch[1] ?? "createRoute");
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return names;
|
|
55
|
+
}
|
|
56
|
+
function findActionRoutes(code, factories, id) {
|
|
57
|
+
const routes = [];
|
|
58
|
+
for (const match of code.matchAll(EXPORTED_ROUTE_RE)) {
|
|
59
|
+
const exportName = match[1];
|
|
60
|
+
const calleeName = match[2];
|
|
61
|
+
if (!exportName || !calleeName || !factories.has(calleeName))
|
|
62
|
+
continue;
|
|
63
|
+
const openParen = (match.index ?? 0) + match[0].length - 1;
|
|
64
|
+
const closeParen = findMatchingDelimiter(code, openParen, "(", ")");
|
|
65
|
+
if (closeParen === -1)
|
|
66
|
+
continue;
|
|
67
|
+
const args = splitTopLevel(code, openParen + 1, closeParen);
|
|
68
|
+
if (args.length < 2)
|
|
69
|
+
continue;
|
|
70
|
+
const pathSource = readSegment(code, args[0]);
|
|
71
|
+
if (!isQuotedString(pathSource)) {
|
|
72
|
+
throw new Error(`[Farm.js] Route "${exportName}" in ${id} must use a static string path before its actions can be imported by a Client Component.`);
|
|
73
|
+
}
|
|
74
|
+
const options = readSegment(code, args[1]);
|
|
75
|
+
if (!options.startsWith("{") || !options.endsWith("}"))
|
|
76
|
+
continue;
|
|
77
|
+
const properties = parseObjectProperties(options.slice(1, -1));
|
|
78
|
+
const actionsSource = properties.get("actions");
|
|
79
|
+
if (!actionsSource)
|
|
80
|
+
continue;
|
|
81
|
+
if (!actionsSource.startsWith("{") || !actionsSource.endsWith("}")) {
|
|
82
|
+
throw new Error(`[Farm.js] Route "${exportName}" actions in ${id} must be an object literal so Farm can generate its client proxy.`);
|
|
83
|
+
}
|
|
84
|
+
const actions = parseActionProperties(actionsSource.slice(1, -1), exportName, id);
|
|
85
|
+
if (actions.length === 0)
|
|
86
|
+
continue;
|
|
87
|
+
const defaultSource = properties.get("defaultAction");
|
|
88
|
+
const defaultAction = defaultSource
|
|
89
|
+
? parseStringValue(defaultSource, exportName, id)
|
|
90
|
+
: actions[0].name;
|
|
91
|
+
if (!actions.some((action) => action.name === defaultAction)) {
|
|
92
|
+
throw new Error(`[Farm.js] Route "${exportName}" defaultAction "${defaultAction}" in ${id} does not match a declared action.`);
|
|
93
|
+
}
|
|
94
|
+
routes.push({ exportName, pathSource, actions, defaultAction });
|
|
95
|
+
}
|
|
96
|
+
return routes;
|
|
97
|
+
}
|
|
98
|
+
function parseActionProperties(source, routeName, id) {
|
|
99
|
+
const properties = parseObjectProperties(source, true);
|
|
100
|
+
const actions = [];
|
|
101
|
+
for (const [name, value] of properties) {
|
|
102
|
+
const local = value || name;
|
|
103
|
+
if (!IDENTIFIER_RE.test(name) || !IDENTIFIER_RE.test(local)) {
|
|
104
|
+
throw new Error(`[Farm.js] Route "${routeName}" actions in ${id} must use identifier entries such as { update } or { update: updateProduct }.`);
|
|
105
|
+
}
|
|
106
|
+
actions.push({ name, local });
|
|
107
|
+
}
|
|
108
|
+
return actions;
|
|
109
|
+
}
|
|
110
|
+
function parseObjectProperties(source, allowShorthand = false) {
|
|
111
|
+
const properties = new Map();
|
|
112
|
+
for (const segment of splitTopLevel(source, 0, source.length)) {
|
|
113
|
+
const entry = readSegment(source, segment);
|
|
114
|
+
if (!entry)
|
|
115
|
+
continue;
|
|
116
|
+
if (entry.startsWith("...")) {
|
|
117
|
+
properties.set(entry, "");
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const colon = findTopLevelColon(entry);
|
|
121
|
+
if (colon === -1) {
|
|
122
|
+
if (allowShorthand)
|
|
123
|
+
properties.set(entry, "");
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const keySource = entry.slice(0, colon).trim();
|
|
127
|
+
const value = entry.slice(colon + 1).trim();
|
|
128
|
+
const key = isQuotedString(keySource) ? unquote(keySource) : keySource;
|
|
129
|
+
properties.set(key, value);
|
|
130
|
+
}
|
|
131
|
+
return properties;
|
|
132
|
+
}
|
|
133
|
+
function findRuntimeImports(code) {
|
|
134
|
+
const imports = new Map();
|
|
135
|
+
for (const match of code.matchAll(IMPORT_RE)) {
|
|
136
|
+
const clause = (match[1] ?? "").trim();
|
|
137
|
+
const statement = match[0];
|
|
138
|
+
const namedStart = clause.indexOf("{");
|
|
139
|
+
const namedEnd = clause.lastIndexOf("}");
|
|
140
|
+
if (namedStart > -1 && namedEnd > namedStart) {
|
|
141
|
+
for (const specifier of clause.slice(namedStart + 1, namedEnd).split(",")) {
|
|
142
|
+
const normalized = specifier.trim().replace(/^type\s+/, "");
|
|
143
|
+
if (!normalized || specifier.trim().startsWith("type "))
|
|
144
|
+
continue;
|
|
145
|
+
const parts = normalized.split(/\s+as\s+/);
|
|
146
|
+
const local = parts[1] ?? parts[0];
|
|
147
|
+
if (local && IDENTIFIER_RE.test(local))
|
|
148
|
+
imports.set(local, statement);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
const defaultImport = clause
|
|
152
|
+
.slice(0, namedStart === -1 ? clause.length : namedStart)
|
|
153
|
+
.replace(/,$/, "")
|
|
154
|
+
.trim();
|
|
155
|
+
if (IDENTIFIER_RE.test(defaultImport))
|
|
156
|
+
imports.set(defaultImport, statement);
|
|
157
|
+
}
|
|
158
|
+
return imports;
|
|
159
|
+
}
|
|
160
|
+
function generateRouteActionProxy(route) {
|
|
161
|
+
const actions = route.actions
|
|
162
|
+
.map((action) => `${JSON.stringify(action.name)}: ${action.local}`)
|
|
163
|
+
.join(", ");
|
|
164
|
+
const defaultAction = route.actions.find((action) => action.name === route.defaultAction);
|
|
165
|
+
return `export const ${route.exportName} = Object.freeze({
|
|
166
|
+
__farmRouteActionTarget: true,
|
|
167
|
+
path: ${route.pathSource},
|
|
168
|
+
actions: Object.freeze({ ${actions} }),
|
|
169
|
+
defaultAction: ${JSON.stringify(route.defaultAction)},
|
|
170
|
+
action: ${defaultAction.local},
|
|
171
|
+
});`;
|
|
172
|
+
}
|
|
173
|
+
function findRouteDefaultExports(code, routes) {
|
|
174
|
+
const exports = [];
|
|
175
|
+
for (const route of routes) {
|
|
176
|
+
const defaultExport = new RegExp(`\\bexport\\s+default\\s+${escapeRegExp(route.exportName)}\\s*;?`);
|
|
177
|
+
if (defaultExport.test(code))
|
|
178
|
+
exports.push(`export default ${route.exportName};`);
|
|
179
|
+
}
|
|
180
|
+
return exports;
|
|
181
|
+
}
|
|
182
|
+
function splitTopLevel(source, start, end) {
|
|
183
|
+
const segments = [];
|
|
184
|
+
let segmentStart = start;
|
|
185
|
+
let parens = 0;
|
|
186
|
+
let braces = 0;
|
|
187
|
+
let brackets = 0;
|
|
188
|
+
let quote = null;
|
|
189
|
+
let escaped = false;
|
|
190
|
+
let lineComment = false;
|
|
191
|
+
let blockComment = false;
|
|
192
|
+
for (let index = start; index < end; index++) {
|
|
193
|
+
const char = source[index];
|
|
194
|
+
const next = source[index + 1];
|
|
195
|
+
if (lineComment) {
|
|
196
|
+
if (char === "\n" || char === "\r")
|
|
197
|
+
lineComment = false;
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (blockComment) {
|
|
201
|
+
if (char === "*" && next === "/") {
|
|
202
|
+
blockComment = false;
|
|
203
|
+
index++;
|
|
204
|
+
}
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (quote) {
|
|
208
|
+
if (escaped)
|
|
209
|
+
escaped = false;
|
|
210
|
+
else if (char === "\\")
|
|
211
|
+
escaped = true;
|
|
212
|
+
else if (char === quote)
|
|
213
|
+
quote = null;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (char === "/" && next === "/") {
|
|
217
|
+
lineComment = true;
|
|
218
|
+
index++;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (char === "/" && next === "*") {
|
|
222
|
+
blockComment = true;
|
|
223
|
+
index++;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (char === "'" || char === '"' || char === "`") {
|
|
227
|
+
quote = char;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (char === "(")
|
|
231
|
+
parens++;
|
|
232
|
+
else if (char === ")")
|
|
233
|
+
parens--;
|
|
234
|
+
else if (char === "{")
|
|
235
|
+
braces++;
|
|
236
|
+
else if (char === "}")
|
|
237
|
+
braces--;
|
|
238
|
+
else if (char === "[")
|
|
239
|
+
brackets++;
|
|
240
|
+
else if (char === "]")
|
|
241
|
+
brackets--;
|
|
242
|
+
else if (char === "," && parens === 0 && braces === 0 && brackets === 0) {
|
|
243
|
+
segments.push([segmentStart, index]);
|
|
244
|
+
segmentStart = index + 1;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
segments.push([segmentStart, end]);
|
|
248
|
+
return segments;
|
|
249
|
+
}
|
|
250
|
+
function findTopLevelColon(source) {
|
|
251
|
+
const segments = splitTopLevel(source, 0, source.length);
|
|
252
|
+
if (segments.length !== 1)
|
|
253
|
+
return -1;
|
|
254
|
+
let parens = 0;
|
|
255
|
+
let braces = 0;
|
|
256
|
+
let brackets = 0;
|
|
257
|
+
let quote = null;
|
|
258
|
+
let escaped = false;
|
|
259
|
+
for (let index = 0; index < source.length; index++) {
|
|
260
|
+
const char = source[index];
|
|
261
|
+
if (quote) {
|
|
262
|
+
if (escaped)
|
|
263
|
+
escaped = false;
|
|
264
|
+
else if (char === "\\")
|
|
265
|
+
escaped = true;
|
|
266
|
+
else if (char === quote)
|
|
267
|
+
quote = null;
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (char === "'" || char === '"' || char === "`") {
|
|
271
|
+
quote = char;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (char === "(")
|
|
275
|
+
parens++;
|
|
276
|
+
else if (char === ")")
|
|
277
|
+
parens--;
|
|
278
|
+
else if (char === "{")
|
|
279
|
+
braces++;
|
|
280
|
+
else if (char === "}")
|
|
281
|
+
braces--;
|
|
282
|
+
else if (char === "[")
|
|
283
|
+
brackets++;
|
|
284
|
+
else if (char === "]")
|
|
285
|
+
brackets--;
|
|
286
|
+
else if (char === ":" && parens === 0 && braces === 0 && brackets === 0)
|
|
287
|
+
return index;
|
|
288
|
+
}
|
|
289
|
+
return -1;
|
|
290
|
+
}
|
|
291
|
+
function findMatchingDelimiter(source, openIndex, open, close) {
|
|
292
|
+
let depth = 0;
|
|
293
|
+
let quote = null;
|
|
294
|
+
let lineComment = false;
|
|
295
|
+
let blockComment = false;
|
|
296
|
+
let escaped = false;
|
|
297
|
+
for (let index = openIndex; index < source.length; index++) {
|
|
298
|
+
const char = source[index];
|
|
299
|
+
const next = source[index + 1];
|
|
300
|
+
if (lineComment) {
|
|
301
|
+
if (char === "\n" || char === "\r")
|
|
302
|
+
lineComment = false;
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (blockComment) {
|
|
306
|
+
if (char === "*" && next === "/") {
|
|
307
|
+
blockComment = false;
|
|
308
|
+
index++;
|
|
309
|
+
}
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (quote) {
|
|
313
|
+
if (escaped)
|
|
314
|
+
escaped = false;
|
|
315
|
+
else if (char === "\\")
|
|
316
|
+
escaped = true;
|
|
317
|
+
else if (char === quote)
|
|
318
|
+
quote = null;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (char === "/" && next === "/") {
|
|
322
|
+
lineComment = true;
|
|
323
|
+
index++;
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
if (char === "/" && next === "*") {
|
|
327
|
+
blockComment = true;
|
|
328
|
+
index++;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
if (char === "'" || char === '"' || char === "`") {
|
|
332
|
+
quote = char;
|
|
333
|
+
continue;
|
|
334
|
+
}
|
|
335
|
+
if (char === open)
|
|
336
|
+
depth++;
|
|
337
|
+
else if (char === close && --depth === 0)
|
|
338
|
+
return index;
|
|
339
|
+
}
|
|
340
|
+
return -1;
|
|
341
|
+
}
|
|
342
|
+
function readSegment(source, segment) {
|
|
343
|
+
return source.slice(segment[0], segment[1]).trim();
|
|
344
|
+
}
|
|
345
|
+
function parseStringValue(source, routeName, id) {
|
|
346
|
+
if (!isQuotedString(source)) {
|
|
347
|
+
throw new Error(`[Farm.js] Route "${routeName}" defaultAction in ${id} must be a static string.`);
|
|
348
|
+
}
|
|
349
|
+
return unquote(source);
|
|
350
|
+
}
|
|
351
|
+
function isQuotedString(value) {
|
|
352
|
+
return (value.length >= 2 &&
|
|
353
|
+
((value.startsWith('"') && value.endsWith('"')) ||
|
|
354
|
+
(value.startsWith("'") && value.endsWith("'"))));
|
|
355
|
+
}
|
|
356
|
+
function unquote(value) {
|
|
357
|
+
const quote = value[0];
|
|
358
|
+
const body = value.slice(1, -1);
|
|
359
|
+
if (quote === '"')
|
|
360
|
+
return JSON.parse(value);
|
|
361
|
+
return body.replace(/\\'/g, "'").replace(/\\\\/g, "\\");
|
|
362
|
+
}
|
|
363
|
+
function isTransformableModule(id) {
|
|
364
|
+
const cleanId = id.split("?", 1)[0];
|
|
365
|
+
return MODULE_EXT_RE.test(cleanId) && !/\.d\.[cm]?ts$/.test(cleanId);
|
|
366
|
+
}
|
|
367
|
+
function escapeRegExp(value) {
|
|
368
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
369
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-fn-transform.d.ts","sourceRoot":"","sources":["../../src/rsc/server-fn-transform.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,2BAA2B,GAAG;IACxC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB,CAAC;AAEF,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,2BAA2B,GAAG,IAAI,CAiCpC"}
|