stream-inject
v1.0.3
Published
Inject values into a NodeJS Stream
Downloads
32
Readme
stream-inject
A zero dependency, strictly typed Transform stream for mid-stream injections.
Supports replace by string and regular expression.
npm install stream-inject
Usage
import { Readable } from "stream";
import { StreamInjector } from "stream-inject";
const injectTransformer = new StreamInjector("consectetur", "replaced");
const readStream = new Readable({ read: () => undefined });
readStream.push("Lorem consectetur dolor sit amet, consectetur adipiscing elit.");
readStream.push(null);
readStream.pipe(injectTransformer).pipe(process.stdout);
// Lorem replaced dolor sit amet, replaced adipiscing elit.
More complicated stream replacement logic can be done by taking advantage stream pipes.
import { join } from "path";
import { createReadStream } from "fs";
import { createElement } from "react";
import { renderToNodeStream } from "react-dom/server";
import { StreamInjector } from "..";
// ...
const htmlPath = join(__dirname, "index.html");
const htmlStream = createReadStream(htmlPath, { encoding: "utf8" });
const reactStream = renderToNodeStream(createElement(MyReactComponent));
// express (req, res) => { ...
const testStream = htmlStream
.pipe(new StreamInjector("{body}", reactStream))
.pipe(new StreamInjector("{reduxStateCode}", (
"<script>window.__PRELOADED_REDUX_DATA__= " +
JSON.stringify({ a: 1 }).replace(/</g, "\\u003c") +
"</script>"
)))
.pipe(res);