Skip to content
Open
Show file tree
Hide file tree
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
13 changes: 6 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "reshape-preact-components",
"description":
"server render preact components and use them like custom elements",
"description": "server render preact components and use them like custom elements",
"version": "0.6.0",
"author": "reshape",
"ava": {
Expand All @@ -10,10 +9,12 @@
"browser": "lib/browser.js",
"bugs": "https://github.com/reshape/preact-components/issues",
"dependencies": {
"buffer": "^5.0.8",
"preact": "^8.1.0",
"preact-render-to-string": "^3.6.2",
"reshape-parser": "^0.2.1",
"reshape-plugin-util": "^0.2.1"
"reshape-plugin-util": "^0.2.1",
"utf8": "^3.0.0"
},
"devDependencies": {
"ava": "^0.19.1",
Expand Down Expand Up @@ -48,11 +49,9 @@
"main": "lib",
"repository": "reshape/preact-components",
"scripts": {
"build":
"rm -rf lib && mkdir lib && cp src/index.js lib && cp src/serialize.js lib && cp src/reshape-ast-to-vdom.js lib && rollup -c rollup.browser.js && rollup -c rollup.ast-vdom.js",
"build": "rm -rf lib && mkdir lib && cp src/index.js lib && cp src/serialize.js lib && cp src/reshape-ast-to-vdom.js lib && rollup -c rollup.browser.js && rollup -c rollup.ast-vdom.js",
"codecov": "nyc report -r lcovonly && codecov",
"coverage":
"nyc ava && nyc report --reporter=html && open ./coverage/index.html",
"coverage": "nyc ava && nyc report --reporter=html && open ./coverage/index.html",
"lint": "standard | snazzy",
"test": "npm run build && nyc ava",
"version": "npm run build"
Expand Down
6 changes: 3 additions & 3 deletions src/browser.js
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'
51 changes: 16 additions & 35 deletions src/serialize.js
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
Copy link
Member

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

Copy link
Author

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 inside node_modules instead of using buffer from the internal api


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
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,13 @@ buffer@^4.3.0:
ieee754 "^1.1.4"
isarray "^1.0.0"

buffer@^5.0.8:
version "5.0.8"
resolved "https://registry.yarnpkg.com/buffer/-/buffer-5.0.8.tgz#84daa52e7cf2fa8ce4195bc5cf0f7809e0930b24"
dependencies:
base64-js "^1.0.2"
ieee754 "^1.1.4"

builtin-modules@^1.0.0, builtin-modules@^1.1.0, builtin-modules@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/builtin-modules/-/builtin-modules-1.1.1.tgz#270f076c5a72c02f5b65a47df94c5fe3a278892f"
Expand Down Expand Up @@ -4910,6 +4917,10 @@ user-home@^2.0.0:
dependencies:
os-homedir "^1.0.0"

utf8@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/utf8/-/utf8-3.0.0.tgz#f052eed1364d696e769ef058b183df88c87f69d1"

util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
Expand Down