Last active
March 9, 2021 16:52
-
-
Save Oderjunkie/b422d572c6f53e69c34a354fa4910bc8 to your computer and use it in GitHub Desktop.
Javascript: Threadsafe WASM-safe fetch() and Response alternative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
class Response_sync extends Response { | |
#zzz | |
constructor(...args) { | |
super(...args); | |
this.#zzz = args[0]; | |
} | |
text() { // WASM-safe Response.textd() | |
return this.#zzz; | |
} | |
json() { // WASM-safe Response.json() | |
return JSON.parse(this.#zzz); | |
} | |
} | |
Response = Response_sync; | |
} | |
function fetch(resource_, init_={method: 'GET', headers: new Headers()}) { | |
// Firstly: make sure GIGO doesn't happen (Garbage in, garbage out.) | |
let resource; | |
let init; | |
if (typeof(resource_)==Request) { | |
resource = resource_.url; | |
init = { | |
method: resource_.method ?? 'GET', | |
headers: resource_.headers ?? new Headers(), | |
body: resource_.body ?? undefined, | |
mode: resource_.mode, | |
credentials: resource_.credentials, | |
redirect: resource_.redirect, | |
referrer: resource_.referrer, | |
referrerPolicy: resource_.referrerPolicy, | |
integrity: resource_.integrity, | |
keepalive: resource_.keepalive, | |
signal: resource_.signal | |
}; | |
} else { | |
resource = resource_; | |
init = init_; | |
init.method ??= 'GET'; | |
init.headers ??= new Headers(); | |
init.body ??= undefined; | |
} | |
resource ??= '/'; | |
var back = null; | |
let xml = new XMLHttpRequest(); // Prepare the XML request. | |
xml.open(init.method, resource, false); // Use the method, url, and make it synchronous (for WASM use.) | |
for (let pair of init.headers.entries()) { xml.setRequestHeader(...pair); } // Set the headers. | |
//xml.responseType = ''; // Give us back TEXT. | |
xml.onreadystatechange = function() { | |
if (xml.readyState!=4) return; // If we haven't downloaded everything yet, just don't do anything. | |
headers = new Headers(); // hell begins | |
for (let part of xml.getAllResponseHeaders().split('\r\n')) { // for each header | |
part ??= ''; // if null or undefined | |
if (part=='') continue; // or empty, return | |
part = part.split(': '); // otherwise, parse from "key: value" into ["key", "value"] | |
headers.append(...part); // and add it. | |
} | |
let a = document.createElement('a'); | |
a.href = resource; | |
// Ok, we're done! | |
back = new Response( | |
xml.response, // Well, the... response. | |
{ | |
'status': xml.status, // Get the status from the XML request. (Ex. 200) | |
'statusText': xml.statusText, // Get the status text from the XML request. (Ex. "OK") | |
'headers': headers // Good thing we set that up! | |
} | |
); | |
Object.defineProperties(back, {'type': {'value': 'basic'}, 'url': {'value': a.protocol+'//'+a.host+a.pathname+a.search+a.hash}}); | |
} | |
xml.send(init.body); | |
return back; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
its WASM not NASM me dumb
i added sync response so its not horrible