@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/xhr.js CHANGED
@@ -1,258 +1,246 @@
1
- import GLib from "gi://GLib?version=2.0";
2
1
  import fetch from "./index.js";
2
+ import GLib from "gi://GLib?version=2.0";
3
+
4
+ //#region src/xhr.ts
3
5
  let _blobCounter = 0;
4
6
  function guessBlobExt(url) {
5
- const lower = url.toLowerCase().split("?")[0];
6
- const dot = lower.lastIndexOf(".");
7
- const ext = dot > -1 ? lower.slice(dot) : "";
8
- switch (ext) {
9
- case ".png":
10
- case ".jpg":
11
- case ".jpeg":
12
- case ".gif":
13
- case ".webp":
14
- case ".svg":
15
- case ".bmp":
16
- case ".ttf":
17
- case ".otf":
18
- case ".woff":
19
- case ".woff2":
20
- case ".mp3":
21
- case ".wav":
22
- case ".ogg":
23
- case ".flac":
24
- case ".m4a":
25
- case ".mp4":
26
- case ".webm":
27
- case ".mkv":
28
- case ".xml":
29
- case ".tmx":
30
- case ".json":
31
- return ext;
32
- default:
33
- return ".bin";
34
- }
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
35
  }
36
36
  function writeBlobToTempFile(bytes, url) {
37
- const tmpPath = GLib.build_filenamev([
38
- GLib.get_tmp_dir(),
39
- `gjsify-blob-${_blobCounter++}${guessBlobExt(url)}`
40
- ]);
41
- GLib.file_set_contents(tmpPath, bytes);
42
- return tmpPath;
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;
43
40
  }
44
41
  const UNSENT = 0;
45
42
  const OPENED = 1;
46
43
  const HEADERS_RECEIVED = 2;
47
44
  const LOADING = 3;
48
45
  const DONE = 4;
49
- class XMLHttpRequest extends EventTarget {
50
- static UNSENT = UNSENT;
51
- static OPENED = OPENED;
52
- static HEADERS_RECEIVED = HEADERS_RECEIVED;
53
- static LOADING = LOADING;
54
- static DONE = DONE;
55
- UNSENT = UNSENT;
56
- OPENED = OPENED;
57
- HEADERS_RECEIVED = HEADERS_RECEIVED;
58
- LOADING = LOADING;
59
- DONE = DONE;
60
- readyState = UNSENT;
61
- status = 0;
62
- statusText = "";
63
- responseType = "";
64
- responseText = "";
65
- response = null;
66
- responseURL = "";
67
- withCredentials = false;
68
- timeout = 0;
69
- upload = new XMLHttpRequestUpload();
70
- onreadystatechange = null;
71
- onload = null;
72
- onerror = null;
73
- onabort = null;
74
- ontimeout = null;
75
- onloadstart = null;
76
- onloadend = null;
77
- onprogress = null;
78
- _method = "GET";
79
- _url = "";
80
- _headers = /* @__PURE__ */ new Map();
81
- _responseHeaders = /* @__PURE__ */ new Map();
82
- _controller = new AbortController();
83
- _aborted = false;
84
- _timeoutId = null;
85
- open(method, url, _async = true, _user, _password) {
86
- this._method = method.toUpperCase();
87
- this._url = url;
88
- this._headers.clear();
89
- this._responseHeaders.clear();
90
- this._aborted = false;
91
- this._controller = new AbortController();
92
- this._setReadyState(OPENED);
93
- }
94
- setRequestHeader(header, value) {
95
- if (this.readyState < OPENED) throw new DOMException("Must open first", "InvalidStateError");
96
- this._headers.set(header.toLowerCase(), value);
97
- }
98
- getResponseHeader(header) {
99
- return this._responseHeaders.get(header.toLowerCase()) ?? null;
100
- }
101
- getAllResponseHeaders() {
102
- const lines = [];
103
- this._responseHeaders.forEach((v, k) => lines.push(`${k}: ${v}`));
104
- return lines.join("\r\n");
105
- }
106
- send(body) {
107
- if (this.readyState !== OPENED) throw new DOMException("Must open first", "InvalidStateError");
108
- if (this._aborted) return;
109
- const headersInit = {};
110
- this._headers.forEach((v, k) => {
111
- headersInit[k] = v;
112
- });
113
- const fetchOptions = {
114
- method: this._method,
115
- headers: headersInit,
116
- credentials: this.withCredentials ? "include" : "omit",
117
- signal: this._controller.signal
118
- };
119
- if (body != null && this._method !== "GET" && this._method !== "HEAD") {
120
- fetchOptions.body = body;
121
- }
122
- if (this.timeout > 0) {
123
- this._timeoutId = setTimeout(() => {
124
- this._controller.abort();
125
- this._onTimeout();
126
- }, this.timeout);
127
- }
128
- this.dispatchEvent(new Event("loadstart"));
129
- if (this.onloadstart) this.onloadstart(new ProgressEvent("loadstart"));
130
- fetch(this._url, fetchOptions).then(async (res) => {
131
- if (this._aborted) return;
132
- if (this._timeoutId) {
133
- clearTimeout(this._timeoutId);
134
- this._timeoutId = null;
135
- }
136
- this.status = res.status;
137
- this.statusText = res.statusText;
138
- this.responseURL = res.url;
139
- res.headers.forEach((v, k) => {
140
- this._responseHeaders.set(k.toLowerCase(), v);
141
- });
142
- this._setReadyState(HEADERS_RECEIVED);
143
- this._setReadyState(LOADING);
144
- switch (this.responseType) {
145
- case "arraybuffer": {
146
- const ab = await res.arrayBuffer();
147
- this.response = ab;
148
- this.responseText = "";
149
- break;
150
- }
151
- case "blob": {
152
- const ab = await res.arrayBuffer();
153
- const bytes = new Uint8Array(ab);
154
- const tmpPath = writeBlobToTempFile(bytes, this._url);
155
- const blob = new Blob([ab], {
156
- type: this._responseHeaders.get("content-type") ?? ""
157
- });
158
- blob._tmpPath = tmpPath;
159
- this.response = blob;
160
- this.responseText = "";
161
- break;
162
- }
163
- case "json": {
164
- const text = await res.text();
165
- this.responseText = "";
166
- try {
167
- this.response = text.length > 0 ? JSON.parse(text) : null;
168
- } catch {
169
- this.response = null;
170
- }
171
- break;
172
- }
173
- case "document": {
174
- const text = await res.text();
175
- this.responseText = text;
176
- this.response = text;
177
- break;
178
- }
179
- case "":
180
- case "text":
181
- default: {
182
- const text = await res.text();
183
- const stripped = text.charCodeAt(0) === 65279 ? text.slice(1) : text;
184
- this.responseText = stripped;
185
- this.response = stripped;
186
- break;
187
- }
188
- }
189
- this._setReadyState(DONE);
190
- this.dispatchEvent(new ProgressEvent("load"));
191
- this.dispatchEvent(new ProgressEvent("loadend"));
192
- if (this.onload) this.onload(new ProgressEvent("load"));
193
- if (this.onloadend) this.onloadend(new ProgressEvent("loadend"));
194
- }).catch((_err) => {
195
- if (this._timeoutId) {
196
- clearTimeout(this._timeoutId);
197
- this._timeoutId = null;
198
- }
199
- if (this._aborted) return;
200
- this._setReadyState(DONE);
201
- const ev = new ProgressEvent("error");
202
- this.dispatchEvent(ev);
203
- if (this.onerror) this.onerror(ev);
204
- if (this.onloadend) this.onloadend(new ProgressEvent("loadend"));
205
- });
206
- }
207
- abort() {
208
- if (this._aborted) return;
209
- this._aborted = true;
210
- if (this._timeoutId) {
211
- clearTimeout(this._timeoutId);
212
- this._timeoutId = null;
213
- }
214
- this._controller.abort();
215
- if (this.readyState !== UNSENT && this.readyState !== DONE) {
216
- this._setReadyState(DONE);
217
- this.status = 0;
218
- }
219
- const ev = new ProgressEvent("abort");
220
- this.dispatchEvent(ev);
221
- if (this.onabort) this.onabort(ev);
222
- if (this.onloadend) this.onloadend(new ProgressEvent("loadend"));
223
- }
224
- overrideMimeType(_mime) {
225
- }
226
- _onTimeout() {
227
- if (this._aborted) return;
228
- this._aborted = true;
229
- this._setReadyState(DONE);
230
- const ev = new ProgressEvent("timeout");
231
- this.dispatchEvent(ev);
232
- if (this.ontimeout) this.ontimeout(ev);
233
- }
234
- _setReadyState(state) {
235
- this.readyState = state;
236
- const ev = new Event("readystatechange");
237
- this.dispatchEvent(ev);
238
- if (this.onreadystatechange) this.onreadystatechange(ev);
239
- }
240
- }
241
- class XMLHttpRequestUpload extends EventTarget {
242
- onprogress = null;
243
- onloadstart = null;
244
- onloadend = null;
245
- onload = null;
246
- onerror = null;
247
- onabort = null;
248
- ontimeout = null;
249
- }
250
- export {
251
- DONE,
252
- HEADERS_RECEIVED,
253
- LOADING,
254
- OPENED,
255
- UNSENT,
256
- XMLHttpRequest,
257
- XMLHttpRequestUpload
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;
258
243
  };
244
+
245
+ //#endregion
246
+ export { DONE, HEADERS_RECEIVED, LOADING, OPENED, UNSENT, XMLHttpRequest, XMLHttpRequestUpload };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/fetch",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
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.13",
49
- "@gjsify/unit": "^0.3.13",
48
+ "@gjsify/cli": "^0.3.15",
49
+ "@gjsify/unit": "^0.3.15",
50
50
  "@types/node": "^25.6.0",
51
51
  "typescript": "^6.0.3"
52
52
  },
53
53
  "dependencies": {
54
- "@girs/gio-2.0": "^2.88.0-4.0.0-rc.9",
55
- "@girs/gjs": "^4.0.0-rc.9",
56
- "@girs/glib-2.0": "^2.88.0-4.0.0-rc.9",
57
- "@girs/soup-3.0": "^3.6.6-4.0.0-rc.9",
58
- "@gjsify/formdata": "^0.3.13",
59
- "@gjsify/http": "^0.3.13",
60
- "@gjsify/url": "^0.3.13",
61
- "@gjsify/utils": "^0.3.13"
54
+ "@girs/gio-2.0": "2.88.0-4.0.0-rc.9",
55
+ "@girs/gjs": "4.0.0-rc.9",
56
+ "@girs/glib-2.0": "2.88.0-4.0.0-rc.9",
57
+ "@girs/soup-3.0": "3.6.6-4.0.0-rc.9",
58
+ "@gjsify/formdata": "^0.3.15",
59
+ "@gjsify/http": "^0.3.15",
60
+ "@gjsify/url": "^0.3.15",
61
+ "@gjsify/utils": "^0.3.15"
62
62
  }
63
63
  }