Fix #201: browser implementation broken due to broken dependency

This commit is contained in:
Sefa Ilkimen
2019-04-08 20:12:36 +02:00
parent 4b964e8b7c
commit 70c8b9bb32
7 changed files with 54 additions and 50 deletions
+1
View File
@@ -11,6 +11,7 @@
<js-module src="www/cookie-handler.js" name="cookie-handler"/> <js-module src="www/cookie-handler.js" name="cookie-handler"/>
<js-module src="www/global-configs.js" name="global-configs"/> <js-module src="www/global-configs.js" name="global-configs"/>
<js-module src="www/helpers.js" name="helpers"/> <js-module src="www/helpers.js" name="helpers"/>
<js-module src="www/js-util.js" name="js-util"/>
<js-module src="www/local-storage-store.js" name="local-storage-store"/> <js-module src="www/local-storage-store.js" name="local-storage-store"/>
<js-module src="www/lodash.js" name="lodash"/> <js-module src="www/lodash.js" name="lodash"/>
<js-module src="www/messages.js" name="messages"/> <js-module src="www/messages.js" name="messages"/>
+4 -4
View File
@@ -1,7 +1,7 @@
var pluginId = module.id.slice(0, module.id.lastIndexOf('.')); var pluginId = module.id.slice(0, module.id.lastIndexOf('.'));
var cordovaProxy = require('cordova/exec/proxy'); var cordovaProxy = require('cordova/exec/proxy');
var helpers = require(pluginId + '.helpers'); var jsUtil = require(pluginId + '.js-util');
function serializeJsonData(data) { function serializeJsonData(data) {
try { try {
@@ -29,7 +29,7 @@ function serializeParams(params) {
if (params === null) return ''; if (params === null) return '';
return Object.keys(params).map(function(key) { return Object.keys(params).map(function(key) {
if (helpers.getTypeOf(params[key]) === 'Array') { if (jsUtil.getTypeOf(params[key]) === 'Array') {
return serializeArray(key, params[key]); return serializeArray(key, params[key]);
} }
@@ -56,7 +56,7 @@ function createXhrSuccessObject(xhr) {
return { return {
url: xhr.responseURL, url: xhr.responseURL,
status: xhr.status, status: xhr.status,
data: helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response, data: jsUtil.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response,
headers: deserializeResponseHeaders(xhr.getAllResponseHeaders()) headers: deserializeResponseHeaders(xhr.getAllResponseHeaders())
}; };
} }
@@ -65,7 +65,7 @@ function createXhrFailureObject(xhr) {
var obj = {}; var obj = {};
obj.headers = xhr.getAllResponseHeaders(); obj.headers = xhr.getAllResponseHeaders();
obj.error = helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response; obj.error = jsUtil.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response;
obj.error = obj.error || 'advanced-http: please check browser console for error messages'; obj.error = obj.error || 'advanced-http: please check browser console for error messages';
if (xhr.responseURL) obj.url = xhr.responseURL; if (xhr.responseURL) obj.url = xhr.responseURL;
+5 -4
View File
@@ -10,12 +10,13 @@ describe('Advanced HTTP public interface', function () {
const getDependenciesBlueprint = () => { const getDependenciesBlueprint = () => {
const messages = require('../www/messages'); const messages = require('../www/messages');
const globalConfigs = require('../www/global-configs'); const globalConfigs = require('../www/global-configs');
const jsUtil = require('../www/js-util');
const ToughCookie = require('../www/umd-tough-cookie'); const ToughCookie = require('../www/umd-tough-cookie');
const lodash = require('../www/lodash'); const lodash = require('../www/lodash');
const WebStorageCookieStore = require('../www/local-storage-store')(ToughCookie, lodash); const WebStorageCookieStore = require('../www/local-storage-store')(ToughCookie, lodash);
const cookieHandler = require('../www/cookie-handler')(null, ToughCookie, WebStorageCookieStore); const cookieHandler = require('../www/cookie-handler')(null, ToughCookie, WebStorageCookieStore);
const helpers = require('../www/helpers')(cookieHandler, messages); const helpers = require('../www/helpers')(jsUtil, cookieHandler, messages);
const urlUtil = require('../www/url-util')(helpers); const urlUtil = require('../www/url-util')(jsUtil);
return { exec: noop, cookieHandler, urlUtil: urlUtil, helpers, globalConfigs }; return { exec: noop, cookieHandler, urlUtil: urlUtil, helpers, globalConfigs };
}; };
@@ -131,8 +132,8 @@ describe('Advanced HTTP public interface', function () {
}); });
describe('URL util', function () { describe('URL util', function () {
const helpers = require('../www/helpers')(null, null); const jsUtil = require('../www/js-util');
const util = require('../www/url-util')(helpers); const util = require('../www/url-util')(jsUtil);
it('parses URL with protocol, hostname and path correctly', () => { it('parses URL with protocol, hostname and path correctly', () => {
util.parseUrl('http://ilkimen.net/test').should.include({ util.parseUrl('http://ilkimen.net/test').should.include({
+3 -2
View File
@@ -7,12 +7,13 @@ var pluginId = module.id.slice(0, module.id.lastIndexOf('.'));
var exec = require('cordova/exec'); var exec = require('cordova/exec');
var messages = require(pluginId + '.messages'); var messages = require(pluginId + '.messages');
var globalConfigs = require(pluginId + '.global-configs'); var globalConfigs = require(pluginId + '.global-configs');
var jsUtil = require(pluginId + '.js-util');
var ToughCookie = require(pluginId + '.tough-cookie'); var ToughCookie = require(pluginId + '.tough-cookie');
var lodash = require(pluginId + '.lodash'); var lodash = require(pluginId + '.lodash');
var WebStorageCookieStore = require(pluginId + '.local-storage-store')(ToughCookie, lodash); var WebStorageCookieStore = require(pluginId + '.local-storage-store')(ToughCookie, lodash);
var cookieHandler = require(pluginId + '.cookie-handler')(window.localStorage, ToughCookie, WebStorageCookieStore); var cookieHandler = require(pluginId + '.cookie-handler')(window.localStorage, ToughCookie, WebStorageCookieStore);
var helpers = require(pluginId + '.helpers')(cookieHandler, messages); var helpers = require(pluginId + '.helpers')(jsUtil, cookieHandler, messages);
var urlUtil = require(pluginId + '.url-util')(helpers); var urlUtil = require(pluginId + '.url-util')(jsUtil);
var publicInterface = require(pluginId + '.public-interface')(exec, cookieHandler, urlUtil, helpers, globalConfigs); var publicInterface = require(pluginId + '.public-interface')(exec, cookieHandler, urlUtil, helpers, globalConfigs);
module.exports = publicInterface; module.exports = publicInterface;
+10 -35
View File
@@ -1,4 +1,4 @@
module.exports = function init(cookieHandler, messages) { module.exports = function init(jsUtil, cookieHandler, messages) {
var validSerializers = ['urlencoded', 'json', 'utf8']; var validSerializers = ['urlencoded', 'json', 'utf8'];
var validCertModes = ['default', 'nocheck', 'pinned', 'legacy']; var validCertModes = ['default', 'nocheck', 'pinned', 'legacy'];
var validClientAuthModes = ['none', 'systemstore', 'file']; var validClientAuthModes = ['none', 'systemstore', 'file'];
@@ -6,7 +6,6 @@ module.exports = function init(cookieHandler, messages) {
return { return {
b64EncodeUnicode: b64EncodeUnicode, b64EncodeUnicode: b64EncodeUnicode,
getTypeOf: getTypeOf,
checkSerializer: checkSerializer, checkSerializer: checkSerializer,
checkSSLCertMode: checkSSLCertMode, checkSSLCertMode: checkSSLCertMode,
checkClientAuthMode: checkClientAuthMode, checkClientAuthMode: checkClientAuthMode,
@@ -43,7 +42,7 @@ module.exports = function init(cookieHandler, messages) {
} }
function checkForValidStringValue(list, value, onInvalidValueMessage) { function checkForValidStringValue(list, value, onInvalidValueMessage) {
if (getTypeOf(value) !== 'String') { if (jsUtil.getTypeOf(value) !== 'String') {
throw new Error(onInvalidValueMessage + ' ' + list.join(', ')); throw new Error(onInvalidValueMessage + ' ' + list.join(', '));
} }
@@ -57,14 +56,14 @@ module.exports = function init(cookieHandler, messages) {
} }
function checkKeyValuePairObject(obj, allowedChildren, onInvalidValueMessage) { function checkKeyValuePairObject(obj, allowedChildren, onInvalidValueMessage) {
if (getTypeOf(obj) !== 'Object') { if (jsUtil.getTypeOf(obj) !== 'Object') {
throw new Error(onInvalidValueMessage); throw new Error(onInvalidValueMessage);
} }
var keys = Object.keys(obj); var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) { for (var i = 0; i < keys.length; i++) {
if (allowedChildren.indexOf(getTypeOf(obj[keys[i]])) === -1) { if (allowedChildren.indexOf(jsUtil.getTypeOf(obj[keys[i]])) === -1) {
throw new Error(onInvalidValueMessage); throw new Error(onInvalidValueMessage);
} }
} }
@@ -97,7 +96,7 @@ module.exports = function init(cookieHandler, messages) {
} }
function checkForInvalidHeaderValue(value) { function checkForInvalidHeaderValue(value) {
if (getTypeOf(value) !== 'String') { if (jsUtil.getTypeOf(value) !== 'String') {
throw new Error(messages.INVALID_HEADERS_VALUE); throw new Error(messages.INVALID_HEADERS_VALUE);
} }
@@ -105,7 +104,7 @@ module.exports = function init(cookieHandler, messages) {
} }
function checkTimeoutValue(timeout) { function checkTimeoutValue(timeout) {
if (getTypeOf(timeout) !== 'Number' || timeout < 0) { if (jsUtil.getTypeOf(timeout) !== 'Number' || timeout < 0) {
throw new Error(messages.INVALID_TIMEOUT_VALUE); throw new Error(messages.INVALID_TIMEOUT_VALUE);
} }
@@ -180,30 +179,6 @@ module.exports = function init(cookieHandler, messages) {
return mergedHeaders; return mergedHeaders;
} }
// typeof is not working reliably in JS
function getTypeOf(object) {
switch (Object.prototype.toString.call(object)) {
case '[object Array]':
return 'Array';
case '[object Boolean]':
return 'Boolean';
case '[object Function]':
return 'Function';
case '[object Null]':
return 'Null';
case '[object Number]':
return 'Number';
case '[object Object]':
return 'Object';
case '[object String]':
return 'String';
case '[object Undefined]':
return 'Undefined';
default:
return 'Unknown';
}
}
function getAllowedDataTypes(dataSerializer) { function getAllowedDataTypes(dataSerializer) {
switch (dataSerializer) { switch (dataSerializer) {
case 'utf8': case 'utf8':
@@ -216,7 +191,7 @@ module.exports = function init(cookieHandler, messages) {
} }
function getProcessedData(data, dataSerializer) { function getProcessedData(data, dataSerializer) {
var currentDataType = getTypeOf(data); var currentDataType = jsUtil.getTypeOf(data);
var allowedDataTypes = getAllowedDataTypes(dataSerializer); var allowedDataTypes = getAllowedDataTypes(dataSerializer);
if (allowedDataTypes.indexOf(currentDataType) === -1) { if (allowedDataTypes.indexOf(currentDataType) === -1) {
@@ -231,11 +206,11 @@ module.exports = function init(cookieHandler, messages) {
} }
function handleMissingCallbacks(successFn, failFn) { function handleMissingCallbacks(successFn, failFn) {
if (getTypeOf(successFn) !== 'Function') { if (jsUtil.getTypeOf(successFn) !== 'Function') {
throw new Error(messages.MANDATORY_SUCCESS); throw new Error(messages.MANDATORY_SUCCESS);
} }
if (getTypeOf(failFn) !== 'Function') { if (jsUtil.getTypeOf(failFn) !== 'Function') {
throw new Error(messages.MANDATORY_FAIL); throw new Error(messages.MANDATORY_FAIL);
} }
} }
@@ -249,7 +224,7 @@ module.exports = function init(cookieHandler, messages) {
timeout: checkTimeoutValue(options.timeout || globals.timeout), timeout: checkTimeoutValue(options.timeout || globals.timeout),
headers: checkHeadersObject(options.headers || {}), headers: checkHeadersObject(options.headers || {}),
params: checkParamsObject(options.params || {}), params: checkParamsObject(options.params || {}),
data: getTypeOf(options.data) === 'Undefined' ? null : options.data, data: jsUtil.getTypeOf(options.data) === 'Undefined' ? null : options.data,
filePath: options.filePath || '', filePath: options.filePath || '',
name: options.name || '' name: options.name || ''
}; };
+26
View File
@@ -0,0 +1,26 @@
module.exports = {
// typeof is not working reliably in JS
getTypeOf: function (object) {
switch (Object.prototype.toString.call(object)) {
case '[object Array]':
return 'Array';
case '[object Boolean]':
return 'Boolean';
case '[object Function]':
return 'Function';
case '[object Null]':
return 'Null';
case '[object Number]':
return 'Number';
case '[object Object]':
return 'Object';
case '[object String]':
return 'String';
case '[object Undefined]':
return 'Undefined';
default:
return 'Unknown';
}
}
}
+5 -5
View File
@@ -1,4 +1,4 @@
module.exports = function init(helpers) { module.exports = function init(jsUtil) {
return { return {
parseUrl: parseUrl, parseUrl: parseUrl,
appendQueryParamsString: appendQueryParamsString, appendQueryParamsString: appendQueryParamsString,
@@ -48,10 +48,10 @@ module.exports = function init(helpers) {
var identifier = parentKey.length ? parentKey + '[' + key + ']' : key; var identifier = parentKey.length ? parentKey + '[' + key + ']' : key;
if (helpers.getTypeOf(object[key]) === 'Array') { if (jsUtil.getTypeOf(object[key]) === 'Array') {
parts.push(serializeArray(identifier, object[key], encode)); parts.push(serializeArray(identifier, object[key], encode));
continue; continue;
} else if (helpers.getTypeOf(object[key]) === 'Object') { } else if (jsUtil.getTypeOf(object[key]) === 'Object') {
parts.push(serializeObject(identifier, object[key], encode)); parts.push(serializeObject(identifier, object[key], encode));
continue; continue;
} }
@@ -66,10 +66,10 @@ module.exports = function init(helpers) {
var parts = []; var parts = [];
for (var i = 0; i < array.length; ++i) { for (var i = 0; i < array.length; ++i) {
if (helpers.getTypeOf(array[i]) === 'Array') { if (jsUtil.getTypeOf(array[i]) === 'Array') {
parts.push(serializeArray(parentKey + '[]', array[i], encode)); parts.push(serializeArray(parentKey + '[]', array[i], encode));
continue; continue;
} else if (helpers.getTypeOf(array[i]) === 'Object') { } else if (jsUtil.getTypeOf(array[i]) === 'Object') {
parts.push(serializeObject(parentKey + '[]' + array[i], encode)); parts.push(serializeObject(parentKey + '[]' + array[i], encode));
continue; continue;
} }