Improve script URL ecoding

added some magic to get scripts to efficiently encoded into the URL.
We're using pako to compress the script, but this outputs to a 8bit
array. Stringifying this array adds a lot of overhead, because "125"
has three characters in it.
Instead we're using the character codes to turn these a bit numbers
into single characters base64 is used as well because not all of the
characters are allowed in a url (and b64 is better than
encodeURIComponent).
This commit is contained in:
Kurt Hutten
2021-05-08 09:07:54 +10:00
parent 53985dd250
commit 053b1d642c
5 changed files with 40 additions and 7 deletions

View File

@@ -0,0 +1,21 @@
import { inflate, deflate } from 'pako'
/*
some magic to get scripts to efficiently encoded into the URL.
We're using pako to compress the script, but this outputs to a 8bit array. Stringifying this array adds a lot of overhead, because "125" has three characters in it
Instead we're using the character codes to turn these a bit numbers into single characters
base64 is used as well because not all of the characters are allowed in a url (and b64 is better than encodeURIComponent)
*/
export const encode = (string: string): string =>
btoa(String.fromCharCode.apply(null, deflate(string)))
export const decode = (string: string): string =>
inflate(
new Uint8Array(
atob(string)
.split('')
.map((character) => character.charCodeAt(0))
),
{ to: 'string' }
)