nuxt-kirby 4.2.0 → 5.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.
- package/README.md +4 -4
- package/dist/module.d.mts +49 -41
- package/dist/module.json +2 -2
- package/dist/module.mjs +159 -136
- package/dist/runtime/composables/$kirby.d.ts +7 -32
- package/dist/runtime/composables/$kirby.js +48 -61
- package/dist/runtime/composables/$kql.d.ts +1 -28
- package/dist/runtime/composables/$kql.js +7 -48
- package/dist/runtime/composables/useKirbyData.d.ts +31 -20
- package/dist/runtime/composables/useKirbyData.js +19 -30
- package/dist/runtime/composables/useKql.d.ts +5 -23
- package/dist/runtime/composables/useKql.js +10 -26
- package/dist/runtime/server/cache.d.ts +2 -0
- package/dist/runtime/server/cache.js +77 -0
- package/dist/runtime/server/imports.d.ts +3 -21
- package/dist/runtime/server/imports.js +11 -20
- package/dist/runtime/server/path.d.ts +9 -0
- package/dist/runtime/server/path.js +56 -0
- package/dist/runtime/server/proxy.d.ts +20 -0
- package/dist/runtime/server/proxy.js +118 -0
- package/dist/runtime/server/stream.d.ts +5 -0
- package/dist/runtime/server/stream.js +20 -0
- package/dist/runtime/types.d.ts +50 -15
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/utils.d.ts +5 -16
- package/dist/runtime/utils.js +10 -15
- package/package.json +5 -4
- package/dist/runtime/cache.d.ts +0 -11
- package/dist/runtime/cache.js +0 -25
- package/dist/runtime/kirby-types.d.ts +0 -1
- package/dist/runtime/kirby-types.js +0 -0
- package/dist/runtime/path.d.ts +0 -6
- package/dist/runtime/path.js +0 -10
- package/dist/runtime/server/handler.d.ts +0 -2
- package/dist/runtime/server/handler.js +0 -179
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createError,
|
|
3
|
+
defineEventHandler,
|
|
4
|
+
getRequestHeader,
|
|
5
|
+
getRequestWebStream,
|
|
6
|
+
send,
|
|
7
|
+
setResponseHeader,
|
|
8
|
+
setResponseStatus
|
|
9
|
+
} from "h3";
|
|
10
|
+
import { useRuntimeConfig } from "nitropack/runtime";
|
|
11
|
+
import { getQuery, joinURL, withQuery } from "ufo";
|
|
12
|
+
import { createAuthHeader } from "../utils.js";
|
|
13
|
+
import { assertAllowedRoute, assertPathBelowBase, resolveRoutePath } from "./path.js";
|
|
14
|
+
import { endOnAbort } from "./stream.js";
|
|
15
|
+
const HOP_BY_HOP_HEADERS = /* @__PURE__ */ new Set([
|
|
16
|
+
"connection",
|
|
17
|
+
"keep-alive",
|
|
18
|
+
"proxy-authenticate",
|
|
19
|
+
"proxy-authorization",
|
|
20
|
+
"proxy-connection",
|
|
21
|
+
"te",
|
|
22
|
+
"trailer",
|
|
23
|
+
"transfer-encoding",
|
|
24
|
+
"upgrade"
|
|
25
|
+
]);
|
|
26
|
+
const FORWARDED_REQUEST_HEADERS = ["content-type", "content-length", "x-language"];
|
|
27
|
+
export const WITHHELD_RESPONSE_HEADERS = /* @__PURE__ */ new Set([
|
|
28
|
+
...HOP_BY_HOP_HEADERS,
|
|
29
|
+
// The body arrives decoded, so its original encoding and length no longer apply.
|
|
30
|
+
"content-encoding",
|
|
31
|
+
"content-length",
|
|
32
|
+
"set-cookie",
|
|
33
|
+
// Both name the software behind the Kirby URL.
|
|
34
|
+
"server",
|
|
35
|
+
"x-powered-by"
|
|
36
|
+
]);
|
|
37
|
+
export default defineEventHandler(async (event) => {
|
|
38
|
+
const kirby = useRuntimeConfig(event).kirby;
|
|
39
|
+
const { url, headers } = resolveProxyRequest(event);
|
|
40
|
+
const abortController = new AbortController();
|
|
41
|
+
if (event.node.res.socket)
|
|
42
|
+
event.node.res.once("close", () => abortController.abort());
|
|
43
|
+
let response;
|
|
44
|
+
try {
|
|
45
|
+
response = await globalThis.$fetch.raw(url, {
|
|
46
|
+
method: event.method,
|
|
47
|
+
headers,
|
|
48
|
+
body: getRequestWebStream(event),
|
|
49
|
+
responseType: "stream",
|
|
50
|
+
// The client decides whether to follow a redirect.
|
|
51
|
+
redirect: "manual",
|
|
52
|
+
ignoreResponseError: true,
|
|
53
|
+
signal: abortController.signal
|
|
54
|
+
});
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (abortController.signal.aborted)
|
|
57
|
+
return null;
|
|
58
|
+
console.error("[nuxt-kirby]", error);
|
|
59
|
+
throw createError({
|
|
60
|
+
statusCode: 502,
|
|
61
|
+
statusMessage: "Bad Gateway"
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
relayResponseHead(event, response);
|
|
65
|
+
const cookies = response.headers.getSetCookie();
|
|
66
|
+
if (kirby.forwardCookies && cookies.length)
|
|
67
|
+
setResponseHeader(event, "set-cookie", cookies);
|
|
68
|
+
if (!response._data)
|
|
69
|
+
return send(event);
|
|
70
|
+
return event.node.res.socket ? endOnAbort(response._data, abortController.signal) : response._data;
|
|
71
|
+
});
|
|
72
|
+
export function relayResponseHead(event, { status, statusText, headers }) {
|
|
73
|
+
setResponseStatus(event, status, statusText);
|
|
74
|
+
for (const [name, value] of headers) {
|
|
75
|
+
if (!WITHHELD_RESPONSE_HEADERS.has(name))
|
|
76
|
+
setResponseHeader(event, name, value);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export function resolveProxyRequest(event) {
|
|
80
|
+
const kirby = useRuntimeConfig(event).kirby;
|
|
81
|
+
const path = getRawPath(event);
|
|
82
|
+
if (!kirby.url) {
|
|
83
|
+
console.error("[nuxt-kirby] The Kirby URL is missing. Set `NUXT_KIRBY_URL` or the `url` option.");
|
|
84
|
+
throw createError({
|
|
85
|
+
statusCode: 500,
|
|
86
|
+
statusMessage: "Kirby URL is missing"
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (kirby.forwardCookies && getRequestHeader(event, "sec-fetch-site") === "cross-site") {
|
|
90
|
+
throw createError({
|
|
91
|
+
statusCode: 403,
|
|
92
|
+
statusMessage: "Cross-site requests are refused with cookie forwarding"
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
assertPathBelowBase(path);
|
|
96
|
+
const url = withQuery(joinURL(kirby.url, path), getQuery(event.node.req.url));
|
|
97
|
+
const routePath = resolveRoutePath(url, kirby.url);
|
|
98
|
+
assertAllowedRoute(kirby.allowedRoutes, routePath, event.method);
|
|
99
|
+
const headers = {};
|
|
100
|
+
for (const name of kirby.forwardCookies ? [...FORWARDED_REQUEST_HEADERS, "cookie"] : FORWARDED_REQUEST_HEADERS) {
|
|
101
|
+
const value = getRequestHeader(event, name);
|
|
102
|
+
if (value != null)
|
|
103
|
+
headers[name] = value;
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
url,
|
|
107
|
+
routePath,
|
|
108
|
+
headers: {
|
|
109
|
+
...headers,
|
|
110
|
+
...createAuthHeader(kirby)
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function getRawPath(event) {
|
|
115
|
+
const pathname = event.node.req.url.split("?", 1)[0];
|
|
116
|
+
const pathIndex = event.context.matchedRoute.path.split("/").indexOf("**:path");
|
|
117
|
+
return pathname.split("/").slice(pathIndex).join("/");
|
|
118
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ends the stream instead of failing it where its source fails after `signal` aborted. The aborted API
|
|
3
|
+
* response would otherwise surface as the error of a request nobody waits for.
|
|
4
|
+
*/
|
|
5
|
+
export declare function endOnAbort(stream: ReadableStream<Uint8Array>, signal: AbortSignal): ReadableStream<Uint8Array<ArrayBufferLike>>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export function endOnAbort(stream, signal) {
|
|
2
|
+
const reader = stream.getReader();
|
|
3
|
+
return new ReadableStream({
|
|
4
|
+
async pull(controller) {
|
|
5
|
+
try {
|
|
6
|
+
const { done, value } = await reader.read();
|
|
7
|
+
if (done)
|
|
8
|
+
controller.close();
|
|
9
|
+
else
|
|
10
|
+
controller.enqueue(value);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (signal.aborted)
|
|
13
|
+
controller.close();
|
|
14
|
+
else
|
|
15
|
+
controller.error(error);
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
cancel: (reason) => reader.cancel(reason)
|
|
19
|
+
});
|
|
20
|
+
}
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,19 +1,54 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { RouterMethod } from 'h3';
|
|
2
2
|
import type { NitroFetchOptions } from 'nitropack';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
export type
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
export declare const AUTH_METHODS: readonly ["basic", "bearer", "none"];
|
|
4
|
+
export type KirbyAuth = typeof AUTH_METHODS[number];
|
|
5
|
+
export type RequestMethod = Uppercase<RouterMethod> | RouterMethod;
|
|
6
|
+
export interface AllowedRoute {
|
|
7
|
+
/** Pathname below the Kirby URL, where `*` matches one segment and a trailing `**` any number of them. */
|
|
8
|
+
path: string;
|
|
9
|
+
/** Methods the route accepts; all of them when omitted. */
|
|
10
|
+
methods?: RequestMethod[];
|
|
11
|
+
}
|
|
12
|
+
export interface BasicCredentials {
|
|
13
|
+
username: string;
|
|
14
|
+
password: string;
|
|
15
|
+
}
|
|
16
|
+
export type KirbyFetchOptions = Omit<NitroFetchOptions<string>, 'baseURL'> & {
|
|
17
|
+
/**
|
|
18
|
+
* Language to ask the content in.
|
|
19
|
+
*
|
|
20
|
+
* @remarks
|
|
21
|
+
* Travels as the `X-Language` header, which Kirby reads on API routes. Kirby Headless 8.1 also
|
|
22
|
+
* reads it on the global-routes catch-all, but only for a path that names no language itself.
|
|
23
|
+
*/
|
|
24
|
+
language?: string;
|
|
9
25
|
};
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
26
|
+
export type KqlOptions = Omit<KirbyFetchOptions, 'method' | 'body' | 'query' | 'params'>;
|
|
27
|
+
/** The private runtime config, which `NUXT_KIRBY_*` variables override. */
|
|
28
|
+
export interface ServerKirbyConfig {
|
|
29
|
+
url: string;
|
|
30
|
+
kqlPath: string;
|
|
31
|
+
auth: KirbyAuth;
|
|
32
|
+
token: string;
|
|
33
|
+
credentials: BasicCredentials;
|
|
34
|
+
forwardCookies: boolean;
|
|
35
|
+
allowedRoutes: AllowedRoute[];
|
|
36
|
+
server: {
|
|
37
|
+
cache: boolean;
|
|
38
|
+
storage: string;
|
|
39
|
+
swr: boolean;
|
|
40
|
+
maxAge: number;
|
|
41
|
+
};
|
|
16
42
|
}
|
|
17
|
-
|
|
18
|
-
|
|
43
|
+
/**
|
|
44
|
+
* The public runtime config, which `NUXT_PUBLIC_KIRBY_*` variables override. The URL and the
|
|
45
|
+
* credentials are published in client mode only.
|
|
46
|
+
*/
|
|
47
|
+
export interface PublicKirbyConfig {
|
|
48
|
+
client: boolean;
|
|
49
|
+
kqlPath: string;
|
|
50
|
+
url?: string;
|
|
51
|
+
auth?: KirbyAuth;
|
|
52
|
+
token?: string;
|
|
53
|
+
credentials?: BasicCredentials;
|
|
19
54
|
}
|
package/dist/runtime/types.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const AUTH_METHODS = ["basic", "bearer", "none"];
|
package/dist/runtime/utils.d.ts
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* `Authorization` instead of being joined with it into one comma-separated value.
|
|
6
|
-
*/
|
|
7
|
-
export declare function createAuthHeader({ auth, token, credentials, }: {
|
|
8
|
-
auth?: string;
|
|
1
|
+
import type { BasicCredentials, KirbyAuth } from './types.js';
|
|
2
|
+
export declare function mergeHeaders(...headers: (HeadersInit | undefined)[]): Record<string, string>;
|
|
3
|
+
export declare function createAuthHeader({ auth, token, credentials }: {
|
|
4
|
+
auth?: KirbyAuth;
|
|
9
5
|
token?: string;
|
|
10
|
-
credentials?:
|
|
11
|
-
username: string;
|
|
12
|
-
password: string;
|
|
13
|
-
};
|
|
6
|
+
credentials?: BasicCredentials;
|
|
14
7
|
}): {
|
|
15
8
|
authorization: string;
|
|
16
9
|
} | undefined;
|
|
17
|
-
/**
|
|
18
|
-
* Builds the header Kirby reads the language from, lowercased so that it overwrites
|
|
19
|
-
* a caller's own `X-Language` instead of traveling alongside it as a second value.
|
|
20
|
-
*/
|
|
21
10
|
export declare function createLanguageHeader(language: string | undefined): {
|
|
22
11
|
'x-language': string;
|
|
23
12
|
} | undefined;
|
package/dist/runtime/utils.js
CHANGED
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
export function
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
export function createAuthHeader({
|
|
8
|
-
auth,
|
|
9
|
-
token,
|
|
10
|
-
credentials
|
|
11
|
-
}) {
|
|
12
|
-
if (auth === "basic" && credentials) {
|
|
13
|
-
const { username, password } = credentials;
|
|
14
|
-
const encoded = globalThis.btoa(`${username}:${password}`);
|
|
15
|
-
return { authorization: `Basic ${encoded}` };
|
|
1
|
+
export function mergeHeaders(...headers) {
|
|
2
|
+
const merged = {};
|
|
3
|
+
for (const source of headers) {
|
|
4
|
+
for (const [name, value] of new Headers(source))
|
|
5
|
+
merged[name] = value;
|
|
16
6
|
}
|
|
7
|
+
return merged;
|
|
8
|
+
}
|
|
9
|
+
export function createAuthHeader({ auth, token, credentials }) {
|
|
10
|
+
if (auth === "basic" && credentials)
|
|
11
|
+
return { authorization: `Basic ${globalThis.btoa(`${credentials.username}:${credentials.password}`)}` };
|
|
17
12
|
if (auth === "bearer")
|
|
18
13
|
return { authorization: `Bearer ${token}` };
|
|
19
14
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-kirby",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.0",
|
|
5
5
|
"description": "Kirby CMS data in Nuxt, via KQL or the REST API",
|
|
6
6
|
"author": "Johann Schopplich <hello@johannschopplich.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -32,16 +32,17 @@
|
|
|
32
32
|
"files": [
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"kirby-types": "^1.8.2 || ^2.0.0-0"
|
|
37
|
+
},
|
|
35
38
|
"dependencies": {
|
|
36
39
|
"@nuxt/kit": "^4.6.4",
|
|
37
40
|
"defu": "^6.1.7",
|
|
38
41
|
"destr": "^2.0.5",
|
|
39
42
|
"h3": "^1.15.11",
|
|
40
|
-
"kirby-types": "^1.8.2",
|
|
41
43
|
"ofetch": "^1.5.1",
|
|
42
44
|
"ohash": "^2.0.12",
|
|
43
45
|
"pathe": "^2.0.3",
|
|
44
|
-
"scule": "^1.3.0",
|
|
45
46
|
"ufo": "^1.6.4",
|
|
46
47
|
"uint8array-extras": "^1.6.0"
|
|
47
48
|
},
|
|
@@ -53,9 +54,9 @@
|
|
|
53
54
|
"@types/node": "^26.6.6",
|
|
54
55
|
"bumpp": "^12.3.0",
|
|
55
56
|
"eslint": "^10.14.0",
|
|
57
|
+
"kirby-types": "^1.8.2",
|
|
56
58
|
"nuxt": "^4.6.4",
|
|
57
59
|
"playwright-core": "^1.65.0",
|
|
58
|
-
"srvx": "^1.0.8",
|
|
59
60
|
"typescript": "^6.0.3",
|
|
60
61
|
"vitest": "^5.1.0"
|
|
61
62
|
},
|
package/dist/runtime/cache.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sends the request unless an identical one is already in flight, or has already put its response
|
|
3
|
-
* into the Nuxt payload.
|
|
4
|
-
*
|
|
5
|
-
* @remarks
|
|
6
|
-
* `payloadCache` decides what happens within one environment. Across the two, the payload is the
|
|
7
|
-
* only way an SSR response reaches the client, so the server always writes and the hydrating client
|
|
8
|
-
* always reads – otherwise turning the option off would cost every request a second round trip in
|
|
9
|
-
* the browser.
|
|
10
|
-
*/
|
|
11
|
-
export declare function sendCachedRequest<T>(requestKey: string, payloadCache: boolean, send: () => Promise<T>): Promise<T>;
|
package/dist/runtime/cache.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { useNuxtApp } from "#imports";
|
|
2
|
-
export function sendCachedRequest(requestKey, payloadCache, send) {
|
|
3
|
-
const nuxt = useNuxtApp();
|
|
4
|
-
const pendingRequests = nuxt._pendingRequests ||= /* @__PURE__ */ new Map();
|
|
5
|
-
const cachesPayload = import.meta.server || payloadCache;
|
|
6
|
-
const key = `raw:${requestKey}`;
|
|
7
|
-
if ((nuxt.isHydrating || payloadCache) && nuxt.payload.data[key])
|
|
8
|
-
return Promise.resolve(nuxt.payload.data[key]);
|
|
9
|
-
const pendingRequest = pendingRequests.get(key);
|
|
10
|
-
if (pendingRequest)
|
|
11
|
-
return pendingRequest;
|
|
12
|
-
const request = send().then((response) => {
|
|
13
|
-
if (cachesPayload)
|
|
14
|
-
nuxt.payload.data[key] = response;
|
|
15
|
-
return response;
|
|
16
|
-
}).catch((error) => {
|
|
17
|
-
if (cachesPayload)
|
|
18
|
-
nuxt.payload.data[key] = void 0;
|
|
19
|
-
throw error;
|
|
20
|
-
}).finally(() => {
|
|
21
|
-
pendingRequests.delete(key);
|
|
22
|
-
});
|
|
23
|
-
pendingRequests.set(key, request);
|
|
24
|
-
return request;
|
|
25
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type * from 'kirby-types';
|
|
File without changes
|
package/dist/runtime/path.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Throws a 400 for a path that a URL parser or the server behind the Kirby URL could read as absolute
|
|
3
|
-
* or as climbing above it: a scheme, an authority, a tab or line break, or a `..` segment, also
|
|
4
|
-
* percent-encoded or with a `;` parameter.
|
|
5
|
-
*/
|
|
6
|
-
export declare function assertPathBelowBase(path: string): void;
|
package/dist/runtime/path.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { createError } from "h3";
|
|
2
|
-
export function assertPathBelowBase(path) {
|
|
3
|
-
const segments = path.split("?", 1)[0].replace(/%2e/gi, ".").replace(/%3b/gi, ";").split(/[/\\#]|%2f|%5c/i);
|
|
4
|
-
if (/[\t\n\r\u2028\u2029]/.test(path) || /^[^/?#]*:/.test(path) || /^[\s\u0000-\u001F]*(?:[/\\]\s*){2}/.test(path) || segments.some((segment) => /^\.\.(?:;|[\u0000-\u0020]*$)/.test(segment))) {
|
|
5
|
-
throw createError({
|
|
6
|
-
statusCode: 400,
|
|
7
|
-
statusMessage: "Path must stay below the Kirby URL"
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { destr } from "destr";
|
|
2
|
-
import { createError, defineEventHandler, getRequestHeader, getRouterParam, readBody, setResponseHeader, setResponseStatus, splitCookiesString } from "h3";
|
|
3
|
-
import { defineCachedFunction } from "nitropack/runtime";
|
|
4
|
-
import { hash } from "ohash";
|
|
5
|
-
import { base64ToUint8Array, uint8ArrayToBase64, uint8ArrayToString } from "uint8array-extras";
|
|
6
|
-
import { useRuntimeConfig } from "#imports";
|
|
7
|
-
import { assertPathBelowBase } from "../path.js";
|
|
8
|
-
import { createAuthHeader, headersToObject } from "../utils.js";
|
|
9
|
-
const EXCLUDED_HEADERS = /* @__PURE__ */ new Set([
|
|
10
|
-
// Headers that describe a single connection rather than the message, so they end at the proxy.
|
|
11
|
-
"connection",
|
|
12
|
-
"keep-alive",
|
|
13
|
-
"proxy-authenticate",
|
|
14
|
-
"proxy-authorization",
|
|
15
|
-
"proxy-connection",
|
|
16
|
-
"te",
|
|
17
|
-
"trailer",
|
|
18
|
-
"transfer-encoding",
|
|
19
|
-
"upgrade",
|
|
20
|
-
// The body arrives decoded, so its original encoding and length no longer apply.
|
|
21
|
-
"content-encoding",
|
|
22
|
-
"content-length",
|
|
23
|
-
// Reduce information leakage.
|
|
24
|
-
"server",
|
|
25
|
-
"x-powered-by"
|
|
26
|
-
]);
|
|
27
|
-
async function fetchFromKirby(event, {
|
|
28
|
-
isQueryRequest,
|
|
29
|
-
query,
|
|
30
|
-
path,
|
|
31
|
-
headers,
|
|
32
|
-
method,
|
|
33
|
-
body,
|
|
34
|
-
forwardCookies
|
|
35
|
-
}) {
|
|
36
|
-
const kirby = useRuntimeConfig(event).kirby;
|
|
37
|
-
const cookie = forwardCookies ? getRequestHeader(event, "cookie") : void 0;
|
|
38
|
-
const response = await globalThis.$fetch.raw(isQueryRequest ? kirby.kqlPath : path, {
|
|
39
|
-
responseType: "arrayBuffer",
|
|
40
|
-
ignoreResponseError: true,
|
|
41
|
-
baseURL: kirby.url,
|
|
42
|
-
...isQueryRequest ? {
|
|
43
|
-
method: "POST",
|
|
44
|
-
body: query
|
|
45
|
-
} : {
|
|
46
|
-
method,
|
|
47
|
-
query,
|
|
48
|
-
body
|
|
49
|
-
},
|
|
50
|
-
headers: {
|
|
51
|
-
...headers,
|
|
52
|
-
...cookie ? { cookie } : {},
|
|
53
|
-
...createAuthHeader(kirby)
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
return {
|
|
57
|
-
status: response.status,
|
|
58
|
-
statusText: response.statusText,
|
|
59
|
-
headers: [
|
|
60
|
-
...response.headers.entries()
|
|
61
|
-
],
|
|
62
|
-
data: new Uint8Array(response._data ?? [])
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
function createCachedFetcher(kirby) {
|
|
66
|
-
return defineCachedFunction(
|
|
67
|
-
// Nitro reads the event from the first argument only, and hands the cache write to
|
|
68
|
-
// `event.waitUntil()` – without it an edge worker is torn down before the entry reaches storage.
|
|
69
|
-
async (event, options) => {
|
|
70
|
-
const { data, headers, ...rest } = await fetchFromKirby(event, options);
|
|
71
|
-
return {
|
|
72
|
-
...rest,
|
|
73
|
-
// A stored `set-cookie` would hand every later visitor on this key the session Kirby
|
|
74
|
-
// issued for the first one.
|
|
75
|
-
headers: headers.filter(([key]) => key !== "set-cookie"),
|
|
76
|
-
data: uint8ArrayToBase64(data)
|
|
77
|
-
};
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
name: "nuxt-kirby",
|
|
81
|
-
base: kirby.server.storage,
|
|
82
|
-
swr: kirby.server.swr,
|
|
83
|
-
maxAge: kirby.server.maxAge,
|
|
84
|
-
// Reading the caller's `key` instead would let one visitor plant a response under the key another reads.
|
|
85
|
-
getKey: (event, options) => hash(options)
|
|
86
|
-
}
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
let cachedFetcher;
|
|
90
|
-
export default defineEventHandler(async (event) => {
|
|
91
|
-
const kirby = useRuntimeConfig(event).kirby;
|
|
92
|
-
const body = await readBody(event) ?? {};
|
|
93
|
-
const key = decodeURIComponent(getRouterParam(event, "key"));
|
|
94
|
-
const isQueryRequest = key.startsWith("$kql");
|
|
95
|
-
if (isQueryRequest) {
|
|
96
|
-
if (!body.query?.query) {
|
|
97
|
-
throw createError({
|
|
98
|
-
statusCode: 400,
|
|
99
|
-
statusMessage: "KQL query is empty"
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
} else {
|
|
103
|
-
if (typeof body.path !== "string") {
|
|
104
|
-
throw createError({
|
|
105
|
-
statusCode: 400,
|
|
106
|
-
statusMessage: "Path must be a string"
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
if (body.method != null && typeof body.method !== "string") {
|
|
110
|
-
throw createError({
|
|
111
|
-
statusCode: 400,
|
|
112
|
-
statusMessage: "Method must be a string"
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
assertPathBelowBase(body.path);
|
|
116
|
-
}
|
|
117
|
-
let headers;
|
|
118
|
-
try {
|
|
119
|
-
headers = headersToObject(body.headers ?? void 0);
|
|
120
|
-
} catch {
|
|
121
|
-
throw createError({
|
|
122
|
-
statusCode: 400,
|
|
123
|
-
statusMessage: "Headers are invalid"
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
const forwardCookies = body.forwardCookies ?? kirby.forwardCookies;
|
|
127
|
-
try {
|
|
128
|
-
let response;
|
|
129
|
-
const request = {
|
|
130
|
-
...body,
|
|
131
|
-
headers,
|
|
132
|
-
isQueryRequest,
|
|
133
|
-
forwardCookies
|
|
134
|
-
};
|
|
135
|
-
if (kirby.server.cache && !forwardCookies) {
|
|
136
|
-
cachedFetcher ??= createCachedFetcher(kirby);
|
|
137
|
-
const cached = await cachedFetcher(event, request);
|
|
138
|
-
response = {
|
|
139
|
-
...cached,
|
|
140
|
-
data: base64ToUint8Array(cached.data)
|
|
141
|
-
};
|
|
142
|
-
} else {
|
|
143
|
-
response = await fetchFromKirby(event, request);
|
|
144
|
-
}
|
|
145
|
-
const dataArray = response.data;
|
|
146
|
-
if (response.status >= 400 && response.status < 600) {
|
|
147
|
-
if (isQueryRequest) {
|
|
148
|
-
console.error(`[nuxt-kirby] Failed KQL query "${body.query?.query}" (...) with status code ${response.status}:
|
|
149
|
-
`, destr(
|
|
150
|
-
uint8ArrayToString(dataArray)
|
|
151
|
-
));
|
|
152
|
-
if (kirby.server.verboseErrors)
|
|
153
|
-
console.error("[nuxt-kirby] Full KQL query request:", body.query);
|
|
154
|
-
} else {
|
|
155
|
-
console.error(`[nuxt-kirby] Failed ${(body.method || "get").toUpperCase()} request to "${body.path}"`);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
const cookies = [];
|
|
159
|
-
for (const [key2, value] of response.headers) {
|
|
160
|
-
if (EXCLUDED_HEADERS.has(key2))
|
|
161
|
-
continue;
|
|
162
|
-
if (key2 === "set-cookie") {
|
|
163
|
-
cookies.push(...splitCookiesString(value));
|
|
164
|
-
continue;
|
|
165
|
-
}
|
|
166
|
-
setResponseHeader(event, key2, value);
|
|
167
|
-
}
|
|
168
|
-
if (cookies.length > 0)
|
|
169
|
-
setResponseHeader(event, "set-cookie", cookies);
|
|
170
|
-
setResponseStatus(event, response.status, response.statusText);
|
|
171
|
-
return dataArray;
|
|
172
|
-
} catch (error) {
|
|
173
|
-
console.error("[nuxt-kirby]", error);
|
|
174
|
-
throw createError({
|
|
175
|
-
statusCode: 503,
|
|
176
|
-
statusMessage: "Service Unavailable"
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
});
|