Skip to content

Instantly share code, notes, and snippets.

@kephin
Created October 8, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kephin/fbb2cb06241128c18277cd442fb98882 to your computer and use it in GitHub Desktop.
Save kephin/fbb2cb06241128c18277cd442fb98882 to your computer and use it in GitHub Desktop.
replace string through stream
// 1
let body = []
proxyRes.on('data', chunk => {
body.push(chunk)
})
proxyRes.on('end', () => {
body = Buffer.concat(body).toString()
.replace(/\/dist\/rsa-ui/g, `${MLISA_BASE_URL_PREFIX}/dist/rsa-ui`)
isTextFile(req)
? res.end(body)
: res.end()
})
// 2
if (isTextFile(req)) {
proxyRes
.pipe(replace(/\/dist\/rsa-ui/g, `${MLISA_BASE_URL_PREFIX}/dist/rsa-ui`))
.pipe(res)
} else {
proxyRes.on('data', () => {})
proxyRes.on('end', () => res.end())
}
// 3
const replacer = replace(/\/dist\/rsa-ui/g, `${MLISA_BASE_URL_PREFIX}/dist/rsa-ui`)
replacer.pipe(res)
proxyRes.on('data', data => {
if (isTextFile(req)) replacer.write(data)
else replacer.write('')
})
proxyRes.on('end', () => replacer.end())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment