@whatwg-node/server 0.4.2 → 0.4.4

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.
Files changed (4) hide show
  1. package/README.md +15 -0
  2. package/index.js +29 -25
  3. package/index.mjs +29 -25
  4. package/package.json +1 -1
package/README.md CHANGED
@@ -192,6 +192,21 @@ import myServerAdapter from './myServerAdapter'
192
192
  export { myServerAdapter as get, myServerAdapter as post }
193
193
  ```
194
194
 
195
+ ### Bun
196
+
197
+ [Bun](https://bun.sh/) is a modern JavaScript runtime like Node or Deno, and it supports Fetch API as a first class citizen.
198
+ So the configuration is really simple like any other JS runtime;
199
+
200
+ ```ts
201
+ import myServerAdapter from './myServerAdapter'
202
+
203
+ Bun.serve(myServerAdapter)
204
+
205
+ const server = Bun.serve(yoga)
206
+
207
+ console.info(`Server is running on ${server.hostname}`)
208
+ ```
209
+
195
210
  ## File Uploads / Multipart Requests
196
211
 
197
212
  Multipart requests are a type of HTTP request that allows you to send blobs together with regular text data which has a mime-type `multipart/form-data`.
package/index.js CHANGED
@@ -170,12 +170,6 @@ function createServerAdapter(serverAdapterBaseObject,
170
170
  */
171
171
  RequestCtor = fetch.Request) {
172
172
  const handleRequest = typeof serverAdapterBaseObject === 'function' ? serverAdapterBaseObject : serverAdapterBaseObject.handle;
173
- function fetchFn(input, init, ...ctx) {
174
- if (typeof input === 'string' || input instanceof URL) {
175
- return handleRequest(new RequestCtor(input, init), Object.assign({}, ...ctx));
176
- }
177
- return handleRequest(input, Object.assign({}, init, ...ctx));
178
- }
179
173
  function handleNodeRequest(nodeRequest, serverContext) {
180
174
  const request = normalizeNodeRequest(nodeRequest, RequestCtor);
181
175
  return handleRequest(request, serverContext);
@@ -209,27 +203,12 @@ RequestCtor = fetch.Request) {
209
203
  const response$ = handleRequest(event.request, event);
210
204
  event.respondWith(response$);
211
205
  }
212
- function genericRequestHandler(input, ctx = {}, ...rest) {
206
+ function handleRequestWithWaitUntil(request, ctx = {}, ...rest) {
213
207
  var _a;
214
208
  if ('process' in globalThis && ((_a = process.versions) === null || _a === void 0 ? void 0 : _a['bun']) != null) {
215
209
  // This is required for bun
216
- input.text();
217
- }
218
- // If it is a Node request
219
- if (isReadable(input) && ctx != null && isServerResponse(ctx)) {
220
- return requestListener(input, ctx);
210
+ request.text();
221
211
  }
222
- // Is input a container object over Request?
223
- if (input.request) {
224
- // Is it FetchEvent?
225
- if (input.respondWith) {
226
- return handleEvent(input);
227
- }
228
- // In this input is also the context
229
- return handleRequest(input.request, input);
230
- }
231
- // Or is it Request itself?
232
- // Then ctx is present and it is the context
233
212
  if ((rest === null || rest === void 0 ? void 0 : rest.length) > 0) {
234
213
  ctx = Object.assign({}, ctx, ...rest);
235
214
  }
@@ -238,7 +217,7 @@ RequestCtor = fetch.Request) {
238
217
  ctx.waitUntil = (p) => {
239
218
  waitUntilPromises.push(p);
240
219
  };
241
- const response$ = handleRequest(input, {
220
+ const response$ = handleRequest(request, {
242
221
  ...ctx,
243
222
  waitUntil(p) {
244
223
  waitUntilPromises.push(p);
@@ -247,8 +226,33 @@ RequestCtor = fetch.Request) {
247
226
  if (waitUntilPromises.length > 0) {
248
227
  return handleWaitUntils(waitUntilPromises).then(() => response$);
249
228
  }
229
+ return response$;
230
+ }
231
+ return handleRequest(request, ctx);
232
+ }
233
+ function genericRequestHandler(input, ctx, ...rest) {
234
+ // If it is a Node request
235
+ if (isReadable(input) && ctx != null && isServerResponse(ctx)) {
236
+ return requestListener(input, ctx);
237
+ }
238
+ // Is input a container object over Request?
239
+ if (input.request) {
240
+ // Is it FetchEvent?
241
+ if (input.respondWith) {
242
+ return handleEvent(input);
243
+ }
244
+ // In this input is also the context
245
+ return handleRequestWithWaitUntil(input.request, input, ...rest);
246
+ }
247
+ // Or is it Request itself?
248
+ // Then ctx is present and it is the context
249
+ return handleRequestWithWaitUntil(input, ctx, ...rest);
250
+ }
251
+ function fetchFn(input, init, ...ctx) {
252
+ if (typeof input === 'string' || input instanceof URL) {
253
+ return handleRequestWithWaitUntil(new RequestCtor(input, init), ...ctx);
250
254
  }
251
- return handleRequest(input, ctx);
255
+ return handleRequestWithWaitUntil(input, init, ...ctx);
252
256
  }
253
257
  const adapterObj = {
254
258
  handleRequest,
package/index.mjs CHANGED
@@ -166,12 +166,6 @@ function createServerAdapter(serverAdapterBaseObject,
166
166
  */
167
167
  RequestCtor = Request) {
168
168
  const handleRequest = typeof serverAdapterBaseObject === 'function' ? serverAdapterBaseObject : serverAdapterBaseObject.handle;
169
- function fetchFn(input, init, ...ctx) {
170
- if (typeof input === 'string' || input instanceof URL) {
171
- return handleRequest(new RequestCtor(input, init), Object.assign({}, ...ctx));
172
- }
173
- return handleRequest(input, Object.assign({}, init, ...ctx));
174
- }
175
169
  function handleNodeRequest(nodeRequest, serverContext) {
176
170
  const request = normalizeNodeRequest(nodeRequest, RequestCtor);
177
171
  return handleRequest(request, serverContext);
@@ -205,27 +199,12 @@ RequestCtor = Request) {
205
199
  const response$ = handleRequest(event.request, event);
206
200
  event.respondWith(response$);
207
201
  }
208
- function genericRequestHandler(input, ctx = {}, ...rest) {
202
+ function handleRequestWithWaitUntil(request, ctx = {}, ...rest) {
209
203
  var _a;
210
204
  if ('process' in globalThis && ((_a = process.versions) === null || _a === void 0 ? void 0 : _a['bun']) != null) {
211
205
  // This is required for bun
212
- input.text();
213
- }
214
- // If it is a Node request
215
- if (isReadable(input) && ctx != null && isServerResponse(ctx)) {
216
- return requestListener(input, ctx);
206
+ request.text();
217
207
  }
218
- // Is input a container object over Request?
219
- if (input.request) {
220
- // Is it FetchEvent?
221
- if (input.respondWith) {
222
- return handleEvent(input);
223
- }
224
- // In this input is also the context
225
- return handleRequest(input.request, input);
226
- }
227
- // Or is it Request itself?
228
- // Then ctx is present and it is the context
229
208
  if ((rest === null || rest === void 0 ? void 0 : rest.length) > 0) {
230
209
  ctx = Object.assign({}, ctx, ...rest);
231
210
  }
@@ -234,7 +213,7 @@ RequestCtor = Request) {
234
213
  ctx.waitUntil = (p) => {
235
214
  waitUntilPromises.push(p);
236
215
  };
237
- const response$ = handleRequest(input, {
216
+ const response$ = handleRequest(request, {
238
217
  ...ctx,
239
218
  waitUntil(p) {
240
219
  waitUntilPromises.push(p);
@@ -243,8 +222,33 @@ RequestCtor = Request) {
243
222
  if (waitUntilPromises.length > 0) {
244
223
  return handleWaitUntils(waitUntilPromises).then(() => response$);
245
224
  }
225
+ return response$;
226
+ }
227
+ return handleRequest(request, ctx);
228
+ }
229
+ function genericRequestHandler(input, ctx, ...rest) {
230
+ // If it is a Node request
231
+ if (isReadable(input) && ctx != null && isServerResponse(ctx)) {
232
+ return requestListener(input, ctx);
233
+ }
234
+ // Is input a container object over Request?
235
+ if (input.request) {
236
+ // Is it FetchEvent?
237
+ if (input.respondWith) {
238
+ return handleEvent(input);
239
+ }
240
+ // In this input is also the context
241
+ return handleRequestWithWaitUntil(input.request, input, ...rest);
242
+ }
243
+ // Or is it Request itself?
244
+ // Then ctx is present and it is the context
245
+ return handleRequestWithWaitUntil(input, ctx, ...rest);
246
+ }
247
+ function fetchFn(input, init, ...ctx) {
248
+ if (typeof input === 'string' || input instanceof URL) {
249
+ return handleRequestWithWaitUntil(new RequestCtor(input, init), ...ctx);
246
250
  }
247
- return handleRequest(input, ctx);
251
+ return handleRequestWithWaitUntil(input, init, ...ctx);
248
252
  }
249
253
  const adapterObj = {
250
254
  handleRequest,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/server",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Fetch API compliant HTTP Server adapter",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {