@torpor/adapter-node 0.1.0 → 0.1.2

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.
@@ -4,4 +4,4 @@ import { Adapter } from "@torpor/build";
4
4
  declare const _default: Adapter;
5
5
  //#endregion
6
6
  export { _default as node };
7
- //# sourceMappingURL=index.d.ts.map
7
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/adapter.ts"],"sourcesContent":[],"mappings":";;;cAIA,UAmBuB"}
@@ -81,9 +81,7 @@ globalThis.Response = class Response$1 extends GlobalResponse {
81
81
  };
82
82
  function createFetchHandler(handler) {
83
83
  return async (incoming, outgoing) => {
84
- const req = await nodeMessageToRequest(incoming);
85
- const res = await handler(req);
86
- return await responseToNodeResponse(res, outgoing);
84
+ return await responseToNodeResponse(await handler(await nodeMessageToRequest(incoming)), outgoing);
87
85
  };
88
86
  }
89
87
 
@@ -109,4 +107,4 @@ var adapter_default = { serve: (server) => {
109
107
 
110
108
  //#endregion
111
109
  export { adapter_default as node };
112
- //# sourceMappingURL=index.js.map
110
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","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":";;;;;AAAA,MAAaA,gBAA+B,OAAO,gBAAgB;;;;ACGnE,SAAwB,eACvB,SACc;CACd,MAAM,cAAc,IAAI,SAAS;AACjC,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,QAAQ,CACjD,KAAI,UAAU,UAAa,UAAU,KACpC;UACU,MAAM,QAAQ,MAAM,EAC9B;OAAK,MAAM,KAAK,MACf,KAAI,MAAM,UAAa,MAAM,KAC5B,OAAM,SAAS,QAAM,YAAY,OAAO,KAAKC,IAAE,CAAC;OAIlD,aAAY,IAAI,KAAK,OAAO,MAAM,CAAC;AAGrC,QAAO;;;;;ACZR,eAA8B,qBAC7B,KACmB;CAEnB,IAAI,OAAO,IAAI;AACf,KAAI,SAAS,OACZ,OAAM,IAAI,MAAM,oBAAoB;AAErC,KAAI,CAAC,KAAK,WAAW,IAAI,CACxB,QAAO,MAAM;CAEd,MAAM,EAAE,SAAS,IAAI;AACrB,KAAI,QAAQ,OACX,OAAM,IAAI,MAAM,wBAAwB;CAGzC,MAAM,MAAM,GADK,eAAe,IAAI,UAAU,IAAI,OAAO,YAAY,UAAU,OACvD,KAAK,OAAO;CAGpC,MAAM,UAAU,eAAe,IAAI,QAAQ;CAG3C,MAAM,OACL,IAAI,WAAW,SAAS,IAAI,WAAW,SACnC,SAAS,MAAM,IAAI,GACpB;AAEJ,QAAO,IAAI,QAAQ,KAAK;EACvB,QAAQ,IAAI;EACH;EAEH;EAEN,QAAQ;EACR,CAAC;;;;;ACrCH,eAA8B,uBAC7B,KACA,UACgB;AAChB,UAAS,aAAa,IAAI;AAC1B,UAAS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,CAAC;;;;;ACLjD,sBAAe,EACd,QAAQ,WAAmB;AAC1B,SAAQ,IAAI,aAAa;AACzB,SAAQ,IAAI,SAAS;AACrB,SAAQ,IAAI,SAAS;AAKrB,YAAW,UAAU,EAAE,KAAK,QAAQ,KAAK;CAGzC,MAAM,MAAM,GAAG,QAAQ,IAAI,SAAS,IAAI,QAAQ,IAAI,KAAK,GAAG,QAAQ,IAAI;AACxE,SAAQ,IAAI,mBAAmB,MAAM;AAErC,CADkB,iBAAiB,OAAO,MAAM,CACtC,OAAO,SAAS,QAAQ,IAAI,KAAK,EAAE,QAAQ,IAAI,YAAY;AACpE,UAAQ,IAAI,gBAAgB,IAAI,IAAI;GACnC;GAEH"}
package/package.json CHANGED
@@ -1,12 +1,15 @@
1
1
  {
2
2
  "name": "@torpor/adapter-node",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "A @torpor/build adapter for running on Node",
5
5
  "type": "module",
6
6
  "exports": {
7
- "types": "./dist/index.d.ts",
8
- "import": "./dist/index.js"
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,19 @@
14
17
  "author": "",
15
18
  "license": "ISC",
16
19
  "dependencies": {
17
- "vite": "^7.1.9",
18
- "@torpor/build": "^0.3.0"
20
+ "vite": "^7.2.2",
21
+ "@torpor/build": "^0.4.10"
19
22
  },
20
23
  "devDependencies": {
21
- "@trivago/prettier-plugin-sort-imports": "^5.2.2",
22
- "@typescript/native-preview": "7.0.0-dev.20251005.1",
23
- "oxlint-tsgolint": "^0.2.0",
24
- "tsdown": "^0.15.6",
24
+ "@trivago/prettier-plugin-sort-imports": "^6.0.0",
25
+ "@typescript/native-preview": "7.0.0-dev.20251112.1",
26
+ "oxlint-tsgolint": "^0.6.0",
27
+ "tsdown": "^0.16.4",
25
28
  "typescript": "^5.9.3"
26
29
  },
27
30
  "scripts": {
31
+ "check": "tsgo --noEmit && pnpm dlx oxlint --type-aware",
28
32
  "build": "tsgo --noEmit && tsdown",
29
- "build:watch": "tsdown --watch",
30
- "check": "tsgo --noEmit && pnpm dlx oxlint --type-aware"
33
+ "build:watch": "tsdown --watch"
31
34
  }
32
35
  }
package/CHANGELOG.md DELETED
@@ -1,218 +0,0 @@
1
- # @torpor/adapter-node
2
-
3
- ## 0.1.0
4
-
5
- ### Minor Changes
6
-
7
- - 51dbba7: Chore: drop CJS build and go ESM only
8
-
9
- ### Patch Changes
10
-
11
- - Updated dependencies [bbdc8d3]
12
- - Updated dependencies [e1ae2bb]
13
- - Updated dependencies [51dbba7]
14
- - Updated dependencies [62b0d2e]
15
- - @torpor/build@0.3.0
16
-
17
- ## 0.0.30
18
-
19
- ### Patch Changes
20
-
21
- - Updated dependencies [a4f2573]
22
- - Updated dependencies [67e6ca6]
23
- - Updated dependencies [b716d5c]
24
- - @torpor/build@0.2.0
25
-
26
- ## 0.0.29
27
-
28
- ### Patch Changes
29
-
30
- - @torpor/build@0.1.31
31
-
32
- ## 0.0.28
33
-
34
- ### Patch Changes
35
-
36
- - @torpor/build@0.1.30
37
-
38
- ## 0.0.27
39
-
40
- ### Patch Changes
41
-
42
- - @torpor/build@0.1.29
43
-
44
- ## 0.0.26
45
-
46
- ### Patch Changes
47
-
48
- - Updated dependencies [fa85b57]
49
- - @torpor/build@0.1.28
50
-
51
- ## 0.0.25
52
-
53
- ### Patch Changes
54
-
55
- - Updated dependencies [bed55d5]
56
- - @torpor/build@0.1.27
57
-
58
- ## 0.0.24
59
-
60
- ### Patch Changes
61
-
62
- - Updated dependencies [348cd75]
63
- - @torpor/build@0.1.26
64
-
65
- ## 0.0.23
66
-
67
- ### Patch Changes
68
-
69
- - Updated dependencies [3fe0d0e]
70
- - Updated dependencies [f26603f]
71
- - @torpor/build@0.1.25
72
-
73
- ## 0.0.22
74
-
75
- ### Patch Changes
76
-
77
- - Updated dependencies [6092a91]
78
- - @torpor/build@0.1.24
79
-
80
- ## 0.0.21
81
-
82
- ### Patch Changes
83
-
84
- - Updated dependencies [22f62d4]
85
- - @torpor/build@0.1.23
86
-
87
- ## 0.0.20
88
-
89
- ### Patch Changes
90
-
91
- - Updated dependencies [4e0ec90]
92
- - @torpor/build@0.1.22
93
-
94
- ## 0.0.19
95
-
96
- ### Patch Changes
97
-
98
- - Updated dependencies [4296742]
99
- - @torpor/build@0.1.21
100
-
101
- ## 0.0.18
102
-
103
- ### Patch Changes
104
-
105
- - Updated dependencies [a61f30f]
106
- - @torpor/build@0.1.20
107
-
108
- ## 0.0.17
109
-
110
- ### Patch Changes
111
-
112
- - Updated dependencies [170c886]
113
- - @torpor/build@0.1.19
114
-
115
- ## 0.0.16
116
-
117
- ### Patch Changes
118
-
119
- - @torpor/build@0.1.18
120
-
121
- ## 0.0.15
122
-
123
- ### Patch Changes
124
-
125
- - Updated dependencies [7373f27]
126
- - Updated dependencies [679a448]
127
- - Updated dependencies [e5b761b]
128
- - @torpor/build@0.1.17
129
-
130
- ## 0.0.14
131
-
132
- ### Patch Changes
133
-
134
- - Updated dependencies [9332635]
135
- - @torpor/build@0.1.16
136
-
137
- ## 0.0.13
138
-
139
- ### Patch Changes
140
-
141
- - @torpor/build@0.1.15
142
-
143
- ## 0.0.12
144
-
145
- ### Patch Changes
146
-
147
- - Updated dependencies [e395c9e]
148
- - @torpor/build@0.1.14
149
-
150
- ## 0.0.11
151
-
152
- ### Patch Changes
153
-
154
- - 7162232: Fix: set globalThis.adapter in all adapters
155
- - Updated dependencies [02eac8e]
156
- - @torpor/build@0.1.13
157
-
158
- ## 0.0.10
159
-
160
- ### Patch Changes
161
-
162
- - 3ed8a23: Chore: specify exports in packages
163
-
164
- ## 0.0.9
165
-
166
- ### Patch Changes
167
-
168
- - Updated dependencies [8de094b]
169
- - @torpor/build@0.1.12
170
-
171
- ## 0.0.8
172
-
173
- ### Patch Changes
174
-
175
- - Updated dependencies [858538b]
176
- - @torpor/build@0.1.11
177
-
178
- ## 0.0.7
179
-
180
- ### Patch Changes
181
-
182
- - @torpor/build@0.1.10
183
-
184
- ## 0.0.6
185
-
186
- ### Patch Changes
187
-
188
- - Updated dependencies [ff685cb]
189
- - @torpor/build@0.1.9
190
-
191
- ## 0.0.5
192
-
193
- ### Patch Changes
194
-
195
- - fe7444e: Refactor: rearrange build exports
196
- - Updated dependencies [fe7444e]
197
- - @torpor/build@0.1.8
198
-
199
- ## 0.0.4
200
-
201
- ### Patch Changes
202
-
203
- - @torpor/build@0.1.7
204
-
205
- ## 0.0.3
206
-
207
- ### Patch Changes
208
-
209
- - 47c5277: Chore: update dependencies
210
- - Updated dependencies [47c5277]
211
- - @torpor/build@0.1.6
212
-
213
- ## 0.0.2
214
-
215
- ### Patch Changes
216
-
217
- - Updated dependencies [5c9d90f]
218
- - @torpor/build@0.1.5
@@ -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\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end();\n\t} else if (typeof body === \"string\" || body instanceof Uint8Array) {\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end(body);\n\t} else if (body instanceof Blob) {\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\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end(new Uint8Array(body));\n\t} else if (isArrayBufferView(body)) {\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\n\t\toutgoing.writeHead(res.status, res.statusText);\n\t\toutgoing.end();\n\t} else {\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\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,MAAM,MAAM,GAAG,QAAQ,IAAI,SAAS,IAAI,QAAQ,IAAI,KAAK,GAAG,QAAQ,IAAI;AACxE,SAAQ,IAAI,mBAAmB,MAAM;AAErC,CADkB,iBAAiB,OAAO,MAAM,CACtC,OAAO,SAAS,QAAQ,IAAI,KAAK,EAAE,QAAQ,IAAI,YAAY;AACpE,UAAQ,IAAI,gBAAgB,IAAI,IAAI;GACnC;GAEH"}
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
- }
@@ -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
- }
@@ -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,3 +0,0 @@
1
- import node from "./adapter";
2
-
3
- export { node };
@@ -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,44 +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
- outgoing.writeHead(res.status, res.statusText);
21
- outgoing.end();
22
- } else if (typeof body === "string" || body instanceof Uint8Array) {
23
- outgoing.writeHead(res.status, res.statusText);
24
- outgoing.end(body);
25
- } else if (body instanceof Blob) {
26
- outgoing.writeHead(res.status, res.statusText);
27
- outgoing.end(new Uint8Array(await body.arrayBuffer()));
28
- } else if (body instanceof ArrayBuffer) {
29
- outgoing.writeHead(res.status, res.statusText);
30
- outgoing.end(new Uint8Array(body));
31
- } else if (isArrayBufferView(body)) {
32
- outgoing.writeHead(res.status, res.statusText);
33
- outgoing.end(new Uint8Array(body.buffer));
34
- } else if (body instanceof FormData) {
35
- // TODO
36
- outgoing.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
37
-
38
- outgoing.writeHead(res.status, res.statusText);
39
- outgoing.end();
40
- } else {
41
- outgoing.writeHead(res.status, res.statusText);
42
- outgoing.end();
43
- }
44
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../../tsconfig.json",
3
- "compilerOptions": {
4
- // HACK:
5
- "skipLibCheck": true,
6
- "verbatimModuleSyntax": false
7
- },
8
- "include": ["src"]
9
- }
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;