@gjsify/fetch 0.3.13 → 0.3.15
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/lib/esm/body.js +325 -303
- package/lib/esm/errors/abort-error.js +12 -7
- package/lib/esm/errors/base.js +19 -18
- package/lib/esm/errors/fetch-error.js +24 -19
- package/lib/esm/headers.js +188 -182
- package/lib/esm/index.js +223 -208
- package/lib/esm/register/fetch.js +12 -5
- package/lib/esm/register/xhr.js +6 -2
- package/lib/esm/request.js +279 -283
- package/lib/esm/response.js +152 -143
- package/lib/esm/types/index.js +1 -1
- package/lib/esm/types/system-error.js +3 -0
- package/lib/esm/utils/blob-from.js +2 -4
- package/lib/esm/utils/data-uri.js +31 -21
- package/lib/esm/utils/get-search.js +10 -9
- package/lib/esm/utils/is-redirect.js +18 -5
- package/lib/esm/utils/is.js +52 -20
- package/lib/esm/utils/multipart-parser.js +334 -338
- package/lib/esm/utils/referrer.js +189 -137
- package/lib/esm/utils/soup-helpers.js +23 -16
- package/lib/esm/xhr.js +234 -246
- package/package.json +11 -11
package/lib/esm/errors/base.js
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
export {
|
|
18
|
-
FetchBaseError
|
|
1
|
+
//#region src/errors/base.ts
|
|
2
|
+
var FetchBaseError = class extends Error {
|
|
3
|
+
type;
|
|
4
|
+
constructor(message, type) {
|
|
5
|
+
super(message);
|
|
6
|
+
if (typeof Error.captureStackTrace === "function") {
|
|
7
|
+
Error.captureStackTrace(this, this.constructor);
|
|
8
|
+
}
|
|
9
|
+
this.type = type;
|
|
10
|
+
}
|
|
11
|
+
get name() {
|
|
12
|
+
return this.constructor.name;
|
|
13
|
+
}
|
|
14
|
+
get [Symbol.toStringTag]() {
|
|
15
|
+
return this.constructor.name;
|
|
16
|
+
}
|
|
19
17
|
};
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { FetchBaseError };
|
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
import { FetchBaseError } from "./base.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
|
|
3
|
+
//#region src/errors/fetch-error.ts
|
|
4
|
+
/**
|
|
5
|
+
* FetchError interface for operational errors
|
|
6
|
+
*/
|
|
7
|
+
var FetchError = class extends FetchBaseError {
|
|
8
|
+
code;
|
|
9
|
+
errno;
|
|
10
|
+
erroredSysCall;
|
|
11
|
+
/**
|
|
12
|
+
* @param message Error message for human
|
|
13
|
+
* @param type Error type for machine
|
|
14
|
+
* @param systemError For Node.js system error
|
|
15
|
+
*/
|
|
16
|
+
constructor(message, type, systemError) {
|
|
17
|
+
super(message, type);
|
|
18
|
+
if (systemError) {
|
|
19
|
+
this.code = this.errno = systemError.code;
|
|
20
|
+
this.erroredSysCall = systemError.syscall;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
21
23
|
};
|
|
24
|
+
|
|
25
|
+
//#endregion
|
|
26
|
+
export { FetchError };
|
package/lib/esm/headers.js
CHANGED
|
@@ -1,187 +1,193 @@
|
|
|
1
1
|
import Soup from "@girs/soup-3.0";
|
|
2
2
|
import { validateHeaderName, validateHeaderValue } from "@gjsify/http/validators";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
//#region src/headers.ts
|
|
5
|
+
const _headers = Symbol("Headers.headers");
|
|
4
6
|
function isBoxedPrimitive(val) {
|
|
5
|
-
|
|
7
|
+
return val instanceof String || val instanceof Number || val instanceof Boolean || typeof Symbol !== "undefined" && val instanceof Symbol || typeof BigInt !== "undefined" && val instanceof BigInt;
|
|
6
8
|
}
|
|
7
|
-
class Headers {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
Headers.prototype,
|
|
180
|
-
["get", "entries", "forEach", "values"].reduce((result, property) => {
|
|
181
|
-
result[property] = { enumerable: true };
|
|
182
|
-
return result;
|
|
183
|
-
}, {})
|
|
184
|
-
);
|
|
185
|
-
export {
|
|
186
|
-
Headers as default
|
|
9
|
+
var Headers = class Headers {
|
|
10
|
+
[_headers];
|
|
11
|
+
constructor(init) {
|
|
12
|
+
this[_headers] = new Map();
|
|
13
|
+
if (init == null) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (init instanceof Headers) {
|
|
17
|
+
for (const [name, values] of init[_headers]) {
|
|
18
|
+
this[_headers].set(name, [...values]);
|
|
19
|
+
}
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (typeof init === "object" && !isBoxedPrimitive(init)) {
|
|
23
|
+
const method = init[Symbol.iterator];
|
|
24
|
+
if (method == null) {
|
|
25
|
+
for (const [name, value] of Object.entries(init)) {
|
|
26
|
+
validateHeaderName(name);
|
|
27
|
+
validateHeaderValue(name, String(value));
|
|
28
|
+
this.append(name, String(value));
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
if (typeof method !== "function") {
|
|
32
|
+
throw new TypeError("Header pairs must be iterable");
|
|
33
|
+
}
|
|
34
|
+
for (const pair of init) {
|
|
35
|
+
if (typeof pair !== "object" || isBoxedPrimitive(pair)) {
|
|
36
|
+
throw new TypeError("Each header pair must be an iterable object");
|
|
37
|
+
}
|
|
38
|
+
const arr = [...pair];
|
|
39
|
+
if (arr.length !== 2) {
|
|
40
|
+
throw new TypeError("Each header pair must be a name/value tuple");
|
|
41
|
+
}
|
|
42
|
+
validateHeaderName(arr[0]);
|
|
43
|
+
validateHeaderValue(arr[0], String(arr[1]));
|
|
44
|
+
this.append(arr[0], String(arr[1]));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
throw new TypeError("Failed to construct 'Headers': The provided value is not of type " + "'(sequence<sequence<ByteString>> or record<ByteString, ByteString>)'");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
append(name, value) {
|
|
52
|
+
validateHeaderName(name);
|
|
53
|
+
validateHeaderValue(name, value);
|
|
54
|
+
const lowerName = String(name).toLowerCase();
|
|
55
|
+
const strValue = String(value);
|
|
56
|
+
const existing = this[_headers].get(lowerName);
|
|
57
|
+
if (existing) {
|
|
58
|
+
existing.push(strValue);
|
|
59
|
+
} else {
|
|
60
|
+
this[_headers].set(lowerName, [strValue]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
set(name, value) {
|
|
64
|
+
validateHeaderName(name);
|
|
65
|
+
validateHeaderValue(name, value);
|
|
66
|
+
const lowerName = String(name).toLowerCase();
|
|
67
|
+
this[_headers].set(lowerName, [String(value)]);
|
|
68
|
+
}
|
|
69
|
+
delete(name) {
|
|
70
|
+
this[_headers].delete(String(name).toLowerCase());
|
|
71
|
+
}
|
|
72
|
+
has(name) {
|
|
73
|
+
return this[_headers].has(String(name).toLowerCase());
|
|
74
|
+
}
|
|
75
|
+
get(name) {
|
|
76
|
+
const values = this[_headers].get(String(name).toLowerCase());
|
|
77
|
+
if (!values || values.length === 0) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
let value = values.join(", ");
|
|
81
|
+
if (/^content-encoding$/i.test(name)) {
|
|
82
|
+
value = value.toLowerCase();
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
}
|
|
86
|
+
getAll(name) {
|
|
87
|
+
return this[_headers].get(String(name).toLowerCase()) ?? [];
|
|
88
|
+
}
|
|
89
|
+
getSetCookie() {
|
|
90
|
+
return this[_headers].get("set-cookie") ?? [];
|
|
91
|
+
}
|
|
92
|
+
forEach(callback, thisArg) {
|
|
93
|
+
for (const name of this.keys()) {
|
|
94
|
+
Reflect.apply(callback, thisArg, [
|
|
95
|
+
this.get(name),
|
|
96
|
+
name,
|
|
97
|
+
this
|
|
98
|
+
]);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
*keys() {
|
|
102
|
+
const sorted = [...this[_headers].keys()].sort();
|
|
103
|
+
const seen = new Set();
|
|
104
|
+
for (const key of sorted) {
|
|
105
|
+
if (!seen.has(key)) {
|
|
106
|
+
seen.add(key);
|
|
107
|
+
yield key;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
*values() {
|
|
112
|
+
for (const name of this.keys()) {
|
|
113
|
+
yield this.get(name);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
*entries() {
|
|
117
|
+
for (const name of this.keys()) {
|
|
118
|
+
yield [name, this.get(name)];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
[Symbol.iterator]() {
|
|
122
|
+
return this.entries();
|
|
123
|
+
}
|
|
124
|
+
get [Symbol.toStringTag]() {
|
|
125
|
+
return "Headers";
|
|
126
|
+
}
|
|
127
|
+
toString() {
|
|
128
|
+
return Object.prototype.toString.call(this);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Node-fetch non-spec method: return all headers and their values as arrays.
|
|
132
|
+
*/
|
|
133
|
+
raw() {
|
|
134
|
+
const result = {};
|
|
135
|
+
for (const name of this.keys()) {
|
|
136
|
+
result[name] = this.getAll(name);
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Append all headers to a Soup.Message for sending.
|
|
142
|
+
*/
|
|
143
|
+
_appendToSoupMessage(message, type = Soup.MessageHeadersType.REQUEST) {
|
|
144
|
+
const soupHeaders = message ? message.get_request_headers() : new Soup.MessageHeaders(type);
|
|
145
|
+
for (const [name, value] of this.entries()) {
|
|
146
|
+
soupHeaders.append(name, value);
|
|
147
|
+
}
|
|
148
|
+
return soupHeaders;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Create a Headers instance from a Soup.Message's headers.
|
|
152
|
+
*/
|
|
153
|
+
static _newFromSoupMessage(message, type = Soup.MessageHeadersType.RESPONSE) {
|
|
154
|
+
const headers = new Headers();
|
|
155
|
+
let soupHeaders;
|
|
156
|
+
if (type === Soup.MessageHeadersType.RESPONSE) {
|
|
157
|
+
soupHeaders = message.get_response_headers();
|
|
158
|
+
} else {
|
|
159
|
+
soupHeaders = message.get_request_headers();
|
|
160
|
+
}
|
|
161
|
+
soupHeaders.foreach((name, value) => {
|
|
162
|
+
headers.append(name, value);
|
|
163
|
+
});
|
|
164
|
+
return headers;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* For better console.log(headers)
|
|
168
|
+
*/
|
|
169
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
170
|
+
const result = {};
|
|
171
|
+
for (const key of this.keys()) {
|
|
172
|
+
const values = this.getAll(key);
|
|
173
|
+
if (key === "host") {
|
|
174
|
+
result[key] = values[0];
|
|
175
|
+
} else {
|
|
176
|
+
result[key] = values.length > 1 ? values : values[0];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
187
181
|
};
|
|
182
|
+
Object.defineProperties(Headers.prototype, [
|
|
183
|
+
"get",
|
|
184
|
+
"entries",
|
|
185
|
+
"forEach",
|
|
186
|
+
"values"
|
|
187
|
+
].reduce((result, property) => {
|
|
188
|
+
result[property] = { enumerable: true };
|
|
189
|
+
return result;
|
|
190
|
+
}, {}));
|
|
191
|
+
|
|
192
|
+
//#endregion
|
|
193
|
+
export { Headers as default };
|