Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Transform, TransformOptions } from "stream";
- import { types } from "util";
- export type Mapper = (
- this: Transform,
- chunk: any,
- encoding: string,
- callback?: (err?: any, data?: any) => void
- ) => any;
- export class Map extends Transform {
- private _map: Mapper;
- constructor(map: Mapper, options?: TransformOptions) {
- super(options);
- this._map = map;
- }
- _transform(chunk: Buffer | string, encoding: string, callback: (err?: any, data?: any) => void) {
- if (this._map.length >= 3) {
- const out = this._map.call(this, chunk, encoding, callback);
- if (types.isPromise(out)) {
- process.nextTick(() => {
- this.emit("error", new Error("Resolution method is overspeciied"));
- });
- }
- } else {
- const out = this._map.call(this, chunk, encoding);
- if (types.isPromise(out)) {
- out.then(data => callback(null, data)).catch(callback);
- } else {
- callback(null, out);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment