@farm.js/plugin 0.1.16 → 0.1.17
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.map +1 -1
- package/dist/api/index.js +17 -12
- package/dist/dev-request.d.ts +28 -0
- package/dist/dev-request.d.ts.map +1 -0
- package/dist/dev-request.js +82 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/middleware/index.d.ts.map +1 -1
- package/dist/middleware/index.js +19 -7
- package/dist/rsc/index.d.ts.map +1 -1
- package/dist/rsc/index.js +10 -6
- package/dist/rsc/middleware-config-module.d.ts.map +1 -1
- package/dist/rsc/middleware-config-module.js +35 -0
- package/package.json +3 -3
package/dist/api/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAiB,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAiB,MAAM,MAAM,CAAC;AAsBlD,MAAM,WAAW,cAAc;IAC7B,kEAAkE;IAClE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,uDAAuD;IACvD,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACjC;AAED,MAAM,WAAW,QAAQ;IACvB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,OAAO,GAAE,cAAmB,GAAG,MAAM,CA+hBpE;AAED,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
package/dist/api/index.js
CHANGED
|
@@ -25,6 +25,7 @@ import { _withAfterNodeMiddleware } from "@farm.js/core/after";
|
|
|
25
25
|
import { _runWithAPIRequestRuntime, createFarmRequestBodyErrorResponse, readNodeRequestBody, resolveFarmServerConfig, } from "@farm.js/core/internal/production-runtime";
|
|
26
26
|
import { API_ROUTE_METHODS, getAllowedAPIRouteMethods, invokeAPIRouteEndpoint, matchAPIRouteAtBasePath, resolveAPIRouteEndpoint, } from "@farm.js/core/api/runtime";
|
|
27
27
|
import { sendWebResponse } from "@farm.js/core/server";
|
|
28
|
+
import { createDevRequestHeaders, resolveDevRequestURL, resolveViteHtmlFallbackUrl, } from "../dev-request.js";
|
|
28
29
|
/**
|
|
29
30
|
* Farm.js API Routes Plugin
|
|
30
31
|
*/
|
|
@@ -295,19 +296,29 @@ export default function farmApi(options = {}) {
|
|
|
295
296
|
// Add middleware to handle API requests
|
|
296
297
|
return () => {
|
|
297
298
|
const apiMiddleware = _withAfterNodeMiddleware(async (req, res, next) => {
|
|
298
|
-
|
|
299
|
-
|
|
299
|
+
// Route on the URL the client sent, not Vite's SPA fallback rewrite,
|
|
300
|
+
// and put the rewrite back for Vite when this is not an API request.
|
|
301
|
+
const fallbackUrl = req.url;
|
|
302
|
+
const requestedUrl = resolveViteHtmlFallbackUrl(req, server.config.base);
|
|
303
|
+
if (requestedUrl)
|
|
304
|
+
req.url = requestedUrl;
|
|
305
|
+
const passThrough = () => {
|
|
306
|
+
if (requestedUrl && req.url === requestedUrl)
|
|
307
|
+
req.url = fallbackUrl;
|
|
308
|
+
return next();
|
|
309
|
+
};
|
|
310
|
+
const pathname = resolveDevRequestURL(req).pathname;
|
|
300
311
|
const method = req.method || "GET";
|
|
301
312
|
// Only handle /api/ routes
|
|
302
313
|
if (!pathname.startsWith("/api/")) {
|
|
303
|
-
return
|
|
314
|
+
return passThrough();
|
|
304
315
|
}
|
|
305
316
|
// Wait for discovery
|
|
306
317
|
if (discoveryPromise && !discoveryComplete) {
|
|
307
318
|
await discoveryPromise;
|
|
308
319
|
}
|
|
309
320
|
if (!apiRouterHandler) {
|
|
310
|
-
return
|
|
321
|
+
return passThrough();
|
|
311
322
|
}
|
|
312
323
|
const startTime = Date.now();
|
|
313
324
|
try {
|
|
@@ -326,14 +337,8 @@ export default function farmApi(options = {}) {
|
|
|
326
337
|
// Convert Node request to Web Request. ctx.rewrite() updates
|
|
327
338
|
// req.url, so dispatch from the current value, not the one
|
|
328
339
|
// captured before middleware ran.
|
|
329
|
-
const
|
|
330
|
-
const
|
|
331
|
-
const headers = new Headers();
|
|
332
|
-
for (const [key, value] of Object.entries(req.headers)) {
|
|
333
|
-
if (value) {
|
|
334
|
-
headers.set(key, Array.isArray(value) ? value.join(", ") : value);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
340
|
+
const fullUrl = resolveDevRequestURL(req);
|
|
341
|
+
const headers = createDevRequestHeaders(req.headers);
|
|
337
342
|
// Get body for non-GET requests
|
|
338
343
|
let body;
|
|
339
344
|
if (method !== "GET" && method !== "HEAD") {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { IncomingHttpHeaders, IncomingMessage } from "node:http";
|
|
2
|
+
type DevRequest = Pick<IncomingMessage, "url" | "headers"> & {
|
|
3
|
+
originalUrl?: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Node request headers as Web `Headers`. An HTTP/2 request carries
|
|
7
|
+
* pseudo-headers (`:path`, `:authority`, ...) in `req.headers`, and `Headers`
|
|
8
|
+
* throws on those names, so they are skipped. `:authority` stands in for a
|
|
9
|
+
* missing `host`, which is how HTTP/2 carries it.
|
|
10
|
+
*/
|
|
11
|
+
export declare function createDevRequestHeaders(nodeHeaders: IncomingHttpHeaders): Headers;
|
|
12
|
+
/**
|
|
13
|
+
* The URL of a dev request. The path and query come only from `url` (parsed
|
|
14
|
+
* like `new URL(url, "http://localhost")`), and the Host header only picks the
|
|
15
|
+
* origin, so matching and dispatch see the same pathname whatever the Host is.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveDevRequestURL(req: DevRequest, url?: string): URL;
|
|
18
|
+
/**
|
|
19
|
+
* Plugin middleware returned from `configureServer` runs after Vite's own, so
|
|
20
|
+
* Vite's host check, CORS and base handling still apply to it. Under the
|
|
21
|
+
* default `appType: "spa"` Vite's HTML fallback runs before it too, and
|
|
22
|
+
* rewrites every GET that accepts HTML or any type (a plain `fetch()`) to an
|
|
23
|
+
* `.html` file. Returns the URL the client asked for when that happened: the
|
|
24
|
+
* original URL with Vite's base removed, as Vite's base middleware does.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveViteHtmlFallbackUrl(req: DevRequest, base: string | undefined): string | undefined;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=dev-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dev-request.d.ts","sourceRoot":"","sources":["../src/dev-request.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAEtE,KAAK,UAAU,GAAG,IAAI,CAAC,eAAe,EAAE,KAAK,GAAG,SAAS,CAAC,GAAG;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtF;;;;;GAKG;AACH,wBAAgB,uBAAuB,CAAC,WAAW,EAAE,mBAAmB,GAAG,OAAO,CAejF;AAiBD;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,UAAU,EAAE,GAAG,SAAiB,GAAG,GAAG,CAM/E;AAaD;;;;;;;GAOG;AACH,wBAAgB,0BAA0B,CACxC,GAAG,EAAE,UAAU,EACf,IAAI,EAAE,MAAM,GAAG,SAAS,GACvB,MAAM,GAAG,SAAS,CASpB"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node request headers as Web `Headers`. An HTTP/2 request carries
|
|
3
|
+
* pseudo-headers (`:path`, `:authority`, ...) in `req.headers`, and `Headers`
|
|
4
|
+
* throws on those names, so they are skipped. `:authority` stands in for a
|
|
5
|
+
* missing `host`, which is how HTTP/2 carries it.
|
|
6
|
+
*/
|
|
7
|
+
export function createDevRequestHeaders(nodeHeaders) {
|
|
8
|
+
const headers = new Headers();
|
|
9
|
+
for (const [key, value] of Object.entries(nodeHeaders)) {
|
|
10
|
+
if (value === undefined || key.startsWith(":"))
|
|
11
|
+
continue;
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
for (const item of value)
|
|
14
|
+
headers.append(key, item);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
headers.set(key, value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const authority = nodeHeaders[":authority"];
|
|
21
|
+
if (!headers.has("host") && typeof authority === "string" && authority) {
|
|
22
|
+
headers.set("host", authority);
|
|
23
|
+
}
|
|
24
|
+
return headers;
|
|
25
|
+
}
|
|
26
|
+
function resolveDevOrigin(headers) {
|
|
27
|
+
const authority = headers[":authority"];
|
|
28
|
+
const host = headers.host ?? (typeof authority === "string" ? authority : undefined);
|
|
29
|
+
// A host that is not a bare authority (it has a path, query, fragment or
|
|
30
|
+
// userinfo) must not be able to move the request path.
|
|
31
|
+
if (host && !/[\s/?#@\\]/u.test(host)) {
|
|
32
|
+
try {
|
|
33
|
+
return new URL(`http://${host}`).origin;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Fall through to localhost.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return "http://localhost";
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The URL of a dev request. The path and query come only from `url` (parsed
|
|
43
|
+
* like `new URL(url, "http://localhost")`), and the Host header only picks the
|
|
44
|
+
* origin, so matching and dispatch see the same pathname whatever the Host is.
|
|
45
|
+
*/
|
|
46
|
+
export function resolveDevRequestURL(req, url = req.url || "/") {
|
|
47
|
+
const requested = new URL(url, "http://localhost");
|
|
48
|
+
const resolved = new URL(resolveDevOrigin(req.headers));
|
|
49
|
+
resolved.pathname = requested.pathname;
|
|
50
|
+
resolved.search = requested.search;
|
|
51
|
+
return resolved;
|
|
52
|
+
}
|
|
53
|
+
function cleanUrl(url) {
|
|
54
|
+
return url.replace(/[?#].*$/s, "");
|
|
55
|
+
}
|
|
56
|
+
function stripViteBase(url, base) {
|
|
57
|
+
if (!base || base === "/")
|
|
58
|
+
return url;
|
|
59
|
+
const devBase = base.endsWith("/") ? base : `${base}/`;
|
|
60
|
+
if (cleanUrl(url) === devBase.slice(0, -1))
|
|
61
|
+
return "/";
|
|
62
|
+
return url.startsWith(devBase) ? url.slice(devBase.length - 1) : url;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Plugin middleware returned from `configureServer` runs after Vite's own, so
|
|
66
|
+
* Vite's host check, CORS and base handling still apply to it. Under the
|
|
67
|
+
* default `appType: "spa"` Vite's HTML fallback runs before it too, and
|
|
68
|
+
* rewrites every GET that accepts HTML or any type (a plain `fetch()`) to an
|
|
69
|
+
* `.html` file. Returns the URL the client asked for when that happened: the
|
|
70
|
+
* original URL with Vite's base removed, as Vite's base middleware does.
|
|
71
|
+
*/
|
|
72
|
+
export function resolveViteHtmlFallbackUrl(req, base) {
|
|
73
|
+
const current = req.url || "/";
|
|
74
|
+
const original = req.originalUrl;
|
|
75
|
+
if (!original || original === current || !cleanUrl(current).endsWith(".html")) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
const requested = stripViteBase(original, base);
|
|
79
|
+
if (cleanUrl(requested).endsWith(".html"))
|
|
80
|
+
return undefined;
|
|
81
|
+
return requested;
|
|
82
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,MAAM,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAM5D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AA8BjE,MAAM,WAAW,qBAAqB;IACpC,oEAAoE;IACpE,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,2BAA2B;IAC3B,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,IAAI,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,QAAQ,GAAG,KAAK,GAAG,MAAM,CAAC;CACtC;AAED;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;IACtC,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,IAAI,CAAC;IAChE,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,I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,CAAC;IAC7C,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9C;AAED,MAAM,MAAM,iBAAiB,GAAG,CAC9B,GAAG,EAAE,iBAAiB,EACtB,IAAI,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,KACtB,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;AAE1B,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EACF,iBAAiB,GACjB;QAAE,KAAK,EAAE,MAAM;YAAE,QAAQ,EAAE,iBAAiB,EAAE,CAAA;SAAE,CAAC;QAAC,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;KAAE,CAAC;IAC7F,MAAM,CAAC,EAAE,gBAAgB,CAAC;CAC3B;AAuKD;;GAEG;AACH,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAAO,GAAE,qBAA0B,GAAG,MAAM,CAoUlF;AAED,OAAO,EAAE,cAAc,EAAE,CAAC"}
|
package/dist/middleware/index.js
CHANGED
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
import { createRequire } from "node:module";
|
|
17
17
|
import { _withAfterNodeMiddleware } from "@farm.js/core/after";
|
|
18
18
|
import { devServableFileExists } from "../dev-static.js";
|
|
19
|
+
import { resolveDevRequestURL, resolveViteHtmlFallbackUrl } from "../dev-request.js";
|
|
19
20
|
import { matchesMiddlewareConfig } from "@farm.js/core/middleware";
|
|
20
21
|
import { middlewareMatchesPath, parseCookies, serializeCookie } from "./matching.js";
|
|
21
22
|
// Create require for ESM compatibility
|
|
@@ -79,7 +80,7 @@ function createCookieJar(req, res) {
|
|
|
79
80
|
* Create a full middleware context
|
|
80
81
|
*/
|
|
81
82
|
function createContext(req, res, viteServer) {
|
|
82
|
-
const url =
|
|
83
|
+
const url = resolveDevRequestURL(req);
|
|
83
84
|
const headers = new Map();
|
|
84
85
|
const data = new Map();
|
|
85
86
|
const cookies = createCookieJar(req, res);
|
|
@@ -359,8 +360,19 @@ export default function farmMiddleware(options = {}) {
|
|
|
359
360
|
// Add middleware to handle requests
|
|
360
361
|
return () => {
|
|
361
362
|
server.middlewares.use(_withAfterNodeMiddleware(async (req, res, next) => {
|
|
362
|
-
|
|
363
|
-
|
|
363
|
+
// Run middleware for the URL the client sent, not Vite's SPA
|
|
364
|
+
// fallback rewrite, then hand Vite its rewrite back so it still
|
|
365
|
+
// serves the HTML shell (unless middleware rewrote the request).
|
|
366
|
+
const fallbackUrl = req.url;
|
|
367
|
+
const requestedUrl = resolveViteHtmlFallbackUrl(req, server.config.base);
|
|
368
|
+
if (requestedUrl)
|
|
369
|
+
req.url = requestedUrl;
|
|
370
|
+
const passThrough = (error) => {
|
|
371
|
+
if (requestedUrl && req.url === requestedUrl)
|
|
372
|
+
req.url = fallbackUrl;
|
|
373
|
+
return error === undefined ? next() : next(error);
|
|
374
|
+
};
|
|
375
|
+
const pathname = resolveDevRequestURL(req).pathname;
|
|
364
376
|
// Skip Vite internal requests and real static assets. Dotted
|
|
365
377
|
// paths that don't map to a file on disk are app routes (route
|
|
366
378
|
// segments may contain dots) and must still run middleware.
|
|
@@ -370,13 +382,13 @@ export default function farmMiddleware(options = {}) {
|
|
|
370
382
|
(pathname.includes(".") &&
|
|
371
383
|
!pathname.endsWith("/") &&
|
|
372
384
|
devServableFileExists(pathname, [server.config.publicDir, server.config.root]))) {
|
|
373
|
-
return
|
|
385
|
+
return passThrough();
|
|
374
386
|
}
|
|
375
387
|
// @farm.js/plugin/api runs middleware itself for the requests it
|
|
376
388
|
// serves, because it may be registered ahead of this hook. Running
|
|
377
389
|
// it here as well executed every middleware twice per API request.
|
|
378
390
|
if (server.__farmApi__ && pathname.startsWith("/api/")) {
|
|
379
|
-
return
|
|
391
|
+
return passThrough();
|
|
380
392
|
}
|
|
381
393
|
try {
|
|
382
394
|
// Execute middleware
|
|
@@ -390,9 +402,9 @@ export default function farmMiddleware(options = {}) {
|
|
|
390
402
|
// Fail the request, as core's dev server does. Carrying on would
|
|
391
403
|
// render the page a throwing auth check was meant to protect.
|
|
392
404
|
console.error("[FARM] Middleware error:", e);
|
|
393
|
-
return
|
|
405
|
+
return passThrough(e);
|
|
394
406
|
}
|
|
395
|
-
|
|
407
|
+
passThrough();
|
|
396
408
|
}));
|
|
397
409
|
};
|
|
398
410
|
},
|
package/dist/rsc/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rsc/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAIL,KAAK,MAAM,EACX,KAAK,UAAU,EAChB,MAAM,MAAM,CAAC;AAEd,OAAO,KAAK,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AACvD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,KAAK,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/rsc/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAIL,KAAK,MAAM,EACX,KAAK,UAAU,EAChB,MAAM,MAAM,CAAC;AAEd,OAAO,KAAK,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,KAAK,EA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C,EAAE,MAAM,CAAC;IACtB,eAAe,CAAC,EAAE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACjD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE;QACR,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,CAAC;IACF,gHAAgH;IAChH,OAAO,CAAC,EAAE,GAAG,EAAE,CAAC;CACjB;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,MAAM,GAAE,aAAkB,GAAG,UAAU,CA2DnE;AAuXD;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,OAAO,GAAE,oBAAyB,GAAG,MAAM,EAAE,CA+yC5E"}
|
package/dist/rsc/index.js
CHANGED
|
@@ -39,6 +39,7 @@ import { loadRscMiddlewareConfigModule } from "./middleware-config-module.js";
|
|
|
39
39
|
import fs from "fs/promises";
|
|
40
40
|
import path from "path";
|
|
41
41
|
import { devServableFileExists } from "../dev-static.js";
|
|
42
|
+
import { createDevRequestHeaders, resolveDevRequestURL } from "../dev-request.js";
|
|
42
43
|
import { pathToFileURL } from "node:url";
|
|
43
44
|
import { createRequire } from "node:module";
|
|
44
45
|
// Use API and middleware plugins from @farm.js/core (require so CJS build resolves when ESM .mjs is missing)
|
|
@@ -1163,8 +1164,7 @@ if (document.readyState === 'loading') {
|
|
|
1163
1164
|
: "";
|
|
1164
1165
|
globalThis.__FARM_VITE_RSC_LOAD_BOOTSTRAP__ = async () => bootstrapPrefix +
|
|
1165
1166
|
`import("/@react-refresh").then(m=>{m.default.injectIntoGlobalHook(window);window.$RefreshReg$=()=>{};window.$RefreshSig$=()=>type=>type;window.__vite_plugin_react_preamble_installed__=true;return import("/@vite/client");}).then(()=>import(${JSON.stringify(clientEntryUrl)}));`;
|
|
1166
|
-
const
|
|
1167
|
-
const requestUrl = new URL(url, base);
|
|
1167
|
+
const requestUrl = resolveDevRequestURL(req);
|
|
1168
1168
|
const isAction = actionsEnabled &&
|
|
1169
1169
|
method === "POST" &&
|
|
1170
1170
|
!isFarmAPIPathname(requestUrl.pathname, entryContext.apiBasePath) &&
|
|
@@ -1172,7 +1172,7 @@ if (document.readyState === 'loading') {
|
|
|
1172
1172
|
if (isAction) {
|
|
1173
1173
|
validateServerActionRequest(new Request(requestUrl, {
|
|
1174
1174
|
method,
|
|
1175
|
-
headers: req.headers,
|
|
1175
|
+
headers: createDevRequestHeaders(req.headers),
|
|
1176
1176
|
}), entryContext.serverActions);
|
|
1177
1177
|
}
|
|
1178
1178
|
let body;
|
|
@@ -1181,7 +1181,7 @@ if (document.readyState === 'loading') {
|
|
|
1181
1181
|
}
|
|
1182
1182
|
const request = new Request(requestUrl, {
|
|
1183
1183
|
method,
|
|
1184
|
-
headers: req.headers,
|
|
1184
|
+
headers: createDevRequestHeaders(req.headers),
|
|
1185
1185
|
body: body && body.length > 0
|
|
1186
1186
|
? body.buffer.slice(body.byteOffset, body.byteOffset + body.byteLength)
|
|
1187
1187
|
: undefined,
|
|
@@ -1216,7 +1216,11 @@ if (document.readyState === 'loading') {
|
|
|
1216
1216
|
}
|
|
1217
1217
|
if (!rscEntry?.default?.fetch)
|
|
1218
1218
|
throw new Error("[Farm.js] Could not load RSC entry in rsc environment");
|
|
1219
|
-
const response = await rscEntry.default.fetch(request
|
|
1219
|
+
const response = await rscEntry.default.fetch(request, {
|
|
1220
|
+
// The rebuilt Request has no socket; the default rate-limit key
|
|
1221
|
+
// needs the peer address, as the Nitro handler passes it.
|
|
1222
|
+
clientAddress: req.socket?.remoteAddress,
|
|
1223
|
+
});
|
|
1220
1224
|
await sendRscDevelopmentResponse(res, response);
|
|
1221
1225
|
const duration = Date.now() - startTime;
|
|
1222
1226
|
logResponse(method, pathname, response.status, duration);
|
|
@@ -1318,7 +1322,7 @@ if (document.readyState === 'loading') {
|
|
|
1318
1322
|
const Layout = layoutModule?.default;
|
|
1319
1323
|
// Parse URL params (basic dynamic route support)
|
|
1320
1324
|
const params = {};
|
|
1321
|
-
const searchParams = searchParamsToObject(
|
|
1325
|
+
const searchParams = searchParamsToObject(resolveDevRequestURL(req).searchParams);
|
|
1322
1326
|
// Render the page with middleware data
|
|
1323
1327
|
const pageProps = {
|
|
1324
1328
|
params,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware-config-module.d.ts","sourceRoot":"","sources":["../../src/rsc/middleware-config-module.ts"],"names":[],"mappings":"AAQA,KAAK,OAAO,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AASF,KAAK,gBAAgB,GACjB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAChC;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"middleware-config-module.d.ts","sourceRoot":"","sources":["../../src/rsc/middleware-config-module.ts"],"names":[],"mappings":"AAQA,KAAK,OAAO,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACxB,CAAC;AASF,KAAK,gBAAgB,GACjB;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAChC;IAAE,IAAI,EAAE,QAAQ,CAAA;CAAE,GAClB;IAAE,IAAI,EAAE,aAAa,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AA4P5C,wBAAgB,gCAAgC,CAC9C,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,OAAO,GACf,gBAAgB,CA+IlB;AAiBD,wBAAsB,6BAA6B,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAejG"}
|
|
@@ -195,6 +195,38 @@ function resolveConfigObject(node, variableInitializers, configHelpers, seen = n
|
|
|
195
195
|
}
|
|
196
196
|
return null;
|
|
197
197
|
}
|
|
198
|
+
/**
|
|
199
|
+
* The extracted module keeps declarations and drops everything else, so a
|
|
200
|
+
* top-level statement that runs code (`matcher.push("/admin")`,
|
|
201
|
+
* `config.middleware = ...`, a side-effect import) would silently vanish and
|
|
202
|
+
* leave the guard with a different matcher than the config it came from. Fail
|
|
203
|
+
* on those instead of producing a module that disagrees with the config.
|
|
204
|
+
*/
|
|
205
|
+
function unsupportedStatementReason(statement, source) {
|
|
206
|
+
switch (statement.type) {
|
|
207
|
+
case "ImportDeclaration":
|
|
208
|
+
if ((statement.specifiers ?? []).length > 0)
|
|
209
|
+
return null;
|
|
210
|
+
break;
|
|
211
|
+
case "ExportAllDeclaration":
|
|
212
|
+
case "ExportDefaultDeclaration":
|
|
213
|
+
case "ExportNamedDeclaration":
|
|
214
|
+
case "VariableDeclaration":
|
|
215
|
+
case "FunctionDeclaration":
|
|
216
|
+
case "ClassDeclaration":
|
|
217
|
+
case "EmptyStatement":
|
|
218
|
+
return null;
|
|
219
|
+
case "ExpressionStatement":
|
|
220
|
+
if (typeof statement.directive === "string")
|
|
221
|
+
return null;
|
|
222
|
+
break;
|
|
223
|
+
}
|
|
224
|
+
const text = source.slice(statement.start, statement.end).replace(/\s+/g, " ").trim();
|
|
225
|
+
const snippet = text.length > 80 ? `${text.slice(0, 77)}...` : text;
|
|
226
|
+
return statement.type === "ImportDeclaration"
|
|
227
|
+
? `the side-effect import \`${snippet}\` would not run with the extracted middleware`
|
|
228
|
+
: `the top-level statement \`${snippet}\` would not run with the extracted middleware`;
|
|
229
|
+
}
|
|
198
230
|
export function extractRscMiddlewareConfigModule(source, program) {
|
|
199
231
|
const body = program.body ?? [];
|
|
200
232
|
const bindings = new Map();
|
|
@@ -203,6 +235,9 @@ export function extractRscMiddlewareConfigModule(source, program) {
|
|
|
203
235
|
let defaultExport = null;
|
|
204
236
|
let order = 0;
|
|
205
237
|
for (const statement of body) {
|
|
238
|
+
const unsupported = unsupportedStatementReason(statement, source);
|
|
239
|
+
if (unsupported)
|
|
240
|
+
return { kind: "unsupported", reason: unsupported };
|
|
206
241
|
if (statement.type === "ImportDeclaration") {
|
|
207
242
|
const moduleName = statement.source.value;
|
|
208
243
|
for (const specifier of statement.specifiers ?? []) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.17",
|
|
4
4
|
"description": "Official plugins for Farm.js framework",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Farm.js Team",
|
|
@@ -62,7 +62,7 @@
|
|
|
62
62
|
"nitro": "3.0.260903-beta",
|
|
63
63
|
"picocolors": "^1.0.0",
|
|
64
64
|
"rsc-html-stream": "^0.0.4",
|
|
65
|
-
"@farm.js/core": "0.1.
|
|
65
|
+
"@farm.js/core": "0.1.17"
|
|
66
66
|
},
|
|
67
67
|
"devDependencies": {
|
|
68
68
|
"@types/node": "^20.19.21",
|
|
@@ -83,7 +83,7 @@
|
|
|
83
83
|
"react-dom": "19.2.8",
|
|
84
84
|
"react-server-dom-webpack": "19.2.8",
|
|
85
85
|
"vite": ">=6.0.0",
|
|
86
|
-
"@farm.js/core": "0.1.
|
|
86
|
+
"@farm.js/core": "0.1.17"
|
|
87
87
|
},
|
|
88
88
|
"peerDependenciesMeta": {
|
|
89
89
|
"@vitejs/plugin-rsc": {
|