compressing 1.10.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,6 +3,9 @@
3
3
  [![NPM version][npm-image]][npm-url]
4
4
  [![Test coverage][codecov-image]][codecov-url]
5
5
  [![npm download][download-image]][download-url]
6
+ [![Node.js Version](https://img.shields.io/node/v/compressing.svg?style=flat)](https://nodejs.org/en/download/)
7
+ [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
8
+ ![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/node-modules/compressing)
6
9
 
7
10
  [npm-image]: https://img.shields.io/npm/v/compressing.svg?style=flat-square
8
11
  [npm-url]: https://npmjs.org/package/compressing
@@ -72,7 +75,7 @@ fs.createReadStream('file/path/to/compress')
72
75
  .on('error', handleError);
73
76
 
74
77
  // You should take care of stream errors in caution, use pump to handle error in one place
75
- const pump = require('pump');
78
+ const { pipeline: pump } = require('stream');
76
79
  const sourceStream = fs.createReadStream('file/path/to/compress');
77
80
  const gzipStream = new compressing.gzip.FileStream();
78
81
  const destStream = fs.createWriteStream('path/to/destination.gz');
@@ -190,7 +193,7 @@ function onEntry(header, stream, next) => {
190
193
  if (header.type === 'file') {
191
194
  stream.pipe(fs.createWriteStream(path.join(destDir, header.name)));
192
195
  } else { // directory
193
- mkdirp(path.join(destDir, header.name), err => {
196
+ fs.mkdir(path.join(destDir, header.name), { recursive: true }, err => {
194
197
  if (err) return handleError(err);
195
198
  stream.resume();
196
199
  });
@@ -364,15 +367,9 @@ Due to the design of the .zip file format, it's impossible to interpret a .zip f
364
367
  Although the API is streaming style(try to keep it handy), it still loads all data into memory.
365
368
 
366
369
  <https://github.com/thejoshwolfe/yauzl#no-streaming-unzip-api>
367
- <!-- GITCONTRIBUTOR_START -->
368
370
 
369
371
  ## Contributors
370
372
 
371
- |[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/456108?v=4" width="100px;"/><br/><sub><b>shaoshuai0102</b></sub>](https://github.com/shaoshuai0102)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/9692408?v=4" width="100px;"/><br/><sub><b>DiamondYuan</b></sub>](https://github.com/DiamondYuan)<br/>|[<img src="https://avatars.githubusercontent.com/u/101238421?v=4" width="100px;"/><br/><sub><b>acyza</b></sub>](https://github.com/acyza)<br/>|
372
- | :---: | :---: | :---: | :---: | :---: | :---: |
373
- |[<img src="https://avatars.githubusercontent.com/u/13938334?v=4" width="100px;"/><br/><sub><b>bytemain</b></sub>](https://github.com/bytemain)<br/>|[<img src="https://avatars.githubusercontent.com/u/20432815?v=4" width="100px;"/><br/><sub><b>rickyes</b></sub>](https://github.com/rickyes)<br/>|[<img src="https://avatars.githubusercontent.com/u/8382136?v=4" width="100px;"/><br/><sub><b>Ryqsky</b></sub>](https://github.com/Ryqsky)<br/>|[<img src="https://avatars.githubusercontent.com/u/47357585?v=4" width="100px;"/><br/><sub><b>songhn233</b></sub>](https://github.com/songhn233)<br/>|[<img src="https://avatars.githubusercontent.com/u/160386?v=4" width="100px;"/><br/><sub><b>Infiltrator</b></sub>](https://github.com/Infiltrator)<br/>|[<img src="https://avatars.githubusercontent.com/u/13861843?v=4" width="100px;"/><br/><sub><b>ZeekoZhu</b></sub>](https://github.com/ZeekoZhu)<br/>|
374
- [<img src="https://avatars.githubusercontent.com/u/6897780?v=4" width="100px;"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src="https://avatars.githubusercontent.com/u/59508678?v=4" width="100px;"/><br/><sub><b>okaponta</b></sub>](https://github.com/okaponta)<br/>|[<img src="https://avatars.githubusercontent.com/u/9857273?v=4" width="100px;"/><br/><sub><b>ShadyZOZ</b></sub>](https://github.com/ShadyZOZ)<br/>
373
+ [![Contributors](https://contrib.rocks/image?repo=node-modules/compressing)](https://github.com/node-modules/compressing/graphs/contributors)
375
374
 
376
- This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Thu Aug 03 2023 01:39:37 GMT+0800`.
377
-
378
- <!-- GITCONTRIBUTOR_END -->
375
+ Made with [contributors-img](https://contrib.rocks).
@@ -4,7 +4,7 @@ const tar = require('../tar');
4
4
  const gzip = require('../gzip');
5
5
  const utils = require('../utils');
6
6
  const stream = require('stream');
7
- const pump = require('pump');
7
+ const { pipeline: pump } = require('stream');
8
8
  const ready = require('get-ready');
9
9
 
10
10
  class TgzFileStream extends stream.Transform {
package/lib/utils.js CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  const fs = require('fs');
4
4
  const path = require('path');
5
- const mkdirp = require('mkdirp');
6
- const pump = require('pump');
5
+ const { pipeline: pump } = require('stream');
7
6
 
8
7
  // file/fileBuffer/stream
9
8
  exports.sourceType = source => {
@@ -90,7 +89,7 @@ exports.makeUncompressFn = StreamClass => {
90
89
  }
91
90
 
92
91
  return new Promise((resolve, reject) => {
93
- mkdirp(destDir, err => {
92
+ fs.mkdir(destDir, { recursive: true }, err => {
94
93
  if (err) return reject(err);
95
94
 
96
95
  let entryCount = 0;
@@ -109,31 +108,37 @@ exports.makeUncompressFn = StreamClass => {
109
108
  .on('error', reject)
110
109
  .on('entry', (header, stream, next) => {
111
110
  stream.on('end', next);
111
+ const destFilePath = path.join(destDir, header.name);
112
112
 
113
113
  if (header.type === 'file') {
114
- const fullpath = path.join(destDir, header.name);
115
- mkdirp(path.dirname(fullpath), err => {
114
+ const dir = path.dirname(destFilePath);
115
+ fs.mkdir(dir, { recursive: true }, err => {
116
116
  if (err) return reject(err);
117
117
 
118
118
  entryCount++;
119
- pump(stream, fs.createWriteStream(fullpath, { mode: opts.mode || header.mode }), err => {
119
+ pump(stream, fs.createWriteStream(destFilePath, { mode: opts.mode || header.mode }), err => {
120
120
  if (err) return reject(err);
121
121
  successCount++;
122
122
  done();
123
123
  });
124
124
  });
125
125
  } else if (header.type === 'symlink') {
126
- // symlink
127
- const src = path.join(destDir, header.name);
128
- const target = path.resolve(path.dirname(src), header.linkname);
126
+ const dir = path.dirname(destFilePath);
127
+ const target = path.resolve(dir, header.linkname);
129
128
  entryCount++;
130
- fs.symlink(target, src, err => {
129
+
130
+ fs.mkdir(dir, { recursive: true }, err => {
131
131
  if (err) return reject(err);
132
- successCount++;
133
- stream.resume();
132
+
133
+ const relativeTarget = path.relative(dir, target);
134
+ fs.symlink(relativeTarget, destFilePath, err => {
135
+ if (err) return reject(err);
136
+ successCount++;
137
+ stream.resume();
138
+ });
134
139
  });
135
140
  } else { // directory
136
- mkdirp(path.join(destDir, header.name), err => {
141
+ fs.mkdir(destFilePath, { recursive: true }, err => {
137
142
  if (err) return reject(err);
138
143
  stream.resume();
139
144
  });
@@ -175,7 +180,7 @@ function normalizePath(fileName) {
175
180
  return fileName;
176
181
  }
177
182
 
178
- exports.stripFileName = (strip, fileName, type) => {
183
+ function stripFileName(strip, fileName, type) {
179
184
  // before
180
185
  // node/package.json
181
186
  // node/lib/index.js
@@ -218,4 +223,6 @@ exports.stripFileName = (strip, fileName, type) => {
218
223
 
219
224
  strip = Math.min(strip, s.length - 1);
220
225
  return s.slice(strip).join('/') || '/';
221
- };
226
+ }
227
+
228
+ exports.stripFileName = stripFileName;
@@ -2,6 +2,7 @@
2
2
 
3
3
  // https://github.com/thejoshwolfe/yauzl#no-streaming-unzip-api
4
4
 
5
+ const debug = require('util').debuglog('compressing/zip/uncompress_stream');
5
6
  const yauzl = require('@eggjs/yauzl');
6
7
  const stream = require('stream');
7
8
  const UncompressBaseStream = require('../base_write_stream');
@@ -38,12 +39,20 @@ class ZipUncompressStream extends UncompressBaseStream {
38
39
  if (this._zipFileNameEncoding === 'utf-8') {
39
40
  this._zipFileNameEncoding = 'utf8';
40
41
  }
42
+ this._finalCallback = err => {
43
+ if (err) {
44
+ debug('finalCallback, error: %j', err);
45
+ return this.emit('error', err);
46
+ }
47
+ this.emit('finish');
48
+ };
41
49
 
42
50
  this[YAUZL_CALLBACK] = this[YAUZL_CALLBACK].bind(this);
43
51
 
44
52
  const sourceType = utils.sourceType(opts.source);
45
53
 
46
54
  const yauzlOpts = this._yauzlOpts = Object.assign({}, DEFAULTS, opts.yauzl);
55
+ debug('sourceType: %s, yauzlOpts: %j', sourceType, yauzlOpts);
47
56
  if (sourceType === 'file') {
48
57
  yauzl.open(opts.source, yauzlOpts, this[YAUZL_CALLBACK]);
49
58
  return;
@@ -60,27 +69,26 @@ class ZipUncompressStream extends UncompressBaseStream {
60
69
  .catch(e => this.emit('error', e));
61
70
  return;
62
71
  }
63
-
64
- this.on('pipe', srcStream => {
65
- srcStream.unpipe(srcStream);
66
-
67
- utils.streamToBuffer(srcStream)
68
- .then(buf => {
69
- this._chunks.push(buf);
70
- buf = Buffer.concat(this._chunks);
71
- yauzl.fromBuffer(buf, yauzlOpts, this[YAUZL_CALLBACK]);
72
- })
73
- .catch(e => this.emit('error', e));
74
- });
75
72
  }
76
73
 
77
- _write(chunk) {
78
- // push to _chunks array, this will only happen once, for stream will be unpiped.
74
+ _write(chunk, _encoding, callback) {
79
75
  this._chunks.push(chunk);
76
+ debug('write size: %d, chunks: %d', chunk.length, this._chunks.length);
77
+ callback();
78
+ }
79
+
80
+ _final(callback) {
81
+ const buf = Buffer.concat(this._chunks);
82
+ debug('final, buf size: %d, chunks: %d', buf.length, this._chunks.length);
83
+ this._finalCallback = callback;
84
+ yauzl.fromBuffer(buf, this._yauzlOpts, this[YAUZL_CALLBACK]);
80
85
  }
81
86
 
82
87
  [YAUZL_CALLBACK](err, zipFile) {
83
- if (err) return this.emit('error', err);
88
+ if (err) {
89
+ debug('yauzl error', err);
90
+ return this._finalCallback(err);
91
+ }
84
92
 
85
93
  zipFile.readEntry();
86
94
 
@@ -106,17 +114,22 @@ class ZipUncompressStream extends UncompressBaseStream {
106
114
 
107
115
  if (type === 'file') {
108
116
  zipFile.openReadStream(entry, (err, readStream) => {
109
- if (err) return this.emit('error', err);
117
+ if (err) {
118
+ debug('file, error: %j', err);
119
+ return this._finalCallback(err);
120
+ }
121
+ debug('file, header: %j', header);
110
122
  this.emit('entry', header, readStream, next);
111
123
  });
112
124
  } else { // directory
113
125
  const placeholder = new stream.Readable({ read() {} });
126
+ debug('directory, header: %j', header);
114
127
  this.emit('entry', header, placeholder, next);
115
128
  setImmediate(() => placeholder.emit('end'));
116
129
  }
117
130
  })
118
- .on('end', () => this.emit('finish'))
119
- .on('error', err => this.emit('error', err));
131
+ .on('end', () => this._finalCallback())
132
+ .on('error', err => this._finalCallback(err));
120
133
 
121
134
  function next() {
122
135
  zipFile.readEntry();
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "compressing",
3
- "version": "1.10.2",
3
+ "version": "2.0.0",
4
4
  "description": "Everything you need for compressing and uncompressing",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "contributor": "git-contributor",
8
7
  "test:ts": "tsc -p ./test/fixtures/types/tsconfig.json",
9
8
  "test:js": "egg-bin test --ts false",
10
9
  "test": "npm run test:js && npm run test:ts",
@@ -40,14 +39,12 @@
40
39
  },
41
40
  "homepage": "https://github.com/node-modules/compressing#readme",
42
41
  "dependencies": {
42
+ "@eggjs/yauzl": "^2.11.0",
43
43
  "flushwritable": "^1.0.0",
44
44
  "get-ready": "^1.0.0",
45
45
  "iconv-lite": "^0.5.0",
46
- "mkdirp": "^0.5.1",
47
- "pump": "^3.0.0",
48
46
  "streamifier": "^0.1.1",
49
47
  "tar-stream": "^1.5.2",
50
- "@eggjs/yauzl": "^2.11.0",
51
48
  "yazl": "^2.4.2"
52
49
  },
53
50
  "devDependencies": {
@@ -57,13 +54,12 @@
57
54
  "egg-bin": "6",
58
55
  "eslint": "8",
59
56
  "eslint-config-egg": "12",
60
- "git-contributor": "2",
61
57
  "mm": "^2.0.0",
62
58
  "mz-modules": "^2.1.0",
63
59
  "typescript": "5",
64
60
  "uuid": "^3.0.1"
65
61
  },
66
62
  "engines": {
67
- "node": ">= 4.0.0"
63
+ "node": ">= 18.0.0"
68
64
  }
69
65
  }