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

Cannot manually trigger the end of compression #44

Open
perfey opened this issue May 6, 2020 · 1 comment
Open

Cannot manually trigger the end of compression #44

perfey opened this issue May 6, 2020 · 1 comment

Comments

@perfey
Copy link

perfey commented May 6, 2020

My requirement is to provide a nodejs service, compress the network resources, and then provide it to the front-end page to download.
While downloading network resources become compressed, can provide a better user experience.
My code is like follows

const tarStream = new compressing.tar.Stream();
tarStream.addEntry(stream, {
  relativePath: fileName,
  size: contentLength, // from http.request response headers content-length
});

I need to request multiple resources. I must set size. But the size can only be obtained after the request returns. I can't wait all resources request returns, It will take too much time.
If when the first request returns. then I send the secound request and do addEntry after secound request returns. Compression will end early. The second resource will not be compressed.
I need to be able to actively control the end of compression.
And I tried to modify the source code as follows
/lib/tar/stream.js

constructor(opts) {
  super(opts);

  this._waitingEntries = [];
  this._processing = false;
  this._autoFinalize = false; // +
  this._init(opts);
}

_onEntryFinish(err) {
  if (err) return this.emit('error', err);

  this._processing = false;
  const waitingEntry = this._waitingEntries.shift();
  if (waitingEntry) {
    this.addEntry.apply(this, waitingEntry);
  } else {
    if (this._autoFinalize) {  // +
      this._finalize();
    }  // +
  }
}

finalize() {  // +
  this._autoFinalize = true;  // +
  if (!this._processing && this._waitingEntries.length === 0) {  // +
    this._finalize();  // +
  }  // +
}  // +

When I do addEntry all resource.

tarStream.finalize()

This way I can fulfill my needs.
It is recommended to modify the source code and add a manual trigger to complete the addition of all resources.

Thanks.

@lichenglong-blog
Copy link

How to stop decompressing zip?

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