@intuitionrobotics/thunderstorm
Version:
157 lines • 6.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XhrHttpModule = exports.XhrHttpModule_Class = void 0;
const ts_common_1 = require("@intuitionrobotics/ts-common");
const zlib_1 = require("zlib");
const request_types_1 = require("../../../shared/request-types");
const BaseHttpRequest_1 = require("../../../shared/BaseHttpRequest");
const BaseHttpModule_1 = require("../../../shared/BaseHttpModule");
class XhrHttpModule_Class extends BaseHttpModule_1.BaseHttpModule_Class {
constructor(name) {
super(name || "XhrHttpModule");
}
init() {
super.init();
this.origin = this.config.origin;
}
createRequest(method, key, data) {
return new XhrHttpRequest(key, data, this.shouldCompress())
.setOrigin(this.origin)
.setMethod(method)
.setTimeout(this.timeout)
.setDefaultHeaders(this.defaultHeaders)
.setHandleRequestSuccess(this.handleRequestSuccess)
.setHandleRequestFailure(this.handleRequestFailure)
.setDefaultRequestHandler(this.processDefaultResponseHandlers);
}
}
exports.XhrHttpModule_Class = XhrHttpModule_Class;
exports.XhrHttpModule = new XhrHttpModule_Class();
class XhrHttpRequest extends BaseHttpRequest_1.BaseHttpRequest {
constructor(requestKey, requestData, shouldCompress) {
super(requestKey, requestData);
this.getElapsedTime = () => this.elapsedMS;
this.compress = shouldCompress === undefined ? true : shouldCompress;
}
getStatus() {
const xhr = this.xhr;
if (!xhr)
throw new ts_common_1.BadImplementationException("No xhr object!");
return xhr.status;
}
getResponse() {
const xhr = this.xhr;
if (!xhr)
throw new ts_common_1.BadImplementationException("No xhr object!");
return xhr.response;
}
abortImpl() {
var _a;
(_a = this.xhr) === null || _a === void 0 ? void 0 : _a.abort();
}
getErrorResponse() {
const rawResponse = this.getResponse();
let response = undefined;
if (rawResponse) {
try {
response = rawResponse && this.asJson();
}
catch (e) {
response = { debugMessage: rawResponse };
}
}
return response;
}
prepareJsonBody(bodyObject) {
return JSON.stringify(bodyObject);
}
executeImpl() {
//loop through whatever preprocessor
return new Promise((resolve, reject) => {
if (this.aborted)
return resolve();
const xhr = new XMLHttpRequest();
// @ts-ignore
// noinspection JSConstantReassignment
this.xhr = xhr;
this.xhr.onreadystatechange = () => {
if (xhr.readyState !== 4)
return;
if (this.startTime)
this.elapsedMS = (0, ts_common_1.currentTimeMillies)() - this.startTime;
resolve();
};
this.xhr.onerror = (err) => {
if (xhr.readyState === 4 && xhr.status === 0) {
reject(new request_types_1.HttpException(404, this.url));
return;
}
reject(err);
};
this.xhr.ontimeout = (err) => {
reject(err);
};
this.xhr.onload = (err) => {
// HttpModule.logVerbose("onload");
};
this.xhr.onloadstart = (err) => {
// HttpModule.logVerbose("onloadstart");
};
this.xhr.onloadend = (err) => {
// HttpModule.logVerbose("onloadend");
};
this.xhr.onabort = (err) => {
// HttpModule.logVerbose("onabort");
};
let nextOperator = this.url.indexOf("?") === -1 ? "?" : "&";
let fullUrl = this.url;
const params = this.params;
if (params)
fullUrl = (0, ts_common_1._keys)(params).reduce((url, paramKey) => {
const param = params[paramKey];
if (!param)
return url;
const toRet = `${url}${nextOperator}${String(paramKey)}=${encodeURIComponent(param)}`;
nextOperator = "&";
return toRet;
}, this.url);
// TODO: investigate which one should work
this.xhr.onprogress = this.onProgressListener;
this.xhr.upload.onprogress = this.onProgressListener;
this.xhr.open(this.method, fullUrl);
this.xhr.timeout = this.timeout;
Object.keys(this.headers).forEach((key) => {
xhr.setRequestHeader(key, this.headers[key].join("; "));
});
let body = this.body;
if (typeof body === 'string' && this.compress)
try {
body = (0, zlib_1.gzipSync)(body);
}
catch (error) {
return reject(error);
}
this.startTime = (0, ts_common_1.currentTimeMillies)();
return this.xhr.send(body);
});
}
getAllResponseHeaders() {
var _a;
return (_a = this.xhr) === null || _a === void 0 ? void 0 : _a.getAllResponseHeaders();
}
getResponseHeader(headerKey) {
if (!this.xhr)
throw new ts_common_1.BadImplementationException("No xhr object!");
if (!this.xhr.response)
throw new ts_common_1.BadImplementationException(`xhr didn't return yet`);
// Chrome bug, if the response header is not present then it throws an error (not really problematic but just annoying)
// https://trackjs.com/blog/refused-unsafe-header/
if (this.xhr.getAllResponseHeaders().indexOf(headerKey) < 0)
return undefined;
const header = this.xhr.getResponseHeader(headerKey);
if (!header)
return undefined;
return header;
}
}
//# sourceMappingURL=XhrHttpModule.js.map