@openworkers/adapter-sveltekit 0.3.0 → 0.3.2
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/worker.js +4 -0
- package/index.js +10 -47
- package/package.json +1 -1
package/files/worker.js
CHANGED
|
@@ -55,6 +55,10 @@ var worker_default = {
|
|
|
55
55
|
is_static_asset = manifest.assets.has(filename) || manifest.assets.has(filename + "/index.html") || filename in manifest._.server_assets || filename + "/index.html" in manifest._.server_assets;
|
|
56
56
|
}
|
|
57
57
|
if (is_static_asset || prerendered.has(pathname) || pathname === version_file || pathname.startsWith(immutable)) {
|
|
58
|
+
if (prerendered.has(pathname) && !pathname.includes(".")) {
|
|
59
|
+
const htmlPath = pathname === "/" ? "/index.html" : `${pathname}.html`;
|
|
60
|
+
return env.ASSETS.fetch(htmlPath);
|
|
61
|
+
}
|
|
58
62
|
return env.ASSETS.fetch(req);
|
|
59
63
|
}
|
|
60
64
|
let location = pathname.at(-1) === "/" ? stripped_pathname : pathname + "/";
|
package/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { existsSync, writeFileSync, readdirSync, statSync } from 'node:fs';
|
|
1
|
+
import { existsSync, writeFileSync, readdirSync, statSync, readFileSync } from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import { build } from 'esbuild';
|
|
5
5
|
|
|
6
|
+
const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf-8'));
|
|
6
7
|
const name = '@openworkers/adapter-sveltekit';
|
|
8
|
+
const version = pkg.version;
|
|
7
9
|
|
|
8
10
|
/** @type {import('./index.js').default} */
|
|
9
11
|
export default function (options = {}) {
|
|
@@ -78,7 +80,7 @@ export default function (options = {}) {
|
|
|
78
80
|
external: ['node:*'],
|
|
79
81
|
minify: false,
|
|
80
82
|
banner: {
|
|
81
|
-
js: `// Generated by ${name}\n`
|
|
83
|
+
js: `// Generated by ${name} v${version}\n`
|
|
82
84
|
}
|
|
83
85
|
});
|
|
84
86
|
|
|
@@ -90,6 +92,7 @@ export default function (options = {}) {
|
|
|
90
92
|
builder.mkdirp(`${dest}/functions`);
|
|
91
93
|
|
|
92
94
|
const endpointsDir = path.join(builder.getServerDirectory(), 'entries/endpoints');
|
|
95
|
+
const functionTemplate = posixify(path.resolve(files, 'function-worker.js'));
|
|
93
96
|
|
|
94
97
|
if (existsSync(endpointsDir)) {
|
|
95
98
|
const endpoints = findEndpoints(endpointsDir);
|
|
@@ -99,61 +102,21 @@ export default function (options = {}) {
|
|
|
99
102
|
const workerName = routePattern.replace(/\//g, '-').replace(/^-/, '') || 'index';
|
|
100
103
|
const workerFile = `functions/${workerName}.js`;
|
|
101
104
|
|
|
102
|
-
//
|
|
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
|
|
105
|
+
// Bundle using the template with ENDPOINT alias
|
|
144
106
|
await build({
|
|
145
|
-
entryPoints: [
|
|
107
|
+
entryPoints: [functionTemplate],
|
|
146
108
|
bundle: true,
|
|
147
109
|
format: 'esm',
|
|
148
110
|
platform: 'browser',
|
|
149
111
|
outfile: `${dest}/${workerFile}`,
|
|
150
112
|
alias: {
|
|
113
|
+
ENDPOINT: posixify(endpoint.file),
|
|
151
114
|
'node:async_hooks': shimAsyncHooks
|
|
152
115
|
},
|
|
153
116
|
external: ['node:*'],
|
|
154
117
|
minify: false,
|
|
155
118
|
banner: {
|
|
156
|
-
js: `// Generated by ${name} - Function: ${routePattern}\n`
|
|
119
|
+
js: `// Generated by ${name} v${version} - Function: ${routePattern}\n`
|
|
157
120
|
}
|
|
158
121
|
});
|
|
159
122
|
|
|
@@ -183,7 +146,7 @@ export default function (options = {}) {
|
|
|
183
146
|
|
|
184
147
|
writeFileSync(
|
|
185
148
|
`${dest}/routes.js`,
|
|
186
|
-
`// Generated by ${name}\n` +
|
|
149
|
+
`// Generated by ${name} v${version}\n` +
|
|
187
150
|
`// Used by OpenWorkers edge router to bypass worker for static content\n\n` +
|
|
188
151
|
`export default ${JSON.stringify(routes, null, '\t')};\n`
|
|
189
152
|
);
|