@torpor/adapter-node 0.1.1 → 1.0.0
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 +15 -1
- package/dist/{index.d.ts → index.d.mts} +1 -2
- package/dist/index.d.mts.map +1 -0
- package/dist/{index.js → index.mjs} +18 -22
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -13
- package/CHANGELOG.md +0 -226
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/src/adapter.ts +0 -24
- package/src/constants.ts +0 -1
- package/src/createFetchHandler.ts +0 -29
- package/src/createNodeServer.ts +0 -11
- package/src/flattenHeaders.ts +0 -44
- package/src/index.ts +0 -3
- package/src/nodeMessageToRequest.ts +0 -44
- package/src/responseToNodeResponse.ts +0 -50
- package/tsconfig.json +0 -9
- package/tsdown.config.ts +0 -13
package/README.md
CHANGED
|
@@ -6,4 +6,18 @@ A @torpor/build adapter for running on Node.
|
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Install the adapter using `npm` (or your preferred package manager):
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @torpor/adapter-node
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Then use the adapter in your `site.config`:
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
import { node } from "@torpor/adapter-node";
|
|
19
|
+
import { Site } from "@torpor/build";
|
|
20
|
+
|
|
21
|
+
const site: Site = new Site();
|
|
22
|
+
site.adapter = node;
|
|
23
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/adapter.ts"],"mappings":";;cAIA,UAmBuB"}
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import { createServer } from "node:http";
|
|
2
2
|
import { Readable } from "node:stream";
|
|
3
3
|
import { isArrayBufferView } from "node:util/types";
|
|
4
|
-
|
|
5
4
|
//#region src/constants.ts
|
|
6
5
|
const INTERNAL_BODY = Symbol("internal_body");
|
|
7
|
-
|
|
8
6
|
//#endregion
|
|
9
7
|
//#region src/flattenHeaders.ts
|
|
8
|
+
/**
|
|
9
|
+
* Flattens raw node request headers into a `HeadersInit`. Returning plain
|
|
10
|
+
* tuples (rather than a `Headers` instance) means the Request constructor
|
|
11
|
+
* walks them once instead of copying from a Headers into another Headers.
|
|
12
|
+
*/
|
|
10
13
|
function flattenHeaders(headers) {
|
|
11
|
-
const flatHeaders =
|
|
14
|
+
const flatHeaders = [];
|
|
12
15
|
for (const [key, value] of Object.entries(headers)) if (value === void 0 || value === null) continue;
|
|
13
16
|
else if (Array.isArray(value)) {
|
|
14
|
-
for (const v of value) if (v !== void 0 && v !== null)
|
|
15
|
-
} else flatHeaders.
|
|
17
|
+
for (const v of value) if (v !== void 0 && v !== null) flatHeaders.push([key, String(v)]);
|
|
18
|
+
} else flatHeaders.push([key, String(value)]);
|
|
16
19
|
return flatHeaders;
|
|
17
20
|
}
|
|
18
|
-
|
|
19
21
|
//#endregion
|
|
20
22
|
//#region src/nodeMessageToRequest.ts
|
|
21
23
|
async function nodeMessageToRequest(req) {
|
|
@@ -34,45 +36,41 @@ async function nodeMessageToRequest(req) {
|
|
|
34
36
|
duplex: "half"
|
|
35
37
|
});
|
|
36
38
|
}
|
|
37
|
-
|
|
38
39
|
//#endregion
|
|
39
40
|
//#region src/responseToNodeResponse.ts
|
|
40
41
|
async function responseToNodeResponse(res, outgoing) {
|
|
41
|
-
outgoing.statusCode = res.status;
|
|
42
|
-
outgoing.statusMessage = res.statusText;
|
|
43
42
|
res.headers.forEach((value, key) => {
|
|
44
43
|
outgoing.setHeader(key, value);
|
|
45
44
|
});
|
|
46
45
|
const body = res[INTERNAL_BODY];
|
|
47
46
|
if (body === null || body === void 0) {
|
|
48
|
-
outgoing.writeHead(res.status
|
|
47
|
+
outgoing.writeHead(res.status);
|
|
49
48
|
outgoing.end();
|
|
50
49
|
} else if (typeof body === "string" || body instanceof Uint8Array) {
|
|
51
|
-
outgoing.writeHead(res.status
|
|
50
|
+
outgoing.writeHead(res.status);
|
|
52
51
|
outgoing.end(body);
|
|
53
52
|
} else if (body instanceof Blob) {
|
|
54
|
-
outgoing.writeHead(res.status
|
|
53
|
+
outgoing.writeHead(res.status);
|
|
55
54
|
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
56
55
|
} else if (body instanceof ArrayBuffer) {
|
|
57
|
-
outgoing.writeHead(res.status
|
|
56
|
+
outgoing.writeHead(res.status);
|
|
58
57
|
outgoing.end(new Uint8Array(body));
|
|
59
58
|
} else if (isArrayBufferView(body)) {
|
|
60
|
-
outgoing.writeHead(res.status
|
|
59
|
+
outgoing.writeHead(res.status);
|
|
61
60
|
outgoing.end(new Uint8Array(body.buffer));
|
|
62
61
|
} else if (body instanceof FormData) {
|
|
63
62
|
outgoing.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
|
|
64
|
-
outgoing.writeHead(res.status
|
|
63
|
+
outgoing.writeHead(res.status);
|
|
65
64
|
outgoing.end();
|
|
66
65
|
} else {
|
|
67
|
-
outgoing.writeHead(res.status
|
|
66
|
+
outgoing.writeHead(res.status);
|
|
68
67
|
outgoing.end();
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
|
-
|
|
72
70
|
//#endregion
|
|
73
71
|
//#region src/createFetchHandler.ts
|
|
74
72
|
const GlobalResponse = Response;
|
|
75
|
-
globalThis.Response = class Response
|
|
73
|
+
globalThis.Response = class Response extends GlobalResponse {
|
|
76
74
|
[INTERNAL_BODY] = null;
|
|
77
75
|
constructor(body, init) {
|
|
78
76
|
super(body, init);
|
|
@@ -84,13 +82,11 @@ function createFetchHandler(handler) {
|
|
|
84
82
|
return await responseToNodeResponse(await handler(await nodeMessageToRequest(incoming)), outgoing);
|
|
85
83
|
};
|
|
86
84
|
}
|
|
87
|
-
|
|
88
85
|
//#endregion
|
|
89
86
|
//#region src/createNodeServer.ts
|
|
90
87
|
function createNodeServer(handler) {
|
|
91
88
|
return createServer(createFetchHandler(handler));
|
|
92
89
|
}
|
|
93
|
-
|
|
94
90
|
//#endregion
|
|
95
91
|
//#region src/adapter.ts
|
|
96
92
|
var adapter_default = { serve: (server) => {
|
|
@@ -104,7 +100,7 @@ var adapter_default = { serve: (server) => {
|
|
|
104
100
|
console.log(`Listening on ${url}\n`);
|
|
105
101
|
});
|
|
106
102
|
} };
|
|
107
|
-
|
|
108
103
|
//#endregion
|
|
109
104
|
export { adapter_default as node };
|
|
110
|
-
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/constants.ts","../src/flattenHeaders.ts","../src/nodeMessageToRequest.ts","../src/responseToNodeResponse.ts","../src/createFetchHandler.ts","../src/createNodeServer.ts","../src/adapter.ts"],"sourcesContent":["export const INTERNAL_BODY: unique symbol = Symbol(\"internal_body\");\n","import { IncomingHttpHeaders } from \"node:http\";\nimport { OutgoingHttpHeaders } from \"node:http2\";\n\n/**\n * Flattens raw node request headers into a `HeadersInit`. Returning plain\n * tuples (rather than a `Headers` instance) means the Request constructor\n * walks them once instead of copying from a Headers into another Headers.\n */\nexport default function flattenHeaders(\n\theaders: IncomingHttpHeaders | OutgoingHttpHeaders,\n): HeadersInit {\n\tconst flatHeaders: [string, string][] = [];\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (value === undefined || value === null) {\n\t\t\tcontinue;\n\t\t} else if (Array.isArray(value)) {\n\t\t\tfor (const v of value) {\n\t\t\t\tif (v !== undefined && v !== null) {\n\t\t\t\t\tflatHeaders.push([key, String(v)]);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tflatHeaders.push([key, String(value)]);\n\t\t}\n\t}\n\treturn flatHeaders;\n}\n","import { type IncomingMessage } from \"node:http\";\nimport { Http2ServerRequest } from \"node:http2\";\nimport { Readable } from \"node:stream\";\nimport flattenHeaders from \"./flattenHeaders\";\n\n// From https://stackoverflow.com/a/78849544\n// and https://gist.github.com/marvinhagemeister/cc236ec97235ce0305ae9d48a24a607d\n\nexport default async function nodeMessageToRequest(\n\treq: IncomingMessage | Http2ServerRequest,\n): Promise<Request> {\n\t// Build the full url\n\tlet path = req.url;\n\tif (path === undefined) {\n\t\tthrow new Error(`Empty request URL`);\n\t}\n\tif (!path.startsWith(\"/\")) {\n\t\tpath = \"/\" + path;\n\t}\n\tconst { host } = req.headers;\n\tif (host == undefined) {\n\t\tthrow new Error(`Missing \"Host\" header`);\n\t}\n\tconst protocol = \"encrypted\" in req.socket && req.socket.encrypted ? \"https\" : \"http\";\n\tconst url = `${protocol}://${host}${path}`;\n\n\t// Flatten the headers\n\tconst headers = flattenHeaders(req.headers);\n\n\t// Convert body to Buffer if applicable\n\tconst body =\n\t\treq.method !== \"GET\" && req.method !== \"HEAD\"\n\t\t\t? // @ts-ignore\n\t\t\t\t(Readable.toWeb(req) as ReadableStream) // await readableToBuffer(req)\n\t\t\t: null;\n\n\treturn new Request(url, {\n\t\tmethod: req.method,\n\t\theaders: headers,\n\t\t// eslint-disable-next-line no-invalid-fetch-options\n\t\tbody: body,\n\t\t// @ts-ignore this is needed for using a Readable for some reason\n\t\tduplex: \"half\",\n\t});\n}\n","import { ServerResponse } from \"node:http\";\nimport { Http2ServerResponse } from \"node:http2\";\nimport { isArrayBufferView } from \"node:util/types\";\nimport { INTERNAL_BODY } from \"./constants\";\n\nexport default async function responseToNodeResponse(\n\tres: Response,\n\toutgoing: ServerResponse | Http2ServerResponse,\n): Promise<void> {\n\tres.headers.forEach((value, key) => {\n\t\toutgoing.setHeader(key, value);\n\t});\n\n\tconst body = (res as any)[INTERNAL_BODY];\n\n\t// `writeHead(status)` uses node's standard reason phrase for the code\n\t// (the same one undici's `statusText` carries)\n\tif (body === null || body === undefined) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status);\n\t\toutgoing.end();\n\t} else if (typeof body === \"string\" || body instanceof Uint8Array) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status);\n\t\toutgoing.end(body);\n\t} else if (body instanceof Blob) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status);\n\t\toutgoing.end(new Uint8Array(await body.arrayBuffer()));\n\t} else if (body instanceof ArrayBuffer) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status);\n\t\toutgoing.end(new Uint8Array(body));\n\t} else if (isArrayBufferView(body)) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status);\n\t\toutgoing.end(new Uint8Array(body.buffer));\n\t} else if (body instanceof FormData) {\n\t\toutgoing.setHeader(\"Content-Type\", \"application/x-www-form-urlencoded;charset=UTF-8\");\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status);\n\t\toutgoing.end();\n\t} else {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status);\n\t\toutgoing.end();\n\t}\n}\n","import { type IncomingMessage, ServerResponse } from \"node:http\";\nimport { Http2ServerRequest, Http2ServerResponse } from \"node:http2\";\nimport { INTERNAL_BODY } from \"./constants\";\nimport nodeMessageToRequest from \"./nodeMessageToRequest\";\nimport responseToNodeResponse from \"./responseToNodeResponse\";\n\n// From https://gist.github.com/marvinhagemeister/cc236ec97235ce0305ae9d48a24a607d\n// Referenced from https://marvinh.dev/blog/modern-way-to-write-javascript-servers/\n\nconst GlobalResponse = Response;\nglobalThis.Response = class Response extends GlobalResponse {\n\t[INTERNAL_BODY]: BodyInit | null | undefined = null;\n\n\tconstructor(body?: BodyInit | null, init?: ResponseInit) {\n\t\tsuper(body, init);\n\t\tthis[INTERNAL_BODY] = body;\n\t}\n};\n\nexport function createFetchHandler(handler: (req: Request) => Response | Promise<Response>) {\n\treturn async (\n\t\tincoming: IncomingMessage | Http2ServerRequest,\n\t\toutgoing: ServerResponse | Http2ServerResponse,\n\t): Promise<void> => {\n\t\tconst req = await nodeMessageToRequest(incoming);\n\t\tconst res = await handler(req);\n\t\treturn await responseToNodeResponse(res, outgoing);\n\t};\n}\n","import { IncomingMessage, Server, ServerResponse } from \"http\";\nimport { createServer } from \"node:http\";\nimport { createFetchHandler } from \"./createFetchHandler\";\n\nexport default function createNodeServer(\n\thandler: (req: Request) => Response | Promise<Response>,\n): Server<typeof IncomingMessage, typeof ServerResponse> {\n\t// Create a server, polyfill it so that it can handle standard fetch methods,\n\t// and pass our app fetch method to the handler\n\treturn createServer(createFetchHandler(handler));\n}\n","import { type Adapter } from \"@torpor/build\";\nimport { Server } from \"@torpor/build/server\";\nimport createNodeServer from \"./createNodeServer\";\n\nexport default {\n\tserve: (server: Server) => {\n\t\tprocess.env.PROTOCOL ??= \"http:\";\n\t\tprocess.env.HOST ??= \"localhost\";\n\t\tprocess.env.PORT ??= \"7059\";\n\n\t\t// Put adapter-specific functionality in the `adapter` property of\n\t\t// globalThis for now\n\t\t// @ts-ignore\n\t\tglobalThis.adapter = { env: process.env };\n\n\t\t// Create the Node server and start listening\n\t\tconst url = `${process.env.PROTOCOL}//${process.env.HOST}:${process.env.PORT}`;\n\t\tconsole.log(`\\nConnecting to ${url}`);\n\t\tconst devServer = createNodeServer(server.fetch);\n\t\tdevServer.listen(parseInt(process.env.PORT), process.env.HOST, () => {\n\t\t\tconsole.log(`Listening on ${url}\\n`);\n\t\t});\n\t},\n} satisfies Adapter as Adapter;\n"],"mappings":";;;;AAAA,MAAa,gBAA+B,OAAO,eAAe;;;;;;;;ACQlE,SAAwB,eACvB,SACc;CACd,MAAM,cAAkC,CAAC;CACzC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,GAChD,IAAI,UAAU,KAAA,KAAa,UAAU,MACpC;MACM,IAAI,MAAM,QAAQ,KAAK,GACxB;OAAA,MAAM,KAAK,OACf,IAAI,MAAM,KAAA,KAAa,MAAM,MAC5B,YAAY,KAAK,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC;CAAA,OAInC,YAAY,KAAK,CAAC,KAAK,OAAO,KAAK,CAAC,CAAC;CAGvC,OAAO;AACR;;;AClBA,eAA8B,qBAC7B,KACmB;CAEnB,IAAI,OAAO,IAAI;CACf,IAAI,SAAS,KAAA,GACZ,MAAM,IAAI,MAAM,mBAAmB;CAEpC,IAAI,CAAC,KAAK,WAAW,GAAG,GACvB,OAAO,MAAM;CAEd,MAAM,EAAE,SAAS,IAAI;CACrB,IAAI,QAAQ,KAAA,GACX,MAAM,IAAI,MAAM,uBAAuB;CAGxC,MAAM,MAAM,GADK,eAAe,IAAI,UAAU,IAAI,OAAO,YAAY,UAAU,OACvD,KAAK,OAAO;CAGpC,MAAM,UAAU,eAAe,IAAI,OAAO;CAG1C,MAAM,OACL,IAAI,WAAW,SAAS,IAAI,WAAW,SAEpC,SAAS,MAAM,GAAG,IAClB;CAEJ,OAAO,IAAI,QAAQ,KAAK;EACvB,QAAQ,IAAI;EACH;EAEH;EAEN,QAAQ;CACT,CAAC;AACF;;;ACvCA,eAA8B,uBAC7B,KACA,UACgB;CAChB,IAAI,QAAQ,SAAS,OAAO,QAAQ;EACnC,SAAS,UAAU,KAAK,KAAK;CAC9B,CAAC;CAED,MAAM,OAAQ,IAAY;CAI1B,IAAI,SAAS,QAAQ,SAAS,KAAA,GAAW;EAExC,SAAS,UAAU,IAAI,MAAM;EAC7B,SAAS,IAAI;CACd,OAAO,IAAI,OAAO,SAAS,YAAY,gBAAgB,YAAY;EAElE,SAAS,UAAU,IAAI,MAAM;EAC7B,SAAS,IAAI,IAAI;CAClB,OAAO,IAAI,gBAAgB,MAAM;EAEhC,SAAS,UAAU,IAAI,MAAM;EAC7B,SAAS,IAAI,IAAI,WAAW,MAAM,KAAK,YAAY,CAAC,CAAC;CACtD,OAAO,IAAI,gBAAgB,aAAa;EAEvC,SAAS,UAAU,IAAI,MAAM;EAC7B,SAAS,IAAI,IAAI,WAAW,IAAI,CAAC;CAClC,OAAO,IAAI,kBAAkB,IAAI,GAAG;EAEnC,SAAS,UAAU,IAAI,MAAM;EAC7B,SAAS,IAAI,IAAI,WAAW,KAAK,MAAM,CAAC;CACzC,OAAO,IAAI,gBAAgB,UAAU;EACpC,SAAS,UAAU,gBAAgB,iDAAiD;EAEpF,SAAS,UAAU,IAAI,MAAM;EAC7B,SAAS,IAAI;CACd,OAAO;EAEN,SAAS,UAAU,IAAI,MAAM;EAC7B,SAAS,IAAI;CACd;AACD;;;ACtCA,MAAM,iBAAiB;AACvB,WAAW,WAAW,MAAM,iBAAiB,eAAe;CAC3D,CAAC,iBAA8C;CAE/C,YAAY,MAAwB,MAAqB;EACxD,MAAM,MAAM,IAAI;EAChB,KAAK,iBAAiB;CACvB;AACD;AAEA,SAAgB,mBAAmB,SAAyD;CAC3F,OAAO,OACN,UACA,aACmB;EAGnB,OAAO,MAAM,uBAAuB,MADlB,QAAQ,MADR,qBAAqB,QAAQ,CAClB,GACY,QAAQ;CAClD;AACD;;;ACxBA,SAAwB,iBACvB,SACwD;CAGxD,OAAO,aAAa,mBAAmB,OAAO,CAAC;AAChD;;;ACNA,IAAA,kBAAe,EACd,QAAQ,WAAmB;CAC1B,QAAQ,IAAI,aAAa;CACzB,QAAQ,IAAI,SAAS;CACrB,QAAQ,IAAI,SAAS;CAKrB,WAAW,UAAU,EAAE,KAAK,QAAQ,IAAI;CAGxC,MAAM,MAAM,GAAG,QAAQ,IAAI,SAAS,IAAI,QAAQ,IAAI,KAAK,GAAG,QAAQ,IAAI;CACxE,QAAQ,IAAI,mBAAmB,KAAK;CAEpC,iBADmC,OAAO,KAClC,CAAC,CAAC,OAAO,SAAS,QAAQ,IAAI,IAAI,GAAG,QAAQ,IAAI,YAAY;EACpE,QAAQ,IAAI,gBAAgB,IAAI,GAAG;CACpC,CAAC;AACF,EACD"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@torpor/adapter-node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "A @torpor/build adapter for running on Node",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
|
-
"types": "./dist/index.d.
|
|
8
|
-
"import": "./dist/index.
|
|
7
|
+
"types": "./dist/index.d.mts",
|
|
8
|
+
"import": "./dist/index.mjs"
|
|
9
9
|
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
10
13
|
"publishConfig": {
|
|
11
14
|
"access": "public"
|
|
12
15
|
},
|
|
@@ -14,19 +17,17 @@
|
|
|
14
17
|
"author": "",
|
|
15
18
|
"license": "ISC",
|
|
16
19
|
"dependencies": {
|
|
17
|
-
"vite": "
|
|
18
|
-
"@torpor/build": "^0.
|
|
20
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.7",
|
|
21
|
+
"@torpor/build": "^1.0.0"
|
|
19
22
|
},
|
|
20
23
|
"devDependencies": {
|
|
21
|
-
"@
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"tsdown": "^0.15.11",
|
|
25
|
-
"typescript": "^5.9.3"
|
|
24
|
+
"@typescript/native-preview": "7.0.0-dev.20260707.2",
|
|
25
|
+
"typescript": "^7.0.2",
|
|
26
|
+
"vite-plus": "0.2.7"
|
|
26
27
|
},
|
|
27
28
|
"scripts": {
|
|
28
|
-
"
|
|
29
|
-
"build
|
|
30
|
-
"
|
|
29
|
+
"check": "tsgo --noEmit && pnpm dlx oxlint --type-aware",
|
|
30
|
+
"build": "tsgo --noEmit && vp pack",
|
|
31
|
+
"build:watch": "vp pack --watch"
|
|
31
32
|
}
|
|
32
33
|
}
|
package/CHANGELOG.md
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
# @torpor/adapter-node
|
|
2
|
-
|
|
3
|
-
## 0.1.1
|
|
4
|
-
|
|
5
|
-
### Patch Changes
|
|
6
|
-
|
|
7
|
-
- Updated dependencies [168dff3]
|
|
8
|
-
- Updated dependencies [051998b]
|
|
9
|
-
- @torpor/build@0.4.0
|
|
10
|
-
|
|
11
|
-
## 0.1.0
|
|
12
|
-
|
|
13
|
-
### Minor Changes
|
|
14
|
-
|
|
15
|
-
- 51dbba7: Chore: drop CJS build and go ESM only
|
|
16
|
-
|
|
17
|
-
### Patch Changes
|
|
18
|
-
|
|
19
|
-
- Updated dependencies [bbdc8d3]
|
|
20
|
-
- Updated dependencies [e1ae2bb]
|
|
21
|
-
- Updated dependencies [51dbba7]
|
|
22
|
-
- Updated dependencies [62b0d2e]
|
|
23
|
-
- @torpor/build@0.3.0
|
|
24
|
-
|
|
25
|
-
## 0.0.30
|
|
26
|
-
|
|
27
|
-
### Patch Changes
|
|
28
|
-
|
|
29
|
-
- Updated dependencies [a4f2573]
|
|
30
|
-
- Updated dependencies [67e6ca6]
|
|
31
|
-
- Updated dependencies [b716d5c]
|
|
32
|
-
- @torpor/build@0.2.0
|
|
33
|
-
|
|
34
|
-
## 0.0.29
|
|
35
|
-
|
|
36
|
-
### Patch Changes
|
|
37
|
-
|
|
38
|
-
- @torpor/build@0.1.31
|
|
39
|
-
|
|
40
|
-
## 0.0.28
|
|
41
|
-
|
|
42
|
-
### Patch Changes
|
|
43
|
-
|
|
44
|
-
- @torpor/build@0.1.30
|
|
45
|
-
|
|
46
|
-
## 0.0.27
|
|
47
|
-
|
|
48
|
-
### Patch Changes
|
|
49
|
-
|
|
50
|
-
- @torpor/build@0.1.29
|
|
51
|
-
|
|
52
|
-
## 0.0.26
|
|
53
|
-
|
|
54
|
-
### Patch Changes
|
|
55
|
-
|
|
56
|
-
- Updated dependencies [fa85b57]
|
|
57
|
-
- @torpor/build@0.1.28
|
|
58
|
-
|
|
59
|
-
## 0.0.25
|
|
60
|
-
|
|
61
|
-
### Patch Changes
|
|
62
|
-
|
|
63
|
-
- Updated dependencies [bed55d5]
|
|
64
|
-
- @torpor/build@0.1.27
|
|
65
|
-
|
|
66
|
-
## 0.0.24
|
|
67
|
-
|
|
68
|
-
### Patch Changes
|
|
69
|
-
|
|
70
|
-
- Updated dependencies [348cd75]
|
|
71
|
-
- @torpor/build@0.1.26
|
|
72
|
-
|
|
73
|
-
## 0.0.23
|
|
74
|
-
|
|
75
|
-
### Patch Changes
|
|
76
|
-
|
|
77
|
-
- Updated dependencies [3fe0d0e]
|
|
78
|
-
- Updated dependencies [f26603f]
|
|
79
|
-
- @torpor/build@0.1.25
|
|
80
|
-
|
|
81
|
-
## 0.0.22
|
|
82
|
-
|
|
83
|
-
### Patch Changes
|
|
84
|
-
|
|
85
|
-
- Updated dependencies [6092a91]
|
|
86
|
-
- @torpor/build@0.1.24
|
|
87
|
-
|
|
88
|
-
## 0.0.21
|
|
89
|
-
|
|
90
|
-
### Patch Changes
|
|
91
|
-
|
|
92
|
-
- Updated dependencies [22f62d4]
|
|
93
|
-
- @torpor/build@0.1.23
|
|
94
|
-
|
|
95
|
-
## 0.0.20
|
|
96
|
-
|
|
97
|
-
### Patch Changes
|
|
98
|
-
|
|
99
|
-
- Updated dependencies [4e0ec90]
|
|
100
|
-
- @torpor/build@0.1.22
|
|
101
|
-
|
|
102
|
-
## 0.0.19
|
|
103
|
-
|
|
104
|
-
### Patch Changes
|
|
105
|
-
|
|
106
|
-
- Updated dependencies [4296742]
|
|
107
|
-
- @torpor/build@0.1.21
|
|
108
|
-
|
|
109
|
-
## 0.0.18
|
|
110
|
-
|
|
111
|
-
### Patch Changes
|
|
112
|
-
|
|
113
|
-
- Updated dependencies [a61f30f]
|
|
114
|
-
- @torpor/build@0.1.20
|
|
115
|
-
|
|
116
|
-
## 0.0.17
|
|
117
|
-
|
|
118
|
-
### Patch Changes
|
|
119
|
-
|
|
120
|
-
- Updated dependencies [170c886]
|
|
121
|
-
- @torpor/build@0.1.19
|
|
122
|
-
|
|
123
|
-
## 0.0.16
|
|
124
|
-
|
|
125
|
-
### Patch Changes
|
|
126
|
-
|
|
127
|
-
- @torpor/build@0.1.18
|
|
128
|
-
|
|
129
|
-
## 0.0.15
|
|
130
|
-
|
|
131
|
-
### Patch Changes
|
|
132
|
-
|
|
133
|
-
- Updated dependencies [7373f27]
|
|
134
|
-
- Updated dependencies [679a448]
|
|
135
|
-
- Updated dependencies [e5b761b]
|
|
136
|
-
- @torpor/build@0.1.17
|
|
137
|
-
|
|
138
|
-
## 0.0.14
|
|
139
|
-
|
|
140
|
-
### Patch Changes
|
|
141
|
-
|
|
142
|
-
- Updated dependencies [9332635]
|
|
143
|
-
- @torpor/build@0.1.16
|
|
144
|
-
|
|
145
|
-
## 0.0.13
|
|
146
|
-
|
|
147
|
-
### Patch Changes
|
|
148
|
-
|
|
149
|
-
- @torpor/build@0.1.15
|
|
150
|
-
|
|
151
|
-
## 0.0.12
|
|
152
|
-
|
|
153
|
-
### Patch Changes
|
|
154
|
-
|
|
155
|
-
- Updated dependencies [e395c9e]
|
|
156
|
-
- @torpor/build@0.1.14
|
|
157
|
-
|
|
158
|
-
## 0.0.11
|
|
159
|
-
|
|
160
|
-
### Patch Changes
|
|
161
|
-
|
|
162
|
-
- 7162232: Fix: set globalThis.adapter in all adapters
|
|
163
|
-
- Updated dependencies [02eac8e]
|
|
164
|
-
- @torpor/build@0.1.13
|
|
165
|
-
|
|
166
|
-
## 0.0.10
|
|
167
|
-
|
|
168
|
-
### Patch Changes
|
|
169
|
-
|
|
170
|
-
- 3ed8a23: Chore: specify exports in packages
|
|
171
|
-
|
|
172
|
-
## 0.0.9
|
|
173
|
-
|
|
174
|
-
### Patch Changes
|
|
175
|
-
|
|
176
|
-
- Updated dependencies [8de094b]
|
|
177
|
-
- @torpor/build@0.1.12
|
|
178
|
-
|
|
179
|
-
## 0.0.8
|
|
180
|
-
|
|
181
|
-
### Patch Changes
|
|
182
|
-
|
|
183
|
-
- Updated dependencies [858538b]
|
|
184
|
-
- @torpor/build@0.1.11
|
|
185
|
-
|
|
186
|
-
## 0.0.7
|
|
187
|
-
|
|
188
|
-
### Patch Changes
|
|
189
|
-
|
|
190
|
-
- @torpor/build@0.1.10
|
|
191
|
-
|
|
192
|
-
## 0.0.6
|
|
193
|
-
|
|
194
|
-
### Patch Changes
|
|
195
|
-
|
|
196
|
-
- Updated dependencies [ff685cb]
|
|
197
|
-
- @torpor/build@0.1.9
|
|
198
|
-
|
|
199
|
-
## 0.0.5
|
|
200
|
-
|
|
201
|
-
### Patch Changes
|
|
202
|
-
|
|
203
|
-
- fe7444e: Refactor: rearrange build exports
|
|
204
|
-
- Updated dependencies [fe7444e]
|
|
205
|
-
- @torpor/build@0.1.8
|
|
206
|
-
|
|
207
|
-
## 0.0.4
|
|
208
|
-
|
|
209
|
-
### Patch Changes
|
|
210
|
-
|
|
211
|
-
- @torpor/build@0.1.7
|
|
212
|
-
|
|
213
|
-
## 0.0.3
|
|
214
|
-
|
|
215
|
-
### Patch Changes
|
|
216
|
-
|
|
217
|
-
- 47c5277: Chore: update dependencies
|
|
218
|
-
- Updated dependencies [47c5277]
|
|
219
|
-
- @torpor/build@0.1.6
|
|
220
|
-
|
|
221
|
-
## 0.0.2
|
|
222
|
-
|
|
223
|
-
### Patch Changes
|
|
224
|
-
|
|
225
|
-
- Updated dependencies [5c9d90f]
|
|
226
|
-
- @torpor/build@0.1.5
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/adapter.ts"],"sourcesContent":[],"mappings":";;;cAIA,UAmBuB"}
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["INTERNAL_BODY: unique symbol","v","Response"],"sources":["../src/constants.ts","../src/flattenHeaders.ts","../src/nodeMessageToRequest.ts","../src/responseToNodeResponse.ts","../src/createFetchHandler.ts","../src/createNodeServer.ts","../src/adapter.ts"],"sourcesContent":["export const INTERNAL_BODY: unique symbol = Symbol(\"internal_body\");\n","import { IncomingHttpHeaders } from \"node:http\";\nimport { OutgoingHttpHeaders } from \"node:http2\";\n\nexport default function flattenHeaders(\n\theaders: IncomingHttpHeaders | OutgoingHttpHeaders,\n): HeadersInit {\n\tconst flatHeaders = new Headers();\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (value === undefined || value === null) {\n\t\t\tcontinue;\n\t\t} else if (Array.isArray(value)) {\n\t\t\tfor (const v of value) {\n\t\t\t\tif (v !== undefined && v !== null) {\n\t\t\t\t\tvalue.forEach((v) => flatHeaders.append(key, v));\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tflatHeaders.set(key, String(value));\n\t\t}\n\t}\n\treturn flatHeaders;\n\n\t/*\n const flatHeaders: [string, string][] = [];\n\n\tfor (const [key, value] of Object.entries(headers)) {\n\t\tif (value === undefined || value === null) {\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (Array.isArray(value)) {\n\t\t\tfor (const v of value) {\n\t\t\t\tif (v != null) {\n\t\t\t\t\tflatHeaders.push([key, String(v)]);\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tflatHeaders.push([key, String(value)]);\n\t\t}\n\t}\n\n\treturn flatHeaders;\n */\n}\n","import { type IncomingMessage } from \"node:http\";\nimport { Http2ServerRequest } from \"node:http2\";\nimport { Readable } from \"node:stream\";\nimport flattenHeaders from \"./flattenHeaders\";\n\n// From https://stackoverflow.com/a/78849544\n// and https://gist.github.com/marvinhagemeister/cc236ec97235ce0305ae9d48a24a607d\n\nexport default async function nodeMessageToRequest(\n\treq: IncomingMessage | Http2ServerRequest,\n): Promise<Request> {\n\t// Build the full url\n\tlet path = req.url;\n\tif (path === undefined) {\n\t\tthrow new Error(`Empty request URL`);\n\t}\n\tif (!path.startsWith(\"/\")) {\n\t\tpath = \"/\" + path;\n\t}\n\tconst { host } = req.headers;\n\tif (host == undefined) {\n\t\tthrow new Error(`Missing \"Host\" header`);\n\t}\n\tconst protocol = \"encrypted\" in req.socket && req.socket.encrypted ? \"https\" : \"http\";\n\tconst url = `${protocol}://${host}${path}`;\n\n\t// Flatten the headers\n\tconst headers = flattenHeaders(req.headers);\n\n\t// Convert body to Buffer if applicable\n\tconst body =\n\t\treq.method !== \"GET\" && req.method !== \"HEAD\"\n\t\t\t? (Readable.toWeb(req) as ReadableStream) // await readableToBuffer(req)\n\t\t\t: null;\n\n\treturn new Request(url, {\n\t\tmethod: req.method,\n\t\theaders: headers,\n\t\t// eslint-disable-next-line no-invalid-fetch-options\n\t\tbody: body,\n\t\t// @ts-ignore this is needed for using a Readable for some reason\n\t\tduplex: \"half\",\n\t});\n}\n","import { ServerResponse } from \"node:http\";\nimport { Http2ServerResponse } from \"node:http2\";\nimport { isArrayBufferView } from \"node:util/types\";\nimport { INTERNAL_BODY } from \"./constants\";\n\nexport default async function responseToNodeResponse(\n\tres: Response,\n\toutgoing: ServerResponse | Http2ServerResponse,\n): Promise<void> {\n\toutgoing.statusCode = res.status;\n\toutgoing.statusMessage = res.statusText;\n\n\tres.headers.forEach((value, key) => {\n\t\toutgoing.setHeader(key, value);\n\t});\n\n\tconst body = (res as any)[INTERNAL_BODY];\n\n\tif (body === null || body === undefined) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end();\n\t} else if (typeof body === \"string\" || body instanceof Uint8Array) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end(body);\n\t} else if (body instanceof Blob) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end(new Uint8Array(await body.arrayBuffer()));\n\t} else if (body instanceof ArrayBuffer) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end(new Uint8Array(body));\n\t} else if (isArrayBufferView(body)) {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end(new Uint8Array(body.buffer));\n\t} else if (body instanceof FormData) {\n\t\t// TODO\n\t\toutgoing.setHeader(\"Content-Type\", \"application/x-www-form-urlencoded;charset=UTF-8\");\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end();\n\t} else {\n\t\t// @ts-ignore\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end();\n\t}\n}\n","import { type IncomingMessage, ServerResponse } from \"node:http\";\nimport { Http2ServerRequest, Http2ServerResponse } from \"node:http2\";\nimport { INTERNAL_BODY } from \"./constants\";\nimport nodeMessageToRequest from \"./nodeMessageToRequest\";\nimport responseToNodeResponse from \"./responseToNodeResponse\";\n\n// From https://gist.github.com/marvinhagemeister/cc236ec97235ce0305ae9d48a24a607d\n// Referenced from https://marvinh.dev/blog/modern-way-to-write-javascript-servers/\n\nconst GlobalResponse = Response;\nglobalThis.Response = class Response extends GlobalResponse {\n\t[INTERNAL_BODY]: BodyInit | null | undefined = null;\n\n\tconstructor(body?: BodyInit | null, init?: ResponseInit) {\n\t\tsuper(body, init);\n\t\tthis[INTERNAL_BODY] = body;\n\t}\n};\n\nexport function createFetchHandler(handler: (req: Request) => Response | Promise<Response>) {\n\treturn async (\n\t\tincoming: IncomingMessage | Http2ServerRequest,\n\t\toutgoing: ServerResponse | Http2ServerResponse,\n\t): Promise<void> => {\n\t\tconst req = await nodeMessageToRequest(incoming);\n\t\tconst res = await handler(req);\n\t\treturn await responseToNodeResponse(res, outgoing);\n\t};\n}\n","import { IncomingMessage, Server, ServerResponse } from \"http\";\nimport { createServer } from \"node:http\";\nimport { createFetchHandler } from \"./createFetchHandler\";\n\nexport default function createNodeServer(\n\thandler: (req: Request) => Response | Promise<Response>,\n): Server<typeof IncomingMessage, typeof ServerResponse> {\n\t// Create a server, polyfill it so that it can handle standard fetch methods,\n\t// and pass our app fetch method to the handler\n\treturn createServer(createFetchHandler(handler));\n}\n","import { type Adapter } from \"@torpor/build\";\nimport { Server } from \"@torpor/build/server\";\nimport createNodeServer from \"./createNodeServer\";\n\nexport default {\n\tserve: (server: Server) => {\n\t\tprocess.env.PROTOCOL ??= \"http:\";\n\t\tprocess.env.HOST ??= \"localhost\";\n\t\tprocess.env.PORT ??= \"7059\";\n\n\t\t// Put adapter-specific functionality in the `adapter` property of\n\t\t// globalThis for now\n\t\t// @ts-ignore\n\t\tglobalThis.adapter = { env: process.env };\n\n\t\t// Create the Node server and start listening\n\t\tconst url = `${process.env.PROTOCOL}//${process.env.HOST}:${process.env.PORT}`;\n\t\tconsole.log(`\\nConnecting to ${url}`);\n\t\tconst devServer = createNodeServer(server.fetch);\n\t\tdevServer.listen(parseInt(process.env.PORT), process.env.HOST, () => {\n\t\t\tconsole.log(`Listening on ${url}\\n`);\n\t\t});\n\t},\n} satisfies Adapter as Adapter;\n"],"mappings":"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"}
|
package/src/adapter.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { type Adapter } from "@torpor/build";
|
|
2
|
-
import { Server } from "@torpor/build/server";
|
|
3
|
-
import createNodeServer from "./createNodeServer";
|
|
4
|
-
|
|
5
|
-
export default {
|
|
6
|
-
serve: (server: Server) => {
|
|
7
|
-
process.env.PROTOCOL ??= "http:";
|
|
8
|
-
process.env.HOST ??= "localhost";
|
|
9
|
-
process.env.PORT ??= "7059";
|
|
10
|
-
|
|
11
|
-
// Put adapter-specific functionality in the `adapter` property of
|
|
12
|
-
// globalThis for now
|
|
13
|
-
// @ts-ignore
|
|
14
|
-
globalThis.adapter = { env: process.env };
|
|
15
|
-
|
|
16
|
-
// Create the Node server and start listening
|
|
17
|
-
const url = `${process.env.PROTOCOL}//${process.env.HOST}:${process.env.PORT}`;
|
|
18
|
-
console.log(`\nConnecting to ${url}`);
|
|
19
|
-
const devServer = createNodeServer(server.fetch);
|
|
20
|
-
devServer.listen(parseInt(process.env.PORT), process.env.HOST, () => {
|
|
21
|
-
console.log(`Listening on ${url}\n`);
|
|
22
|
-
});
|
|
23
|
-
},
|
|
24
|
-
} satisfies Adapter as Adapter;
|
package/src/constants.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const INTERNAL_BODY: unique symbol = Symbol("internal_body");
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { type IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import { Http2ServerRequest, Http2ServerResponse } from "node:http2";
|
|
3
|
-
import { INTERNAL_BODY } from "./constants";
|
|
4
|
-
import nodeMessageToRequest from "./nodeMessageToRequest";
|
|
5
|
-
import responseToNodeResponse from "./responseToNodeResponse";
|
|
6
|
-
|
|
7
|
-
// From https://gist.github.com/marvinhagemeister/cc236ec97235ce0305ae9d48a24a607d
|
|
8
|
-
// Referenced from https://marvinh.dev/blog/modern-way-to-write-javascript-servers/
|
|
9
|
-
|
|
10
|
-
const GlobalResponse = Response;
|
|
11
|
-
globalThis.Response = class Response extends GlobalResponse {
|
|
12
|
-
[INTERNAL_BODY]: BodyInit | null | undefined = null;
|
|
13
|
-
|
|
14
|
-
constructor(body?: BodyInit | null, init?: ResponseInit) {
|
|
15
|
-
super(body, init);
|
|
16
|
-
this[INTERNAL_BODY] = body;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export function createFetchHandler(handler: (req: Request) => Response | Promise<Response>) {
|
|
21
|
-
return async (
|
|
22
|
-
incoming: IncomingMessage | Http2ServerRequest,
|
|
23
|
-
outgoing: ServerResponse | Http2ServerResponse,
|
|
24
|
-
): Promise<void> => {
|
|
25
|
-
const req = await nodeMessageToRequest(incoming);
|
|
26
|
-
const res = await handler(req);
|
|
27
|
-
return await responseToNodeResponse(res, outgoing);
|
|
28
|
-
};
|
|
29
|
-
}
|
package/src/createNodeServer.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { IncomingMessage, Server, ServerResponse } from "http";
|
|
2
|
-
import { createServer } from "node:http";
|
|
3
|
-
import { createFetchHandler } from "./createFetchHandler";
|
|
4
|
-
|
|
5
|
-
export default function createNodeServer(
|
|
6
|
-
handler: (req: Request) => Response | Promise<Response>,
|
|
7
|
-
): Server<typeof IncomingMessage, typeof ServerResponse> {
|
|
8
|
-
// Create a server, polyfill it so that it can handle standard fetch methods,
|
|
9
|
-
// and pass our app fetch method to the handler
|
|
10
|
-
return createServer(createFetchHandler(handler));
|
|
11
|
-
}
|
package/src/flattenHeaders.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { IncomingHttpHeaders } from "node:http";
|
|
2
|
-
import { OutgoingHttpHeaders } from "node:http2";
|
|
3
|
-
|
|
4
|
-
export default function flattenHeaders(
|
|
5
|
-
headers: IncomingHttpHeaders | OutgoingHttpHeaders,
|
|
6
|
-
): HeadersInit {
|
|
7
|
-
const flatHeaders = new Headers();
|
|
8
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
9
|
-
if (value === undefined || value === null) {
|
|
10
|
-
continue;
|
|
11
|
-
} else if (Array.isArray(value)) {
|
|
12
|
-
for (const v of value) {
|
|
13
|
-
if (v !== undefined && v !== null) {
|
|
14
|
-
value.forEach((v) => flatHeaders.append(key, v));
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
} else {
|
|
18
|
-
flatHeaders.set(key, String(value));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return flatHeaders;
|
|
22
|
-
|
|
23
|
-
/*
|
|
24
|
-
const flatHeaders: [string, string][] = [];
|
|
25
|
-
|
|
26
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
27
|
-
if (value === undefined || value === null) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (Array.isArray(value)) {
|
|
32
|
-
for (const v of value) {
|
|
33
|
-
if (v != null) {
|
|
34
|
-
flatHeaders.push([key, String(v)]);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
} else {
|
|
38
|
-
flatHeaders.push([key, String(value)]);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return flatHeaders;
|
|
43
|
-
*/
|
|
44
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { type IncomingMessage } from "node:http";
|
|
2
|
-
import { Http2ServerRequest } from "node:http2";
|
|
3
|
-
import { Readable } from "node:stream";
|
|
4
|
-
import flattenHeaders from "./flattenHeaders";
|
|
5
|
-
|
|
6
|
-
// From https://stackoverflow.com/a/78849544
|
|
7
|
-
// and https://gist.github.com/marvinhagemeister/cc236ec97235ce0305ae9d48a24a607d
|
|
8
|
-
|
|
9
|
-
export default async function nodeMessageToRequest(
|
|
10
|
-
req: IncomingMessage | Http2ServerRequest,
|
|
11
|
-
): Promise<Request> {
|
|
12
|
-
// Build the full url
|
|
13
|
-
let path = req.url;
|
|
14
|
-
if (path === undefined) {
|
|
15
|
-
throw new Error(`Empty request URL`);
|
|
16
|
-
}
|
|
17
|
-
if (!path.startsWith("/")) {
|
|
18
|
-
path = "/" + path;
|
|
19
|
-
}
|
|
20
|
-
const { host } = req.headers;
|
|
21
|
-
if (host == undefined) {
|
|
22
|
-
throw new Error(`Missing "Host" header`);
|
|
23
|
-
}
|
|
24
|
-
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https" : "http";
|
|
25
|
-
const url = `${protocol}://${host}${path}`;
|
|
26
|
-
|
|
27
|
-
// Flatten the headers
|
|
28
|
-
const headers = flattenHeaders(req.headers);
|
|
29
|
-
|
|
30
|
-
// Convert body to Buffer if applicable
|
|
31
|
-
const body =
|
|
32
|
-
req.method !== "GET" && req.method !== "HEAD"
|
|
33
|
-
? (Readable.toWeb(req) as ReadableStream) // await readableToBuffer(req)
|
|
34
|
-
: null;
|
|
35
|
-
|
|
36
|
-
return new Request(url, {
|
|
37
|
-
method: req.method,
|
|
38
|
-
headers: headers,
|
|
39
|
-
// eslint-disable-next-line no-invalid-fetch-options
|
|
40
|
-
body: body,
|
|
41
|
-
// @ts-ignore this is needed for using a Readable for some reason
|
|
42
|
-
duplex: "half",
|
|
43
|
-
});
|
|
44
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { ServerResponse } from "node:http";
|
|
2
|
-
import { Http2ServerResponse } from "node:http2";
|
|
3
|
-
import { isArrayBufferView } from "node:util/types";
|
|
4
|
-
import { INTERNAL_BODY } from "./constants";
|
|
5
|
-
|
|
6
|
-
export default async function responseToNodeResponse(
|
|
7
|
-
res: Response,
|
|
8
|
-
outgoing: ServerResponse | Http2ServerResponse,
|
|
9
|
-
): Promise<void> {
|
|
10
|
-
outgoing.statusCode = res.status;
|
|
11
|
-
outgoing.statusMessage = res.statusText;
|
|
12
|
-
|
|
13
|
-
res.headers.forEach((value, key) => {
|
|
14
|
-
outgoing.setHeader(key, value);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
const body = (res as any)[INTERNAL_BODY];
|
|
18
|
-
|
|
19
|
-
if (body === null || body === undefined) {
|
|
20
|
-
// @ts-ignore
|
|
21
|
-
outgoing.writeHead(res.status, res.statusText);
|
|
22
|
-
outgoing.end();
|
|
23
|
-
} else if (typeof body === "string" || body instanceof Uint8Array) {
|
|
24
|
-
// @ts-ignore
|
|
25
|
-
outgoing.writeHead(res.status, res.statusText);
|
|
26
|
-
outgoing.end(body);
|
|
27
|
-
} else if (body instanceof Blob) {
|
|
28
|
-
// @ts-ignore
|
|
29
|
-
outgoing.writeHead(res.status, res.statusText);
|
|
30
|
-
outgoing.end(new Uint8Array(await body.arrayBuffer()));
|
|
31
|
-
} else if (body instanceof ArrayBuffer) {
|
|
32
|
-
// @ts-ignore
|
|
33
|
-
outgoing.writeHead(res.status, res.statusText);
|
|
34
|
-
outgoing.end(new Uint8Array(body));
|
|
35
|
-
} else if (isArrayBufferView(body)) {
|
|
36
|
-
// @ts-ignore
|
|
37
|
-
outgoing.writeHead(res.status, res.statusText);
|
|
38
|
-
outgoing.end(new Uint8Array(body.buffer));
|
|
39
|
-
} else if (body instanceof FormData) {
|
|
40
|
-
// TODO
|
|
41
|
-
outgoing.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
|
|
42
|
-
// @ts-ignore
|
|
43
|
-
outgoing.writeHead(res.status, res.statusText);
|
|
44
|
-
outgoing.end();
|
|
45
|
-
} else {
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
outgoing.writeHead(res.status, res.statusText);
|
|
48
|
-
outgoing.end();
|
|
49
|
-
}
|
|
50
|
-
}
|
package/tsconfig.json
DELETED
package/tsdown.config.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { type Options, defineConfig } from "tsdown";
|
|
2
|
-
|
|
3
|
-
type Config =
|
|
4
|
-
| Options
|
|
5
|
-
| Options[]
|
|
6
|
-
| ((overrideOptions: Options) => Options | Options[] | Promise<Options | Options[]>);
|
|
7
|
-
|
|
8
|
-
export default defineConfig({
|
|
9
|
-
entry: ["src/index.ts"],
|
|
10
|
-
format: "esm",
|
|
11
|
-
dts: true,
|
|
12
|
-
sourcemap: true,
|
|
13
|
-
}) satisfies Config as Config;
|