Skip to content

Commit 6cd0632

Browse files
committed
Configure xhr inside create function
No need to deliver many extra parameters
1 parent ac03562 commit 6cd0632

File tree

1 file changed

+82
-88
lines changed

1 file changed

+82
-88
lines changed

modules/core.mjs

Lines changed: 82 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -856,120 +856,114 @@ function findFunction(name) {
856856
}
857857

858858

859-
/** @summary Assign methods to request
859+
/** @summary Method to create http request, without promise can be used only in browser environment
860860
* @private */
861-
function setRequestMethods(xhr, url, kind, user_accept_callback, user_reject_callback) {
862-
xhr.http_callback = isFunc(user_accept_callback) ? user_accept_callback.bind(xhr) : function() {};
863-
xhr.error_callback = isFunc(user_reject_callback) ? user_reject_callback.bind(xhr) : function(err) { console.warn(err.message); this.http_callback(null); }.bind(xhr);
864-
865-
if (!kind) kind = 'buf';
866-
867-
let method = 'GET', is_async = true, p = kind.indexOf(';sync');
868-
if (p > 0) { kind = kind.slice(0,p); is_async = false; }
869-
switch (kind) {
870-
case 'head': method = 'HEAD'; break;
871-
case 'posttext': method = 'POST'; kind = 'text'; break;
872-
case 'postbuf': method = 'POST'; kind = 'buf'; break;
873-
case 'post':
874-
case 'multi': method = 'POST'; break;
875-
}
861+
function createHttpRequest(url, kind, user_accept_callback, user_reject_callback, use_promise) {
876862

877-
xhr.kind = kind;
863+
function configureXhr(xhr) {
864+
xhr.http_callback = isFunc(user_accept_callback) ? user_accept_callback.bind(xhr) : () => {};
865+
xhr.error_callback = isFunc(user_reject_callback) ? user_reject_callback.bind(xhr) : function(err) { console.warn(err.message); this.http_callback(null); }.bind(xhr);
878866

879-
if (settings.WithCredentials)
880-
xhr.withCredentials = true;
867+
if (!kind) kind = 'buf';
881868

882-
if (settings.HandleWrongHttpResponse && (method == 'GET') && isFunc(xhr.addEventListener))
883-
xhr.addEventListener('progress', function(oEvent) {
884-
if (oEvent.lengthComputable && this.expected_size && (oEvent.loaded > this.expected_size)) {
885-
this.did_abort = true;
886-
this.abort();
887-
this.error_callback(Error(`Server sends more bytes ${oEvent.loaded} than expected ${this.expected_size}. Abort I/O operation`), 598);
888-
}
889-
}.bind(xhr));
869+
let method = 'GET', is_async = true, p = kind.indexOf(';sync');
870+
if (p > 0) { kind = kind.slice(0,p); is_async = false; }
871+
switch (kind) {
872+
case 'head': method = 'HEAD'; break;
873+
case 'posttext': method = 'POST'; kind = 'text'; break;
874+
case 'postbuf': method = 'POST'; kind = 'buf'; break;
875+
case 'post':
876+
case 'multi': method = 'POST'; break;
877+
}
878+
879+
xhr.kind = kind;
880+
881+
if (settings.WithCredentials)
882+
xhr.withCredentials = true;
890883

891-
xhr.onreadystatechange = function() {
884+
if (settings.HandleWrongHttpResponse && (method == 'GET') && isFunc(xhr.addEventListener))
885+
xhr.addEventListener('progress', function(oEvent) {
886+
if (oEvent.lengthComputable && this.expected_size && (oEvent.loaded > this.expected_size)) {
887+
this.did_abort = true;
888+
this.abort();
889+
this.error_callback(Error(`Server sends more bytes ${oEvent.loaded} than expected ${this.expected_size}. Abort I/O operation`), 598);
890+
}
891+
}.bind(xhr));
892+
893+
xhr.onreadystatechange = function() {
892894

893-
if (this.did_abort) return;
895+
if (this.did_abort) return;
894896

895-
if ((this.readyState === 2) && this.expected_size) {
896-
let len = parseInt(this.getResponseHeader('Content-Length'));
897-
if (Number.isInteger(len) && (len > this.expected_size) && !settings.HandleWrongHttpResponse) {
898-
this.did_abort = true;
899-
this.abort();
900-
return this.error_callback(Error(`Server response size ${len} larger than expected ${this.expected_size}. Abort I/O operation`), 599);
897+
if ((this.readyState === 2) && this.expected_size) {
898+
let len = parseInt(this.getResponseHeader('Content-Length'));
899+
if (Number.isInteger(len) && (len > this.expected_size) && !settings.HandleWrongHttpResponse) {
900+
this.did_abort = true;
901+
this.abort();
902+
return this.error_callback(Error(`Server response size ${len} larger than expected ${this.expected_size}. Abort I/O operation`), 599);
903+
}
901904
}
902-
}
903905

904-
if (this.readyState != 4) return;
906+
if (this.readyState != 4) return;
905907

906-
if ((this.status != 200) && (this.status != 206) && !browser.qt5 &&
907-
// in these special cases browsers not always set status
908-
!((this.status == 0) && ((url.indexOf('file://') == 0) || (url.indexOf('blob:') == 0)))) {
909-
return this.error_callback(Error(`Fail to load url ${url}`), this.status);
910-
}
908+
if ((this.status != 200) && (this.status != 206) && !browser.qt5 &&
909+
// in these special cases browsers not always set status
910+
!((this.status == 0) && ((url.indexOf('file://') == 0) || (url.indexOf('blob:') == 0)))) {
911+
return this.error_callback(Error(`Fail to load url ${url}`), this.status);
912+
}
911913

912-
if (this.nodejs_checkzip && (this.getResponseHeader('content-encoding') == 'gzip'))
913-
// special handling of gzipped JSON objects in Node.js
914-
return import('zlib').then(handle => {
915-
let res = handle.unzipSync(Buffer.from(this.response)),
916-
obj = JSON.parse(res); // zlib returns Buffer, use JSON to parse it
917-
return this.http_callback(parse(obj));
918-
});
914+
if (this.nodejs_checkzip && (this.getResponseHeader('content-encoding') == 'gzip'))
915+
// special handling of gzipped JSON objects in Node.js
916+
return import('zlib').then(handle => {
917+
let res = handle.unzipSync(Buffer.from(this.response)),
918+
obj = JSON.parse(res); // zlib returns Buffer, use JSON to parse it
919+
return this.http_callback(parse(obj));
920+
});
921+
922+
switch(this.kind) {
923+
case 'xml': return this.http_callback(this.responseXML);
924+
case 'text': return this.http_callback(this.responseText);
925+
case 'object': return this.http_callback(parse(this.responseText));
926+
case 'multi': return this.http_callback(parseMulti(this.responseText));
927+
case 'head': return this.http_callback(this);
928+
}
919929

920-
switch(this.kind) {
921-
case 'xml': return this.http_callback(this.responseXML);
922-
case 'text': return this.http_callback(this.responseText);
923-
case 'object': return this.http_callback(parse(this.responseText));
924-
case 'multi': return this.http_callback(parseMulti(this.responseText));
925-
case 'head': return this.http_callback(this);
926-
}
930+
// if no response type is supported, return as text (most probably, will fail)
931+
if (this.responseType === undefined)
932+
return this.http_callback(this.responseText);
927933

928-
// if no response type is supported, return as text (most probably, will fail)
929-
if (this.responseType === undefined)
930-
return this.http_callback(this.responseText);
934+
if ((this.kind == 'bin') && ('byteLength' in this.response)) {
935+
// if string representation in requested - provide it
931936

932-
if ((this.kind == 'bin') && ('byteLength' in this.response)) {
933-
// if string representation in requested - provide it
937+
let filecontent = '', u8Arr = new Uint8Array(this.response);
938+
for (let i = 0; i < u8Arr.length; ++i)
939+
filecontent += String.fromCharCode(u8Arr[i]);
934940

935-
let filecontent = '', u8Arr = new Uint8Array(this.response);
936-
for (let i = 0; i < u8Arr.length; ++i)
937-
filecontent += String.fromCharCode(u8Arr[i]);
941+
return this.http_callback(filecontent);
942+
}
938943

939-
return this.http_callback(filecontent);
940-
}
944+
this.http_callback(this.response);
945+
};
941946

942-
this.http_callback(this.response);
943-
};
947+
xhr.open(method, url, is_async);
944948

945-
xhr.open(method, url, is_async);
949+
if ((kind == 'bin') || (kind == 'buf'))
950+
xhr.responseType = 'arraybuffer';
946951

947-
if ((kind == 'bin') || (kind == 'buf'))
948-
xhr.responseType = 'arraybuffer';
952+
if (nodejs && (method == 'GET') && (kind === 'object') && (url.indexOf('.json.gz') > 0)) {
953+
xhr.nodejs_checkzip = true;
954+
xhr.responseType = 'arraybuffer';
955+
}
949956

950-
if (nodejs && (method == 'GET') && (kind === 'object') && (url.indexOf('.json.gz') > 0)) {
951-
xhr.nodejs_checkzip = true;
952-
xhr.responseType = 'arraybuffer';
957+
return xhr;
953958
}
954959

955-
return xhr;
956-
}
957-
958-
/** @summary Method to create http request, without promise can be used only in browser environment
959-
* @private */
960-
function createHttpRequest(url, kind, user_accept_callback, user_reject_callback, use_promise) {
961960
if (isNodeJs()) {
962961
if (!use_promise)
963-
throw Error('Not allowed to create http requests in node without promise');
964-
return import('xhr2').then(h => {
965-
let xhr = new h.default();
966-
setRequestMethods(xhr, url, kind, user_accept_callback, user_reject_callback);
967-
return xhr;
968-
});
962+
throw Error('Not allowed to create http requests in node.js without promise');
963+
return import('xhr2').then(h => configureXhr(new h.default()));
969964
}
970965

971-
let xhr = new XMLHttpRequest();
972-
setRequestMethods(xhr, url, kind, user_accept_callback, user_reject_callback);
966+
let xhr = configureXhr(new XMLHttpRequest());
973967
return use_promise ? Promise.resolve(xhr) : xhr;
974968
}
975969

0 commit comments

Comments
 (0)