better-call 1.0.20 → 1.0.22

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/node.cjs CHANGED
@@ -1,204 +1,137 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ const require_chunk = require('./chunk-CUT6urMc.cjs');
2
+ let set_cookie_parser = require("set-cookie-parser");
3
+ set_cookie_parser = require_chunk.__toESM(set_cookie_parser);
29
4
 
30
- // src/adapters/node/index.ts
31
- var node_exports = {};
32
- __export(node_exports, {
33
- getRequest: () => getRequest,
34
- setResponse: () => setResponse,
35
- toNodeHandler: () => toNodeHandler
36
- });
37
- module.exports = __toCommonJS(node_exports);
38
-
39
- // src/adapters/node/request.ts
40
- var set_cookie_parser = __toESM(require("set-cookie-parser"), 1);
5
+ //#region src/adapters/node/request.ts
41
6
  function get_raw_body(req, body_size_limit) {
42
- const h = req.headers;
43
- if (!h["content-type"]) return null;
44
- const content_length = Number(h["content-length"]);
45
- if (req.httpVersionMajor === 1 && isNaN(content_length) && h["transfer-encoding"] == null || content_length === 0) {
46
- return null;
47
- }
48
- let length = content_length;
49
- if (body_size_limit) {
50
- if (!length) {
51
- length = body_size_limit;
52
- } else if (length > body_size_limit) {
53
- throw Error(
54
- `Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`
55
- );
56
- }
57
- }
58
- if (req.destroyed) {
59
- const readable = new ReadableStream();
60
- readable.cancel();
61
- return readable;
62
- }
63
- let size = 0;
64
- let cancelled = false;
65
- return new ReadableStream({
66
- start(controller) {
67
- req.on("error", (error) => {
68
- cancelled = true;
69
- controller.error(error);
70
- });
71
- req.on("end", () => {
72
- if (cancelled) return;
73
- controller.close();
74
- });
75
- req.on("data", (chunk) => {
76
- if (cancelled) return;
77
- size += chunk.length;
78
- if (size > length) {
79
- cancelled = true;
80
- controller.error(
81
- new Error(
82
- `request body size exceeded ${content_length ? "'content-length'" : "BODY_SIZE_LIMIT"} of ${length}`
83
- )
84
- );
85
- return;
86
- }
87
- controller.enqueue(chunk);
88
- if (controller.desiredSize === null || controller.desiredSize <= 0) {
89
- req.pause();
90
- }
91
- });
92
- },
93
- pull() {
94
- req.resume();
95
- },
96
- cancel(reason) {
97
- cancelled = true;
98
- req.destroy(reason);
99
- }
100
- });
7
+ const h = req.headers;
8
+ if (!h["content-type"]) return null;
9
+ const content_length = Number(h["content-length"]);
10
+ if (req.httpVersionMajor === 1 && isNaN(content_length) && h["transfer-encoding"] == null || content_length === 0) return null;
11
+ let length = content_length;
12
+ if (body_size_limit) {
13
+ if (!length) length = body_size_limit;
14
+ else if (length > body_size_limit) throw Error(`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`);
15
+ }
16
+ if (req.destroyed) {
17
+ const readable = new ReadableStream();
18
+ readable.cancel();
19
+ return readable;
20
+ }
21
+ let size = 0;
22
+ let cancelled = false;
23
+ return new ReadableStream({
24
+ start(controller) {
25
+ req.on("error", (error) => {
26
+ cancelled = true;
27
+ controller.error(error);
28
+ });
29
+ req.on("end", () => {
30
+ if (cancelled) return;
31
+ controller.close();
32
+ });
33
+ req.on("data", (chunk) => {
34
+ if (cancelled) return;
35
+ size += chunk.length;
36
+ if (size > length) {
37
+ cancelled = true;
38
+ controller.error(/* @__PURE__ */ new Error(`request body size exceeded ${content_length ? "'content-length'" : "BODY_SIZE_LIMIT"} of ${length}`));
39
+ return;
40
+ }
41
+ controller.enqueue(chunk);
42
+ if (controller.desiredSize === null || controller.desiredSize <= 0) req.pause();
43
+ });
44
+ },
45
+ pull() {
46
+ req.resume();
47
+ },
48
+ cancel(reason) {
49
+ cancelled = true;
50
+ req.destroy(reason);
51
+ }
52
+ });
101
53
  }
102
- function getRequest({
103
- request,
104
- base,
105
- bodySizeLimit
106
- }) {
107
- const baseUrl = request?.baseUrl;
108
- const fullPath = baseUrl ? baseUrl + request.url : request.url;
109
- const maybeConsumedReq = request;
110
- let body = void 0;
111
- const method = request.method;
112
- if (method !== "GET" && method !== "HEAD") {
113
- if (maybeConsumedReq.body !== void 0) {
114
- const bodyContent = typeof maybeConsumedReq.body === "string" ? maybeConsumedReq.body : JSON.stringify(maybeConsumedReq.body);
115
- body = new ReadableStream({
116
- start(controller) {
117
- controller.enqueue(new TextEncoder().encode(bodyContent));
118
- controller.close();
119
- }
120
- });
121
- } else {
122
- body = get_raw_body(request, bodySizeLimit);
123
- }
124
- }
125
- return new Request(base + fullPath, {
126
- // @ts-expect-error
127
- duplex: "half",
128
- method: request.method,
129
- body,
130
- headers: request.headers
131
- });
54
+ function getRequest({ request, base, bodySizeLimit }) {
55
+ const baseUrl = request?.baseUrl;
56
+ const fullPath = baseUrl ? baseUrl + request.url : request.url;
57
+ const maybeConsumedReq = request;
58
+ let body = void 0;
59
+ const method = request.method;
60
+ if (method !== "GET" && method !== "HEAD") if (maybeConsumedReq.body !== void 0) {
61
+ const bodyContent = typeof maybeConsumedReq.body === "string" ? maybeConsumedReq.body : JSON.stringify(maybeConsumedReq.body);
62
+ body = new ReadableStream({ start(controller) {
63
+ controller.enqueue(new TextEncoder().encode(bodyContent));
64
+ controller.close();
65
+ } });
66
+ } else body = get_raw_body(request, bodySizeLimit);
67
+ return new Request(base + fullPath, {
68
+ duplex: "half",
69
+ method: request.method,
70
+ body,
71
+ headers: request.headers
72
+ });
132
73
  }
133
74
  async function setResponse(res, response) {
134
- for (const [key, value] of response.headers) {
135
- try {
136
- res.setHeader(
137
- key,
138
- key === "set-cookie" ? set_cookie_parser.splitCookiesString(response.headers.get(key)) : value
139
- );
140
- } catch (error) {
141
- res.getHeaderNames().forEach((name) => res.removeHeader(name));
142
- res.writeHead(500).end(String(error));
143
- return;
144
- }
145
- }
146
- res.writeHead(response.status);
147
- if (!response.body) {
148
- res.end();
149
- return;
150
- }
151
- if (response.body.locked) {
152
- res.end(
153
- "Fatal error: Response body is locked. This can happen when the response was already read (for example through 'response.json()' or 'response.text()')."
154
- );
155
- return;
156
- }
157
- const reader = response.body.getReader();
158
- if (res.destroyed) {
159
- reader.cancel();
160
- return;
161
- }
162
- const cancel = (error) => {
163
- res.off("close", cancel);
164
- res.off("error", cancel);
165
- reader.cancel(error).catch(() => {
166
- });
167
- if (error) res.destroy(error);
168
- };
169
- res.on("close", cancel);
170
- res.on("error", cancel);
171
- next();
172
- async function next() {
173
- try {
174
- for (; ; ) {
175
- const { done, value } = await reader.read();
176
- if (done) break;
177
- if (!res.write(value)) {
178
- res.once("drain", next);
179
- return;
180
- }
181
- }
182
- res.end();
183
- } catch (error) {
184
- cancel(error instanceof Error ? error : new Error(String(error)));
185
- }
186
- }
75
+ for (const [key, value] of response.headers) try {
76
+ res.setHeader(key, key === "set-cookie" ? set_cookie_parser.splitCookiesString(response.headers.get(key)) : value);
77
+ } catch (error) {
78
+ res.getHeaderNames().forEach((name) => res.removeHeader(name));
79
+ res.writeHead(500).end(String(error));
80
+ return;
81
+ }
82
+ res.writeHead(response.status);
83
+ if (!response.body) {
84
+ res.end();
85
+ return;
86
+ }
87
+ if (response.body.locked) {
88
+ res.end("Fatal error: Response body is locked. This can happen when the response was already read (for example through 'response.json()' or 'response.text()').");
89
+ return;
90
+ }
91
+ const reader = response.body.getReader();
92
+ if (res.destroyed) {
93
+ reader.cancel();
94
+ return;
95
+ }
96
+ const cancel = (error) => {
97
+ res.off("close", cancel);
98
+ res.off("error", cancel);
99
+ reader.cancel(error).catch(() => {});
100
+ if (error) res.destroy(error);
101
+ };
102
+ res.on("close", cancel);
103
+ res.on("error", cancel);
104
+ next();
105
+ async function next() {
106
+ try {
107
+ for (;;) {
108
+ const { done, value } = await reader.read();
109
+ if (done) break;
110
+ if (!res.write(value)) {
111
+ res.once("drain", next);
112
+ return;
113
+ }
114
+ }
115
+ res.end();
116
+ } catch (error) {
117
+ cancel(error instanceof Error ? error : new Error(String(error)));
118
+ }
119
+ }
187
120
  }
188
121
 
189
- // src/adapters/node/index.ts
122
+ //#endregion
123
+ //#region src/adapters/node/index.ts
190
124
  function toNodeHandler(handler) {
191
- return async (req, res) => {
192
- const protocol = req.headers["x-forwarded-proto"] || (req.socket.encrypted ? "https" : "http");
193
- const base = `${protocol}://${req.headers[":authority"] || req.headers.host}`;
194
- const response = await handler(getRequest({ base, request: req }));
195
- return setResponse(res, response);
196
- };
125
+ return async (req, res) => {
126
+ return setResponse(res, await handler(getRequest({
127
+ base: `${req.headers["x-forwarded-proto"] || (req.socket.encrypted ? "https" : "http")}://${req.headers[":authority"] || req.headers.host}`,
128
+ request: req
129
+ })));
130
+ };
197
131
  }
198
- // Annotate the CommonJS export names for ESM import in node:
199
- 0 && (module.exports = {
200
- getRequest,
201
- setResponse,
202
- toNodeHandler
203
- });
132
+
133
+ //#endregion
134
+ exports.getRequest = getRequest;
135
+ exports.setResponse = setResponse;
136
+ exports.toNodeHandler = toNodeHandler;
204
137
  //# sourceMappingURL=node.cjs.map
package/dist/node.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/adapters/node/index.ts","../src/adapters/node/request.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from \"node:http\";\n\nimport { getRequest, setResponse } from \"./request\";\nimport type { Router } from \"../../router.js\";\n\nexport function toNodeHandler(handler: Router[\"handler\"]) {\n\treturn async (req: IncomingMessage, res: ServerResponse) => {\n\t\tconst protocol =\n\t\t\treq.headers[\"x-forwarded-proto\"] || ((req.socket as any).encrypted ? \"https\" : \"http\");\n\t\tconst base = `${protocol}://${req.headers[\":authority\"] || req.headers.host}`;\n\t\tconst response = await handler(getRequest({ base, request: req }));\n\t\treturn setResponse(res, response);\n\t};\n}\n\nexport { getRequest, setResponse };\n","import type { IncomingMessage, ServerResponse } from \"node:http\";\nimport * as set_cookie_parser from \"set-cookie-parser\";\n\nfunction get_raw_body(req: IncomingMessage, body_size_limit?: number) {\n\tconst h = req.headers;\n\n\tif (!h[\"content-type\"]) return null;\n\n\tconst content_length = Number(h[\"content-length\"]);\n\n\t// check if no request body\n\tif (\n\t\t(req.httpVersionMajor === 1 && isNaN(content_length) && h[\"transfer-encoding\"] == null) ||\n\t\tcontent_length === 0\n\t) {\n\t\treturn null;\n\t}\n\n\tlet length = content_length;\n\n\tif (body_size_limit) {\n\t\tif (!length) {\n\t\t\tlength = body_size_limit;\n\t\t} else if (length > body_size_limit) {\n\t\t\tthrow Error(\n\t\t\t\t`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tif (req.destroyed) {\n\t\tconst readable = new ReadableStream();\n\t\treadable.cancel();\n\t\treturn readable;\n\t}\n\n\tlet size = 0;\n\tlet cancelled = false;\n\n\treturn new ReadableStream({\n\t\tstart(controller) {\n\t\t\treq.on(\"error\", (error) => {\n\t\t\t\tcancelled = true;\n\t\t\t\tcontroller.error(error);\n\t\t\t});\n\n\t\t\treq.on(\"end\", () => {\n\t\t\t\tif (cancelled) return;\n\t\t\t\tcontroller.close();\n\t\t\t});\n\n\t\t\treq.on(\"data\", (chunk) => {\n\t\t\t\tif (cancelled) return;\n\n\t\t\t\tsize += chunk.length;\n\n\t\t\t\tif (size > length) {\n\t\t\t\t\tcancelled = true;\n\n\t\t\t\t\tcontroller.error(\n\t\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\t`request body size exceeded ${\n\t\t\t\t\t\t\t\tcontent_length ? \"'content-length'\" : \"BODY_SIZE_LIMIT\"\n\t\t\t\t\t\t\t} of ${length}`,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tcontroller.enqueue(chunk);\n\n\t\t\t\tif (controller.desiredSize === null || controller.desiredSize <= 0) {\n\t\t\t\t\treq.pause();\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\n\t\tpull() {\n\t\t\treq.resume();\n\t\t},\n\n\t\tcancel(reason) {\n\t\t\tcancelled = true;\n\t\t\treq.destroy(reason);\n\t\t},\n\t});\n}\n\nexport function getRequest({\n\trequest,\n\tbase,\n\tbodySizeLimit,\n}: {\n\tbase: string;\n\tbodySizeLimit?: number;\n\trequest: IncomingMessage;\n}) {\n\t// In Express subrouters, `request.url` is relative to the mount path (e.g., '/auth/xxx'),\n\t// and `request.baseUrl` holds the mount path (e.g., '/api').\n\t// Build the full path as baseUrl + url when available to preserve the full route.\n\tconst baseUrl = (request as any)?.baseUrl as string | undefined;\n\tconst fullPath = baseUrl ? baseUrl + request.url : request.url;\n\n\t// Check if body has already been parsed by Express middleware\n\tconst maybeConsumedReq = request as any;\n\tlet body = undefined;\n\n\tconst method = request.method;\n\t// Request with GET/HEAD method cannot have body.\n\tif (method !== \"GET\" && method !== \"HEAD\") {\n\t\t// If body was already parsed by Express body-parser middleware\n\t\tif (maybeConsumedReq.body !== undefined) {\n\t\t\t// Convert parsed body back to a ReadableStream\n\t\t\tconst bodyContent =\n\t\t\t\ttypeof maybeConsumedReq.body === \"string\"\n\t\t\t\t\t? maybeConsumedReq.body\n\t\t\t\t\t: JSON.stringify(maybeConsumedReq.body);\n\n\t\t\tbody = new ReadableStream({\n\t\t\t\tstart(controller) {\n\t\t\t\t\tcontroller.enqueue(new TextEncoder().encode(bodyContent));\n\t\t\t\t\tcontroller.close();\n\t\t\t\t},\n\t\t\t});\n\t\t} else {\n\t\t\t// Otherwise, get the raw body stream\n\t\t\tbody = get_raw_body(request, bodySizeLimit);\n\t\t}\n\t}\n\n\treturn new Request(base + fullPath, {\n\t\t// @ts-expect-error\n\t\tduplex: \"half\",\n\t\tmethod: request.method,\n\t\tbody,\n\t\theaders: request.headers as Record<string, string>,\n\t});\n}\n\nexport async function setResponse(res: ServerResponse, response: Response) {\n\tfor (const [key, value] of response.headers as any) {\n\t\ttry {\n\t\t\tres.setHeader(\n\t\t\t\tkey,\n\t\t\t\tkey === \"set-cookie\"\n\t\t\t\t\t? set_cookie_parser.splitCookiesString(response.headers.get(key) as string)\n\t\t\t\t\t: value,\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tres.getHeaderNames().forEach((name) => res.removeHeader(name));\n\t\t\tres.writeHead(500).end(String(error));\n\t\t\treturn;\n\t\t}\n\t}\n\n\tres.writeHead(response.status);\n\n\tif (!response.body) {\n\t\tres.end();\n\t\treturn;\n\t}\n\n\tif (response.body.locked) {\n\t\tres.end(\n\t\t\t\"Fatal error: Response body is locked. \" +\n\t\t\t\t\"This can happen when the response was already read (for example through 'response.json()' or 'response.text()').\",\n\t\t);\n\t\treturn;\n\t}\n\n\tconst reader = response.body.getReader();\n\n\tif (res.destroyed) {\n\t\treader.cancel();\n\t\treturn;\n\t}\n\n\tconst cancel = (error?: Error) => {\n\t\tres.off(\"close\", cancel);\n\t\tres.off(\"error\", cancel);\n\n\t\t// If the reader has already been interrupted with an error earlier,\n\t\t// then it will appear here, it is useless, but it needs to be catch.\n\t\treader.cancel(error).catch(() => {});\n\t\tif (error) res.destroy(error);\n\t};\n\n\tres.on(\"close\", cancel);\n\tres.on(\"error\", cancel);\n\n\tnext();\n\tasync function next() {\n\t\ttry {\n\t\t\tfor (;;) {\n\t\t\t\tconst { done, value } = await reader.read();\n\n\t\t\t\tif (done) break;\n\n\t\t\t\tif (!res.write(value)) {\n\t\t\t\t\tres.once(\"drain\", next);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\tres.end();\n\t\t} catch (error) {\n\t\t\tcancel(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,wBAAmC;AAEnC,SAAS,aAAa,KAAsB,iBAA0B;AACrE,QAAM,IAAI,IAAI;AAEd,MAAI,CAAC,EAAE,cAAc,EAAG,QAAO;AAE/B,QAAM,iBAAiB,OAAO,EAAE,gBAAgB,CAAC;AAGjD,MACE,IAAI,qBAAqB,KAAK,MAAM,cAAc,KAAK,EAAE,mBAAmB,KAAK,QAClF,mBAAmB,GAClB;AACD,WAAO;AAAA,EACR;AAEA,MAAI,SAAS;AAEb,MAAI,iBAAiB;AACpB,QAAI,CAAC,QAAQ;AACZ,eAAS;AAAA,IACV,WAAW,SAAS,iBAAiB;AACpC,YAAM;AAAA,QACL,8BAA8B,MAAM,2BAA2B,eAAe;AAAA,MAC/E;AAAA,IACD;AAAA,EACD;AAEA,MAAI,IAAI,WAAW;AAClB,UAAM,WAAW,IAAI,eAAe;AACpC,aAAS,OAAO;AAChB,WAAO;AAAA,EACR;AAEA,MAAI,OAAO;AACX,MAAI,YAAY;AAEhB,SAAO,IAAI,eAAe;AAAA,IACzB,MAAM,YAAY;AACjB,UAAI,GAAG,SAAS,CAAC,UAAU;AAC1B,oBAAY;AACZ,mBAAW,MAAM,KAAK;AAAA,MACvB,CAAC;AAED,UAAI,GAAG,OAAO,MAAM;AACnB,YAAI,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,aAAO,iBAAiB,QAAQ,QAAQ,IAAI,MAAM,OAAO,KAAK,CAAC,CAAC;AAAA,IACjE;AAAA,EACD;AACD;;;AD3MO,SAAS,cAAc,SAA4B;AACzD,SAAO,OAAO,KAAsB,QAAwB;AAC3D,UAAM,WACL,IAAI,QAAQ,mBAAmB,MAAO,IAAI,OAAe,YAAY,UAAU;AAChF,UAAM,OAAO,GAAG,QAAQ,MAAM,IAAI,QAAQ,YAAY,KAAK,IAAI,QAAQ,IAAI;AAC3E,UAAM,WAAW,MAAM,QAAQ,WAAW,EAAE,MAAM,SAAS,IAAI,CAAC,CAAC;AACjE,WAAO,YAAY,KAAK,QAAQ;AAAA,EACjC;AACD;","names":[]}
1
+ {"version":3,"file":"node.cjs","names":[],"sources":["../src/adapters/node/request.ts","../src/adapters/node/index.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from \"node:http\";\nimport * as set_cookie_parser from \"set-cookie-parser\";\n\nfunction get_raw_body(req: IncomingMessage, body_size_limit?: number) {\n\tconst h = req.headers;\n\n\tif (!h[\"content-type\"]) return null;\n\n\tconst content_length = Number(h[\"content-length\"]);\n\n\t// check if no request body\n\tif (\n\t\t(req.httpVersionMajor === 1 && isNaN(content_length) && h[\"transfer-encoding\"] == null) ||\n\t\tcontent_length === 0\n\t) {\n\t\treturn null;\n\t}\n\n\tlet length = content_length;\n\n\tif (body_size_limit) {\n\t\tif (!length) {\n\t\t\tlength = body_size_limit;\n\t\t} else if (length > body_size_limit) {\n\t\t\tthrow Error(\n\t\t\t\t`Received content-length of ${length}, but only accept up to ${body_size_limit} bytes.`,\n\t\t\t);\n\t\t}\n\t}\n\n\tif (req.destroyed) {\n\t\tconst readable = new ReadableStream();\n\t\treadable.cancel();\n\t\treturn readable;\n\t}\n\n\tlet size = 0;\n\tlet cancelled = false;\n\n\treturn new ReadableStream({\n\t\tstart(controller) {\n\t\t\treq.on(\"error\", (error) => {\n\t\t\t\tcancelled = true;\n\t\t\t\tcontroller.error(error);\n\t\t\t});\n\n\t\t\treq.on(\"end\", () => {\n\t\t\t\tif (cancelled) return;\n\t\t\t\tcontroller.close();\n\t\t\t});\n\n\t\t\treq.on(\"data\", (chunk) => {\n\t\t\t\tif (cancelled) return;\n\n\t\t\t\tsize += chunk.length;\n\n\t\t\t\tif (size > length) {\n\t\t\t\t\tcancelled = true;\n\n\t\t\t\t\tcontroller.error(\n\t\t\t\t\t\tnew Error(\n\t\t\t\t\t\t\t`request body size exceeded ${\n\t\t\t\t\t\t\t\tcontent_length ? \"'content-length'\" : \"BODY_SIZE_LIMIT\"\n\t\t\t\t\t\t\t} of ${length}`,\n\t\t\t\t\t\t),\n\t\t\t\t\t);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\n\t\t\t\tcontroller.enqueue(chunk);\n\n\t\t\t\tif (controller.desiredSize === null || controller.desiredSize <= 0) {\n\t\t\t\t\treq.pause();\n\t\t\t\t}\n\t\t\t});\n\t\t},\n\n\t\tpull() {\n\t\t\treq.resume();\n\t\t},\n\n\t\tcancel(reason) {\n\t\t\tcancelled = true;\n\t\t\treq.destroy(reason);\n\t\t},\n\t});\n}\n\nexport function getRequest({\n\trequest,\n\tbase,\n\tbodySizeLimit,\n}: {\n\tbase: string;\n\tbodySizeLimit?: number;\n\trequest: IncomingMessage;\n}) {\n\t// In Express subrouters, `request.url` is relative to the mount path (e.g., '/auth/xxx'),\n\t// and `request.baseUrl` holds the mount path (e.g., '/api').\n\t// Build the full path as baseUrl + url when available to preserve the full route.\n\tconst baseUrl = (request as any)?.baseUrl as string | undefined;\n\tconst fullPath = baseUrl ? baseUrl + request.url : request.url;\n\n\t// Check if body has already been parsed by Express middleware\n\tconst maybeConsumedReq = request as any;\n\tlet body = undefined;\n\n\tconst method = request.method;\n\t// Request with GET/HEAD method cannot have body.\n\tif (method !== \"GET\" && method !== \"HEAD\") {\n\t\t// If body was already parsed by Express body-parser middleware\n\t\tif (maybeConsumedReq.body !== undefined) {\n\t\t\t// Convert parsed body back to a ReadableStream\n\t\t\tconst bodyContent =\n\t\t\t\ttypeof maybeConsumedReq.body === \"string\"\n\t\t\t\t\t? maybeConsumedReq.body\n\t\t\t\t\t: JSON.stringify(maybeConsumedReq.body);\n\n\t\t\tbody = new ReadableStream({\n\t\t\t\tstart(controller) {\n\t\t\t\t\tcontroller.enqueue(new TextEncoder().encode(bodyContent));\n\t\t\t\t\tcontroller.close();\n\t\t\t\t},\n\t\t\t});\n\t\t} else {\n\t\t\t// Otherwise, get the raw body stream\n\t\t\tbody = get_raw_body(request, bodySizeLimit);\n\t\t}\n\t}\n\n\treturn new Request(base + fullPath, {\n\t\t// @ts-expect-error\n\t\tduplex: \"half\",\n\t\tmethod: request.method,\n\t\tbody,\n\t\theaders: request.headers as Record<string, string>,\n\t});\n}\n\nexport async function setResponse(res: ServerResponse, response: Response) {\n\tfor (const [key, value] of response.headers as any) {\n\t\ttry {\n\t\t\tres.setHeader(\n\t\t\t\tkey,\n\t\t\t\tkey === \"set-cookie\"\n\t\t\t\t\t? set_cookie_parser.splitCookiesString(response.headers.get(key) as string)\n\t\t\t\t\t: value,\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tres.getHeaderNames().forEach((name) => res.removeHeader(name));\n\t\t\tres.writeHead(500).end(String(error));\n\t\t\treturn;\n\t\t}\n\t}\n\n\tres.writeHead(response.status);\n\n\tif (!response.body) {\n\t\tres.end();\n\t\treturn;\n\t}\n\n\tif (response.body.locked) {\n\t\tres.end(\n\t\t\t\"Fatal error: Response body is locked. \" +\n\t\t\t\t\"This can happen when the response was already read (for example through 'response.json()' or 'response.text()').\",\n\t\t);\n\t\treturn;\n\t}\n\n\tconst reader = response.body.getReader();\n\n\tif (res.destroyed) {\n\t\treader.cancel();\n\t\treturn;\n\t}\n\n\tconst cancel = (error?: Error) => {\n\t\tres.off(\"close\", cancel);\n\t\tres.off(\"error\", cancel);\n\n\t\t// If the reader has already been interrupted with an error earlier,\n\t\t// then it will appear here, it is useless, but it needs to be catch.\n\t\treader.cancel(error).catch(() => {});\n\t\tif (error) res.destroy(error);\n\t};\n\n\tres.on(\"close\", cancel);\n\tres.on(\"error\", cancel);\n\n\tnext();\n\tasync function next() {\n\t\ttry {\n\t\t\tfor (;;) {\n\t\t\t\tconst { done, value } = await reader.read();\n\n\t\t\t\tif (done) break;\n\n\t\t\t\tif (!res.write(value)) {\n\t\t\t\t\tres.once(\"drain\", next);\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\t\t\tres.end();\n\t\t} catch (error) {\n\t\t\tcancel(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t}\n}\n","import type { IncomingMessage, ServerResponse } from \"node:http\";\n\nimport { getRequest, setResponse } from \"./request\";\nimport type { Router } from \"../../router.js\";\n\nexport function toNodeHandler(handler: Router[\"handler\"]) {\n\treturn async (req: IncomingMessage, res: ServerResponse) => {\n\t\tconst protocol =\n\t\t\treq.headers[\"x-forwarded-proto\"] || ((req.socket as any).encrypted ? \"https\" : \"http\");\n\t\tconst base = `${protocol}://${req.headers[\":authority\"] || req.headers.host}`;\n\t\tconst response = await handler(getRequest({ base, request: req }));\n\t\treturn setResponse(res, response);\n\t};\n}\n\nexport { getRequest, setResponse };\n"],"mappings":";;;;;AAGA,SAAS,aAAa,KAAsB,iBAA0B;CACrE,MAAM,IAAI,IAAI;AAEd,KAAI,CAAC,EAAE,gBAAiB,QAAO;CAE/B,MAAM,iBAAiB,OAAO,EAAE,kBAAkB;AAGlD,KACE,IAAI,qBAAqB,KAAK,MAAM,eAAe,IAAI,EAAE,wBAAwB,QAClF,mBAAmB,EAEnB,QAAO;CAGR,IAAI,SAAS;AAEb,KAAI,iBACH;MAAI,CAAC,OACJ,UAAS;WACC,SAAS,gBACnB,OAAM,MACL,8BAA8B,OAAO,0BAA0B,gBAAgB,SAC/E;;AAIH,KAAI,IAAI,WAAW;EAClB,MAAM,WAAW,IAAI,gBAAgB;AACrC,WAAS,QAAQ;AACjB,SAAO;;CAGR,IAAI,OAAO;CACX,IAAI,YAAY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I,QAAQ;GACrB,SAAS;GAAK,CAAC,CAAC,CACjC"}
package/dist/node.d.cts CHANGED
@@ -1,13 +1,20 @@
1
- import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { j as Router } from './router-BDQGRZdc.cjs';
1
+ import { Router } from "./router-IP1pntSS.cjs";
2
+ import { IncomingMessage, ServerResponse } from "node:http";
3
3
 
4
- declare function getRequest({ request, base, bodySizeLimit, }: {
5
- base: string;
6
- bodySizeLimit?: number;
7
- request: IncomingMessage;
4
+ //#region src/adapters/node/request.d.ts
5
+ declare function getRequest({
6
+ request,
7
+ base,
8
+ bodySizeLimit
9
+ }: {
10
+ base: string;
11
+ bodySizeLimit?: number;
12
+ request: IncomingMessage;
8
13
  }): Request;
9
14
  declare function setResponse(res: ServerResponse, response: Response): Promise<void>;
10
-
15
+ //#endregion
16
+ //#region src/adapters/node/index.d.ts
11
17
  declare function toNodeHandler(handler: Router["handler"]): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
12
-
18
+ //#endregion
13
19
  export { getRequest, setResponse, toNodeHandler };
20
+ //# sourceMappingURL=node.d.cts.map
package/dist/node.d.ts CHANGED
@@ -1,13 +1,20 @@
1
- import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { j as Router } from './router-BDQGRZdc.js';
1
+ import { Router } from "./router-BPTd8L-P.js";
2
+ import { IncomingMessage, ServerResponse } from "node:http";
3
3
 
4
- declare function getRequest({ request, base, bodySizeLimit, }: {
5
- base: string;
6
- bodySizeLimit?: number;
7
- request: IncomingMessage;
4
+ //#region src/adapters/node/request.d.ts
5
+ declare function getRequest({
6
+ request,
7
+ base,
8
+ bodySizeLimit
9
+ }: {
10
+ base: string;
11
+ bodySizeLimit?: number;
12
+ request: IncomingMessage;
8
13
  }): Request;
9
14
  declare function setResponse(res: ServerResponse, response: Response): Promise<void>;
10
-
15
+ //#endregion
16
+ //#region src/adapters/node/index.d.ts
11
17
  declare function toNodeHandler(handler: Router["handler"]): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
12
-
18
+ //#endregion
13
19
  export { getRequest, setResponse, toNodeHandler };
20
+ //# sourceMappingURL=node.d.ts.map