@hono/node-server 1.2.0 → 1.2.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/vercel.mjs CHANGED
@@ -1,5 +1,135 @@
1
+ // src/listener.ts
2
+ import { Readable } from "stream";
3
+
4
+ // src/globals.ts
5
+ import crypto from "crypto";
6
+ var webFetch = global.fetch;
7
+ if (typeof global.crypto === "undefined") {
8
+ global.crypto = crypto;
9
+ }
10
+ global.fetch = (info, init) => {
11
+ init = {
12
+ // Disable compression handling so people can return the result of a fetch
13
+ // directly in the loader without messing with the Content-Encoding header.
14
+ compress: false,
15
+ ...init
16
+ };
17
+ return webFetch(info, init);
18
+ };
19
+
20
+ // src/utils.ts
21
+ function writeFromReadableStream(stream, writable) {
22
+ if (stream.locked) {
23
+ throw new TypeError("ReadableStream is locked.");
24
+ }
25
+ const reader = stream.getReader();
26
+ if (writable.destroyed) {
27
+ reader.cancel();
28
+ return;
29
+ }
30
+ writable.on("drain", onDrain);
31
+ writable.on("close", cancel);
32
+ writable.on("error", cancel);
33
+ reader.read().then(flow, cancel);
34
+ return reader.closed.finally(() => {
35
+ writable.off("close", cancel);
36
+ writable.off("error", cancel);
37
+ writable.off("drain", onDrain);
38
+ });
39
+ function cancel(error) {
40
+ reader.cancel(error).catch(() => {
41
+ });
42
+ if (error)
43
+ writable.destroy(error);
44
+ }
45
+ function onDrain() {
46
+ reader.read().then(flow, cancel);
47
+ }
48
+ function flow({ done, value }) {
49
+ try {
50
+ if (done) {
51
+ writable.end();
52
+ } else if (writable.write(value)) {
53
+ return reader.read().then(flow, cancel);
54
+ }
55
+ } catch (e) {
56
+ cancel(e);
57
+ }
58
+ }
59
+ }
60
+
61
+ // src/listener.ts
62
+ var regBuffer = /^no$/i;
63
+ var regContentType = /^(application\/json\b|text\/(?!event-stream\b))/i;
64
+ var getRequestListener = (fetchCallback) => {
65
+ return async (incoming, outgoing) => {
66
+ const method = incoming.method || "GET";
67
+ const url = `http://${incoming.headers.host}${incoming.url}`;
68
+ const headerRecord = [];
69
+ const len = incoming.rawHeaders.length;
70
+ for (let i = 0; i < len; i += 2) {
71
+ headerRecord.push([incoming.rawHeaders[i], incoming.rawHeaders[i + 1]]);
72
+ }
73
+ const init = {
74
+ method,
75
+ headers: headerRecord
76
+ };
77
+ if (!(method === "GET" || method === "HEAD")) {
78
+ init.body = Readable.toWeb(incoming);
79
+ init.duplex = "half";
80
+ }
81
+ let res;
82
+ try {
83
+ res = await fetchCallback(new Request(url, init));
84
+ } catch (e) {
85
+ res = new Response(null, { status: 500 });
86
+ if (e instanceof Error) {
87
+ if (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") {
88
+ res = new Response(null, { status: 504 });
89
+ }
90
+ }
91
+ }
92
+ const resHeaderRecord = {};
93
+ const cookies = [];
94
+ for (const [k, v] of res.headers) {
95
+ if (k === "set-cookie") {
96
+ cookies.push(v);
97
+ } else {
98
+ resHeaderRecord[k] = v;
99
+ }
100
+ }
101
+ if (cookies.length > 0) {
102
+ resHeaderRecord["set-cookie"] = cookies;
103
+ }
104
+ if (res.body) {
105
+ try {
106
+ if (resHeaderRecord["transfer-encoding"] || resHeaderRecord["content-encoding"] || resHeaderRecord["content-length"] || // nginx buffering variant
107
+ resHeaderRecord["x-accel-buffering"] && regBuffer.test(resHeaderRecord["x-accel-buffering"]) || !regContentType.test(resHeaderRecord["content-type"])) {
108
+ outgoing.writeHead(res.status, resHeaderRecord);
109
+ await writeFromReadableStream(res.body, outgoing);
110
+ } else {
111
+ const buffer = await res.arrayBuffer();
112
+ resHeaderRecord["content-length"] = buffer.byteLength;
113
+ outgoing.writeHead(res.status, resHeaderRecord);
114
+ outgoing.end(new Uint8Array(buffer));
115
+ }
116
+ } catch (e) {
117
+ const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
118
+ if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
119
+ console.info("The user aborted a request.");
120
+ } else {
121
+ console.error(e);
122
+ outgoing.destroy(err);
123
+ }
124
+ }
125
+ } else {
126
+ outgoing.writeHead(res.status, resHeaderRecord);
127
+ outgoing.end();
128
+ }
129
+ };
130
+ };
131
+
1
132
  // src/vercel.ts
2
- import { getRequestListener } from "./listener.mjs";
3
133
  var handle = (app) => {
4
134
  return getRequestListener(app.fetch);
5
135
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/node-server",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Node.js Adapter for Hono",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -39,7 +39,8 @@
39
39
  },
40
40
  "scripts": {
41
41
  "test": "jest",
42
- "build": "rimraf dist && tsx ./build.ts",
42
+ "build": "tsup",
43
+ "watch": "tsup --watch",
43
44
  "postbuild": "publint",
44
45
  "prerelease": "yarn build && yarn test",
45
46
  "release": "np"
@@ -56,23 +57,20 @@
56
57
  "access": "public"
57
58
  },
58
59
  "engines": {
59
- "node": ">=18.0.0"
60
+ "node": ">=18.14.1"
60
61
  },
61
62
  "dependencies": {},
62
63
  "devDependencies": {
63
- "@types/glob": "^8.1.0",
64
64
  "@types/jest": "^29.5.3",
65
65
  "@types/node": "^18.7.16",
66
66
  "@types/supertest": "^2.0.12",
67
- "esbuild": "^0.18.13",
68
- "hono": "^3.3.4",
67
+ "hono": "^3.9.2",
69
68
  "jest": "^29.6.1",
70
69
  "np": "^7.7.0",
71
70
  "publint": "^0.1.16",
72
- "rimraf": "^3.0.2",
73
71
  "supertest": "^6.3.3",
74
72
  "ts-jest": "^29.1.1",
75
- "tsx": "^3.12.7",
73
+ "tsup": "^7.2.0",
76
74
  "typescript": "^4.8.3"
77
75
  }
78
76
  }