@tinkered/bun 0.34.5 → 0.34.7
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 +8 -0
- package/dist/index.d.mts +6 -1
- package/dist/index.mjs +74 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -73,6 +73,14 @@ export default defineMain(
|
|
|
73
73
|
`Response` wins.
|
|
74
74
|
- `/api/*`, `/rpc` and `/rpc/*` go to the
|
|
75
75
|
handlers; `paths` adds more.
|
|
76
|
+
- Each handler reads a body up to its own
|
|
77
|
+
`maxBody`, 1 MiB when unset. Past it,
|
|
78
|
+
`serve` answers 413.
|
|
79
|
+
- A handler's `request.signal` aborts when
|
|
80
|
+
the client leaves, body or not.
|
|
81
|
+
- `server.requestIP(request)` and
|
|
82
|
+
`server.timeout(request, s)` work in a
|
|
83
|
+
handler, body or not.
|
|
76
84
|
|
|
77
85
|
A handler is a capsule whose start returns
|
|
78
86
|
Bun's own shape, `{ fetch, websocket? }`.
|
package/dist/index.d.mts
CHANGED
|
@@ -122,7 +122,12 @@ type Handler = {
|
|
|
122
122
|
fetch: (request: Request, scope: Scope.Handle, server: Server<never>) => Response | null | undefined | Promise<Response | null | undefined>;
|
|
123
123
|
/** Bun's socket handler, typed by the handler that owns it. */
|
|
124
124
|
websocket?: WebSocketHandler<never> | object;
|
|
125
|
-
/**
|
|
125
|
+
/**
|
|
126
|
+
* The largest request body this handler takes; 1 MiB when unset (Bun's default). `serve`
|
|
127
|
+
* counts the body as this handler reads it: a read past it (by Content-Length or by count)
|
|
128
|
+
* rejects with a RangeError, and `serve` answers 413 whatever the handler answered. The
|
|
129
|
+
* largest of every handler's sets Bun's `maxRequestBodySize`, the server-wide backstop.
|
|
130
|
+
*/
|
|
126
131
|
maxBody?: number;
|
|
127
132
|
};
|
|
128
133
|
/**
|
package/dist/index.mjs
CHANGED
|
@@ -369,6 +369,75 @@ function createWs() {
|
|
|
369
369
|
const ws = createWs();
|
|
370
370
|
//#endregion
|
|
371
371
|
//#region src/serve.ts
|
|
372
|
+
/** A handler's `maxBody` when it names none: Bun's own default. */
|
|
373
|
+
const defaultMaxBody = 1048576;
|
|
374
|
+
/**
|
|
375
|
+
* The 413 closes the connection (RFC 9110 15.5.14): the client may still be sending the rest of
|
|
376
|
+
* a body nobody reads, and Bun's graceful `stop` would wait on it.
|
|
377
|
+
*/
|
|
378
|
+
const tooLarge = {
|
|
379
|
+
status: 413,
|
|
380
|
+
headers: { connection: "close" }
|
|
381
|
+
};
|
|
382
|
+
/** Bun's calls that take a request: they know only Bun's own, not a copy. */
|
|
383
|
+
const byRequest = /* @__PURE__ */ new Set([
|
|
384
|
+
"requestIP",
|
|
385
|
+
"timeout",
|
|
386
|
+
"upgrade"
|
|
387
|
+
]);
|
|
388
|
+
/**
|
|
389
|
+
* A request's body, counted against the cap of the handler that reads it (Hono's `bodyLimit`).
|
|
390
|
+
* `ask` sets `cap` before it asks each handler. A Content-Length or a count past the cap errors
|
|
391
|
+
* the read, and `over` tells `serve` to answer 413. It is its own stream's transformer, and the
|
|
392
|
+
* proxy handler of the `server` the handlers get: that maps the copy back to Bun's own request.
|
|
393
|
+
*/
|
|
394
|
+
var CappedBody = class {
|
|
395
|
+
cap = defaultMaxBody;
|
|
396
|
+
seen = 0;
|
|
397
|
+
over = false;
|
|
398
|
+
/** The Content-Length; 0 when the body is chunked and has none. */
|
|
399
|
+
length;
|
|
400
|
+
/** Bun's own request, which Bun's `requestIP`, `timeout` and `upgrade` know. */
|
|
401
|
+
original;
|
|
402
|
+
/** The request each handler gets: the original's url, headers and signal, this body. */
|
|
403
|
+
request;
|
|
404
|
+
/** The server each handler gets: Bun's, with `request` mapped back to `original`. */
|
|
405
|
+
server;
|
|
406
|
+
constructor(request, body, bun) {
|
|
407
|
+
this.length = Number(request.headers.get("content-length"));
|
|
408
|
+
this.original = request;
|
|
409
|
+
this.request = new Request(request, {
|
|
410
|
+
body: body.pipeThrough(new TransformStream(this)),
|
|
411
|
+
duplex: "half"
|
|
412
|
+
});
|
|
413
|
+
this.server = new Proxy(bun, this);
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* One handler's reply, its read held to its own cap. A throw from a read past the cap is not
|
|
417
|
+
* the handler's own: the caller answers 413 for it.
|
|
418
|
+
*/
|
|
419
|
+
async ask(handler, scope) {
|
|
420
|
+
this.cap = handler.maxBody ?? defaultMaxBody;
|
|
421
|
+
try {
|
|
422
|
+
return await handler.fetch(this.request, scope, this.server);
|
|
423
|
+
} catch (error) {
|
|
424
|
+
if (!this.over) throw error;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/** A server member, bound to Bun's server; a call that takes a request gets Bun's own. */
|
|
428
|
+
get(bun, key) {
|
|
429
|
+
const value = Object(bun)[key];
|
|
430
|
+
if (typeof value !== "function") return value;
|
|
431
|
+
if (!byRequest.has(key)) return value.bind(bun);
|
|
432
|
+
return (request, ...rest) => value.call(bun, request === this.request ? this.original : request, ...rest);
|
|
433
|
+
}
|
|
434
|
+
transform(chunk, out) {
|
|
435
|
+
this.seen += chunk.byteLength;
|
|
436
|
+
if (this.seen <= this.cap && !(this.length > this.cap)) return out.enqueue(chunk);
|
|
437
|
+
this.over = true;
|
|
438
|
+
out.error(/* @__PURE__ */ new RangeError("The body is over its handler's maxBody."));
|
|
439
|
+
}
|
|
440
|
+
};
|
|
372
441
|
const port = tag({ label: "serve.port" });
|
|
373
442
|
const pages = tag({
|
|
374
443
|
label: "serve.pages",
|
|
@@ -453,9 +522,12 @@ const serve = capsule({
|
|
|
453
522
|
key: "websocket"
|
|
454
523
|
});
|
|
455
524
|
const found = socket ? [socket, ...rest] : rest;
|
|
525
|
+
/** A body over the cap of the handler that read it is a 413, whatever that handler did. */
|
|
456
526
|
const answer = async (request, bun) => {
|
|
527
|
+
const body = request.body && new CappedBody(request, request.body, bun);
|
|
457
528
|
for (const handler of found) {
|
|
458
|
-
const reply = await handler.fetch(request, scope, bun);
|
|
529
|
+
const reply = await (body ? body.ask(handler, scope) : handler.fetch(request, scope, bun));
|
|
530
|
+
if (body?.over) return new Response(null, tooLarge);
|
|
459
531
|
if (reply !== void 0) return reply ?? void 0;
|
|
460
532
|
}
|
|
461
533
|
return new Response(null, { status: 404 });
|
|
@@ -463,7 +535,7 @@ const serve = capsule({
|
|
|
463
535
|
const server = Bun.serve({
|
|
464
536
|
port: config.port,
|
|
465
537
|
idleTimeout: 60,
|
|
466
|
-
maxRequestBodySize: Math.max(
|
|
538
|
+
maxRequestBodySize: Math.max(defaultMaxBody, ...found.map((handler) => handler.maxBody ?? 0)),
|
|
467
539
|
websocket: socket?.websocket,
|
|
468
540
|
fetch: () => new Response(null, { status: 404 }),
|
|
469
541
|
development: config.development && {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinkered/bun",
|
|
3
|
-
"version": "0.34.
|
|
3
|
+
"version": "0.34.7",
|
|
4
4
|
"description": "Tinker on Bun: the host on Bun.serve, rpc over one WebSocket, and Bun's own databases (Bun.SQL, bun:sqlite).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@tinkered/core": "^0.34.
|
|
27
|
+
"@tinkered/core": "^0.34.7"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@hughescr/stryker-bun-runner": "1.6.0",
|