@whatwg-node/server 0.0.4 → 0.1.0-alpha-20220811084127-98f74ee

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/index.d.ts +2 -1
  2. package/index.js +39 -15
  3. package/index.mjs +39 -15
  4. package/package.json +2 -2
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference lib="webworker" />
2
3
  import type { RequestListener, ServerResponse } from 'node:http';
3
4
  import { NodeRequest } from './utils';
4
5
  import { fetch } from '@whatwg-node/fetch';
@@ -25,7 +26,7 @@ export interface ServerAdapterObject<TServerContext> extends EventListenerObject
25
26
  /**
26
27
  * WHATWG Fetch spec compliant `fetch` function that can be used for testing purposes.
27
28
  */
28
- fetch: typeof fetch;
29
+ fetch: typeof fetch | ((request: Request, ...ctx: any[]) => Promise<Response>);
29
30
  /**
30
31
  * This function takes Node's request object and returns a WHATWG Fetch spec compliant `Response` object.
31
32
  **/
package/index.js CHANGED
@@ -7,16 +7,37 @@ const fetch = require('@whatwg-node/fetch');
7
7
  function isAsyncIterable(body) {
8
8
  return body != null && typeof body === 'object' && typeof body[Symbol.asyncIterator] === 'function';
9
9
  }
10
+ function getPort(nodeRequest) {
11
+ var _a, _b, _c, _d, _e;
12
+ if ((_a = nodeRequest.socket) === null || _a === void 0 ? void 0 : _a.localPort) {
13
+ return (_b = nodeRequest.socket) === null || _b === void 0 ? void 0 : _b.localPort;
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];
16
+ if (portInHeader) {
17
+ return portInHeader;
18
+ }
19
+ return 80;
20
+ }
21
+ 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;
25
+ }
26
+ const port = getPort(nodeRequest);
27
+ if (nodeRequest.hostname) {
28
+ return nodeRequest.hostname + ':' + port;
29
+ }
30
+ const localIp = (_c = nodeRequest.socket) === null || _c === void 0 ? void 0 : _c.localAddress;
31
+ if (localIp && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('::')) && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('ffff'))) {
32
+ return `${localIp}:${port}`;
33
+ }
34
+ return 'localhost';
35
+ }
10
36
  function buildFullUrl(nodeRequest) {
11
- var _a, _b, _c, _d, _e, _f, _g, _h;
12
- const hostname = nodeRequest.hostname ||
13
- ((_e = (_d = (_c = (_b = (_a = nodeRequest.socket) === null || _a === void 0 ? void 0 : _a.localAddress) === null || _b === void 0 ? void 0 : _b.split('ffff')) === null || _c === void 0 ? void 0 : _c.join('')) === null || _d === void 0 ? void 0 : _d.split(':')) === null || _e === void 0 ? void 0 : _e.join('')) ||
14
- ((_g = (_f = nodeRequest.headers) === null || _f === void 0 ? void 0 : _f.host) === null || _g === void 0 ? void 0 : _g.split(':')[0]) ||
15
- 'localhost';
16
- const port = ((_h = nodeRequest.socket) === null || _h === void 0 ? void 0 : _h.localPort) || 80;
37
+ const hostnameWithPort = getHostnameWithPort(nodeRequest);
17
38
  const protocol = nodeRequest.protocol || 'http';
18
39
  const endpoint = nodeRequest.originalUrl || nodeRequest.url || '/graphql';
19
- return `${protocol}://${hostname}:${port}${endpoint}`;
40
+ return `${protocol}://${hostnameWithPort}${endpoint}`;
20
41
  }
21
42
  function configureSocket(rawRequest) {
22
43
  var _a, _b, _c, _d, _e, _f;
@@ -132,23 +153,26 @@ async function sendNodeResponse({ headers, status, statusText, body }, serverRes
132
153
  });
133
154
  }
134
155
 
156
+ /// <reference lib="webworker" />
135
157
  function createServerAdapter({ Request: RequestCtor = fetch.Request, handleRequest, baseObject, }) {
136
- function fetchFn(...[input, init]) {
137
- let request;
158
+ function fetchFn(input, init, ...ctx) {
138
159
  if (typeof input === 'string' || input instanceof URL) {
139
- request = new RequestCtor(input, init);
140
- }
141
- else {
142
- request = input;
160
+ return handleRequest(new RequestCtor(input, init), Object.assign({}, ...ctx));
143
161
  }
144
- return handleRequest(request, init);
162
+ return handleRequest(input, Object.assign({}, init, ...ctx));
145
163
  }
146
164
  function handleNodeRequest(nodeRequest, serverContext) {
147
165
  const request = normalizeNodeRequest(nodeRequest, RequestCtor);
148
166
  return handleRequest(request, serverContext);
149
167
  }
150
168
  async function requestListener(nodeRequest, serverResponse) {
151
- const response = await handleNodeRequest(nodeRequest, { req: nodeRequest, res: serverResponse });
169
+ const response = await handleNodeRequest(nodeRequest, {
170
+ req: nodeRequest,
171
+ res: serverResponse,
172
+ waitUntil(p) {
173
+ p.catch(err => console.error(err));
174
+ },
175
+ });
152
176
  return sendNodeResponse(response, serverResponse);
153
177
  }
154
178
  function handleEvent(event) {
package/index.mjs CHANGED
@@ -3,16 +3,37 @@ import { Request } from '@whatwg-node/fetch';
3
3
  function isAsyncIterable(body) {
4
4
  return body != null && typeof body === 'object' && typeof body[Symbol.asyncIterator] === 'function';
5
5
  }
6
+ function getPort(nodeRequest) {
7
+ var _a, _b, _c, _d, _e;
8
+ if ((_a = nodeRequest.socket) === null || _a === void 0 ? void 0 : _a.localPort) {
9
+ return (_b = nodeRequest.socket) === null || _b === void 0 ? void 0 : _b.localPort;
10
+ }
11
+ 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
+ if (portInHeader) {
13
+ return portInHeader;
14
+ }
15
+ return 80;
16
+ }
17
+ function getHostnameWithPort(nodeRequest) {
18
+ var _a, _b, _c;
19
+ if ((_a = nodeRequest.headers) === null || _a === void 0 ? void 0 : _a.host) {
20
+ return (_b = nodeRequest.headers) === null || _b === void 0 ? void 0 : _b.host;
21
+ }
22
+ const port = getPort(nodeRequest);
23
+ if (nodeRequest.hostname) {
24
+ return nodeRequest.hostname + ':' + port;
25
+ }
26
+ const localIp = (_c = nodeRequest.socket) === null || _c === void 0 ? void 0 : _c.localAddress;
27
+ if (localIp && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('::')) && !(localIp === null || localIp === void 0 ? void 0 : localIp.includes('ffff'))) {
28
+ return `${localIp}:${port}`;
29
+ }
30
+ return 'localhost';
31
+ }
6
32
  function buildFullUrl(nodeRequest) {
7
- var _a, _b, _c, _d, _e, _f, _g, _h;
8
- const hostname = nodeRequest.hostname ||
9
- ((_e = (_d = (_c = (_b = (_a = nodeRequest.socket) === null || _a === void 0 ? void 0 : _a.localAddress) === null || _b === void 0 ? void 0 : _b.split('ffff')) === null || _c === void 0 ? void 0 : _c.join('')) === null || _d === void 0 ? void 0 : _d.split(':')) === null || _e === void 0 ? void 0 : _e.join('')) ||
10
- ((_g = (_f = nodeRequest.headers) === null || _f === void 0 ? void 0 : _f.host) === null || _g === void 0 ? void 0 : _g.split(':')[0]) ||
11
- 'localhost';
12
- const port = ((_h = nodeRequest.socket) === null || _h === void 0 ? void 0 : _h.localPort) || 80;
33
+ const hostnameWithPort = getHostnameWithPort(nodeRequest);
13
34
  const protocol = nodeRequest.protocol || 'http';
14
35
  const endpoint = nodeRequest.originalUrl || nodeRequest.url || '/graphql';
15
- return `${protocol}://${hostname}:${port}${endpoint}`;
36
+ return `${protocol}://${hostnameWithPort}${endpoint}`;
16
37
  }
17
38
  function configureSocket(rawRequest) {
18
39
  var _a, _b, _c, _d, _e, _f;
@@ -128,23 +149,26 @@ async function sendNodeResponse({ headers, status, statusText, body }, serverRes
128
149
  });
129
150
  }
130
151
 
152
+ /// <reference lib="webworker" />
131
153
  function createServerAdapter({ Request: RequestCtor = Request, handleRequest, baseObject, }) {
132
- function fetchFn(...[input, init]) {
133
- let request;
154
+ function fetchFn(input, init, ...ctx) {
134
155
  if (typeof input === 'string' || input instanceof URL) {
135
- request = new RequestCtor(input, init);
136
- }
137
- else {
138
- request = input;
156
+ return handleRequest(new RequestCtor(input, init), Object.assign({}, ...ctx));
139
157
  }
140
- return handleRequest(request, init);
158
+ return handleRequest(input, Object.assign({}, init, ...ctx));
141
159
  }
142
160
  function handleNodeRequest(nodeRequest, serverContext) {
143
161
  const request = normalizeNodeRequest(nodeRequest, RequestCtor);
144
162
  return handleRequest(request, serverContext);
145
163
  }
146
164
  async function requestListener(nodeRequest, serverResponse) {
147
- const response = await handleNodeRequest(nodeRequest, { req: nodeRequest, res: serverResponse });
165
+ const response = await handleNodeRequest(nodeRequest, {
166
+ req: nodeRequest,
167
+ res: serverResponse,
168
+ waitUntil(p) {
169
+ p.catch(err => console.error(err));
170
+ },
171
+ });
148
172
  return sendNodeResponse(response, serverResponse);
149
173
  }
150
174
  function handleEvent(event) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@whatwg-node/server",
3
- "version": "0.0.4",
3
+ "version": "0.1.0-alpha-20220811084127-98f74ee",
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.1.0",
10
+ "@whatwg-node/fetch": "^0.2.0",
11
11
  "tslib": "^2.3.1"
12
12
  },
13
13
  "repository": {