Created
October 8, 2019 10:59
-
-
Save kephin/fbb2cb06241128c18277cd442fb98882 to your computer and use it in GitHub Desktop.
replace string through stream
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
// 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