@eslym/sveltekit-adapter-bun 1.0.13 → 1.0.15
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 +62 -1
- package/dist/files/index.js +14 -13
- package/dist/index.d.ts +1 -1
- package/dist/index.js +22 -20
- package/dist/types.d.ts +6 -7
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -8,4 +8,65 @@ Another sveltekit adapter for bun, an alternative to [svelte-adapter-bun](https:
|
|
|
8
8
|
bun add -d @eslym/sveltekit-adapter-bun
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Setup dev server
|
|
12
|
+
|
|
13
|
+
> [!NOTE]
|
|
14
|
+
> You do not need to do this if you are not using websocket in dev mode.
|
|
15
|
+
|
|
16
|
+
1. Create an entrypoint file for dev server, e.g. `./dev.ts`
|
|
17
|
+
2. Add the following code to the entrypoint file
|
|
18
|
+
```typescript
|
|
19
|
+
import { patchSvelteKit, startDevServer } from '@eslym/sveltekit-adapter-bun';
|
|
20
|
+
|
|
21
|
+
await patchSvelteKit();
|
|
22
|
+
await startDevServer();
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
3. run `bun dev.ts`
|
|
26
|
+
|
|
27
|
+
The `patchSvelteKit` function will patch the sveltekit using `bun patch` to let it get the original `Request` object from bun and pass it to the dev server, making `Bun.Server#upgrade` possible. The `startDevServer` function will start the dev server with websocket support.
|
|
28
|
+
|
|
29
|
+
The `patchSvelteKit` will not impact anything in production build, since the production build will not involve `@sveltejs/kit/node` unless you are using it in your code.
|
|
30
|
+
|
|
31
|
+
> [!IMPORTANT]
|
|
32
|
+
> This dev server uses bun's internal stuff, so it might break in the future bun version, but the
|
|
33
|
+
> production build will not be affected.
|
|
34
|
+
|
|
35
|
+
## Use the websocket
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
// ./src/app.d.ts
|
|
39
|
+
// for the type checking
|
|
40
|
+
|
|
41
|
+
import type { AdapterPlatform } from '@eslym/sveltekit-adapter-bun';
|
|
42
|
+
|
|
43
|
+
// See https://kit.svelte.dev/docs/types#app
|
|
44
|
+
// for information about these interfaces
|
|
45
|
+
declare global {
|
|
46
|
+
namespace App {
|
|
47
|
+
// interface Error {}
|
|
48
|
+
// interface Locals {}
|
|
49
|
+
// interface PageData {}
|
|
50
|
+
// interface PageState {}
|
|
51
|
+
interface Platform extends AdapterPlatform {}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// ./src/routes/echo/+server.ts
|
|
58
|
+
|
|
59
|
+
export async function GET({ platform }) {
|
|
60
|
+
// can mark any response for upgrade, if the upgrade failed, the response will be sent as is
|
|
61
|
+
return platform!.markForUpgrade(
|
|
62
|
+
new Response('Websocket Requried', {
|
|
63
|
+
status: 400
|
|
64
|
+
}),
|
|
65
|
+
{
|
|
66
|
+
message(ws, message) {
|
|
67
|
+
ws.send(message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
package/dist/files/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// src/files/server.ts
|
|
3
|
-
import {Server} from "SERVER";
|
|
4
|
-
import {manifest} from "MANIFEST";
|
|
3
|
+
import { Server } from "SERVER";
|
|
4
|
+
import { manifest } from "MANIFEST";
|
|
5
5
|
var server = new Server(manifest);
|
|
6
6
|
await server.init({
|
|
7
7
|
env: Bun.env,
|
|
@@ -11,7 +11,7 @@ await server.init({
|
|
|
11
11
|
});
|
|
12
12
|
|
|
13
13
|
// node_modules/cac/dist/index.mjs
|
|
14
|
-
import {EventEmitter} from "events";
|
|
14
|
+
import { EventEmitter } from "events";
|
|
15
15
|
function toArr(any) {
|
|
16
16
|
return any == null ? [] : Array.isArray(any) ? any : [any];
|
|
17
17
|
}
|
|
@@ -618,16 +618,16 @@ function set_url(request, url) {
|
|
|
618
618
|
return url;
|
|
619
619
|
}
|
|
620
620
|
function get_basepath() {
|
|
621
|
-
return basePath;
|
|
621
|
+
return globalThis[basePath] || "";
|
|
622
622
|
}
|
|
623
623
|
function set_basepath(path) {
|
|
624
|
-
basePath = path;
|
|
624
|
+
globalThis[basePath] = path;
|
|
625
625
|
}
|
|
626
626
|
var urls = new WeakMap;
|
|
627
|
-
var basePath;
|
|
627
|
+
var basePath = Symbol.for("app.basepath");
|
|
628
628
|
|
|
629
629
|
// src/files/static.ts
|
|
630
|
-
import {normalize} from "path/posix";
|
|
630
|
+
import { normalize } from "path/posix";
|
|
631
631
|
|
|
632
632
|
// src/mapping.ts
|
|
633
633
|
var mapping = {
|
|
@@ -859,10 +859,10 @@ function create_fetch({
|
|
|
859
859
|
resolvers.push((args) => override_origin_with_header(args, hostHeader, protocolHeader));
|
|
860
860
|
}
|
|
861
861
|
resolvers.push(({ request }) => serve_static(request, basePath2));
|
|
862
|
-
return
|
|
862
|
+
return (request, srv) => {
|
|
863
863
|
const request_ip = srv.requestIP(request)?.address;
|
|
864
864
|
const try_get_ip = getIp ? () => getIp(request, request_ip) : () => request_ip;
|
|
865
|
-
return
|
|
865
|
+
return first_resolve(request, [
|
|
866
866
|
...resolvers,
|
|
867
867
|
async (args) => {
|
|
868
868
|
const res = await server.respond(args.request, {
|
|
@@ -894,7 +894,7 @@ function create_fetch({
|
|
|
894
894
|
}
|
|
895
895
|
|
|
896
896
|
// src/files/index.ts
|
|
897
|
-
import {get_hooks} from "SERVER";
|
|
897
|
+
import { get_hooks } from "SERVER";
|
|
898
898
|
set_basepath(import.meta.dir);
|
|
899
899
|
var hooks = await get_hooks();
|
|
900
900
|
var cli = dist_default(CLI_NAME);
|
|
@@ -915,7 +915,7 @@ cli.command("", "Serve the app").alias("serve").option("--port, -p <port>", "Por
|
|
|
915
915
|
hostname: options.host,
|
|
916
916
|
port: options.port
|
|
917
917
|
};
|
|
918
|
-
const
|
|
918
|
+
const server2 = Bun.serve({
|
|
919
919
|
...serverOptions,
|
|
920
920
|
fetch: create_fetch(options),
|
|
921
921
|
websocket: {
|
|
@@ -940,8 +940,8 @@ cli.command("", "Serve the app").alias("serve").option("--port, -p <port>", "Por
|
|
|
940
940
|
}
|
|
941
941
|
}
|
|
942
942
|
});
|
|
943
|
-
await hooks.afterServe?.(
|
|
944
|
-
console.log(`Serving on ${
|
|
943
|
+
await hooks.afterServe?.(server2, options);
|
|
944
|
+
console.log(`Serving on ${server2.url}`);
|
|
945
945
|
});
|
|
946
946
|
cli.help();
|
|
947
947
|
await hooks.setupCLI?.(cli);
|
|
@@ -949,5 +949,6 @@ if (Bun.main === Bun.fileURLToPath(import.meta.url)) {
|
|
|
949
949
|
cli.parse();
|
|
950
950
|
}
|
|
951
951
|
export {
|
|
952
|
+
create_fetch as createBunFetch,
|
|
952
953
|
cli
|
|
953
954
|
};
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -3,22 +3,22 @@
|
|
|
3
3
|
var name = "@eslym/sveltekit-adapter-bun";
|
|
4
4
|
|
|
5
5
|
// src/index.ts
|
|
6
|
-
import {fileURLToPath} from "url";
|
|
6
|
+
import { fileURLToPath } from "url";
|
|
7
7
|
import zlib from "zlib";
|
|
8
8
|
import {
|
|
9
|
-
createReadStream,
|
|
10
|
-
createWriteStream,
|
|
11
|
-
existsSync,
|
|
12
|
-
readFileSync,
|
|
13
|
-
statSync,
|
|
14
|
-
writeFileSync
|
|
9
|
+
createReadStream,
|
|
10
|
+
createWriteStream,
|
|
11
|
+
existsSync,
|
|
12
|
+
readFileSync,
|
|
13
|
+
statSync,
|
|
14
|
+
writeFileSync
|
|
15
15
|
} from "fs";
|
|
16
|
-
import {pipeline} from "stream/promises";
|
|
17
|
-
import {rollup} from "rollup";
|
|
18
|
-
import {nodeResolve} from "@rollup/plugin-node-resolve";
|
|
16
|
+
import { pipeline } from "stream/promises";
|
|
17
|
+
import { rollup } from "rollup";
|
|
18
|
+
import { nodeResolve } from "@rollup/plugin-node-resolve";
|
|
19
19
|
import commonjs from "@rollup/plugin-commonjs";
|
|
20
20
|
import json from "@rollup/plugin-json";
|
|
21
|
-
import {join as join2} from "path/posix";
|
|
21
|
+
import { join as join2 } from "path/posix";
|
|
22
22
|
|
|
23
23
|
// node_modules/devalue/src/utils.js
|
|
24
24
|
function is_primitive(thing) {
|
|
@@ -296,9 +296,16 @@ var symServer = Symbol.for(`${name}/server`);
|
|
|
296
296
|
var symUpgrades = Symbol.for(`${name}/upgrades`);
|
|
297
297
|
|
|
298
298
|
// src/dev.ts
|
|
299
|
-
import {dirname, join} from "path";
|
|
300
|
-
import {EventEmitter} from "events";
|
|
301
|
-
import {IncomingMessage, ServerResponse} from "http";
|
|
299
|
+
import { dirname, join } from "path";
|
|
300
|
+
import { EventEmitter } from "events";
|
|
301
|
+
import { IncomingMessage, ServerResponse } from "http";
|
|
302
|
+
function setupBunternal(socket, bunServer, httpServer, httpRes, bunReq) {
|
|
303
|
+
if (Bun.semver.satisfies(Bun.version, "<1.1.25")) {
|
|
304
|
+
socket[bunternal] = [bunServer, httpRes, bunReq];
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
socket[bunternal] = [httpServer, httpRes, bunReq];
|
|
308
|
+
}
|
|
302
309
|
async function patchSveltekit() {
|
|
303
310
|
if (!("Bun" in globalThis)) {
|
|
304
311
|
throw new Error("Please run with bun");
|
|
@@ -385,7 +392,7 @@ async function startDevServer({
|
|
|
385
392
|
const req = new IncomingMessage(request);
|
|
386
393
|
const res = new ServerResponse(req, respond);
|
|
387
394
|
const socket = req.socket;
|
|
388
|
-
setupBunternal(socket, server2, fakeServer,
|
|
395
|
+
setupBunternal(socket, server2, fakeServer, res, request);
|
|
389
396
|
req.once("error", raise);
|
|
390
397
|
res.once("error", raise);
|
|
391
398
|
if (request.headers.get("upgrade")) {
|
|
@@ -447,11 +454,6 @@ var setResponsePatch = `
|
|
|
447
454
|
}
|
|
448
455
|
`;
|
|
449
456
|
var bunternal = Symbol.for("::bunternal::");
|
|
450
|
-
var setupBunternal = Bun.semver.satisfies(Bun.version, "<1.1.25") ? (socket, bunServer, _, httpReq, bunReq) => {
|
|
451
|
-
socket[bunternal] = [bunServer, httpReq, bunReq];
|
|
452
|
-
} : (socket, _, httpServer, httpReq, bunReq) => {
|
|
453
|
-
socket[bunternal] = [httpServer, httpReq, bunReq];
|
|
454
|
-
};
|
|
455
457
|
|
|
456
458
|
// src/index.ts
|
|
457
459
|
async function compress(directory, options) {
|
package/dist/types.d.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
/// <reference types="bun-types" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
/// <reference types="bun-types" />
|
|
4
1
|
import type { WebSocketHandler as BunWSHandler, ServerWebSocket } from 'bun';
|
|
5
2
|
import type { Server } from 'bun';
|
|
6
|
-
export type
|
|
7
|
-
port: number;
|
|
8
|
-
host: string;
|
|
9
|
-
unixSocket?: string;
|
|
3
|
+
export type CreateFetchOptions = {
|
|
10
4
|
overrideOrigin?: string;
|
|
11
5
|
hostHeader?: string;
|
|
12
6
|
protocolHeader?: string;
|
|
13
7
|
ipHeader?: string;
|
|
14
8
|
xffDepth?: number;
|
|
15
9
|
};
|
|
10
|
+
export type ServeOptions = {
|
|
11
|
+
port: number;
|
|
12
|
+
host: string;
|
|
13
|
+
unixSocket?: string;
|
|
14
|
+
} & CreateFetchOptions;
|
|
16
15
|
export type WebSocketOptions = Omit<BunWSHandler, 'message' | 'open' | 'close' | 'ping' | 'pong' | 'drain'>;
|
|
17
16
|
export interface WebSocketHandler {
|
|
18
17
|
/**
|
package/package.json
CHANGED
|
@@ -21,18 +21,18 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
|
-
"@sveltejs/kit": "^2.5.
|
|
24
|
+
"@sveltejs/kit": "^2.5.26",
|
|
25
25
|
"@types/bun": "latest",
|
|
26
26
|
"cac": "^6.7.14",
|
|
27
27
|
"devalue": "^5.0.0",
|
|
28
|
-
"prettier": "^3.3.
|
|
29
|
-
"typescript": "^5.
|
|
28
|
+
"prettier": "^3.3.3",
|
|
29
|
+
"typescript": "^5.5.4"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@rollup/plugin-commonjs": "^26.0.1",
|
|
33
33
|
"@rollup/plugin-json": "^6.1.0",
|
|
34
34
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
35
|
-
"rollup": "^4.
|
|
35
|
+
"rollup": "^4.21.2"
|
|
36
36
|
},
|
|
37
|
-
"version": "
|
|
37
|
+
"version": "v1.0.15"
|
|
38
38
|
}
|