@torpor/adapter-node 0.1.1 → 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.
- package/dist/{index.d.ts → index.d.mts} +1 -1
- package/dist/index.d.mts.map +1 -0
- package/dist/{index.js → index.mjs} +1 -1
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -11
- 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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/adapter.ts"],"sourcesContent":[],"mappings":";;;cAIA,UAmBuB"}
|
|
@@ -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,gBAAgB,IAAI;AAE7B,KAAI,QAAQ,SAAS,OAAO,QAAQ;AACnC,WAAS,UAAU,KAAK,MAAM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,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.
|
|
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.
|
|
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,19 @@
|
|
|
14
17
|
"author": "",
|
|
15
18
|
"license": "ISC",
|
|
16
19
|
"dependencies": {
|
|
17
|
-
"vite": "^7.
|
|
18
|
-
"@torpor/build": "^0.4.
|
|
20
|
+
"vite": "^7.2.2",
|
|
21
|
+
"@torpor/build": "^0.4.10"
|
|
19
22
|
},
|
|
20
23
|
"devDependencies": {
|
|
21
|
-
"@trivago/prettier-plugin-sort-imports": "^
|
|
22
|
-
"@typescript/native-preview": "7.0.0-dev.
|
|
23
|
-
"oxlint-tsgolint": "^0.
|
|
24
|
-
"tsdown": "^0.
|
|
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,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":";;;;;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;OAA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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;
|