From ecad77263585cd5954758f797327d98232d880dc Mon Sep 17 00:00:00 2001 From: Maximilian Krambach Date: Tue, 22 May 2018 14:24:16 +0200 Subject: js: transfer encoding changes -- * Uint8Arrays are not supported for now there are unsolved issues in conversion, and they are lower priority * encrypt gains a new option to indicate that input values are base64 encoded * as decrypted values are always base64 encoded, the option base64 will not try to decode the result into utf, but leave it as it is --- lang/js/src/Connection.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'lang/js/src/Connection.js') diff --git a/lang/js/src/Connection.js b/lang/js/src/Connection.js index 1931a55b..9c2a6428 100644 --- a/lang/js/src/Connection.js +++ b/lang/js/src/Connection.js @@ -93,7 +93,7 @@ export class Connection{ } let me = this; return new Promise(function(resolve, reject){ - let answer = new Answer(message.operation); + let answer = new Answer(message); let listener = function(msg) { if (!msg){ me._connection.onMessage.removeListener(listener) @@ -147,8 +147,9 @@ export class Connection{ */ class Answer{ - constructor(operation){ - this.operation = operation; + constructor(message){ + this.operation = message.operation; + this.expected = message.expected; } /** @@ -210,26 +211,31 @@ class Answer{ } /** - * @returns {Object} the assembled message. - * TODO: does not care yet if completed. + * @returns {Object} the assembled message, original data assumed to be + * (javascript-) strings */ get message(){ let keys = Object.keys(this._response); + let msg = {}; let poa = permittedOperations[this.operation].answer; for (let i=0; i < keys.length; i++) { - if (poa.data.indexOf(keys[i]) >= 0){ - if (this._response.base64 == true){ - let respatob = atob(this._response[keys[i]]); - - let result = decodeURIComponent( - respatob.split('').map(function(c) { + if (poa.data.indexOf(keys[i]) >= 0 + && this._response.base64 === true + ) { + msg[keys[i]] = atob(this._response[keys[i]]); + if (this.expected === 'base64'){ + msg[keys[i]] = this._response[keys[i]]; + } else { + msg[keys[i]] = decodeURIComponent( + atob(this._response[keys[i]]).split('').map(function(c) { return '%' + - ('00' + c.charCodeAt(0).toString(16)).slice(-2); + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }).join('')); - this._response[keys[i]] = result; } + } else { + msg[keys[i]] = this._response[keys[i]]; } } - return this._response; + return msg; } } -- cgit v1.2.3