@orochibraru/svelte-smol 1.8.2 → 1.8.3
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 +61 -18
- package/dist/kit3/templates/env.ts +1 -0
- package/dist/kit3/templates/handler.ts +16 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -10,6 +10,9 @@ 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
|
+
Every merge to `main` ships as `@orochibraru/svelte-smol@canary`; stable
|
|
14
|
+
releases go out under `latest`.
|
|
15
|
+
|
|
13
16
|
## Usage
|
|
14
17
|
|
|
15
18
|
```typescript
|
|
@@ -29,28 +32,68 @@ The compile step runs under the Bun runtime, so build with:
|
|
|
29
32
|
bun run vite build
|
|
30
33
|
```
|
|
31
34
|
|
|
32
|
-
## SvelteKit 3
|
|
35
|
+
## SvelteKit 2 and SvelteKit 3
|
|
33
36
|
|
|
34
|
-
|
|
35
|
-
which SvelteKit is
|
|
36
|
-
|
|
37
|
+
One package supports both. There is no flag to set: at build time the adapter
|
|
38
|
+
checks which SvelteKit is running the build and uses the matching
|
|
39
|
+
implementation. Upgrading SvelteKit is enough to switch.
|
|
37
40
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
| | SvelteKit 2 | SvelteKit 3 (prerelease) |
|
|
42
|
+
| ------------------- | --------------------------------------- | ---------------------------------------------- |
|
|
43
|
+
| Adapter config | `svelte.config.js` | `vite.config.js`, passed to `sveltekit()` |
|
|
44
|
+
| Static assets | `client/` and `prerendered/` beside it | embedded in the executable |
|
|
45
|
+
| Build options | top level (`compile`, `target`, …) | under `buildOptions` |
|
|
46
|
+
| `Bun.serve` options | `serveOptions` | `serverOptions` (JSON-serializable only) |
|
|
47
|
+
| Public origin | `ORIGIN` env var | `ORIGIN` env var, else `paths.origin` |
|
|
43
48
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
49
|
+
The rest of this README covers SvelteKit 2. For SvelteKit 3's options,
|
|
50
|
+
environment variables and output, see [SvelteKit 3](docs/sveltekit-3.md).
|
|
51
|
+
|
|
52
|
+
### Upgrading to SvelteKit 3
|
|
53
|
+
|
|
54
|
+
1. Install SvelteKit 3: `bun add -d @sveltejs/kit@next`.
|
|
55
|
+
2. Move the adapter from `svelte.config.js` into the Vite plugin:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
// vite.config.js
|
|
59
|
+
import { sveltekit } from "@sveltejs/kit/vite";
|
|
60
|
+
import { defineConfig } from "vite";
|
|
61
|
+
import adapter from "@orochibraru/svelte-smol";
|
|
62
|
+
|
|
63
|
+
export default defineConfig({
|
|
64
|
+
plugins: [sveltekit({ adapter: adapter() })],
|
|
65
|
+
});
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
3. Rename your options and env vars:
|
|
69
|
+
|
|
70
|
+
| SvelteKit 2 | SvelteKit 3 |
|
|
71
|
+
| --------------------------------- | ------------------------------------------------ |
|
|
72
|
+
| `compile: false` | `buildOptions: { compile: false }` |
|
|
73
|
+
| `target: "bun-linux-x64"` | `buildOptions: { compile: "bun-linux-x64" }` |
|
|
74
|
+
| `name: "app"` | `buildOptions: { compile: { outfile: "app" } }` |
|
|
75
|
+
| `bytecode`, `minify`, `sourcemap` | `buildOptions.*` |
|
|
76
|
+
| `serveOptions` | `serverOptions` (JSON-serializable only) |
|
|
77
|
+
| `serveAssets: false` | removed, assets are embedded |
|
|
78
|
+
| `IDLE_TIMEOUT` | `CONNECTION_IDLE_TIMEOUT` |
|
|
79
|
+
| `ASSETS_DIR` | removed, assets are embedded |
|
|
80
|
+
|
|
81
|
+
4. Deploy the `build/server` executable on its own: it no longer needs the
|
|
82
|
+
`client/` and `prerendered/` folders next to it.
|
|
83
|
+
|
|
84
|
+
`out`, `healthcheck` and `envPrefix` work the same on both. `precompress` too,
|
|
85
|
+
except that SvelteKit 3 ignores it when compiling, since it has no asset files
|
|
86
|
+
to serve. Options that belong to the other SvelteKit version fail the build instead of
|
|
87
|
+
being ignored:
|
|
88
|
+
|
|
89
|
+
```text
|
|
90
|
+
svelte-smol: compile not supported with SvelteKit 3. See https://github.com/orochibraru/svelte-smol/blob/main/docs/sveltekit-3.md for the option mapping.
|
|
47
91
|
```
|
|
48
92
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
SvelteKit 2.
|
|
93
|
+
Going back to SvelteKit 2 is the same steps in reverse.
|
|
94
|
+
|
|
95
|
+
SvelteKit 2 support will be deprecated once SvelteKit 3 is stable, and removed
|
|
96
|
+
in the next major version of this package.
|
|
54
97
|
|
|
55
98
|
## Output
|
|
56
99
|
|
|
@@ -130,7 +173,7 @@ runtime the first time you use a target.
|
|
|
130
173
|
| `PORT` | `3000` | Listen port |
|
|
131
174
|
| `SOCKET_PATH` | — | Listen on a Unix socket instead of `HOST`/`PORT` |
|
|
132
175
|
| `ASSETS_DIR` | — | Override where `client/` and `prerendered/` are looked up (absolute, or relative to the binary) |
|
|
133
|
-
| `ORIGIN` | — |
|
|
176
|
+
| `ORIGIN` | — | Public origin, read at runtime (Kit 2 and 3; on Kit 3 it overrides `paths.origin`) |
|
|
134
177
|
| `PROTOCOL_HEADER` | — | Header carrying the forwarded protocol (e.g. `x-forwarded-proto`) |
|
|
135
178
|
| `HOST_HEADER` | — | Header carrying the forwarded host |
|
|
136
179
|
| `PORT_HEADER` | — | Header carrying the forwarded port |
|
|
@@ -9,6 +9,8 @@ const protocol_header = env("PROTOCOL_HEADER", "").toLowerCase();
|
|
|
9
9
|
const host_header = env("HOST_HEADER", "").toLowerCase();
|
|
10
10
|
const port_header = env("PORT_HEADER", "").toLowerCase();
|
|
11
11
|
const xff_depth = number_env("XFF_DEPTH", 1, { min: 1 });
|
|
12
|
+
// runtime wins over paths.origin: the same build is deployed at different origins
|
|
13
|
+
const origin_env = parse_origin(env("ORIGIN", undefined));
|
|
12
14
|
|
|
13
15
|
await server.init({
|
|
14
16
|
env: Bun.env as Record<string, string>,
|
|
@@ -43,6 +45,7 @@ function normalize_request(request: Request): Request | Response {
|
|
|
43
45
|
const url = new URL(request.url);
|
|
44
46
|
const request_origin =
|
|
45
47
|
same_host_remote_origin(request, url) ||
|
|
48
|
+
origin_env ||
|
|
46
49
|
origin ||
|
|
47
50
|
get_origin(request, url);
|
|
48
51
|
return request_origin === url.origin
|
|
@@ -56,6 +59,19 @@ function normalize_request(request: Request): Request | Response {
|
|
|
56
59
|
}
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
function parse_origin(value: string | undefined): string | undefined {
|
|
63
|
+
// empty means unset, as in the Kit 2 handler
|
|
64
|
+
if (!value) return undefined;
|
|
65
|
+
// .origin drops any path or trailing slash, so the url.origin comparison holds
|
|
66
|
+
const url = URL.parse(value);
|
|
67
|
+
if (!url || !/^https?:$/.test(url.protocol)) {
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Invalid value for environment variable ${env_prefix}ORIGIN: ${JSON.stringify(value)} (expected an absolute URL, e.g. https://my.site)`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
return url.origin;
|
|
73
|
+
}
|
|
74
|
+
|
|
59
75
|
function get_origin(request: Request, url: URL): string {
|
|
60
76
|
// assume TLS terminates upstream, like adapter-node; an http origin would fail CSRF checks
|
|
61
77
|
const protocol = decodeURIComponent(
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orochibraru/svelte-smol",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"build": "rm -rf dist && tsc -p tsconfig.build.json && cp -R kit2/templates dist/kit2/templates && cp -R kit3/templates dist/kit3/templates",
|
|
37
37
|
"prepack": "bun run build",
|
|
38
38
|
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p kit2 && tsc --noEmit -p kit3",
|
|
39
|
-
"test": "bun test index.test.ts",
|
|
39
|
+
"test": "bun test index.test.ts handler.test.ts",
|
|
40
40
|
"test:integration": "bun test integration",
|
|
41
41
|
"lint": "biome check .",
|
|
42
42
|
"lint:fix": "biome check --write .",
|