@openworkers/adapter-sveltekit 0.2.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.
- package/files/worker.js +79 -0
- package/index.d.ts +11 -0
- package/index.js +110 -0
- package/package.json +52 -0
package/files/worker.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// src/worker.js
|
|
2
|
+
import { Server, manifest, prerendered, base_path } from "SERVER";
|
|
3
|
+
var server = new Server(manifest);
|
|
4
|
+
var app_path = `/${manifest.appPath}`;
|
|
5
|
+
var immutable = `${app_path}/immutable/`;
|
|
6
|
+
var version_file = `${app_path}/version.json`;
|
|
7
|
+
if (typeof caches === "undefined") {
|
|
8
|
+
const noopCache = (
|
|
9
|
+
/** @type {any} */
|
|
10
|
+
{
|
|
11
|
+
match: async () => void 0,
|
|
12
|
+
put: async () => {
|
|
13
|
+
},
|
|
14
|
+
delete: async () => false
|
|
15
|
+
}
|
|
16
|
+
);
|
|
17
|
+
globalThis.caches = /** @type {any} */
|
|
18
|
+
{
|
|
19
|
+
default: noopCache,
|
|
20
|
+
open: async () => noopCache
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
var origin;
|
|
24
|
+
var initialized = server.init({
|
|
25
|
+
env: (
|
|
26
|
+
/** @type {Record<string, string>} */
|
|
27
|
+
globalThis.env ?? {}
|
|
28
|
+
),
|
|
29
|
+
read: async (file) => {
|
|
30
|
+
const url = `${origin}/${file}`;
|
|
31
|
+
const response = await /** @type {Env} */
|
|
32
|
+
globalThis.env.ASSETS.fetch(url);
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
throw new Error(`read(...) failed: could not fetch ${url} (${response.status} ${response.statusText})`);
|
|
35
|
+
}
|
|
36
|
+
return response.body;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
var worker_default = {
|
|
40
|
+
async fetch(req, env, ctx) {
|
|
41
|
+
globalThis.env = env;
|
|
42
|
+
if (!origin) {
|
|
43
|
+
origin = new URL(req.url).origin;
|
|
44
|
+
}
|
|
45
|
+
await initialized;
|
|
46
|
+
let { pathname } = new URL(req.url);
|
|
47
|
+
try {
|
|
48
|
+
pathname = decodeURIComponent(pathname);
|
|
49
|
+
} catch {
|
|
50
|
+
}
|
|
51
|
+
const stripped_pathname = pathname.replace(/\/$/, "");
|
|
52
|
+
let is_static_asset = false;
|
|
53
|
+
const filename = stripped_pathname.slice(base_path.length + 1);
|
|
54
|
+
if (filename) {
|
|
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
|
+
}
|
|
57
|
+
if (is_static_asset || prerendered.has(pathname) || pathname === version_file || pathname.startsWith(immutable)) {
|
|
58
|
+
return env.ASSETS.fetch(req);
|
|
59
|
+
}
|
|
60
|
+
let location = pathname.at(-1) === "/" ? stripped_pathname : pathname + "/";
|
|
61
|
+
if (location && prerendered.has(location)) {
|
|
62
|
+
const search = new URL(req.url).search;
|
|
63
|
+
if (search) location += search;
|
|
64
|
+
return new Response("", {
|
|
65
|
+
status: 308,
|
|
66
|
+
headers: { location }
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return server.respond(req, {
|
|
70
|
+
platform: { env, ctx },
|
|
71
|
+
getClientAddress() {
|
|
72
|
+
return req.headers.get("x-real-ip") ?? req.headers.get("x-forwarded-for") ?? "";
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
export {
|
|
78
|
+
worker_default as default
|
|
79
|
+
};
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { existsSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { build } from 'esbuild';
|
|
5
|
+
|
|
6
|
+
const name = '@openworkers/adapter-sveltekit';
|
|
7
|
+
|
|
8
|
+
/** @type {import('./index.js').default} */
|
|
9
|
+
export default function (options = {}) {
|
|
10
|
+
return {
|
|
11
|
+
name,
|
|
12
|
+
/** @param {import('@sveltejs/kit').Builder} builder */
|
|
13
|
+
async adapt(builder) {
|
|
14
|
+
const dest = options.out ?? 'dist';
|
|
15
|
+
const assetsDir = `${dest}/assets`;
|
|
16
|
+
|
|
17
|
+
const files = fileURLToPath(new URL('./files', import.meta.url).href);
|
|
18
|
+
const tmp = builder.getBuildDirectory('openworkers-tmp');
|
|
19
|
+
|
|
20
|
+
builder.rimraf(dest);
|
|
21
|
+
builder.mkdirp(dest);
|
|
22
|
+
builder.mkdirp(assetsDir);
|
|
23
|
+
builder.mkdirp(tmp);
|
|
24
|
+
|
|
25
|
+
// Write client assets (returns list of files)
|
|
26
|
+
const clientFiles = builder.writeClient(assetsDir);
|
|
27
|
+
|
|
28
|
+
// Write prerendered pages
|
|
29
|
+
builder.writePrerendered(assetsDir);
|
|
30
|
+
|
|
31
|
+
// Separate static files from app files
|
|
32
|
+
const appPath = builder.getAppPath();
|
|
33
|
+
const staticFiles = clientFiles.filter((f) => !f.startsWith(appPath));
|
|
34
|
+
|
|
35
|
+
// Generate 404.html fallback
|
|
36
|
+
const fallback = path.join(assetsDir, '404.html');
|
|
37
|
+
if (!existsSync(fallback)) {
|
|
38
|
+
writeFileSync(fallback, 'Not Found');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Generate manifest
|
|
42
|
+
writeFileSync(
|
|
43
|
+
`${tmp}/manifest.js`,
|
|
44
|
+
`export const manifest = ${builder.generateManifest({ relativePath: path.posix.relative(tmp, builder.getServerDirectory()) })};\n\n` +
|
|
45
|
+
`export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});\n\n` +
|
|
46
|
+
`export const base_path = ${JSON.stringify(builder.config.kit.paths.base)};\n`
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// Create entry point that imports server and manifest
|
|
50
|
+
const entryPoint = `${tmp}/entry.js`;
|
|
51
|
+
const serverPath = posixify(path.resolve(builder.getServerDirectory(), 'index.js'));
|
|
52
|
+
const manifestPath = posixify(path.resolve(tmp, 'manifest.js'));
|
|
53
|
+
|
|
54
|
+
writeFileSync(
|
|
55
|
+
entryPoint,
|
|
56
|
+
`import { Server } from '${serverPath}';\n` +
|
|
57
|
+
`import { manifest, prerendered, base_path } from '${manifestPath}';\n` +
|
|
58
|
+
`export { Server, manifest, prerendered, base_path };\n`
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
// Bundle worker with esbuild
|
|
62
|
+
const workerDest = `${dest}/worker.js`;
|
|
63
|
+
|
|
64
|
+
await build({
|
|
65
|
+
entryPoints: [`${files}/worker.js`],
|
|
66
|
+
bundle: true,
|
|
67
|
+
format: 'esm',
|
|
68
|
+
platform: 'browser',
|
|
69
|
+
outfile: workerDest,
|
|
70
|
+
alias: {
|
|
71
|
+
SERVER: entryPoint,
|
|
72
|
+
MANIFEST: entryPoint
|
|
73
|
+
},
|
|
74
|
+
external: ['node:*'],
|
|
75
|
+
minify: false,
|
|
76
|
+
banner: {
|
|
77
|
+
js: `// Generated by ${name}\n`
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Generate routes.js for edge routing
|
|
82
|
+
const routes = {
|
|
83
|
+
// Immutable assets (hashed filenames, cache forever)
|
|
84
|
+
immutable: [`/${appPath}/immutable/*`],
|
|
85
|
+
// Static files from /static folder (robots.txt, favicon, etc.)
|
|
86
|
+
static: staticFiles.map((f) => `/${f}`),
|
|
87
|
+
// Prerendered pages (serve from assets, no worker needed)
|
|
88
|
+
prerendered: builder.prerendered.paths,
|
|
89
|
+
// Everything else -> SSR
|
|
90
|
+
ssr: ['/*']
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
writeFileSync(
|
|
94
|
+
`${dest}/routes.js`,
|
|
95
|
+
`// Generated by ${name}\n` +
|
|
96
|
+
`// Used by OpenWorkers edge router to bypass worker for static content\n\n` +
|
|
97
|
+
`export default ${JSON.stringify(routes, null, '\t')};\n`
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
builder.log.minor(`Wrote ${workerDest}`);
|
|
101
|
+
builder.log.minor(`Wrote ${dest}/routes.js`);
|
|
102
|
+
builder.log.minor(`Wrote ${assetsDir} (${builder.prerendered.paths.length} prerendered pages)`);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** @param {string} str */
|
|
108
|
+
function posixify(str) {
|
|
109
|
+
return str.replace(/\\/g, '/');
|
|
110
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openworkers/adapter-sveltekit",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "SvelteKit adapter for OpenWorkers",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"adapter",
|
|
7
|
+
"openworkers",
|
|
8
|
+
"edge",
|
|
9
|
+
"workers",
|
|
10
|
+
"svelte",
|
|
11
|
+
"sveltekit"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/openworkers/adapter-sveltekit.git"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"type": "module",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"types": "./index.d.ts",
|
|
22
|
+
"import": "./index.js"
|
|
23
|
+
},
|
|
24
|
+
"./package.json": "./package.json"
|
|
25
|
+
},
|
|
26
|
+
"types": "index.d.ts",
|
|
27
|
+
"files": [
|
|
28
|
+
"files",
|
|
29
|
+
"index.js",
|
|
30
|
+
"index.d.ts"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "esbuild src/worker.js --bundle --outfile=files/worker.js --external:SERVER --external:MANIFEST --format=esm",
|
|
34
|
+
"prepublishOnly": "npm run build",
|
|
35
|
+
"format": "prettier --write ."
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"esbuild": "^0.24.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@openworkers/workers-types": "^0.1.7",
|
|
42
|
+
"@sveltejs/kit": "^2.0.0",
|
|
43
|
+
"prettier": "^3.8.1"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"@sveltejs/kit": "^2.0.0"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public",
|
|
50
|
+
"registry": "https://registry.npmjs.org/"
|
|
51
|
+
}
|
|
52
|
+
}
|