@gjsify/xmlhttprequest 0.3.21 → 0.4.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.
@@ -0,0 +1 @@
1
+ var e=Object.defineProperty,__name=(t,n)=>e(t,`name`,{value:n,configurable:!0});export{__name};
package/lib/esm/index.js CHANGED
@@ -1 +1 @@
1
- import e from"gi://GLib?version=2.0";import t from"system";import 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(r){let i=this._url,a=this.responseType,o=globalThis.__GJSIFY_DEBUG_XHR===!0;if(i.startsWith(`/`)&&!i.startsWith(`//`)){let n=t.programPath??t.programInvocationName??``;n&&(i=`file://${e.path_get_dirname(n)}${this._url}`,o&&console.log(`[xmlhttprequest] rewrite ${this._url} → ${i}`))}o&&console.log(`[xmlhttprequest] ${this._method} ${i} responseType=${a}`);let doFetch=()=>i.startsWith(`file://`)?readFileUrl(i):n(i,{method:this._method}).then(e=>(o&&console.log(`[xmlhttprequest] fetch ok ${i} status=${e.status}`),this.status=e.status===0?200:e.status,this.statusText=e.statusText||`OK`,this.responseURL=e.url||i,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}),a===`blob`||a===``){let e=writeToTmp(t,guessExt(i)),r=new FakeBlob(guessMime(i),n);r._tmpPath=e,this.response=r}else a===`arraybuffer`?this.response=e:a===`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} ${i} — ${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(){if(typeof URL.createObjectURL!=`function`||URL.__gjsify_objecturl!==!0){let t=new Map;URL.createObjectURL=function(e){if(e._tmpPath){let n=`file://${e._tmpPath}`;return t.set(n,e._tmpPath),n}return console.warn(`[createObjectURL] received non-FakeBlob — cannot create file:// URL`),`file:///dev/null`},URL.revokeObjectURL=function(n){let r=t.get(n);if(r){try{let t=e.build_filenamev([r]);e.unlink(t)}catch{}t.delete(n)}},URL.__gjsify_objecturl=!0}}export{FakeBlob,XMLHttpRequest,installObjectURLSupport};
1
+ import"./_virtual/_rolldown/runtime.js";import e from"gi://GLib?version=2.0";import t from"system";import 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(r){let i=this._url,a=this.responseType,o=globalThis.__GJSIFY_DEBUG_XHR===!0;if(i.startsWith(`/`)&&!i.startsWith(`//`)){let n=t.programPath??t.programInvocationName??``;n&&(i=`file://${e.path_get_dirname(n)}${this._url}`,o&&console.log(`[xmlhttprequest] rewrite ${this._url} → ${i}`))}o&&console.log(`[xmlhttprequest] ${this._method} ${i} responseType=${a}`);let doFetch=()=>i.startsWith(`file://`)?readFileUrl(i):n(i,{method:this._method}).then(e=>(o&&console.log(`[xmlhttprequest] fetch ok ${i} status=${e.status}`),this.status=e.status===0?200:e.status,this.statusText=e.statusText||`OK`,this.responseURL=e.url||i,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}),a===`blob`||a===``){let e=writeToTmp(t,guessExt(i)),r=new FakeBlob(guessMime(i),n);r._tmpPath=e,this.response=r}else a===`arraybuffer`?this.response=e:a===`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} ${i} — ${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(){if(typeof URL.createObjectURL!=`function`||URL.__gjsify_objecturl!==!0){let t=new Map;URL.createObjectURL=function(e){if(e._tmpPath){let n=`file://${e._tmpPath}`;return t.set(n,e._tmpPath),n}return console.warn(`[createObjectURL] received non-FakeBlob — cannot create file:// URL`),`file:///dev/null`},URL.revokeObjectURL=function(n){let r=t.get(n);if(r){try{let t=e.build_filenamev([r]);e.unlink(t)}catch{}t.delete(n)}},URL.__gjsify_objecturl=!0}}export{FakeBlob,XMLHttpRequest,installObjectURLSupport};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/xmlhttprequest",
3
- "version": "0.3.21",
3
+ "version": "0.4.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",
@@ -32,13 +32,13 @@
32
32
  "web-api"
33
33
  ],
34
34
  "dependencies": {
35
- "@girs/gio-2.0": "2.88.0-4.0.0-rc.14",
36
- "@girs/gjs": "4.0.0-rc.14",
37
- "@girs/glib-2.0": "2.88.0-4.0.0-rc.14",
38
- "@gjsify/fetch": "^0.3.21"
35
+ "@girs/gio-2.0": "2.88.0-4.0.0-rc.15",
36
+ "@girs/gjs": "4.0.0-rc.15",
37
+ "@girs/glib-2.0": "2.88.0-4.0.0-rc.15",
38
+ "@gjsify/fetch": "^0.4.0"
39
39
  },
40
40
  "devDependencies": {
41
- "@gjsify/cli": "^0.3.21",
41
+ "@gjsify/cli": "^0.4.0",
42
42
  "@types/node": "^25.6.2",
43
43
  "typescript": "^6.0.3"
44
44
  }