-
Notifications
You must be signed in to change notification settings - Fork 175
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
feat: gzip serialized state #43
Conversation
212a0e9
to
5d50299
Compare
removes checksum and header
src/utils.ts
Outdated
return btoa(unescape(encodeURIComponent(data))) | ||
const zipped = deflate(data, { level: 9 }) | ||
const b = Array.from(zipped, v => String.fromCharCode(v)).join('') | ||
return btoa(b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's no longer necessary to encode the data before passing it to btoa because the zipped string will only have ASCII characters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. Uint8Array is 0-255, btoa accepts Latin1 characters.
src/utils.ts
Outdated
|
||
// old unicode hacks for backward compatibility | ||
// https://base64.guru/developers/javascript/examples/unicode-strings | ||
return decodeURIComponent(escape(b)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it necessary to decode if the string wasn't initially encoded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so it still supports old links. Line 21 is checking for the zlib header and has an early return.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah of course 🤦♂️
Consider using fflate instead since it's significantly smaller (8kb vs 45kb): https://github.com/101arrowz/fflate |
The repl produces very long URLs, we can shorten them by gzipping the stringified state before converting to base64
backwards compatible,
atou
checks if the string was gzippedExample
Before (2156 bytes):
eyJBcHAudnVlIjoiPHNjcmlwdCBzZXR1cD5cblx0aW1wb3J0IEdyb3VwIGZyb20gJy4vR3JvdXAuanMnXG4gIGltcG9ydCBJdGVtIGZyb20gJy4vSXRlbS52dWUnXG4gIGltcG9ydCBXcmFwcGVkSXRlbSBmcm9tICcu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