@whatwg-node/server 0.0.6 → 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 +1 -1
  2. package/index.js +38 -15
  3. package/index.mjs +38 -15
  4. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -26,7 +26,7 @@ export interface ServerAdapterObject<TServerContext> extends EventListenerObject
26
26
  /**
27
27
  * WHATWG Fetch spec compliant `fetch` function that can be used for testing purposes.
28
28
  */
29
- fetch: typeof fetch;
29
+ fetch: typeof fetch | ((request: Request, ...ctx: any[]) => Promise<Response>);
30
30
  /**
31
31
  * This function takes Node's request object and returns a WHATWG Fetch spec compliant `Response` object.
32
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;
@@ -134,22 +155,24 @@ async function sendNodeResponse({ headers, status, statusText, body }, serverRes
134
155
 
135
156
  /// <reference lib="webworker" />
136
157
  function createServerAdapter({ Request: RequestCtor = fetch.Request, handleRequest, baseObject, }) {
137
- function fetchFn(...[input, init]) {
138
- let request;
158
+ function fetchFn(input, init, ...ctx) {
139
159
  if (typeof input === 'string' || input instanceof URL) {
140
- request = new RequestCtor(input, init);
141
- }
142
- else {
143
- request = input;
160
+ return handleRequest(new RequestCtor(input, init), Object.assign({}, ...ctx));
144
161
  }
145
- return handleRequest(request, init);
162
+ return handleRequest(input, Object.assign({}, init, ...ctx));
146
163
  }
147
164
  function handleNodeRequest(nodeRequest, serverContext) {
148
165
  const request = normalizeNodeRequest(nodeRequest, RequestCtor);
149
166
  return handleRequest(request, serverContext);
150
167
  }
151
168
  async function requestListener(nodeRequest, serverResponse) {
152
- 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
+ });
153
176
  return sendNodeResponse(response, serverResponse);
154
177
  }
155
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;
@@ -130,22 +151,24 @@ async function sendNodeResponse({ headers, status, statusText, body }, serverRes
130
151
 
131
152
  /// <reference lib="webworker" />
132
153
  function createServerAdapter({ Request: RequestCtor = Request, handleRequest, baseObject, }) {
133
- function fetchFn(...[input, init]) {
134
- let request;
154
+ function fetchFn(input, init, ...ctx) {
135
155
  if (typeof input === 'string' || input instanceof URL) {
136
- request = new RequestCtor(input, init);
137
- }
138
- else {
139
- request = input;
156
+ return handleRequest(new RequestCtor(input, init), Object.assign({}, ...ctx));
140
157
  }
141
- return handleRequest(request, init);
158
+ return handleRequest(input, Object.assign({}, init, ...ctx));
142
159
  }
143
160
  function handleNodeRequest(nodeRequest, serverContext) {
144
161
  const request = normalizeNodeRequest(nodeRequest, RequestCtor);
145
162
  return handleRequest(request, serverContext);
146
163
  }
147
164
  async function requestListener(nodeRequest, serverResponse) {
148
- 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
+ });
149
172
  return sendNodeResponse(response, serverResponse);
150
173
  }
151
174
  function handleEvent(event) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/server",
3
- "version": "0.0.6",
3
+ "version": "0.1.0-alpha-20220811084127-98f74ee",
4
4
  "description": "Fetch API compliant HTTP Server adapter",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {