Skip to content

Commit

Permalink
Merge pull request #1444 from scireum/feature/sbi/SIRI-987
Browse files Browse the repository at this point in the history
Shortens encoded deeplink URL parameter value
  • Loading branch information
sabieber authored Jul 17, 2024
2 parents ec25b26 + dd5268a commit 970fd64
Showing 1 changed file with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,32 +333,27 @@ window.sirius.Continuity = (function () {
}

const paramStateText = JSON.stringify(paramState);
return this.options.encodeStateParameter ? convertToBinary(paramStateText) : paramStateText;
return this.options.encodeStateParameter ? btoa(paramStateText) : paramStateText;
};

/**@
* Converts the given UTF-8 text to a binary base64 encoded representation.
*
* @param string the text to encode
* @returns {string} the binary base64 encoded representation of the text
*/
function convertToBinary(string) {
const codeUnits = new Uint16Array(string.length);
for (let i = 0; i < codeUnits.length; i++) {
codeUnits[i] = string.charCodeAt(i);
}
return btoa(String.fromCharCode.apply(this, new Uint8Array(codeUnits.buffer)));
}

/**@
* Parses the value of our history state deep-link parameter from the URL and transforms it into a JS Object.
*
* @param {string} param the value of the parameter to parse
* @returns {Object} the history state as an object
*/
Continuity.prototype.parseStateUrlParam = function (param) {
const paramStateText = this.options.encodeStateParameter ? convertFromBinary(param) : param;
return JSON.parse(paramStateText);
if (!this.options.encodeStateParameter) {
return JSON.parse(param);
}
try {
// Try to decode the new binary format first.
return JSON.parse(atob(param));
} catch (exception) {
// If the new format fails, try to decode the old legacy format.
// When this also fails we throw the exception.
return JSON.parse(convertFromLegacyBinary(param));
}
};

/**@
Expand All @@ -367,7 +362,7 @@ window.sirius.Continuity = (function () {
* @param string the binary base64 encoded representation of a text
* @returns {string} the original text
*/
function convertFromBinary(encoded) {
function convertFromLegacyBinary(encoded) {
const binary = atob(encoded);
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < bytes.length; i++) {
Expand Down

0 comments on commit 970fd64

Please sign in to comment.