@gjsify/fetch 0.3.15 → 0.3.17
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 +1 -349
- package/lib/esm/errors/abort-error.js +1 -14
- package/lib/esm/errors/base.js +1 -20
- package/lib/esm/errors/fetch-error.js +1 -26
- package/lib/esm/headers.js +1 -193
- package/lib/esm/index.js +1 -232
- package/lib/esm/register/fetch.js +1 -20
- package/lib/esm/register/xhr.js +1 -11
- package/lib/esm/register.js +1 -2
- package/lib/esm/request.js +1 -287
- package/lib/esm/response.js +1 -157
- package/lib/esm/types/index.js +1 -1
- package/lib/esm/types/system-error.js +0 -4
- package/lib/esm/utils/blob-from.js +1 -3
- package/lib/esm/utils/data-uri.js +1 -33
- package/lib/esm/utils/get-search.js +1 -12
- package/lib/esm/utils/is-redirect.js +1 -20
- package/lib/esm/utils/is.js +1 -64
- package/lib/esm/utils/multipart-parser.js +2 -352
- package/lib/esm/utils/referrer.js +1 -205
- package/lib/esm/utils/soup-helpers.js +1 -29
- package/lib/esm/xhr.js +2 -246
- package/package.json +12 -12
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -1,205 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { isIP } from "node:net";
|
|
3
|
-
|
|
4
|
-
//#region src/utils/referrer.ts
|
|
5
|
-
/**
|
|
6
|
-
* @external URL
|
|
7
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/URL URL}
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* @module utils/referrer
|
|
11
|
-
* @private
|
|
12
|
-
*/
|
|
13
|
-
/**
|
|
14
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#strip-url Referrer Policy §8.4. Strip url for use as a referrer}
|
|
15
|
-
* @param url
|
|
16
|
-
* @param originOnly
|
|
17
|
-
*/
|
|
18
|
-
function stripURLForUseAsAReferrer(url, originOnly = false) {
|
|
19
|
-
if (url == null || url === "no-referrer") {
|
|
20
|
-
return "no-referrer";
|
|
21
|
-
}
|
|
22
|
-
const u = new URL(url);
|
|
23
|
-
if (/^(about|blob|data):$/.test(u.protocol)) {
|
|
24
|
-
return "no-referrer";
|
|
25
|
-
}
|
|
26
|
-
u.username = "";
|
|
27
|
-
u.password = "";
|
|
28
|
-
u.hash = "";
|
|
29
|
-
if (originOnly) {
|
|
30
|
-
u.pathname = "";
|
|
31
|
-
u.search = "";
|
|
32
|
-
}
|
|
33
|
-
return u;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#enumdef-referrerpolicy enum ReferrerPolicy}
|
|
37
|
-
*/
|
|
38
|
-
const ReferrerPolicy = new Set([
|
|
39
|
-
"",
|
|
40
|
-
"no-referrer",
|
|
41
|
-
"no-referrer-when-downgrade",
|
|
42
|
-
"same-origin",
|
|
43
|
-
"origin",
|
|
44
|
-
"strict-origin",
|
|
45
|
-
"origin-when-cross-origin",
|
|
46
|
-
"strict-origin-when-cross-origin",
|
|
47
|
-
"unsafe-url"
|
|
48
|
-
]);
|
|
49
|
-
/**
|
|
50
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#default-referrer-policy default referrer policy}
|
|
51
|
-
*/
|
|
52
|
-
const DEFAULT_REFERRER_POLICY = "strict-origin-when-cross-origin";
|
|
53
|
-
/**
|
|
54
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#referrer-policies Referrer Policy §3. Referrer Policies}
|
|
55
|
-
* @param referrerPolicy
|
|
56
|
-
* @returns referrerPolicy
|
|
57
|
-
*/
|
|
58
|
-
function validateReferrerPolicy(referrerPolicy) {
|
|
59
|
-
if (!ReferrerPolicy.has(referrerPolicy)) {
|
|
60
|
-
throw new TypeError(`Invalid referrerPolicy: ${referrerPolicy}`);
|
|
61
|
-
}
|
|
62
|
-
return referrerPolicy;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* @see {@link https://w3c.github.io/webappsec-secure-contexts/#is-origin-trustworthy Referrer Policy §3.2. Is origin potentially trustworthy?}
|
|
66
|
-
* @param url
|
|
67
|
-
* @returns `true`: "Potentially Trustworthy", `false`: "Not Trustworthy"
|
|
68
|
-
*/
|
|
69
|
-
function isOriginPotentiallyTrustworthy(url) {
|
|
70
|
-
if (/^(http|ws)s:$/.test(url.protocol)) {
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
const hostIp = url.host.replace(/(^\[)|(]$)/g, "");
|
|
74
|
-
const hostIPVersion = isIP(hostIp);
|
|
75
|
-
if (hostIPVersion === 4 && /^127\./.test(hostIp)) {
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
if (hostIPVersion === 6 && /^(((0+:){7})|(::(0+:){0,6}))0*1$/.test(hostIp)) {
|
|
79
|
-
return true;
|
|
80
|
-
}
|
|
81
|
-
if (url.host === "localhost" || url.host.endsWith(".localhost")) {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
if (url.protocol === "file:") {
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
return false;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* @see {@link https://w3c.github.io/webappsec-secure-contexts/#is-url-trustworthy Referrer Policy §3.3. Is url potentially trustworthy?}
|
|
91
|
-
* @param url
|
|
92
|
-
* @returns `true`: "Potentially Trustworthy", `false`: "Not Trustworthy"
|
|
93
|
-
*/
|
|
94
|
-
function isUrlPotentiallyTrustworthy(url) {
|
|
95
|
-
if (/^about:(blank|srcdoc)$/.test(url.toString())) {
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
if (typeof url === "string") {
|
|
99
|
-
url = new URL(url);
|
|
100
|
-
}
|
|
101
|
-
if (url.protocol === "data:") {
|
|
102
|
-
return true;
|
|
103
|
-
}
|
|
104
|
-
if (/^(blob|filesystem):$/.test(url.protocol)) {
|
|
105
|
-
return true;
|
|
106
|
-
}
|
|
107
|
-
return isOriginPotentiallyTrustworthy(url);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Modifies the referrerURL to enforce any extra security policy considerations.
|
|
111
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer Referrer Policy §8.3. Determine request's Referrer}, step 7
|
|
112
|
-
* @callback module:utils/referrer~referrerURLCallback
|
|
113
|
-
* @param {external:URL} referrerURL
|
|
114
|
-
* @returns {external:URL} modified referrerURL
|
|
115
|
-
*/
|
|
116
|
-
/**
|
|
117
|
-
* Modifies the referrerOrigin to enforce any extra security policy considerations.
|
|
118
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer Referrer Policy §8.3. Determine request's Referrer}, step 7
|
|
119
|
-
* @callback module:utils/referrer~referrerOriginCallback
|
|
120
|
-
* @param {external:URL} referrerOrigin
|
|
121
|
-
* @returns {external:URL} modified referrerOrigin
|
|
122
|
-
*/
|
|
123
|
-
/**
|
|
124
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer Referrer Policy §8.3. Determine request's Referrer}
|
|
125
|
-
* @param request
|
|
126
|
-
* @param {object} o
|
|
127
|
-
* @param {module:utils/referrer~referrerURLCallback} o.referrerURLCallback
|
|
128
|
-
* @param {module:utils/referrer~referrerOriginCallback} o.referrerOriginCallback
|
|
129
|
-
* @returns {external:URL} Request's referrer
|
|
130
|
-
*/
|
|
131
|
-
function determineRequestsReferrer(request, obj = {}) {
|
|
132
|
-
const { referrerURLCallback, referrerOriginCallback } = obj;
|
|
133
|
-
if (request.referrer === "no-referrer" || request.referrerPolicy === "") {
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
const policy = request.referrerPolicy;
|
|
137
|
-
if (request.referrer === "about:client") {
|
|
138
|
-
return "no-referrer";
|
|
139
|
-
}
|
|
140
|
-
const referrerSource = new URL(request.referrer);
|
|
141
|
-
let referrerURL = stripURLForUseAsAReferrer(referrerSource);
|
|
142
|
-
let referrerOrigin = stripURLForUseAsAReferrer(referrerSource, true);
|
|
143
|
-
if (referrerURL.toString().length > 4096) {
|
|
144
|
-
referrerURL = referrerOrigin;
|
|
145
|
-
}
|
|
146
|
-
if (referrerURLCallback) {
|
|
147
|
-
referrerURL = referrerURLCallback(referrerURL);
|
|
148
|
-
}
|
|
149
|
-
if (referrerOriginCallback) {
|
|
150
|
-
referrerOrigin = referrerOriginCallback(referrerOrigin);
|
|
151
|
-
}
|
|
152
|
-
const currentURL = new URL(request.url);
|
|
153
|
-
switch (policy) {
|
|
154
|
-
case "no-referrer": return "no-referrer";
|
|
155
|
-
case "origin": return referrerOrigin;
|
|
156
|
-
case "unsafe-url": return referrerURL;
|
|
157
|
-
case "strict-origin":
|
|
158
|
-
if (isUrlPotentiallyTrustworthy(referrerURL) && !isUrlPotentiallyTrustworthy(currentURL)) {
|
|
159
|
-
return "no-referrer";
|
|
160
|
-
}
|
|
161
|
-
return referrerOrigin.toString();
|
|
162
|
-
case "strict-origin-when-cross-origin":
|
|
163
|
-
if (referrerURL.origin === currentURL.origin) {
|
|
164
|
-
return referrerURL;
|
|
165
|
-
}
|
|
166
|
-
if (isUrlPotentiallyTrustworthy(referrerURL) && !isUrlPotentiallyTrustworthy(currentURL)) {
|
|
167
|
-
return "no-referrer";
|
|
168
|
-
}
|
|
169
|
-
return referrerOrigin;
|
|
170
|
-
case "same-origin":
|
|
171
|
-
if (referrerURL.origin === currentURL.origin) {
|
|
172
|
-
return referrerURL;
|
|
173
|
-
}
|
|
174
|
-
return "no-referrer";
|
|
175
|
-
case "origin-when-cross-origin":
|
|
176
|
-
if (referrerURL.origin === currentURL.origin) {
|
|
177
|
-
return referrerURL;
|
|
178
|
-
}
|
|
179
|
-
return referrerOrigin;
|
|
180
|
-
case "no-referrer-when-downgrade":
|
|
181
|
-
if (isUrlPotentiallyTrustworthy(referrerURL) && !isUrlPotentiallyTrustworthy(currentURL)) {
|
|
182
|
-
return "no-referrer";
|
|
183
|
-
}
|
|
184
|
-
return referrerURL;
|
|
185
|
-
default: throw new TypeError(`Invalid referrerPolicy: ${policy}`);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* @see {@link https://w3c.github.io/webappsec-referrer-policy/#parse-referrer-policy-from-header|Referrer Policy §8.1. Parse a referrer policy from a Referrer-Policy header}
|
|
190
|
-
* @param {Headers} headers Response headers
|
|
191
|
-
* @returns {string} policy
|
|
192
|
-
*/
|
|
193
|
-
function parseReferrerPolicyFromHeader(headers) {
|
|
194
|
-
const policyTokens = (headers.get("referrer-policy") || "").split(/[,\s]+/);
|
|
195
|
-
let policy = "";
|
|
196
|
-
for (const token of policyTokens) {
|
|
197
|
-
if (token && ReferrerPolicy.has(token)) {
|
|
198
|
-
policy = token;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
return policy;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
//#endregion
|
|
205
|
-
export { DEFAULT_REFERRER_POLICY, ReferrerPolicy, determineRequestsReferrer, isOriginPotentiallyTrustworthy, isUrlPotentiallyTrustworthy, parseReferrerPolicyFromHeader, stripURLForUseAsAReferrer, validateReferrerPolicy };
|
|
1
|
+
import{URL as e}from"@gjsify/url";import{isIP as t}from"node:net";function n(t,n=!1){if(t==null||t===`no-referrer`)return`no-referrer`;let r=new e(t);return/^(about|blob|data):$/.test(r.protocol)?`no-referrer`:(r.username=``,r.password=``,r.hash=``,n&&(r.pathname=``,r.search=``),r)}const r=new Set([``,`no-referrer`,`no-referrer-when-downgrade`,`same-origin`,`origin`,`strict-origin`,`origin-when-cross-origin`,`strict-origin-when-cross-origin`,`unsafe-url`]),i=`strict-origin-when-cross-origin`;function a(e){if(!r.has(e))throw TypeError(`Invalid referrerPolicy: ${e}`);return e}function o(e){if(/^(http|ws)s:$/.test(e.protocol))return!0;let n=e.host.replace(/(^\[)|(]$)/g,``),r=t(n);return r===4&&/^127\./.test(n)||r===6&&/^(((0+:){7})|(::(0+:){0,6}))0*1$/.test(n)?!0:e.host===`localhost`||e.host.endsWith(`.localhost`)?!1:e.protocol===`file:`}function s(t){return/^about:(blank|srcdoc)$/.test(t.toString())||(typeof t==`string`&&(t=new e(t)),t.protocol===`data:`)||/^(blob|filesystem):$/.test(t.protocol)?!0:o(t)}function c(t,r={}){let{referrerURLCallback:i,referrerOriginCallback:a}=r;if(t.referrer===`no-referrer`||t.referrerPolicy===``)return null;let o=t.referrerPolicy;if(t.referrer===`about:client`)return`no-referrer`;let c=new e(t.referrer),l=n(c),u=n(c,!0);l.toString().length>4096&&(l=u),i&&(l=i(l)),a&&(u=a(u));let d=new e(t.url);switch(o){case`no-referrer`:return`no-referrer`;case`origin`:return u;case`unsafe-url`:return l;case`strict-origin`:return s(l)&&!s(d)?`no-referrer`:u.toString();case`strict-origin-when-cross-origin`:return l.origin===d.origin?l:s(l)&&!s(d)?`no-referrer`:u;case`same-origin`:return l.origin===d.origin?l:`no-referrer`;case`origin-when-cross-origin`:return l.origin===d.origin?l:u;case`no-referrer-when-downgrade`:return s(l)&&!s(d)?`no-referrer`:l;default:throw TypeError(`Invalid referrerPolicy: ${o}`)}}function l(e){let t=(e.get(`referrer-policy`)||``).split(/[,\s]+/),n=``;for(let e of t)e&&r.has(e)&&(n=e);return n}export{i as DEFAULT_REFERRER_POLICY,r as ReferrerPolicy,c as determineRequestsReferrer,o as isOriginPotentiallyTrustworthy,s as isUrlPotentiallyTrustworthy,l as parseReferrerPolicyFromHeader,n as stripURLForUseAsAReferrer,a as validateReferrerPolicy};
|
|
@@ -1,29 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import GLib from "@girs/glib-2.0";
|
|
3
|
-
import { inputStreamAsyncIterator } from "@gjsify/utils";
|
|
4
|
-
|
|
5
|
-
//#region src/utils/soup-helpers.ts
|
|
6
|
-
/**
|
|
7
|
-
* Promise wrapper around `Soup.Session.send_async` / `send_finish`.
|
|
8
|
-
*/
|
|
9
|
-
async function soupSendAsync(session, msg, ioPriority = GLib.PRIORITY_DEFAULT, cancellable = null) {
|
|
10
|
-
return new Promise((resolve, reject) => {
|
|
11
|
-
session.send_async(msg, ioPriority, cancellable, (_self, asyncRes) => {
|
|
12
|
-
try {
|
|
13
|
-
const inputStream = session.send_finish(asyncRes);
|
|
14
|
-
resolve(inputStream);
|
|
15
|
-
} catch (error) {
|
|
16
|
-
reject(error);
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Converts a `Gio.InputStream` to a Node.js `Readable` stream.
|
|
23
|
-
*/
|
|
24
|
-
function inputStreamToReadable(inputStream, options = {}) {
|
|
25
|
-
return Readable.from(inputStreamAsyncIterator(inputStream), options);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
//#endregion
|
|
29
|
-
export { inputStreamToReadable, soupSendAsync };
|
|
1
|
+
import{Readable as e}from"node:stream";import t from"@girs/glib-2.0";import{inputStreamAsyncIterator as n}from"@gjsify/utils";async function r(e,n,r=t.PRIORITY_DEFAULT,i=null){return new Promise((t,a)=>{e.send_async(n,r,i,(n,r)=>{try{t(e.send_finish(r))}catch(e){a(e)}})})}function i(t,r={}){return e.from(n(t),r)}export{i as inputStreamToReadable,r as soupSendAsync};
|
package/lib/esm/xhr.js
CHANGED
|
@@ -1,246 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
//#region src/xhr.ts
|
|
5
|
-
let _blobCounter = 0;
|
|
6
|
-
function guessBlobExt(url) {
|
|
7
|
-
const lower = url.toLowerCase().split("?")[0];
|
|
8
|
-
const dot = lower.lastIndexOf(".");
|
|
9
|
-
const ext = dot > -1 ? lower.slice(dot) : "";
|
|
10
|
-
switch (ext) {
|
|
11
|
-
case ".png":
|
|
12
|
-
case ".jpg":
|
|
13
|
-
case ".jpeg":
|
|
14
|
-
case ".gif":
|
|
15
|
-
case ".webp":
|
|
16
|
-
case ".svg":
|
|
17
|
-
case ".bmp":
|
|
18
|
-
case ".ttf":
|
|
19
|
-
case ".otf":
|
|
20
|
-
case ".woff":
|
|
21
|
-
case ".woff2":
|
|
22
|
-
case ".mp3":
|
|
23
|
-
case ".wav":
|
|
24
|
-
case ".ogg":
|
|
25
|
-
case ".flac":
|
|
26
|
-
case ".m4a":
|
|
27
|
-
case ".mp4":
|
|
28
|
-
case ".webm":
|
|
29
|
-
case ".mkv":
|
|
30
|
-
case ".xml":
|
|
31
|
-
case ".tmx":
|
|
32
|
-
case ".json": return ext;
|
|
33
|
-
default: return ".bin";
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function writeBlobToTempFile(bytes, url) {
|
|
37
|
-
const tmpPath = GLib.build_filenamev([GLib.get_tmp_dir(), `gjsify-blob-${_blobCounter++}${guessBlobExt(url)}`]);
|
|
38
|
-
GLib.file_set_contents(tmpPath, bytes);
|
|
39
|
-
return tmpPath;
|
|
40
|
-
}
|
|
41
|
-
const UNSENT = 0;
|
|
42
|
-
const OPENED = 1;
|
|
43
|
-
const HEADERS_RECEIVED = 2;
|
|
44
|
-
const LOADING = 3;
|
|
45
|
-
const DONE = 4;
|
|
46
|
-
var XMLHttpRequest = class extends EventTarget {
|
|
47
|
-
static UNSENT = 0;
|
|
48
|
-
static OPENED = 1;
|
|
49
|
-
static HEADERS_RECEIVED = 2;
|
|
50
|
-
static LOADING = 3;
|
|
51
|
-
static DONE = 4;
|
|
52
|
-
UNSENT = 0;
|
|
53
|
-
OPENED = 1;
|
|
54
|
-
HEADERS_RECEIVED = 2;
|
|
55
|
-
LOADING = 3;
|
|
56
|
-
DONE = 4;
|
|
57
|
-
readyState = 0;
|
|
58
|
-
status = 0;
|
|
59
|
-
statusText = "";
|
|
60
|
-
responseType = "";
|
|
61
|
-
responseText = "";
|
|
62
|
-
response = null;
|
|
63
|
-
responseURL = "";
|
|
64
|
-
withCredentials = false;
|
|
65
|
-
timeout = 0;
|
|
66
|
-
upload = new XMLHttpRequestUpload();
|
|
67
|
-
onreadystatechange = null;
|
|
68
|
-
onload = null;
|
|
69
|
-
onerror = null;
|
|
70
|
-
onabort = null;
|
|
71
|
-
ontimeout = null;
|
|
72
|
-
onloadstart = null;
|
|
73
|
-
onloadend = null;
|
|
74
|
-
onprogress = null;
|
|
75
|
-
_method = "GET";
|
|
76
|
-
_url = "";
|
|
77
|
-
_headers = new Map();
|
|
78
|
-
_responseHeaders = new Map();
|
|
79
|
-
_controller = new AbortController();
|
|
80
|
-
_aborted = false;
|
|
81
|
-
_timeoutId = null;
|
|
82
|
-
open(method, url, _async = true, _user, _password) {
|
|
83
|
-
this._method = method.toUpperCase();
|
|
84
|
-
this._url = url;
|
|
85
|
-
this._headers.clear();
|
|
86
|
-
this._responseHeaders.clear();
|
|
87
|
-
this._aborted = false;
|
|
88
|
-
this._controller = new AbortController();
|
|
89
|
-
this._setReadyState(1);
|
|
90
|
-
}
|
|
91
|
-
setRequestHeader(header, value) {
|
|
92
|
-
if (this.readyState < 1) throw new DOMException("Must open first", "InvalidStateError");
|
|
93
|
-
this._headers.set(header.toLowerCase(), value);
|
|
94
|
-
}
|
|
95
|
-
getResponseHeader(header) {
|
|
96
|
-
return this._responseHeaders.get(header.toLowerCase()) ?? null;
|
|
97
|
-
}
|
|
98
|
-
getAllResponseHeaders() {
|
|
99
|
-
const lines = [];
|
|
100
|
-
this._responseHeaders.forEach((v, k) => lines.push(`${k}: ${v}`));
|
|
101
|
-
return lines.join("\r\n");
|
|
102
|
-
}
|
|
103
|
-
send(body) {
|
|
104
|
-
if (this.readyState !== 1) throw new DOMException("Must open first", "InvalidStateError");
|
|
105
|
-
if (this._aborted) return;
|
|
106
|
-
const headersInit = {};
|
|
107
|
-
this._headers.forEach((v, k) => {
|
|
108
|
-
headersInit[k] = v;
|
|
109
|
-
});
|
|
110
|
-
const fetchOptions = {
|
|
111
|
-
method: this._method,
|
|
112
|
-
headers: headersInit,
|
|
113
|
-
credentials: this.withCredentials ? "include" : "omit",
|
|
114
|
-
signal: this._controller.signal
|
|
115
|
-
};
|
|
116
|
-
if (body != null && this._method !== "GET" && this._method !== "HEAD") {
|
|
117
|
-
fetchOptions.body = body;
|
|
118
|
-
}
|
|
119
|
-
if (this.timeout > 0) {
|
|
120
|
-
this._timeoutId = setTimeout(() => {
|
|
121
|
-
this._controller.abort();
|
|
122
|
-
this._onTimeout();
|
|
123
|
-
}, this.timeout);
|
|
124
|
-
}
|
|
125
|
-
this.dispatchEvent(new Event("loadstart"));
|
|
126
|
-
if (this.onloadstart) this.onloadstart(new ProgressEvent("loadstart"));
|
|
127
|
-
fetch(this._url, fetchOptions).then(async (res) => {
|
|
128
|
-
if (this._aborted) return;
|
|
129
|
-
if (this._timeoutId) {
|
|
130
|
-
clearTimeout(this._timeoutId);
|
|
131
|
-
this._timeoutId = null;
|
|
132
|
-
}
|
|
133
|
-
this.status = res.status;
|
|
134
|
-
this.statusText = res.statusText;
|
|
135
|
-
this.responseURL = res.url;
|
|
136
|
-
res.headers.forEach((v, k) => {
|
|
137
|
-
this._responseHeaders.set(k.toLowerCase(), v);
|
|
138
|
-
});
|
|
139
|
-
this._setReadyState(2);
|
|
140
|
-
this._setReadyState(3);
|
|
141
|
-
switch (this.responseType) {
|
|
142
|
-
case "arraybuffer": {
|
|
143
|
-
const ab = await res.arrayBuffer();
|
|
144
|
-
this.response = ab;
|
|
145
|
-
this.responseText = "";
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
case "blob": {
|
|
149
|
-
const ab = await res.arrayBuffer();
|
|
150
|
-
const bytes = new Uint8Array(ab);
|
|
151
|
-
const tmpPath = writeBlobToTempFile(bytes, this._url);
|
|
152
|
-
const blob = new Blob([ab], { type: this._responseHeaders.get("content-type") ?? "" });
|
|
153
|
-
blob._tmpPath = tmpPath;
|
|
154
|
-
this.response = blob;
|
|
155
|
-
this.responseText = "";
|
|
156
|
-
break;
|
|
157
|
-
}
|
|
158
|
-
case "json": {
|
|
159
|
-
const text = await res.text();
|
|
160
|
-
this.responseText = "";
|
|
161
|
-
try {
|
|
162
|
-
this.response = text.length > 0 ? JSON.parse(text) : null;
|
|
163
|
-
} catch {
|
|
164
|
-
this.response = null;
|
|
165
|
-
}
|
|
166
|
-
break;
|
|
167
|
-
}
|
|
168
|
-
case "document": {
|
|
169
|
-
const text = await res.text();
|
|
170
|
-
this.responseText = text;
|
|
171
|
-
this.response = text;
|
|
172
|
-
break;
|
|
173
|
-
}
|
|
174
|
-
case "":
|
|
175
|
-
case "text":
|
|
176
|
-
default: {
|
|
177
|
-
const text = await res.text();
|
|
178
|
-
const stripped = text.charCodeAt(0) === 65279 ? text.slice(1) : text;
|
|
179
|
-
this.responseText = stripped;
|
|
180
|
-
this.response = stripped;
|
|
181
|
-
break;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
this._setReadyState(4);
|
|
185
|
-
this.dispatchEvent(new ProgressEvent("load"));
|
|
186
|
-
this.dispatchEvent(new ProgressEvent("loadend"));
|
|
187
|
-
if (this.onload) this.onload(new ProgressEvent("load"));
|
|
188
|
-
if (this.onloadend) this.onloadend(new ProgressEvent("loadend"));
|
|
189
|
-
}).catch((_err) => {
|
|
190
|
-
if (this._timeoutId) {
|
|
191
|
-
clearTimeout(this._timeoutId);
|
|
192
|
-
this._timeoutId = null;
|
|
193
|
-
}
|
|
194
|
-
if (this._aborted) return;
|
|
195
|
-
this._setReadyState(4);
|
|
196
|
-
const ev = new ProgressEvent("error");
|
|
197
|
-
this.dispatchEvent(ev);
|
|
198
|
-
if (this.onerror) this.onerror(ev);
|
|
199
|
-
if (this.onloadend) this.onloadend(new ProgressEvent("loadend"));
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
abort() {
|
|
203
|
-
if (this._aborted) return;
|
|
204
|
-
this._aborted = true;
|
|
205
|
-
if (this._timeoutId) {
|
|
206
|
-
clearTimeout(this._timeoutId);
|
|
207
|
-
this._timeoutId = null;
|
|
208
|
-
}
|
|
209
|
-
this._controller.abort();
|
|
210
|
-
if (this.readyState !== 0 && this.readyState !== 4) {
|
|
211
|
-
this._setReadyState(4);
|
|
212
|
-
this.status = 0;
|
|
213
|
-
}
|
|
214
|
-
const ev = new ProgressEvent("abort");
|
|
215
|
-
this.dispatchEvent(ev);
|
|
216
|
-
if (this.onabort) this.onabort(ev);
|
|
217
|
-
if (this.onloadend) this.onloadend(new ProgressEvent("loadend"));
|
|
218
|
-
}
|
|
219
|
-
overrideMimeType(_mime) {}
|
|
220
|
-
_onTimeout() {
|
|
221
|
-
if (this._aborted) return;
|
|
222
|
-
this._aborted = true;
|
|
223
|
-
this._setReadyState(4);
|
|
224
|
-
const ev = new ProgressEvent("timeout");
|
|
225
|
-
this.dispatchEvent(ev);
|
|
226
|
-
if (this.ontimeout) this.ontimeout(ev);
|
|
227
|
-
}
|
|
228
|
-
_setReadyState(state) {
|
|
229
|
-
this.readyState = state;
|
|
230
|
-
const ev = new Event("readystatechange");
|
|
231
|
-
this.dispatchEvent(ev);
|
|
232
|
-
if (this.onreadystatechange) this.onreadystatechange(ev);
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
var XMLHttpRequestUpload = class extends EventTarget {
|
|
236
|
-
onprogress = null;
|
|
237
|
-
onloadstart = null;
|
|
238
|
-
onloadend = null;
|
|
239
|
-
onload = null;
|
|
240
|
-
onerror = null;
|
|
241
|
-
onabort = null;
|
|
242
|
-
ontimeout = null;
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
//#endregion
|
|
246
|
-
export { DONE, HEADERS_RECEIVED, LOADING, OPENED, UNSENT, XMLHttpRequest, XMLHttpRequestUpload };
|
|
1
|
+
import e from"./index.js";import t from"gi://GLib?version=2.0";let n=0;function r(e){let t=e.toLowerCase().split(`?`)[0],n=t.lastIndexOf(`.`),r=n>-1?t.slice(n):``;switch(r){case`.png`:case`.jpg`:case`.jpeg`:case`.gif`:case`.webp`:case`.svg`:case`.bmp`:case`.ttf`:case`.otf`:case`.woff`:case`.woff2`:case`.mp3`:case`.wav`:case`.ogg`:case`.flac`:case`.m4a`:case`.mp4`:case`.webm`:case`.mkv`:case`.xml`:case`.tmx`:case`.json`:return r;default:return`.bin`}}function i(e,i){let a=t.build_filenamev([t.get_tmp_dir(),`gjsify-blob-${n++}${r(i)}`]);return t.file_set_contents(a,e),a}const a=0,o=1,s=2,c=3,l=4;var u=class extends EventTarget{static UNSENT=0;static OPENED=1;static HEADERS_RECEIVED=2;static LOADING=3;static DONE=4;UNSENT=0;OPENED=1;HEADERS_RECEIVED=2;LOADING=3;DONE=4;readyState=0;status=0;statusText=``;responseType=``;responseText=``;response=null;responseURL=``;withCredentials=!1;timeout=0;upload=new d;onreadystatechange=null;onload=null;onerror=null;onabort=null;ontimeout=null;onloadstart=null;onloadend=null;onprogress=null;_method=`GET`;_url=``;_headers=new Map;_responseHeaders=new Map;_controller=new AbortController;_aborted=!1;_timeoutId=null;open(e,t,n=!0,r,i){this._method=e.toUpperCase(),this._url=t,this._headers.clear(),this._responseHeaders.clear(),this._aborted=!1,this._controller=new AbortController,this._setReadyState(1)}setRequestHeader(e,t){if(this.readyState<1)throw new DOMException(`Must open first`,`InvalidStateError`);this._headers.set(e.toLowerCase(),t)}getResponseHeader(e){return this._responseHeaders.get(e.toLowerCase())??null}getAllResponseHeaders(){let e=[];return this._responseHeaders.forEach((t,n)=>e.push(`${n}: ${t}`)),e.join(`\r
|
|
2
|
+
`)}send(t){if(this.readyState!==1)throw new DOMException(`Must open first`,`InvalidStateError`);if(this._aborted)return;let n={};this._headers.forEach((e,t)=>{n[t]=e});let r={method:this._method,headers:n,credentials:this.withCredentials?`include`:`omit`,signal:this._controller.signal};t!=null&&this._method!==`GET`&&this._method!==`HEAD`&&(r.body=t),this.timeout>0&&(this._timeoutId=setTimeout(()=>{this._controller.abort(),this._onTimeout()},this.timeout)),this.dispatchEvent(new Event(`loadstart`)),this.onloadstart&&this.onloadstart(new ProgressEvent(`loadstart`)),e(this._url,r).then(async e=>{if(!this._aborted){switch(this._timeoutId&&=(clearTimeout(this._timeoutId),null),this.status=e.status,this.statusText=e.statusText,this.responseURL=e.url,e.headers.forEach((e,t)=>{this._responseHeaders.set(t.toLowerCase(),e)}),this._setReadyState(2),this._setReadyState(3),this.responseType){case`arraybuffer`:{let t=await e.arrayBuffer();this.response=t,this.responseText=``;break}case`blob`:{let t=await e.arrayBuffer(),n=i(new Uint8Array(t),this._url),r=new Blob([t],{type:this._responseHeaders.get(`content-type`)??``});r._tmpPath=n,this.response=r,this.responseText=``;break}case`json`:{let t=await e.text();this.responseText=``;try{this.response=t.length>0?JSON.parse(t):null}catch{this.response=null}break}case`document`:{let t=await e.text();this.responseText=t,this.response=t;break}default:{let t=await e.text(),n=t.charCodeAt(0)===65279?t.slice(1):t;this.responseText=n,this.response=n;break}}this._setReadyState(4),this.dispatchEvent(new ProgressEvent(`load`)),this.dispatchEvent(new ProgressEvent(`loadend`)),this.onload&&this.onload(new ProgressEvent(`load`)),this.onloadend&&this.onloadend(new ProgressEvent(`loadend`))}}).catch(e=>{if(this._timeoutId&&=(clearTimeout(this._timeoutId),null),this._aborted)return;this._setReadyState(4);let t=new ProgressEvent(`error`);this.dispatchEvent(t),this.onerror&&this.onerror(t),this.onloadend&&this.onloadend(new ProgressEvent(`loadend`))})}abort(){if(this._aborted)return;this._aborted=!0,this._timeoutId&&=(clearTimeout(this._timeoutId),null),this._controller.abort(),this.readyState!==0&&this.readyState!==4&&(this._setReadyState(4),this.status=0);let e=new ProgressEvent(`abort`);this.dispatchEvent(e),this.onabort&&this.onabort(e),this.onloadend&&this.onloadend(new ProgressEvent(`loadend`))}overrideMimeType(e){}_onTimeout(){if(this._aborted)return;this._aborted=!0,this._setReadyState(4);let e=new ProgressEvent(`timeout`);this.dispatchEvent(e),this.ontimeout&&this.ontimeout(e)}_setReadyState(e){this.readyState=e;let t=new Event(`readystatechange`);this.dispatchEvent(t),this.onreadystatechange&&this.onreadystatechange(t)}},d=class extends EventTarget{onprogress=null;onloadstart=null;onloadend=null;onload=null;onerror=null;onabort=null;ontimeout=null};export{l as DONE,s as HEADERS_RECEIVED,c as LOADING,o as OPENED,a as UNSENT,u as XMLHttpRequest,d as XMLHttpRequestUpload};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/fetch",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.17",
|
|
4
4
|
"description": "Web and Node.js fetch module for Gjs",
|
|
5
5
|
"module": "lib/esm/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -45,19 +45,19 @@
|
|
|
45
45
|
"fetch"
|
|
46
46
|
],
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@gjsify/cli": "^0.3.
|
|
49
|
-
"@gjsify/unit": "^0.3.
|
|
50
|
-
"@types/node": "^25.6.
|
|
48
|
+
"@gjsify/cli": "^0.3.17",
|
|
49
|
+
"@gjsify/unit": "^0.3.17",
|
|
50
|
+
"@types/node": "^25.6.2",
|
|
51
51
|
"typescript": "^6.0.3"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@girs/gio-2.0": "2.88.0-4.0.0-rc.
|
|
55
|
-
"@girs/gjs": "4.0.0-rc.
|
|
56
|
-
"@girs/glib-2.0": "2.88.0-4.0.0-rc.
|
|
57
|
-
"@girs/soup-3.0": "3.6.6-4.0.0-rc.
|
|
58
|
-
"@gjsify/formdata": "^0.3.
|
|
59
|
-
"@gjsify/http": "^0.3.
|
|
60
|
-
"@gjsify/url": "^0.3.
|
|
61
|
-
"@gjsify/utils": "^0.3.
|
|
54
|
+
"@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
|
|
55
|
+
"@girs/gjs": "4.0.0-rc.14",
|
|
56
|
+
"@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
|
|
57
|
+
"@girs/soup-3.0": "3.6.6-4.0.0-rc.14",
|
|
58
|
+
"@gjsify/formdata": "^0.3.17",
|
|
59
|
+
"@gjsify/http": "^0.3.17",
|
|
60
|
+
"@gjsify/url": "^0.3.17",
|
|
61
|
+
"@gjsify/utils": "^0.3.17"
|
|
62
62
|
}
|
|
63
63
|
}
|