Remove libtextsecure's jquery dependency

Use only the finest, hand-crafted, artisanal XMLHttpRequests.
pull/749/head
lilia 10 years ago
parent a2d88b4fad
commit e52224e481

@ -39043,6 +39043,41 @@ window.textsecure.api = function () {
* do_auth: alternative to user/password where user/password are figured out automagically * do_auth: alternative to user/password where user/password are figured out automagically
* jsonData: JSON data sent in the request body * jsonData: JSON data sent in the request body
*/ */
function ajax(url, options) {
var xhr = new XMLHttpRequest();
xhr.open(options.type, url, true /*async*/);
if ( options.responseType ) {
xhr[ 'responseType' ] = options.responseType;
}
if (options.user && options.password) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(getString(options.user) + ":" + getString(options.password)));
}
if (options.contentType) {
xhr.setRequestHeader( "Content-Type", options.contentType );
}
xhr.onload = function() {
var result = xhr.response;
if ( (!xhr.responseType || xhr.responseType === "text") &&
typeof xhr.responseText === "string" ) {
result = xhr.responseText;
}
if (options.dataType === 'json') {
try { result = JSON.parse(xhr.responseText + ''); } catch(e) {}
}
if (xhr.status < 400) {
options.success(result, xhr.status);
} else {
options.error(result, xhr.status);
}
};
xhr.onerror = function() {
options.error(null, xhr.status);
};
xhr.send( options.data || null );
};
var doAjax = function (param) { var doAjax = function (param) {
if (param.urlParameters === undefined) { if (param.urlParameters === undefined) {
param.urlParameters = ""; param.urlParameters = "";
@ -39054,24 +39089,15 @@ window.textsecure.api = function () {
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
$.ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, { ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
type : param.httpType, type : param.httpType,
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData), data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
contentType : 'application/json; charset=utf-8', contentType : 'application/json; charset=utf-8',
dataType : 'json', dataType : 'json',
user : param.user,
beforeSend : function (xhr) { password : param.password,
if (param.user !== undefined && success : resolve,
param.password !== undefined) error : function(result, code) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(getString(param.user) + ":" + getString(param.password)));
},
success : function(response, textStatus, jqXHR) {
resolve(response);
},
error : function(jqXHR, textStatus, errorThrown) {
var code = jqXHR.status;
if (code === 200) { if (code === 200) {
// happens sometimes when we get no response // happens sometimes when we get no response
// (TODO: Fix server to return 204? instead) // (TODO: Fix server to return 204? instead)
@ -39106,8 +39132,9 @@ window.textsecure.api = function () {
"The server rejected our query, please file a bug report."); "The server rejected our query, please file a bug report.");
} }
} catch (e) { } catch (e) {
if (jqXHR.responseJSON) if (result) {
e.response = jqXHR.responseJSON; e.response = result;
}
reject(e); reject(e);
} }
} }
@ -39220,28 +39247,20 @@ window.textsecure.api = function () {
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
$.ajax(response.location, { ajax(response.location, {
type : "GET", type : "GET",
xhrFields: { responseType: "arraybuffer",
responseType: "arraybuffer" contentType: "application/octet-stream",
},
headers: {
"Content-Type": "application/octet-stream"
},
success : function(response, textStatus, jqXHR) { success : resolve,
resolve(response); error : function(result, code) {
},
error : function(jqXHR, textStatus, errorThrown) {
var code = jqXHR.status;
if (code > 999 || code < 100) if (code > 999 || code < 100)
code = -1; code = -1;
var e = new Error(code); var e = new Error(code);
e.name = "HTTPError"; e.name = "HTTPError";
if (jqXHR.responseJSON) if (result)
e.response = jqXHR.responseJSON; e.response = result;
reject(e); reject(e);
} }
}); });
@ -39257,9 +39276,9 @@ window.textsecure.api = function () {
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
$.ajax(response.location, { ajax(response.location, {
type : "PUT", type : "PUT",
headers : {"Content-Type" : "application/octet-stream"}, contentType : "application/octet-stream",
data : encryptedBin, data : encryptedBin,
processData : false, processData : false,
success : function() { success : function() {
@ -39272,15 +39291,14 @@ window.textsecure.api = function () {
reject(e); reject(e);
} }
}, },
error : function(jqXHR, textStatus, errorThrown) { error : function(result, code) {
var code = jqXHR.status;
if (code > 999 || code < 100) if (code > 999 || code < 100)
code = -1; code = -1;
var e = new Error(code); var e = new Error(code);
e.name = "HTTPError"; e.name = "HTTPError";
if (jqXHR.responseJSON) if (result)
e.response = jqXHR.responseJSON; e.response = result;
reject(e); reject(e);
} }
}); });
@ -39294,10 +39312,7 @@ window.textsecure.api = function () {
if (auth) { if (auth) {
var user = textsecure.storage.getUnencrypted("number_id"); var user = textsecure.storage.getUnencrypted("number_id");
var password = textsecure.storage.getEncrypted("password"); var password = textsecure.storage.getEncrypted("password");
var params = $.param({ var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password);
login: '+' + user.substring(1),
password: password
});
} }
return window.textsecure.websocket(URL+params) return window.textsecure.websocket(URL+params)
} }

@ -54,6 +54,41 @@ window.textsecure.api = function () {
* do_auth: alternative to user/password where user/password are figured out automagically * do_auth: alternative to user/password where user/password are figured out automagically
* jsonData: JSON data sent in the request body * jsonData: JSON data sent in the request body
*/ */
function ajax(url, options) {
var xhr = new XMLHttpRequest();
xhr.open(options.type, url, true /*async*/);
if ( options.responseType ) {
xhr[ 'responseType' ] = options.responseType;
}
if (options.user && options.password) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(getString(options.user) + ":" + getString(options.password)));
}
if (options.contentType) {
xhr.setRequestHeader( "Content-Type", options.contentType );
}
xhr.onload = function() {
var result = xhr.response;
if ( (!xhr.responseType || xhr.responseType === "text") &&
typeof xhr.responseText === "string" ) {
result = xhr.responseText;
}
if (options.dataType === 'json') {
try { result = JSON.parse(xhr.responseText + ''); } catch(e) {}
}
if (xhr.status < 400) {
options.success(result, xhr.status);
} else {
options.error(result, xhr.status);
}
};
xhr.onerror = function() {
options.error(null, xhr.status);
};
xhr.send( options.data || null );
};
var doAjax = function (param) { var doAjax = function (param) {
if (param.urlParameters === undefined) { if (param.urlParameters === undefined) {
param.urlParameters = ""; param.urlParameters = "";
@ -65,24 +100,15 @@ window.textsecure.api = function () {
} }
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
$.ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, { ajax(URL_BASE + URL_CALLS[param.call] + param.urlParameters, {
type : param.httpType, type : param.httpType,
data : param.jsonData && textsecure.utils.jsonThing(param.jsonData), data : param.jsonData && textsecure.utils.jsonThing(param.jsonData),
contentType : 'application/json; charset=utf-8', contentType : 'application/json; charset=utf-8',
dataType : 'json', dataType : 'json',
user : param.user,
beforeSend : function (xhr) { password : param.password,
if (param.user !== undefined && success : resolve,
param.password !== undefined) error : function(result, code) {
xhr.setRequestHeader("Authorization", "Basic " + btoa(getString(param.user) + ":" + getString(param.password)));
},
success : function(response, textStatus, jqXHR) {
resolve(response);
},
error : function(jqXHR, textStatus, errorThrown) {
var code = jqXHR.status;
if (code === 200) { if (code === 200) {
// happens sometimes when we get no response // happens sometimes when we get no response
// (TODO: Fix server to return 204? instead) // (TODO: Fix server to return 204? instead)
@ -117,8 +143,9 @@ window.textsecure.api = function () {
"The server rejected our query, please file a bug report."); "The server rejected our query, please file a bug report.");
} }
} catch (e) { } catch (e) {
if (jqXHR.responseJSON) if (result) {
e.response = jqXHR.responseJSON; e.response = result;
}
reject(e); reject(e);
} }
} }
@ -231,28 +258,20 @@ window.textsecure.api = function () {
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
$.ajax(response.location, { ajax(response.location, {
type : "GET", type : "GET",
xhrFields: { responseType: "arraybuffer",
responseType: "arraybuffer" contentType: "application/octet-stream",
},
headers: {
"Content-Type": "application/octet-stream"
},
success : function(response, textStatus, jqXHR) { success : resolve,
resolve(response); error : function(result, code) {
},
error : function(jqXHR, textStatus, errorThrown) {
var code = jqXHR.status;
if (code > 999 || code < 100) if (code > 999 || code < 100)
code = -1; code = -1;
var e = new Error(code); var e = new Error(code);
e.name = "HTTPError"; e.name = "HTTPError";
if (jqXHR.responseJSON) if (result)
e.response = jqXHR.responseJSON; e.response = result;
reject(e); reject(e);
} }
}); });
@ -268,9 +287,9 @@ window.textsecure.api = function () {
do_auth : true, do_auth : true,
}).then(function(response) { }).then(function(response) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
$.ajax(response.location, { ajax(response.location, {
type : "PUT", type : "PUT",
headers : {"Content-Type" : "application/octet-stream"}, contentType : "application/octet-stream",
data : encryptedBin, data : encryptedBin,
processData : false, processData : false,
success : function() { success : function() {
@ -283,15 +302,14 @@ window.textsecure.api = function () {
reject(e); reject(e);
} }
}, },
error : function(jqXHR, textStatus, errorThrown) { error : function(result, code) {
var code = jqXHR.status;
if (code > 999 || code < 100) if (code > 999 || code < 100)
code = -1; code = -1;
var e = new Error(code); var e = new Error(code);
e.name = "HTTPError"; e.name = "HTTPError";
if (jqXHR.responseJSON) if (result)
e.response = jqXHR.responseJSON; e.response = result;
reject(e); reject(e);
} }
}); });
@ -305,10 +323,7 @@ window.textsecure.api = function () {
if (auth) { if (auth) {
var user = textsecure.storage.getUnencrypted("number_id"); var user = textsecure.storage.getUnencrypted("number_id");
var password = textsecure.storage.getEncrypted("password"); var password = textsecure.storage.getEncrypted("password");
var params = $.param({ var params = 'login=%2B' + encodeURIComponent(user.substring(1)) + '&password=' + encodeURIComponent(password);
login: '+' + user.substring(1),
password: password
});
} }
return window.textsecure.websocket(URL+params) return window.textsecure.websocket(URL+params)
} }

Loading…
Cancel
Save