@whatwg-node/server 0.5.4 → 0.5.6-alpha-20230109165738-8edf269

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/README.md CHANGED
@@ -242,16 +242,14 @@ You can learn more about [File API](https://developer.mozilla.org/en-US/docs/Web
242
242
 
243
243
  ## Routing and Middlewares
244
244
 
245
- We'd recommend to use [itty-router](https://github.com/kwhitley/itty-router) to handle routing and middleware approach. So it is really easy to integrate your router to `@whatwg-node/server`.
245
+ We'd recommend to use `@whatwg-node/router` to handle routing and middleware approach. It uses `@whatwg-node/server` under the hood.
246
246
 
247
247
  ### Basic Routing
248
248
 
249
249
  ```ts
250
- import { Router } from 'itty-router'
251
- import { createServerAdapter } from '@whatwg-node/server'
250
+ import { createRouter, Router } from '@whatwg-node/router'
252
251
 
253
- // now let's create a router (note the lack of "new")
254
- const router = Router()
252
+ const router = createRouter()
255
253
  // GET collection index
256
254
  router.get('/todos', () => new Response('Todos Index!'))
257
255
  // GET item
@@ -268,21 +266,18 @@ router.get('/google', () => Response.redirect('http://www.google.com'))
268
266
  // 404 for everything else
269
267
  router.all('*', () => new Response('Not Found.', { status: 404 }))
270
268
 
271
- // attach the router to our server adapter
272
- const myServerAdapter = createServerAdapter(router)
273
-
274
269
  // Then use it in any environment
275
270
  import { createServer } from 'http'
276
- const httpServer = createServer(myServer)
271
+ const httpServer = createServer(router)
277
272
  httpServer.listen(4000)
278
273
  ```
279
274
 
280
275
  ### Middlewares to handle CORS, cookies and more
281
276
 
282
- There is another package called [itty-router-extras](https://www.npmjs.com/package/itty-router-extras) that provides some utilities for your platform agnostic server implementation. The following example shows how to get the cookies as an object from the request.
277
+ This package also provides some utilities for your platform agnostic server implementation. The following example shows how to get the cookies as an object from the request.
283
278
 
284
279
  ```ts
285
- import { withCookies } from 'itty-router-extras'
280
+ import { withCookies } from '@whatwg-node/server'
286
281
 
287
282
  router.get('/foo', withCookies, ({ cookies }) => {
288
283
  // cookies are parsed from the header into request.cookies
@@ -293,15 +288,12 @@ router.get('/foo', withCookies, ({ cookies }) => {
293
288
  You can also setup a CORS middleware to handle preflight CORS requests.
294
289
 
295
290
  ```ts
296
- import { withCors } from 'itty-router-extras'
297
-
298
- router.all(
299
- '*',
300
- withCors({
301
- origin: 'http://localhost:4000',
302
- methods: 'GET, POST, PATCH, DELETE',
303
- headers: 'authorization, referer, origin, content-type',
304
- credentials: false
305
- })
306
- )
291
+ import { withCors } from '@whatwg-node/server'
292
+
293
+ const corsWithRouter = withCors(router, {
294
+ origin: 'http://localhost:4000',
295
+ methods: 'GET, POST, PATCH, DELETE',
296
+ headers: 'authorization, referer, origin, content-type',
297
+ credentials: false
298
+ })
307
299
  ```
package/index.js CHANGED
@@ -12,22 +12,26 @@ function getPort(nodeRequest) {
12
12
  if ((_a = nodeRequest.socket) === null || _a === void 0 ? void 0 : _a.localPort) {
13
13
  return (_b = nodeRequest.socket) === null || _b === void 0 ? void 0 : _b.localPort;
14
14
  }
15
- const portInHeader = (_e = (_d = (_c = nodeRequest.headers) === null || _c === void 0 ? void 0 : _c.host) === null || _d === void 0 ? void 0 : _d.split(':')) === null || _e === void 0 ? void 0 : _e[1];
15
+ const hostInHeader = ((_c = nodeRequest.headers) === null || _c === void 0 ? void 0 : _c[':authority']) || ((_d = nodeRequest.headers) === null || _d === void 0 ? void 0 : _d.host);
16
+ const portInHeader = (_e = hostInHeader === null || hostInHeader === void 0 ? void 0 : hostInHeader.split(':')) === null || _e === void 0 ? void 0 : _e[1];
16
17
  if (portInHeader) {
17
18
  return portInHeader;
18
19
  }
19
20
  return 80;
20
21
  }
21
22
  function getHostnameWithPort(nodeRequest) {
22
- var _a, _b, _c;
23
- if ((_a = nodeRequest.headers) === null || _a === void 0 ? void 0 : _a.host) {
24
- return (_b = nodeRequest.headers) === null || _b === void 0 ? void 0 : _b.host;
23
+ var _a, _b, _c, _d, _e;
24
+ if ((_a = nodeRequest.headers) === null || _a === void 0 ? void 0 : _a[':authority']) {
25
+ return (_b = nodeRequest.headers) === null || _b === void 0 ? void 0 : _b[':authority'];
26
+ }
27
+ if ((_c = nodeRequest.headers) === null || _c === void 0 ? void 0 : _c.host) {
28
+ return (_d = nodeRequest.headers) === null || _d === void 0 ? void 0 : _d.host;
25
29
  }
26
30
  const port = getPort(nodeRequest);
27
31
  if (nodeRequest.hostname) {
28
32
  return nodeRequest.hostname + ':' + port;
29
33
  }
30
- const localIp = (_c = nodeRequest.socket) === null || _c === void 0 ? void 0 : _c.localAddress;
34
+ const localIp = (_e = nodeRequest.socket) === null || _e === void 0 ? void 0 : _e.localAddress;
31
35
  if (localIp && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('::')) && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('ffff'))) {
32
36
  return `${localIp}:${port}`;
33
37
  }
@@ -210,8 +214,10 @@ RequestCtor = fetch.Request) {
210
214
  const defaultServerContext = {
211
215
  req: nodeRequest,
212
216
  res: serverResponse,
213
- waitUntil(p) {
214
- waitUntilPromises.push(p);
217
+ waitUntil(promise) {
218
+ if (promise != null) {
219
+ waitUntilPromises.push(promise);
220
+ }
215
221
  },
216
222
  };
217
223
  const response = await handleNodeRequest(nodeRequest, defaultServerContext, ...ctx);
@@ -243,8 +249,10 @@ RequestCtor = fetch.Request) {
243
249
  const waitUntilPromises = [];
244
250
  const response$ = handleRequest(request, {
245
251
  ...serverContext,
246
- waitUntil(p) {
247
- waitUntilPromises.push(p);
252
+ waitUntil(promise) {
253
+ if (promise != null) {
254
+ waitUntilPromises.push(promise);
255
+ }
248
256
  },
249
257
  });
250
258
  if (waitUntilPromises.length > 0) {
package/index.mjs CHANGED
@@ -9,22 +9,26 @@ function getPort(nodeRequest) {
9
9
  if ((_a = nodeRequest.socket) === null || _a === void 0 ? void 0 : _a.localPort) {
10
10
  return (_b = nodeRequest.socket) === null || _b === void 0 ? void 0 : _b.localPort;
11
11
  }
12
- const portInHeader = (_e = (_d = (_c = nodeRequest.headers) === null || _c === void 0 ? void 0 : _c.host) === null || _d === void 0 ? void 0 : _d.split(':')) === null || _e === void 0 ? void 0 : _e[1];
12
+ const hostInHeader = ((_c = nodeRequest.headers) === null || _c === void 0 ? void 0 : _c[':authority']) || ((_d = nodeRequest.headers) === null || _d === void 0 ? void 0 : _d.host);
13
+ const portInHeader = (_e = hostInHeader === null || hostInHeader === void 0 ? void 0 : hostInHeader.split(':')) === null || _e === void 0 ? void 0 : _e[1];
13
14
  if (portInHeader) {
14
15
  return portInHeader;
15
16
  }
16
17
  return 80;
17
18
  }
18
19
  function getHostnameWithPort(nodeRequest) {
19
- var _a, _b, _c;
20
- if ((_a = nodeRequest.headers) === null || _a === void 0 ? void 0 : _a.host) {
21
- return (_b = nodeRequest.headers) === null || _b === void 0 ? void 0 : _b.host;
20
+ var _a, _b, _c, _d, _e;
21
+ if ((_a = nodeRequest.headers) === null || _a === void 0 ? void 0 : _a[':authority']) {
22
+ return (_b = nodeRequest.headers) === null || _b === void 0 ? void 0 : _b[':authority'];
23
+ }
24
+ if ((_c = nodeRequest.headers) === null || _c === void 0 ? void 0 : _c.host) {
25
+ return (_d = nodeRequest.headers) === null || _d === void 0 ? void 0 : _d.host;
22
26
  }
23
27
  const port = getPort(nodeRequest);
24
28
  if (nodeRequest.hostname) {
25
29
  return nodeRequest.hostname + ':' + port;
26
30
  }
27
- const localIp = (_c = nodeRequest.socket) === null || _c === void 0 ? void 0 : _c.localAddress;
31
+ const localIp = (_e = nodeRequest.socket) === null || _e === void 0 ? void 0 : _e.localAddress;
28
32
  if (localIp && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('::')) && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('ffff'))) {
29
33
  return `${localIp}:${port}`;
30
34
  }
@@ -207,8 +211,10 @@ RequestCtor = Request) {
207
211
  const defaultServerContext = {
208
212
  req: nodeRequest,
209
213
  res: serverResponse,
210
- waitUntil(p) {
211
- waitUntilPromises.push(p);
214
+ waitUntil(promise) {
215
+ if (promise != null) {
216
+ waitUntilPromises.push(promise);
217
+ }
212
218
  },
213
219
  };
214
220
  const response = await handleNodeRequest(nodeRequest, defaultServerContext, ...ctx);
@@ -240,8 +246,10 @@ RequestCtor = Request) {
240
246
  const waitUntilPromises = [];
241
247
  const response$ = handleRequest(request, {
242
248
  ...serverContext,
243
- waitUntil(p) {
244
- waitUntilPromises.push(p);
249
+ waitUntil(promise) {
250
+ if (promise != null) {
251
+ waitUntilPromises.push(promise);
252
+ }
245
253
  },
246
254
  });
247
255
  if (waitUntilPromises.length > 0) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@whatwg-node/server",
3
- "version": "0.5.4",
3
+ "version": "0.5.6-alpha-20230109165738-8edf269",
4
4
  "description": "Fetch API compliant HTTP Server adapter",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
7
  "@types/node": "^18.0.6"
8
8
  },
9
9
  "dependencies": {
10
- "@whatwg-node/fetch": "0.6.1",
10
+ "@whatwg-node/fetch": "0.6.2-alpha-20230109165738-8edf269",
11
11
  "tslib": "^2.3.1"
12
12
  },
13
13
  "repository": {
package/types.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import type { RequestListener } from 'node:http';
3
3
  import type { NodeRequest, NodeResponse } from './utils';
4
4
  export interface FetchEvent extends Event {
5
- waitUntil(f: Promise<any>): void;
5
+ waitUntil(f: Promise<void> | void): void;
6
6
  request: Request;
7
7
  respondWith(r: Response | PromiseLike<Response>): void;
8
8
  }
@@ -51,5 +51,5 @@ export type ServerAdapterRequestHandler<TServerContext> = (request: Request, ctx
51
51
  export type DefaultServerAdapterContext = {
52
52
  req: NodeRequest;
53
53
  res: NodeResponse;
54
- waitUntil(promise: Promise<unknown>): void;
54
+ waitUntil(promise: Promise<void> | void): void;
55
55
  };
package/utils.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  /// <reference types="node" />
3
3
  /// <reference types="node" />
4
4
  /// <reference types="node" />
5
- import { IncomingMessage, ServerResponse } from 'node:http';
5
+ import type { IncomingMessage, ServerResponse } from 'node:http';
6
6
  import type { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
7
7
  import type { Socket } from 'node:net';
8
8
  import type { Readable } from 'node:stream';