rekwest 3.1.0 → 3.3.0

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.
@@ -1,14 +1,14 @@
1
- import { Blob } from 'buffer';
2
- import http2 from 'http2';
1
+ import { Blob } from 'node:buffer';
2
+ import http2 from 'node:http2';
3
3
  import {
4
4
  PassThrough,
5
5
  Readable,
6
- } from 'stream';
6
+ } from 'node:stream';
7
7
  import {
8
8
  promisify,
9
9
  types,
10
- } from 'util';
11
- import zlib from 'zlib';
10
+ } from 'node:util';
11
+ import zlib from 'node:zlib';
12
12
  import { Cookies } from './cookies.mjs';
13
13
  import { TimeoutError } from './errors.mjs';
14
14
  import { File } from './file.mjs';
@@ -86,6 +86,12 @@ export const affix = (client, req, options) => {
86
86
  });
87
87
  };
88
88
 
89
+ export const collate = (entity, primordial) => {
90
+ if (!(entity instanceof primordial)) {
91
+ throw new TypeError('Illegal invocation');
92
+ }
93
+ };
94
+
89
95
  export const compress = (buf, encoding, { async = false } = {}) => {
90
96
  encoding &&= encoding.match(/(?<encoding>\bbr\b|\bdeflate\b|\bgzip\b)/i)?.groups.encoding.toLowerCase();
91
97
  const compressor = {
@@ -163,7 +169,8 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
163
169
  Object.defineProperties(res, {
164
170
  arrayBuffer: {
165
171
  enumerable: true,
166
- value: function () {
172
+ value() {
173
+ collate(this, res?.constructor);
167
174
  parse &&= false;
168
175
 
169
176
  return this.body().then(({ buffer, byteLength, byteOffset }) => buffer.slice(
@@ -174,19 +181,25 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
174
181
  },
175
182
  blob: {
176
183
  enumerable: true,
177
- value: function () {
184
+ value() {
185
+ collate(this, res?.constructor);
186
+
178
187
  return this.arrayBuffer().then((res) => new Blob([res]));
179
188
  },
180
189
  },
181
190
  json: {
182
191
  enumerable: true,
183
- value: function () {
192
+ value() {
193
+ collate(this, res?.constructor);
194
+
184
195
  return this.text().then((res) => JSON.parse(res));
185
196
  },
186
197
  },
187
198
  text: {
188
199
  enumerable: true,
189
- value: function () {
200
+ value() {
201
+ collate(this, res?.constructor);
202
+
190
203
  return this.blob().then((blob) => blob.text());
191
204
  },
192
205
  },
@@ -197,8 +210,10 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
197
210
  body: {
198
211
  enumerable: true,
199
212
  value: async function () {
213
+ collate(this, res?.constructor);
214
+
200
215
  if (this.bodyUsed) {
201
- throw new TypeError('Response stream already read.');
216
+ throw new TypeError('Response stream already read');
202
217
  }
203
218
 
204
219
  let spool = [];
@@ -239,7 +254,7 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
239
254
  },
240
255
  bodyUsed: {
241
256
  enumerable: true,
242
- get: function () {
257
+ get() {
243
258
  return this.readableEnded;
244
259
  },
245
260
  },
@@ -247,8 +262,7 @@ export const mixin = (res, { digest = false, parse = false } = {}) => {
247
262
  };
248
263
 
249
264
  export const preflight = (options) => {
250
- const url = options.url = new URL(options.url);
251
- const { cookies, h2 = false, method = HTTP2_METHOD_GET, headers, redirected } = options;
265
+ const { cookies, h2 = false, headers, method = HTTP2_METHOD_GET, redirected, url } = options;
252
266
 
253
267
  if (h2) {
254
268
  options.endStream = [
@@ -316,6 +330,16 @@ export const redirects = {
316
330
  manual: 'manual',
317
331
  };
318
332
 
333
+ export const revise = ({ url, options }) => {
334
+ if (options.trimTrailingSlashes) {
335
+ url = `${ url }`.replace(/(?<!:)\/+/gi, '/');
336
+ }
337
+
338
+ url = new URL(url);
339
+
340
+ return Object.assign(options, { url });
341
+ };
342
+
319
343
  export async function* tap(value) {
320
344
  if (Reflect.has(value, Symbol.asyncIterator)) {
321
345
  yield* value;