Skip to content

Commit 60ec88d

Browse files
committed
fix some types and exports
1 parent 767316b commit 60ec88d

File tree

2 files changed

+43
-39
lines changed

2 files changed

+43
-39
lines changed

src/files/handle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function resolve_xff_ip(request: Request, depth: number) {
7171
return ips.at(-depth) || undefined;
7272
}
7373

74-
export async function create_fetch({
74+
export function create_fetch({
7575
overrideOrigin,
7676
hostHeader,
7777
protocolHeader,
@@ -98,7 +98,7 @@ export async function create_fetch({
9898
if (SERVE_STATIC) {
9999
resolvers.push(serve_static);
100100
}
101-
return (request: Request, srv: Server) => {
101+
return (request: Request, srv: Bun.Server<unknown>) => {
102102
const request_ip = srv.requestIP(request)?.address;
103103
const try_get_ip = getIp ? () => getIp(request, request_ip) : () => request_ip;
104104
return first_resolve(request, [

src/files/index.ts

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@ import type { WebSocketHandler } from '../types';
44
import { create_fetch } from './handle';
55
import type { WebSocketHandler as BunWSHandler } from 'bun';
66
import { bool_env, bytes_env, duration_env, get_env, int_env } from './env';
7-
import { resolve } from 'path';
87

9-
Bun.plugin({
10-
name: 'asset-module',
11-
target: 'bun',
12-
setup(build) {
13-
build.module('sveltekit-adapter-bun:assets', async () => ({
14-
loader: 'object',
15-
exports: await import(resolve(import.meta.dir, './assets'))
16-
}));
8+
export const websocketHandler = {
9+
message(ws, message) {
10+
return ws.data.message(ws, message);
11+
},
12+
open(ws) {
13+
return ws.data.open?.(ws);
14+
},
15+
close(ws, code, reason) {
16+
return ws.data.close?.(ws, code, reason);
17+
},
18+
ping(ws, data) {
19+
return ws.data.ping?.(ws, data);
20+
},
21+
pong(ws, data) {
22+
return ws.data.pong?.(ws, data);
23+
},
24+
drain(ws) {
25+
return ws.data.drain?.(ws);
1726
}
18-
});
27+
} as BunWSHandler<WebSocketHandler>;
1928

20-
async function serve() {
29+
export function serveOptions() {
2130
const socket = get_env('HTTP_SOCKET');
2231
const serverOptions = socket
2332
? {
@@ -28,41 +37,36 @@ async function serve() {
2837
port: get_env('HTTP_PORT', '3000')
2938
};
3039

31-
const server = Bun.serve({
40+
return {
3241
...serverOptions,
3342
idleTimeout: duration_env('HTTP_IDLE_TIMEOUT', 30),
34-
maxRequestBodySize: bytes_env('HTTP_MAX_BODY', 128 * 1024 * 1024),
35-
fetch: await create_fetch({
43+
maxRequestBodySize: bytes_env('HTTP_MAX_BODY', 128 * 1024 * 1024)
44+
};
45+
}
46+
47+
export function websocketOptions() {
48+
return {
49+
idleTimeout: duration_env('WS_IDLE_TIMEOUT', 120),
50+
maxPayloadLength: bytes_env('WS_MAX_PAYLOAD', 16 * 1024 * 1024),
51+
sendPings: !bool_env('WS_NO_PING')
52+
};
53+
}
54+
55+
export function serve() {
56+
const server = Bun.serve({
57+
...serveOptions(),
58+
fetch: create_fetch({
3659
overrideOrigin: get_env('HTTP_OVERRIDE_ORIGIN'),
3760
hostHeader: get_env('HTTP_HOST_HEADER'),
3861
protocolHeader: get_env('HTTP_PROTOCOL_HEADER'),
3962
ipHeader: get_env('HTTP_IP_HEADER'),
4063
xffDepth: int_env('HTTP_XFF_DEPTH', 1)
4164
}),
4265
websocket: {
43-
idleTimeout: duration_env('WS_IDLE_TIMEOUT', 120),
44-
maxPayloadLength: bytes_env('WS_MAX_PAYLOAD', 16 * 1024 * 1024),
45-
sendPings: !bool_env('WS_NO_PING'),
46-
message(ws, message) {
47-
return ws.data.message(ws, message);
48-
},
49-
open(ws) {
50-
return ws.data.open?.(ws);
51-
},
52-
close(ws, code, reason) {
53-
return ws.data.close?.(ws, code, reason);
54-
},
55-
ping(ws, data) {
56-
return ws.data.ping?.(ws, data);
57-
},
58-
pong(ws, data) {
59-
return ws.data.pong?.(ws, data);
60-
},
61-
drain(ws) {
62-
return ws.data.drain?.(ws);
63-
}
64-
} as BunWSHandler<WebSocketHandler>
65-
});
66+
...websocketOptions(),
67+
...websocketHandler
68+
}
69+
} as any);
6670
console.log(`Serving on ${server.url}`);
6771
}
6872

0 commit comments

Comments
 (0)