Skip to content

Commit

Permalink
Add support for escape to default render function
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Nov 27, 2019
1 parent 838e8c3 commit 9b4ef7f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,32 @@ app.listen(3000, err => {
```js
// default render function
function render(_template, _data) {
function render (_template, _data) {
/* eslint no-unused-vars: 0 */
function quote (data, type) {
if (type) {
if (type === String) {
return '"' + data.toString().replace(/("|\\)/g, '\\$1') + '"'
} else if (type === Number) {
return Number(data)
} else {
return JSON.stringify(data)
}
} else {
return JSON.stringify(data)
}
}
function escapeHTML (data) {
return data.toString().replace(/[\u00A0-\u9999<>&]/gim, (i) => `&#${i.charCodeAt(0)};`)
}
function escapeURL (data) {
return encodeURI(data.toString())
}
/* eslint no-eval: 0 */
return eval(
'const '
+ Object.keys(_data).map(key => `${key} = ${JSON.stringify(_data[key])}`).join()
+ ';(`' + _template.toString().replace(/\\/g, '\\\\').replace(/`/g, '\\`') + '`)'
'const ' +
Object.keys(_data).map(key => `${key} = ${JSON.stringify(_data[key])}`).join() + ';' +
'(`' + _template.toString().replace(/(`|\\)/g, '\\$1') + '`)'
)
}
```
Expand Down
38 changes: 30 additions & 8 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ if (!process.nextTick) {
}
process.on('exit', uWS.free)

function render (_template, _data) {
/* eslint no-unused-vars: 0 */
function quote (data, type) {
if (type) {
if (type === String) {
return '"' + data.toString().replace(/("|\\)/g, '\\$1') + '"'
} else if (type === Number) {
return Number(data)
} else {
return JSON.stringify(data)
}
} else {
return JSON.stringify(data)
}
}
function escapeHTML (data) {
return data.toString().replace(/[\u00A0-\u9999<>&]/gim, (i) => `&#${i.charCodeAt(0)};`)
}
function escapeURL (data) {
return encodeURI(data.toString())
}
/* eslint no-eval: 0 */
return eval(
'const ' +
Object.keys(_data).map(key => `${key} = ${JSON.stringify(_data[key])}`).join() + ';' +
'(`' + _template.toString().replace(/(`|\\)/g, '\\$1') + '`)'
)
}

class fastWS {
constructor ({
ssl = null,
Expand All @@ -32,14 +61,7 @@ class fastWS {
if (typeof templateRender === 'function') {
this._templateRender = templateRender
} else {
this._templateRender = function (_template, _data) {
/* eslint no-eval: 0 */
return global.eval(
'const ' +
Object.keys(_data).map(key => `${key} = ${JSON.stringify(_data[key])}`).join() +
';(`' + _template.toString().replace(/\\/g, '\\\\').replace(/`/g, '\\`') + '`)'
)
}
this._templateRender = render
}
if (typeof cache === 'object' && !(cache instanceof Object)) {
this._cache = cache
Expand Down

0 comments on commit 9b4ef7f

Please sign in to comment.