@gjsify/fetch 0.0.2
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/README.md +9 -0
- package/lib/cjs/body.js +284 -0
- package/lib/cjs/errors/abort-error.js +28 -0
- package/lib/cjs/errors/base.js +36 -0
- package/lib/cjs/errors/fetch-error.js +40 -0
- package/lib/cjs/headers.js +231 -0
- package/lib/cjs/index.js +246 -0
- package/lib/cjs/request.js +306 -0
- package/lib/cjs/response.js +162 -0
- package/lib/cjs/types/index.js +17 -0
- package/lib/cjs/types/system-error.js +16 -0
- package/lib/cjs/utils/blob-from.js +124 -0
- package/lib/cjs/utils/get-search.js +30 -0
- package/lib/cjs/utils/is-redirect.js +26 -0
- package/lib/cjs/utils/is.js +47 -0
- package/lib/cjs/utils/multipart-parser.js +372 -0
- package/lib/cjs/utils/referrer.js +172 -0
- package/lib/esm/body.js +255 -0
- package/lib/esm/errors/abort-error.js +9 -0
- package/lib/esm/errors/base.js +17 -0
- package/lib/esm/errors/fetch-error.js +21 -0
- package/lib/esm/headers.js +202 -0
- package/lib/esm/index.js +224 -0
- package/lib/esm/request.js +281 -0
- package/lib/esm/response.js +133 -0
- package/lib/esm/types/index.js +1 -0
- package/lib/esm/types/system-error.js +1 -0
- package/lib/esm/utils/blob-from.js +101 -0
- package/lib/esm/utils/get-search.js +11 -0
- package/lib/esm/utils/is-redirect.js +7 -0
- package/lib/esm/utils/is.js +28 -0
- package/lib/esm/utils/multipart-parser.js +353 -0
- package/lib/esm/utils/referrer.js +153 -0
- package/package.json +53 -0
- package/src/body.ts +415 -0
- package/src/errors/abort-error.ts +10 -0
- package/src/errors/base.ts +20 -0
- package/src/errors/fetch-error.ts +26 -0
- package/src/headers.ts +279 -0
- package/src/index.spec.ts +13 -0
- package/src/index.ts +367 -0
- package/src/request.ts +396 -0
- package/src/response.ts +197 -0
- package/src/test.mts +6 -0
- package/src/types/index.ts +1 -0
- package/src/types/system-error.ts +11 -0
- package/src/utils/blob-from.ts +168 -0
- package/src/utils/get-search.ts +9 -0
- package/src/utils/is-redirect.ts +11 -0
- package/src/utils/is.ts +88 -0
- package/src/utils/multipart-parser.ts +448 -0
- package/src/utils/referrer.ts +350 -0
- package/test.gjs.js +34758 -0
- package/test.gjs.mjs +53177 -0
- package/test.node.js +1226 -0
- package/test.node.mjs +6294 -0
- package/tsconfig.json +19 -0
- package/tsconfig.types.json +8 -0
package/README.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# @gjsify/fetch
|
|
2
|
+
|
|
3
|
+
Web and Node.js >= 18 fetch module for Gjs
|
|
4
|
+
|
|
5
|
+
## Inspirations and credits
|
|
6
|
+
- https://github.com/sonnyp/troll/blob/main/src/std/fetch.js
|
|
7
|
+
- https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node-fetch
|
|
8
|
+
- https://github.com/node-fetch/node-fetch
|
|
9
|
+
- https://github.com/denoland/deno/tree/main/ext/fetch
|
package/lib/cjs/body.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var body_exports = {};
|
|
29
|
+
__export(body_exports, {
|
|
30
|
+
clone: () => clone,
|
|
31
|
+
default: () => Body,
|
|
32
|
+
extractContentType: () => extractContentType,
|
|
33
|
+
getTotalBytes: () => getTotalBytes,
|
|
34
|
+
writeToStream: () => writeToStream
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(body_exports);
|
|
37
|
+
var import_url = require("@gjsify/deno-runtime/ext/url/00_url");
|
|
38
|
+
var import_file = require("@gjsify/deno-runtime/ext/web/09_file");
|
|
39
|
+
var import_stream = require("stream");
|
|
40
|
+
var import_web = require("stream/web");
|
|
41
|
+
var import_util = require("util");
|
|
42
|
+
var import_buffer = require("buffer");
|
|
43
|
+
var import_esm_min = require("formdata-polyfill/esm.min.js");
|
|
44
|
+
var import_fetch_error = require("./errors/fetch-error.js");
|
|
45
|
+
var import_base = require("./errors/base.js");
|
|
46
|
+
var import_is = require("./utils/is.js");
|
|
47
|
+
const pipeline = (0, import_util.promisify)(import_stream.pipeline);
|
|
48
|
+
const INTERNALS = Symbol("Body internals");
|
|
49
|
+
class Body {
|
|
50
|
+
[INTERNALS] = {
|
|
51
|
+
body: null,
|
|
52
|
+
stream: null,
|
|
53
|
+
boundary: "",
|
|
54
|
+
disturbed: false,
|
|
55
|
+
error: null
|
|
56
|
+
};
|
|
57
|
+
size = 0;
|
|
58
|
+
constructor(body, options = { size: 0 }) {
|
|
59
|
+
this.size = options.size || 0;
|
|
60
|
+
if (body === null) {
|
|
61
|
+
this[INTERNALS].body = null;
|
|
62
|
+
} else if ((0, import_is.isURLSearchParameters)(body)) {
|
|
63
|
+
this[INTERNALS].body = import_buffer.Buffer.from(body.toString());
|
|
64
|
+
} else if ((0, import_is.isBlob)(body)) {
|
|
65
|
+
} else if (import_buffer.Buffer.isBuffer(body)) {
|
|
66
|
+
} else if (import_util.types.isAnyArrayBuffer(body)) {
|
|
67
|
+
this[INTERNALS].body = import_buffer.Buffer.from(body);
|
|
68
|
+
} else if (ArrayBuffer.isView(body)) {
|
|
69
|
+
this[INTERNALS].body = import_buffer.Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
|
70
|
+
} else if (body instanceof import_stream.Readable) {
|
|
71
|
+
this[INTERNALS].body = body;
|
|
72
|
+
} else if (body instanceof ReadableStream || body instanceof import_web.ReadableStream) {
|
|
73
|
+
this[INTERNALS].body = import_stream.Readable.fromWeb(body);
|
|
74
|
+
} else if (body instanceof import_esm_min.FormData) {
|
|
75
|
+
this[INTERNALS].body = (0, import_esm_min.formDataToBlob)(body);
|
|
76
|
+
this[INTERNALS].boundary = this[INTERNALS].body.type.split("=")[1];
|
|
77
|
+
} else if (typeof body === "string") {
|
|
78
|
+
this[INTERNALS].body = import_buffer.Buffer.from(body);
|
|
79
|
+
} else if (body instanceof import_url.URLSearchParams) {
|
|
80
|
+
this[INTERNALS].body = import_buffer.Buffer.from(body.toString());
|
|
81
|
+
} else {
|
|
82
|
+
console.warn(`Unknown body type "${typeof body}", try to parse the body to string!`);
|
|
83
|
+
this[INTERNALS].body = import_stream.Readable.from(typeof body.toString === "function" ? body.toString() : body);
|
|
84
|
+
}
|
|
85
|
+
if (import_buffer.Buffer.isBuffer(body)) {
|
|
86
|
+
this[INTERNALS].stream = import_stream.Readable.from(body);
|
|
87
|
+
} else if ((0, import_is.isBlob)(body)) {
|
|
88
|
+
this[INTERNALS].stream = import_stream.Readable.from(body.stream());
|
|
89
|
+
} else if (body instanceof import_stream.Readable) {
|
|
90
|
+
this[INTERNALS].stream = body;
|
|
91
|
+
}
|
|
92
|
+
if (body instanceof import_stream.Stream) {
|
|
93
|
+
body.on("error", (error_) => {
|
|
94
|
+
const error = error_ instanceof import_base.FetchBaseError ? error_ : new import_fetch_error.FetchError(`Invalid response body while trying to fetch ${this.url}: ${error_.message}`, "system", error_);
|
|
95
|
+
this[INTERNALS].error = error;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
get body() {
|
|
100
|
+
return import_stream.Readable.toWeb(this[INTERNALS].stream);
|
|
101
|
+
}
|
|
102
|
+
get _stream() {
|
|
103
|
+
return this[INTERNALS].stream;
|
|
104
|
+
}
|
|
105
|
+
get bodyUsed() {
|
|
106
|
+
return this[INTERNALS].disturbed;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Decode response as ArrayBuffer
|
|
110
|
+
*
|
|
111
|
+
* @return Promise
|
|
112
|
+
*/
|
|
113
|
+
async arrayBuffer() {
|
|
114
|
+
const { buffer, byteOffset, byteLength } = await consumeBody(this);
|
|
115
|
+
return buffer.slice(byteOffset, byteOffset + byteLength);
|
|
116
|
+
}
|
|
117
|
+
async formData() {
|
|
118
|
+
const ct = this.headers?.get("content-type");
|
|
119
|
+
if (ct.startsWith("application/x-www-form-urlencoded")) {
|
|
120
|
+
const formData = new import_esm_min.FormData();
|
|
121
|
+
const parameters = new import_url.URLSearchParams(await this.text());
|
|
122
|
+
for (const [name, value] of parameters) {
|
|
123
|
+
formData.append(name, value);
|
|
124
|
+
}
|
|
125
|
+
return formData;
|
|
126
|
+
}
|
|
127
|
+
const { toFormData } = await import("./utils/multipart-parser.js");
|
|
128
|
+
return toFormData(this.body, ct);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Return raw response as Blob
|
|
132
|
+
*
|
|
133
|
+
* @return Promise
|
|
134
|
+
*/
|
|
135
|
+
// @ts-ignore
|
|
136
|
+
async blob() {
|
|
137
|
+
const ct = this.headers?.get("content-type") || this[INTERNALS].body && this[INTERNALS].body.type || "";
|
|
138
|
+
const buf = await this.arrayBuffer();
|
|
139
|
+
return new import_file.Blob([buf], {
|
|
140
|
+
type: ct
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Decode response as json
|
|
145
|
+
*
|
|
146
|
+
* @return Promise
|
|
147
|
+
*/
|
|
148
|
+
async json() {
|
|
149
|
+
const text = await this.text();
|
|
150
|
+
return JSON.parse(text);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Decode response as text
|
|
154
|
+
*
|
|
155
|
+
* @return Promise
|
|
156
|
+
*/
|
|
157
|
+
async text() {
|
|
158
|
+
const buffer = await consumeBody(this);
|
|
159
|
+
return new TextDecoder().decode(buffer);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
Object.defineProperties(Body.prototype, {
|
|
163
|
+
body: { enumerable: true },
|
|
164
|
+
bodyUsed: { enumerable: true },
|
|
165
|
+
arrayBuffer: { enumerable: true },
|
|
166
|
+
blob: { enumerable: true },
|
|
167
|
+
json: { enumerable: true },
|
|
168
|
+
text: { enumerable: true },
|
|
169
|
+
data: { get: (0, import_util.deprecate)(
|
|
170
|
+
() => {
|
|
171
|
+
},
|
|
172
|
+
"data doesn't exist, use json(), text(), arrayBuffer(), or body instead",
|
|
173
|
+
"https://github.com/node-fetch/node-fetch/issues/1000 (response)"
|
|
174
|
+
) }
|
|
175
|
+
});
|
|
176
|
+
async function consumeBody(data) {
|
|
177
|
+
if (data[INTERNALS].disturbed) {
|
|
178
|
+
throw new TypeError(`body used already for: ${data.url}`);
|
|
179
|
+
}
|
|
180
|
+
data[INTERNALS].disturbed = true;
|
|
181
|
+
if (data[INTERNALS].error) {
|
|
182
|
+
throw data[INTERNALS].error;
|
|
183
|
+
}
|
|
184
|
+
const { _stream: body } = data;
|
|
185
|
+
if (body === null) {
|
|
186
|
+
return import_buffer.Buffer.alloc(0);
|
|
187
|
+
}
|
|
188
|
+
if (!(body instanceof import_stream.Stream)) {
|
|
189
|
+
return import_buffer.Buffer.alloc(0);
|
|
190
|
+
}
|
|
191
|
+
const accum = [];
|
|
192
|
+
let accumBytes = 0;
|
|
193
|
+
try {
|
|
194
|
+
for await (const chunk of body) {
|
|
195
|
+
if (data.size > 0 && accumBytes + chunk.length > data.size) {
|
|
196
|
+
const error = new import_fetch_error.FetchError(`content size at ${data.url} over limit: ${data.size}`, "max-size");
|
|
197
|
+
body.destroy(error);
|
|
198
|
+
throw error;
|
|
199
|
+
}
|
|
200
|
+
accumBytes += chunk.length;
|
|
201
|
+
accum.push(chunk);
|
|
202
|
+
}
|
|
203
|
+
} catch (error) {
|
|
204
|
+
const error_ = error instanceof import_base.FetchBaseError ? error : new import_fetch_error.FetchError(`Invalid response body while trying to fetch ${data.url}: ${error.message}`, "system", error);
|
|
205
|
+
throw error_;
|
|
206
|
+
}
|
|
207
|
+
if (body.readableEnded === true || body._readableState.ended === true) {
|
|
208
|
+
try {
|
|
209
|
+
if (accum.every((c) => typeof c === "string")) {
|
|
210
|
+
return import_buffer.Buffer.from(accum.join(""));
|
|
211
|
+
}
|
|
212
|
+
return import_buffer.Buffer.concat(accum, accumBytes);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
throw new import_fetch_error.FetchError(`Could not create Buffer from response body for ${data.url}: ${error.message}`, "system", error);
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
throw new import_fetch_error.FetchError(`Premature close of server response while trying to fetch ${data.url}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const clone = (instance, highWaterMark) => {
|
|
221
|
+
let p1;
|
|
222
|
+
let p2;
|
|
223
|
+
let { body } = instance[INTERNALS];
|
|
224
|
+
if (instance.bodyUsed) {
|
|
225
|
+
throw new Error("cannot clone body after it is used");
|
|
226
|
+
}
|
|
227
|
+
if (body instanceof import_stream.Stream && typeof body.getBoundary !== "function") {
|
|
228
|
+
p1 = new import_stream.PassThrough({ highWaterMark });
|
|
229
|
+
p2 = new import_stream.PassThrough({ highWaterMark });
|
|
230
|
+
body.pipe(p1);
|
|
231
|
+
body.pipe(p2);
|
|
232
|
+
instance[INTERNALS].stream = p1;
|
|
233
|
+
body = p2;
|
|
234
|
+
}
|
|
235
|
+
return body;
|
|
236
|
+
};
|
|
237
|
+
const extractContentType = (body, request) => {
|
|
238
|
+
if (body === null) {
|
|
239
|
+
return null;
|
|
240
|
+
}
|
|
241
|
+
if (typeof body === "string") {
|
|
242
|
+
return "text/plain;charset=UTF-8";
|
|
243
|
+
}
|
|
244
|
+
if ((0, import_is.isURLSearchParameters)(body)) {
|
|
245
|
+
return "application/x-www-form-urlencoded;charset=UTF-8";
|
|
246
|
+
}
|
|
247
|
+
if ((0, import_is.isBlob)(body)) {
|
|
248
|
+
return body.type || null;
|
|
249
|
+
}
|
|
250
|
+
if (import_buffer.Buffer.isBuffer(body) || import_util.types.isAnyArrayBuffer(body) || ArrayBuffer.isView(body)) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
if (body instanceof import_esm_min.FormData) {
|
|
254
|
+
return `multipart/form-data; boundary=${request[INTERNALS].boundary}`;
|
|
255
|
+
}
|
|
256
|
+
if (body instanceof import_stream.Stream) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
return "text/plain;charset=UTF-8";
|
|
260
|
+
};
|
|
261
|
+
const getTotalBytes = (request) => {
|
|
262
|
+
const { body } = request[INTERNALS];
|
|
263
|
+
if (body === null) {
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
if ((0, import_is.isBlob)(body)) {
|
|
267
|
+
return body.size;
|
|
268
|
+
}
|
|
269
|
+
if (import_buffer.Buffer.isBuffer(body)) {
|
|
270
|
+
return body.length;
|
|
271
|
+
}
|
|
272
|
+
if (body && typeof body.getLengthSync === "function") {
|
|
273
|
+
const anyBody = body;
|
|
274
|
+
return anyBody.hasKnownLength && anyBody.hasKnownLength() ? anyBody.getLengthSync() : null;
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
};
|
|
278
|
+
const writeToStream = async (dest, { body }) => {
|
|
279
|
+
if (body === null) {
|
|
280
|
+
dest.end();
|
|
281
|
+
} else {
|
|
282
|
+
await pipeline(body, dest);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var abort_error_exports = {};
|
|
19
|
+
__export(abort_error_exports, {
|
|
20
|
+
AbortError: () => AbortError
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(abort_error_exports);
|
|
23
|
+
var import_base = require("./base.js");
|
|
24
|
+
class AbortError extends import_base.FetchBaseError {
|
|
25
|
+
constructor(message, type = "aborted") {
|
|
26
|
+
super(message, type);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var base_exports = {};
|
|
19
|
+
__export(base_exports, {
|
|
20
|
+
FetchBaseError: () => FetchBaseError
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(base_exports);
|
|
23
|
+
class FetchBaseError extends Error {
|
|
24
|
+
type;
|
|
25
|
+
constructor(message, type) {
|
|
26
|
+
super(message);
|
|
27
|
+
Error.captureStackTrace(this, this.constructor);
|
|
28
|
+
this.type = type;
|
|
29
|
+
}
|
|
30
|
+
get name() {
|
|
31
|
+
return this.constructor.name;
|
|
32
|
+
}
|
|
33
|
+
get [Symbol.toStringTag]() {
|
|
34
|
+
return this.constructor.name;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var fetch_error_exports = {};
|
|
19
|
+
__export(fetch_error_exports, {
|
|
20
|
+
FetchError: () => FetchError
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(fetch_error_exports);
|
|
23
|
+
var import_base = require("./base.js");
|
|
24
|
+
class FetchError extends import_base.FetchBaseError {
|
|
25
|
+
code;
|
|
26
|
+
errno;
|
|
27
|
+
erroredSysCall;
|
|
28
|
+
/**
|
|
29
|
+
* @param message Error message for human
|
|
30
|
+
* @param type Error type for machine
|
|
31
|
+
* @param systemError For Node.js system error
|
|
32
|
+
*/
|
|
33
|
+
constructor(message, type, systemError) {
|
|
34
|
+
super(message, type);
|
|
35
|
+
if (systemError) {
|
|
36
|
+
this.code = this.errno = systemError.code;
|
|
37
|
+
this.erroredSysCall = systemError.syscall;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
+
mod
|
|
26
|
+
));
|
|
27
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
+
var headers_exports = {};
|
|
29
|
+
__export(headers_exports, {
|
|
30
|
+
default: () => Headers,
|
|
31
|
+
fromRawHeaders: () => fromRawHeaders
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(headers_exports);
|
|
34
|
+
var import_soup_3 = __toESM(require("@girs/soup-3.0"), 1);
|
|
35
|
+
var import_url = require("@gjsify/deno-runtime/ext/url/00_url");
|
|
36
|
+
var import_util = require("util");
|
|
37
|
+
var http = __toESM(require("http"), 1);
|
|
38
|
+
const validateHeaderName = http.validateHeaderName;
|
|
39
|
+
const validateHeaderValue = http.validateHeaderValue;
|
|
40
|
+
class Headers extends import_url.URLSearchParams {
|
|
41
|
+
/**
|
|
42
|
+
* Headers class
|
|
43
|
+
*
|
|
44
|
+
* @constructor
|
|
45
|
+
* @param init Response headers
|
|
46
|
+
*/
|
|
47
|
+
constructor(init) {
|
|
48
|
+
let result = [];
|
|
49
|
+
if (init instanceof Headers) {
|
|
50
|
+
const raw = init.raw();
|
|
51
|
+
for (const [name, values] of Object.entries(raw)) {
|
|
52
|
+
result.push(...values.map((value) => [name, value]));
|
|
53
|
+
}
|
|
54
|
+
} else if (init == null) {
|
|
55
|
+
} else if (typeof init === "object" && !import_util.types.isBoxedPrimitive(init)) {
|
|
56
|
+
const method = init[Symbol.iterator];
|
|
57
|
+
if (method == null) {
|
|
58
|
+
result.push(...Object.entries(init));
|
|
59
|
+
} else {
|
|
60
|
+
if (typeof method !== "function") {
|
|
61
|
+
throw new TypeError("Header pairs must be iterable");
|
|
62
|
+
}
|
|
63
|
+
result = [...init].map((pair) => {
|
|
64
|
+
if (typeof pair !== "object" || import_util.types.isBoxedPrimitive(pair)) {
|
|
65
|
+
throw new TypeError("Each header pair must be an iterable object");
|
|
66
|
+
}
|
|
67
|
+
return [...pair];
|
|
68
|
+
}).map((pair) => {
|
|
69
|
+
if (pair.length !== 2) {
|
|
70
|
+
throw new TypeError("Each header pair must be a name/value tuple");
|
|
71
|
+
}
|
|
72
|
+
return [...pair];
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
throw new TypeError("Failed to construct 'Headers': The provided value is not of type '(sequence<sequence<ByteString>> or record<ByteString, ByteString>)");
|
|
77
|
+
}
|
|
78
|
+
result = result.length > 0 ? result.map(([name, value]) => {
|
|
79
|
+
validateHeaderName(name);
|
|
80
|
+
validateHeaderValue(name, String(value));
|
|
81
|
+
return [String(name).toLowerCase(), String(value)];
|
|
82
|
+
}) : void 0;
|
|
83
|
+
super(result);
|
|
84
|
+
return new Proxy(this, {
|
|
85
|
+
get(target, p, receiver) {
|
|
86
|
+
switch (p) {
|
|
87
|
+
case "append":
|
|
88
|
+
case "set":
|
|
89
|
+
return (name, value) => {
|
|
90
|
+
validateHeaderName(name);
|
|
91
|
+
validateHeaderValue(name, String(value));
|
|
92
|
+
return import_url.URLSearchParams.prototype[p].call(
|
|
93
|
+
target,
|
|
94
|
+
String(name).toLowerCase(),
|
|
95
|
+
String(value)
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
case "delete":
|
|
99
|
+
case "has":
|
|
100
|
+
case "getAll":
|
|
101
|
+
return (name) => {
|
|
102
|
+
validateHeaderName(name);
|
|
103
|
+
return import_url.URLSearchParams.prototype[p].call(
|
|
104
|
+
target,
|
|
105
|
+
String(name).toLowerCase()
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
case "keys":
|
|
109
|
+
return () => {
|
|
110
|
+
target.sort();
|
|
111
|
+
return new Set(import_url.URLSearchParams.prototype.keys.call(target)).keys();
|
|
112
|
+
};
|
|
113
|
+
default:
|
|
114
|
+
return Reflect.get(target, p, receiver);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
get [Symbol.toStringTag]() {
|
|
120
|
+
return this.constructor.name;
|
|
121
|
+
}
|
|
122
|
+
toString() {
|
|
123
|
+
return Object.prototype.toString.call(this);
|
|
124
|
+
}
|
|
125
|
+
_appendToSoupMessage(message, type = import_soup_3.default.MessageHeadersType.REQUEST) {
|
|
126
|
+
const soupHeaders = message ? message.get_request_headers() : new import_soup_3.default.MessageHeaders(type);
|
|
127
|
+
for (const header in this.entries()) {
|
|
128
|
+
soupHeaders.append(header, this.get(header));
|
|
129
|
+
}
|
|
130
|
+
return soupHeaders;
|
|
131
|
+
}
|
|
132
|
+
static _newFromSoupMessage(message, type = import_soup_3.default.MessageHeadersType.RESPONSE) {
|
|
133
|
+
let soupHeaders;
|
|
134
|
+
const headers = new Headers();
|
|
135
|
+
if (type === import_soup_3.default.MessageHeadersType.RESPONSE) {
|
|
136
|
+
soupHeaders = message.get_response_headers();
|
|
137
|
+
} else if (type === import_soup_3.default.MessageHeadersType.REQUEST) {
|
|
138
|
+
soupHeaders = message.get_request_headers();
|
|
139
|
+
} else {
|
|
140
|
+
for (const header in message.get_request_headers()) {
|
|
141
|
+
headers.append(header, soupHeaders[header]);
|
|
142
|
+
}
|
|
143
|
+
soupHeaders = message.get_response_headers();
|
|
144
|
+
}
|
|
145
|
+
for (const header in soupHeaders) {
|
|
146
|
+
headers.append(header, soupHeaders[header]);
|
|
147
|
+
}
|
|
148
|
+
return headers;
|
|
149
|
+
}
|
|
150
|
+
get(name) {
|
|
151
|
+
const values = this.getAll(name);
|
|
152
|
+
if (values.length === 0) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
let value = values.join(", ");
|
|
156
|
+
if (/^content-encoding$/i.test(name)) {
|
|
157
|
+
value = value.toLowerCase();
|
|
158
|
+
}
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
forEach(callback, thisArg = void 0) {
|
|
162
|
+
for (const name of this.keys()) {
|
|
163
|
+
Reflect.apply(callback, thisArg, [this.get(name), name, this]);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
*values() {
|
|
167
|
+
for (const name of this.keys()) {
|
|
168
|
+
yield this.get(name);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
*
|
|
173
|
+
*/
|
|
174
|
+
*entries() {
|
|
175
|
+
for (const name of this.keys()) {
|
|
176
|
+
yield [name, this.get(name)];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
[Symbol.iterator]() {
|
|
180
|
+
return this.entries();
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Node-fetch non-spec method
|
|
184
|
+
* returning all headers and their values as array
|
|
185
|
+
*/
|
|
186
|
+
raw() {
|
|
187
|
+
return [...this.keys()].reduce((result, key) => {
|
|
188
|
+
result[key] = this.getAll(key);
|
|
189
|
+
return result;
|
|
190
|
+
}, {});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* For better console.log(headers) and also to convert Headers into Node.js Request compatible format
|
|
194
|
+
*/
|
|
195
|
+
[Symbol.for("nodejs.util.inspect.custom")]() {
|
|
196
|
+
return [...this.keys()].reduce((result, key) => {
|
|
197
|
+
const values = this.getAll(key);
|
|
198
|
+
if (key === "host") {
|
|
199
|
+
result[key] = values[0];
|
|
200
|
+
} else {
|
|
201
|
+
result[key] = values.length > 1 ? values : values[0];
|
|
202
|
+
}
|
|
203
|
+
return result;
|
|
204
|
+
}, {});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
Object.defineProperties(
|
|
208
|
+
Headers.prototype,
|
|
209
|
+
["get", "entries", "forEach", "values"].reduce((result, property) => {
|
|
210
|
+
result[property] = { enumerable: true };
|
|
211
|
+
return result;
|
|
212
|
+
}, {})
|
|
213
|
+
);
|
|
214
|
+
function fromRawHeaders(headers = []) {
|
|
215
|
+
return new Headers(
|
|
216
|
+
headers.reduce((result, value, index, array) => {
|
|
217
|
+
if (index % 2 === 0) {
|
|
218
|
+
result.push(array.slice(index, index + 2));
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
}, []).filter(([name, value]) => {
|
|
222
|
+
try {
|
|
223
|
+
validateHeaderName(name);
|
|
224
|
+
validateHeaderValue(name, String(value));
|
|
225
|
+
return true;
|
|
226
|
+
} catch {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
})
|
|
230
|
+
);
|
|
231
|
+
}
|