From 269d5d4c8a77c4d004f0028c00d0c5a26481ee00 Mon Sep 17 00:00:00 2001 From: russa Date: Wed, 16 Sep 2020 17:44:59 +0200 Subject: [PATCH] added abort() function to js-module --- www/error-codes.js | 1 + www/helpers.js | 8 ++++++++ www/public-interface.js | 23 ++++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/www/error-codes.js b/www/error-codes.js index a0bfd6c..15c13c0 100644 --- a/www/error-codes.js +++ b/www/error-codes.js @@ -6,4 +6,5 @@ module.exports = { UNSUPPORTED_URL: -5, NOT_CONNECTED: -6, POST_PROCESSING_FAILED: -7, + ABORTED: -8, }; diff --git a/www/helpers.js b/www/helpers.js index b88d776..19ad259 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -5,6 +5,13 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'options', 'upload', 'download']; var validResponseTypes = ['text', 'json', 'arraybuffer', 'blob']; + var nextRequestId = (function(){ + var currReqId = 0; + return function nextRequestId() { + return ++currReqId; + } + })(); + var interface = { b64EncodeUnicode: b64EncodeUnicode, checkClientAuthMode: checkClientAuthMode, @@ -24,6 +31,7 @@ module.exports = function init(global, jsUtil, cookieHandler, messages, base64, injectCookieHandler: injectCookieHandler, injectFileEntryHandler: injectFileEntryHandler, injectRawResponseHandler: injectRawResponseHandler, + nextRequestId: nextRequestId, }; // expose all functions for testing purposes diff --git a/www/public-interface.js b/www/public-interface.js index 73f75a9..650b5fa 100644 --- a/www/public-interface.js +++ b/www/public-interface.js @@ -26,6 +26,7 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf options: options, uploadFile: uploadFile, downloadFile: downloadFile, + abort: abort, ErrorCode: errorCodes, ponyfills: ponyfills }; @@ -143,23 +144,31 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf var onFail = helpers.injectCookieHandler(url, failure); var onSuccess = helpers.injectCookieHandler(url, helpers.injectRawResponseHandler(options.responseType, success, failure)); + var reqId = helpers.nextRequestId(); + switch (options.method) { case 'post': case 'put': case 'patch': - return helpers.processData(options.data, options.serializer, function (data) { - exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, data, options.serializer, headers, options.timeout, options.followRedirect, options.responseType]); + helpers.processData(options.data, options.serializer, function (data) { + exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, data, options.serializer, headers, options.timeout, options.followRedirect, options.responseType, reqId]); }); + break; case 'upload': var fileOptions = helpers.checkUploadFileOptions(options.filePath, options.name); - return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFiles', [url, headers, fileOptions.filePaths, fileOptions.names, options.timeout, options.followRedirect, options.responseType]); + exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFiles', [url, headers, fileOptions.filePaths, fileOptions.names, options.timeout, options.followRedirect, options.responseType, reqId]); + break; case 'download': var filePath = helpers.checkDownloadFilePath(options.filePath); var onDownloadSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success)); - return exec(onDownloadSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, headers, filePath, options.timeout, options.followRedirect]); + exec(onDownloadSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, headers, filePath, options.timeout, options.followRedirect, reqId]); + break; default: - return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, headers, options.timeout, options.followRedirect, options.responseType]); + exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, headers, options.timeout, options.followRedirect, options.responseType, reqId]); + break; } + + return reqId; } function post(url, data, headers, success, failure) { @@ -198,5 +207,9 @@ module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConf return publicInterface.sendRequest(url, { method: 'download', params: params, headers: headers, filePath: filePath }, success, failure); } + function abort(requestId , success, failure) { + return exec(success, failure, 'CordovaHttpPlugin', 'abort', [requestId]); + } + return publicInterface; }