Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow static config for stream subclasses #22958

Closed
ORESoftware opened this issue Sep 19, 2018 · 1 comment
Closed

Allow static config for stream subclasses #22958

ORESoftware opened this issue Sep 19, 2018 · 1 comment

Comments

@ORESoftware
Copy link
Contributor

ORESoftware commented Sep 19, 2018

Right now I have this:

export class JSONParser extends stream.Transform {

  lastLineData = '';

  constructor() {
      super({objectMode: true});
  }

  _transform(chunk: any, encoding: string, cb: Function) {

    let data = String(chunk);
    if (this.lastLineData) {
      data = this.lastLineData + data;
    }

    let lines = data.split('\n');
    this.lastLineData = lines.splice(lines.length - 1, 1)[0];

    lines.forEach(l => {
      try {
        // l might be an empty string; ignore if so
        l && this.push(JSON.parse(l));
      }
      catch (err) {
        // noop
      }
    });

    cb();

  }

  _flush(cb: Function) {
    if (this.lastLineData) {
      try {
        this.push(JSON.parse(this.lastLineData));
      }
      catch (err) {
        // noop
      }
    }
    this.lastLineData = '';
    cb();
  }

}

that seems to work, but what would be nice is something like this:

export class JSONParser extends stream.Transform {

  lastLineData = '';
  static options = {objectMode: true};   // <<< options are same for all instances

  constructor() {
    super();
  }

  _transform(chunk: any, encoding: string, cb: Function) {

    let data = String(chunk);
    if (this.lastLineData) {
      data = this.lastLineData + data;
    }

    let lines = data.split('\n');
    this.lastLineData = lines.splice(lines.length - 1, 1)[0];

    lines.forEach(l => {
      try {
        // l might be an empty string; ignore if so
        l && this.push(JSON.parse(l));
      }
      catch (err) {
        // noop
      }
    });

    cb();

  }

  _flush(cb: Function) {
    if (this.lastLineData) {
      try {
        this.push(JSON.parse(this.lastLineData));
      }
      catch (err) {
        // noop
      }
    }
    this.lastLineData = '';
    cb();
  }

}

it could potentially be more performant, but not that sure if it's worth it.

@apapirovski
Copy link
Member

Given the lack of input from others and the fact that this is quite awkward to make work ergonomically, I don't think this is something that will actually get implemented in Node.js. That said, if you do want to see something like this the best way is to just do it yourself and open a PR — see where that goes. :) Do feel free to reopen if you feel strongly about this though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants