Menci
6 years ago
5 changed files with 103 additions and 94 deletions
@ -1,32 +1,32 @@
|
||||
const child_process = require('child_process'); |
||||
|
||||
const rendererd = child_process.fork(__dirname + '/rendererd', [syzoj.config.redis]); |
||||
|
||||
const resolver = {}; |
||||
let currentId = 0; |
||||
|
||||
rendererd.on('message', msg => { |
||||
resolver[msg.id](msg.result); |
||||
delete resolver[msg.id]; |
||||
}); |
||||
|
||||
exports.markdown = (markdownCode, callback) => { |
||||
resolver[++currentId] = callback; |
||||
rendererd.send({ |
||||
id: currentId, |
||||
type: 'markdown', |
||||
source: markdownCode |
||||
}); |
||||
} |
||||
|
||||
exports.highlight = (code, lang, callback) => { |
||||
resolver[++currentId] = callback; |
||||
rendererd.send({ |
||||
id: currentId, |
||||
type: 'highlight', |
||||
source: { |
||||
code, |
||||
lang |
||||
} |
||||
}); |
||||
} |
||||
const child_process = require('child_process'); |
||||
|
||||
const rendererd = child_process.fork(__dirname + '/rendererd', [syzoj.config.redis, parseInt(syzoj.config.renderer_cache_size)]); |
||||
|
||||
const resolver = {}; |
||||
let currentId = 0; |
||||
|
||||
rendererd.on('message', msg => { |
||||
resolver[msg.id](msg.result); |
||||
delete resolver[msg.id]; |
||||
}); |
||||
|
||||
exports.markdown = (markdownCode, callback) => { |
||||
resolver[++currentId] = callback; |
||||
rendererd.send({ |
||||
id: currentId, |
||||
type: 'markdown', |
||||
source: markdownCode |
||||
}); |
||||
} |
||||
|
||||
exports.highlight = (code, lang, callback) => { |
||||
resolver[++currentId] = callback; |
||||
rendererd.send({ |
||||
id: currentId, |
||||
type: 'highlight', |
||||
source: { |
||||
code, |
||||
lang |
||||
} |
||||
}); |
||||
} |
||||
|
@ -1,61 +1,63 @@
|
||||
const renderer = require('syzoj-renderer'); |
||||
const XSS = require('xss'); |
||||
const xssWhiteList = Object.assign({}, require('xss/lib/default').whiteList); |
||||
delete xssWhiteList.audio; |
||||
delete xssWhiteList.video; |
||||
|
||||
for (const tag in xssWhiteList) { |
||||
xssWhiteList[tag] = xssWhiteList[tag].concat(['style', 'class']); |
||||
} |
||||
|
||||
const xss = new XSS.FilterXSS({ |
||||
whiteList: xssWhiteList, |
||||
stripIgnoreTag: true, |
||||
onTagAttr: (tag, name, value, isWhiteAttr) => { |
||||
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) { |
||||
return name + '="' + XSS.escapeAttrValue(value) + '"'; |
||||
} |
||||
} |
||||
}); |
||||
|
||||
const Redis = require('redis'); |
||||
const util = require('util'); |
||||
const redis = Redis.createClient(process.argv[2]); |
||||
const redisCache = { |
||||
get: util.promisify(redis.get).bind(redis), |
||||
set: util.promisify(redis.set).bind(redis) |
||||
}; |
||||
|
||||
async function highlight(code, lang) { |
||||
return await renderer.highlight(code, lang, redisCache, { |
||||
wrapper: null |
||||
}); |
||||
} |
||||
|
||||
async function markdown(markdownCode) { |
||||
function filter(html) { |
||||
html = xss.process(html); |
||||
if (html) { |
||||
html = `<div style="position: relative; overflow: hidden; ">${html}</div>`; |
||||
} |
||||
return html; |
||||
}; |
||||
|
||||
return await renderer.markdown(markdownCode, redisCache, filter); |
||||
} |
||||
|
||||
process.on('message', async msg => { |
||||
if (msg.type === 'markdown') { |
||||
process.send({ |
||||
id: msg.id, |
||||
result: await markdown(msg.source) |
||||
}); |
||||
} else if (msg.type === 'highlight') { |
||||
process.send({ |
||||
id: msg.id, |
||||
result: await highlight(msg.source.code, msg.source.lang) |
||||
}); |
||||
} |
||||
}); |
||||
|
||||
process.on('disconnect', () => process.exit()); |
||||
const renderer = require('syzoj-renderer'); |
||||
const XSS = require('xss'); |
||||
const xssWhiteList = Object.assign({}, require('xss/lib/default').whiteList); |
||||
delete xssWhiteList.audio; |
||||
delete xssWhiteList.video; |
||||
|
||||
for (const tag in xssWhiteList) { |
||||
xssWhiteList[tag] = xssWhiteList[tag].concat(['style', 'class']); |
||||
} |
||||
|
||||
const xss = new XSS.FilterXSS({ |
||||
whiteList: xssWhiteList, |
||||
stripIgnoreTag: true, |
||||
onTagAttr: (tag, name, value, isWhiteAttr) => { |
||||
if (tag.toLowerCase() === 'img' && name.toLowerCase() === 'src' && value.startsWith('data:image/')) { |
||||
return name + '="' + XSS.escapeAttrValue(value) + '"'; |
||||
} |
||||
} |
||||
}); |
||||
|
||||
const Redis = require('redis'); |
||||
const RedisLRU = require('redis-lru'); |
||||
const util = require('util'); |
||||
const redis = Redis.createClient(process.argv[2]); |
||||
const redisLru = RedisLRU(redis, parseInt(process.argv[3])); |
||||
const redisCache = { |
||||
get: redisLru.get.bind(redisLru), |
||||
set: redisLru.set.bind(redisLru) |
||||
}; |
||||
|
||||
async function highlight(code, lang) { |
||||
return await renderer.highlight(code, lang, redisCache, { |
||||
wrapper: null |
||||
}); |
||||
} |
||||
|
||||
async function markdown(markdownCode) { |
||||
function filter(html) { |
||||
html = xss.process(html); |
||||
if (html) { |
||||
html = `<div style="position: relative; overflow: hidden; ">${html}</div>`; |
||||
} |
||||
return html; |
||||
}; |
||||
|
||||
return await renderer.markdown(markdownCode, redisCache, filter); |
||||
} |
||||
|
||||
process.on('message', async msg => { |
||||
if (msg.type === 'markdown') { |
||||
process.send({ |
||||
id: msg.id, |
||||
result: await markdown(msg.source) |
||||
}); |
||||
} else if (msg.type === 'highlight') { |
||||
process.send({ |
||||
id: msg.id, |
||||
result: await highlight(msg.source.code, msg.source.lang) |
||||
}); |
||||
} |
||||
}); |
||||
|
||||
process.on('disconnect', () => process.exit()); |
||||
|
Loading…
Reference in new issue