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