fus-ext
Version:
FutureScript Extension
355 lines (353 loc) • 27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Socket = exports.jsonPost = exports.post = exports.binaryGet = exports.jsonGet = exports.get = exports.request = undefined;
var _main = require("./main");
var ok_573300145710716007 = function (a) {
return a !== undefined && a !== null;
};var dotDotCalc_573300145710716007 = function (dotDot, a, b, c) {
return dotDot(a)[b](a, ...c);
};var NODE_MAX_REDIRECT_COUNT, request, get, jsonGet, binaryGet, post, jsonPost, Socket;NODE_MAX_REDIRECT_COUNT = 20;exports.request = request = function (options) {
return (() => {
return new Promise(function (resolve, reject) {
return (() => {
var method, uri, headerFields, body, timeout, responseBodyType, var_573300145710716007_5, var_573300145710716007_6, var_573300145710716007_7, var_573300145710716007_8;method = options.method;uri = options.uri;headerFields = (var_573300145710716007_5 = options.headerFields) === undefined || var_573300145710716007_5 === null ? null : var_573300145710716007_5;body = (var_573300145710716007_6 = options.body) === undefined || var_573300145710716007_6 === null ? null : var_573300145710716007_6;timeout = (var_573300145710716007_7 = options.timeout) === undefined || var_573300145710716007_7 === null ? null : var_573300145710716007_7;responseBodyType = (var_573300145710716007_8 = options.responseBodyType) === undefined || var_573300145710716007_8 === null ? "text" : var_573300145710716007_8;!ok_573300145710716007(method) ? (() => {
return (0, _main.fail)();
})() : undefined;!ok_573300145710716007(uri) ? (() => {
return (0, _main.fail)();
})() : undefined;ok_573300145710716007(body) && typeof body !== "string" && !(body instanceof Uint8Array) ? (() => {
return (0, _main.fail)();
})() : undefined;["binary", "text", "json"].indexOf(responseBodyType) < 0 ? (() => {
return (0, _main.fail)();
})() : undefined;return !_main.sys.isNode ? (() => {
return function () {
return (() => {
var xhr, var_573300145710716007_9;xhr = new XMLHttpRequest();xhr.open(method, uri);ok_573300145710716007(headerFields) ? (() => {
return dotDotCalc_573300145710716007(_main.dotDot_573300145710716007, Object, "keyValues", [headerFields]).forEach(function ([key, value]) {
return (() => {
return xhr.setRequestHeader(key, value);
})();
});
})() : undefined;xhr.responseType = responseBodyType === "binary" ? (() => {
return "arraybuffer";
})() : (() => {
return responseBodyType === "text" ? (() => {
return "text";
})() : (() => {
return responseBodyType === "json" ? (() => {
return "text";
})() : undefined;
})();
})();xhr.timeout = (var_573300145710716007_9 = timeout) === undefined || var_573300145710716007_9 === null ? 0 : var_573300145710716007_9;xhr.onload = function () {
var arg_573300145710716007 = arguments;return (() => {
var response, ex, var_573300145710716007_10;return (() => {
try {
return (() => {
response = { statusCode: xhr.status, statusReason: xhr.statusText, headerFields: dotDotCalc_573300145710716007(_main.dotDot_573300145710716007, dotDotCalc_573300145710716007(_main.dotDot_573300145710716007, dotDotCalc_573300145710716007(_main.dotDot_573300145710716007, xhr.getAllResponseHeaders(), "stripTrailingNewline", []), "split", ["\r\n", ": ", 1]).map(function (field) {
return (() => {
return [field[0].toLowerCase(), field[1]];
})();
}), "toObject", []), body: responseBodyType === "binary" ? (() => {
return new Uint8Array(xhr.response);
})() : (() => {
return responseBodyType === "text" ? (() => {
return xhr.response;
})() : (() => {
return responseBodyType === "json" ? (() => {
return JSON.parse(xhr.response);
})() : undefined;
})();
})() };return 200 <= (var_573300145710716007_10 = response.statusCode) && var_573300145710716007_10 < 300 ? (() => {
return resolve(response);
})() : (() => {
return reject(response);
})();
})();
} catch (catch_573300145710716007) {
ex = catch_573300145710716007;return (() => {
return reject(ex);
})();
} finally {}
})();
})();
};xhr.onerror = function (e) {
return (() => {
return reject(e);
})();
};xhr.ontimeout = function () {
var arg_573300145710716007 = arguments;return (() => {
return reject(new Error("timeout"));
})();
};xhr.onabort = function () {
var arg_573300145710716007 = arguments;return (() => {
return reject(new Error("abort"));
})();
};return xhr.send(body);
})();
}();
})() : (() => {
return function () {
return (() => {
var http, https, urlMod, redirectCount, internalRequest;http = module.require("http");https = module.require("https");urlMod = module.require("url");redirectCount = 0;internalRequest = function (internalUri) {
return (() => {
var parsedUri, httpOrHttps, rawRequest;redirectCount > NODE_MAX_REDIRECT_COUNT ? (() => {
return (0, _main.fail)();
})() : undefined;parsedUri = urlMod.parse(internalUri);httpOrHttps = parsedUri.protocol === "https:" ? (() => {
return https;
})() : (() => {
return http;
})();rawRequest = httpOrHttps.request({ method: method, hostname: parsedUri.hostname, port: parsedUri.port, path: parsedUri.path, headers: headerFields }, function (rawResponse) {
return (() => {
var data;data = Buffer.alloc(0);rawResponse.on("data", function (chunk) {
return (() => {
data = Buffer.concat([data, chunk]);
})();
});return rawResponse.on("end", function () {
var arg_573300145710716007 = arguments;return (() => {
var response, bodyString, contentType, contentEncodingMatches, encoding, ex, var_573300145710716007_11, var_573300145710716007_12;return (() => {
try {
return (() => {
return method === "GET" && 300 <= (var_573300145710716007_11 = rawResponse.statusCode) && var_573300145710716007_11 < 400 && rawResponse.headers["location"] !== undefined ? (() => {
redirectCount = redirectCount + 1;return internalRequest(rawResponse.headers["location"]);
})() : (() => {
response = { statusCode: rawResponse.statusCode, statusReason: rawResponse.statusMessage, headerFields: rawResponse.headers, body: responseBodyType === "binary" ? (() => {
return new Uint8Array(data);
})() : (() => {
return ["text", "json"].indexOf(responseBodyType) >= 0 ? (() => {
bodyString = data[0] === 0xef && data[1] === 0xbb && data[2] === 0xbf ? (() => {
return data.toString(undefined, 3);
})() : (() => {
return data[0] === 0xff && data[1] === 0xfe ? (() => {
return data.toString("utf16le", 2);
})() : (() => {
return data[0] === 0xfe && data[1] === 0xff ? (() => {
return (0, _main.fail)();
})() : (() => {
contentType = ok_573300145710716007(rawResponse.headers["content-type"]) ? (() => {
return rawResponse.headers["content-type"].toLowerCase();
})() : (() => {
return null;
})();return ok_573300145710716007(contentType) ? (() => {
contentEncodingMatches = contentType.match(new RegExp(";\\s*charset=([^\\s;]+)\\s*(;|$)"));return ok_573300145710716007(contentEncodingMatches) ? (() => {
encoding = contentEncodingMatches[1];return encoding === "utf-8" ? (() => {
return data.toString();
})() : (() => {
return ["utf-16", "utf-16le"].indexOf(encoding) >= 0 ? (() => {
return data.toString("utf16le");
})() : (() => {
return (0, _main.fail)();
})();
})();
})() : (() => {
return data.toString();
})();
})() : (() => {
return data.toString();
})();
})();
})();
})();return responseBodyType === "text" ? (() => {
return bodyString;
})() : (() => {
return JSON.parse(bodyString);
})();
})() : undefined;
})() };return 200 <= (var_573300145710716007_12 = response.statusCode) && var_573300145710716007_12 < 300 ? (() => {
return resolve(response);
})() : (() => {
return reject(response);
})();
})();
})();
} catch (catch_573300145710716007) {
ex = catch_573300145710716007;return (() => {
return reject(ex);
})();
} finally {}
})();
})();
});
})();
});ok_573300145710716007(timeout) ? (() => {
return rawRequest.setTimeout(timeout, function () {
var arg_573300145710716007 = arguments;return (() => {
reject(new Error("timeout"));return rawRequest.abort();
})();
});
})() : undefined;return rawRequest.on("error", function (e) {
return (() => {
return reject(e);
})();
}).on("abort", function (e) {
return (() => {
return reject(new Error("abort"));
})();
}).on("aborted", function (e) {
return (() => {
return reject(new Error("abort"));
})();
}).end(body instanceof Uint8Array ? (() => {
return Buffer.from(body.buffer);
})() : (() => {
return body;
})());
})();
};return internalRequest(uri);
})();
}();
})();
})();
});
})();
};exports.get = get = function (uri, options) {
return (() => {
var actualOptions;actualOptions = { method: "GET", uri: uri };Object.assign(actualOptions, options);return _main.web.request(actualOptions);
})();
};exports.jsonGet = jsonGet = function (uri, options) {
return (() => {
var actualOptions;actualOptions = { method: "GET", uri: uri, responseBodyType: "json" };Object.assign(actualOptions, options);return _main.web.request(actualOptions);
})();
};exports.binaryGet = binaryGet = function (uri, options) {
return (() => {
var actualOptions;actualOptions = { method: "GET", uri: uri, responseBodyType: "binary" };Object.assign(actualOptions, options);return _main.web.request(actualOptions);
})();
};exports.post = post = function (uri, body, options) {
return (() => {
var actualOptions;actualOptions = { method: "POST", uri: uri, body: body };Object.assign(actualOptions, options);return _main.web.request(actualOptions);
})();
};exports.jsonPost = jsonPost = function (uri, body, options) {
return (() => {
var actualOptions;actualOptions = { method: "POST", uri: uri, headerFields: { ["Content-Type"]: "application/json" }, body: JSON.stringify(body), responseBodyType: "json" };Object.assign(actualOptions, options);return _main.web.request(actualOptions);
})();
};exports.Socket = Socket = (() => {
var var_573300145710716007_2 = Symbol();var var_573300145710716007_3 = Symbol();var var_573300145710716007_4 = Symbol();var class_573300145710716007 = class {
constructor(uri) {
(uri => {
return (() => {
this.uri = uri;this.onOpen = (0, _main.eventField)();this.onClose = (0, _main.eventField)();this.onMessage = (0, _main.eventField)();this.onError = (0, _main.eventField)();this[var_573300145710716007_2] = null;this[var_573300145710716007_3] = false;this[var_573300145710716007_4] = false;
})();
})(uri);
}open() {
return (() => {
var arg_573300145710716007 = arguments;return (() => {
return new Promise((resolve, reject) => {
return (() => {
(0, _main.assert)(!this[var_573300145710716007_4]);this[var_573300145710716007_4] = true;return !_main.sys.isNode ? (() => {
return (() => {
return (() => {
var uri, mapProtocol;uri = this.uri.startsWith("ws:") || this.uri.startsWith("wss:") ? (() => {
return this.uri;
})() : (() => {
mapProtocol = () => {
var arg_573300145710716007 = arguments;return (() => {
return location.protocol === "http:" ? (() => {
return "ws:";
})() : (() => {
return location.protocol === "https:" ? (() => {
return "wss:";
})() : (() => {
return (0, _main.fail)();
})();
})();
})();
};return this.uri.startsWith("//") ? (() => {
return mapProtocol() + this.uri;
})() : (() => {
return this.uri.startsWith("/") ? (() => {
return mapProtocol() + "//" + location.host + this.uri;
})() : (() => {
return (0, _main.fail)();
})();
})();
})();this[var_573300145710716007_2] = new WebSocket(uri);this[var_573300145710716007_2].onopen = () => {
var arg_573300145710716007 = arguments;return (() => {
this[var_573300145710716007_3] = true;this.onOpen.fire();return resolve();
})();
};this[var_573300145710716007_2].onclose = () => {
var arg_573300145710716007 = arguments;return (() => {
this.onClose.fire();return !this[var_573300145710716007_3] ? (() => {
return reject();
})() : undefined;
})();
};this[var_573300145710716007_2].onmessage = e => {
return (() => {
return this.onMessage.fire({ data: e.data });
})();
};this[var_573300145710716007_2].onerror = e => {
return (() => {
this.onError.fire(e);return !this[var_573300145710716007_3] ? (() => {
return reject();
})() : undefined;
})();
};
})();
})();
})() : (() => {
return (() => {
return (() => {
return (0, _main.fail)();
})();
})();
})();
})();
});
})();
})();
}send(data) {
return (data => {
return (() => {
return !_main.sys.isNode ? (() => {
return (() => {
return (() => {
return this[var_573300145710716007_2].send(data);
})();
})();
})() : (() => {
return (() => {
return (() => {
return (0, _main.fail)();
})();
})();
})();
})();
})(data);
}close() {
return (() => {
var arg_573300145710716007 = arguments;return (() => {
return new Promise((resolve, reject) => {
return (() => {
(0, _main.assert)(this[var_573300145710716007_3]);return !_main.sys.isNode ? (() => {
return (() => {
return (() => {
this[var_573300145710716007_2].onclose = () => {
var arg_573300145710716007 = arguments;return (() => {
this.onClose.fire();return resolve();
})();
};this[var_573300145710716007_2].onerror = e => {
return (() => {
this.onError.fire(e);return reject();
})();
};return this[var_573300145710716007_2].close();
})();
})();
})() : (() => {
return (() => {
return (() => {
return (0, _main.fail)();
})();
})();
})();
})();
});
})();
})();
}
};return class_573300145710716007;
})();exports.request = request;
exports.get = get;
exports.jsonGet = jsonGet;
exports.binaryGet = binaryGet;
exports.post = post;
exports.jsonPost = jsonPost;
exports.Socket = Socket;