@openworkers/adapter-sveltekit 0.3.4 → 0.3.5

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.
Files changed (3) hide show
  1. package/README.md +5 -5
  2. package/index.js +71 -26
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -35,10 +35,10 @@ export default {
35
35
 
36
36
  ```
37
37
  dist/
38
- ├── worker.js # Main SSR worker
39
- ├── routes.js # Route manifest for edge routing
40
- ├── assets/ # Static assets and prerendered pages
41
- └── functions/ # Mini-workers for API routes (if functions: true)
38
+ ├── _worker.js # Main SSR worker
39
+ ├── _routes.json # Route manifest for edge routing
40
+ ├── assets/ # Static assets and prerendered pages
41
+ └── functions/ # Mini-workers for API routes (if functions: true)
42
42
  ├── api-hello.js
43
43
  └── api-users.js
44
44
  ```
@@ -50,7 +50,7 @@ When `functions: true`, the adapter generates a separate mini-worker for each `+
50
50
  - `/api/hello/+server.ts` → `functions/api-hello.js`
51
51
  - `/api/users/+server.ts` → `functions/api-users.js`
52
52
 
53
- The route mappings are included in `routes.js`:
53
+ The route mappings are included in `_routes.json`:
54
54
 
55
55
  ```js
56
56
  {
package/index.js CHANGED
@@ -91,15 +91,18 @@ export default function (options = {}) {
91
91
  if (functionsEnabled) {
92
92
  builder.mkdirp(`${dest}/functions`);
93
93
 
94
- const endpointsDir = path.join(builder.getServerDirectory(), 'entries/endpoints');
94
+ const serverDir = builder.getServerDirectory();
95
+ const viteManifestPath = path.join(serverDir, '.vite/manifest.json');
95
96
  const functionTemplate = posixify(path.resolve(files, 'function-worker.js'));
96
97
 
97
- if (existsSync(endpointsDir)) {
98
- const endpoints = findEndpoints(endpointsDir);
98
+ if (existsSync(viteManifestPath)) {
99
+ const viteManifest = JSON.parse(readFileSync(viteManifestPath, 'utf-8'));
100
+ const endpoints = extractEndpointsFromManifest(viteManifest, serverDir);
99
101
 
100
102
  for (const endpoint of endpoints) {
101
- const routePattern = endpoint.route;
102
- const workerName = routePattern.replace(/\//g, '-').replace(/^-/, '') || 'index';
103
+ const routePattern = endpoint.pattern;
104
+ // Use SvelteKit route syntax for worker filename
105
+ const workerName = endpoint.route.replace(/\//g, '-').replace(/^-/, '') || 'index';
103
106
  const workerFile = `functions/${workerName}.js`;
104
107
 
105
108
  // Bundle using the template with ENDPOINT alias
@@ -162,30 +165,72 @@ export default function (options = {}) {
162
165
  }
163
166
 
164
167
  /**
165
- * Find all endpoint files in the server output
166
- * @param {string} dir
167
- * @param {string} [base='']
168
- * @returns {Array<{route: string, file: string}>}
168
+ * Extract endpoints from Vite manifest and convert SvelteKit routes to simple patterns
169
+ * @param {object} manifest - Vite manifest object
170
+ * @param {string} serverDir - Server build directory
171
+ * @returns {Array<{pattern: string, route: string, file: string}>}
169
172
  */
170
- function findEndpoints(dir, base = '') {
171
- const results = [];
172
- const entries = readdirSync(dir);
173
-
174
- for (const entry of entries) {
175
- const fullPath = path.join(dir, entry);
176
- const stat = statSync(fullPath);
177
-
178
- if (stat.isDirectory()) {
179
- results.push(...findEndpoints(fullPath, `${base}/${entry}`));
180
- } else if (entry === '_server.ts.js' || entry === '_server.js.js') {
181
- results.push({
182
- route: base || '/',
183
- file: fullPath
184
- });
185
- }
173
+ function extractEndpointsFromManifest(manifest, serverDir) {
174
+ const endpoints = [];
175
+
176
+ for (const [key, entry] of Object.entries(manifest)) {
177
+ // Only process endpoint files (+server.ts)
178
+ if (!key.includes('+server.ts')) continue;
179
+
180
+ const sourcePath = entry.src;
181
+ if (!sourcePath || !sourcePath.startsWith('src/routes/')) continue;
182
+
183
+ // Extract route from source path: src/routes/status/[code]/[[reason]]/+server.ts
184
+ const routePart = sourcePath
185
+ .replace(/^src\/routes/, '')
186
+ .replace(/\/\+server\.(ts|js)$/, '');
187
+
188
+ // Convert SvelteKit route syntax to simple wildcard patterns
189
+ const pattern = convertRouteToPattern(routePart);
190
+
191
+ // Get the built file path
192
+ const file = path.join(serverDir, entry.file);
193
+
194
+ endpoints.push({
195
+ pattern, // For routing: /status/*/*
196
+ route: routePart, // For filename: /status/[code]/[[reason]]
197
+ file
198
+ });
186
199
  }
187
200
 
188
- return results;
201
+ return endpoints;
202
+ }
203
+
204
+ /**
205
+ * Convert SvelteKit route syntax to simple wildcard patterns
206
+ * Examples:
207
+ * /status/[code]/[[reason]] becomes /status/star/star
208
+ * /range/[n] becomes /range/star
209
+ * /drip/[...params] becomes /drip/doublestar
210
+ * @param {string} route - SvelteKit route (e.g. "/status/[code]/[[reason]]")
211
+ * @returns {string} - Simple pattern with wildcards
212
+ */
213
+ function convertRouteToPattern(route) {
214
+ if (!route || route === '/') return '/';
215
+
216
+ const segments = route.split('/').filter(Boolean);
217
+ const patternSegments = segments.map(segment => {
218
+ // Rest parameter: [...params] -> **
219
+ if (segment.startsWith('[...') && segment.endsWith(']')) {
220
+ return '**';
221
+ }
222
+
223
+ // Required or optional parameter: [param] or [[param]] -> *
224
+ if ((segment.startsWith('[') && segment.endsWith(']')) ||
225
+ (segment.startsWith('[[') && segment.endsWith(']]'))) {
226
+ return '*';
227
+ }
228
+
229
+ // Static segment: keep as-is
230
+ return segment;
231
+ });
232
+
233
+ return '/' + patternSegments.join('/');
189
234
  }
190
235
 
191
236
  /** @param {string} str */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openworkers/adapter-sveltekit",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "SvelteKit adapter for OpenWorkers",
5
5
  "keywords": [
6
6
  "adapter",