@rhythmjs/router 0.0.6 → 0.0.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/dist/adapters/node.js +23 -5
- package/package.json +2 -2
package/dist/adapters/node.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHttpContext, toResponse } from "./context.js";
|
|
2
2
|
import { Readable } from "node:stream";
|
|
3
|
+
import { Buffer } from "node:buffer";
|
|
3
4
|
//#region src/adapters/node.ts
|
|
4
5
|
const DEFAULT_BODY_LIMIT = 1048576;
|
|
5
6
|
var RhythmBodyTooLargeError = class extends Error {
|
|
@@ -46,11 +47,12 @@ function readRawBody(req, limit) {
|
|
|
46
47
|
async function toWebRequest(req, bodyLimit) {
|
|
47
48
|
const method = req.method ?? "GET";
|
|
48
49
|
const url = `http://${req.headers.host ?? "localhost"}${req.url ?? "/"}`;
|
|
49
|
-
const headers =
|
|
50
|
-
for (const
|
|
50
|
+
const headers = [];
|
|
51
|
+
for (const key in req.headers) {
|
|
52
|
+
const value = req.headers[key];
|
|
51
53
|
if (value === void 0) continue;
|
|
52
|
-
if (Array.isArray(value)) for (const entry of value) headers.
|
|
53
|
-
else headers.
|
|
54
|
+
if (Array.isArray(value)) for (const entry of value) headers.push([key, entry]);
|
|
55
|
+
else headers.push([key, value]);
|
|
54
56
|
}
|
|
55
57
|
if (!(method !== "GET" && method !== "HEAD")) return new Request(url, {
|
|
56
58
|
method,
|
|
@@ -92,6 +94,21 @@ async function writeWebResponse(response, res) {
|
|
|
92
94
|
nodeStream.pipe(res);
|
|
93
95
|
});
|
|
94
96
|
}
|
|
97
|
+
function writeDirectResponse(response, res) {
|
|
98
|
+
const { body, status } = response;
|
|
99
|
+
const headers = toNodeHeaders(response.headers);
|
|
100
|
+
let payload;
|
|
101
|
+
if (typeof body === "string") payload = body;
|
|
102
|
+
else if (body instanceof Uint8Array) payload = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
103
|
+
else if (body instanceof ArrayBuffer) payload = Buffer.from(body);
|
|
104
|
+
if (!("content-length" in headers) && status !== 204 && status !== 304) headers["content-length"] = payload === void 0 ? "0" : String(typeof payload === "string" ? Buffer.byteLength(payload) : payload.byteLength);
|
|
105
|
+
if (response.statusText !== void 0) res.writeHead(status, response.statusText, headers);
|
|
106
|
+
else res.writeHead(status, headers);
|
|
107
|
+
res.end(payload);
|
|
108
|
+
}
|
|
109
|
+
function hasDirectBody(body) {
|
|
110
|
+
return body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ArrayBuffer;
|
|
111
|
+
}
|
|
95
112
|
function toNodeHandler(app, options = {}) {
|
|
96
113
|
const run = app.callback();
|
|
97
114
|
const bodyLimit = options.bodyLimit ?? DEFAULT_BODY_LIMIT;
|
|
@@ -99,7 +116,8 @@ function toNodeHandler(app, options = {}) {
|
|
|
99
116
|
try {
|
|
100
117
|
const request = await toWebRequest(req, bodyLimit);
|
|
101
118
|
const ctx = await run(createHttpContext(request));
|
|
102
|
-
|
|
119
|
+
if (hasDirectBody(ctx.response.body)) writeDirectResponse(ctx.response, res);
|
|
120
|
+
else await writeWebResponse(toResponse(ctx.response), res);
|
|
103
121
|
} catch (err) {
|
|
104
122
|
if (!(err instanceof RhythmBodyTooLargeError)) console.error(err);
|
|
105
123
|
if (!res.headersSent) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rhythmjs/router",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"description": "Radix-tree HTTP router for the Rhythm middleware kernel, with Bun, Node, and Deno adapters.",
|
|
5
5
|
"homepage": "https://rhythm.js.org/router",
|
|
6
6
|
"license": "ISC",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@rhythmjs/rhythm": "0.0.
|
|
52
|
+
"@rhythmjs/rhythm": "0.0.7"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@types/node": "^26.6.3",
|