@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.
@@ -1,291 +1,287 @@
1
- import GLib from "@girs/glib-2.0";
1
+ import { isAbortSignal } from "./utils/is.js";
2
+ import Body, { clone, extractContentType, getTotalBytes } from "./body.js";
3
+ import Headers from "./headers.js";
4
+ import { inputStreamToReadable, soupSendAsync } from "./utils/soup-helpers.js";
5
+ import { DEFAULT_REFERRER_POLICY, determineRequestsReferrer, validateReferrerPolicy } from "./utils/referrer.js";
6
+ import { URL } from "@gjsify/url";
2
7
  import Soup from "@girs/soup-3.0";
8
+ import GLib from "@girs/glib-2.0";
3
9
  import Gio from "@girs/gio-2.0";
4
- import { soupSendAsync, inputStreamToReadable } from "./utils/soup-helpers.js";
5
- import { URL } from "@gjsify/url";
6
- import Headers from "./headers.js";
7
- import Body, { clone, extractContentType, getTotalBytes } from "./body.js";
8
- import { isAbortSignal } from "./utils/is.js";
9
- import {
10
- validateReferrerPolicy,
11
- determineRequestsReferrer,
12
- DEFAULT_REFERRER_POLICY
13
- } from "./utils/referrer.js";
14
- const INTERNALS = /* @__PURE__ */ Symbol("Request internals");
10
+
11
+ //#region src/request.ts
12
+ const INTERNALS = Symbol("Request internals");
13
+ /**
14
+ * Check if `obj` is an instance of Request.
15
+ */
15
16
  const isRequest = (obj) => {
16
- return typeof obj === "object" && typeof obj.url === "string";
17
+ return typeof obj === "object" && typeof obj.url === "string";
18
+ };
19
+ /** This Fetch API interface represents a resource request. */
20
+ var Request = class Request extends Body {
21
+ /** Returns the cache mode associated with request, which is a string indicating how the request will interact with the browser's cache when fetching. */
22
+ cache;
23
+ /** Returns the credentials mode associated with request, which is a string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. */
24
+ credentials;
25
+ /** Returns the kind of resource requested by request, e.g., "document" or "script". */
26
+ destination;
27
+ /** Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header. */
28
+ get headers() {
29
+ return this[INTERNALS].headers;
30
+ }
31
+ /** Returns request's subresource integrity metadata, which is a cryptographic hash of the resource being fetched. Its value consists of multiple hashes separated by whitespace. [SRI] */
32
+ integrity;
33
+ /** Returns a boolean indicating whether or not request can outlive the global in which it was created. */
34
+ keepalive;
35
+ /** Returns request's HTTP method, which is "GET" by default. */
36
+ get method() {
37
+ return this[INTERNALS].method;
38
+ }
39
+ /** Returns the mode associated with request, which is a string indicating whether the request will use CORS, or will be restricted to same-origin URLs. */
40
+ mode;
41
+ /** Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default. */
42
+ get redirect() {
43
+ return this[INTERNALS].redirect;
44
+ }
45
+ /**
46
+ * Returns the referrer of request.
47
+ * Its value can be a same-origin URL if explicitly set in init, the empty string to indicate no referrer, and "about:client" when defaulting to the global's default.
48
+ * This is used during fetching to determine the value of the `Referer` header of the request being made.
49
+ * @see https://fetch.spec.whatwg.org/#dom-request-referrer
50
+ **/
51
+ get referrer() {
52
+ if (this[INTERNALS].referrer === "no-referrer") {
53
+ return "";
54
+ }
55
+ if (this[INTERNALS].referrer === "client") {
56
+ return "about:client";
57
+ }
58
+ if (this[INTERNALS].referrer) {
59
+ return this[INTERNALS].referrer.toString();
60
+ }
61
+ return undefined;
62
+ }
63
+ /** Returns the referrer policy associated with request. This is used during fetching to compute the value of the request's referrer. */
64
+ get referrerPolicy() {
65
+ return this[INTERNALS].referrerPolicy;
66
+ }
67
+ set referrerPolicy(referrerPolicy) {
68
+ this[INTERNALS].referrerPolicy = validateReferrerPolicy(referrerPolicy);
69
+ }
70
+ /** Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler. */
71
+ get signal() {
72
+ return this[INTERNALS].signal;
73
+ }
74
+ /** Returns the URL of request as a string. */
75
+ get url() {
76
+ return this[INTERNALS].parsedURL.toString();
77
+ }
78
+ get _uri() {
79
+ return GLib.Uri.parse(this.url, GLib.UriFlags.NONE);
80
+ }
81
+ get _session() {
82
+ return this[INTERNALS].session;
83
+ }
84
+ get _message() {
85
+ return this[INTERNALS].message;
86
+ }
87
+ get _inputStream() {
88
+ return this[INTERNALS].inputStream;
89
+ }
90
+ get [Symbol.toStringTag]() {
91
+ return "Request";
92
+ }
93
+ [INTERNALS];
94
+ follow;
95
+ compress = false;
96
+ counter = 0;
97
+ agent = "";
98
+ highWaterMark = 16384;
99
+ insecureHTTPParser = false;
100
+ constructor(input, init) {
101
+ const inputRL = input;
102
+ const initRL = init || {};
103
+ let parsedURL;
104
+ let requestObj = {};
105
+ if (isRequest(input)) {
106
+ parsedURL = new URL(inputRL.url);
107
+ requestObj = inputRL;
108
+ } else {
109
+ parsedURL = new URL(input);
110
+ }
111
+ if (parsedURL.username !== "" || parsedURL.password !== "") {
112
+ throw new TypeError(`${parsedURL} is an url with embedded credentials.`);
113
+ }
114
+ let method = initRL.method || requestObj.method || "GET";
115
+ if (/^(delete|get|head|options|post|put)$/i.test(method)) {
116
+ method = method.toUpperCase();
117
+ }
118
+ if ((init?.body != null || isRequest(input) && inputRL.body !== null) && (method === "GET" || method === "HEAD")) {
119
+ throw new TypeError("Request with GET/HEAD method cannot have body");
120
+ }
121
+ const inputBody = init?.body ? init.body : isRequest(input) && inputRL.body !== null ? clone(input) : null;
122
+ super(inputBody, { size: initRL.size || 0 });
123
+ const headers = new Headers(init?.headers || inputRL.headers || {});
124
+ if (inputBody !== null && !headers.has("Content-Type")) {
125
+ const contentType = extractContentType(inputBody, this);
126
+ if (contentType) {
127
+ headers.set("Content-Type", contentType);
128
+ }
129
+ }
130
+ let signal = isRequest(input) ? inputRL.signal : null;
131
+ if (init && "signal" in init) {
132
+ signal = init.signal;
133
+ }
134
+ if (signal != null && !isAbortSignal(signal)) {
135
+ throw new TypeError("Expected signal to be an instanceof AbortSignal or EventTarget");
136
+ }
137
+ let referrer = init?.referrer == null ? inputRL.referrer : init.referrer;
138
+ if (referrer === "") {
139
+ referrer = "no-referrer";
140
+ } else if (referrer) {
141
+ const parsedReferrer = new URL(referrer);
142
+ referrer = /^about:(\/\/)?client$/.test(parsedReferrer.toString()) ? "client" : parsedReferrer;
143
+ } else {
144
+ referrer = undefined;
145
+ }
146
+ const scheme = parsedURL.protocol;
147
+ let session = null;
148
+ let message = null;
149
+ if (scheme === "http:" || scheme === "https:") {
150
+ session = new Soup.Session();
151
+ message = new Soup.Message({
152
+ method,
153
+ uri: GLib.Uri.parse(parsedURL.toString(), GLib.UriFlags.NONE)
154
+ });
155
+ }
156
+ this[INTERNALS] = {
157
+ method,
158
+ redirect: init?.redirect || inputRL.redirect || "follow",
159
+ headers,
160
+ parsedURL,
161
+ signal,
162
+ referrer,
163
+ referrerPolicy: "",
164
+ session,
165
+ message
166
+ };
167
+ this.follow = initRL.follow === undefined ? inputRL.follow === undefined ? 20 : inputRL.follow : initRL.follow;
168
+ this.compress = initRL.compress === undefined ? inputRL.compress === undefined ? true : inputRL.compress : initRL.compress;
169
+ this.counter = initRL.counter || inputRL.counter || 0;
170
+ this.agent = initRL.agent || inputRL.agent;
171
+ this.highWaterMark = initRL.highWaterMark || inputRL.highWaterMark || 16384;
172
+ this.insecureHTTPParser = initRL.insecureHTTPParser || inputRL.insecureHTTPParser || false;
173
+ this.referrerPolicy = init?.referrerPolicy || inputRL.referrerPolicy || "";
174
+ }
175
+ /**
176
+ * Send the request using Soup.
177
+ */
178
+ async _send(options) {
179
+ const { session, message } = this[INTERNALS];
180
+ if (!session || !message) {
181
+ throw new Error("Cannot send request: no Soup session (non-HTTP URL?)");
182
+ }
183
+ try {
184
+ session.remove_feature_by_type(Soup.ContentDecoder.$gtype);
185
+ } catch {}
186
+ options.headers._appendToSoupMessage(message);
187
+ const rawBuf = this._rawBodyBuffer;
188
+ if (rawBuf !== null && rawBuf.byteLength > 0) {
189
+ const contentType = options.headers.get("content-type") || null;
190
+ message.set_request_body_from_bytes(contentType, new GLib.Bytes(rawBuf));
191
+ }
192
+ const cancellable = new Gio.Cancellable();
193
+ this[INTERNALS].inputStream = await soupSendAsync(session, message, GLib.PRIORITY_DEFAULT, cancellable);
194
+ this[INTERNALS].readable = inputStreamToReadable(this[INTERNALS].inputStream);
195
+ return {
196
+ inputStream: this[INTERNALS].inputStream,
197
+ readable: this[INTERNALS].readable,
198
+ cancellable
199
+ };
200
+ }
201
+ /**
202
+ * Clone this request
203
+ */
204
+ clone() {
205
+ return new Request(this);
206
+ }
207
+ async arrayBuffer() {
208
+ return super.arrayBuffer();
209
+ }
210
+ async blob() {
211
+ return super.blob();
212
+ }
213
+ async formData() {
214
+ return super.formData();
215
+ }
216
+ async json() {
217
+ return super.json();
218
+ }
219
+ async text() {
220
+ return super.text();
221
+ }
17
222
  };
18
- class Request extends Body {
19
- /** Returns the cache mode associated with request, which is a string indicating how the request will interact with the browser's cache when fetching. */
20
- cache;
21
- /** Returns the credentials mode associated with request, which is a string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. */
22
- credentials;
23
- /** Returns the kind of resource requested by request, e.g., "document" or "script". */
24
- destination;
25
- /** Returns a Headers object consisting of the headers associated with request. Note that headers added in the network layer by the user agent will not be accounted for in this object, e.g., the "Host" header. */
26
- get headers() {
27
- return this[INTERNALS].headers;
28
- }
29
- /** Returns request's subresource integrity metadata, which is a cryptographic hash of the resource being fetched. Its value consists of multiple hashes separated by whitespace. [SRI] */
30
- integrity;
31
- /** Returns a boolean indicating whether or not request can outlive the global in which it was created. */
32
- keepalive;
33
- /** Returns request's HTTP method, which is "GET" by default. */
34
- get method() {
35
- return this[INTERNALS].method;
36
- }
37
- /** Returns the mode associated with request, which is a string indicating whether the request will use CORS, or will be restricted to same-origin URLs. */
38
- mode;
39
- /** Returns the redirect mode associated with request, which is a string indicating how redirects for the request will be handled during fetching. A request will follow redirects by default. */
40
- get redirect() {
41
- return this[INTERNALS].redirect;
42
- }
43
- /**
44
- * Returns the referrer of request.
45
- * Its value can be a same-origin URL if explicitly set in init, the empty string to indicate no referrer, and "about:client" when defaulting to the global's default.
46
- * This is used during fetching to determine the value of the `Referer` header of the request being made.
47
- * @see https://fetch.spec.whatwg.org/#dom-request-referrer
48
- **/
49
- get referrer() {
50
- if (this[INTERNALS].referrer === "no-referrer") {
51
- return "";
52
- }
53
- if (this[INTERNALS].referrer === "client") {
54
- return "about:client";
55
- }
56
- if (this[INTERNALS].referrer) {
57
- return this[INTERNALS].referrer.toString();
58
- }
59
- return void 0;
60
- }
61
- /** Returns the referrer policy associated with request. This is used during fetching to compute the value of the request's referrer. */
62
- get referrerPolicy() {
63
- return this[INTERNALS].referrerPolicy;
64
- }
65
- set referrerPolicy(referrerPolicy) {
66
- this[INTERNALS].referrerPolicy = validateReferrerPolicy(referrerPolicy);
67
- }
68
- /** Returns the signal associated with request, which is an AbortSignal object indicating whether or not request has been aborted, and its abort event handler. */
69
- get signal() {
70
- return this[INTERNALS].signal;
71
- }
72
- /** Returns the URL of request as a string. */
73
- get url() {
74
- return this[INTERNALS].parsedURL.toString();
75
- }
76
- get _uri() {
77
- return GLib.Uri.parse(this.url, GLib.UriFlags.NONE);
78
- }
79
- get _session() {
80
- return this[INTERNALS].session;
81
- }
82
- get _message() {
83
- return this[INTERNALS].message;
84
- }
85
- get _inputStream() {
86
- return this[INTERNALS].inputStream;
87
- }
88
- get [Symbol.toStringTag]() {
89
- return "Request";
90
- }
91
- [INTERNALS];
92
- // Node-fetch-only options
93
- follow;
94
- compress = false;
95
- counter = 0;
96
- agent = "";
97
- highWaterMark = 16384;
98
- insecureHTTPParser = false;
99
- constructor(input, init) {
100
- const inputRL = input;
101
- const initRL = init || {};
102
- let parsedURL;
103
- let requestObj = {};
104
- if (isRequest(input)) {
105
- parsedURL = new URL(inputRL.url);
106
- requestObj = inputRL;
107
- } else {
108
- parsedURL = new URL(input);
109
- }
110
- if (parsedURL.username !== "" || parsedURL.password !== "") {
111
- throw new TypeError(`${parsedURL} is an url with embedded credentials.`);
112
- }
113
- let method = initRL.method || requestObj.method || "GET";
114
- if (/^(delete|get|head|options|post|put)$/i.test(method)) {
115
- method = method.toUpperCase();
116
- }
117
- if ((init?.body != null || isRequest(input) && inputRL.body !== null) && (method === "GET" || method === "HEAD")) {
118
- throw new TypeError("Request with GET/HEAD method cannot have body");
119
- }
120
- const inputBody = init?.body ? init.body : isRequest(input) && inputRL.body !== null ? clone(input) : null;
121
- super(inputBody, {
122
- size: initRL.size || 0
123
- });
124
- const headers = new Headers(init?.headers || inputRL.headers || {});
125
- if (inputBody !== null && !headers.has("Content-Type")) {
126
- const contentType = extractContentType(inputBody, this);
127
- if (contentType) {
128
- headers.set("Content-Type", contentType);
129
- }
130
- }
131
- let signal = isRequest(input) ? inputRL.signal : null;
132
- if (init && "signal" in init) {
133
- signal = init.signal;
134
- }
135
- if (signal != null && !isAbortSignal(signal)) {
136
- throw new TypeError("Expected signal to be an instanceof AbortSignal or EventTarget");
137
- }
138
- let referrer = init?.referrer == null ? inputRL.referrer : init.referrer;
139
- if (referrer === "") {
140
- referrer = "no-referrer";
141
- } else if (referrer) {
142
- const parsedReferrer = new URL(referrer);
143
- referrer = /^about:(\/\/)?client$/.test(parsedReferrer.toString()) ? "client" : parsedReferrer;
144
- } else {
145
- referrer = void 0;
146
- }
147
- const scheme = parsedURL.protocol;
148
- let session = null;
149
- let message = null;
150
- if (scheme === "http:" || scheme === "https:") {
151
- session = new Soup.Session();
152
- message = new Soup.Message({
153
- method,
154
- uri: GLib.Uri.parse(parsedURL.toString(), GLib.UriFlags.NONE)
155
- });
156
- }
157
- this[INTERNALS] = {
158
- method,
159
- redirect: init?.redirect || inputRL.redirect || "follow",
160
- headers,
161
- parsedURL,
162
- signal,
163
- referrer,
164
- referrerPolicy: "",
165
- session,
166
- message
167
- };
168
- this.follow = initRL.follow === void 0 ? inputRL.follow === void 0 ? 20 : inputRL.follow : initRL.follow;
169
- this.compress = initRL.compress === void 0 ? inputRL.compress === void 0 ? true : inputRL.compress : initRL.compress;
170
- this.counter = initRL.counter || inputRL.counter || 0;
171
- this.agent = initRL.agent || inputRL.agent;
172
- this.highWaterMark = initRL.highWaterMark || inputRL.highWaterMark || 16384;
173
- this.insecureHTTPParser = initRL.insecureHTTPParser || inputRL.insecureHTTPParser || false;
174
- this.referrerPolicy = init?.referrerPolicy || inputRL.referrerPolicy || "";
175
- }
176
- /**
177
- * Send the request using Soup.
178
- */
179
- async _send(options) {
180
- const { session, message } = this[INTERNALS];
181
- if (!session || !message) {
182
- throw new Error("Cannot send request: no Soup session (non-HTTP URL?)");
183
- }
184
- try {
185
- session.remove_feature_by_type(Soup.ContentDecoder.$gtype);
186
- } catch {
187
- }
188
- options.headers._appendToSoupMessage(message);
189
- const rawBuf = this._rawBodyBuffer;
190
- if (rawBuf !== null && rawBuf.byteLength > 0) {
191
- const contentType = options.headers.get("content-type") || null;
192
- message.set_request_body_from_bytes(contentType, new GLib.Bytes(rawBuf));
193
- }
194
- const cancellable = new Gio.Cancellable();
195
- this[INTERNALS].inputStream = await soupSendAsync(session, message, GLib.PRIORITY_DEFAULT, cancellable);
196
- this[INTERNALS].readable = inputStreamToReadable(this[INTERNALS].inputStream);
197
- return {
198
- inputStream: this[INTERNALS].inputStream,
199
- readable: this[INTERNALS].readable,
200
- cancellable
201
- };
202
- }
203
- /**
204
- * Clone this request
205
- */
206
- clone() {
207
- return new Request(this);
208
- }
209
- async arrayBuffer() {
210
- return super.arrayBuffer();
211
- }
212
- async blob() {
213
- return super.blob();
214
- }
215
- async formData() {
216
- return super.formData();
217
- }
218
- async json() {
219
- return super.json();
220
- }
221
- async text() {
222
- return super.text();
223
- }
224
- }
225
223
  Object.defineProperties(Request.prototype, {
226
- method: { enumerable: true },
227
- url: { enumerable: true },
228
- headers: { enumerable: true },
229
- redirect: { enumerable: true },
230
- clone: { enumerable: true },
231
- signal: { enumerable: true },
232
- referrer: { enumerable: true },
233
- referrerPolicy: { enumerable: true }
224
+ method: { enumerable: true },
225
+ url: { enumerable: true },
226
+ headers: { enumerable: true },
227
+ redirect: { enumerable: true },
228
+ clone: { enumerable: true },
229
+ signal: { enumerable: true },
230
+ referrer: { enumerable: true },
231
+ referrerPolicy: { enumerable: true }
234
232
  });
235
- var request_default = Request;
233
+ /**
234
+ * @param request
235
+ */
236
236
  const getSoupRequestOptions = (request) => {
237
- const { parsedURL } = request[INTERNALS];
238
- const headers = new Headers(request[INTERNALS].headers);
239
- if (!headers.has("Accept")) {
240
- headers.set("Accept", "*/*");
241
- }
242
- let contentLengthValue = null;
243
- if (request.body === null && /^(post|put)$/i.test(request.method)) {
244
- contentLengthValue = "0";
245
- }
246
- if (request.body !== null) {
247
- const totalBytes = getTotalBytes(request);
248
- if (typeof totalBytes === "number" && !Number.isNaN(totalBytes)) {
249
- contentLengthValue = String(totalBytes);
250
- }
251
- }
252
- if (contentLengthValue) {
253
- headers.set("Content-Length", contentLengthValue);
254
- }
255
- if (request.referrerPolicy === "") {
256
- request.referrerPolicy = DEFAULT_REFERRER_POLICY;
257
- }
258
- if (request.referrer && request.referrer !== "no-referrer") {
259
- request[INTERNALS].referrer = determineRequestsReferrer(request);
260
- } else {
261
- request[INTERNALS].referrer = "no-referrer";
262
- }
263
- if (request[INTERNALS].referrer instanceof URL) {
264
- headers.set("Referer", request.referrer);
265
- }
266
- if (!headers.has("User-Agent")) {
267
- headers.set("User-Agent", "gjsify-fetch");
268
- }
269
- if (request.compress && !headers.has("Accept-Encoding")) {
270
- headers.set("Accept-Encoding", "gzip, deflate");
271
- }
272
- let { agent } = request;
273
- if (typeof agent === "function") {
274
- agent = agent(parsedURL);
275
- }
276
- if (!headers.has("Connection") && !agent) {
277
- headers.set("Connection", "close");
278
- }
279
- const options = {
280
- headers
281
- };
282
- return {
283
- parsedURL,
284
- options
285
- };
286
- };
287
- export {
288
- Request,
289
- request_default as default,
290
- getSoupRequestOptions
237
+ const { parsedURL } = request[INTERNALS];
238
+ const headers = new Headers(request[INTERNALS].headers);
239
+ if (!headers.has("Accept")) {
240
+ headers.set("Accept", "*/*");
241
+ }
242
+ let contentLengthValue = null;
243
+ if (request.body === null && /^(post|put)$/i.test(request.method)) {
244
+ contentLengthValue = "0";
245
+ }
246
+ if (request.body !== null) {
247
+ const totalBytes = getTotalBytes(request);
248
+ if (typeof totalBytes === "number" && !Number.isNaN(totalBytes)) {
249
+ contentLengthValue = String(totalBytes);
250
+ }
251
+ }
252
+ if (contentLengthValue) {
253
+ headers.set("Content-Length", contentLengthValue);
254
+ }
255
+ if (request.referrerPolicy === "") {
256
+ request.referrerPolicy = DEFAULT_REFERRER_POLICY;
257
+ }
258
+ if (request.referrer && request.referrer !== "no-referrer") {
259
+ request[INTERNALS].referrer = determineRequestsReferrer(request);
260
+ } else {
261
+ request[INTERNALS].referrer = "no-referrer";
262
+ }
263
+ if (request[INTERNALS].referrer instanceof URL) {
264
+ headers.set("Referer", request.referrer);
265
+ }
266
+ if (!headers.has("User-Agent")) {
267
+ headers.set("User-Agent", "gjsify-fetch");
268
+ }
269
+ if (request.compress && !headers.has("Accept-Encoding")) {
270
+ headers.set("Accept-Encoding", "gzip, deflate");
271
+ }
272
+ let { agent } = request;
273
+ if (typeof agent === "function") {
274
+ agent = agent(parsedURL);
275
+ }
276
+ if (!headers.has("Connection") && !agent) {
277
+ headers.set("Connection", "close");
278
+ }
279
+ const options = { headers };
280
+ return {
281
+ parsedURL,
282
+ options
283
+ };
291
284
  };
285
+
286
+ //#endregion
287
+ export { Request, Request as default, getSoupRequestOptions };