@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.
@@ -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
- async fetch(req, env, ctx) {
10
- globalThis.env = env;
11
-
12
- const method = req.method;
13
- const handler = handlers[method];
14
-
15
- if (!handler) {
16
- return new Response('Method Not Allowed', {
17
- status: 405,
18
- headers: { Allow: Object.keys(handlers).join(', ') }
19
- });
20
- }
21
-
22
- const url = new URL(req.url);
23
-
24
- // Build a minimal RequestEvent-like object
25
- const event = {
26
- request: req,
27
- url,
28
- params: ctx.params ?? {},
29
- platform: { env, ctx },
30
- getClientAddress() {
31
- return req.headers.get('x-real-ip') ?? req.headers.get('x-forwarded-for') ?? '';
32
- }
33
- };
34
-
35
- try {
36
- return await handler(event);
37
- } catch (error) {
38
- console.error(`[Function] Error in ${method} handler:`, error);
39
-
40
- return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
41
- status: 500,
42
- headers: { 'Content-Type': 'application/json' }
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
- #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
- }
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
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openworkers/adapter-sveltekit",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "SvelteKit adapter for OpenWorkers",
5
5
  "keywords": [
6
6
  "adapter",