Updating Encoding Decoding to modern standards in floCloudAPI.js
1. The encoding scheme was convoluted. We upgraded to TextEncoder and TextDecoder. 2. In case normal decoding fails, it will revert to old decoding.
This commit is contained in:
parent
b374aacb4a
commit
bd6ee6adb5
@ -374,13 +374,31 @@
|
|||||||
|
|
||||||
const util = floCloudAPI.util = {};
|
const util = floCloudAPI.util = {};
|
||||||
|
|
||||||
|
//Updating encoding/Decoding to modern standards
|
||||||
const encodeMessage = util.encodeMessage = function (message) {
|
const encodeMessage = util.encodeMessage = function (message) {
|
||||||
return btoa(unescape(encodeURIComponent(JSON.stringify(message))))
|
const bytes = new TextEncoder().encode(JSON.stringify(message));
|
||||||
}
|
let bin = "";
|
||||||
|
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
||||||
|
return btoa(bin);
|
||||||
|
};
|
||||||
|
|
||||||
const decodeMessage = util.decodeMessage = function (message) {
|
const decodeMessage = util.decodeMessage = function (message) {
|
||||||
return JSON.parse(decodeURIComponent(escape(atob(message))))
|
try {
|
||||||
}
|
// try modern decode first
|
||||||
|
const bin = atob(message);
|
||||||
|
const bytes = new Uint8Array(bin.length);
|
||||||
|
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
|
||||||
|
return JSON.parse(new TextDecoder().decode(bytes));
|
||||||
|
} catch (e1) {
|
||||||
|
try {
|
||||||
|
// fallback to legacy decode
|
||||||
|
return JSON.parse(decodeURIComponent(escape(atob(message))));
|
||||||
|
} catch (e2) {
|
||||||
|
// final fallback: return raw string to avoid hard crash
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const filterKey = util.filterKey = function (type, options = {}) {
|
const filterKey = util.filterKey = function (type, options = {}) {
|
||||||
return type + (options.comment ? ':' + options.comment : '') +
|
return type + (options.comment ? ':' + options.comment : '') +
|
||||||
@ -1114,4 +1132,4 @@
|
|||||||
})();
|
})();
|
||||||
|
|
||||||
|
|
||||||
})('object' === typeof module ? module.exports : window.floCloudAPI = {});
|
})('object' === typeof module ? module.exports : window.floCloudAPI = {});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user