eddev 2.0.0-beta.11 → 2.0.0-beta.111
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/app/entry/HydrationOverlay.d.ts +1 -0
- package/dist/app/entry/HydrationOverlay.js +2 -0
- package/dist/app/entry/MetaTags.d.ts +7 -0
- package/dist/app/entry/MetaTags.js +17 -0
- package/dist/app/entry/boot-admin.js +8 -3
- package/dist/app/entry/hydration-script.d.ts +1 -0
- package/dist/app/entry/hydration-script.js +18 -0
- package/dist/app/entry/spa-root.js +3 -5
- package/dist/app/entry/ssr-root-client.d.ts +3 -1
- package/dist/app/entry/ssr-root-client.js +17 -6
- package/dist/app/entry/ssr-root.d.ts +3 -4
- package/dist/app/entry/ssr-root.js +19 -19
- package/dist/app/lib/admin/index.d.ts +2 -2
- package/dist/app/lib/admin/index.js +2 -2
- package/dist/app/lib/admin/installFieldTypes.js +1 -1
- package/dist/app/lib/admin/runWidgets.js +1 -1
- package/dist/app/lib/blocks/ContentBlocks.d.ts +1 -1
- package/dist/app/lib/blocks/ContentBlocks.js +5 -5
- package/dist/app/lib/blocks/EditableText.d.ts +1 -1
- package/dist/app/lib/blocks/EditableText.js +3 -3
- package/dist/app/lib/blocks/InnerBlocks.d.ts +13 -3
- package/dist/app/lib/blocks/InnerBlocks.js +13 -5
- package/dist/app/lib/blocks/block-utils.d.ts +2 -2
- package/dist/app/lib/blocks/block-utils.js +2 -2
- package/dist/app/lib/blocks/editor/EditorHighlights.d.ts +7 -0
- package/dist/app/lib/blocks/editor/EditorHighlights.js +130 -0
- package/dist/app/lib/blocks/editor/EditorSupport.js +4 -5
- package/dist/app/lib/blocks/editor/ErrorBoundaryEditor.d.ts +1 -1
- package/dist/app/lib/blocks/editor/editor-config.d.ts +17 -4
- package/dist/app/lib/blocks/editor/editor-config.js +24 -10
- package/dist/app/lib/blocks/editor/installGutenbergHooks.js +20 -14
- package/dist/app/lib/blocks/editor/root-blocks.d.ts +6 -0
- package/dist/app/lib/blocks/editor/root-blocks.js +30 -0
- package/dist/app/lib/blocks/editor/usePostEditor.d.ts +1 -1
- package/dist/app/lib/blocks/index.d.ts +9 -9
- package/dist/app/lib/blocks/index.js +9 -9
- package/dist/app/lib/blocks/inline-editing.d.ts +1 -1
- package/dist/app/lib/blocks/inline-editing.js +7 -5
- package/dist/app/lib/devtools/components/BreakpointIndicator.js +1 -1
- package/dist/app/lib/devtools/components/DevUI.js +4 -3
- package/dist/app/lib/devtools/components/GridIndicator.d.ts +1 -0
- package/dist/app/lib/devtools/components/GridIndicator.js +29 -0
- package/dist/app/lib/devtools/hooks/usePersistState.d.ts +1 -1
- package/dist/app/lib/devtools/hooks/usePersistState.js +11 -2
- package/dist/app/lib/devtools/hooks/useTailwind.d.ts +2305 -1261
- package/dist/app/lib/devtools/hooks/useTailwind.js +1 -1
- package/dist/app/lib/devtools/index.d.ts +1 -1
- package/dist/app/lib/devtools/index.js +1 -1
- package/dist/app/lib/devtools/loader.js +8 -7
- package/dist/app/lib/devtools/useQueryDebug.d.ts +7 -1
- package/dist/app/lib/devtools/useQueryDebug.js +5 -8
- package/dist/app/lib/dynamic/dynamic.d.ts +1 -1
- package/dist/app/lib/dynamic/dynamic.js +5 -1
- package/dist/app/lib/dynamic/index.d.ts +1 -1
- package/dist/app/lib/dynamic/index.js +1 -1
- package/dist/app/lib/hooks/index.d.ts +4 -5
- package/dist/app/lib/hooks/index.js +4 -5
- package/dist/app/lib/hooks/queryUtils.d.ts +37 -3
- package/dist/app/lib/hooks/queryUtils.js +66 -26
- package/dist/app/lib/hooks/useAppData.js +1 -1
- package/dist/app/lib/hooks/useRPC.d.ts +0 -4
- package/dist/app/lib/hooks/useRPC.js +1 -8
- package/dist/app/lib/internal/finalize-rpc.d.ts +17 -0
- package/dist/app/lib/internal/finalize-rpc.js +3 -0
- package/dist/app/lib/internal/index.d.ts +5 -4
- package/dist/app/lib/internal/index.js +5 -4
- package/dist/app/lib/internal/read-admin-manifest.d.ts +1 -1
- package/dist/app/lib/legacy-stitches/createStitches.d.ts +21 -21
- package/dist/app/lib/legacy-stitches/createStitches.js +1 -1
- package/dist/app/lib/legacy-stitches/index.d.ts +1 -1
- package/dist/app/lib/legacy-stitches/index.js +1 -1
- package/dist/app/lib/routing/components/BackButton.d.ts +49 -0
- package/dist/app/lib/routing/components/BackButton.js +47 -0
- package/dist/app/lib/routing/components/BrowserRouter.d.ts +4 -1
- package/dist/app/lib/routing/components/BrowserRouter.js +95 -19
- package/dist/app/lib/routing/components/ClientOnly.d.ts +1 -1
- package/dist/app/lib/routing/components/ClientOnly.js +2 -2
- package/dist/app/lib/routing/components/Link.d.ts +1 -0
- package/dist/app/lib/routing/components/Link.js +11 -12
- package/dist/app/lib/routing/components/RouteRenderer.d.ts +1 -1
- package/dist/app/lib/routing/components/RouteRenderer.js +7 -6
- package/dist/app/lib/routing/components/SSRRouter.d.ts +2 -2
- package/dist/app/lib/routing/components/SSRRouter.js +5 -6
- package/dist/app/lib/routing/components/ScrollRestoration.js +5 -2
- package/dist/app/lib/routing/context.d.ts +8 -5
- package/dist/app/lib/routing/context.js +13 -96
- package/dist/app/lib/routing/hooks/useRestorableState.d.ts +2 -1
- package/dist/app/lib/routing/hooks/useRestorableState.js +2 -1
- package/dist/app/lib/routing/hooks/useRoute.d.ts +16 -1
- package/dist/app/lib/routing/hooks/useRoute.js +22 -1
- package/dist/app/lib/routing/hooks/useRouteMeta.d.ts +5 -0
- package/dist/app/lib/routing/hooks/useRouteMeta.js +9 -0
- package/dist/app/lib/routing/hooks/useRouteTransition.d.ts +1 -1
- package/dist/app/lib/routing/hooks/useRouteTransition.js +1 -1
- package/dist/app/lib/routing/hooks/useRouter.d.ts +1 -1
- package/dist/app/lib/routing/hooks/useRouter.js +1 -1
- package/dist/app/lib/routing/hooks/useRouterEvents.d.ts +1 -1
- package/dist/app/lib/routing/hooks/useRouterEvents.js +1 -1
- package/dist/app/lib/routing/hooks/useRouterState.d.ts +1 -1
- package/dist/app/lib/routing/hooks/useRouterState.js +1 -1
- package/dist/app/lib/routing/hooks/useSearchParams.js +2 -2
- package/dist/app/lib/routing/index.d.ts +14 -13
- package/dist/app/lib/routing/index.js +14 -13
- package/dist/app/lib/routing/loader.d.ts +2 -2
- package/dist/app/lib/routing/loader.js +20 -11
- package/dist/app/lib/routing/types.d.ts +37 -10
- package/dist/app/lib/routing/utils.d.ts +5 -2
- package/dist/app/lib/routing/utils.js +37 -4
- package/dist/app/lib/{hooks → runtime}/apiConfig.d.ts +6 -2
- package/dist/app/lib/runtime/apiConfig.js +6 -0
- package/dist/app/lib/runtime/errorHandling.d.ts +39 -0
- package/dist/app/lib/runtime/errorHandling.js +6 -0
- package/dist/app/lib/runtime/index.d.ts +2 -0
- package/dist/app/lib/runtime/index.js +2 -0
- package/dist/app/lib/views/index.d.ts +1 -1
- package/dist/app/lib/views/index.js +1 -1
- package/dist/app/server/defineRouter.d.ts +2 -0
- package/dist/app/server/defineRouter.js +4 -0
- package/dist/app/server/index.d.ts +5 -3
- package/dist/app/server/index.js +5 -3
- package/dist/app/server/proxy-wp-admin.d.ts +1 -2
- package/dist/app/server/proxy-wp-admin.js +41 -14
- package/dist/app/server/render-ssr-page.d.ts +27 -2
- package/dist/app/server/render-ssr-page.js +191 -12
- package/dist/app/server/rpc.d.ts +56 -0
- package/dist/app/server/rpc.js +18 -0
- package/dist/app/server/server-context.d.ts +42 -4
- package/dist/app/server/server-context.js +195 -28
- package/dist/app/server/utils/replace-host.d.ts +1 -1
- package/dist/app/server/utils/replace-host.js +10 -2
- package/dist/app/server/utils/swr-cache.d.ts +4 -0
- package/dist/app/server/utils/swr-cache.js +31 -0
- package/dist/app/utils/APIProvider.d.ts +2 -0
- package/dist/app/utils/APIProvider.js +5 -0
- package/dist/app/utils/BlockErrorBoundary.d.ts +19 -0
- package/dist/app/utils/BlockErrorBoundary.js +38 -0
- package/dist/app/utils/ErrorMessage.d.ts +5 -0
- package/dist/app/utils/ErrorMessage.js +14 -0
- package/dist/app/utils/RouteErrorBoundary.d.ts +18 -0
- package/dist/app/utils/RouteErrorBoundary.js +38 -0
- package/dist/app/utils/asset-capture.d.ts +2 -0
- package/dist/app/utils/asset-capture.js +5 -0
- package/dist/app/utils/hydration-debugger.d.ts +13 -0
- package/dist/app/utils/hydration-debugger.js +11 -0
- package/dist/app/utils/query-client.d.ts +2 -0
- package/dist/app/utils/query-client.js +5 -1
- package/dist/app/utils/trpc-client.d.ts +2 -0
- package/dist/app/utils/trpc-client.js +39 -0
- package/dist/node/cli/cli-worker.js +10 -5
- package/dist/node/cli/cli.js +79 -11
- package/dist/node/cli/display/CLIApp.js +3 -6
- package/dist/node/cli/display/boot-cli-app.js +1 -1
- package/dist/node/cli/display/tools/CreateBlock.d.ts +1 -1
- package/dist/node/cli/display/tools/cli-tools.d.ts +1 -11
- package/dist/node/cli/display/tools/cli-tools.js +9 -9
- package/dist/node/cli/version.d.ts +1 -1
- package/dist/node/cli/version.js +1 -1
- package/dist/node/compiler/build-vinxi.js +2 -1
- package/dist/node/compiler/bundler.admin.d.ts +1 -1
- package/dist/node/compiler/bundler.admin.js +1 -1
- package/dist/node/compiler/bundler.frontend.js +1 -1
- package/dist/node/compiler/dev-server.js +10 -0
- package/dist/node/compiler/get-vite-config.d.ts +2 -0
- package/dist/node/compiler/get-vite-config.js +42 -12
- package/dist/node/compiler/vinxi-app.d.ts +12 -0
- package/dist/node/compiler/vinxi-app.js +139 -32
- package/dist/node/compiler/vinxi-codegen.js +342 -112
- package/dist/node/graphql/graphql-codegen.d.ts +11 -1
- package/dist/node/graphql/graphql-codegen.js +210 -33
- package/dist/node/graphql/graphql-schema-loader.d.ts +2 -1
- package/dist/node/graphql/graphql-schema-loader.js +5 -16
- package/dist/node/graphql/plugins/gql-plugin-queries.js +1 -1
- package/dist/node/graphql/query-files-loader.d.ts +3 -0
- package/dist/node/graphql/query-files-loader.js +5 -0
- package/dist/node/project/config.d.ts +159 -73
- package/dist/node/project/config.js +69 -20
- package/dist/node/project/env.d.ts +4 -0
- package/dist/node/project/env.js +1 -0
- package/dist/node/project/manifest/block-manifest.js +2 -1
- package/dist/node/project/manifest/manifest.d.ts +1 -0
- package/dist/node/project/manifest/manifest.js +14 -10
- package/dist/node/project/manifest/routes-manifest.d.ts +20 -0
- package/dist/node/project/manifest/routes-manifest.js +74 -0
- package/dist/node/project/manifest/view-manifest.js +1 -1
- package/dist/node/project/project.d.ts +11 -1
- package/dist/node/project/project.js +61 -7
- package/dist/node/project/wp-info.d.ts +1 -0
- package/dist/node/project/wp-info.js +13 -1
- package/dist/node/storybook/index.d.ts +2 -0
- package/dist/node/storybook/index.js +13 -0
- package/dist/node/types/block-type.d.ts +25 -20
- package/dist/node/types/block-type.js +1 -0
- package/dist/node/types/view-type.d.ts +7 -7
- package/dist/node/utils/fetch-wp.d.ts +1 -0
- package/dist/node/utils/fetch-wp.js +27 -0
- package/dist/node/utils/fs-codegen.d.ts +2 -0
- package/dist/node/utils/fs-codegen.js +2 -1
- package/dist/node/utils/is-deploying.js +1 -1
- package/dist/node/utils/report-builder.d.ts +6 -6
- package/dist/node/utils/self-signed-cert.d.ts +2 -0
- package/dist/node/utils/self-signed-cert.js +10 -2
- package/dist/node/utils/stateful-log.js +2 -0
- package/dist/node/utils/watch-file-tree.d.ts +17 -3
- package/dist/node/utils/watch-file-tree.js +12 -5
- package/package.json +21 -12
- package/types.app.d.ts +4 -2
- package/types.app.internal.d.ts +2 -2
- package/types.node.d.ts +3 -3
- package/dist/app/lib/blocks/ErrorBoundaryFrontend.d.ts +0 -15
- package/dist/app/lib/blocks/ErrorBoundaryFrontend.js +0 -35
- package/dist/app/lib/hooks/apiConfig.js +0 -4
- package/dist/app/lib/hooks/usePageLoad.d.ts +0 -6
- package/dist/app/lib/hooks/usePageLoad.js +0 -5
|
@@ -1,2 +1,27 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { RouteDataWithTrackers } from "../lib/routing/types.js";
|
|
2
|
+
type SSRArgs = {
|
|
3
|
+
pathname: string;
|
|
4
|
+
initialData: RouteDataWithTrackers;
|
|
5
|
+
};
|
|
6
|
+
export declare function getSsrStream(args: SSRArgs): Promise<ReadableStream<Uint8Array<ArrayBufferLike>>>;
|
|
7
|
+
type RenderArgs = {
|
|
8
|
+
pathname: string;
|
|
9
|
+
statusCode?: number;
|
|
10
|
+
newOrigin?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function renderPage(args: RenderArgs): Promise<Response>;
|
|
13
|
+
type RenderErrorPageArgs = {
|
|
14
|
+
pathname: string;
|
|
15
|
+
code: number;
|
|
16
|
+
title: string;
|
|
17
|
+
newOrigin?: string;
|
|
18
|
+
realError?: Error;
|
|
19
|
+
};
|
|
20
|
+
export declare function renderErrorPage(args: RenderErrorPageArgs): Promise<Response>;
|
|
21
|
+
type Asset = {
|
|
22
|
+
tag: string;
|
|
23
|
+
attrs: Record<string, string | boolean>;
|
|
24
|
+
children?: string;
|
|
25
|
+
};
|
|
26
|
+
export declare function renderAsset({ tag, attrs, children }: Asset): string;
|
|
27
|
+
export {};
|
|
@@ -1,22 +1,201 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { getManifest } from "vinxi/manifest";
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { renderToPipeableStream, renderToString } from "react-dom/server";
|
|
3
|
+
import { Writable } from "stream";
|
|
4
|
+
import { MetaTags } from "../entry/MetaTags.js";
|
|
6
5
|
import { SSRRoot } from "../entry/ssr-root.js";
|
|
7
6
|
import { RouteLoader } from "../lib/routing/loader.js";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
7
|
+
import { AssetCaptureContext } from "../utils/asset-capture.js";
|
|
8
|
+
import { ServerContext } from "./server-context.js";
|
|
9
|
+
import { Suspense } from "react";
|
|
10
|
+
export async function getSsrStream(args) {
|
|
11
|
+
const clientManifest = ServerContext.main.runtime.getManifest("client");
|
|
12
|
+
const preloadAssets = new Set([clientManifest.handler]);
|
|
13
|
+
const includedAssets = new Set();
|
|
14
|
+
const jsx = (_jsx(Suspense, { children: _jsx(AssetCaptureContext.Provider, { value: preloadAssets, children: _jsx(SSRRoot, { metaTags: args.initialData?.meta?.head || [], pathname: args.pathname, initialData: args.initialData, loader: new RouteLoader() }) }) }));
|
|
15
|
+
const stream = await new Promise(async (resolve, reject) => {
|
|
16
|
+
// console.log("Rendering to pipable")
|
|
13
17
|
const stream = renderToPipeableStream(jsx, {
|
|
14
18
|
onShellReady() {
|
|
19
|
+
// console.log("Shell ready")
|
|
15
20
|
resolve(stream);
|
|
16
21
|
},
|
|
22
|
+
onShellError(err) {
|
|
23
|
+
console.log("onShellError", err);
|
|
24
|
+
reject(err);
|
|
25
|
+
},
|
|
26
|
+
onError(err, errInfo) {
|
|
27
|
+
console.log("Error occurred after shell ready", err, errInfo);
|
|
28
|
+
console.error(err);
|
|
29
|
+
},
|
|
17
30
|
bootstrapModules: [clientManifest.inputs[clientManifest.handler].output.path],
|
|
18
|
-
bootstrapScriptContent:
|
|
31
|
+
bootstrapScriptContent: `\nwindow.manifest = ${JSON.stringify(await clientManifest.json())};\nwindow._PAGE_DATA = ${JSON.stringify({
|
|
32
|
+
...args.initialData,
|
|
33
|
+
trackers: undefined,
|
|
34
|
+
})};\n`,
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
const [initial, middle, end] = renderToString(_jsxs("html", { lang: "en", children: [_jsxs("head", { children: ["%%$%%", _jsx(MetaTags, { tags: args.initialData.meta?.head ?? [] }), "%%trackers_head%%"] }), _jsxs("body", { children: ["%%trackers_body%%", _jsx("div", { id: "root", children: "%%$%%" }), "%%trackers_footer%%"] })] }))
|
|
38
|
+
.replace(/%%trackers_(head|body|footer)%%/g, (_, name) => {
|
|
39
|
+
return args.initialData.trackers?.[name] || "";
|
|
40
|
+
})
|
|
41
|
+
.split("%%$%%");
|
|
42
|
+
return new ReadableStream({
|
|
43
|
+
async start(controller) {
|
|
44
|
+
controller.enqueue(new TextEncoder().encode(initial));
|
|
45
|
+
const inner = await new Promise((resolve, reject) => {
|
|
46
|
+
let buffer = [];
|
|
47
|
+
const writableStream = new Writable({
|
|
48
|
+
emitClose: true,
|
|
49
|
+
write(chunk, encoding, callback) {
|
|
50
|
+
buffer.push(chunk);
|
|
51
|
+
// controller.enqueue(chunk)
|
|
52
|
+
callback();
|
|
53
|
+
},
|
|
54
|
+
final(callback) {
|
|
55
|
+
resolve(buffer);
|
|
56
|
+
// controller.close()
|
|
57
|
+
callback();
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
stream.pipe(writableStream);
|
|
61
|
+
});
|
|
62
|
+
for (const input of preloadAssets) {
|
|
63
|
+
const assets = await clientManifest.inputs[input].assets();
|
|
64
|
+
// controller.enqueue(new TextEncoder().encode(`\n<!-- for ${input} -->`))
|
|
65
|
+
for (const _asset of assets) {
|
|
66
|
+
const asset = _asset;
|
|
67
|
+
// Ensure that each asset is only included once
|
|
68
|
+
const key = asset?.attrs?.key;
|
|
69
|
+
if (!key) {
|
|
70
|
+
throw new Error("Asset must have a key: " + JSON.stringify(asset));
|
|
71
|
+
}
|
|
72
|
+
if (includedAssets.has(key))
|
|
73
|
+
continue;
|
|
74
|
+
includedAssets.add(key);
|
|
75
|
+
// Render the asset
|
|
76
|
+
try {
|
|
77
|
+
const tag = renderAsset(asset);
|
|
78
|
+
controller.enqueue(new TextEncoder().encode(tag + "\n"));
|
|
79
|
+
}
|
|
80
|
+
catch (err) {
|
|
81
|
+
controller.enqueue(new TextEncoder().encode(`\n<!-- ${JSON.stringify(err)} -->\n`));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// console.log("ASSETS", await clientManifest)
|
|
86
|
+
controller.enqueue(new TextEncoder().encode(middle));
|
|
87
|
+
for (const chunk of inner) {
|
|
88
|
+
controller.enqueue(chunk);
|
|
89
|
+
}
|
|
90
|
+
controller.enqueue(new TextEncoder().encode(end));
|
|
91
|
+
// console.log("Assets", dynamicAssets)
|
|
92
|
+
controller.close();
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
export async function renderPage(args) {
|
|
97
|
+
const serverContext = ServerContext.main;
|
|
98
|
+
let headers = new Headers();
|
|
99
|
+
let responseInit = {
|
|
100
|
+
headers,
|
|
101
|
+
};
|
|
102
|
+
try {
|
|
103
|
+
const [{ appData, trackers }, response] = await Promise.all([
|
|
104
|
+
serverContext.fetchAppData(),
|
|
105
|
+
serverContext.fetchRouteData({
|
|
106
|
+
pathname: args.pathname,
|
|
107
|
+
newOrigin: args.newOrigin,
|
|
108
|
+
}),
|
|
109
|
+
]);
|
|
110
|
+
let data;
|
|
111
|
+
try {
|
|
112
|
+
data = await response.json();
|
|
113
|
+
data.appData = appData;
|
|
114
|
+
data.trackers = trackers;
|
|
115
|
+
responseInit.status = response.status;
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
console.error(err);
|
|
119
|
+
return renderErrorPage({
|
|
120
|
+
pathname: args.pathname,
|
|
121
|
+
code: 500,
|
|
122
|
+
newOrigin: args.newOrigin,
|
|
123
|
+
realError: err,
|
|
124
|
+
title: "Internal Server Error",
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (data.redirect) {
|
|
128
|
+
return new Response(null, {
|
|
129
|
+
status: data.status || 302,
|
|
130
|
+
headers: {
|
|
131
|
+
Location: data.redirect,
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
headers.set("Content-Type", "text/html; charset=utf-8");
|
|
136
|
+
const stream = await getSsrStream({
|
|
137
|
+
...args,
|
|
138
|
+
initialData: data,
|
|
139
|
+
});
|
|
140
|
+
return new Response(stream, responseInit);
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
console.error(err);
|
|
144
|
+
if (args.statusCode !== 500) {
|
|
145
|
+
return renderErrorPage({
|
|
146
|
+
pathname: args.pathname,
|
|
147
|
+
code: 500,
|
|
148
|
+
newOrigin: args.newOrigin,
|
|
149
|
+
realError: err,
|
|
150
|
+
title: "Internal Server Error",
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
return new Response('<!DOCTYPE html><html><head><title>500 Internal Server Error</title></head><body><h1>500 Internal Server Error</h1><p>"' +
|
|
154
|
+
String(err) +
|
|
155
|
+
'"</p></body></html>', {
|
|
156
|
+
status: 500,
|
|
157
|
+
statusText: "Internal Server Error",
|
|
158
|
+
headers: {
|
|
159
|
+
"Content-Type": "text/html; charset=utf-8",
|
|
160
|
+
},
|
|
19
161
|
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
export async function renderErrorPage(args) {
|
|
165
|
+
return renderPage({
|
|
166
|
+
pathname: "/_error",
|
|
167
|
+
statusCode: args.code,
|
|
168
|
+
newOrigin: args.newOrigin,
|
|
20
169
|
});
|
|
21
|
-
|
|
170
|
+
}
|
|
171
|
+
export function renderAsset({ tag, attrs, children }) {
|
|
172
|
+
const formatAttributes = (attributes) => {
|
|
173
|
+
return Object.entries(attributes)
|
|
174
|
+
.map(([attr, value]) => {
|
|
175
|
+
if (value === true) {
|
|
176
|
+
return attr;
|
|
177
|
+
}
|
|
178
|
+
else if (value !== false && value != null) {
|
|
179
|
+
return `${attr}="${String(value).replace(/"/g, """)}"`;
|
|
180
|
+
}
|
|
181
|
+
return "";
|
|
182
|
+
})
|
|
183
|
+
.filter(Boolean)
|
|
184
|
+
.join(" ");
|
|
185
|
+
};
|
|
186
|
+
switch (tag) {
|
|
187
|
+
case "script":
|
|
188
|
+
if (attrs.src) {
|
|
189
|
+
return `<script ${formatAttributes(attrs)}></script>`;
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
return `<script ${formatAttributes(attrs)}>${children ?? ""}</script>`;
|
|
193
|
+
}
|
|
194
|
+
case "link":
|
|
195
|
+
return `<link ${formatAttributes(attrs)} />`;
|
|
196
|
+
case "style":
|
|
197
|
+
return `<style ${formatAttributes(attrs)}>${children ?? ""}</style>`;
|
|
198
|
+
default:
|
|
199
|
+
return "";
|
|
200
|
+
}
|
|
22
201
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { AnyProcedure } from "@trpc/server";
|
|
2
|
+
import { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch";
|
|
3
|
+
export declare const rpcApi: {
|
|
4
|
+
_config: import("@trpc/server/unstable-core-do-not-import").RootConfig<{
|
|
5
|
+
ctx: RPCContext;
|
|
6
|
+
meta: object;
|
|
7
|
+
errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape;
|
|
8
|
+
transformer: true;
|
|
9
|
+
}>;
|
|
10
|
+
procedure: import("@trpc/server/unstable-core-do-not-import").ProcedureBuilder<RPCContext, object, object, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, false>;
|
|
11
|
+
middleware: <$ContextOverrides>(fn: import("@trpc/server/unstable-core-do-not-import").MiddlewareFunction<RPCContext, object, object, $ContextOverrides, unknown>) => import("@trpc/server/unstable-core-do-not-import").MiddlewareBuilder<RPCContext, object, $ContextOverrides, unknown>;
|
|
12
|
+
router: {
|
|
13
|
+
<TInput extends import("@trpc/server").RouterRecord>(input: TInput): import("@trpc/server/unstable-core-do-not-import").BuiltRouter<{
|
|
14
|
+
ctx: RPCContext;
|
|
15
|
+
meta: object;
|
|
16
|
+
errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape;
|
|
17
|
+
transformer: true;
|
|
18
|
+
}, TInput>;
|
|
19
|
+
<TInput extends import("@trpc/server/unstable-core-do-not-import").CreateRouterOptions>(input: TInput): import("@trpc/server/unstable-core-do-not-import").BuiltRouter<{
|
|
20
|
+
ctx: RPCContext;
|
|
21
|
+
meta: object;
|
|
22
|
+
errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape;
|
|
23
|
+
transformer: true;
|
|
24
|
+
}, import("@trpc/server/unstable-core-do-not-import").DecorateCreateRouterOptions<TInput>>;
|
|
25
|
+
};
|
|
26
|
+
mergeRouters: typeof import("@trpc/server/unstable-core-do-not-import").mergeRouters;
|
|
27
|
+
createCallerFactory: <TRecord extends import("@trpc/server").RouterRecord>(router: Pick<import("@trpc/server/unstable-core-do-not-import").Router<{
|
|
28
|
+
ctx: RPCContext;
|
|
29
|
+
meta: object;
|
|
30
|
+
errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape;
|
|
31
|
+
transformer: true;
|
|
32
|
+
}, TRecord>, "_def">) => import("@trpc/server/unstable-core-do-not-import").RouterCaller<{
|
|
33
|
+
ctx: RPCContext;
|
|
34
|
+
meta: object;
|
|
35
|
+
errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape;
|
|
36
|
+
transformer: true;
|
|
37
|
+
}, TRecord>;
|
|
38
|
+
};
|
|
39
|
+
type RouterSchema = Record<string, Record<string, AnyProcedure> | AnyProcedure>;
|
|
40
|
+
export declare const rpc: {
|
|
41
|
+
middleware: <$ContextOverrides>(fn: import("@trpc/server/unstable-core-do-not-import").MiddlewareFunction<RPCContext, object, object, $ContextOverrides, unknown>) => import("@trpc/server/unstable-core-do-not-import").MiddlewareBuilder<RPCContext, object, $ContextOverrides, unknown>;
|
|
42
|
+
procedure: import("@trpc/server/unstable-core-do-not-import").ProcedureBuilder<RPCContext, object, object, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, typeof import("@trpc/server/unstable-core-do-not-import").unsetMarker, false>;
|
|
43
|
+
router<T extends RouterSchema>(routes: T): T;
|
|
44
|
+
};
|
|
45
|
+
export declare function defineServerContext<T>(func: (opts: FetchCreateContextFnOptions) => T): (opts: FetchCreateContextFnOptions) => T;
|
|
46
|
+
export declare function instantiateRouter<T extends RouterSchema>(routes: T): import("@trpc/server/unstable-core-do-not-import").BuiltRouter<{
|
|
47
|
+
ctx: RPCContext;
|
|
48
|
+
meta: object;
|
|
49
|
+
errorShape: import("@trpc/server/unstable-core-do-not-import").DefaultErrorShape;
|
|
50
|
+
transformer: true;
|
|
51
|
+
}, T>;
|
|
52
|
+
export type InferRPCServerContext<TFunc extends null | ((opts: FetchCreateContextFnOptions) => any)> = {
|
|
53
|
+
req: Request;
|
|
54
|
+
resHeaders: Headers;
|
|
55
|
+
} & (TFunc extends (opts: FetchCreateContextFnOptions) => infer T ? T : {});
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { initTRPC } from "@trpc/server";
|
|
2
|
+
import superjson from "superjson";
|
|
3
|
+
export const rpcApi = initTRPC.context().create({
|
|
4
|
+
transformer: superjson,
|
|
5
|
+
});
|
|
6
|
+
export const rpc = {
|
|
7
|
+
middleware: rpcApi.middleware,
|
|
8
|
+
procedure: rpcApi.procedure,
|
|
9
|
+
router(routes) {
|
|
10
|
+
return routes;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
export function defineServerContext(func) {
|
|
14
|
+
return func;
|
|
15
|
+
}
|
|
16
|
+
export function instantiateRouter(routes) {
|
|
17
|
+
return rpcApi.router(routes);
|
|
18
|
+
}
|
|
@@ -1,25 +1,47 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Manifest } from "vinxi/dist/types/types/manifest";
|
|
2
2
|
import { RequestHeaders } from "vinxi/http";
|
|
3
|
+
import type { EDConfig } from "../../node/project/config.js";
|
|
4
|
+
import { TrackerTags } from "../lib/routing/types.js";
|
|
5
|
+
import { UrlReplacerConf } from "./utils/replace-host.js";
|
|
3
6
|
export type ServerContextArgs = {
|
|
4
7
|
dev: boolean;
|
|
5
8
|
origin: string;
|
|
6
9
|
replaceUrls?: UrlReplacerConf;
|
|
10
|
+
rpcBases?: string[];
|
|
11
|
+
config: EDConfig;
|
|
12
|
+
};
|
|
13
|
+
export type ServerContextRuntime = {
|
|
14
|
+
getManifest: (name: string) => Manifest;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Data returned from WordPress origin /_appdata route
|
|
18
|
+
*/
|
|
19
|
+
type ServerAppData = {
|
|
20
|
+
appData: any;
|
|
21
|
+
trackers: TrackerTags;
|
|
7
22
|
};
|
|
8
23
|
export declare class ServerContext {
|
|
9
24
|
dev: boolean;
|
|
10
25
|
origin: string;
|
|
11
|
-
|
|
26
|
+
_urlReplacer?: (text: string, origin?: string) => string;
|
|
27
|
+
rpcBases: string[];
|
|
28
|
+
config: EDConfig;
|
|
29
|
+
static main: ServerContext;
|
|
12
30
|
constructor(conf: ServerContextArgs);
|
|
31
|
+
replaceUrls(text: string, origin?: string): string;
|
|
32
|
+
get runtime(): ServerContextRuntime;
|
|
33
|
+
static setRuntime(rt: ServerContextRuntime): void;
|
|
13
34
|
getOriginUrl(url: string): string;
|
|
14
35
|
fetchOrigin(url: string, opts?: RequestInit & {
|
|
15
36
|
replaceUrls?: boolean;
|
|
37
|
+
newOrigin?: string;
|
|
16
38
|
}): Promise<Response>;
|
|
17
39
|
fetchRouteData(req: {
|
|
18
40
|
pathname: string;
|
|
19
|
-
query?: Record<string, any>;
|
|
20
41
|
headers?: RequestHeaders;
|
|
21
|
-
|
|
42
|
+
newOrigin?: string;
|
|
22
43
|
}): Promise<Response>;
|
|
44
|
+
fetchAppData(): Promise<ServerAppData>;
|
|
23
45
|
extractRequestHeaders(req?: RequestHeaders): Partial<Record<import("vinxi/http").HTTPHeaderName, string | undefined>>;
|
|
24
46
|
fetchNamedQuery(req: {
|
|
25
47
|
name: string;
|
|
@@ -31,4 +53,20 @@ export declare class ServerContext {
|
|
|
31
53
|
body: object;
|
|
32
54
|
headers: RequestHeaders;
|
|
33
55
|
}): Promise<Response>;
|
|
56
|
+
get allowedCorsOrigins(): string[];
|
|
57
|
+
getCorsOrigin(originHeader: string): string | undefined;
|
|
58
|
+
getCorsHeaders(origin: string): {
|
|
59
|
+
"Access-Control-Allow-Methods"?: undefined;
|
|
60
|
+
"Access-Control-Allow-Origin"?: undefined;
|
|
61
|
+
"Access-Control-Allow-Credentials"?: undefined;
|
|
62
|
+
"Access-Control-Allow-Headers"?: undefined;
|
|
63
|
+
"Access-Control-Expose-Headers"?: undefined;
|
|
64
|
+
} | {
|
|
65
|
+
"Access-Control-Allow-Methods": string;
|
|
66
|
+
"Access-Control-Allow-Origin": string;
|
|
67
|
+
"Access-Control-Allow-Credentials": string;
|
|
68
|
+
"Access-Control-Allow-Headers": string;
|
|
69
|
+
"Access-Control-Expose-Headers": string;
|
|
70
|
+
};
|
|
34
71
|
}
|
|
72
|
+
export {};
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { parseURL, stringifyParsedURL, withQuery } from "ufo";
|
|
1
|
+
import { parseURL, stringifyParsedURL, withQuery, withTrailingSlash } from "ufo";
|
|
2
|
+
import { fetchWP } from "../../node/utils/fetch-wp.js";
|
|
2
3
|
import { filterHeader } from "./utils/headers.js";
|
|
3
4
|
import { createUrlReplacer } from "./utils/replace-host.js";
|
|
4
|
-
|
|
5
|
+
import { pageCache, queryCache, swr } from "./utils/swr-cache.js";
|
|
6
|
+
const PROXY_RESPONSE_HEADERS = ["content-type", "set-cookie", /^x-/, "cache-control", /woocommerce/, "location"];
|
|
5
7
|
const PROXY_REQUEST_HEADERS = [
|
|
6
8
|
"content-type",
|
|
7
9
|
"accept",
|
|
@@ -11,28 +13,49 @@ const PROXY_REQUEST_HEADERS = [
|
|
|
11
13
|
"referer",
|
|
12
14
|
"user-agent",
|
|
13
15
|
"authorization",
|
|
16
|
+
"woocommerce-session",
|
|
14
17
|
];
|
|
18
|
+
const VARIES_HEADERS = ["Authorization", "woocommerce-session"];
|
|
19
|
+
let runtime;
|
|
15
20
|
export class ServerContext {
|
|
16
21
|
dev;
|
|
17
22
|
origin;
|
|
18
|
-
|
|
23
|
+
_urlReplacer;
|
|
24
|
+
rpcBases = [];
|
|
25
|
+
config;
|
|
26
|
+
static main;
|
|
19
27
|
constructor(conf) {
|
|
20
28
|
this.dev = conf.dev;
|
|
21
29
|
this.origin = conf.origin;
|
|
30
|
+
this.rpcBases = conf.rpcBases ?? [];
|
|
31
|
+
this.config = conf.config;
|
|
22
32
|
if (conf.replaceUrls) {
|
|
23
|
-
this.
|
|
33
|
+
this._urlReplacer = createUrlReplacer(conf.replaceUrls);
|
|
24
34
|
}
|
|
35
|
+
ServerContext.main = this;
|
|
36
|
+
}
|
|
37
|
+
replaceUrls(text, origin) {
|
|
38
|
+
if (this._urlReplacer) {
|
|
39
|
+
return this._urlReplacer(text, origin);
|
|
40
|
+
}
|
|
41
|
+
return text;
|
|
42
|
+
}
|
|
43
|
+
get runtime() {
|
|
44
|
+
return runtime;
|
|
45
|
+
}
|
|
46
|
+
static setRuntime(rt) {
|
|
47
|
+
runtime = rt;
|
|
25
48
|
}
|
|
26
49
|
getOriginUrl(url) {
|
|
27
50
|
const parsed = parseURL(url);
|
|
28
|
-
const parsedOrigin = parseURL(
|
|
51
|
+
const parsedOrigin = parseURL(this.origin);
|
|
29
52
|
parsed.host = parsedOrigin.host;
|
|
30
53
|
parsed.protocol = parsedOrigin.protocol;
|
|
31
54
|
return stringifyParsedURL(parsed);
|
|
32
55
|
}
|
|
33
56
|
async fetchOrigin(url, opts) {
|
|
34
57
|
url = this.getOriginUrl(url);
|
|
35
|
-
const response = await
|
|
58
|
+
const response = await fetchWP(url, {
|
|
36
59
|
...opts,
|
|
37
60
|
headers: {
|
|
38
61
|
"Content-Type": "application/json",
|
|
@@ -50,8 +73,8 @@ export class ServerContext {
|
|
|
50
73
|
}
|
|
51
74
|
});
|
|
52
75
|
let text = await response.text();
|
|
53
|
-
if (opts?.replaceUrls
|
|
54
|
-
text = this.replaceUrls(text);
|
|
76
|
+
if (opts?.replaceUrls) {
|
|
77
|
+
text = this.replaceUrls(text, opts.newOrigin);
|
|
55
78
|
}
|
|
56
79
|
return new Response(text, {
|
|
57
80
|
status: response.status,
|
|
@@ -60,21 +83,94 @@ export class ServerContext {
|
|
|
60
83
|
});
|
|
61
84
|
}
|
|
62
85
|
async fetchRouteData(req) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
86
|
+
/**
|
|
87
|
+
* Calculcate the cache key.
|
|
88
|
+
* For route data, the cache key is the pathname.
|
|
89
|
+
* TODO: Potential support for Vercel draft mode.
|
|
90
|
+
*/
|
|
91
|
+
let cacheKey = "fetchRouteData:" + req.pathname;
|
|
92
|
+
const result = await swr({
|
|
93
|
+
key: cacheKey,
|
|
94
|
+
cache: pageCache,
|
|
95
|
+
forceFresh: this.dev,
|
|
96
|
+
getFreshValue: async (ctx) => {
|
|
97
|
+
ctx.metadata.createdTime;
|
|
98
|
+
const fetchUrl = withQuery(withTrailingSlash(req.pathname), {
|
|
99
|
+
_props: "1",
|
|
100
|
+
_ssr: "1",
|
|
101
|
+
_debug: this.dev ? "1" : undefined,
|
|
102
|
+
});
|
|
103
|
+
const result = await this.fetchOrigin(fetchUrl, {
|
|
104
|
+
cache: "no-cache",
|
|
105
|
+
replaceUrls: true,
|
|
106
|
+
newOrigin: req.newOrigin,
|
|
107
|
+
headers: {
|
|
108
|
+
"Content-Type": "application/json",
|
|
109
|
+
Accept: "application/json",
|
|
110
|
+
},
|
|
111
|
+
redirect: "manual",
|
|
112
|
+
});
|
|
113
|
+
const preferredCacheDuration = parseInt(result.headers.get("x-ed-cache-duration") ?? "");
|
|
114
|
+
if (isFinite(preferredCacheDuration)) {
|
|
115
|
+
ctx.metadata.ttl = preferredCacheDuration * 1000;
|
|
116
|
+
}
|
|
117
|
+
let resultStatus = result.status;
|
|
118
|
+
let resultHeaders = new Headers(result.headers);
|
|
119
|
+
let resultData = {};
|
|
120
|
+
// Special case for redirects
|
|
121
|
+
if (result.headers.get("location")) {
|
|
122
|
+
let location = result.headers.get("location");
|
|
123
|
+
let status = result.status;
|
|
124
|
+
if (this.replaceUrls) {
|
|
125
|
+
location = this.replaceUrls(location);
|
|
126
|
+
}
|
|
127
|
+
resultHeaders.delete("location");
|
|
128
|
+
return {
|
|
129
|
+
status: 200,
|
|
130
|
+
headers: resultHeaders,
|
|
131
|
+
data: JSON.stringify({ redirect: location, status: status }),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
resultData = await result.json();
|
|
135
|
+
if (resultData && typeof resultData === "object") {
|
|
136
|
+
resultData.__generated = new Date().toISOString();
|
|
137
|
+
}
|
|
138
|
+
return {
|
|
139
|
+
status: resultStatus,
|
|
140
|
+
headers: resultHeaders,
|
|
141
|
+
data: JSON.stringify(resultData),
|
|
142
|
+
};
|
|
143
|
+
},
|
|
68
144
|
});
|
|
69
|
-
return
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
145
|
+
return new Response(result.data, {
|
|
146
|
+
status: result.status,
|
|
147
|
+
headers: result.headers,
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
async fetchAppData() {
|
|
151
|
+
const data = await swr({
|
|
152
|
+
key: "fetchAppData",
|
|
153
|
+
cache: pageCache,
|
|
154
|
+
getFreshValue: async (ctx) => {
|
|
155
|
+
const response = await this.fetchOrigin("/_appdata", {
|
|
156
|
+
cache: "no-cache",
|
|
157
|
+
replaceUrls: true,
|
|
158
|
+
headers: {
|
|
159
|
+
"Content-Type": "application/json",
|
|
160
|
+
Accept: "application/json",
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
const result = await response.json();
|
|
164
|
+
const preferredCacheDuration = parseInt(response.headers.get("x-ed-cache-duration") ?? "");
|
|
165
|
+
if (isFinite(preferredCacheDuration)) {
|
|
166
|
+
ctx.metadata.ttl = preferredCacheDuration * 1000;
|
|
167
|
+
}
|
|
168
|
+
result.__generated = new Date().toISOString();
|
|
169
|
+
return result;
|
|
76
170
|
},
|
|
171
|
+
forceFresh: this.dev,
|
|
77
172
|
});
|
|
173
|
+
return data;
|
|
78
174
|
}
|
|
79
175
|
extractRequestHeaders(req) {
|
|
80
176
|
const headers = {};
|
|
@@ -89,14 +185,49 @@ export class ServerContext {
|
|
|
89
185
|
}
|
|
90
186
|
async fetchNamedQuery(req) {
|
|
91
187
|
const url = `/wp-json/ed/v1/query/${req.name}?params=${encodeURIComponent(JSON.stringify(req.params))}`;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
188
|
+
const key = `fetchNamedQuery:${req.name}:${JSON.stringify(req.params)}`;
|
|
189
|
+
const fetch = async () => {
|
|
190
|
+
const result = await this.fetchOrigin(url, {
|
|
191
|
+
cache: "no-cache",
|
|
192
|
+
replaceUrls: true,
|
|
193
|
+
headers: {
|
|
194
|
+
...this.extractRequestHeaders(req.headers),
|
|
195
|
+
"Content-Type": "application/json",
|
|
196
|
+
Accept: "application/json",
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
const preferredCacheDuration = parseInt(result.headers.get("x-ed-cache-duration") ?? "");
|
|
200
|
+
let resultStatus = result.status;
|
|
201
|
+
let resultHeaders = result.headers;
|
|
202
|
+
let resultData = await result.json();
|
|
203
|
+
if (resultData && typeof resultData === "object") {
|
|
204
|
+
resultData.__generated = new Date().toISOString();
|
|
205
|
+
}
|
|
206
|
+
return {
|
|
207
|
+
status: resultStatus,
|
|
208
|
+
headers: resultHeaders,
|
|
209
|
+
data: JSON.stringify(resultData),
|
|
210
|
+
cacheTime: isFinite(preferredCacheDuration) ? preferredCacheDuration * 1000 : undefined,
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
// If headers like 'Authorization' are set, then we shouldn't cache.
|
|
214
|
+
const hasVariedHeaders = VARIES_HEADERS.some((key) => !!req.headers[key]);
|
|
215
|
+
// Get the result, either from cache or by fetching.
|
|
216
|
+
const result = hasVariedHeaders
|
|
217
|
+
? await fetch()
|
|
218
|
+
: await swr({
|
|
219
|
+
key,
|
|
220
|
+
cache: queryCache,
|
|
221
|
+
forceFresh: this.dev,
|
|
222
|
+
getFreshValue: async (ctx) => {
|
|
223
|
+
const result = await fetch();
|
|
224
|
+
ctx.metadata.ttl = result.cacheTime;
|
|
225
|
+
return result;
|
|
226
|
+
},
|
|
227
|
+
});
|
|
228
|
+
return new Response(result.data, {
|
|
229
|
+
status: result.status,
|
|
230
|
+
headers: result.headers,
|
|
100
231
|
});
|
|
101
232
|
}
|
|
102
233
|
async fetchMutation(req) {
|
|
@@ -113,4 +244,40 @@ export class ServerContext {
|
|
|
113
244
|
body: JSON.stringify(req.body),
|
|
114
245
|
});
|
|
115
246
|
}
|
|
247
|
+
get allowedCorsOrigins() {
|
|
248
|
+
let result = [];
|
|
249
|
+
if (this.config.serverless.endpoints) {
|
|
250
|
+
result.push(...Object.keys(this.config.serverless.endpoints));
|
|
251
|
+
result.push(...Object.values(this.config.serverless.endpoints));
|
|
252
|
+
result = result.filter((origin) => origin !== "*");
|
|
253
|
+
}
|
|
254
|
+
if (this.config.serverless.cors?.origins) {
|
|
255
|
+
result.push(...this.config.serverless.cors.origins);
|
|
256
|
+
}
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
getCorsOrigin(originHeader) {
|
|
260
|
+
const parsed = parseURL(originHeader);
|
|
261
|
+
if (this.allowedCorsOrigins.includes(parsed.host)) {
|
|
262
|
+
return originHeader;
|
|
263
|
+
}
|
|
264
|
+
if (this.allowedCorsOrigins.includes("*")) {
|
|
265
|
+
return "*";
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
getCorsHeaders(origin) {
|
|
269
|
+
if (!origin)
|
|
270
|
+
return {};
|
|
271
|
+
const allowedOrigin = this.getCorsOrigin(origin);
|
|
272
|
+
if (allowedOrigin) {
|
|
273
|
+
return {
|
|
274
|
+
"Access-Control-Allow-Methods": "OPTIONS,GET,HEAD,PUT,PATCH,POST,DELETE",
|
|
275
|
+
"Access-Control-Allow-Origin": allowedOrigin,
|
|
276
|
+
"Access-Control-Allow-Credentials": "true",
|
|
277
|
+
"Access-Control-Allow-Headers": "*",
|
|
278
|
+
"Access-Control-Expose-Headers": "*",
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return {};
|
|
282
|
+
}
|
|
116
283
|
}
|