@whatwg-node/node-fetch 0.0.1 → 0.0.2-alpha-20230202081836-d7810ac

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 (3) hide show
  1. package/index.js +23 -7
  2. package/index.mjs +25 -9
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -700,7 +700,12 @@ class PonyfillRequest extends PonyfillBody {
700
700
  this.credentials = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.credentials) || 'same-origin';
701
701
  this.headers = new PonyfillHeaders(requestInit === null || requestInit === void 0 ? void 0 : requestInit.headers);
702
702
  this.integrity = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.integrity) || '';
703
- this.keepalive = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.keepalive) || true;
703
+ let defaultKeepAlive = true;
704
+ const connectionInHeaders = this.headers.get('connection');
705
+ if (connectionInHeaders) {
706
+ defaultKeepAlive = connectionInHeaders.toLowerCase() === 'keep-alive';
707
+ }
708
+ this.keepalive = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.keepalive) != null ? requestInit.keepalive : defaultKeepAlive;
704
709
  this.method = ((_a = requestInit === null || requestInit === void 0 ? void 0 : requestInit.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'GET';
705
710
  this.mode = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.mode) || 'cors';
706
711
  this.redirect = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.redirect) || 'follow';
@@ -708,10 +713,8 @@ class PonyfillRequest extends PonyfillBody {
708
713
  this.referrerPolicy = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.referrerPolicy) || 'no-referrer';
709
714
  this.signal = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.signal) || new PonyfillAbortController().signal;
710
715
  this.url = url || '';
711
- if (this.keepalive) {
712
- if (!this.headers.has('connection')) {
713
- this.headers.set('connection', 'keep-alive');
714
- }
716
+ if (this.keepalive && !connectionInHeaders) {
717
+ this.headers.set('connection', 'keep-alive');
715
718
  }
716
719
  const contentTypeInHeaders = this.headers.get('content-type');
717
720
  if (!contentTypeInHeaders) {
@@ -814,6 +817,15 @@ function getRequestFnForProtocol(protocol) {
814
817
  }
815
818
  throw new Error(`Unsupported protocol: ${protocol}`);
816
819
  }
820
+ const AGENT_MAP = {
821
+ 'http:keepAlive': new http.Agent({ keepAlive: true }),
822
+ 'http:nonKeepAlive': new http.Agent({ keepAlive: false }),
823
+ 'https:keepAlive': new https.Agent({ keepAlive: true }),
824
+ 'https:nonKeepAlive': new https.Agent({ keepAlive: false }),
825
+ };
826
+ function getAgent(protocol, keepAlive) {
827
+ return AGENT_MAP[`${protocol}${keepAlive ? 'keepAlive' : 'nonKeepAlive'}`];
828
+ }
817
829
  const BASE64_SUFFIX = ';base64';
818
830
  function fetchPonyfill(info, init) {
819
831
  if (typeof info === 'string' || info instanceof URL) {
@@ -868,11 +880,15 @@ function fetchPonyfill(info, init) {
868
880
  reject(new PonyfillAbortError(reason));
869
881
  };
870
882
  fetchRequest.signal.addEventListener('abort', abortListener);
871
- const nodeRequest = requestFn(url, {
883
+ const agent = getAgent(url.protocol, fetchRequest.keepalive);
884
+ const requestOptions = {
872
885
  // signal: fetchRequest.signal will be added when v14 reaches EOL
873
886
  method: fetchRequest.method,
874
887
  headers: nodeHeaders,
875
- });
888
+ rejectUnauthorized: false,
889
+ agent,
890
+ };
891
+ const nodeRequest = requestFn(url, requestOptions);
876
892
  nodeRequest.once('response', nodeResponse => {
877
893
  if (nodeResponse.headers.location) {
878
894
  if (fetchRequest.redirect === 'error') {
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createReadStream } from 'fs';
2
- import { request as request$1 } from 'http';
3
- import { request } from 'https';
2
+ import { Agent, request as request$1 } from 'http';
3
+ import { Agent as Agent$1, request } from 'https';
4
4
  import { Readable } from 'stream';
5
5
  import { fileURLToPath } from 'url';
6
6
  import { EventTarget, CustomEvent } from '@whatwg-node/events';
@@ -694,7 +694,12 @@ class PonyfillRequest extends PonyfillBody {
694
694
  this.credentials = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.credentials) || 'same-origin';
695
695
  this.headers = new PonyfillHeaders(requestInit === null || requestInit === void 0 ? void 0 : requestInit.headers);
696
696
  this.integrity = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.integrity) || '';
697
- this.keepalive = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.keepalive) || true;
697
+ let defaultKeepAlive = true;
698
+ const connectionInHeaders = this.headers.get('connection');
699
+ if (connectionInHeaders) {
700
+ defaultKeepAlive = connectionInHeaders.toLowerCase() === 'keep-alive';
701
+ }
702
+ this.keepalive = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.keepalive) != null ? requestInit.keepalive : defaultKeepAlive;
698
703
  this.method = ((_a = requestInit === null || requestInit === void 0 ? void 0 : requestInit.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()) || 'GET';
699
704
  this.mode = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.mode) || 'cors';
700
705
  this.redirect = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.redirect) || 'follow';
@@ -702,10 +707,8 @@ class PonyfillRequest extends PonyfillBody {
702
707
  this.referrerPolicy = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.referrerPolicy) || 'no-referrer';
703
708
  this.signal = (requestInit === null || requestInit === void 0 ? void 0 : requestInit.signal) || new PonyfillAbortController().signal;
704
709
  this.url = url || '';
705
- if (this.keepalive) {
706
- if (!this.headers.has('connection')) {
707
- this.headers.set('connection', 'keep-alive');
708
- }
710
+ if (this.keepalive && !connectionInHeaders) {
711
+ this.headers.set('connection', 'keep-alive');
709
712
  }
710
713
  const contentTypeInHeaders = this.headers.get('content-type');
711
714
  if (!contentTypeInHeaders) {
@@ -808,6 +811,15 @@ function getRequestFnForProtocol(protocol) {
808
811
  }
809
812
  throw new Error(`Unsupported protocol: ${protocol}`);
810
813
  }
814
+ const AGENT_MAP = {
815
+ 'http:keepAlive': new Agent({ keepAlive: true }),
816
+ 'http:nonKeepAlive': new Agent({ keepAlive: false }),
817
+ 'https:keepAlive': new Agent$1({ keepAlive: true }),
818
+ 'https:nonKeepAlive': new Agent$1({ keepAlive: false }),
819
+ };
820
+ function getAgent(protocol, keepAlive) {
821
+ return AGENT_MAP[`${protocol}${keepAlive ? 'keepAlive' : 'nonKeepAlive'}`];
822
+ }
811
823
  const BASE64_SUFFIX = ';base64';
812
824
  function fetchPonyfill(info, init) {
813
825
  if (typeof info === 'string' || info instanceof URL) {
@@ -862,11 +874,15 @@ function fetchPonyfill(info, init) {
862
874
  reject(new PonyfillAbortError(reason));
863
875
  };
864
876
  fetchRequest.signal.addEventListener('abort', abortListener);
865
- const nodeRequest = requestFn(url, {
877
+ const agent = getAgent(url.protocol, fetchRequest.keepalive);
878
+ const requestOptions = {
866
879
  // signal: fetchRequest.signal will be added when v14 reaches EOL
867
880
  method: fetchRequest.method,
868
881
  headers: nodeHeaders,
869
- });
882
+ rejectUnauthorized: false,
883
+ agent,
884
+ };
885
+ const nodeRequest = requestFn(url, requestOptions);
870
886
  nodeRequest.once('response', nodeResponse => {
871
887
  if (nodeResponse.headers.location) {
872
888
  if (fetchRequest.redirect === 'error') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whatwg-node/node-fetch",
3
- "version": "0.0.1",
3
+ "version": "0.0.2-alpha-20230202081836-d7810ac",
4
4
  "description": "Fetch API implementation for Node",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {