Files
meteor/tools/utils/eachline.ts
Michael Newman 8af53f97d6 Convert tools/utils/eachline.js to TypeScript (#10614)
Thanks to @menewman for tackling this small but important module!
2019-07-15 11:34:45 -04:00

35 lines
749 B
TypeScript

const split = require("split2");
const pipe = require("multipipe");
import { Transform, Stream } from "stream";
type LineTransformer = (line: string) => string | Promise<string>
export function eachline(stream: Stream, callback: LineTransformer) {
stream.pipe(transform(callback));
}
export function transform(callback: LineTransformer) {
const splitStream = split(/\r?\n/, null, {
trailing: false
});
const transform = new Transform();
transform._transform = async function (chunk, _encoding, done) {
let line = chunk.toString("utf8");
try {
line = await callback(line);
} catch (error) {
done(error);
return;
}
done(null, line);
};
return pipe(
splitStream,
transform,
);
}