@sveltejs/kit 1.0.0-next.52 → 1.0.0-next.520
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 +6 -3
- package/package.json +93 -67
- package/postinstall.js +47 -0
- package/scripts/special-types/$env+dynamic+private.md +10 -0
- package/scripts/special-types/$env+dynamic+public.md +8 -0
- package/scripts/special-types/$env+static+private.md +19 -0
- package/scripts/special-types/$env+static+public.md +7 -0
- package/scripts/special-types/$lib.md +5 -0
- package/src/cli.js +108 -0
- package/src/constants.js +7 -0
- package/src/core/adapt/builder.js +215 -0
- package/src/core/adapt/index.js +31 -0
- package/src/core/config/default-error.html +56 -0
- package/src/core/config/index.js +110 -0
- package/src/core/config/options.js +504 -0
- package/src/core/config/types.d.ts +1 -0
- package/src/core/env.js +121 -0
- package/src/core/generate_manifest/index.js +94 -0
- package/src/core/prerender/crawl.js +198 -0
- package/src/core/prerender/entities.js +2252 -0
- package/src/core/prerender/prerender.js +458 -0
- package/src/core/prerender/queue.js +80 -0
- package/src/core/sync/create_manifest_data/conflict.js +0 -0
- package/src/core/sync/create_manifest_data/index.js +470 -0
- package/src/core/sync/create_manifest_data/sort.js +163 -0
- package/src/core/sync/create_manifest_data/types.d.ts +37 -0
- package/src/core/sync/sync.js +78 -0
- package/src/core/sync/utils.js +33 -0
- package/src/core/sync/write_ambient.js +53 -0
- package/src/core/sync/write_client_manifest.js +106 -0
- package/src/core/sync/write_matchers.js +25 -0
- package/src/core/sync/write_root.js +91 -0
- package/src/core/sync/write_tsconfig.js +195 -0
- package/src/core/sync/write_types/index.js +783 -0
- package/src/core/utils.js +70 -0
- package/src/exports/hooks/index.js +1 -0
- package/src/exports/hooks/sequence.js +44 -0
- package/src/exports/index.js +45 -0
- package/src/exports/node/index.js +161 -0
- package/src/exports/node/polyfills.js +28 -0
- package/src/exports/vite/build/build_server.js +378 -0
- package/src/exports/vite/build/build_service_worker.js +91 -0
- package/src/exports/vite/build/utils.js +181 -0
- package/src/exports/vite/dev/index.js +581 -0
- package/src/exports/vite/graph_analysis/index.js +277 -0
- package/src/exports/vite/graph_analysis/types.d.ts +5 -0
- package/src/exports/vite/graph_analysis/utils.js +30 -0
- package/src/exports/vite/index.js +603 -0
- package/src/exports/vite/preview/index.js +189 -0
- package/src/exports/vite/types.d.ts +3 -0
- package/src/exports/vite/utils.js +157 -0
- package/src/runtime/app/env.js +1 -0
- package/src/runtime/app/environment.js +11 -0
- package/src/runtime/app/forms.js +123 -0
- package/src/runtime/app/navigation.js +23 -0
- package/src/runtime/app/paths.js +1 -0
- package/src/runtime/app/stores.js +102 -0
- package/src/runtime/client/ambient.d.ts +30 -0
- package/src/runtime/client/client.js +1595 -0
- package/src/runtime/client/fetcher.js +107 -0
- package/src/runtime/client/parse.js +60 -0
- package/src/runtime/client/singletons.js +21 -0
- package/src/runtime/client/start.js +37 -0
- package/src/runtime/client/types.d.ts +84 -0
- package/src/runtime/client/utils.js +159 -0
- package/src/runtime/components/error.svelte +16 -0
- package/{assets → src/runtime}/components/layout.svelte +0 -0
- package/src/runtime/control.js +98 -0
- package/src/runtime/env/dynamic/private.js +1 -0
- package/src/runtime/env/dynamic/public.js +1 -0
- package/src/runtime/env-private.js +6 -0
- package/src/runtime/env-public.js +6 -0
- package/src/runtime/env.js +6 -0
- package/src/runtime/hash.js +20 -0
- package/src/runtime/paths.js +11 -0
- package/src/runtime/server/cookie.js +166 -0
- package/src/runtime/server/data/index.js +131 -0
- package/src/runtime/server/endpoint.js +92 -0
- package/src/runtime/server/fetch.js +174 -0
- package/src/runtime/server/index.js +355 -0
- package/src/runtime/server/page/actions.js +256 -0
- package/src/runtime/server/page/crypto.js +239 -0
- package/src/runtime/server/page/csp.js +250 -0
- package/src/runtime/server/page/index.js +304 -0
- package/src/runtime/server/page/load_data.js +215 -0
- package/src/runtime/server/page/render.js +346 -0
- package/src/runtime/server/page/respond_with_error.js +102 -0
- package/src/runtime/server/page/serialize_data.js +87 -0
- package/src/runtime/server/page/types.d.ts +35 -0
- package/src/runtime/server/utils.js +181 -0
- package/src/utils/array.js +9 -0
- package/src/utils/error.js +22 -0
- package/src/utils/escape.js +46 -0
- package/src/utils/filesystem.js +142 -0
- package/src/utils/functions.js +16 -0
- package/src/utils/http.js +72 -0
- package/src/utils/misc.js +1 -0
- package/src/utils/promises.js +17 -0
- package/src/utils/routing.js +130 -0
- package/src/utils/unit_test.js +11 -0
- package/src/utils/url.js +144 -0
- package/svelte-kit.js +1 -1
- package/types/ambient.d.ts +431 -0
- package/types/index.d.ts +477 -0
- package/types/internal.d.ts +380 -0
- package/types/private.d.ts +224 -0
- package/CHANGELOG.md +0 -496
- package/assets/components/error.svelte +0 -13
- package/assets/runtime/app/env.js +0 -5
- package/assets/runtime/app/navigation.js +0 -44
- package/assets/runtime/app/paths.js +0 -1
- package/assets/runtime/app/stores.js +0 -93
- package/assets/runtime/chunks/utils.js +0 -22
- package/assets/runtime/internal/singletons.js +0 -23
- package/assets/runtime/internal/start.js +0 -776
- package/assets/runtime/paths.js +0 -12
- package/dist/chunks/index.js +0 -3537
- package/dist/chunks/index2.js +0 -587
- package/dist/chunks/index3.js +0 -246
- package/dist/chunks/index4.js +0 -568
- package/dist/chunks/index5.js +0 -763
- package/dist/chunks/index6.js +0 -323
- package/dist/chunks/standard.js +0 -99
- package/dist/chunks/utils.js +0 -83
- package/dist/cli.js +0 -555
- package/dist/ssr.js +0 -2604
- package/types.d.ts +0 -73
- package/types.internal.d.ts +0 -222
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import * as devalue from 'devalue';
|
|
2
|
+
import { DATA_SUFFIX } from '../../../constants.js';
|
|
3
|
+
import { compact } from '../../../utils/array.js';
|
|
4
|
+
import { normalize_error } from '../../../utils/error.js';
|
|
5
|
+
import { HttpError, Redirect } from '../../control.js';
|
|
6
|
+
import {
|
|
7
|
+
get_option,
|
|
8
|
+
redirect_response,
|
|
9
|
+
static_error_page,
|
|
10
|
+
handle_error_and_jsonify
|
|
11
|
+
} from '../utils.js';
|
|
12
|
+
import {
|
|
13
|
+
handle_action_json_request,
|
|
14
|
+
handle_action_request,
|
|
15
|
+
is_action_json_request,
|
|
16
|
+
is_action_request
|
|
17
|
+
} from './actions.js';
|
|
18
|
+
import { load_data, load_server_data } from './load_data.js';
|
|
19
|
+
import { render_response } from './render.js';
|
|
20
|
+
import { respond_with_error } from './respond_with_error.js';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param {import('types').RequestEvent} event
|
|
24
|
+
* @param {import('types').SSRRoute} route
|
|
25
|
+
* @param {import('types').PageNodeIndexes} page
|
|
26
|
+
* @param {import('types').SSROptions} options
|
|
27
|
+
* @param {import('types').SSRState} state
|
|
28
|
+
* @param {import('types').RequiredResolveOptions} resolve_opts
|
|
29
|
+
* @returns {Promise<Response>}
|
|
30
|
+
*/
|
|
31
|
+
export async function render_page(event, route, page, options, state, resolve_opts) {
|
|
32
|
+
if (state.initiator === route) {
|
|
33
|
+
// infinite request cycle detected
|
|
34
|
+
return new Response(`Not found: ${event.url.pathname}`, {
|
|
35
|
+
status: 404
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
state.initiator = route;
|
|
40
|
+
|
|
41
|
+
if (is_action_json_request(event)) {
|
|
42
|
+
const node = await options.manifest._.nodes[page.leaf]();
|
|
43
|
+
if (node.server) {
|
|
44
|
+
return handle_action_json_request(event, options, node.server);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const nodes = await Promise.all([
|
|
50
|
+
// we use == here rather than === because [undefined] serializes as "[null]"
|
|
51
|
+
...page.layouts.map((n) => (n == undefined ? n : options.manifest._.nodes[n]())),
|
|
52
|
+
options.manifest._.nodes[page.leaf]()
|
|
53
|
+
]);
|
|
54
|
+
|
|
55
|
+
const leaf_node = /** @type {import('types').SSRNode} */ (nodes.at(-1));
|
|
56
|
+
|
|
57
|
+
let status = 200;
|
|
58
|
+
|
|
59
|
+
/** @type {import('types').ActionResult | undefined} */
|
|
60
|
+
let action_result = undefined;
|
|
61
|
+
|
|
62
|
+
if (is_action_request(event, leaf_node)) {
|
|
63
|
+
// for action requests, first call handler in +page.server.js
|
|
64
|
+
// (this also determines status code)
|
|
65
|
+
action_result = await handle_action_request(event, leaf_node.server);
|
|
66
|
+
if (action_result?.type === 'redirect') {
|
|
67
|
+
return redirect_response(303, action_result.location);
|
|
68
|
+
}
|
|
69
|
+
if (action_result?.type === 'error') {
|
|
70
|
+
const error = action_result.error;
|
|
71
|
+
status = error instanceof HttpError ? error.status : 500;
|
|
72
|
+
}
|
|
73
|
+
if (action_result?.type === 'invalid') {
|
|
74
|
+
status = action_result.status;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const should_prerender_data = nodes.some((node) => node?.server);
|
|
79
|
+
const data_pathname = event.url.pathname.replace(/\/$/, '') + DATA_SUFFIX;
|
|
80
|
+
|
|
81
|
+
// it's crucial that we do this before returning the non-SSR response, otherwise
|
|
82
|
+
// SvelteKit will erroneously believe that the path has been prerendered,
|
|
83
|
+
// causing functions to be omitted from the manifesst generated later
|
|
84
|
+
const should_prerender = get_option(nodes, 'prerender') ?? false;
|
|
85
|
+
if (should_prerender) {
|
|
86
|
+
const mod = leaf_node.server;
|
|
87
|
+
if (mod && mod.actions) {
|
|
88
|
+
throw new Error('Cannot prerender pages with actions');
|
|
89
|
+
}
|
|
90
|
+
} else if (state.prerendering) {
|
|
91
|
+
// if the page isn't marked as prerenderable, then bail out at this point
|
|
92
|
+
return new Response(undefined, {
|
|
93
|
+
status: 204
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// if we fetch any endpoints while loading data for this page, they should
|
|
98
|
+
// inherit the prerender option of the page
|
|
99
|
+
state.prerender_default = should_prerender;
|
|
100
|
+
|
|
101
|
+
/** @type {import('./types').Fetched[]} */
|
|
102
|
+
const fetched = [];
|
|
103
|
+
|
|
104
|
+
if (get_option(nodes, 'ssr') === false) {
|
|
105
|
+
return await render_response({
|
|
106
|
+
branch: [],
|
|
107
|
+
fetched,
|
|
108
|
+
page_config: {
|
|
109
|
+
ssr: false,
|
|
110
|
+
csr: get_option(nodes, 'csr') ?? true
|
|
111
|
+
},
|
|
112
|
+
status,
|
|
113
|
+
error: null,
|
|
114
|
+
event,
|
|
115
|
+
options,
|
|
116
|
+
state,
|
|
117
|
+
resolve_opts
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @type {Array<import('./types.js').Loaded | null>} */
|
|
122
|
+
let branch = [];
|
|
123
|
+
|
|
124
|
+
/** @type {Error | null} */
|
|
125
|
+
let load_error = null;
|
|
126
|
+
|
|
127
|
+
/** @type {Array<Promise<import('types').ServerDataNode | null>>} */
|
|
128
|
+
const server_promises = nodes.map((node, i) => {
|
|
129
|
+
if (load_error) {
|
|
130
|
+
// if an error happens immediately, don't bother with the rest of the nodes
|
|
131
|
+
throw load_error;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return Promise.resolve().then(async () => {
|
|
135
|
+
try {
|
|
136
|
+
if (node === leaf_node && action_result?.type === 'error') {
|
|
137
|
+
// we wait until here to throw the error so that we can use
|
|
138
|
+
// any nested +error.svelte components that were defined
|
|
139
|
+
throw action_result.error;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return await load_server_data({
|
|
143
|
+
event,
|
|
144
|
+
state,
|
|
145
|
+
node,
|
|
146
|
+
parent: async () => {
|
|
147
|
+
/** @type {Record<string, any>} */
|
|
148
|
+
const data = {};
|
|
149
|
+
for (let j = 0; j < i; j += 1) {
|
|
150
|
+
const parent = await server_promises[j];
|
|
151
|
+
if (parent) Object.assign(data, await parent.data);
|
|
152
|
+
}
|
|
153
|
+
return data;
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
} catch (e) {
|
|
157
|
+
load_error = /** @type {Error} */ (e);
|
|
158
|
+
throw load_error;
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
const csr = get_option(nodes, 'csr') ?? true;
|
|
164
|
+
|
|
165
|
+
/** @type {Array<Promise<Record<string, any> | null>>} */
|
|
166
|
+
const load_promises = nodes.map((node, i) => {
|
|
167
|
+
if (load_error) throw load_error;
|
|
168
|
+
return Promise.resolve().then(async () => {
|
|
169
|
+
try {
|
|
170
|
+
return await load_data({
|
|
171
|
+
event,
|
|
172
|
+
fetched,
|
|
173
|
+
node,
|
|
174
|
+
parent: async () => {
|
|
175
|
+
const data = {};
|
|
176
|
+
for (let j = 0; j < i; j += 1) {
|
|
177
|
+
Object.assign(data, await load_promises[j]);
|
|
178
|
+
}
|
|
179
|
+
return data;
|
|
180
|
+
},
|
|
181
|
+
resolve_opts,
|
|
182
|
+
server_data_promise: server_promises[i],
|
|
183
|
+
state,
|
|
184
|
+
csr
|
|
185
|
+
});
|
|
186
|
+
} catch (e) {
|
|
187
|
+
load_error = /** @type {Error} */ (e);
|
|
188
|
+
throw load_error;
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// if we don't do this, rejections will be unhandled
|
|
194
|
+
for (const p of server_promises) p.catch(() => {});
|
|
195
|
+
for (const p of load_promises) p.catch(() => {});
|
|
196
|
+
|
|
197
|
+
for (let i = 0; i < nodes.length; i += 1) {
|
|
198
|
+
const node = nodes[i];
|
|
199
|
+
|
|
200
|
+
if (node) {
|
|
201
|
+
try {
|
|
202
|
+
const server_data = await server_promises[i];
|
|
203
|
+
const data = await load_promises[i];
|
|
204
|
+
|
|
205
|
+
branch.push({ node, server_data, data });
|
|
206
|
+
} catch (e) {
|
|
207
|
+
const err = normalize_error(e);
|
|
208
|
+
|
|
209
|
+
if (err instanceof Redirect) {
|
|
210
|
+
if (state.prerendering && should_prerender_data) {
|
|
211
|
+
const body = devalue.stringify({
|
|
212
|
+
type: 'redirect',
|
|
213
|
+
location: err.location
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
state.prerendering.dependencies.set(data_pathname, {
|
|
217
|
+
response: new Response(body),
|
|
218
|
+
body
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return redirect_response(err.status, err.location);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const status = err instanceof HttpError ? err.status : 500;
|
|
226
|
+
const error = handle_error_and_jsonify(event, options, err);
|
|
227
|
+
|
|
228
|
+
while (i--) {
|
|
229
|
+
if (page.errors[i]) {
|
|
230
|
+
const index = /** @type {number} */ (page.errors[i]);
|
|
231
|
+
const node = await options.manifest._.nodes[index]();
|
|
232
|
+
|
|
233
|
+
let j = i;
|
|
234
|
+
while (!branch[j]) j -= 1;
|
|
235
|
+
|
|
236
|
+
return await render_response({
|
|
237
|
+
event,
|
|
238
|
+
options,
|
|
239
|
+
state,
|
|
240
|
+
resolve_opts,
|
|
241
|
+
page_config: { ssr: true, csr: true },
|
|
242
|
+
status,
|
|
243
|
+
error,
|
|
244
|
+
branch: compact(branch.slice(0, j + 1)).concat({
|
|
245
|
+
node,
|
|
246
|
+
data: null,
|
|
247
|
+
server_data: null
|
|
248
|
+
}),
|
|
249
|
+
fetched
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// if we're still here, it means the error happened in the root layout,
|
|
255
|
+
// which means we have to fall back to error.html
|
|
256
|
+
return static_error_page(options, status, error.message);
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
// push an empty slot so we can rewind past gaps to the
|
|
260
|
+
// layout that corresponds with an +error.svelte page
|
|
261
|
+
branch.push(null);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (state.prerendering && should_prerender_data) {
|
|
266
|
+
const body = devalue.stringify({
|
|
267
|
+
type: 'data',
|
|
268
|
+
nodes: branch.map((branch_node) => branch_node?.server_data)
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
state.prerendering.dependencies.set(data_pathname, {
|
|
272
|
+
response: new Response(body),
|
|
273
|
+
body
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return await render_response({
|
|
278
|
+
event,
|
|
279
|
+
options,
|
|
280
|
+
state,
|
|
281
|
+
resolve_opts,
|
|
282
|
+
page_config: {
|
|
283
|
+
csr: get_option(nodes, 'csr') ?? true,
|
|
284
|
+
ssr: true
|
|
285
|
+
},
|
|
286
|
+
status,
|
|
287
|
+
error: null,
|
|
288
|
+
branch: compact(branch),
|
|
289
|
+
action_result,
|
|
290
|
+
fetched
|
|
291
|
+
});
|
|
292
|
+
} catch (error) {
|
|
293
|
+
// if we end up here, it means the data loaded successfull
|
|
294
|
+
// but the page failed to render, or that a prerendering error occurred
|
|
295
|
+
return await respond_with_error({
|
|
296
|
+
event,
|
|
297
|
+
options,
|
|
298
|
+
state,
|
|
299
|
+
status: 500,
|
|
300
|
+
error,
|
|
301
|
+
resolve_opts
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { disable_search, make_trackable } from '../../../utils/url.js';
|
|
2
|
+
import { unwrap_promises } from '../../../utils/promises.js';
|
|
3
|
+
/**
|
|
4
|
+
* Calls the user's `load` function.
|
|
5
|
+
* @param {{
|
|
6
|
+
* event: import('types').RequestEvent;
|
|
7
|
+
* state: import('types').SSRState;
|
|
8
|
+
* node: import('types').SSRNode | undefined;
|
|
9
|
+
* parent: () => Promise<Record<string, any>>;
|
|
10
|
+
* }} opts
|
|
11
|
+
* @returns {Promise<import('types').ServerDataNode | null>}
|
|
12
|
+
*/
|
|
13
|
+
export async function load_server_data({ event, state, node, parent }) {
|
|
14
|
+
if (!node?.server) return null;
|
|
15
|
+
|
|
16
|
+
const uses = {
|
|
17
|
+
dependencies: new Set(),
|
|
18
|
+
params: new Set(),
|
|
19
|
+
parent: false,
|
|
20
|
+
url: false
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const url = make_trackable(event.url, () => {
|
|
24
|
+
uses.url = true;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (state.prerendering) {
|
|
28
|
+
disable_search(url);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const result = await node.server.load?.call(null, {
|
|
32
|
+
...event,
|
|
33
|
+
/** @param {string[]} deps */
|
|
34
|
+
depends: (...deps) => {
|
|
35
|
+
for (const dep of deps) {
|
|
36
|
+
const { href } = new URL(dep, event.url);
|
|
37
|
+
uses.dependencies.add(href);
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
params: new Proxy(event.params, {
|
|
41
|
+
get: (target, key) => {
|
|
42
|
+
uses.params.add(key);
|
|
43
|
+
return target[/** @type {string} */ (key)];
|
|
44
|
+
}
|
|
45
|
+
}),
|
|
46
|
+
parent: async () => {
|
|
47
|
+
uses.parent = true;
|
|
48
|
+
return parent();
|
|
49
|
+
},
|
|
50
|
+
url
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const data = result ? await unwrap_promises(result) : null;
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
type: 'data',
|
|
57
|
+
data,
|
|
58
|
+
uses: {
|
|
59
|
+
dependencies: uses.dependencies.size > 0 ? Array.from(uses.dependencies) : undefined,
|
|
60
|
+
params: uses.params.size > 0 ? Array.from(uses.params) : undefined,
|
|
61
|
+
parent: uses.parent ? 1 : undefined,
|
|
62
|
+
url: uses.url ? 1 : undefined
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Calls the user's `load` function.
|
|
69
|
+
* @param {{
|
|
70
|
+
* event: import('types').RequestEvent;
|
|
71
|
+
* fetched: import('./types').Fetched[];
|
|
72
|
+
* node: import('types').SSRNode | undefined;
|
|
73
|
+
* parent: () => Promise<Record<string, any>>;
|
|
74
|
+
* resolve_opts: import('types').RequiredResolveOptions;
|
|
75
|
+
* server_data_promise: Promise<import('types').ServerDataNode | null>;
|
|
76
|
+
* state: import('types').SSRState;
|
|
77
|
+
* csr: boolean;
|
|
78
|
+
* }} opts
|
|
79
|
+
* @returns {Promise<Record<string, any> | null>}
|
|
80
|
+
*/
|
|
81
|
+
export async function load_data({
|
|
82
|
+
event,
|
|
83
|
+
fetched,
|
|
84
|
+
node,
|
|
85
|
+
parent,
|
|
86
|
+
server_data_promise,
|
|
87
|
+
state,
|
|
88
|
+
resolve_opts,
|
|
89
|
+
csr
|
|
90
|
+
}) {
|
|
91
|
+
const server_data_node = await server_data_promise;
|
|
92
|
+
|
|
93
|
+
if (!node?.shared?.load) {
|
|
94
|
+
return server_data_node?.data ?? null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** @type {import('types').LoadEvent} */
|
|
98
|
+
const load_event = {
|
|
99
|
+
url: event.url,
|
|
100
|
+
params: event.params,
|
|
101
|
+
data: server_data_node?.data ?? null,
|
|
102
|
+
routeId: event.routeId,
|
|
103
|
+
fetch: async (input, init) => {
|
|
104
|
+
const response = await event.fetch(input, init);
|
|
105
|
+
|
|
106
|
+
const url = new URL(input instanceof Request ? input.url : input, event.url);
|
|
107
|
+
const same_origin = url.origin === event.url.origin;
|
|
108
|
+
|
|
109
|
+
const request_body = init?.body;
|
|
110
|
+
const dependency = same_origin && state.prerendering?.dependencies.get(url.pathname);
|
|
111
|
+
|
|
112
|
+
const proxy = new Proxy(response, {
|
|
113
|
+
get(response, key, _receiver) {
|
|
114
|
+
async function text() {
|
|
115
|
+
const body = await response.text();
|
|
116
|
+
|
|
117
|
+
if (!body || typeof body === 'string') {
|
|
118
|
+
const status_number = Number(response.status);
|
|
119
|
+
if (isNaN(status_number)) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
`response.status is not a number. value: "${
|
|
122
|
+
response.status
|
|
123
|
+
}" type: ${typeof response.status}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
fetched.push({
|
|
128
|
+
url: same_origin ? url.href.slice(event.url.origin.length) : url.href,
|
|
129
|
+
method: event.request.method,
|
|
130
|
+
request_body: /** @type {string | ArrayBufferView | undefined} */ (request_body),
|
|
131
|
+
response_body: body,
|
|
132
|
+
response: response
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (dependency) {
|
|
137
|
+
dependency.body = body;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return body;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (key === 'arrayBuffer') {
|
|
144
|
+
return async () => {
|
|
145
|
+
const buffer = await response.arrayBuffer();
|
|
146
|
+
|
|
147
|
+
if (dependency) {
|
|
148
|
+
dependency.body = new Uint8Array(buffer);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// TODO should buffer be inlined into the page (albeit base64'd)?
|
|
152
|
+
// any conditions in which it shouldn't be?
|
|
153
|
+
|
|
154
|
+
return buffer;
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (key === 'text') {
|
|
159
|
+
return text;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
if (key === 'json') {
|
|
163
|
+
return async () => {
|
|
164
|
+
return JSON.parse(await text());
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// TODO arrayBuffer?
|
|
169
|
+
|
|
170
|
+
return Reflect.get(response, key, response);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
if (csr) {
|
|
175
|
+
// ensure that excluded headers can't be read
|
|
176
|
+
const get = response.headers.get;
|
|
177
|
+
response.headers.get = (key) => {
|
|
178
|
+
const lower = key.toLowerCase();
|
|
179
|
+
const value = get.call(response.headers, lower);
|
|
180
|
+
if (value && !lower.startsWith('x-sveltekit-')) {
|
|
181
|
+
const included = resolve_opts.filterSerializedResponseHeaders(lower, value);
|
|
182
|
+
if (!included) {
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Failed to get response header "${lower}" — it must be included by the \`filterSerializedResponseHeaders\` option: https://kit.svelte.dev/docs/hooks#handle (at ${event.routeId})`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
return value;
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return proxy;
|
|
194
|
+
},
|
|
195
|
+
setHeaders: event.setHeaders,
|
|
196
|
+
depends: () => {},
|
|
197
|
+
parent
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// TODO remove this for 1.0
|
|
201
|
+
Object.defineProperties(load_event, {
|
|
202
|
+
session: {
|
|
203
|
+
get() {
|
|
204
|
+
throw new Error(
|
|
205
|
+
'session is no longer available. See https://github.com/sveltejs/kit/discussions/5883'
|
|
206
|
+
);
|
|
207
|
+
},
|
|
208
|
+
enumerable: false
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
const data = await node.shared.load.call(null, load_event);
|
|
213
|
+
|
|
214
|
+
return data ? unwrap_promises(data) : null;
|
|
215
|
+
}
|