rekwest 7.2.3 → 7.2.5

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/src/utils.js CHANGED
@@ -1,7 +1,4 @@
1
- import {
2
- Blob,
3
- File,
4
- } from 'node:buffer';
1
+ import { Blob } from 'node:buffer';
5
2
  import http2 from 'node:http2';
6
3
  import {
7
4
  isReadable,
@@ -30,7 +27,7 @@ export const addSearchParams = (url, params = {}) => {
30
27
  };
31
28
 
32
29
  export const augment = (res, headers, options) => {
33
- const { h2 } = options;
30
+ const h2 = /\bh2c?\b/i.test(res.session?.alpnProtocol);
34
31
 
35
32
  if (h2) {
36
33
  Reflect.defineProperty(res, 'headers', {
@@ -96,66 +93,48 @@ export const deepMerge = (target, ...rest) => {
96
93
 
97
94
  export const dispatch = (req, { body }) => {
98
95
  if (isReadable(body)) {
96
+ body.once('error', (err) => (req.session && req.emit('error', err) && req.destroy()) || req.destroy(err));
99
97
  body.pipe(req);
100
98
  } else {
101
99
  req.end(body);
102
100
  }
103
101
  };
104
102
 
105
- export const isFileLike = (val) => {
106
- return [
107
- Blob,
108
- File,
109
- ].some((it) => val instanceof it);
110
- };
103
+ export const isBlobLike = (val) => val instanceof Blob;
111
104
 
112
- export const isLikelyH2cPrefaceError = (err) => {
113
- return err.code === 'HPE_INVALID_CONSTANT';
114
- };
105
+ export const isLikelyH2cPrefaceError = (err) => err.code === 'HPE_INVALID_CONSTANT';
115
106
 
116
- export const isPipeStream = (val) => {
117
- return val instanceof Readable;
118
- };
107
+ export const isPipeStream = (val) => val instanceof Readable;
119
108
 
120
- export const isReadableStream = (val) => {
121
- return val instanceof ReadableStream;
122
- };
109
+ export const isReadableStream = (val) => val instanceof ReadableStream;
123
110
 
124
111
  export const normalize = (url, options = {}) => {
125
112
  if (!options.redirected) {
126
113
  options = cloneWith(config.defaults, options);
127
114
  }
128
115
 
129
- if (options.trimTrailingSlashes) {
130
- url = `${ url }`.replace(/(?<!:)\/+/g, '/');
131
- }
132
-
133
- if (options.stripTrailingSlash) {
134
- url = `${ url }`.replace(/\/$|\/(?=#)|\/(?=\?)/g, '');
135
- }
136
-
137
116
  return Object.assign(options, {
138
117
  headers: normalizeHeaders(options.headers),
139
- method: options.method.toUpperCase(),
140
- url: addSearchParams(new URL(url, options.baseURL), options.params),
118
+ method: options.method?.toUpperCase(),
119
+ url: addSearchParams(normalizeUrl(new URL(url, options.baseURL), options), options.params),
141
120
  });
142
121
  };
143
122
 
144
123
  export const normalizeHeaders = (headers = {}) => {
145
124
  const acc = {};
146
125
 
147
- for (const [key, val] of Object.entries(headers)) {
148
- const name = key.toLowerCase();
126
+ for (let [key, val] of Object.entries(headers)) {
127
+ key = key.toLowerCase();
149
128
 
150
129
  acc[key] = val;
151
130
 
152
131
  if (key === HTTP2_HEADER_ACCEPT_ENCODING && !isZstdSupported) {
153
- const modified = val.replace(/\s?zstd,?/gi, '').trim();
132
+ val = val.replace(/\s?zstd,?/gi, '').trim();
154
133
 
155
- if (modified) {
156
- acc[key] = modified;
134
+ if (val) {
135
+ acc[key] = val;
157
136
  } else {
158
- Reflect.deleteProperty(acc, name);
137
+ Reflect.deleteProperty(acc, key);
159
138
  }
160
139
  }
161
140
  }
@@ -163,11 +142,27 @@ export const normalizeHeaders = (headers = {}) => {
163
142
  return acc;
164
143
  };
165
144
 
166
- export const sameOrigin = (a, b) => a.protocol === b.protocol && a.hostname === b.hostname && a.port === b.port;
145
+ function normalizeUrl(url, { trimTrailingSlashes, stripTrailingSlash } = {}) {
146
+ if (trimTrailingSlashes) {
147
+ url.pathname = url.pathname.replace(/\/{2,}/g, '/');
148
+ }
149
+
150
+ if (stripTrailingSlash && url.pathname !== '/') {
151
+ url.pathname = url.pathname.replace(/\/$/, '');
152
+ }
153
+
154
+ return url;
155
+ }
167
156
 
168
- export const snoop = (client, req, options) => {
157
+ export const sameOrigin = (a, b) => a.origin === b.origin;
158
+
159
+ export const snoop = (client, req, options, { reject } = { reject: () => void 0 }) => {
160
+ req.once('aborted', reject);
169
161
  req.once('close', () => client?.close());
170
162
  req.once('end', () => client?.close());
163
+ req.once('error', reject);
164
+ req.once('frameError', reject);
165
+ req.once('goaway', reject);
171
166
  req.once('timeout', () => req.destroy(new TimeoutError(`Timed out after ${ options.timeout } ms`)));
172
167
  req.once('trailers', (trailers) => {
173
168
  Reflect.defineProperty(req, 'trailers', {
@@ -177,14 +172,10 @@ export const snoop = (client, req, options) => {
177
172
  });
178
173
  };
179
174
 
180
- export const stripHeaders = (headers = {}, names = []) => {
181
- names = new Set(names);
175
+ export const stripHeaders = (headers = {}, keys = []) => {
176
+ keys = new Set(keys);
182
177
 
183
- return Object.fromEntries(
184
- Object.entries(headers).filter(
185
- ([key]) => !names.has(key.toLowerCase()),
186
- ),
187
- );
178
+ return Object.fromEntries(Object.entries(headers).filter(([key]) => !keys.has(key)));
188
179
  };
189
180
 
190
181
  export async function* tap(val) {
@@ -192,8 +183,6 @@ export async function* tap(val) {
192
183
  yield* val;
193
184
  } else if (val.stream) {
194
185
  yield* val.stream();
195
- } else {
196
- yield await val.arrayBuffer();
197
186
  }
198
187
  }
199
188