@vltpkg/tar 1.8.0 → 1.8.2

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.
Files changed (2) hide show
  1. package/dist/unpack.js +27 -4
  2. package/package.json +4 -4
package/dist/unpack.js CHANGED
@@ -24,6 +24,17 @@ const parseMaxUnpackedBytes = (raw) => {
24
24
  };
25
25
  const maxUnpackedBytes = parseMaxUnpackedBytes(process.env.VLT_TAR_MAX_UNPACKED_BYTES);
26
26
  const unzipMax = (len) => Math.min(maxUnpackedBytes, len * MAX_DECOMPRESSION_RATIO);
27
+ // Inflate in one output chunk: zlib's default 16 KiB chunks each cost a
28
+ // threadpool round trip (async) or a loop pass (sync), plus a concat.
29
+ // The size is a guess, so a wrong one only costs extra chunks. zlib
30
+ // reserves the whole chunk up front, so it stays within `max` and a cap.
31
+ const minChunk = 64 * 1024;
32
+ const maxChunk = 256 * 1024 * 1024;
33
+ const chunkSize = (want, max) => Math.min(maxChunk, Math.max(minChunk, Math.min(want, max + 1)));
34
+ // the gzip trailer holds the inflated size (of the last member, mod
35
+ // 2^32); + 1 because zlib takes another pass when the chunk fills up
36
+ const gzipChunkSize = (input, max) => chunkSize((input.length < 4 ? 0 : input.readUInt32LE(input.length - 4)) + 1, max);
37
+ const brotliChunkSize = (input, max) => chunkSize(input.length * 8, max);
27
38
  const unzipError = (er, found, max) => er.code === 'ERR_BUFFER_TOO_LARGE' ?
28
39
  error('tarball exceeds maximum unpacked size', {
29
40
  found,
@@ -33,12 +44,18 @@ const unzipError = (er, found, max) => er.code === 'ERR_BUFFER_TOO_LARGE' ?
33
44
  : er;
34
45
  const unzip = async (input) => {
35
46
  const max = unzipMax(input.length);
36
- return new Promise((res, rej) => unzipCB(input, { maxOutputLength: max }, (er, result) => er ? rej(unzipError(er, input.length, max)) : res(result)));
47
+ return new Promise((res, rej) => unzipCB(input, {
48
+ maxOutputLength: max,
49
+ chunkSize: gzipChunkSize(input, max),
50
+ }, (er, result) => er ? rej(unzipError(er, input.length, max)) : res(result)));
37
51
  };
38
52
  const unzipSync = (input) => {
39
53
  const max = unzipMax(input.length);
40
54
  try {
41
- return unzipSyncCB(input, { maxOutputLength: max });
55
+ return unzipSyncCB(input, {
56
+ maxOutputLength: max,
57
+ chunkSize: gzipChunkSize(input, max),
58
+ });
42
59
  }
43
60
  catch (er) {
44
61
  throw unzipError(er, input.length, max);
@@ -47,12 +64,18 @@ const unzipSync = (input) => {
47
64
  const isGzip = (b) => b[0] === 0x1f && b[1] === 0x8b;
48
65
  const brotli = async (input) => {
49
66
  const max = unzipMax(input.length);
50
- return new Promise((res, rej) => brotliCB(input, { maxOutputLength: max }, (er, result) => er ? rej(unzipError(er, input.length, max)) : res(result)));
67
+ return new Promise((res, rej) => brotliCB(input, {
68
+ maxOutputLength: max,
69
+ chunkSize: brotliChunkSize(input, max),
70
+ }, (er, result) => er ? rej(unzipError(er, input.length, max)) : res(result)));
51
71
  };
52
72
  const brotliSync = (input) => {
53
73
  const max = unzipMax(input.length);
54
74
  try {
55
- return brotliSyncCB(input, { maxOutputLength: max });
75
+ return brotliSyncCB(input, {
76
+ maxOutputLength: max,
77
+ chunkSize: brotliChunkSize(input, max),
78
+ });
56
79
  }
57
80
  catch (er) {
58
81
  throw unzipError(er, input.length, max);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/tar",
3
3
  "description": "An extremely limited and very fast tar extractor",
4
- "version": "1.8.0",
4
+ "version": "1.8.2",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -13,9 +13,9 @@
13
13
  "url": "http://vlt.sh"
14
14
  },
15
15
  "dependencies": {
16
- "@vltpkg/error-cause": "1.8.0",
17
- "@vltpkg/output": "1.8.0",
18
- "@vltpkg/types": "1.8.0",
16
+ "@vltpkg/error-cause": "1.8.2",
17
+ "@vltpkg/output": "1.8.2",
18
+ "@vltpkg/types": "1.8.2",
19
19
  "rimraf": "^6.1.2",
20
20
  "tar": "^7.5.2"
21
21
  },