@whatwg-node/server 0.9.17 → 0.9.18

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/cjs/utils.js CHANGED
@@ -81,6 +81,7 @@ class ServerAdapterRequestAbortSignal extends EventTarget {
81
81
  }
82
82
  }
83
83
  exports.ServerAdapterRequestAbortSignal = ServerAdapterRequestAbortSignal;
84
+ let bunNodeCompatModeWarned = false;
84
85
  function normalizeNodeRequest(nodeRequest, RequestCtor) {
85
86
  const rawRequest = nodeRequest.raw || nodeRequest.req || nodeRequest;
86
87
  let fullUrl = buildFullUrl(rawRequest);
@@ -91,10 +92,22 @@ function normalizeNodeRequest(nodeRequest, RequestCtor) {
91
92
  }
92
93
  fullUrl = url.toString();
93
94
  }
94
- const signal = new ServerAdapterRequestAbortSignal();
95
- if (rawRequest.once) {
96
- rawRequest.once('end', () => signal.sendAbort());
97
- rawRequest.once('close', () => signal.sendAbort());
95
+ let signal;
96
+ // If ponyfilled
97
+ if (RequestCtor !== globalThis.Request) {
98
+ signal = new ServerAdapterRequestAbortSignal();
99
+ if (rawRequest.once) {
100
+ rawRequest.once('end', () => signal.sendAbort());
101
+ rawRequest.once('close', () => signal.sendAbort());
102
+ }
103
+ }
104
+ else {
105
+ const controller = new AbortController();
106
+ signal = controller.signal;
107
+ if (rawRequest.once) {
108
+ rawRequest.once('end', () => controller.abort());
109
+ rawRequest.once('close', () => controller.abort());
110
+ }
98
111
  }
99
112
  if (nodeRequest.method === 'GET' || nodeRequest.method === 'HEAD') {
100
113
  return new RequestCtor(fullUrl, {
@@ -140,6 +153,35 @@ function normalizeNodeRequest(nodeRequest, RequestCtor) {
140
153
  },
141
154
  });
142
155
  }
156
+ // Temporary workaround for a bug in Bun Node compat mode
157
+ if (globalThis.process?.versions?.bun && isReadable(rawRequest)) {
158
+ if (!bunNodeCompatModeWarned) {
159
+ bunNodeCompatModeWarned = true;
160
+ console.warn(`You use Bun Node compatibility mode, which is not recommended!
161
+ It will affect your performance. Please check our Bun integration recipe, and avoid using 'node:http' for your server implementation.`);
162
+ }
163
+ return new RequestCtor(fullUrl, {
164
+ method: nodeRequest.method,
165
+ headers: nodeRequest.headers,
166
+ body: new ReadableStream({
167
+ start(controller) {
168
+ rawRequest.on('data', chunk => {
169
+ controller.enqueue(chunk);
170
+ });
171
+ rawRequest.on('error', e => {
172
+ controller.error(e);
173
+ });
174
+ rawRequest.on('end', () => {
175
+ controller.close();
176
+ });
177
+ },
178
+ cancel(e) {
179
+ rawRequest.destroy(e);
180
+ },
181
+ }),
182
+ signal,
183
+ });
184
+ }
143
185
  // perf: instead of spreading the object, we can just pass it as is and it performs better
144
186
  return new RequestCtor(fullUrl, {
145
187
  method: nodeRequest.method,
package/esm/utils.js CHANGED
@@ -76,6 +76,7 @@ export class ServerAdapterRequestAbortSignal extends EventTarget {
76
76
  }
77
77
  }
78
78
  }
79
+ let bunNodeCompatModeWarned = false;
79
80
  export function normalizeNodeRequest(nodeRequest, RequestCtor) {
80
81
  const rawRequest = nodeRequest.raw || nodeRequest.req || nodeRequest;
81
82
  let fullUrl = buildFullUrl(rawRequest);
@@ -86,10 +87,22 @@ export function normalizeNodeRequest(nodeRequest, RequestCtor) {
86
87
  }
87
88
  fullUrl = url.toString();
88
89
  }
89
- const signal = new ServerAdapterRequestAbortSignal();
90
- if (rawRequest.once) {
91
- rawRequest.once('end', () => signal.sendAbort());
92
- rawRequest.once('close', () => signal.sendAbort());
90
+ let signal;
91
+ // If ponyfilled
92
+ if (RequestCtor !== globalThis.Request) {
93
+ signal = new ServerAdapterRequestAbortSignal();
94
+ if (rawRequest.once) {
95
+ rawRequest.once('end', () => signal.sendAbort());
96
+ rawRequest.once('close', () => signal.sendAbort());
97
+ }
98
+ }
99
+ else {
100
+ const controller = new AbortController();
101
+ signal = controller.signal;
102
+ if (rawRequest.once) {
103
+ rawRequest.once('end', () => controller.abort());
104
+ rawRequest.once('close', () => controller.abort());
105
+ }
93
106
  }
94
107
  if (nodeRequest.method === 'GET' || nodeRequest.method === 'HEAD') {
95
108
  return new RequestCtor(fullUrl, {
@@ -135,6 +148,35 @@ export function normalizeNodeRequest(nodeRequest, RequestCtor) {
135
148
  },
136
149
  });
137
150
  }
151
+ // Temporary workaround for a bug in Bun Node compat mode
152
+ if (globalThis.process?.versions?.bun && isReadable(rawRequest)) {
153
+ if (!bunNodeCompatModeWarned) {
154
+ bunNodeCompatModeWarned = true;
155
+ console.warn(`You use Bun Node compatibility mode, which is not recommended!
156
+ It will affect your performance. Please check our Bun integration recipe, and avoid using 'node:http' for your server implementation.`);
157
+ }
158
+ return new RequestCtor(fullUrl, {
159
+ method: nodeRequest.method,
160
+ headers: nodeRequest.headers,
161
+ body: new ReadableStream({
162
+ start(controller) {
163
+ rawRequest.on('data', chunk => {
164
+ controller.enqueue(chunk);
165
+ });
166
+ rawRequest.on('error', e => {
167
+ controller.error(e);
168
+ });
169
+ rawRequest.on('end', () => {
170
+ controller.close();
171
+ });
172
+ },
173
+ cancel(e) {
174
+ rawRequest.destroy(e);
175
+ },
176
+ }),
177
+ signal,
178
+ });
179
+ }
138
180
  // perf: instead of spreading the object, we can just pass it as is and it performs better
139
181
  return new RequestCtor(fullUrl, {
140
182
  method: nodeRequest.method,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/server",
3
- "version": "0.9.17",
3
+ "version": "0.9.18",
4
4
  "description": "Fetch API compliant HTTP Server adapter",
5
5
  "sideEffects": false,
6
6
  "dependencies": {