@sveltejs/adapter-netlify 7.0.0-next.11 → 7.0.0-next.12
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/files/edge.js +37 -31
- package/index.d.ts +6 -2
- package/index.js +260 -208
- package/package.json +8 -8
- package/utils.js +0 -35
package/files/edge.js
CHANGED
|
@@ -1,41 +1,47 @@
|
|
|
1
|
-
import {
|
|
1
|
+
/** @import { EdgeFunction } from '@netlify/edge-functions' */
|
|
2
|
+
/** @import { Server } from '@sveltejs/kit' */
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @type {string}
|
|
5
|
+
* @param {Server} server
|
|
6
|
+
* @returns {EdgeFunction}
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
export function init(server) {
|
|
9
|
+
/**
|
|
10
|
+
* We don't know the origin until we receive a request, but
|
|
11
|
+
* that's guaranteed to happen before we call `read`
|
|
12
|
+
* @type {string}
|
|
13
|
+
*/
|
|
14
|
+
let origin;
|
|
9
15
|
|
|
10
|
-
const initialized = server.init({
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
16
|
+
const initialized = server.init({
|
|
17
|
+
env: Deno.env.toObject(),
|
|
18
|
+
read: async (file) => {
|
|
19
|
+
const url = `${origin}/${file}`;
|
|
20
|
+
const response = await fetch(url);
|
|
15
21
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
`read(...) failed: could not fetch ${url} (${response.status} ${response.statusText})`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
21
27
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
});
|
|
28
|
+
return response.body;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
25
31
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
32
|
+
return async function handler(request, context) {
|
|
33
|
+
if (!origin) {
|
|
34
|
+
origin = new URL(request.url).origin;
|
|
35
|
+
}
|
|
31
36
|
|
|
32
|
-
|
|
33
|
-
|
|
37
|
+
// always await initialization to prevent race condition with concurrent requests
|
|
38
|
+
await initialized;
|
|
34
39
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
return server.respond(request, {
|
|
41
|
+
platform: { context },
|
|
42
|
+
getClientAddress() {
|
|
43
|
+
return context.ip;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
};
|
|
41
47
|
}
|
package/index.d.ts
CHANGED
|
@@ -9,11 +9,15 @@ export interface AdapterOptions {
|
|
|
9
9
|
edge?: boolean;
|
|
10
10
|
/**
|
|
11
11
|
* If `true`, your app will be split into multiple functions instead of a single one for the entire app.
|
|
12
|
-
*
|
|
13
|
-
* If `edge` is `true`, this option cannot be used.
|
|
14
12
|
* @default false
|
|
15
13
|
*/
|
|
16
14
|
split?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* The directory to write prerendered pages and static assets (the contents of `static`, plus client-side JS and CSS generated by SvelteKit) to.
|
|
17
|
+
* @default 'build'
|
|
18
|
+
* @since 7.0.0
|
|
19
|
+
*/
|
|
20
|
+
publish?: string;
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
export default function plugin(opts?: AdapterOptions): Adapter;
|
package/index.js
CHANGED
|
@@ -1,21 +1,13 @@
|
|
|
1
|
+
/** @import { IntegrationsConfig } from '@netlify/edge-functions' */
|
|
1
2
|
/** @import { Builder, RouteDefinition } from '@sveltejs/kit' */
|
|
2
|
-
/** @import { TomlTable } from 'smol-toml' */
|
|
3
3
|
import crypto from 'node:crypto';
|
|
4
4
|
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
7
|
import { builtinModules } from 'node:module';
|
|
8
8
|
import process from 'node:process';
|
|
9
|
-
import { parse } from 'smol-toml';
|
|
10
9
|
import { build } from 'rolldown';
|
|
11
|
-
import { matches,
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @typedef {{
|
|
15
|
-
* build?: { publish?: string }
|
|
16
|
-
* functions?: { node_bundler?: 'zisi' | 'esbuild' }
|
|
17
|
-
* } & TomlTable} NetlifyConfig
|
|
18
|
-
*/
|
|
10
|
+
import { matches, s } from './utils.js';
|
|
19
11
|
|
|
20
12
|
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf-8'));
|
|
21
13
|
const adapter_version = pkg.version;
|
|
@@ -33,8 +25,10 @@ const netlify_framework_edge_path = '.netlify/v1/edge-functions';
|
|
|
33
25
|
|
|
34
26
|
const FUNCTION_PREFIX = 'sveltekit-';
|
|
35
27
|
|
|
28
|
+
// "build" is the default publish directory when Netlify detects SvelteKit
|
|
29
|
+
|
|
36
30
|
/** @type {typeof import('./index.js').default} */
|
|
37
|
-
export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
31
|
+
export default function ({ split = false, edge = edge_set_in_env_var, publish = 'build' } = {}) {
|
|
38
32
|
return {
|
|
39
33
|
name,
|
|
40
34
|
async adapt(builder) {
|
|
@@ -57,11 +51,6 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
57
51
|
);
|
|
58
52
|
}
|
|
59
53
|
|
|
60
|
-
const netlify_config = get_netlify_config();
|
|
61
|
-
|
|
62
|
-
// "build" is the default publish directory when Netlify detects SvelteKit
|
|
63
|
-
const publish = get_publish_directory(netlify_config, builder) || 'build';
|
|
64
|
-
|
|
65
54
|
// empty out existing build directories
|
|
66
55
|
rmSync(publish, { force: true, recursive: true });
|
|
67
56
|
rmSync('.netlify/v1', { force: true, recursive: true });
|
|
@@ -80,8 +69,6 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
80
69
|
}
|
|
81
70
|
}
|
|
82
71
|
|
|
83
|
-
builder.log.minor(`Publishing to "${publish}"`);
|
|
84
|
-
|
|
85
72
|
builder.log.minor('Copying assets...');
|
|
86
73
|
const publish_dir = `${publish}${builder.config.paths.base}`;
|
|
87
74
|
builder.writeClient(publish_dir);
|
|
@@ -103,11 +90,7 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
103
90
|
write_frameworks_config({ builder });
|
|
104
91
|
|
|
105
92
|
if (edge) {
|
|
106
|
-
|
|
107
|
-
throw new Error('Cannot use `split: true` alongside `edge: true`');
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
await generate_edge_functions({ builder });
|
|
93
|
+
await generate_edge_functions({ builder, split });
|
|
111
94
|
} else {
|
|
112
95
|
generate_serverless_functions(builder, split);
|
|
113
96
|
}
|
|
@@ -135,103 +118,18 @@ function generate_serverless_functions(builder, split) {
|
|
|
135
118
|
builder.log.minor('Generating serverless functions...');
|
|
136
119
|
|
|
137
120
|
if (split) {
|
|
138
|
-
const seen = new Set();
|
|
139
|
-
let index = 0;
|
|
140
121
|
const uuid = crypto.randomUUID();
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const route = builder.routes[i];
|
|
144
|
-
if (route.prerender === true) continue;
|
|
145
|
-
|
|
146
|
-
const routes = [route];
|
|
147
|
-
|
|
148
|
-
/** @type {string[]} */
|
|
149
|
-
const parts = [];
|
|
150
|
-
|
|
151
|
-
// The parts should conform to URLPattern syntax
|
|
152
|
-
// https://docs.netlify.com/build/functions/get-started/?fn-language=ts&data-tab=TypeScript#route-requests
|
|
153
|
-
for (const [i, segment] of route.segments.entries()) {
|
|
154
|
-
if (segment.rest) {
|
|
155
|
-
parts.push('*');
|
|
156
|
-
} else if (segment.dynamic) {
|
|
157
|
-
// URLPattern requires params to start with letters
|
|
158
|
-
const optional = /^\[\[.+\]\]$/.test(segment.content) ? '?' : '';
|
|
159
|
-
parts.push(`:param${i}${optional}`);
|
|
160
|
-
} else {
|
|
161
|
-
parts.push(segment.content);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Netlify handles trailing slashes for us, so we don't need to include them in the pattern
|
|
166
|
-
const pattern = `/${parts.join('/')}`;
|
|
167
|
-
|
|
168
|
-
// skip routes with identical patterns, they were already folded into another function
|
|
169
|
-
if (seen.has(pattern)) continue;
|
|
170
|
-
|
|
171
|
-
// the route itself is human readable via the `name` config export,
|
|
172
|
-
// so the function file can just use an index
|
|
173
|
-
const name = `${FUNCTION_PREFIX}${index++}`;
|
|
174
|
-
|
|
175
|
-
const patterns = [pattern, `${pattern === '/' ? '' : pattern}/__data.json`];
|
|
176
|
-
patterns.forEach((pattern) => seen.add(pattern));
|
|
177
|
-
|
|
178
|
-
// figure out which lower priority routes should be considered fallbacks
|
|
179
|
-
for (let j = i + 1; j < builder.routes.length; j += 1) {
|
|
180
|
-
const other = builder.routes[j];
|
|
181
|
-
if (other.prerender === true) continue;
|
|
182
|
-
|
|
183
|
-
if (matches(route.segments, other.segments)) {
|
|
184
|
-
routes.push(other);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
generate_serverless_function(
|
|
189
|
-
builder,
|
|
190
|
-
routes,
|
|
191
|
-
patterns,
|
|
192
|
-
name,
|
|
193
|
-
`SvelteKit ${route.id}`,
|
|
194
|
-
'split',
|
|
195
|
-
undefined,
|
|
196
|
-
uuid
|
|
197
|
-
);
|
|
122
|
+
for (const fn of get_split_functions(builder)) {
|
|
123
|
+
generate_serverless_function(builder, fn, uuid);
|
|
198
124
|
}
|
|
199
|
-
|
|
200
|
-
generate_serverless_function(
|
|
201
|
-
builder,
|
|
202
|
-
[],
|
|
203
|
-
['/*'],
|
|
204
|
-
`${FUNCTION_PREFIX}catch-all`,
|
|
205
|
-
'SvelteKit catch-all',
|
|
206
|
-
'catch-all',
|
|
207
|
-
Array.from(seen),
|
|
208
|
-
uuid
|
|
209
|
-
);
|
|
210
125
|
} else {
|
|
211
|
-
generate_serverless_function(
|
|
212
|
-
|
|
213
|
-
undefined,
|
|
214
|
-
['/*'],
|
|
215
|
-
`${FUNCTION_PREFIX}render`,
|
|
216
|
-
'SvelteKit server'
|
|
217
|
-
|
|
218
|
-
);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* @returns {NetlifyConfig | null}
|
|
224
|
-
*/
|
|
225
|
-
function get_netlify_config() {
|
|
226
|
-
if (!existsSync('netlify.toml')) return null;
|
|
227
|
-
|
|
228
|
-
try {
|
|
229
|
-
return parse(readFileSync('netlify.toml', 'utf-8'));
|
|
230
|
-
} catch (err) {
|
|
231
|
-
if (err instanceof Error) {
|
|
232
|
-
throw new Error(`Failed to parse netlify.toml: ${err.message}`, { cause: err });
|
|
233
|
-
}
|
|
234
|
-
throw err;
|
|
126
|
+
generate_serverless_function(builder, {
|
|
127
|
+
type: 'singular',
|
|
128
|
+
routes: undefined,
|
|
129
|
+
patterns: ['/*'],
|
|
130
|
+
name: `${FUNCTION_PREFIX}render`,
|
|
131
|
+
display_name: 'SvelteKit server'
|
|
132
|
+
});
|
|
235
133
|
}
|
|
236
134
|
}
|
|
237
135
|
|
|
@@ -258,63 +156,143 @@ function write_frameworks_config({ builder }) {
|
|
|
258
156
|
writeFileSync(netlify_framework_config_path, s(config));
|
|
259
157
|
}
|
|
260
158
|
|
|
261
|
-
/** @typedef {'singular' | 'split' | 'catch-all'}
|
|
159
|
+
/** @typedef {'singular' | 'split' | 'catch-all'} EntrypointType */
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @typedef {object} EntrypointMetadata
|
|
163
|
+
* @property {EntrypointType} type
|
|
164
|
+
* @property {RouteDefinition[] | undefined} routes
|
|
165
|
+
* @property {string[]} patterns
|
|
166
|
+
* @property {string} name
|
|
167
|
+
* @property {string} display_name
|
|
168
|
+
* @property {string[]} [exclude]
|
|
169
|
+
*/
|
|
262
170
|
|
|
263
171
|
/**
|
|
264
172
|
* @param {Builder} builder
|
|
265
|
-
* @
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
173
|
+
* @returns {EntrypointMetadata[]}
|
|
174
|
+
*/
|
|
175
|
+
function get_split_functions(builder) {
|
|
176
|
+
const seen = new Set();
|
|
177
|
+
let index = 0;
|
|
178
|
+
|
|
179
|
+
/** @type {EntrypointMetadata[]} */
|
|
180
|
+
const functions = [];
|
|
181
|
+
|
|
182
|
+
for (let i = 0; i < builder.routes.length; i++) {
|
|
183
|
+
const route = builder.routes[i];
|
|
184
|
+
if (route.prerender === true) continue;
|
|
185
|
+
|
|
186
|
+
const routes = [route];
|
|
187
|
+
/** @type {string[]} */
|
|
188
|
+
const parts = [];
|
|
189
|
+
|
|
190
|
+
// The parts should conform to URLPattern syntax
|
|
191
|
+
// https://docs.netlify.com/build/functions/get-started/?fn-language=ts&data-tab=TypeScript#route-requests
|
|
192
|
+
for (const [i, segment] of route.segments.entries()) {
|
|
193
|
+
if (segment.rest) {
|
|
194
|
+
parts.push('*');
|
|
195
|
+
} else if (segment.dynamic) {
|
|
196
|
+
// URLPattern requires params to start with letters
|
|
197
|
+
const optional = /^\[\[.+\]\]$/.test(segment.content) ? '?' : '';
|
|
198
|
+
parts.push(`:param${i}${optional}`);
|
|
199
|
+
} else {
|
|
200
|
+
parts.push(segment.content);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Netlify handles trailing slashes for us, so we don't need to include them in the pattern
|
|
205
|
+
const pattern = `/${parts.join('/')}`;
|
|
206
|
+
|
|
207
|
+
// skip routes with identical patterns, they were already folded into another function
|
|
208
|
+
if (seen.has(pattern)) continue;
|
|
209
|
+
|
|
210
|
+
const patterns = [pattern, `${pattern === '/' ? '' : pattern}/__data.json`];
|
|
211
|
+
patterns.forEach((pattern) => seen.add(pattern));
|
|
212
|
+
|
|
213
|
+
// figure out which lower priority routes should be considered fallbacks
|
|
214
|
+
for (let j = i + 1; j < builder.routes.length; j += 1) {
|
|
215
|
+
const other = builder.routes[j];
|
|
216
|
+
if (other.prerender === true) continue;
|
|
217
|
+
|
|
218
|
+
if (matches(route.segments, other.segments)) {
|
|
219
|
+
routes.push(other);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
functions.push({
|
|
224
|
+
type: 'split',
|
|
225
|
+
routes,
|
|
226
|
+
patterns,
|
|
227
|
+
name: `${FUNCTION_PREFIX}${index++}`,
|
|
228
|
+
display_name: `SvelteKit ${route.id}`
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
functions.push({
|
|
233
|
+
type: 'catch-all',
|
|
234
|
+
routes: [],
|
|
235
|
+
patterns: ['/*'],
|
|
236
|
+
name: `${FUNCTION_PREFIX}catch-all`,
|
|
237
|
+
display_name: 'SvelteKit catch-all',
|
|
238
|
+
exclude: Array.from(seen)
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
return functions;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* @param {Builder} builder
|
|
246
|
+
* @param {EntrypointMetadata} fn
|
|
271
247
|
* @param {string} [uuid]
|
|
272
248
|
*/
|
|
273
|
-
function generate_serverless_function(
|
|
274
|
-
builder
|
|
275
|
-
|
|
276
|
-
patterns,
|
|
277
|
-
name,
|
|
278
|
-
display_name,
|
|
279
|
-
type,
|
|
280
|
-
exclude,
|
|
281
|
-
uuid
|
|
282
|
-
) {
|
|
283
|
-
builder.generateServerInstance(`.netlify/v1/server-${name}.js`, {
|
|
284
|
-
routes,
|
|
249
|
+
function generate_serverless_function(builder, fn, uuid) {
|
|
250
|
+
builder.generateServerInstance(`.netlify/v1/server-${fn.name}.js`, {
|
|
251
|
+
routes: fn.routes,
|
|
285
252
|
serverDirectory: '.netlify/v1/server'
|
|
286
253
|
});
|
|
287
254
|
|
|
288
|
-
const
|
|
289
|
-
const
|
|
255
|
+
const filename = `${netlify_framework_serverless_path}/${fn.name}.mjs`;
|
|
256
|
+
const code = generate_function_module(
|
|
257
|
+
fn.type,
|
|
258
|
+
'../serverless.js',
|
|
259
|
+
`../server-${fn.name}.js`,
|
|
260
|
+
uuid
|
|
261
|
+
);
|
|
262
|
+
const config = create_function_config('serverless', fn);
|
|
290
263
|
|
|
291
264
|
if (builder.hasServerInstrumentationFile()) {
|
|
292
|
-
writeFileSync(
|
|
265
|
+
writeFileSync(filename, code);
|
|
293
266
|
const initializer = builder.createInstrumentationInitializer({
|
|
294
267
|
outputDirectory: netlify_framework_serverless_path,
|
|
295
268
|
serverDirectory: '.netlify/v1/server'
|
|
296
269
|
});
|
|
297
270
|
builder.instrument({
|
|
298
|
-
entrypoint:
|
|
271
|
+
entrypoint: filename,
|
|
299
272
|
instrumentation: '.netlify/v1/server/instrumentation.server.js',
|
|
300
|
-
start: `.netlify/v1/server/${name}.start.mjs`,
|
|
273
|
+
start: `.netlify/v1/server/${fn.name}.start.mjs`,
|
|
301
274
|
initializer,
|
|
302
275
|
module: {
|
|
303
276
|
generateText: generate_traced_module(config)
|
|
304
277
|
}
|
|
305
278
|
});
|
|
306
279
|
} else {
|
|
307
|
-
writeFileSync(
|
|
280
|
+
writeFileSync(filename, `${code}\n${config}`);
|
|
308
281
|
}
|
|
309
282
|
}
|
|
310
283
|
|
|
311
284
|
/**
|
|
312
|
-
* @param {
|
|
313
|
-
* @param {
|
|
285
|
+
* @param {EntrypointType} type
|
|
286
|
+
* @param {string} init
|
|
287
|
+
* @param {string} server
|
|
314
288
|
* @param {string} [uuid]
|
|
315
289
|
* @returns {string}
|
|
316
290
|
*/
|
|
317
|
-
function
|
|
291
|
+
function generate_function_module(type, init, server, uuid) {
|
|
292
|
+
const runtime_imports = [
|
|
293
|
+
`import { init } from '${init}';`,
|
|
294
|
+
`import { server } from '${server}';`
|
|
295
|
+
].join('\n');
|
|
318
296
|
const original_pathname_header = `const original_pathname_header = \`x-sveltekit-original-pathname-${uuid}\``;
|
|
319
297
|
|
|
320
298
|
if (type === 'catch-all' && uuid) {
|
|
@@ -323,8 +301,7 @@ function generate_serverless_function_module(name, type, uuid) {
|
|
|
323
301
|
// to correctly re-encode it on the way out.
|
|
324
302
|
return `\
|
|
325
303
|
import { applyReroute } from '@sveltejs/kit/adapter';
|
|
326
|
-
|
|
327
|
-
import { server } from '../server-${name}.js';
|
|
304
|
+
${runtime_imports}
|
|
328
305
|
|
|
329
306
|
${original_pathname_header}
|
|
330
307
|
|
|
@@ -353,8 +330,7 @@ export default async (request, context) => {
|
|
|
353
330
|
|
|
354
331
|
if (type === 'split' && uuid) {
|
|
355
332
|
return `\
|
|
356
|
-
|
|
357
|
-
import { server } from '../server-${name}.js';
|
|
333
|
+
${runtime_imports}
|
|
358
334
|
|
|
359
335
|
${original_pathname_header}
|
|
360
336
|
|
|
@@ -374,8 +350,7 @@ export default async (request, context) => {
|
|
|
374
350
|
}
|
|
375
351
|
|
|
376
352
|
return `\
|
|
377
|
-
|
|
378
|
-
import { server } from '../server-${name}.js';
|
|
353
|
+
${runtime_imports}
|
|
379
354
|
|
|
380
355
|
export default init(server);
|
|
381
356
|
`;
|
|
@@ -384,22 +359,24 @@ export default init(server);
|
|
|
384
359
|
const generator_string = `@sveltejs/adapter-netlify@${adapter_version}`;
|
|
385
360
|
|
|
386
361
|
/**
|
|
387
|
-
* @param {
|
|
388
|
-
* @param {
|
|
389
|
-
* @param {string[]} [exclude]
|
|
362
|
+
* @param {'serverless' | 'edge'} runtime
|
|
363
|
+
* @param {EntrypointMetadata} fn
|
|
390
364
|
* @returns {string}
|
|
391
365
|
*/
|
|
392
|
-
function
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
366
|
+
function create_function_config(runtime, fn) {
|
|
367
|
+
/** @type {IntegrationsConfig & { preferStatic?: boolean }} */
|
|
368
|
+
const config = {
|
|
369
|
+
name: fn.display_name,
|
|
370
|
+
generator: generator_string,
|
|
371
|
+
path: /** @type {`/${string}`[]} */ (fn.patterns),
|
|
372
|
+
excludedPath: /** @type {`/${string}`[]} */ (['/.netlify/*', ...(fn.exclude ?? [])])
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
if (runtime === 'serverless') {
|
|
376
|
+
config.preferStatic = true;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
return `export const config = ${s(config)};\n`;
|
|
403
380
|
}
|
|
404
381
|
|
|
405
382
|
/**
|
|
@@ -442,28 +419,21 @@ const rolldown_config = {
|
|
|
442
419
|
};
|
|
443
420
|
|
|
444
421
|
/**
|
|
445
|
-
* @param {
|
|
446
|
-
* @param {
|
|
422
|
+
* @param {object} params
|
|
423
|
+
* @param {Builder} params.builder
|
|
424
|
+
* @param {boolean} params.split
|
|
447
425
|
*/
|
|
448
|
-
async function generate_edge_functions({ builder }) {
|
|
426
|
+
async function generate_edge_functions({ builder, split }) {
|
|
449
427
|
const tmp = builder.getBuildDirectory('netlify-tmp');
|
|
450
428
|
rmSync(tmp, { force: true, recursive: true });
|
|
451
429
|
mkdirSync(tmp, { recursive: true });
|
|
452
430
|
|
|
453
431
|
// https://docs.netlify.com/build/frameworks/frameworks-api/#edge-functions
|
|
454
|
-
mkdirSync(
|
|
432
|
+
mkdirSync(netlify_framework_edge_path, { recursive: true });
|
|
455
433
|
|
|
456
|
-
builder.log.minor('Generating
|
|
434
|
+
builder.log.minor('Generating edge functions...');
|
|
435
|
+
builder.copy(`${files}/edge.js`, `${tmp}/edge.js`);
|
|
457
436
|
|
|
458
|
-
builder.copy(`${files}/edge.js`, `${tmp}/entry.js`, {
|
|
459
|
-
replace: {
|
|
460
|
-
'0SERVER': `./server.js`
|
|
461
|
-
}
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
builder.generateServerInstance(`${tmp}/server.js`);
|
|
465
|
-
|
|
466
|
-
const path = '/*';
|
|
467
437
|
// We only need to specify paths without the trailing slash because
|
|
468
438
|
// Netlify will handle the optional trailing slash for us
|
|
469
439
|
const excluded_paths = [
|
|
@@ -478,56 +448,138 @@ async function generate_edge_functions({ builder }) {
|
|
|
478
448
|
return [`${builder.config.paths.base}/${asset}`, `${builder.config.paths.base}/${dir}`];
|
|
479
449
|
}
|
|
480
450
|
return `${builder.config.paths.base}/${asset}`;
|
|
481
|
-
})
|
|
482
|
-
// Should not be served by SvelteKit at all
|
|
483
|
-
'/.netlify/*'
|
|
451
|
+
})
|
|
484
452
|
];
|
|
485
453
|
|
|
454
|
+
/** @type {EntrypointMetadata[]} */
|
|
455
|
+
const functions = split
|
|
456
|
+
? get_split_functions(builder)
|
|
457
|
+
: [
|
|
458
|
+
{
|
|
459
|
+
type: 'singular',
|
|
460
|
+
routes: undefined,
|
|
461
|
+
patterns: ['/*'],
|
|
462
|
+
name: `${FUNCTION_PREFIX}render`,
|
|
463
|
+
display_name: 'SvelteKit server'
|
|
464
|
+
}
|
|
465
|
+
];
|
|
466
|
+
const reroute_id = split ? crypto.randomUUID() : undefined;
|
|
467
|
+
|
|
468
|
+
for (const fn of functions) {
|
|
469
|
+
await generate_edge_function(
|
|
470
|
+
builder,
|
|
471
|
+
tmp,
|
|
472
|
+
{
|
|
473
|
+
...fn,
|
|
474
|
+
exclude: excluded_paths.concat(fn.exclude ?? [])
|
|
475
|
+
},
|
|
476
|
+
reroute_id
|
|
477
|
+
);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
/**
|
|
482
|
+
* @param {Builder} builder
|
|
483
|
+
* @param {string} tmp
|
|
484
|
+
* @param {EntrypointMetadata} fn
|
|
485
|
+
* @param {string | undefined} reroute_id
|
|
486
|
+
*/
|
|
487
|
+
async function generate_edge_function(builder, tmp, fn, reroute_id) {
|
|
488
|
+
builder.generateServerInstance(`${tmp}/server-${fn.name}.js`, { routes: fn.routes });
|
|
489
|
+
|
|
490
|
+
const entry = `${tmp}/entry-${fn.name}.js`;
|
|
491
|
+
const code = generate_edge_function_module(
|
|
492
|
+
fn.type,
|
|
493
|
+
'./edge.js',
|
|
494
|
+
`./server-${fn.name}.js`,
|
|
495
|
+
reroute_id
|
|
496
|
+
);
|
|
497
|
+
const config = create_function_config('edge', fn);
|
|
498
|
+
writeFileSync(entry, `${code}\n${config}`);
|
|
499
|
+
|
|
500
|
+
let input = entry;
|
|
486
501
|
if (builder.hasServerInstrumentationFile()) {
|
|
487
502
|
const initializer = builder.createInstrumentationInitializer({
|
|
488
503
|
outputDirectory: tmp,
|
|
489
504
|
environment: 'export default Deno.env.toObject();\n'
|
|
490
505
|
});
|
|
491
|
-
|
|
506
|
+
input = `${tmp}/instrumented-entry-${fn.name}.js`;
|
|
507
|
+
writeFileSync(input, `export { default, config } from './entry-${fn.name}.js';\n`);
|
|
492
508
|
builder.instrument({
|
|
493
|
-
entrypoint:
|
|
509
|
+
entrypoint: input,
|
|
494
510
|
instrumentation: `${builder.getServerDirectory()}/instrumentation.server.js`,
|
|
495
|
-
initializer
|
|
511
|
+
initializer,
|
|
512
|
+
module: {
|
|
513
|
+
generateText: generate_traced_module(config)
|
|
514
|
+
}
|
|
496
515
|
});
|
|
497
516
|
}
|
|
498
517
|
|
|
499
518
|
await build({
|
|
500
519
|
...rolldown_config,
|
|
501
|
-
input
|
|
502
|
-
? `${tmp}/instrumented-entry.js`
|
|
503
|
-
: `${tmp}/entry.js`,
|
|
520
|
+
input,
|
|
504
521
|
output: {
|
|
505
522
|
...rolldown_config.output,
|
|
506
|
-
file: `${netlify_framework_edge_path}/${
|
|
523
|
+
file: `${netlify_framework_edge_path}/${fn.name}.js`
|
|
507
524
|
}
|
|
508
525
|
});
|
|
509
|
-
|
|
510
|
-
add_edge_function_config({ builder, path, excluded_paths });
|
|
511
526
|
}
|
|
512
527
|
|
|
513
528
|
/**
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
* @param {
|
|
529
|
+
* @param {EntrypointType} type
|
|
530
|
+
* @param {string} init
|
|
531
|
+
* @param {string} server
|
|
532
|
+
* @param {string | undefined} reroute_id
|
|
533
|
+
* @returns {string}
|
|
517
534
|
*/
|
|
518
|
-
function
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
config.edge_functions = [
|
|
523
|
-
{
|
|
524
|
-
function: `${FUNCTION_PREFIX}render`,
|
|
525
|
-
name: 'SvelteKit server',
|
|
526
|
-
generator: generator_string,
|
|
527
|
-
path,
|
|
528
|
-
excludedPath: excluded_paths
|
|
529
|
-
}
|
|
530
|
-
];
|
|
535
|
+
function generate_edge_function_module(type, init, server, reroute_id) {
|
|
536
|
+
if (type === 'singular') {
|
|
537
|
+
return generate_function_module(type, init, server);
|
|
538
|
+
}
|
|
531
539
|
|
|
532
|
-
|
|
540
|
+
const runtime_imports = [
|
|
541
|
+
`import { init } from '${init}';`,
|
|
542
|
+
`import { server } from '${server}';`
|
|
543
|
+
].join('\n');
|
|
544
|
+
const reroute_parameter = `__sveltekit_original_pathname_${reroute_id}`;
|
|
545
|
+
|
|
546
|
+
if (type === 'catch-all') {
|
|
547
|
+
return `\
|
|
548
|
+
import { applyReroute } from '@sveltejs/kit/adapter';
|
|
549
|
+
${runtime_imports}
|
|
550
|
+
|
|
551
|
+
const respond = init(server);
|
|
552
|
+
|
|
553
|
+
export default async (request, context) => {
|
|
554
|
+
const response = await respond(request, context);
|
|
555
|
+
|
|
556
|
+
let rerouted_url;
|
|
557
|
+
applyReroute(response, (url) => {
|
|
558
|
+
url.searchParams.set('${reroute_parameter}', new URL(request.url).pathname);
|
|
559
|
+
rerouted_url = url;
|
|
560
|
+
});
|
|
561
|
+
|
|
562
|
+
return rerouted_url ?? response;
|
|
563
|
+
};
|
|
564
|
+
`;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return `\
|
|
568
|
+
${runtime_imports}
|
|
569
|
+
|
|
570
|
+
const respond = init(server);
|
|
571
|
+
|
|
572
|
+
export default async (request, context) => {
|
|
573
|
+
const url = new URL(request.url);
|
|
574
|
+
const pathname = url.searchParams.get('${reroute_parameter}');
|
|
575
|
+
|
|
576
|
+
if (pathname) {
|
|
577
|
+
url.pathname = pathname;
|
|
578
|
+
url.searchParams.delete('${reroute_parameter}');
|
|
579
|
+
request = new Request(url, request);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
return await respond(request, context);
|
|
583
|
+
};
|
|
584
|
+
`;
|
|
533
585
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sveltejs/adapter-netlify",
|
|
3
|
-
"version": "7.0.0-next.
|
|
3
|
+
"version": "7.0.0-next.12",
|
|
4
4
|
"description": "A SvelteKit adapter that creates a Netlify app",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"adapter",
|
|
@@ -35,26 +35,26 @@
|
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@netlify/types": "^2.8.0",
|
|
38
|
-
"rolldown": "^1.2.3"
|
|
39
|
-
"smol-toml": "^1.8.0"
|
|
38
|
+
"rolldown": "^1.2.3"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
41
|
"@netlify/edge-functions": "^3.0.8",
|
|
43
|
-
"@sveltejs/kit": "^3.0.0-next.
|
|
42
|
+
"@sveltejs/kit": "^3.0.0-next.28",
|
|
44
43
|
"@types/deno": "^2.7.0",
|
|
45
44
|
"@types/node": "^22.19.19",
|
|
46
|
-
"typescript": "
|
|
47
|
-
"vitest": "^
|
|
45
|
+
"@typescript/native": "npm:typescript@^7.0.2",
|
|
46
|
+
"vitest": "^5.0.1"
|
|
48
47
|
},
|
|
49
48
|
"peerDependencies": {
|
|
50
49
|
"@sveltejs/kit": "^3.0.0-next.0"
|
|
51
50
|
},
|
|
52
51
|
"scripts": {
|
|
53
|
-
"check": "tsc && tsc --project files/tsconfig.json",
|
|
52
|
+
"check": "pnpm tsc-native && pnpm tsc-native --project files/tsconfig.json",
|
|
54
53
|
"lint": "oxfmt --check .",
|
|
55
54
|
"format": "oxfmt --write .",
|
|
56
55
|
"test:unit": "vitest run",
|
|
57
56
|
"test:build": "pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test:build",
|
|
58
|
-
"test": "pnpm test:unit && pnpm test:build"
|
|
57
|
+
"test": "pnpm test:unit && pnpm test:build",
|
|
58
|
+
"tsc-native": "node node_modules/@typescript/native/bin/tsc"
|
|
59
59
|
}
|
|
60
60
|
}
|
package/utils.js
CHANGED
|
@@ -1,17 +1,7 @@
|
|
|
1
|
-
import { resolve } from 'node:path';
|
|
2
|
-
import process from 'node:process';
|
|
3
|
-
|
|
4
1
|
/**
|
|
5
2
|
* @typedef {{ rest: boolean, dynamic: boolean, content: string }} RouteSegment
|
|
6
3
|
*/
|
|
7
4
|
|
|
8
|
-
/**
|
|
9
|
-
* @typedef {{
|
|
10
|
-
* build?: { publish?: string }
|
|
11
|
-
* functions?: { node_bundler?: 'zisi' | 'esbuild' }
|
|
12
|
-
* }} NetlifyConfig
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
5
|
/**
|
|
16
6
|
* @param {RouteSegment[]} a
|
|
17
7
|
* @param {RouteSegment[]} b
|
|
@@ -44,31 +34,6 @@ export function matches(a, b) {
|
|
|
44
34
|
}
|
|
45
35
|
}
|
|
46
36
|
|
|
47
|
-
/**
|
|
48
|
-
* @param {NetlifyConfig | null} netlify_config
|
|
49
|
-
* @param {import('@sveltejs/kit').Builder} builder
|
|
50
|
-
* @returns {string | undefined}
|
|
51
|
-
*/
|
|
52
|
-
export function get_publish_directory(netlify_config, builder) {
|
|
53
|
-
if (netlify_config) {
|
|
54
|
-
if (!netlify_config.build?.publish) {
|
|
55
|
-
builder.log.minor('No publish directory specified in netlify.toml, using default');
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (resolve(netlify_config.build.publish) === process.cwd()) {
|
|
60
|
-
throw new Error(
|
|
61
|
-
'The publish directory cannot be set to the site root. Please change it to another value such as "build" in netlify.toml.'
|
|
62
|
-
);
|
|
63
|
-
}
|
|
64
|
-
return netlify_config.build.publish;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
builder.log.warn(
|
|
68
|
-
'No netlify.toml found. Using default publish directory. Consult https://svelte.dev/docs/kit/adapter-netlify#usage for more details'
|
|
69
|
-
);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
37
|
/**
|
|
73
38
|
* @param {*} value
|
|
74
39
|
* @returns {string}
|