rekwest 7.2.7 → 8.1.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.
package/src/mixin.js CHANGED
@@ -1,118 +1,118 @@
1
- import { isUtf8 } from 'node:buffer';
2
- import http2 from 'node:http2';
3
- import { buffer } from 'node:stream/consumers';
4
- import { MIMEType } from 'node:util';
5
- import { decode } from './codecs.js';
6
- import { brandCheck } from './utils.js';
7
-
8
- const {
9
- HTTP2_HEADER_CONTENT_ENCODING,
10
- HTTP2_HEADER_CONTENT_TYPE,
11
- } = http2.constants;
12
-
13
- export const mixin = (res, { decodersOptions, digest = false, parse = false } = {}) => {
14
- if (!digest) {
15
- Object.defineProperties(res, {
16
- arrayBuffer: {
17
- enumerable: true,
18
- value: async function () {
19
- brandCheck(this, res?.constructor);
20
- parse &&= false;
21
- const { buffer, byteLength, byteOffset } = await this.body();
22
-
23
- return buffer.slice(byteOffset, byteOffset + byteLength);
24
- },
25
- },
26
- blob: {
27
- enumerable: true,
28
- value: async function () {
29
- brandCheck(this, res?.constructor);
30
- const val = await this.arrayBuffer();
31
-
32
- return new Blob([val]);
33
- },
34
- },
35
- bytes: {
36
- enumerable: true,
37
- value: async function () {
38
- brandCheck(this, res?.constructor);
39
-
40
- return new Uint8Array(await this.arrayBuffer());
41
- },
42
- },
43
- json: {
44
- enumerable: true,
45
- value: async function () {
46
- brandCheck(this, res?.constructor);
47
- const val = await this.text();
48
-
49
- return JSON.parse(val);
50
- },
51
- },
52
- text: {
53
- enumerable: true,
54
- value: async function () {
55
- brandCheck(this, res?.constructor);
56
- const blob = await this.blob();
57
-
58
- return blob.text();
59
- },
60
- },
61
- });
62
- }
63
-
64
- return Object.defineProperties(res, {
65
- body: {
66
- enumerable: true,
67
- value: async function () {
68
- brandCheck(this, res?.constructor);
69
-
70
- if (this.bodyUsed) {
71
- throw new TypeError('Response stream already read');
72
- }
73
-
74
- let body = await buffer(decode(this, this.headers[HTTP2_HEADER_CONTENT_ENCODING], { decodersOptions }));
75
-
76
- if (!body.length && parse) {
77
- return null;
78
- }
79
-
80
- if (body.length && parse) {
81
- const contentType = this.headers[HTTP2_HEADER_CONTENT_TYPE] ?? '';
82
- let isTextual, mimeType;
83
-
84
- try {
85
- mimeType = contentType ? new MIMEType(contentType) : null;
86
- } finally {
87
- isTextual = mimeType && (
88
- mimeType.type === 'text'
89
- || mimeType.subtype.match(/\bcsv\b|\bjson\b|\bxml\b|\byaml\b/)
90
- || mimeType.essence.match(/\becmascript\b|\bjavascript\b|\bx-www-form-urlencoded\b/)
91
- );
92
- }
93
-
94
- if (isTextual) {
95
- if (/\bjson\b/.test(mimeType)) {
96
- body = JSON.parse(body.toString());
97
- } else if (isUtf8(body)) {
98
- body = body.toString();
99
- } else {
100
- const charset = mimeType.params.get('charset')?.toLowerCase() ?? 'utf-8';
101
-
102
- body = new TextDecoder(charset, { fatal: true }).decode(body);
103
- }
104
- }
105
- }
106
-
107
- return body;
108
- },
109
- writable: true,
110
- },
111
- bodyUsed: {
112
- enumerable: true,
113
- get() {
114
- return this.readableEnded;
115
- },
116
- },
117
- });
118
- };
1
+ import { isUtf8 } from 'node:buffer';
2
+ import http2 from 'node:http2';
3
+ import { buffer } from 'node:stream/consumers';
4
+ import { MIMEType } from 'node:util';
5
+ import { decode } from './codecs.js';
6
+ import { brandCheck } from './utils.js';
7
+
8
+ const {
9
+ HTTP2_HEADER_CONTENT_ENCODING,
10
+ HTTP2_HEADER_CONTENT_TYPE,
11
+ } = http2.constants;
12
+
13
+ export const mixin = (res, { decodersOptions, digest = false, parse = false } = {}) => {
14
+ if (!digest) {
15
+ Object.defineProperties(res, {
16
+ arrayBuffer: {
17
+ enumerable: true,
18
+ value: async function () {
19
+ brandCheck(this, res?.constructor);
20
+ parse &&= false;
21
+ const { buffer, byteLength, byteOffset } = await this.body();
22
+
23
+ return buffer.slice(byteOffset, byteOffset + byteLength);
24
+ },
25
+ },
26
+ blob: {
27
+ enumerable: true,
28
+ value: async function () {
29
+ brandCheck(this, res?.constructor);
30
+ const val = await this.arrayBuffer();
31
+
32
+ return new Blob([val]);
33
+ },
34
+ },
35
+ bytes: {
36
+ enumerable: true,
37
+ value: async function () {
38
+ brandCheck(this, res?.constructor);
39
+
40
+ return new Uint8Array(await this.arrayBuffer());
41
+ },
42
+ },
43
+ json: {
44
+ enumerable: true,
45
+ value: async function () {
46
+ brandCheck(this, res?.constructor);
47
+ const val = await this.text();
48
+
49
+ return JSON.parse(val);
50
+ },
51
+ },
52
+ text: {
53
+ enumerable: true,
54
+ value: async function () {
55
+ brandCheck(this, res?.constructor);
56
+ const blob = await this.blob();
57
+
58
+ return blob.text();
59
+ },
60
+ },
61
+ });
62
+ }
63
+
64
+ return Object.defineProperties(res, {
65
+ body: {
66
+ enumerable: true,
67
+ value: async function () {
68
+ brandCheck(this, res?.constructor);
69
+
70
+ if (this.bodyUsed) {
71
+ throw new TypeError('Response stream already read');
72
+ }
73
+
74
+ let body = await buffer(decode(this, this.headers[HTTP2_HEADER_CONTENT_ENCODING], { decodersOptions }));
75
+
76
+ if (!body.length && parse) {
77
+ return null;
78
+ }
79
+
80
+ if (body.length && parse) {
81
+ const contentType = this.headers[HTTP2_HEADER_CONTENT_TYPE] ?? '';
82
+ let isTextual, mimeType;
83
+
84
+ try {
85
+ mimeType = contentType ? new MIMEType(contentType) : null;
86
+ } finally {
87
+ isTextual = mimeType && (
88
+ mimeType.type === 'text'
89
+ || mimeType.subtype.match(/\bcsv\b|\bjson\b|\bxml\b|\byaml\b/)
90
+ || mimeType.essence.match(/\becmascript\b|\bjavascript\b|\bx-www-form-urlencoded\b/)
91
+ );
92
+ }
93
+
94
+ if (isTextual) {
95
+ if (/\bjson\b/.test(mimeType)) {
96
+ body = JSON.parse(body.toString());
97
+ } else if (isUtf8(body)) {
98
+ body = body.toString();
99
+ } else {
100
+ const charset = mimeType.params.get('charset')?.toLowerCase() ?? 'utf-8';
101
+
102
+ body = new TextDecoder(charset, { fatal: true }).decode(body);
103
+ }
104
+ }
105
+ }
106
+
107
+ return body;
108
+ },
109
+ writable: true,
110
+ },
111
+ bodyUsed: {
112
+ enumerable: true,
113
+ get() {
114
+ return this.readableEnded;
115
+ },
116
+ },
117
+ });
118
+ };
package/src/postflight.js CHANGED
@@ -1,64 +1,64 @@
1
- import http2 from 'node:http2';
2
- import { Cookies } from './cookies.js';
3
- import { mixin } from './mixin.js';
4
- import { redirects } from './redirects.js';
5
- import { augment } from './utils.js';
6
-
7
- const {
8
- HTTP2_HEADER_SET_COOKIE,
9
- HTTP_STATUS_BAD_REQUEST,
10
- } = http2.constants;
11
-
12
- export const postflight = (req, res, options, { reject, resolve }) => {
13
- const { cookies, h2, url } = options;
14
- let headers;
15
-
16
- if (h2) {
17
- headers = res;
18
- res = req;
19
- } else {
20
- res.once('error', reject);
21
- }
22
-
23
- augment(res, headers, options);
24
-
25
- if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
26
- if (Cookies.jar.has(url.origin)) {
27
- const cookie = new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options);
28
-
29
- Cookies.jar.get(url.origin).forEach((val, key) => {
30
- if (!cookie.has(key)) {
31
- cookie.set(key, val);
32
- }
33
- });
34
- Cookies.jar.set(url.origin, cookie);
35
- } else {
36
- Cookies.jar.set(url.origin, new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options));
37
- }
38
- }
39
-
40
- Reflect.defineProperty(res, 'cookies', {
41
- enumerable: true,
42
- value: cookies !== false && Cookies.jar.has(url.origin) ? Cookies.jar.get(url.origin) : void 0,
43
- });
44
-
45
- let result;
46
-
47
- try {
48
- result = redirects(res, options);
49
- } catch (err) {
50
- res.emit('error', err);
51
-
52
- return reject(mixin(res, options));
53
- }
54
-
55
- if (Object(result) === result) {
56
- return result.then(resolve, reject);
57
- }
58
-
59
- if (res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
60
- return reject(mixin(res, options));
61
- }
62
-
63
- resolve(mixin(res, options));
64
- };
1
+ import http2 from 'node:http2';
2
+ import { Cookies } from './cookies.js';
3
+ import { mixin } from './mixin.js';
4
+ import { redirects } from './redirects.js';
5
+ import { augment } from './utils.js';
6
+
7
+ const {
8
+ HTTP2_HEADER_SET_COOKIE,
9
+ HTTP_STATUS_BAD_REQUEST,
10
+ } = http2.constants;
11
+
12
+ export const postflight = (req, res, options, { reject, resolve }) => {
13
+ const { cookies, h2, url } = options;
14
+ let headers;
15
+
16
+ if (h2) {
17
+ headers = res;
18
+ res = req;
19
+ } else {
20
+ res.once('error', reject);
21
+ }
22
+
23
+ augment(res, headers, options);
24
+
25
+ if (cookies !== false && res.headers[HTTP2_HEADER_SET_COOKIE]) {
26
+ if (Cookies.jar.has(url.origin)) {
27
+ const cookie = new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options);
28
+
29
+ Cookies.jar.get(url.origin).forEach((val, key) => {
30
+ if (!cookie.has(key)) {
31
+ cookie.set(key, val);
32
+ }
33
+ });
34
+ Cookies.jar.set(url.origin, cookie);
35
+ } else {
36
+ Cookies.jar.set(url.origin, new Cookies(res.headers[HTTP2_HEADER_SET_COOKIE], options));
37
+ }
38
+ }
39
+
40
+ Reflect.defineProperty(res, 'cookies', {
41
+ enumerable: true,
42
+ value: cookies !== false && Cookies.jar.has(url.origin) ? Cookies.jar.get(url.origin) : void 0,
43
+ });
44
+
45
+ let result;
46
+
47
+ try {
48
+ result = redirects(res, options);
49
+ } catch (err) {
50
+ res.emit('error', err);
51
+
52
+ return reject(mixin(res, options));
53
+ }
54
+
55
+ if (Object(result) === result) {
56
+ return result.then(resolve, reject);
57
+ }
58
+
59
+ if (res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
60
+ return reject(mixin(res, options));
61
+ }
62
+
63
+ resolve(mixin(res, options));
64
+ };
package/src/preflight.js CHANGED
@@ -1,84 +1,84 @@
1
- import http2 from 'node:http2';
2
- import { requestCredentials } from './constants.js';
3
- import { Cookies } from './cookies.js';
4
- import { stripHeaders } from './utils.js';
5
-
6
- const {
7
- HTTP2_HEADER_AUTHORITY,
8
- HTTP2_HEADER_AUTHORIZATION,
9
- HTTP2_HEADER_COOKIE,
10
- HTTP2_HEADER_METHOD,
11
- HTTP2_HEADER_PATH,
12
- HTTP2_HEADER_SCHEME,
13
- HTTP2_METHOD_GET,
14
- HTTP2_METHOD_HEAD,
15
- } = http2.constants;
16
-
17
- export const preflight = (options) => {
18
- let { cookies, credentials, h2, headers, method, url } = options;
19
-
20
- if (h2) {
21
- options.endStream = [
22
- HTTP2_METHOD_GET,
23
- HTTP2_METHOD_HEAD,
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
- cookies = false;
36
- headers = stripHeaders(headers, [
37
- HTTP2_HEADER_AUTHORIZATION,
38
- HTTP2_HEADER_COOKIE,
39
- ]);
40
- url.password = url.username = '';
41
- }
42
-
43
- if (cookies !== false && credentials !== requestCredentials.omit) {
44
- let cookie = Cookies.jar.has(url.origin);
45
-
46
- if (Object(cookies) === cookies && [
47
- requestCredentials.include,
48
- requestCredentials.sameOrigin,
49
- ].includes(credentials)) {
50
- if (cookie) {
51
- cookie = new Cookies(cookies, options);
52
-
53
- Cookies.jar.get(url.origin).forEach((val, key) => {
54
- if (!cookie.has(key)) {
55
- cookie.set(key, val);
56
- }
57
- });
58
- Cookies.jar.set(url.origin, cookie);
59
- } else {
60
- cookie = new Cookies(cookies, options);
61
- Cookies.jar.set(url.origin, cookie);
62
- }
63
- } else {
64
- cookie &&= Cookies.jar.get(url.origin);
65
- }
66
-
67
- headers = {
68
- ...cookie && { [HTTP2_HEADER_COOKIE]: cookie },
69
- ...headers,
70
- };
71
- }
72
-
73
- options.headers = {
74
- ...headers,
75
- ...h2 && {
76
- [HTTP2_HEADER_AUTHORITY]: url.host,
77
- [HTTP2_HEADER_METHOD]: method,
78
- [HTTP2_HEADER_PATH]: `${ url.pathname }${ url.search }`,
79
- [HTTP2_HEADER_SCHEME]: url.protocol.replace(/\p{Punctuation}/gu, ''),
80
- },
81
- };
82
-
83
- return options;
84
- };
1
+ import http2 from 'node:http2';
2
+ import { requestCredentials } from './constants.js';
3
+ import { Cookies } from './cookies.js';
4
+ import { stripHeaders } from './utils.js';
5
+
6
+ const {
7
+ HTTP2_HEADER_AUTHORITY,
8
+ HTTP2_HEADER_AUTHORIZATION,
9
+ HTTP2_HEADER_COOKIE,
10
+ HTTP2_HEADER_METHOD,
11
+ HTTP2_HEADER_PATH,
12
+ HTTP2_HEADER_SCHEME,
13
+ HTTP2_METHOD_GET,
14
+ HTTP2_METHOD_HEAD,
15
+ } = http2.constants;
16
+
17
+ export const preflight = (options) => {
18
+ let { cookies, credentials, h2, headers, method, url } = options;
19
+
20
+ if (h2) {
21
+ options.endStream = [
22
+ HTTP2_METHOD_GET,
23
+ HTTP2_METHOD_HEAD,
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
+ cookies = false;
36
+ headers = stripHeaders(headers, [
37
+ HTTP2_HEADER_AUTHORIZATION,
38
+ HTTP2_HEADER_COOKIE,
39
+ ]);
40
+ url.password = url.username = '';
41
+ }
42
+
43
+ if (cookies !== false && credentials !== requestCredentials.omit) {
44
+ let cookie = Cookies.jar.has(url.origin);
45
+
46
+ if (Object(cookies) === cookies && [
47
+ requestCredentials.include,
48
+ requestCredentials.sameOrigin,
49
+ ].includes(credentials)) {
50
+ if (cookie) {
51
+ cookie = new Cookies(cookies, options);
52
+
53
+ Cookies.jar.get(url.origin).forEach((val, key) => {
54
+ if (!cookie.has(key)) {
55
+ cookie.set(key, val);
56
+ }
57
+ });
58
+ Cookies.jar.set(url.origin, cookie);
59
+ } else {
60
+ cookie = new Cookies(cookies, options);
61
+ Cookies.jar.set(url.origin, cookie);
62
+ }
63
+ } else {
64
+ cookie &&= Cookies.jar.get(url.origin);
65
+ }
66
+
67
+ headers = {
68
+ ...cookie && { [HTTP2_HEADER_COOKIE]: cookie },
69
+ ...headers,
70
+ };
71
+ }
72
+
73
+ options.headers = {
74
+ ...headers,
75
+ ...h2 && {
76
+ [HTTP2_HEADER_AUTHORITY]: url.host,
77
+ [HTTP2_HEADER_METHOD]: method,
78
+ [HTTP2_HEADER_PATH]: `${ url.pathname }${ url.search }`,
79
+ [HTTP2_HEADER_SCHEME]: url.protocol.replace(/\p{Punctuation}/gu, ''),
80
+ },
81
+ };
82
+
83
+ return options;
84
+ };