Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortens encoded deeplink URL parameter value #1444

Merged
merged 4 commits into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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