Transforming data
The Transform
class is used to create objects, known as transformers, that receive data from a Readable
stream, process it in some way, and then pass it on. Transformers are applied to streams with the pipe
method, as shown in Listing 6.17.
Listing 6.17: Creating a Transformer in the readHandler.ts File in the src Folder
import { IncomingMessage, ServerResponse } from "http";
import { Transform } from "stream";
export const readHandler = async (req: IncomingMessage, resp: ServerResponse) => {
req.pipe(createLowerTransform()).pipe(resp);
}
const createLowerTransform = () => new Transform({
transform(data, encoding, callback) {
callback(null, data.toString().toLowerCase());
}
});
The argument to the Transform
constructor is an object whose transform
property value is a function that will be invoked when there is data to process. The function receives three arguments: a chunk of data to process, which can...