@gjsify/xmlhttprequest 0.45.0 → 0.47.0

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/index.js CHANGED
@@ -1 +1 @@
1
- import"./_virtual/_rolldown/runtime.js";import e from"gi://GLib?version=2.0";import t,{resolveRootRelativeUrl as n}from"@gjsify/fetch";let r=0;var FakeBlob=class{_tmpPath;type;size;constructor(e,t){this.type=e,this.size=t}async arrayBuffer(){if(this._tmpPath){let[t,n]=e.file_get_contents(this._tmpPath);if(t)return n.buffer}return new ArrayBuffer(0)}async text(){return new TextDecoder().decode(await this.arrayBuffer())}stream(){let e=this;return new ReadableStream({async start(t){let n=await e.arrayBuffer();t.enqueue(new Uint8Array(n)),t.close()}})}};function guessExt(e){let t=e.toLowerCase();return t.endsWith(`.png`)?`.png`:t.endsWith(`.jpg`)||t.endsWith(`.jpeg`)?`.jpg`:t.endsWith(`.gif`)?`.gif`:t.endsWith(`.svg`)?`.svg`:t.endsWith(`.ttf`)?`.ttf`:t.endsWith(`.otf`)?`.otf`:t.endsWith(`.woff`)?`.woff`:t.endsWith(`.woff2`)?`.woff2`:t.endsWith(`.mp3`)?`.mp3`:t.endsWith(`.wav`)?`.wav`:t.endsWith(`.ogg`)?`.ogg`:t.endsWith(`.tmx`)||t.endsWith(`.xml`)?`.xml`:t.endsWith(`.json`)?`.json`:`.bin`}function guessMime(e){let t=e.toLowerCase();return t.endsWith(`.png`)?`image/png`:t.endsWith(`.jpg`)||t.endsWith(`.jpeg`)?`image/jpeg`:t.endsWith(`.gif`)?`image/gif`:t.endsWith(`.svg`)?`image/svg+xml`:t.endsWith(`.ttf`)?`font/truetype`:t.endsWith(`.otf`)?`font/otf`:t.endsWith(`.woff`)?`font/woff`:t.endsWith(`.woff2`)?`font/woff2`:t.endsWith(`.mp3`)?`audio/mpeg`:t.endsWith(`.wav`)?`audio/wav`:t.endsWith(`.ogg`)?`audio/ogg`:t.endsWith(`.json`)?`application/json`:`application/octet-stream`}async function readFileUrl(t){let n=e.filename_from_uri(t)[0],[r,i]=e.file_get_contents(n);if(!r)throw Error(`XMLHttpRequest: cannot read file ${n}`);let a=i,o=new Uint8Array(a.byteLength);return o.set(a),o.buffer}function writeToTmp(t,n){let i=e.build_filenamev([e.get_tmp_dir(),`gjsify-blob-${r++}${n}`]);return e.file_set_contents(i,t),i}var XMLHttpRequest=class{UNSENT=0;OPENED=1;HEADERS_RECEIVED=2;LOADING=3;DONE=4;readyState=0;status=0;statusText=``;response=null;responseText=``;responseType=``;responseURL=``;timeout=0;withCredentials=!1;onloadstart=null;onprogress=null;onload=null;onloadend=null;onerror=null;onabort=null;ontimeout=null;onreadystatechange=null;_url=``;_method=`GET`;_aborted=!1;_listeners={};open(e,t,n=!0){this._method=e.toUpperCase(),this._url=t,this._aborted=!1,this.readyState=this.OPENED}setRequestHeader(e,t){}overrideMimeType(e){}send(e){let r=this._url,i=this.responseType,a=globalThis.__GJSIFY_DEBUG_XHR===!0;r=n(r),a&&r!==this._url&&console.log(`[xmlhttprequest] rewrite ${this._url} → ${r}`),a&&console.log(`[xmlhttprequest] ${this._method} ${r} responseType=${i}`);let doFetch=()=>r.startsWith(`file://`)?readFileUrl(r):t(r,{method:this._method}).then(e=>(a&&console.log(`[xmlhttprequest] fetch ok ${r} status=${e.status}`),this.status=e.status===0?200:e.status,this.statusText=e.statusText||`OK`,this.responseURL=e.url||r,e.arrayBuffer()));this.readyState=this.LOADING,this._emit(`loadstart`,{loaded:0,total:0,lengthComputable:!1}),Promise.resolve().then(doFetch).then(e=>{if(this._aborted)return;this.status===0&&(this.status=200),this.statusText||=`OK`,this.readyState=this.DONE;let t=new Uint8Array(e),n=t.byteLength;if(this._emit(`progress`,{loaded:n,total:n,lengthComputable:!0}),i===`blob`||i===``){let e=writeToTmp(t,guessExt(r)),i=new FakeBlob(guessMime(r),n);i._tmpPath=e,this.response=i}else i===`arraybuffer`?this.response=e:i===`json`?this.response=JSON.parse(new TextDecoder().decode(t)):(this.responseText=new TextDecoder().decode(t),this.response=this.responseText);this._emit(`load`,{target:this,loaded:n,total:n,lengthComputable:!0}),this._emit(`loadend`,{target:this,loaded:n,total:n,lengthComputable:!0}),this.onreadystatechange&&this.onreadystatechange({})}).catch(e=>{this._aborted||(console.warn(`[xmlhttprequest] ${this._method} ${r} — ${e?.message??e}`),this.readyState=this.DONE,this._emit(`error`,{error:e}),this._emit(`loadend`,{}))})}abort(){this._aborted=!0,this.readyState=this.DONE,this._emit(`abort`,{}),this._emit(`loadend`,{})}addEventListener(e,t){(this._listeners[e]??=[]).push(t)}removeEventListener(e,t){this._listeners[e]=(this._listeners[e]??[]).filter(e=>e!==t)}getResponseHeader(e){return null}getAllResponseHeaders(){return``}_emit(e,t){let n=this[`on`+e];typeof n==`function`&&n.call(this,{type:e,...t});for(let n of this._listeners[e]??[])n.call(this,{type:e,...t})}};function installObjectURLSupport(){let t=URL;if(typeof t.createObjectURL!=`function`||t.__gjsify_objecturl!==!0){let n=new Map;t.createObjectURL=function(e){if(e._tmpPath){let t=`file://${e._tmpPath}`;return n.set(t,e._tmpPath),t}return console.warn(`[createObjectURL] received non-FakeBlob — cannot create file:// URL`),`file:///dev/null`},t.revokeObjectURL=function(t){let r=n.get(t);if(r){let i=e.build_filenamev([r]);e.unlink(i),n.delete(t)}},t.__gjsify_objecturl=!0}}export{FakeBlob,XMLHttpRequest,installObjectURLSupport};
1
+ import"./_virtual/_rolldown/runtime.js";import e from"gi://GLib?version=2.0";import t,{resolveRootRelativeUrl as n}from"@gjsify/fetch";let r=0;var FakeBlob=class{_tmpPath;type;size;constructor(e,t){this.type=e,this.size=t}async arrayBuffer(){if(this._tmpPath){let[t,n]=e.file_get_contents(this._tmpPath);if(t)return n.buffer}return new ArrayBuffer(0)}async text(){return new TextDecoder().decode(await this.arrayBuffer())}stream(){let e=this;return new ReadableStream({async start(t){let n=await e.arrayBuffer();t.enqueue(new Uint8Array(n)),t.close()}})}};function guessExt(e){let t=e.toLowerCase();return t.endsWith(`.png`)?`.png`:t.endsWith(`.jpg`)||t.endsWith(`.jpeg`)?`.jpg`:t.endsWith(`.gif`)?`.gif`:t.endsWith(`.svg`)?`.svg`:t.endsWith(`.ttf`)?`.ttf`:t.endsWith(`.otf`)?`.otf`:t.endsWith(`.woff`)?`.woff`:t.endsWith(`.woff2`)?`.woff2`:t.endsWith(`.mp3`)?`.mp3`:t.endsWith(`.wav`)?`.wav`:t.endsWith(`.ogg`)?`.ogg`:t.endsWith(`.tmx`)||t.endsWith(`.xml`)?`.xml`:t.endsWith(`.json`)?`.json`:`.bin`}function guessMime(e){let t=e.toLowerCase();return t.endsWith(`.png`)?`image/png`:t.endsWith(`.jpg`)||t.endsWith(`.jpeg`)?`image/jpeg`:t.endsWith(`.gif`)?`image/gif`:t.endsWith(`.svg`)?`image/svg+xml`:t.endsWith(`.ttf`)?`font/truetype`:t.endsWith(`.otf`)?`font/otf`:t.endsWith(`.woff`)?`font/woff`:t.endsWith(`.woff2`)?`font/woff2`:t.endsWith(`.mp3`)?`audio/mpeg`:t.endsWith(`.wav`)?`audio/wav`:t.endsWith(`.ogg`)?`audio/ogg`:t.endsWith(`.json`)?`application/json`:`application/octet-stream`}async function readFileUrl(t){let n=e.filename_from_uri(t)[0],[r,i]=e.file_get_contents(n);if(!r)throw Error(`XMLHttpRequest: cannot read file ${n}`);let a=i,o=new Uint8Array(a.byteLength);return o.set(a),o.buffer}function writeToTmp(t,n){let i=e.build_filenamev([e.get_tmp_dir(),`gjsify-blob-${r++}${n}`]);return e.file_set_contents(i,t),i}var XMLHttpRequest=class{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=``;response=null;responseText=``;responseType=``;responseURL=``;timeout=0;withCredentials=!1;onloadstart=null;onprogress=null;onload=null;onloadend=null;onerror=null;onabort=null;ontimeout=null;onreadystatechange=null;_url=``;_method=`GET`;_aborted=!1;_listeners={};open(e,t,n=!0){this._method=e.toUpperCase(),this._url=t,this._aborted=!1,this.readyState=this.OPENED}setRequestHeader(e,t){}overrideMimeType(e){}send(e){let r=this._url,i=this.responseType,a=globalThis.__GJSIFY_DEBUG_XHR===!0;r=n(r),a&&r!==this._url&&console.log(`[xmlhttprequest] rewrite ${this._url} → ${r}`),a&&console.log(`[xmlhttprequest] ${this._method} ${r} responseType=${i}`);let doFetch=()=>r.startsWith(`file://`)?readFileUrl(r):t(r,{method:this._method}).then(e=>(a&&console.log(`[xmlhttprequest] fetch ok ${r} status=${e.status}`),this.status=e.status===0?200:e.status,this.statusText=e.statusText||`OK`,this.responseURL=e.url||r,e.arrayBuffer()));this.readyState=this.LOADING,this._emit(`loadstart`,{loaded:0,total:0,lengthComputable:!1}),Promise.resolve().then(doFetch).then(e=>{if(this._aborted)return;this.status===0&&(this.status=200),this.statusText||=`OK`,this.readyState=this.DONE;let t=new Uint8Array(e),n=t.byteLength;if(this._emit(`progress`,{loaded:n,total:n,lengthComputable:!0}),i===`blob`){let e=writeToTmp(t,guessExt(r)),i=new FakeBlob(guessMime(r),n);i._tmpPath=e,this.response=i}else i===`arraybuffer`?this.response=e:i===`json`?this.response=JSON.parse(new TextDecoder().decode(t)):(this.responseText=new TextDecoder().decode(t),this.response=this.responseText);this._emit(`load`,{target:this,loaded:n,total:n,lengthComputable:!0}),this._emit(`loadend`,{target:this,loaded:n,total:n,lengthComputable:!0}),this.onreadystatechange&&this.onreadystatechange({})}).catch(e=>{this._aborted||(console.warn(`[xmlhttprequest] ${this._method} ${r} — ${e?.message??e}`),this.readyState=this.DONE,this._emit(`error`,{error:e}),this._emit(`loadend`,{}))})}abort(){this._aborted=!0,this.readyState=this.DONE,this._emit(`abort`,{}),this._emit(`loadend`,{})}addEventListener(e,t){(this._listeners[e]??=[]).push(t)}removeEventListener(e,t){this._listeners[e]=(this._listeners[e]??[]).filter(e=>e!==t)}getResponseHeader(e){return null}getAllResponseHeaders(){return``}_emit(e,t){let n=this[`on`+e];typeof n==`function`&&n.call(this,{type:e,...t});for(let n of this._listeners[e]??[])n.call(this,{type:e,...t})}};function installObjectURLSupport(){let t=URL;if(typeof t.createObjectURL!=`function`||t.__gjsify_objecturl!==!0){let n=new Map;t.createObjectURL=function(e){if(e._tmpPath){let t=`file://${e._tmpPath}`;return n.set(t,e._tmpPath),t}return console.warn(`[createObjectURL] received non-FakeBlob — cannot create file:// URL`),`file:///dev/null`},t.revokeObjectURL=function(t){let r=n.get(t);if(r){let i=e.build_filenamev([r]);e.unlink(i),n.delete(t)}},t.__gjsify_objecturl=!0}}export{FakeBlob,XMLHttpRequest,installObjectURLSupport};
@@ -8,6 +8,11 @@ export declare class FakeBlob {
8
8
  stream(): ReadableStream<Uint8Array>;
9
9
  }
10
10
  export declare class XMLHttpRequest {
11
+ static readonly UNSENT = 0;
12
+ static readonly OPENED = 1;
13
+ static readonly HEADERS_RECEIVED = 2;
14
+ static readonly LOADING = 3;
15
+ static readonly DONE = 4;
11
16
  readonly UNSENT = 0;
12
17
  readonly OPENED = 1;
13
18
  readonly HEADERS_RECEIVED = 2;
@@ -0,0 +1,2 @@
1
+ declare const _default: () => Promise<void>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare const _default: () => Promise<void>;
2
+ export default _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/xmlhttprequest",
3
- "version": "0.45.0",
3
+ "version": "0.47.0",
4
4
  "description": "XMLHttpRequest and URL.createObjectURL/revokeObjectURL for GJS — backed by @gjsify/fetch and GLib",
5
5
  "module": "lib/esm/index.js",
6
6
  "types": "lib/types/index.d.ts",
@@ -25,12 +25,16 @@
25
25
  "./globals.mjs"
26
26
  ],
27
27
  "scripts": {
28
- "clear": "gjsify clear lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo",
28
+ "clear": "gjsify clear lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs",
29
29
  "check": "gjsify tsc --noEmit",
30
30
  "build": "gjsify run build:gjsify && gjsify run build:types",
31
31
  "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
32
32
  "build:types": "gjsify tsc",
33
- "build:test:browser": "gjsify build src/test.browser.mts --app browser --outfile dist/test.browser.mjs"
33
+ "build:test": "gjsify run build:test:gjs && gjsify run build:test:browser",
34
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
35
+ "build:test:browser": "gjsify build src/test.browser.mts --app browser --outfile dist/test.browser.mjs",
36
+ "test": "gjsify run build:gjsify && gjsify run build:test && gjsify run test:gjs",
37
+ "test:gjs": "gjsify run test.gjs.mjs"
34
38
  },
35
39
  "keywords": [
36
40
  "gjs",
@@ -39,13 +43,15 @@
39
43
  "web-api"
40
44
  ],
41
45
  "dependencies": {
42
- "@girs/gio-2.0": "^4.1.0",
43
- "@girs/gjs": "^4.1.0",
44
- "@girs/glib-2.0": "^4.1.0",
45
- "@gjsify/fetch": "^0.45.0"
46
+ "@girs/gio-2.0": "4.6.0",
47
+ "@girs/gjs": "4.6.0",
48
+ "@girs/glib-2.0": "4.6.0",
49
+ "@gjsify/fetch": "^0.47.0"
46
50
  },
47
51
  "devDependencies": {
48
- "@gjsify/cli": "^0.45.0",
52
+ "@gjsify/cli": "^0.47.0",
53
+ "@gjsify/node-globals": "^0.47.0",
54
+ "@gjsify/unit": "^0.47.0",
49
55
  "@types/node": "^25.9.2",
50
56
  "typescript": "^6.0.3"
51
57
  },