@sveltejs/adapter-netlify 7.0.0-next.1 → 7.0.0-next.11
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/ambient.d.ts +1 -1
- package/files/edge.js +20 -13
- package/files/serverless.js +9 -23
- package/index.d.ts +16 -1
- package/index.js +205 -128
- package/package.json +17 -20
- package/utils.js +78 -0
package/ambient.d.ts
CHANGED
package/files/edge.js
CHANGED
|
@@ -1,28 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/edge.js
|
|
4
|
-
const server = new Server(manifest);
|
|
1
|
+
import { server } from '0SERVER';
|
|
2
|
+
|
|
5
3
|
/**
|
|
6
|
-
* We don't know the origin until we receive a request, but
|
|
7
|
-
* that's guaranteed to happen before we call `read`
|
|
8
|
-
* @type {string}
|
|
9
|
-
*/
|
|
4
|
+
* We don't know the origin until we receive a request, but
|
|
5
|
+
* that's guaranteed to happen before we call `read`
|
|
6
|
+
* @type {string}
|
|
7
|
+
*/
|
|
10
8
|
let origin;
|
|
9
|
+
|
|
11
10
|
const initialized = server.init({
|
|
12
11
|
env: Deno.env.toObject(),
|
|
13
12
|
read: async (file) => {
|
|
14
13
|
const url = `${origin}/${file}`;
|
|
15
14
|
const response = await fetch(url);
|
|
16
|
-
|
|
15
|
+
|
|
16
|
+
if (!response.ok) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`read(...) failed: could not fetch ${url} (${response.status} ${response.statusText})`
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
17
22
|
return response.body;
|
|
18
23
|
}
|
|
19
24
|
});
|
|
25
|
+
|
|
20
26
|
/** @type {import('@netlify/edge-functions').EdgeFunction} */
|
|
21
|
-
async function handler(request, context) {
|
|
27
|
+
export default async function handler(request, context) {
|
|
22
28
|
if (!origin) {
|
|
23
29
|
origin = new URL(request.url).origin;
|
|
24
|
-
await initialized;
|
|
25
30
|
}
|
|
31
|
+
|
|
32
|
+
// always await initialization to prevent race condition with concurrent requests
|
|
33
|
+
await initialized;
|
|
34
|
+
|
|
26
35
|
return server.respond(request, {
|
|
27
36
|
platform: { context },
|
|
28
37
|
getClientAddress() {
|
|
@@ -30,5 +39,3 @@ async function handler(request, context) {
|
|
|
30
39
|
}
|
|
31
40
|
});
|
|
32
41
|
}
|
|
33
|
-
//#endregion
|
|
34
|
-
export { handler as default };
|
package/files/serverless.js
CHANGED
|
@@ -1,35 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import process from "node:process";
|
|
5
|
-
//#region ../kit/src/exports/node/index.js
|
|
1
|
+
import { createReadableStream } from '@sveltejs/kit/node';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
|
|
6
4
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*/
|
|
12
|
-
function createReadableStream(file) {
|
|
13
|
-
return Readable.toWeb(createReadStream(file));
|
|
14
|
-
}
|
|
15
|
-
//#endregion
|
|
16
|
-
//#region src/serverless.js
|
|
17
|
-
/**
|
|
18
|
-
* @param {import('@sveltejs/kit').SSRManifest} manifest
|
|
19
|
-
* @returns {(request: Request, context: import('@netlify/functions').Context) => Promise<Response>}
|
|
20
|
-
*/
|
|
21
|
-
function init(manifest) {
|
|
22
|
-
const server = new Server(manifest);
|
|
5
|
+
* @param {import('@sveltejs/kit').Server} server
|
|
6
|
+
* @returns {(request: Request, context: import('@netlify/types').Context) => Promise<Response>}
|
|
7
|
+
*/
|
|
8
|
+
export function init(server) {
|
|
23
9
|
/** @type {Promise<void> | null} */
|
|
24
10
|
let init_promise = server.init({
|
|
25
11
|
env: process.env,
|
|
26
12
|
read: (file) => createReadableStream(`.netlify/server/${file}`)
|
|
27
13
|
});
|
|
14
|
+
|
|
28
15
|
return async (request, context) => {
|
|
29
16
|
if (init_promise !== null) {
|
|
30
17
|
await init_promise;
|
|
31
18
|
init_promise = null;
|
|
32
19
|
}
|
|
20
|
+
|
|
33
21
|
return server.respond(request, {
|
|
34
22
|
platform: { context },
|
|
35
23
|
getClientAddress() {
|
|
@@ -38,5 +26,3 @@ function init(manifest) {
|
|
|
38
26
|
});
|
|
39
27
|
};
|
|
40
28
|
}
|
|
41
|
-
//#endregion
|
|
42
|
-
export { init };
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,19 @@
|
|
|
1
1
|
import { Adapter } from '@sveltejs/kit';
|
|
2
2
|
import './ambient.d.ts';
|
|
3
3
|
|
|
4
|
-
export
|
|
4
|
+
export interface AdapterOptions {
|
|
5
|
+
/**
|
|
6
|
+
* If `true`, your app will be deployed as a [Netlify Edge Function](https://docs.netlify.com/build/edge-functions/overview/) rather than the standard Node-based function.
|
|
7
|
+
* @default false
|
|
8
|
+
*/
|
|
9
|
+
edge?: boolean;
|
|
10
|
+
/**
|
|
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
|
+
* @default false
|
|
15
|
+
*/
|
|
16
|
+
split?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export default function plugin(opts?: AdapterOptions): Adapter;
|
package/index.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
/** @import { Builder, RouteDefinition } from '@sveltejs/kit' */
|
|
2
|
+
/** @import { TomlTable } from 'smol-toml' */
|
|
3
|
+
import crypto from 'node:crypto';
|
|
4
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import { join } from 'node:path';
|
|
3
6
|
import { fileURLToPath } from 'node:url';
|
|
4
7
|
import { builtinModules } from 'node:module';
|
|
5
8
|
import process from 'node:process';
|
|
6
|
-
import
|
|
9
|
+
import { parse } from 'smol-toml';
|
|
7
10
|
import { build } from 'rolldown';
|
|
8
11
|
import { matches, get_publish_directory, s } from './utils.js';
|
|
9
12
|
|
|
@@ -11,7 +14,7 @@ import { matches, get_publish_directory, s } from './utils.js';
|
|
|
11
14
|
* @typedef {{
|
|
12
15
|
* build?: { publish?: string }
|
|
13
16
|
* functions?: { node_bundler?: 'zisi' | 'esbuild' }
|
|
14
|
-
* } &
|
|
17
|
+
* } & TomlTable} NetlifyConfig
|
|
15
18
|
*/
|
|
16
19
|
|
|
17
20
|
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf-8'));
|
|
@@ -30,7 +33,7 @@ const netlify_framework_edge_path = '.netlify/v1/edge-functions';
|
|
|
30
33
|
|
|
31
34
|
const FUNCTION_PREFIX = 'sveltekit-';
|
|
32
35
|
|
|
33
|
-
/** @type {import('./index.js').default} */
|
|
36
|
+
/** @type {typeof import('./index.js').default} */
|
|
34
37
|
export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
35
38
|
return {
|
|
36
39
|
name,
|
|
@@ -42,15 +45,15 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
42
45
|
);
|
|
43
46
|
}
|
|
44
47
|
|
|
45
|
-
if (existsSync(`${builder.config.
|
|
48
|
+
if (existsSync(`${builder.config.files.assets}/_headers`)) {
|
|
46
49
|
throw new Error(
|
|
47
|
-
`The _headers file should be placed in the project root rather than the ${builder.config.
|
|
50
|
+
`The _headers file should be placed in the project root rather than the ${builder.config.files.assets} directory`
|
|
48
51
|
);
|
|
49
52
|
}
|
|
50
53
|
|
|
51
|
-
if (existsSync(`${builder.config.
|
|
54
|
+
if (existsSync(`${builder.config.files.assets}/_redirects`)) {
|
|
52
55
|
throw new Error(
|
|
53
|
-
`The _redirects file should be placed in the project root rather than the ${builder.config.
|
|
56
|
+
`The _redirects file should be placed in the project root rather than the ${builder.config.files.assets} directory`
|
|
54
57
|
);
|
|
55
58
|
}
|
|
56
59
|
|
|
@@ -60,19 +63,19 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
60
63
|
const publish = get_publish_directory(netlify_config, builder) || 'build';
|
|
61
64
|
|
|
62
65
|
// empty out existing build directories
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
rmSync(publish, { force: true, recursive: true });
|
|
67
|
+
rmSync('.netlify/v1', { force: true, recursive: true });
|
|
65
68
|
|
|
66
69
|
// clean up legacy directories from older adapter versions to avoid
|
|
67
70
|
// gnarly edge cases when an existing project is upgraded to this version
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
rmSync('.netlify/edge-functions', { force: true, recursive: true });
|
|
72
|
+
rmSync('.netlify/server', { force: true, recursive: true });
|
|
73
|
+
rmSync('.netlify/package.json', { force: true, recursive: true });
|
|
74
|
+
rmSync('.netlify/serverless.js', { force: true, recursive: true });
|
|
72
75
|
if (existsSync('.netlify/functions-internal')) {
|
|
73
76
|
for (const file of readdirSync('.netlify/functions-internal')) {
|
|
74
77
|
if (file.startsWith(FUNCTION_PREFIX)) {
|
|
75
|
-
|
|
78
|
+
rmSync(join('.netlify/functions-internal', file), { force: true, recursive: true });
|
|
76
79
|
}
|
|
77
80
|
}
|
|
78
81
|
}
|
|
@@ -80,15 +83,22 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
80
83
|
builder.log.minor(`Publishing to "${publish}"`);
|
|
81
84
|
|
|
82
85
|
builder.log.minor('Copying assets...');
|
|
83
|
-
const publish_dir = `${publish}${builder.config.
|
|
86
|
+
const publish_dir = `${publish}${builder.config.paths.base}`;
|
|
84
87
|
builder.writeClient(publish_dir);
|
|
85
88
|
builder.writePrerendered(publish_dir);
|
|
86
89
|
|
|
87
|
-
// Copy user's
|
|
90
|
+
// Copy user's _headers file if it exists
|
|
88
91
|
if (existsSync('_headers')) {
|
|
92
|
+
builder.log.minor('Copying user custom headers...');
|
|
89
93
|
builder.copy('_headers', join(publish, '_headers'));
|
|
90
94
|
}
|
|
91
95
|
|
|
96
|
+
// Copy user's _redirects file if it exists
|
|
97
|
+
if (existsSync('_redirects')) {
|
|
98
|
+
builder.log.minor('Copying user redirects...');
|
|
99
|
+
builder.copy('_redirects', join(publish, '_redirects'));
|
|
100
|
+
}
|
|
101
|
+
|
|
92
102
|
builder.log.minor('Writing Netlify config...');
|
|
93
103
|
write_frameworks_config({ builder });
|
|
94
104
|
|
|
@@ -99,7 +109,7 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
99
109
|
|
|
100
110
|
await generate_edge_functions({ builder });
|
|
101
111
|
} else {
|
|
102
|
-
generate_serverless_functions(
|
|
112
|
+
generate_serverless_functions(builder, split);
|
|
103
113
|
}
|
|
104
114
|
},
|
|
105
115
|
|
|
@@ -111,27 +121,23 @@ export default function ({ split = false, edge = edge_set_in_env_var } = {}) {
|
|
|
111
121
|
}
|
|
112
122
|
|
|
113
123
|
/**
|
|
114
|
-
* @param {
|
|
115
|
-
* @param {
|
|
116
|
-
* @param { string } params.publish
|
|
117
|
-
* @param { boolean } params.split
|
|
124
|
+
* @param {Builder} builder
|
|
125
|
+
* @param {boolean} split
|
|
118
126
|
*/
|
|
119
|
-
function generate_serverless_functions(
|
|
127
|
+
function generate_serverless_functions(builder, split) {
|
|
120
128
|
// https://docs.netlify.com/build/frameworks/frameworks-api/#netlifyv1functions
|
|
121
|
-
|
|
129
|
+
mkdirSync(netlify_framework_serverless_path, { recursive: true });
|
|
122
130
|
|
|
123
131
|
builder.writeServer('.netlify/v1/server');
|
|
124
132
|
|
|
125
|
-
|
|
126
|
-
'0SERVER': './server/index.js' // digit prefix prevents CJS build from using this as a variable name, which would also get replaced
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
builder.copy(files, '.netlify/v1', { replace, filter: (file) => !file.endsWith('edge.js') });
|
|
133
|
+
builder.copy(`${files}/serverless.js`, '.netlify/v1/serverless.js');
|
|
130
134
|
|
|
131
135
|
builder.log.minor('Generating serverless functions...');
|
|
132
136
|
|
|
133
137
|
if (split) {
|
|
134
138
|
const seen = new Set();
|
|
139
|
+
let index = 0;
|
|
140
|
+
const uuid = crypto.randomUUID();
|
|
135
141
|
|
|
136
142
|
for (let i = 0; i < builder.routes.length; i++) {
|
|
137
143
|
const route = builder.routes[i];
|
|
@@ -144,12 +150,13 @@ function generate_serverless_functions({ builder, publish, split }) {
|
|
|
144
150
|
|
|
145
151
|
// The parts should conform to URLPattern syntax
|
|
146
152
|
// https://docs.netlify.com/build/functions/get-started/?fn-language=ts&data-tab=TypeScript#route-requests
|
|
147
|
-
for (const segment of route.segments) {
|
|
153
|
+
for (const [i, segment] of route.segments.entries()) {
|
|
148
154
|
if (segment.rest) {
|
|
149
155
|
parts.push('*');
|
|
150
156
|
} else if (segment.dynamic) {
|
|
151
157
|
// URLPattern requires params to start with letters
|
|
152
|
-
|
|
158
|
+
const optional = /^\[\[.+\]\]$/.test(segment.content) ? '?' : '';
|
|
159
|
+
parts.push(`:param${i}${optional}`);
|
|
153
160
|
} else {
|
|
154
161
|
parts.push(segment.content);
|
|
155
162
|
}
|
|
@@ -157,14 +164,16 @@ function generate_serverless_functions({ builder, publish, split }) {
|
|
|
157
164
|
|
|
158
165
|
// Netlify handles trailing slashes for us, so we don't need to include them in the pattern
|
|
159
166
|
const pattern = `/${parts.join('/')}`;
|
|
160
|
-
const name =
|
|
161
|
-
FUNCTION_PREFIX + (parts.join('-').replace(/[:.]/g, '_').replace('*', '__rest') || 'index');
|
|
162
167
|
|
|
163
168
|
// skip routes with identical patterns, they were already folded into another function
|
|
164
169
|
if (seen.has(pattern)) continue;
|
|
165
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
|
+
|
|
166
175
|
const patterns = [pattern, `${pattern === '/' ? '' : pattern}/__data.json`];
|
|
167
|
-
patterns.forEach((
|
|
176
|
+
patterns.forEach((pattern) => seen.add(pattern));
|
|
168
177
|
|
|
169
178
|
// figure out which lower priority routes should be considered fallbacks
|
|
170
179
|
for (let j = i + 1; j < builder.routes.length; j += 1) {
|
|
@@ -176,35 +185,37 @@ function generate_serverless_functions({ builder, publish, split }) {
|
|
|
176
185
|
}
|
|
177
186
|
}
|
|
178
187
|
|
|
179
|
-
generate_serverless_function(
|
|
188
|
+
generate_serverless_function(
|
|
180
189
|
builder,
|
|
181
190
|
routes,
|
|
182
191
|
patterns,
|
|
183
|
-
name
|
|
184
|
-
|
|
192
|
+
name,
|
|
193
|
+
`SvelteKit ${route.id}`,
|
|
194
|
+
'split',
|
|
195
|
+
undefined,
|
|
196
|
+
uuid
|
|
197
|
+
);
|
|
185
198
|
}
|
|
186
199
|
|
|
187
|
-
generate_serverless_function(
|
|
200
|
+
generate_serverless_function(
|
|
188
201
|
builder,
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
202
|
+
[],
|
|
203
|
+
['/*'],
|
|
204
|
+
`${FUNCTION_PREFIX}catch-all`,
|
|
205
|
+
'SvelteKit catch-all',
|
|
206
|
+
'catch-all',
|
|
207
|
+
Array.from(seen),
|
|
208
|
+
uuid
|
|
209
|
+
);
|
|
194
210
|
} else {
|
|
195
|
-
generate_serverless_function(
|
|
211
|
+
generate_serverless_function(
|
|
196
212
|
builder,
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
// Copy user's custom _redirects file if it exists
|
|
204
|
-
if (existsSync('_redirects')) {
|
|
205
|
-
builder.log.minor('Copying user redirects...');
|
|
206
|
-
const redirects_file = join(publish, '_redirects');
|
|
207
|
-
builder.copy('_redirects', redirects_file);
|
|
213
|
+
undefined,
|
|
214
|
+
['/*'],
|
|
215
|
+
`${FUNCTION_PREFIX}render`,
|
|
216
|
+
'SvelteKit server',
|
|
217
|
+
'singular'
|
|
218
|
+
);
|
|
208
219
|
}
|
|
209
220
|
}
|
|
210
221
|
|
|
@@ -215,7 +226,7 @@ function get_netlify_config() {
|
|
|
215
226
|
if (!existsSync('netlify.toml')) return null;
|
|
216
227
|
|
|
217
228
|
try {
|
|
218
|
-
return
|
|
229
|
+
return parse(readFileSync('netlify.toml', 'utf-8'));
|
|
219
230
|
} catch (err) {
|
|
220
231
|
if (err instanceof Error) {
|
|
221
232
|
throw new Error(`Failed to parse netlify.toml: ${err.message}`, { cause: err });
|
|
@@ -243,35 +254,51 @@ function write_frameworks_config({ builder }) {
|
|
|
243
254
|
]
|
|
244
255
|
};
|
|
245
256
|
|
|
246
|
-
|
|
257
|
+
mkdirSync('.netlify/v1', { recursive: true });
|
|
247
258
|
writeFileSync(netlify_framework_config_path, s(config));
|
|
248
259
|
}
|
|
249
260
|
|
|
261
|
+
/** @typedef {'singular' | 'split' | 'catch-all'} ServerlessFunctionType */
|
|
262
|
+
|
|
250
263
|
/**
|
|
251
|
-
*
|
|
252
|
-
* @param {
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
*
|
|
257
|
-
*
|
|
258
|
-
* }
|
|
264
|
+
* @param {Builder} builder
|
|
265
|
+
* @param {RouteDefinition[] | undefined} routes
|
|
266
|
+
* @param {string[]} patterns
|
|
267
|
+
* @param {string} name
|
|
268
|
+
* @param {string} display_name
|
|
269
|
+
* @param {ServerlessFunctionType} type
|
|
270
|
+
* @param {string[]} [exclude]
|
|
271
|
+
* @param {string} [uuid]
|
|
259
272
|
*/
|
|
260
|
-
function generate_serverless_function(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
273
|
+
function generate_serverless_function(
|
|
274
|
+
builder,
|
|
275
|
+
routes,
|
|
276
|
+
patterns,
|
|
277
|
+
name,
|
|
278
|
+
display_name,
|
|
279
|
+
type,
|
|
280
|
+
exclude,
|
|
281
|
+
uuid
|
|
282
|
+
) {
|
|
283
|
+
builder.generateServerInstance(`.netlify/v1/server-${name}.js`, {
|
|
284
|
+
routes,
|
|
285
|
+
serverDirectory: '.netlify/v1/server'
|
|
264
286
|
});
|
|
265
287
|
|
|
266
|
-
const fn = generate_serverless_function_module(
|
|
267
|
-
const config = generate_config_export(patterns, exclude);
|
|
288
|
+
const fn = generate_serverless_function_module(name, type, uuid);
|
|
289
|
+
const config = generate_config_export(patterns, display_name, exclude);
|
|
268
290
|
|
|
269
291
|
if (builder.hasServerInstrumentationFile()) {
|
|
270
292
|
writeFileSync(`${netlify_framework_serverless_path}/${name}.mjs`, fn);
|
|
293
|
+
const initializer = builder.createInstrumentationInitializer({
|
|
294
|
+
outputDirectory: netlify_framework_serverless_path,
|
|
295
|
+
serverDirectory: '.netlify/v1/server'
|
|
296
|
+
});
|
|
271
297
|
builder.instrument({
|
|
272
298
|
entrypoint: `${netlify_framework_serverless_path}/${name}.mjs`,
|
|
273
299
|
instrumentation: '.netlify/v1/server/instrumentation.server.js',
|
|
274
300
|
start: `.netlify/v1/server/${name}.start.mjs`,
|
|
301
|
+
initializer,
|
|
275
302
|
module: {
|
|
276
303
|
generateText: generate_traced_module(config)
|
|
277
304
|
}
|
|
@@ -282,14 +309,75 @@ function generate_serverless_function({ builder, routes, patterns, name, exclude
|
|
|
282
309
|
}
|
|
283
310
|
|
|
284
311
|
/**
|
|
285
|
-
* @param {string}
|
|
312
|
+
* @param {string} name
|
|
313
|
+
* @param {ServerlessFunctionType} type
|
|
314
|
+
* @param {string} [uuid]
|
|
286
315
|
* @returns {string}
|
|
287
316
|
*/
|
|
288
|
-
function generate_serverless_function_module(
|
|
317
|
+
function generate_serverless_function_module(name, type, uuid) {
|
|
318
|
+
const original_pathname_header = `const original_pathname_header = \`x-sveltekit-original-pathname-${uuid}\``;
|
|
319
|
+
|
|
320
|
+
if (type === 'catch-all' && uuid) {
|
|
321
|
+
// Netlify encodes the response body but `fetch` automatically decodes it.
|
|
322
|
+
// So, we need to remove the `content-encoding` header to allow Netlify
|
|
323
|
+
// to correctly re-encode it on the way out.
|
|
324
|
+
return `\
|
|
325
|
+
import { applyReroute } from '@sveltejs/kit/adapter';
|
|
326
|
+
import { init } from '../serverless.js';
|
|
327
|
+
import { server } from '../server-${name}.js';
|
|
328
|
+
|
|
329
|
+
${original_pathname_header}
|
|
330
|
+
|
|
331
|
+
const respond = init(server);
|
|
332
|
+
|
|
333
|
+
export default async (request, context) => {
|
|
334
|
+
const catch_all_response = await respond(request, context);
|
|
335
|
+
|
|
336
|
+
return await applyReroute(catch_all_response, async (url) => {
|
|
337
|
+
const rerouted_request = new Request(url, request);
|
|
338
|
+
rerouted_request.headers.set(original_pathname_header, new URL(request.url).pathname);
|
|
339
|
+
|
|
340
|
+
const rerouted_response = await fetch(rerouted_request);
|
|
341
|
+
|
|
342
|
+
const response = new Response(rerouted_response.body, rerouted_response);
|
|
343
|
+
if (response.headers.has('content-encoding')) {
|
|
344
|
+
response.headers.delete('content-encoding');
|
|
345
|
+
response.headers.delete('content-length');
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return response;
|
|
349
|
+
});
|
|
350
|
+
};
|
|
351
|
+
`;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (type === 'split' && uuid) {
|
|
355
|
+
return `\
|
|
356
|
+
import { init } from '../serverless.js';
|
|
357
|
+
import { server } from '../server-${name}.js';
|
|
358
|
+
|
|
359
|
+
${original_pathname_header}
|
|
360
|
+
|
|
361
|
+
const respond = init(server);
|
|
362
|
+
|
|
363
|
+
export default async (request, context) => {
|
|
364
|
+
if (request.headers.has(original_pathname_header)) {
|
|
365
|
+
const url = new URL(request.url);
|
|
366
|
+
url.pathname = request.headers.get(original_pathname_header);
|
|
367
|
+
request = new Request(url, request);
|
|
368
|
+
request.headers.delete(original_pathname_header);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return await respond(request, context);
|
|
372
|
+
};
|
|
373
|
+
`;
|
|
374
|
+
}
|
|
375
|
+
|
|
289
376
|
return `\
|
|
290
377
|
import { init } from '../serverless.js';
|
|
378
|
+
import { server } from '../server-${name}.js';
|
|
291
379
|
|
|
292
|
-
export default init(
|
|
380
|
+
export default init(server);
|
|
293
381
|
`;
|
|
294
382
|
}
|
|
295
383
|
|
|
@@ -297,16 +385,15 @@ const generator_string = `@sveltejs/adapter-netlify@${adapter_version}`;
|
|
|
297
385
|
|
|
298
386
|
/**
|
|
299
387
|
* @param {string[]} patterns
|
|
388
|
+
* @param {string} display_name The name that shows up in the logs & metrics functions list
|
|
300
389
|
* @param {string[]} [exclude]
|
|
301
390
|
* @returns {string}
|
|
302
391
|
*/
|
|
303
|
-
function generate_config_export(patterns, exclude = []) {
|
|
304
|
-
// TODO: add a human friendly name for the function https://docs.netlify.com/build/frameworks/frameworks-api/#configuration-options-2
|
|
305
|
-
|
|
392
|
+
function generate_config_export(patterns, display_name, exclude = []) {
|
|
306
393
|
// https://docs.netlify.com/build/frameworks/frameworks-api/#configuration-options-2
|
|
307
394
|
return `\
|
|
308
395
|
export const config = {
|
|
309
|
-
name:
|
|
396
|
+
name: ${JSON.stringify(display_name)},
|
|
310
397
|
generator: '${generator_string}',
|
|
311
398
|
path: [${patterns.map(s).join(', ')}],
|
|
312
399
|
excludedPath: [${['/.netlify/*', ...exclude].map(s).join(', ')}],
|
|
@@ -317,19 +404,25 @@ export const config = {
|
|
|
317
404
|
|
|
318
405
|
/**
|
|
319
406
|
* @param {string} config
|
|
320
|
-
* @returns {(opts: { instrumentation: string; start: string }) => string}
|
|
407
|
+
* @returns {(opts: { instrumentation: string; start: string; initializer: string }) => string}
|
|
321
408
|
*/
|
|
322
409
|
function generate_traced_module(config) {
|
|
323
|
-
return ({ instrumentation, start }) => {
|
|
410
|
+
return ({ instrumentation, start, initializer }) => {
|
|
324
411
|
return `\
|
|
325
|
-
import
|
|
326
|
-
|
|
412
|
+
import ${JSON.stringify(to_import_specifier(initializer))};
|
|
413
|
+
import ${JSON.stringify(to_import_specifier(instrumentation))};
|
|
414
|
+
const { default: _0 } = await import(${JSON.stringify(to_import_specifier(start))});
|
|
327
415
|
export { _0 as default };
|
|
328
416
|
|
|
329
417
|
${config}`;
|
|
330
418
|
};
|
|
331
419
|
}
|
|
332
420
|
|
|
421
|
+
/** @param {string} path */
|
|
422
|
+
function to_import_specifier(path) {
|
|
423
|
+
return path.startsWith('.') ? path : `./${path}`;
|
|
424
|
+
}
|
|
425
|
+
|
|
333
426
|
/** @satisfies {import('rolldown').BuildOptions} */
|
|
334
427
|
const rolldown_config = {
|
|
335
428
|
platform: 'browser',
|
|
@@ -354,31 +447,21 @@ const rolldown_config = {
|
|
|
354
447
|
*/
|
|
355
448
|
async function generate_edge_functions({ builder }) {
|
|
356
449
|
const tmp = builder.getBuildDirectory('netlify-tmp');
|
|
357
|
-
|
|
358
|
-
|
|
450
|
+
rmSync(tmp, { force: true, recursive: true });
|
|
451
|
+
mkdirSync(tmp, { recursive: true });
|
|
359
452
|
|
|
360
453
|
// https://docs.netlify.com/build/frameworks/frameworks-api/#edge-functions
|
|
361
|
-
|
|
454
|
+
mkdirSync('.netlify/v1/edge-functions', { recursive: true });
|
|
362
455
|
|
|
363
456
|
builder.log.minor('Generating Edge Function...');
|
|
364
|
-
const relativePath = posix.relative(tmp, builder.getServerDirectory());
|
|
365
457
|
|
|
366
458
|
builder.copy(`${files}/edge.js`, `${tmp}/entry.js`, {
|
|
367
459
|
replace: {
|
|
368
|
-
'0SERVER':
|
|
369
|
-
MANIFEST: './manifest.js'
|
|
460
|
+
'0SERVER': `./server.js`
|
|
370
461
|
}
|
|
371
462
|
});
|
|
372
463
|
|
|
373
|
-
|
|
374
|
-
relativePath
|
|
375
|
-
});
|
|
376
|
-
|
|
377
|
-
writeFileSync(`${tmp}/manifest.js`, `export const manifest = ${manifest};\n`);
|
|
378
|
-
|
|
379
|
-
/** @type {{ assets: Set<string> }} */
|
|
380
|
-
// we have to prepend the file:// protocol because Windows doesn't support absolute path imports
|
|
381
|
-
const { assets } = (await import(`file://${tmp}/manifest.js`)).manifest;
|
|
464
|
+
builder.generateServerInstance(`${tmp}/server.js`);
|
|
382
465
|
|
|
383
466
|
const path = '/*';
|
|
384
467
|
// We only need to specify paths without the trailing slash because
|
|
@@ -387,49 +470,43 @@ async function generate_edge_functions({ builder }) {
|
|
|
387
470
|
// Contains static files
|
|
388
471
|
`/${builder.getAppPath()}/immutable/*`,
|
|
389
472
|
`/${builder.getAppPath()}/version.json`,
|
|
390
|
-
|
|
391
|
-
...
|
|
473
|
+
// the base root and `trailingSlash: 'always'` pages are recorded with a trailing slash
|
|
474
|
+
...builder.prerendered.paths.map((path) => (path === '/' ? path : path.replace(/\/$/, ''))),
|
|
475
|
+
...Array.from(builder.manifest.assets).flatMap(({ path: asset }) => {
|
|
392
476
|
if (asset.endsWith('/index.html')) {
|
|
393
477
|
const dir = asset.replace(/\/index\.html$/, '');
|
|
394
|
-
return [
|
|
395
|
-
`${builder.config.kit.paths.base}/${asset}`,
|
|
396
|
-
`${builder.config.kit.paths.base}/${dir}`
|
|
397
|
-
];
|
|
478
|
+
return [`${builder.config.paths.base}/${asset}`, `${builder.config.paths.base}/${dir}`];
|
|
398
479
|
}
|
|
399
|
-
return `${builder.config.
|
|
480
|
+
return `${builder.config.paths.base}/${asset}`;
|
|
400
481
|
}),
|
|
401
482
|
// Should not be served by SvelteKit at all
|
|
402
483
|
'/.netlify/*'
|
|
403
484
|
];
|
|
404
485
|
|
|
405
|
-
await Promise.all([
|
|
406
|
-
build({
|
|
407
|
-
...rolldown_config,
|
|
408
|
-
input: `${tmp}/entry.js`,
|
|
409
|
-
output: {
|
|
410
|
-
...rolldown_config.output,
|
|
411
|
-
file: `${netlify_framework_edge_path}/${FUNCTION_PREFIX}render.js`
|
|
412
|
-
}
|
|
413
|
-
}),
|
|
414
|
-
builder.hasServerInstrumentationFile() &&
|
|
415
|
-
build({
|
|
416
|
-
...rolldown_config,
|
|
417
|
-
input: `${builder.getServerDirectory()}/instrumentation.server.js`,
|
|
418
|
-
output: {
|
|
419
|
-
...rolldown_config.output,
|
|
420
|
-
file: `${netlify_framework_edge_path}/${FUNCTION_PREFIX}instrumentation.server.js`
|
|
421
|
-
}
|
|
422
|
-
})
|
|
423
|
-
]);
|
|
424
|
-
|
|
425
486
|
if (builder.hasServerInstrumentationFile()) {
|
|
487
|
+
const initializer = builder.createInstrumentationInitializer({
|
|
488
|
+
outputDirectory: tmp,
|
|
489
|
+
environment: 'export default Deno.env.toObject();\n'
|
|
490
|
+
});
|
|
491
|
+
writeFileSync(`${tmp}/instrumented-entry.js`, `export { default } from './entry.js';\n`);
|
|
426
492
|
builder.instrument({
|
|
427
|
-
entrypoint: `${
|
|
428
|
-
instrumentation: `${
|
|
429
|
-
|
|
493
|
+
entrypoint: `${tmp}/instrumented-entry.js`,
|
|
494
|
+
instrumentation: `${builder.getServerDirectory()}/instrumentation.server.js`,
|
|
495
|
+
initializer
|
|
430
496
|
});
|
|
431
497
|
}
|
|
432
498
|
|
|
499
|
+
await build({
|
|
500
|
+
...rolldown_config,
|
|
501
|
+
input: builder.hasServerInstrumentationFile()
|
|
502
|
+
? `${tmp}/instrumented-entry.js`
|
|
503
|
+
: `${tmp}/entry.js`,
|
|
504
|
+
output: {
|
|
505
|
+
...rolldown_config.output,
|
|
506
|
+
file: `${netlify_framework_edge_path}/${FUNCTION_PREFIX}render.js`
|
|
507
|
+
}
|
|
508
|
+
});
|
|
509
|
+
|
|
433
510
|
add_edge_function_config({ builder, path, excluded_paths });
|
|
434
511
|
}
|
|
435
512
|
|
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.11",
|
|
4
4
|
"description": "A SvelteKit adapter that creates a Netlify app",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"adapter",
|
|
@@ -27,37 +27,34 @@
|
|
|
27
27
|
},
|
|
28
28
|
"types": "index.d.ts",
|
|
29
29
|
"files": [
|
|
30
|
-
"files",
|
|
30
|
+
"files/*.js",
|
|
31
31
|
"index.js",
|
|
32
|
+
"utils.js",
|
|
32
33
|
"index.d.ts",
|
|
33
34
|
"ambient.d.ts"
|
|
34
35
|
],
|
|
35
36
|
"dependencies": {
|
|
36
|
-
"@
|
|
37
|
-
"rolldown": "^1.
|
|
37
|
+
"@netlify/types": "^2.8.0",
|
|
38
|
+
"rolldown": "^1.2.3",
|
|
39
|
+
"smol-toml": "^1.8.0"
|
|
38
40
|
},
|
|
39
41
|
"devDependencies": {
|
|
40
|
-
"@netlify/
|
|
41
|
-
"@
|
|
42
|
-
"@
|
|
43
|
-
"@netlify/node-cookies": "^0.1.0",
|
|
44
|
-
"@netlify/types": "^2.3.0",
|
|
42
|
+
"@netlify/edge-functions": "^3.0.8",
|
|
43
|
+
"@sveltejs/kit": "^3.0.0-next.27",
|
|
44
|
+
"@types/deno": "^2.7.0",
|
|
45
45
|
"@types/node": "^22.19.19",
|
|
46
|
-
"typescript": "^
|
|
47
|
-
"vitest": "^4.1.
|
|
48
|
-
"@sveltejs/kit": "^3.0.0-next.6"
|
|
46
|
+
"typescript": "^7.0.2",
|
|
47
|
+
"vitest": "^4.1.10"
|
|
49
48
|
},
|
|
50
49
|
"peerDependencies": {
|
|
51
|
-
"@sveltejs/kit": "^
|
|
50
|
+
"@sveltejs/kit": "^3.0.0-next.0"
|
|
52
51
|
},
|
|
53
52
|
"scripts": {
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"lint": "prettier --check .",
|
|
58
|
-
"format": "pnpm lint --write",
|
|
59
|
-
"test": "pnpm test:unit && pnpm test:integration",
|
|
53
|
+
"check": "tsc && tsc --project files/tsconfig.json",
|
|
54
|
+
"lint": "oxfmt --check .",
|
|
55
|
+
"format": "oxfmt --write .",
|
|
60
56
|
"test:unit": "vitest run",
|
|
61
|
-
"test:
|
|
57
|
+
"test:build": "pnpm -r --workspace-concurrency 1 --filter=\"./test/**\" test:build",
|
|
58
|
+
"test": "pnpm test:unit && pnpm test:build"
|
|
62
59
|
}
|
|
63
60
|
}
|
package/utils.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
import process from 'node:process';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {{ rest: boolean, dynamic: boolean, content: string }} RouteSegment
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {{
|
|
10
|
+
* build?: { publish?: string }
|
|
11
|
+
* functions?: { node_bundler?: 'zisi' | 'esbuild' }
|
|
12
|
+
* }} NetlifyConfig
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {RouteSegment[]} a
|
|
17
|
+
* @param {RouteSegment[]} b
|
|
18
|
+
* @returns {boolean}
|
|
19
|
+
*/
|
|
20
|
+
export function matches(a, b) {
|
|
21
|
+
if (a[0] && b[0]) {
|
|
22
|
+
if (b[0].rest) {
|
|
23
|
+
if (b.length === 1) return true;
|
|
24
|
+
|
|
25
|
+
const next_b = b.slice(1);
|
|
26
|
+
|
|
27
|
+
for (let i = 0; i < a.length; i += 1) {
|
|
28
|
+
if (matches(a.slice(i), next_b)) return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!b[0].dynamic) {
|
|
35
|
+
if (!a[0].dynamic && a[0].content !== b[0].content) return false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (a.length === 1 && b.length === 1) return true;
|
|
39
|
+
return matches(a.slice(1), b.slice(1));
|
|
40
|
+
} else if (a[0]) {
|
|
41
|
+
return a.length === 1 && a[0].rest;
|
|
42
|
+
} else {
|
|
43
|
+
return b.length === 1 && b[0].rest;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
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
|
+
/**
|
|
73
|
+
* @param {*} value
|
|
74
|
+
* @returns {string}
|
|
75
|
+
*/
|
|
76
|
+
export function s(value) {
|
|
77
|
+
return JSON.stringify(value, null, '\t');
|
|
78
|
+
}
|