Guest User

Untitled

a guest
Feb 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import { Transform, TransformOptions } from "stream";
  2. import { types } from "util";
  3.  
  4. export type Mapper = (
  5. this: Transform,
  6. chunk: any,
  7. encoding: string,
  8. callback?: (err?: any, data?: any) => void
  9. ) => any;
  10.  
  11. export class Map extends Transform {
  12. private _map: Mapper;
  13.  
  14. constructor(map: Mapper, options?: TransformOptions) {
  15. super(options);
  16. this._map = map;
  17. }
  18.  
  19. _transform(chunk: Buffer | string, encoding: string, callback: (err?: any, data?: any) => void) {
  20. if (this._map.length >= 3) {
  21. const out = this._map.call(this, chunk, encoding, callback);
  22.  
  23. if (types.isPromise(out)) {
  24. process.nextTick(() => {
  25. this.emit("error", new Error("Resolution method is overspeciied"));
  26. });
  27. }
  28. } else {
  29. const out = this._map.call(this, chunk, encoding);
  30.  
  31. if (types.isPromise(out)) {
  32. out.then(data => callback(null, data)).catch(callback);
  33. } else {
  34. callback(null, out);
  35. }
  36. }
  37. }
  38. }
Add Comment
Please, Sign In to add comment