@orochibraru/svelte-smol 1.7.0-next.2 → 1.7.0

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/README.md CHANGED
@@ -10,25 +10,17 @@ no JS files to ship, just one binary plus its static assets.
10
10
  bun add -d @orochibraru/svelte-smol
11
11
  ```
12
12
 
13
- For SvelteKit 3 (prerelease), install from the `next` tag instead:
14
-
15
- ```bash
16
- bun add -d @orochibraru/svelte-smol@next @sveltejs/kit@next
17
- ```
18
-
19
13
  ## Usage
20
14
 
21
- SvelteKit 3 reads its options from the `sveltekit()` Vite plugin:
22
-
23
15
  ```js
24
- // vite.config.js
25
- import { sveltekit } from "@sveltejs/kit/vite";
26
- import { defineConfig } from "vite";
16
+ // svelte.config.js
27
17
  import adapter from "@orochibraru/svelte-smol";
28
18
 
29
- export default defineConfig({
30
- plugins: [sveltekit({ adapter: adapter() })],
31
- });
19
+ export default {
20
+ kit: {
21
+ adapter: adapter(),
22
+ },
23
+ };
32
24
  ```
33
25
 
34
26
  The compile step runs under the Bun runtime, so build with:
package/dist/index.d.ts CHANGED
@@ -130,22 +130,18 @@ export interface AdapterOptions {
130
130
  *
131
131
  * @example
132
132
  * ```js
133
- * // vite.config.js
134
- * import { sveltekit } from "@sveltejs/kit/vite";
135
- * import { defineConfig } from "vite";
133
+ * // svelte.config.js
136
134
  * import adapter from "@orochibraru/svelte-smol";
137
135
  *
138
- * export default defineConfig({
139
- * plugins: [
140
- * sveltekit({
141
- * adapter: adapter({
142
- * // cross-compile for an Alpine container from any host
143
- * target: "bun-linux-x64-musl",
144
- * bytecode: true,
145
- * }),
136
+ * export default {
137
+ * kit: {
138
+ * adapter: adapter({
139
+ * // cross-compile for an Alpine container from any host
140
+ * target: "bun-linux-x64-musl",
141
+ * bytecode: true,
146
142
  * }),
147
- * ],
148
- * });
143
+ * },
144
+ * };
149
145
  * ```
150
146
  *
151
147
  * @see {@link https://bun.com/docs/bundler/executables | Bun — Single-file executables}
package/dist/index.js CHANGED
@@ -1,4 +1,3 @@
1
- import { mkdirSync, rmSync } from "node:fs";
2
1
  import { fileURLToPath } from "node:url";
3
2
  // `node:url`, not `Bun.fileURLToPath`: this module is loaded at config-parse
4
3
  // time by Node-based tooling too (svelte-check, the Svelte language server,
@@ -43,22 +42,18 @@ const templates = fileURLToPath(new URL("./templates", import.meta.url));
43
42
  *
44
43
  * @example
45
44
  * ```js
46
- * // vite.config.js
47
- * import { sveltekit } from "@sveltejs/kit/vite";
48
- * import { defineConfig } from "vite";
45
+ * // svelte.config.js
49
46
  * import adapter from "@orochibraru/svelte-smol";
50
47
  *
51
- * export default defineConfig({
52
- * plugins: [
53
- * sveltekit({
54
- * adapter: adapter({
55
- * // cross-compile for an Alpine container from any host
56
- * target: "bun-linux-x64-musl",
57
- * bytecode: true,
58
- * }),
48
+ * export default {
49
+ * kit: {
50
+ * adapter: adapter({
51
+ * // cross-compile for an Alpine container from any host
52
+ * target: "bun-linux-x64-musl",
53
+ * bytecode: true,
59
54
  * }),
60
- * ],
61
- * });
55
+ * },
56
+ * };
62
57
  * ```
63
58
  *
64
59
  * @see {@link https://bun.com/docs/bundler/executables | Bun — Single-file executables}
@@ -77,12 +72,12 @@ export default function adapter(options = {}) {
77
72
  read: () => true,
78
73
  },
79
74
  async adapt(builder) {
80
- const { base } = builder.config.paths;
75
+ const { base } = builder.config.kit.paths;
81
76
  const tmp = builder.getBuildDirectory("adapter-bun");
82
- rmSync(out, { force: true, recursive: true });
83
- rmSync(tmp, { force: true, recursive: true });
84
- mkdirSync(out, { recursive: true });
85
- mkdirSync(tmp, { recursive: true });
77
+ builder.rimraf(out);
78
+ builder.rimraf(tmp);
79
+ builder.mkdirp(`${out}/`);
80
+ builder.mkdirp(tmp);
86
81
  builder.log.minor("Copying assets");
87
82
  builder.writeClient(`${out}/client${base}`);
88
83
  builder.writePrerendered(`${out}/prerendered${base}`);
@@ -95,15 +90,10 @@ export default function adapter(options = {}) {
95
90
  }
96
91
  builder.log.minor("Building server");
97
92
  builder.writeServer(`${tmp}/server`);
98
- // SvelteKit 3 no longer hands out a serialised manifest; it writes a
99
- // module exporting a ready-made `server` instance instead.
100
- builder.generateServerInstance(`${tmp}/server/instance.js`, {
101
- serverDirectory: `${tmp}/server`,
102
- });
103
93
  await Bun.write(`${tmp}/server/manifest.js`, [
94
+ `export const manifest = ${builder.generateManifest({ relativePath: "./" })};`,
104
95
  `export const prerendered = new Set(${JSON.stringify(builder.prerendered.paths)});`,
105
96
  `export const base = ${JSON.stringify(base)};`,
106
- `export const app_path = ${JSON.stringify(builder.getAppPath())};`,
107
97
  ].join("\n\n"));
108
98
  // Entry templates ship as raw `.ts` — `bun build --compile` runs them
109
99
  // straight through. The virtual specifiers (`ENV`, `MANIFEST`,
@@ -124,22 +114,16 @@ export default function adapter(options = {}) {
124
114
  HANDLER: "./handler.ts",
125
115
  MANIFEST: "./server/manifest.js",
126
116
  SERVE_OPTIONS: JSON.stringify(serveOptions),
127
- SERVER: "./server/instance.js",
117
+ SERVER: "./server/index.js",
128
118
  },
129
119
  });
130
120
  const entry = `${tmp}/index.ts`;
131
- if (builder.hasServerInstrumentationFile()) {
121
+ if (builder.hasServerInstrumentationFile?.()) {
132
122
  // Instrumentation (OpenTelemetry &c.) has to load before anything
133
123
  // else in the bundle. A compiled binary has no post-build
134
- // entrypoint to rewrite, so prepend the imports to the compile
135
- // entrypoint instead. The initializer populates
136
- // `$env/dynamic/private` first, so instrumentation can read it.
137
- const initializer = builder.createInstrumentationInitializer({
138
- outputDirectory: tmp,
139
- serverDirectory: `${tmp}/server`,
140
- environment: "export default Bun.env;",
141
- });
142
- await Bun.write(entry, `import ${JSON.stringify(initializer)};\nimport "./server/instrumentation.server.js";\n${await Bun.file(entry).text()}`);
124
+ // entrypoint to rewrite, so prepend the import to the compile
125
+ // entrypoint instead.
126
+ await Bun.write(entry, `import "./server/instrumentation.server.js";\n${await Bun.file(entry).text()}`);
143
127
  }
144
128
  const check = (result, label) => {
145
129
  if (!result.success) {
@@ -1,13 +1,13 @@
1
1
  /* global ENV_PREFIX, BUILD_OPTIONS */
2
2
 
3
3
  import { env } from "ENV";
4
- import { app_path, base, prerendered } from "MANIFEST";
5
- import { server as kit_server } from "SERVER";
4
+ import { base, manifest, prerendered } from "MANIFEST";
5
+ import { Server } from "SERVER";
6
6
  import { statSync } from "node:fs";
7
7
  import { dirname, resolve } from "node:path";
8
8
  import type { Server as SvelteKitServer } from "@sveltejs/kit";
9
9
 
10
- const server = kit_server as SvelteKitServer & {
10
+ const server = new Server(manifest) as SvelteKitServer & {
11
11
  websocket?: () => Bun.WebSocketHandler<undefined> | undefined;
12
12
  };
13
13
 
@@ -33,7 +33,7 @@ const self_dir = compiled ? dirname(process.execPath) : import.meta.dir;
33
33
  const assets_root = resolve(self_dir, env("ASSETS_DIR", ""));
34
34
  const client_dir = `${assets_root}/client${base}`;
35
35
  const prerendered_dir = `${assets_root}/prerendered${base}`;
36
- const immutable_prefix = `/${app_path}/immutable/`;
36
+ const immutable_prefix = `${base}/${manifest.appDir}/immutable/`;
37
37
 
38
38
  await server.init({
39
39
  env: Bun.env as Record<string, string>,
@@ -166,7 +166,10 @@ const ssr = async (request: Request, bunServer: Bun.Server<undefined>) => {
166
166
  return asset;
167
167
  }
168
168
 
169
- const baseOrigin = origin || get_origin(request.headers);
169
+ const baseOrigin =
170
+ same_host_remote_origin(request, url) ||
171
+ origin ||
172
+ get_origin(request.headers);
170
173
  const path = request.url.slice(request.url.split("/", 3).join("/").length);
171
174
  const newRequest = new Request(baseOrigin + path, request);
172
175
 
@@ -233,3 +236,24 @@ function get_origin(headers: Headers) {
233
236
 
234
237
  return port ? `${protocol}://${host}:${port}` : `${protocol}://${host}`;
235
238
  }
239
+
240
+ // Remote function calls are CSRF-checked against the request origin. When the
241
+ // browser's Origin header matches the Host, use it instead of ORIGIN.
242
+ function same_host_remote_origin(request: Request, url: URL) {
243
+ const request_origin = request.headers.get("origin");
244
+ if (
245
+ !request_origin ||
246
+ !url.pathname.startsWith(`${base}/${manifest.appDir}/remote/`)
247
+ ) {
248
+ return null;
249
+ }
250
+ try {
251
+ const parsed = new URL(request_origin);
252
+ const host =
253
+ (host_header && request.headers.get(host_header)) ||
254
+ request.headers.get("host");
255
+ return parsed.host === host ? parsed.origin : null;
256
+ } catch {
257
+ return null;
258
+ }
259
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orochibraru/svelte-smol",
3
- "version": "1.7.0-next.2",
3
+ "version": "1.7.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "keywords": [
@@ -48,7 +48,7 @@
48
48
  "@biomejs/biome": "^2.5.11",
49
49
  "@semantic-release/github": "^12.0.9",
50
50
  "@semantic-release/npm": "^13.1.5",
51
- "@sveltejs/kit": "^3.0.0-next.27",
51
+ "@sveltejs/kit": "^2.70.3",
52
52
  "@sveltejs/vite-plugin-svelte": "^7.3.0",
53
53
  "@types/bun": "^1.4.0",
54
54
  "conventional-changelog-conventionalcommits": "^8",
@@ -59,6 +59,6 @@
59
59
  "vite": "^8.2.2"
60
60
  },
61
61
  "peerDependencies": {
62
- "@sveltejs/kit": "^3.0.0-next.0"
62
+ "@sveltejs/kit": "^2.0.0"
63
63
  }
64
64
  }