@eslym/sveltekit-adapter-bun 2.0.2 → 2.0.5
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 +17 -15
- package/dist/files/index.js +121 -119
- package/dist/index.js +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -165,18 +165,20 @@ export type PreCompressOptions = {
|
|
|
165
165
|
|
|
166
166
|
## Runtime Environments
|
|
167
167
|
|
|
168
|
-
| Name | Description | Default
|
|
169
|
-
| ---------------------- | ------------------------------------------------------------------------------------ |
|
|
170
|
-
| `HTTP_HOST` | The host for the server | `0.0.0.0`
|
|
171
|
-
| `HTTP_PORT` | The port for the server | `3000`
|
|
172
|
-
| `HTTP_SOCKET` | The path of the unix socket which the server will listen to (this will disable http) | -
|
|
173
|
-
| `HTTP_PROTOCOL_HEADER` | The header name to get the protocol from the request | -
|
|
174
|
-
| `HTTP_HOST_HEADER` | The header name to get the host from the request | -
|
|
175
|
-
| `HTTP_IP_HEADER` | The header name to get the client ip from the request (usually `X-Forwarded-For`) | -
|
|
176
|
-
| `HTTP_XFF_DEPTH` | The depth of the `X-Forwarded-For` header to get the client ip | `1`
|
|
177
|
-
| `HTTP_OVERRIDE_ORIGIN` | Force the request origin when it is unable to retrieve from the request | -
|
|
178
|
-
| `HTTP_IDLE_TIMEOUT` | The request timeout for the server(in seconds) | `30`
|
|
179
|
-
| `HTTP_MAX_BODY` | The maximum body size for the request | `128mib`
|
|
180
|
-
| `WS_IDLE_TIMEOUT` | The websocket idle timeout (in seconds) | `120`
|
|
181
|
-
| `WS_MAX_PAYLOAD` | The maximum payload size for the websocket | `16mib`
|
|
182
|
-
| `WS_NO_PING` | Disable automatic ping response | `false`
|
|
168
|
+
| Name | Description | Default |
|
|
169
|
+
| ---------------------- | ------------------------------------------------------------------------------------ | ---------- |
|
|
170
|
+
| `HTTP_HOST` | The host for the server | `0.0.0.0` |
|
|
171
|
+
| `HTTP_PORT` | The port for the server | `3000` |
|
|
172
|
+
| `HTTP_SOCKET` | The path of the unix socket which the server will listen to (this will disable http) | - |
|
|
173
|
+
| `HTTP_PROTOCOL_HEADER` | The header name to get the protocol from the request | - |
|
|
174
|
+
| `HTTP_HOST_HEADER` | The header name to get the host from the request | - |
|
|
175
|
+
| `HTTP_IP_HEADER` | The header name to get the client ip from the request (usually `X-Forwarded-For`) | - |
|
|
176
|
+
| `HTTP_XFF_DEPTH` | The depth of the `X-Forwarded-For` header to get the client ip | `1` |
|
|
177
|
+
| `HTTP_OVERRIDE_ORIGIN` | Force the request origin when it is unable to retrieve from the request | - |
|
|
178
|
+
| `HTTP_IDLE_TIMEOUT` | The request timeout for the server(in seconds) | `30` |
|
|
179
|
+
| `HTTP_MAX_BODY` | The maximum body size for the request | `128mib` |
|
|
180
|
+
| `WS_IDLE_TIMEOUT` | The websocket idle timeout (in seconds) | `120` |
|
|
181
|
+
| `WS_MAX_PAYLOAD` | The maximum payload size for the websocket | `16mib` |
|
|
182
|
+
| `WS_NO_PING` | Disable automatic ping response | `false` |
|
|
183
|
+
| `CACHE_ASSET_AGE` | The max-age for the cache-control header for the assets | `14400` |
|
|
184
|
+
| `CACHE_IMMUTABLE_AGE` | The max-age for the cache-control header for the immutable assets | `31536000` |
|
package/dist/files/index.js
CHANGED
|
@@ -28,8 +28,114 @@ function set_url(request, url) {
|
|
|
28
28
|
// src/files/static.ts
|
|
29
29
|
import { normalize } from "path/posix";
|
|
30
30
|
import { assets } from "ASSETS";
|
|
31
|
+
|
|
32
|
+
// node_modules/parse-duration/locale/en.js
|
|
33
|
+
var unit = Object.create(null);
|
|
34
|
+
var m = 60000;
|
|
35
|
+
var h = m * 60;
|
|
36
|
+
var d = h * 24;
|
|
37
|
+
var y = d * 365.25;
|
|
38
|
+
unit.year = unit.yr = unit.y = y;
|
|
39
|
+
unit.month = unit.mo = unit.mth = y / 12;
|
|
40
|
+
unit.week = unit.wk = unit.w = d * 7;
|
|
41
|
+
unit.day = unit.d = d;
|
|
42
|
+
unit.hour = unit.hr = unit.h = h;
|
|
43
|
+
unit.minute = unit.min = unit.m = m;
|
|
44
|
+
unit.second = unit.sec = unit.s = 1000;
|
|
45
|
+
unit.millisecond = unit.millisec = unit.ms = 1;
|
|
46
|
+
unit.microsecond = unit.microsec = unit.us = unit.\u{b5}s = 0.001;
|
|
47
|
+
unit.nanosecond = unit.nanosec = unit.ns = 0.000001;
|
|
48
|
+
unit.group = ",";
|
|
49
|
+
unit.decimal = ".";
|
|
50
|
+
unit.placeholder = " _";
|
|
51
|
+
var en_default = unit;
|
|
52
|
+
|
|
53
|
+
// node_modules/parse-duration/index.js
|
|
54
|
+
var durationRE = /((?:\d{1,16}(?:\.\d{1,16})?|\.\d{1,16})(?:[eE][-+]?\d{1,4})?)\s?([\p{L}]{0,14})/gu;
|
|
55
|
+
parse.unit = en_default;
|
|
56
|
+
function parse(str = "", format = "ms") {
|
|
57
|
+
let result = null, prevUnits;
|
|
58
|
+
String(str).replace(new RegExp(`(\\d)[${parse.unit.placeholder}${parse.unit.group}](\\d)`, "g"), "$1$2").replace(parse.unit.decimal, ".").replace(durationRE, (_, n, units) => {
|
|
59
|
+
if (!units) {
|
|
60
|
+
if (prevUnits) {
|
|
61
|
+
for (const u in parse.unit)
|
|
62
|
+
if (parse.unit[u] < prevUnits) {
|
|
63
|
+
units = u;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
} else
|
|
67
|
+
units = format;
|
|
68
|
+
} else
|
|
69
|
+
units = units.toLowerCase();
|
|
70
|
+
prevUnits = units = parse.unit[units] || parse.unit[units.replace(/s$/, "")];
|
|
71
|
+
if (units)
|
|
72
|
+
result = (result || 0) + n * units;
|
|
73
|
+
});
|
|
74
|
+
return result && result / (parse.unit[format] || 1) * (str[0] === "-" ? -1 : 1);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// src/files/env.ts
|
|
78
|
+
var env = Bun.env;
|
|
79
|
+
function get_env(name, fallback) {
|
|
80
|
+
return env[name] ?? fallback;
|
|
81
|
+
}
|
|
82
|
+
function int_env(name, fallback) {
|
|
83
|
+
const value = get_env(name);
|
|
84
|
+
if (!value)
|
|
85
|
+
return fallback;
|
|
86
|
+
const val = parseInt(value);
|
|
87
|
+
return isNaN(val) || !isFinite(val) ? fallback : val;
|
|
88
|
+
}
|
|
89
|
+
function duration_env(name, fallback) {
|
|
90
|
+
const value = get_env(name);
|
|
91
|
+
if (!value)
|
|
92
|
+
return fallback;
|
|
93
|
+
const s = parse(value, "s") ?? fallback;
|
|
94
|
+
return s < 0 ? fallback : s;
|
|
95
|
+
}
|
|
96
|
+
var bytes = {
|
|
97
|
+
b: 1,
|
|
98
|
+
kb: 1000,
|
|
99
|
+
mb: 1e6,
|
|
100
|
+
gb: 1e9,
|
|
101
|
+
tb: 1000000000000,
|
|
102
|
+
kib: 1024,
|
|
103
|
+
mib: 1048576,
|
|
104
|
+
gib: 1073741824,
|
|
105
|
+
tib: 1099511627776
|
|
106
|
+
};
|
|
107
|
+
function byte(str) {
|
|
108
|
+
str = str.toLowerCase();
|
|
109
|
+
return str.endsWith("b") ? str : str + "b";
|
|
110
|
+
}
|
|
111
|
+
function bytes_env(name, fallback) {
|
|
112
|
+
const value = get_env(name);
|
|
113
|
+
if (!value)
|
|
114
|
+
return fallback;
|
|
115
|
+
const size_pattern = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*((?:[kmgt]i?)?b?)/gi;
|
|
116
|
+
let match = size_pattern.exec(value);
|
|
117
|
+
if (!match)
|
|
118
|
+
return fallback;
|
|
119
|
+
let val = 0;
|
|
120
|
+
while (match) {
|
|
121
|
+
const [_, num, unit2] = match;
|
|
122
|
+
val += parseFloat(num) * bytes[byte(unit2)];
|
|
123
|
+
match = size_pattern.exec(value);
|
|
124
|
+
}
|
|
125
|
+
return val;
|
|
126
|
+
}
|
|
127
|
+
function bool_env(name, fallback = false) {
|
|
128
|
+
const value = get_env(name);
|
|
129
|
+
if (!value)
|
|
130
|
+
return fallback;
|
|
131
|
+
return value === "true";
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/files/static.ts
|
|
31
135
|
var headersMap = ["last-modified", "etag", "content-length"];
|
|
32
136
|
var methods = new Set(["HEAD", "GET"]);
|
|
137
|
+
var asset_ttl = `public,max-age=${duration_env("CACHE_ASSET_AGE", 14400)}`;
|
|
138
|
+
var immutable_ttl = `public,max-age=${duration_env("CACHE_IMMUTABLE_AGE", 31536000)},immutable`;
|
|
33
139
|
function lookup(pathname) {
|
|
34
140
|
pathname = normalize(pathname);
|
|
35
141
|
let res = assets.get(pathname);
|
|
@@ -84,7 +190,7 @@ async function serve_static({ request }) {
|
|
|
84
190
|
});
|
|
85
191
|
}
|
|
86
192
|
const [_, data] = res;
|
|
87
|
-
const headers = new Headers(data.headers.map((
|
|
193
|
+
const headers = new Headers(data.headers.map((h2, i) => [headersMap[i], h2]));
|
|
88
194
|
const range = parse_range(request.headers);
|
|
89
195
|
const file = data.file;
|
|
90
196
|
headers.set("content-type", file.type);
|
|
@@ -124,7 +230,7 @@ async function serve_static({ request }) {
|
|
|
124
230
|
headers
|
|
125
231
|
});
|
|
126
232
|
}
|
|
127
|
-
headers.set("cache-control", data.immutable ?
|
|
233
|
+
headers.set("cache-control", data.immutable ? immutable_ttl : asset_ttl);
|
|
128
234
|
if (request.headers.has("if-none-match") && request.headers.get("if-none-match") === data.headers[1]) {
|
|
129
235
|
return new Response(null, {
|
|
130
236
|
status: 304,
|
|
@@ -273,110 +379,6 @@ async function create_fetch({
|
|
|
273
379
|
};
|
|
274
380
|
}
|
|
275
381
|
|
|
276
|
-
// node_modules/parse-duration/locale/en.js
|
|
277
|
-
var unit = Object.create(null);
|
|
278
|
-
var m = 60000;
|
|
279
|
-
var h = m * 60;
|
|
280
|
-
var d = h * 24;
|
|
281
|
-
var y = d * 365.25;
|
|
282
|
-
unit.year = unit.yr = unit.y = y;
|
|
283
|
-
unit.month = unit.mo = unit.mth = y / 12;
|
|
284
|
-
unit.week = unit.wk = unit.w = d * 7;
|
|
285
|
-
unit.day = unit.d = d;
|
|
286
|
-
unit.hour = unit.hr = unit.h = h;
|
|
287
|
-
unit.minute = unit.min = unit.m = m;
|
|
288
|
-
unit.second = unit.sec = unit.s = 1000;
|
|
289
|
-
unit.millisecond = unit.millisec = unit.ms = 1;
|
|
290
|
-
unit.microsecond = unit.microsec = unit.us = unit.\u{b5}s = 0.001;
|
|
291
|
-
unit.nanosecond = unit.nanosec = unit.ns = 0.000001;
|
|
292
|
-
unit.group = ",";
|
|
293
|
-
unit.decimal = ".";
|
|
294
|
-
unit.placeholder = " _";
|
|
295
|
-
var en_default = unit;
|
|
296
|
-
|
|
297
|
-
// node_modules/parse-duration/index.js
|
|
298
|
-
var durationRE = /((?:\d{1,16}(?:\.\d{1,16})?|\.\d{1,16})(?:[eE][-+]?\d{1,4})?)\s?([\p{L}]{0,14})/gu;
|
|
299
|
-
parse.unit = en_default;
|
|
300
|
-
function parse(str = "", format = "ms") {
|
|
301
|
-
let result = null, prevUnits;
|
|
302
|
-
String(str).replace(new RegExp(`(\\d)[${parse.unit.placeholder}${parse.unit.group}](\\d)`, "g"), "$1$2").replace(parse.unit.decimal, ".").replace(durationRE, (_, n, units) => {
|
|
303
|
-
if (!units) {
|
|
304
|
-
if (prevUnits) {
|
|
305
|
-
for (const u in parse.unit)
|
|
306
|
-
if (parse.unit[u] < prevUnits) {
|
|
307
|
-
units = u;
|
|
308
|
-
break;
|
|
309
|
-
}
|
|
310
|
-
} else
|
|
311
|
-
units = format;
|
|
312
|
-
} else
|
|
313
|
-
units = units.toLowerCase();
|
|
314
|
-
prevUnits = units = parse.unit[units] || parse.unit[units.replace(/s$/, "")];
|
|
315
|
-
if (units)
|
|
316
|
-
result = (result || 0) + n * units;
|
|
317
|
-
});
|
|
318
|
-
return result && result / (parse.unit[format] || 1) * (str[0] === "-" ? -1 : 1);
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
// src/files/env.ts
|
|
322
|
-
var env = Bun.env;
|
|
323
|
-
function http_env(name, fallback) {
|
|
324
|
-
return env[`HTTP_${name}`] ?? fallback;
|
|
325
|
-
}
|
|
326
|
-
function ws_env(name, fallback) {
|
|
327
|
-
return env[`WS_${name}`] ?? fallback;
|
|
328
|
-
}
|
|
329
|
-
function int_env(fn, name, fallback) {
|
|
330
|
-
const value = fn(name);
|
|
331
|
-
if (!value)
|
|
332
|
-
return fallback;
|
|
333
|
-
const val = parseInt(value);
|
|
334
|
-
return isNaN(val) ? fallback : val;
|
|
335
|
-
}
|
|
336
|
-
function duration_env(fn, name, fallback) {
|
|
337
|
-
const value = fn(name);
|
|
338
|
-
if (!value)
|
|
339
|
-
return fallback;
|
|
340
|
-
return parse(value, "s") ?? fallback;
|
|
341
|
-
}
|
|
342
|
-
var bytes = {
|
|
343
|
-
b: 1,
|
|
344
|
-
kb: 1000,
|
|
345
|
-
mb: 1e6,
|
|
346
|
-
gb: 1e9,
|
|
347
|
-
tb: 1000000000000,
|
|
348
|
-
kib: 1024,
|
|
349
|
-
mib: 1048576,
|
|
350
|
-
gib: 1073741824,
|
|
351
|
-
tib: 1099511627776
|
|
352
|
-
};
|
|
353
|
-
function byte(str) {
|
|
354
|
-
str = str.toLowerCase();
|
|
355
|
-
return str.endsWith("b") ? str : str + "b";
|
|
356
|
-
}
|
|
357
|
-
function bytes_env(fn, name, fallback) {
|
|
358
|
-
const value = fn(name);
|
|
359
|
-
if (!value)
|
|
360
|
-
return fallback;
|
|
361
|
-
const size_pattern = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*((?:[kmgt]i?)?b?)/gi;
|
|
362
|
-
let match = size_pattern.exec(value);
|
|
363
|
-
if (!match)
|
|
364
|
-
return fallback;
|
|
365
|
-
let val = 0;
|
|
366
|
-
while (match) {
|
|
367
|
-
const [_, num, unit2] = match;
|
|
368
|
-
val += parseFloat(num) * bytes[byte(unit2)];
|
|
369
|
-
match = size_pattern.exec(value);
|
|
370
|
-
}
|
|
371
|
-
return val;
|
|
372
|
-
}
|
|
373
|
-
function bool_env(fn, name, fallback = false) {
|
|
374
|
-
const value = fn(name);
|
|
375
|
-
if (!value)
|
|
376
|
-
return fallback;
|
|
377
|
-
return value === "true";
|
|
378
|
-
}
|
|
379
|
-
|
|
380
382
|
// src/files/index.ts
|
|
381
383
|
import { resolve } from "path";
|
|
382
384
|
Bun.plugin({
|
|
@@ -390,28 +392,28 @@ Bun.plugin({
|
|
|
390
392
|
}
|
|
391
393
|
});
|
|
392
394
|
async function serve() {
|
|
393
|
-
const socket =
|
|
395
|
+
const socket = get_env("HTTP_SOCKET");
|
|
394
396
|
const serverOptions = socket ? {
|
|
395
397
|
unix: socket
|
|
396
398
|
} : {
|
|
397
|
-
hostname:
|
|
398
|
-
port:
|
|
399
|
+
hostname: get_env("HTTP_HOST", "0.0.0.0"),
|
|
400
|
+
port: get_env("HTTP_PORT", "3000")
|
|
399
401
|
};
|
|
400
402
|
const server2 = Bun.serve({
|
|
401
403
|
...serverOptions,
|
|
402
|
-
idleTimeout: duration_env(
|
|
403
|
-
maxRequestBodySize: bytes_env(
|
|
404
|
+
idleTimeout: duration_env("HTTP_IDLE_TIMEOUT", 30),
|
|
405
|
+
maxRequestBodySize: bytes_env("HTTP_MAX_BODY", 128 * 1024 * 1024),
|
|
404
406
|
fetch: await create_fetch({
|
|
405
|
-
overrideOrigin:
|
|
406
|
-
hostHeader:
|
|
407
|
-
protocolHeader:
|
|
408
|
-
ipHeader:
|
|
409
|
-
xffDepth: int_env(
|
|
407
|
+
overrideOrigin: get_env("HTTP_OVERRIDE_ORIGIN"),
|
|
408
|
+
hostHeader: get_env("HTTP_HOST_HEADER"),
|
|
409
|
+
protocolHeader: get_env("HTTP_PROTOCOL_HEADER"),
|
|
410
|
+
ipHeader: get_env("HTTP_IP_HEADER"),
|
|
411
|
+
xffDepth: int_env("HTTP_XFF_DEPTH", 1)
|
|
410
412
|
}),
|
|
411
413
|
websocket: {
|
|
412
|
-
idleTimeout: duration_env(
|
|
413
|
-
maxPayloadLength: bytes_env(
|
|
414
|
-
sendPings: !bool_env(
|
|
414
|
+
idleTimeout: duration_env("WS_IDLE_TIMEOUT", 120),
|
|
415
|
+
maxPayloadLength: bytes_env("WS_MAX_PAYLOAD", 16 * 1024 * 1024),
|
|
416
|
+
sendPings: !bool_env("WS_NO_PING"),
|
|
415
417
|
message(ws, message) {
|
|
416
418
|
return ws.data.message(ws, message);
|
|
417
419
|
},
|
package/dist/index.js
CHANGED
|
@@ -419,6 +419,9 @@ function get_imports_n(imports, declared, import_path) {
|
|
|
419
419
|
}
|
|
420
420
|
return n;
|
|
421
421
|
}
|
|
422
|
+
function normalize_path(path) {
|
|
423
|
+
return path.replace(/\\/g, "/");
|
|
424
|
+
}
|
|
422
425
|
async function build_assets_js(base_path, client_files, prerendered_pages, immutable_prefix, ignores) {
|
|
423
426
|
const imports = [];
|
|
424
427
|
const records = [];
|
|
@@ -426,7 +429,8 @@ async function build_assets_js(base_path, client_files, prerendered_pages, immut
|
|
|
426
429
|
for await (const path of client_files) {
|
|
427
430
|
if (ignores.some((ignore) => ignore.match(path)))
|
|
428
431
|
continue;
|
|
429
|
-
|
|
432
|
+
const normalized = normalize_path(path);
|
|
433
|
+
records.push(await build_asset_record(declared, imports, normalized.replace(/^(?:\.?\/)?/, "/"), join(base_path, "client", normalized), "./" + join("client", normalized), immutable_prefix));
|
|
430
434
|
}
|
|
431
435
|
for await (const [pathname, target] of prerendered_pages) {
|
|
432
436
|
records.push(await build_asset_record(declared, imports, pathname, join(base_path, "prerendered", target.file), "./" + join("prerendered", target.file), immutable_prefix));
|
package/package.json
CHANGED