@openworkers/adapter-sveltekit 0.3.5 → 0.3.6
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/function-worker.js +81 -37
- package/files/shims/async_hooks.js +32 -32
- package/index.js +3 -0
- package/package.json +1 -1
package/files/function-worker.js
CHANGED
|
@@ -5,42 +5,86 @@
|
|
|
5
5
|
|
|
6
6
|
import * as handlers from 'ENDPOINT';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Extract route params from URL pathname based on SvelteKit route pattern
|
|
10
|
+
* @param {string} pathname - URL pathname (e.g., "/stream/42")
|
|
11
|
+
* @param {string} pattern - SvelteKit route pattern (e.g., "/stream/[n]")
|
|
12
|
+
* @returns {Record<string, string>} - Extracted params (e.g., {n: "42"})
|
|
13
|
+
*/
|
|
14
|
+
function extractParams(pathname, pattern) {
|
|
15
|
+
const params = {};
|
|
16
|
+
const patternSegments = pattern.split('/').filter(Boolean);
|
|
17
|
+
const pathSegments = pathname.split('/').filter(Boolean);
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < patternSegments.length; i++) {
|
|
20
|
+
const patternSegment = patternSegments[i];
|
|
21
|
+
|
|
22
|
+
// Rest parameter: [...rest]
|
|
23
|
+
if (patternSegment.startsWith('[...') && patternSegment.endsWith(']')) {
|
|
24
|
+
const paramName = patternSegment.slice(4, -1);
|
|
25
|
+
params[paramName] = pathSegments.slice(i).join('/');
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Optional parameter: [[optional]]
|
|
30
|
+
if (patternSegment.startsWith('[[') && patternSegment.endsWith(']]')) {
|
|
31
|
+
const paramName = patternSegment.slice(2, -2);
|
|
32
|
+
if (i < pathSegments.length) {
|
|
33
|
+
params[paramName] = pathSegments[i];
|
|
34
|
+
}
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Regular parameter: [param]
|
|
39
|
+
if (patternSegment.startsWith('[') && patternSegment.endsWith(']')) {
|
|
40
|
+
const paramName = patternSegment.slice(1, -1);
|
|
41
|
+
params[paramName] = pathSegments[i];
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return params;
|
|
47
|
+
}
|
|
48
|
+
|
|
8
49
|
export default {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
50
|
+
async fetch(req, env, ctx) {
|
|
51
|
+
globalThis.env = env;
|
|
52
|
+
|
|
53
|
+
const method = req.method;
|
|
54
|
+
const handler = handlers[method];
|
|
55
|
+
|
|
56
|
+
if (!handler) {
|
|
57
|
+
return new Response('Method Not Allowed', {
|
|
58
|
+
status: 405,
|
|
59
|
+
headers: { Allow: Object.keys(handlers).join(', ') }
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const url = new URL(req.url);
|
|
64
|
+
|
|
65
|
+
// Extract params from URL based on route pattern
|
|
66
|
+
const params = extractParams(url.pathname, ROUTE_PATTERN);
|
|
67
|
+
|
|
68
|
+
// Build a minimal RequestEvent-like object
|
|
69
|
+
const event = {
|
|
70
|
+
request: req,
|
|
71
|
+
url,
|
|
72
|
+
params,
|
|
73
|
+
platform: { env, ctx },
|
|
74
|
+
getClientAddress() {
|
|
75
|
+
return req.headers.get('x-real-ip') ?? req.headers.get('x-forwarded-for') ?? '';
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
return await handler(event);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.error(`[Function] Error in ${method} handler:`, error);
|
|
83
|
+
|
|
84
|
+
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
|
|
85
|
+
status: 500,
|
|
86
|
+
headers: { 'Content-Type': 'application/json' }
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
46
90
|
};
|
|
@@ -6,36 +6,36 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
export class AsyncLocalStorage {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
9
|
+
#store;
|
|
10
|
+
|
|
11
|
+
run(store, fn, ...args) {
|
|
12
|
+
this.#store = store;
|
|
13
|
+
return fn(...args);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
getStore() {
|
|
17
|
+
return this.#store;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Stubs for API completeness
|
|
21
|
+
enterWith(store) {
|
|
22
|
+
this.#store = store;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
exit(fn, ...args) {
|
|
26
|
+
this.#store = undefined;
|
|
27
|
+
return fn(...args);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
disable() {
|
|
31
|
+
this.#store = undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static bind(fn) {
|
|
35
|
+
return fn;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static snapshot() {
|
|
39
|
+
return (fn, ...args) => fn(...args);
|
|
40
|
+
}
|
|
41
41
|
}
|
package/index.js
CHANGED
|
@@ -118,6 +118,9 @@ export default function (options = {}) {
|
|
|
118
118
|
},
|
|
119
119
|
external: ['node:*'],
|
|
120
120
|
minify: false,
|
|
121
|
+
define: {
|
|
122
|
+
ROUTE_PATTERN: JSON.stringify(endpoint.route)
|
|
123
|
+
},
|
|
121
124
|
banner: {
|
|
122
125
|
js: `// Generated by ${name} v${version} - Function: ${routePattern}\n`
|
|
123
126
|
}
|