@openworkers/adapter-sveltekit 0.3.0 → 0.3.1

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 (2) hide show
  1. package/index.js +4 -43
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -90,6 +90,7 @@ export default function (options = {}) {
90
90
  builder.mkdirp(`${dest}/functions`);
91
91
 
92
92
  const endpointsDir = path.join(builder.getServerDirectory(), 'entries/endpoints');
93
+ const functionTemplate = posixify(path.resolve(files, 'function-worker.js'));
93
94
 
94
95
  if (existsSync(endpointsDir)) {
95
96
  const endpoints = findEndpoints(endpointsDir);
@@ -99,55 +100,15 @@ export default function (options = {}) {
99
100
  const workerName = routePattern.replace(/\//g, '-').replace(/^-/, '') || 'index';
100
101
  const workerFile = `functions/${workerName}.js`;
101
102
 
102
- // Create entry point for this function
103
- const functionEntry = `${tmp}/function-${workerName}.js`;
104
- const endpointPath = posixify(endpoint.file);
105
-
106
- writeFileSync(
107
- functionEntry,
108
- `import * as handlers from '${endpointPath}';\n` +
109
- `export default {\n` +
110
- ` async fetch(req, env, ctx) {\n` +
111
- ` globalThis.env = env;\n` +
112
- ` const method = req.method;\n` +
113
- ` const handler = handlers[method];\n` +
114
- ` if (!handler) {\n` +
115
- ` return new Response('Method Not Allowed', {\n` +
116
- ` status: 405,\n` +
117
- ` headers: { Allow: Object.keys(handlers).join(', ') }\n` +
118
- ` });\n` +
119
- ` }\n` +
120
- ` const url = new URL(req.url);\n` +
121
- ` const event = {\n` +
122
- ` request: req,\n` +
123
- ` url,\n` +
124
- ` params: ctx.params ?? {},\n` +
125
- ` platform: { env, ctx },\n` +
126
- ` getClientAddress() {\n` +
127
- ` return req.headers.get('x-real-ip') ?? req.headers.get('x-forwarded-for') ?? '';\n` +
128
- ` }\n` +
129
- ` };\n` +
130
- ` try {\n` +
131
- ` return await handler(event);\n` +
132
- ` } catch (error) {\n` +
133
- ` console.error('[Function] Error:', error);\n` +
134
- ` return new Response(JSON.stringify({ error: 'Internal Server Error' }), {\n` +
135
- ` status: 500,\n` +
136
- ` headers: { 'Content-Type': 'application/json' }\n` +
137
- ` });\n` +
138
- ` }\n` +
139
- ` }\n` +
140
- `};\n`
141
- );
142
-
143
- // Bundle the function
103
+ // Bundle using the template with ENDPOINT alias
144
104
  await build({
145
- entryPoints: [functionEntry],
105
+ entryPoints: [functionTemplate],
146
106
  bundle: true,
147
107
  format: 'esm',
148
108
  platform: 'browser',
149
109
  outfile: `${dest}/${workerFile}`,
150
110
  alias: {
111
+ ENDPOINT: posixify(endpoint.file),
151
112
  'node:async_hooks': shimAsyncHooks
152
113
  },
153
114
  external: ['node:*'],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openworkers/adapter-sveltekit",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "SvelteKit adapter for OpenWorkers",
5
5
  "keywords": [
6
6
  "adapter",