UNPKG

tream

Version:

Lightweight lazy streams in TypeScript

130 lines 4.48 kB
export function request(method, url, headers, body, need_body, res_fn, err_fn) { var xhr = new XMLHttpRequest(); xhr.open(method, url, true); for (var name_1 in headers) { xhr.setRequestHeader(name_1, headers[name_1]); } xhr.onreadystatechange = function () { xhr_on(xhr, res_fn, need_body); }; if (need_body) { download_init(xhr); } try { if (body) { upload(xhr, body); } else { xhr.send(null); } } catch (error) { delete xhr.onreadystatechange; err_fn(error); } return function () { xhr.abort(); }; } function parseHeaders(hs) { var h = {}; for (var _i = 0, _a = hs.split(/\r?\n/); _i < _a.length; _i++) { var he = _a[_i]; var _b = he.split(/:\s*/), f = _b[0], v = _b[1]; if (v && v.length) { h[f.replace(/(:?^|\-)\w/g, function (c) { return c.toUpperCase(); })] = v; } } return h; } function xhr_on(xhr, res_fn, need_body) { switch (xhr.readyState) { case 4: { delete xhr.onreadystatechange; res_fn(xhr.status, xhr.statusText, parseHeaders(xhr.getAllResponseHeaders()), need_body ? download_done(xhr) : undefined); } break; } } ; var _a = xhrapi_new(), upload = _a.upload, download_init = _a.download_init, download_done = _a.download_done; function bodyFrom(data) { if (data == undefined) { return undefined; } if (typeof data == 'string') { return Buffer.from(data, 'binary'); } return Buffer.from(data); } function xhrapi_new() { if (typeof window == 'undefined') { return { upload: function (xhr, buf) { xhr.send(buf); }, download_init: function (xhr) { }, download_done: function (xhr) { return bodyFrom(xhr.responseText); } }; } var xhr = new XMLHttpRequest(); xhr.open('get', '/', true); var buf = Buffer.allocUnsafe(1); var upload = typeof xhr.sendAsBinary == 'function' ? function (xhr, buf) { // upload binary string using Mozilla-specific sendAsBinary method xhr.sendAsBinary(buf.buffer); } : typeof Uint8Array == 'function' ? // upload array buffer using XHR send method (buf instanceof Uint8Array ? function (xhr, buf) { xhr.send(buf.byteOffset === 0 && buf.byteLength === buf.buffer.byteLength ? // If the buffer isn't a subarray, return the underlying ArrayBuffer buf.buffer : // Otherwise we need to get a proper copy buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)); } : function (xhr, buf) { // This is the slow version that will work with any Buffer // implementation (even in old browsers) var arrayCopy = new Uint8Array(buf.length); var len = buf.length; for (var i = 0; i < len; i++) { arrayCopy[i] = buf[i]; } xhr.send(arrayCopy.buffer); }) : function (xhr, buf) { // upload as binary DOMString (fallback) xhr.send(buf.toString('binary')); }; var _a = (function (xhr) { if ('responseType' in xhr) { try { xhr.responseType = 'arraybuffer'; // download array buffer using XHR responseType field return 'response' in xhr && xhr.responseType == 'arraybuffer'; } catch (error) { } } return false; })(xhr) ? [function (xhr) { xhr.responseType = 'arraybuffer'; }, function (xhr) { return bodyFrom(xhr.response); }] : typeof xhr.overrideMimeType == 'function' ? [function (xhr) { // download binary string through overriding mime type xhr.overrideMimeType('text/plain; charset=x-user-defined'); }, function (xhr) { return bodyFrom(xhr.responseText); }] : [function (xhr) { // download binary string as DOMString }, function (xhr) { return bodyFrom(xhr.responseText); }], download_init = _a[0], download_done = _a[1]; return { upload: upload, download_init: download_init, download_done: download_done, }; } //# sourceMappingURL=browser.js.map