node-modules / compressing

Everything you need for compressing and uncompressing
MIT License
431 stars 36 forks source link

Cannot manually trigger the end of compression #44

Open perfey opened 4 years ago

perfey commented 4 years ago

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 commented 4 years ago

How to stop decompressing zip?