-
Notifications
You must be signed in to change notification settings - Fork 3
Fix Unicode Encode/Decode Process #9
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
Open
hhsnopek
wants to merge
1
commit into
reshape:master
Choose a base branch
from
carrot:unicode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import toVdom from './reshape-ast-to-vdom' | ||
import { browserDecode } from './serialize' | ||
import { decode } from './serialize' | ||
|
||
export function hydrateInitialState(encoded, components) { | ||
return toVdom(components, browserDecode(encoded)) | ||
return toVdom(components, decode(encoded)) | ||
} | ||
|
||
export { browserEncode as encode, browserDecode as decode } from './serialize' | ||
export { encode, decode } from './serialize' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,22 @@ | ||
function encode(data) { | ||
return Buffer.from(prepareUnicodeEncode(JSON.stringify(data))).toString( | ||
'base64' | ||
) | ||
} | ||
module.exports.encode = encode | ||
const utf8 = require('utf8') | ||
const Buffer = require('buffer/').Buffer | ||
|
||
function browserEncode(data) { | ||
return window.btoa(prepareUnicodeEncode(JSON.stringify(data))) | ||
// Consumes JSON Object and returns base64 | ||
function encode(object) { | ||
const stringifiedData = JSON.stringify(object) | ||
const utf8Data = utf8.encode(stringifiedData) | ||
const base64Data = new Buffer(utf8Data).toString('base64') | ||
return base64Data | ||
} | ||
module.exports.browserEncode = browserEncode | ||
|
||
function decode(data) { | ||
return JSON.parse(prepareUnicodeDecode(String(Buffer.from(data, 'base64')))) | ||
} | ||
module.exports.decode = decode | ||
// Consumes base64 and returns JSON Object | ||
function decode(base64Data) { | ||
const utf8Data = new Buffer(base64Data, 'base64').toString('utf8') | ||
const stringifiedData = utf8.decode(utf8Data) | ||
const object = JSON.parse(stringifiedData) | ||
|
||
function browserDecode(data) { | ||
return JSON.parse(prepareUnicodeDecode(window.atob(data))) | ||
return object | ||
} | ||
module.exports.browserDecode = browserDecode | ||
|
||
// ref: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem | ||
// encode result to b64 | ||
function prepareUnicodeEncode(str) { | ||
return encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => { | ||
return String.fromCharCode(`0x${p1}`) | ||
}) | ||
} | ||
|
||
// pass in already b64 decoded | ||
function prepareUnicodeDecode(str) { | ||
return decodeURIComponent( | ||
str | ||
.split('') | ||
.map(function(c) { | ||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2) | ||
}) | ||
.join('') | ||
) | ||
} | ||
module.exports.encode = encode | ||
module.exports.decode = decode |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
typo here, no idea how tests would be passing
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.
I'm guessing you're referring to the
/
in the require? this forces node to look insidenode_modules
instead of usingbuffer
from the internal api