rekwest 7.0.0 → 7.0.1

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.
@@ -5,12 +5,11 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.preflight = void 0;
7
7
  var _nodeHttp = _interopRequireDefault(require("node:http2"));
8
- var _config = require("./config.cjs");
9
8
  var _constants = require("./constants.cjs");
10
9
  var _cookies = require("./cookies.cjs");
10
+ var _utils = require("./utils.cjs");
11
11
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
12
  const {
13
- HTTP2_HEADER_ACCEPT_ENCODING,
14
13
  HTTP2_HEADER_AUTHORITY,
15
14
  HTTP2_HEADER_AUTHORIZATION,
16
15
  HTTP2_HEADER_COOKIE,
@@ -21,7 +20,7 @@ const {
21
20
  HTTP2_METHOD_HEAD
22
21
  } = _nodeHttp.default.constants;
23
22
  const preflight = options => {
24
- const {
23
+ let {
25
24
  cookies,
26
25
  credentials,
27
26
  h2,
@@ -31,6 +30,13 @@ const preflight = options => {
31
30
  } = options;
32
31
  if (h2) {
33
32
  options.endStream = [HTTP2_METHOD_GET, HTTP2_METHOD_HEAD].includes(method);
33
+ } else {
34
+ headers = (0, _utils.stripHeaders)(headers, [HTTP2_HEADER_AUTHORITY, HTTP2_HEADER_METHOD, HTTP2_HEADER_PATH, HTTP2_HEADER_SCHEME]);
35
+ }
36
+ if (credentials === _constants.requestCredentials.omit) {
37
+ headers = (0, _utils.stripHeaders)(headers, [HTTP2_HEADER_AUTHORIZATION, HTTP2_HEADER_COOKIE]);
38
+ options.cookies = false;
39
+ url.password = url.username = '';
34
40
  }
35
41
  if (cookies !== false && credentials !== _constants.requestCredentials.omit) {
36
42
  let cookie = _cookies.Cookies.jar.has(url.origin);
@@ -57,26 +63,8 @@ const preflight = options => {
57
63
  ...headers
58
64
  };
59
65
  }
60
- if (credentials === _constants.requestCredentials.omit) {
61
- [HTTP2_HEADER_AUTHORIZATION, HTTP2_HEADER_COOKIE].forEach(key => Reflect.deleteProperty(options.headers, key));
62
- options.cookies = false;
63
- url.password = url.username = '';
64
- }
65
- if (!h2) {
66
- [HTTP2_HEADER_AUTHORITY, HTTP2_HEADER_METHOD, HTTP2_HEADER_PATH, HTTP2_HEADER_SCHEME].forEach(key => Reflect.deleteProperty(options.headers, key));
67
- }
68
66
  options.headers = {
69
- ...Object.entries(options.headers ?? {}).reduce((acc, [key, val]) => {
70
- acc[key.toLowerCase()] = val;
71
- const rex = /\s?zstd,?/gi;
72
- if (acc[HTTP2_HEADER_ACCEPT_ENCODING]?.match(rex) && !_config.isZstdSupported) {
73
- acc[HTTP2_HEADER_ACCEPT_ENCODING] = val.replace(rex, '').trim();
74
- if (!acc[HTTP2_HEADER_ACCEPT_ENCODING]) {
75
- Reflect.deleteProperty(acc, HTTP2_HEADER_ACCEPT_ENCODING);
76
- }
77
- }
78
- return acc;
79
- }, {}),
67
+ ...headers,
80
68
  ...(h2 && {
81
69
  [HTTP2_HEADER_AUTHORITY]: url.host,
82
70
  [HTTP2_HEADER_METHOD]: method,
package/dist/utils.cjs CHANGED
@@ -3,16 +3,18 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.snoop = exports.sameOrigin = exports.normalize = exports.merge = exports.isReadableStream = exports.isPipeStream = exports.isFileLike = exports.dispatch = exports.copyWithMerge = exports.brandCheck = exports.augment = void 0;
6
+ exports.stripHeaders = exports.snoop = exports.sameOrigin = exports.normalizeHeaders = exports.normalize = exports.merge = exports.isReadableStream = exports.isPipeStream = exports.isFileLike = exports.dispatch = exports.copyWithMerge = exports.brandCheck = exports.augment = void 0;
7
7
  exports.tap = tap;
8
8
  exports.unwind = exports.toCamelCase = void 0;
9
9
  var _nodeBuffer = require("node:buffer");
10
10
  var _nodeHttp = _interopRequireDefault(require("node:http2"));
11
11
  var _nodeStream = require("node:stream");
12
- var _config = _interopRequireDefault(require("./config.cjs"));
12
+ var _config = _interopRequireWildcard(require("./config.cjs"));
13
13
  var _errors = require("./errors.cjs");
14
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
14
15
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
16
  const {
17
+ HTTP2_HEADER_ACCEPT_ENCODING,
16
18
  HTTP2_HEADER_STATUS
17
19
  } = _nodeHttp.default.constants;
18
20
  const augment = (res, headers, options) => {
@@ -105,12 +107,30 @@ const normalize = (url, options = {}) => {
105
107
  if (options.stripTrailingSlash) {
106
108
  url = `${url}`.replace(/\/$|\/(?=#)|\/(?=\?)/g, '');
107
109
  }
108
- url = new URL(url, options.baseURL);
109
110
  return Object.assign(options, {
110
- url
111
+ headers: normalizeHeaders(options.headers),
112
+ method: options.method.toUpperCase(),
113
+ url: new URL(url, options.baseURL)
111
114
  });
112
115
  };
113
116
  exports.normalize = normalize;
117
+ const normalizeHeaders = headers => {
118
+ const collector = {};
119
+ for (const [key, value] of Object.entries(headers ?? {})) {
120
+ const name = key.toLowerCase();
121
+ collector[key] = value;
122
+ if (key === HTTP2_HEADER_ACCEPT_ENCODING && !_config.isZstdSupported) {
123
+ const stripped = value.replace(/\s?zstd,?/gi, '').trim();
124
+ if (stripped) {
125
+ collector[key] = stripped;
126
+ } else {
127
+ Reflect.deleteProperty(collector, name);
128
+ }
129
+ }
130
+ }
131
+ return collector;
132
+ };
133
+ exports.normalizeHeaders = normalizeHeaders;
114
134
  const sameOrigin = (a, b) => a.protocol === b.protocol && a.hostname === b.hostname && a.port === b.port;
115
135
  exports.sameOrigin = sameOrigin;
116
136
  const snoop = (client, req, options) => {
@@ -125,6 +145,11 @@ const snoop = (client, req, options) => {
125
145
  });
126
146
  };
127
147
  exports.snoop = snoop;
148
+ const stripHeaders = (headers = {}, names = []) => {
149
+ names = new Set(names);
150
+ return Object.fromEntries(Object.entries(headers).filter(([key]) => !names.has(key.toLowerCase())));
151
+ };
152
+ exports.stripHeaders = stripHeaders;
128
153
  async function* tap(value) {
129
154
  if (Reflect.has(value, Symbol.asyncIterator)) {
130
155
  yield* value;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": {
3
- "name": "Yehor Sergeenko",
4
3
  "email": "yehor.sergeenko@gmail.com",
4
+ "name": "Yehor Sergeenko",
5
5
  "url": "https://github.com/bricss"
6
6
  },
7
7
  "bugs": {
@@ -72,5 +72,5 @@
72
72
  "test:cover": "c8 --include=src --reporter=lcov --reporter=text npm test"
73
73
  },
74
74
  "type": "module",
75
- "version": "7.0.0"
75
+ "version": "7.0.1"
76
76
  }
package/src/preflight.js CHANGED
@@ -1,10 +1,9 @@
1
1
  import http2 from 'node:http2';
2
- import { isZstdSupported } from './config.js';
3
2
  import { requestCredentials } from './constants.js';
4
3
  import { Cookies } from './cookies.js';
4
+ import { stripHeaders } from './utils.js';
5
5
 
6
6
  const {
7
- HTTP2_HEADER_ACCEPT_ENCODING,
8
7
  HTTP2_HEADER_AUTHORITY,
9
8
  HTTP2_HEADER_AUTHORIZATION,
10
9
  HTTP2_HEADER_COOKIE,
@@ -16,13 +15,29 @@ const {
16
15
  } = http2.constants;
17
16
 
18
17
  export const preflight = (options) => {
19
- const { cookies, credentials, h2, headers, method, url } = options;
18
+ let { cookies, credentials, h2, headers, method, url } = options;
20
19
 
21
20
  if (h2) {
22
21
  options.endStream = [
23
22
  HTTP2_METHOD_GET,
24
23
  HTTP2_METHOD_HEAD,
25
24
  ].includes(method);
25
+ } else {
26
+ headers = stripHeaders(headers, [
27
+ HTTP2_HEADER_AUTHORITY,
28
+ HTTP2_HEADER_METHOD,
29
+ HTTP2_HEADER_PATH,
30
+ HTTP2_HEADER_SCHEME,
31
+ ]);
32
+ }
33
+
34
+ if (credentials === requestCredentials.omit) {
35
+ headers = stripHeaders(headers, [
36
+ HTTP2_HEADER_AUTHORIZATION,
37
+ HTTP2_HEADER_COOKIE,
38
+ ]);
39
+ options.cookies = false;
40
+ url.password = url.username = '';
26
41
  }
27
42
 
28
43
  if (cookies !== false && credentials !== requestCredentials.omit) {
@@ -55,39 +70,8 @@ export const preflight = (options) => {
55
70
  };
56
71
  }
57
72
 
58
- if (credentials === requestCredentials.omit) {
59
- [
60
- HTTP2_HEADER_AUTHORIZATION,
61
- HTTP2_HEADER_COOKIE,
62
- ].forEach((key) => Reflect.deleteProperty(options.headers, key));
63
- options.cookies = false;
64
- url.password = url.username = '';
65
- }
66
-
67
- if (!h2) {
68
- [
69
- HTTP2_HEADER_AUTHORITY,
70
- HTTP2_HEADER_METHOD,
71
- HTTP2_HEADER_PATH,
72
- HTTP2_HEADER_SCHEME,
73
- ].forEach((key) => Reflect.deleteProperty(options.headers, key));
74
- }
75
-
76
73
  options.headers = {
77
- ...Object.entries(options.headers ?? {})
78
- .reduce((acc, [key, val]) => {
79
- acc[key.toLowerCase()] = val;
80
- const rex = /\s?zstd,?/gi;
81
-
82
- if (acc[HTTP2_HEADER_ACCEPT_ENCODING]?.match(rex) && !isZstdSupported) {
83
- acc[HTTP2_HEADER_ACCEPT_ENCODING] = val.replace(rex, '').trim();
84
- if (!acc[HTTP2_HEADER_ACCEPT_ENCODING]) {
85
- Reflect.deleteProperty(acc, HTTP2_HEADER_ACCEPT_ENCODING);
86
- }
87
- }
88
-
89
- return acc;
90
- }, {}),
74
+ ...headers,
91
75
  ...h2 && {
92
76
  [HTTP2_HEADER_AUTHORITY]: url.host,
93
77
  [HTTP2_HEADER_METHOD]: method,
package/src/utils.js CHANGED
@@ -7,10 +7,11 @@ import {
7
7
  isReadable,
8
8
  Readable,
9
9
  } from 'node:stream';
10
- import config from './config.js';
10
+ import config, { isZstdSupported } from './config.js';
11
11
  import { TimeoutError } from './errors.js';
12
12
 
13
13
  const {
14
+ HTTP2_HEADER_ACCEPT_ENCODING,
14
15
  HTTP2_HEADER_STATUS,
15
16
  } = http2.constants;
16
17
 
@@ -115,9 +116,33 @@ export const normalize = (url, options = {}) => {
115
116
  url = `${ url }`.replace(/\/$|\/(?=#)|\/(?=\?)/g, '');
116
117
  }
117
118
 
118
- url = new URL(url, options.baseURL);
119
+ return Object.assign(options, {
120
+ headers: normalizeHeaders(options.headers),
121
+ method: options.method.toUpperCase(),
122
+ url: new URL(url, options.baseURL),
123
+ });
124
+ };
125
+
126
+ export const normalizeHeaders = (headers) => {
127
+ const collector = {};
128
+
129
+ for (const [key, value] of Object.entries(headers ?? {})) {
130
+ const name = key.toLowerCase();
119
131
 
120
- return Object.assign(options, { url });
132
+ collector[key] = value;
133
+
134
+ if (key === HTTP2_HEADER_ACCEPT_ENCODING && !isZstdSupported) {
135
+ const stripped = value.replace(/\s?zstd,?/gi, '').trim();
136
+
137
+ if (stripped) {
138
+ collector[key] = stripped;
139
+ } else {
140
+ Reflect.deleteProperty(collector, name);
141
+ }
142
+ }
143
+ }
144
+
145
+ return collector;
121
146
  };
122
147
 
123
148
  export const sameOrigin = (a, b) => a.protocol === b.protocol && a.hostname === b.hostname && a.port === b.port;
@@ -134,6 +159,16 @@ export const snoop = (client, req, options) => {
134
159
  });
135
160
  };
136
161
 
162
+ export const stripHeaders = (headers = {}, names = []) => {
163
+ names = new Set(names);
164
+
165
+ return Object.fromEntries(
166
+ Object.entries(headers).filter(
167
+ ([key]) => !names.has(key.toLowerCase()),
168
+ ),
169
+ );
170
+ };
171
+
137
172
  export async function* tap(value) {
138
173
  if (Reflect.has(value, Symbol.asyncIterator)) {
139
174
  yield* value;