tar-iterator 3.0.0 → 3.0.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.
- package/dist/cjs/TarIterator.js +8 -6
- package/dist/cjs/TarIterator.js.map +1 -1
- package/dist/cjs/lib/Lock.d.cts +4 -0
- package/dist/cjs/lib/Lock.d.ts +4 -0
- package/dist/cjs/lib/Lock.js +14 -0
- package/dist/cjs/lib/Lock.js.map +1 -1
- package/dist/cjs/nextEntry.js +10 -1
- package/dist/cjs/nextEntry.js.map +1 -1
- package/dist/cjs/tar/sparse.d.cts +1 -1
- package/dist/cjs/tar/sparse.d.ts +1 -1
- package/dist/cjs/tar/sparse.js +6 -2
- package/dist/cjs/tar/sparse.js.map +1 -1
- package/dist/esm/TarIterator.js +8 -6
- package/dist/esm/TarIterator.js.map +1 -1
- package/dist/esm/lib/Lock.d.ts +4 -0
- package/dist/esm/lib/Lock.js +14 -0
- package/dist/esm/lib/Lock.js.map +1 -1
- package/dist/esm/nextEntry.js +10 -1
- package/dist/esm/nextEntry.js.map +1 -1
- package/dist/esm/tar/sparse.d.ts +1 -1
- package/dist/esm/tar/sparse.js +6 -2
- package/dist/esm/tar/sparse.js.map +1 -1
- package/package.json +1 -1
package/dist/cjs/TarIterator.js
CHANGED
|
@@ -99,12 +99,15 @@ var TarIterator = /*#__PURE__*/ function(BaseIterator) {
|
|
|
99
99
|
// Note: options passed here are ExtractOptions (strip, force, etc.)
|
|
100
100
|
// TarExtract uses TarExtractOptions (filenameEncoding, etc.) which is different
|
|
101
101
|
_this.extract = new _TarExtractts.default();
|
|
102
|
+
_this.lock.extract = _this.extract;
|
|
102
103
|
var pipe = function(cb) {
|
|
103
104
|
try {
|
|
104
105
|
if (typeof source === 'string') source = _fs.default.createReadStream(source);
|
|
105
106
|
} catch (err) {
|
|
106
107
|
cb(err);
|
|
107
108
|
}
|
|
109
|
+
// Store source stream in lock for cleanup
|
|
110
|
+
_this.lock.sourceStream = source;
|
|
108
111
|
var end = (0, _calloncefn.default)(cb);
|
|
109
112
|
var self = _this;
|
|
110
113
|
source.on('data', function onData(chunk) {
|
|
@@ -132,12 +135,11 @@ var TarIterator = /*#__PURE__*/ function(BaseIterator) {
|
|
|
132
135
|
}
|
|
133
136
|
var _proto = TarIterator.prototype;
|
|
134
137
|
_proto.end = function end(err) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
this.lock
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
_extractbaseiterator.default.prototype.end.call(this, err); // call in lock release so end is properly handled
|
|
138
|
+
var lock = this.lock;
|
|
139
|
+
if (lock) {
|
|
140
|
+
this.lock = null; // Clear FIRST to prevent re-entrancy
|
|
141
|
+
lock.err = err;
|
|
142
|
+
lock.release(); // Lock.__destroy() handles BaseIterator.end()
|
|
141
143
|
}
|
|
142
144
|
this.extract = null;
|
|
143
145
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/TarIterator.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport BaseIterator from 'extract-base-iterator';\nimport fs from 'fs';\n\nimport Lock from './lib/Lock.ts';\nimport nextEntry from './nextEntry.ts';\nimport TarExtract from './tar/TarExtract.ts';\n\nimport type { ExtractOptions } from './types.ts';\n\nexport default class TarIterator extends BaseIterator {\n /** @internal @hidden */\n lock: Lock;\n /** @internal @hidden */\n extract: TarExtract;\n\n constructor(source: string | NodeJS.ReadableStream, options: ExtractOptions = {}) {\n super(options);\n this.lock = new Lock();\n this.lock.iterator = this;\n\n let cancelled = false;\n const setup = (): undefined => {\n cancelled = true;\n };\n this.processing.push(setup);\n\n // Use our pure TarExtract instead of tar-stream-compat\n // Note: options passed here are ExtractOptions (strip, force, etc.)\n // TarExtract uses TarExtractOptions (filenameEncoding, etc.) which is different\n this.extract = new TarExtract();\n\n const pipe = (cb) => {\n try {\n if (typeof source === 'string') source = fs.createReadStream(source);\n } catch (err) {\n cb(err);\n }\n\n const end = once(cb);\n const self = this;\n (source as NodeJS.ReadableStream).on('data', function onData(chunk) {\n try {\n if (self.extract) self.extract.write(chunk);\n } catch (err) {\n // Handle synchronous errors from TarExtract (e.g., invalid format)\n end(err);\n }\n });\n (source as NodeJS.ReadableStream).on('data', function onFirstData() {\n end();\n });\n (source as NodeJS.ReadableStream).on('error', end);\n (source as NodeJS.ReadableStream).on('end', function onEnd() {\n if (self.extract) self.extract.end();\n });\n };\n pipe((err) => {\n this.processing.remove(setup);\n if (this.done || cancelled) return; // done\n err ? this.end(err) : this.push(nextEntry.bind(null, null));\n });\n }\n\n end(err?: Error) {\n
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/TarIterator.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport BaseIterator from 'extract-base-iterator';\nimport fs from 'fs';\n\nimport Lock from './lib/Lock.ts';\nimport nextEntry from './nextEntry.ts';\nimport TarExtract from './tar/TarExtract.ts';\n\nimport type { ExtractOptions } from './types.ts';\n\nexport default class TarIterator extends BaseIterator {\n /** @internal @hidden */\n lock: Lock;\n /** @internal @hidden */\n extract: TarExtract;\n\n constructor(source: string | NodeJS.ReadableStream, options: ExtractOptions = {}) {\n super(options);\n this.lock = new Lock();\n this.lock.iterator = this;\n\n let cancelled = false;\n const setup = (): undefined => {\n cancelled = true;\n };\n this.processing.push(setup);\n\n // Use our pure TarExtract instead of tar-stream-compat\n // Note: options passed here are ExtractOptions (strip, force, etc.)\n // TarExtract uses TarExtractOptions (filenameEncoding, etc.) which is different\n this.extract = new TarExtract();\n this.lock.extract = this.extract;\n\n const pipe = (cb) => {\n try {\n if (typeof source === 'string') source = fs.createReadStream(source);\n } catch (err) {\n cb(err);\n }\n\n // Store source stream in lock for cleanup\n this.lock.sourceStream = source as NodeJS.ReadableStream;\n\n const end = once(cb);\n const self = this;\n (source as NodeJS.ReadableStream).on('data', function onData(chunk) {\n try {\n if (self.extract) self.extract.write(chunk);\n } catch (err) {\n // Handle synchronous errors from TarExtract (e.g., invalid format)\n end(err);\n }\n });\n (source as NodeJS.ReadableStream).on('data', function onFirstData() {\n end();\n });\n (source as NodeJS.ReadableStream).on('error', end);\n (source as NodeJS.ReadableStream).on('end', function onEnd() {\n if (self.extract) self.extract.end();\n });\n };\n pipe((err) => {\n this.processing.remove(setup);\n if (this.done || cancelled) return; // done\n err ? this.end(err) : this.push(nextEntry.bind(null, null));\n });\n }\n\n end(err?: Error) {\n const lock = this.lock;\n if (lock) {\n this.lock = null; // Clear FIRST to prevent re-entrancy\n lock.err = err;\n lock.release(); // Lock.__destroy() handles BaseIterator.end()\n }\n this.extract = null;\n }\n}\n"],"names":["TarIterator","source","options","lock","Lock","iterator","cancelled","setup","processing","push","extract","TarExtract","pipe","cb","fs","createReadStream","err","sourceStream","end","once","self","on","onData","chunk","write","onFirstData","onEnd","remove","done","nextEntry","bind","release","BaseIterator"],"mappings":";;;;;;;eAUqBA;;;iEAVJ;0EACQ;yDACV;6DAEE;kEACK;mEACC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIR,IAAA,AAAMA,4BAAN;;cAAMA;aAAAA,YAMPC,MAAsC;YAAEC,UAAAA,iEAA0B,CAAC;gCAN5DF;;gBAOjB,kBAPiBA;YAOXE;;QACN,MAAKC,IAAI,GAAG,IAAIC,eAAI;QACpB,MAAKD,IAAI,CAACE,QAAQ;QAElB,IAAIC,YAAY;QAChB,IAAMC,QAAQ;YACZD,YAAY;QACd;QACA,MAAKE,UAAU,CAACC,IAAI,CAACF;QAErB,uDAAuD;QACvD,oEAAoE;QACpE,gFAAgF;QAChF,MAAKG,OAAO,GAAG,IAAIC,qBAAU;QAC7B,MAAKR,IAAI,CAACO,OAAO,GAAG,MAAKA,OAAO;QAEhC,IAAME,OAAO,SAACC;YACZ,IAAI;gBACF,IAAI,OAAOZ,WAAW,UAAUA,SAASa,WAAE,CAACC,gBAAgB,CAACd;YAC/D,EAAE,OAAOe,KAAK;gBACZH,GAAGG;YACL;YAEA,0CAA0C;YAC1C,MAAKb,IAAI,CAACc,YAAY,GAAGhB;YAEzB,IAAMiB,MAAMC,IAAAA,mBAAI,EAACN;YACjB,IAAMO;YACLnB,OAAiCoB,EAAE,CAAC,QAAQ,SAASC,OAAOC,KAAK;gBAChE,IAAI;oBACF,IAAIH,KAAKV,OAAO,EAAEU,KAAKV,OAAO,CAACc,KAAK,CAACD;gBACvC,EAAE,OAAOP,KAAK;oBACZ,mEAAmE;oBACnEE,IAAIF;gBACN;YACF;YACCf,OAAiCoB,EAAE,CAAC,QAAQ,SAASI;gBACpDP;YACF;YACCjB,OAAiCoB,EAAE,CAAC,SAASH;YAC7CjB,OAAiCoB,EAAE,CAAC,OAAO,SAASK;gBACnD,IAAIN,KAAKV,OAAO,EAAEU,KAAKV,OAAO,CAACQ,GAAG;YACpC;QACF;QACAN,KAAK,SAACI;YACJ,MAAKR,UAAU,CAACmB,MAAM,CAACpB;YACvB,IAAI,MAAKqB,IAAI,IAAItB,WAAW,QAAQ,OAAO;YAC3CU,MAAM,MAAKE,GAAG,CAACF,OAAO,MAAKP,IAAI,CAACoB,oBAAS,CAACC,IAAI,CAAC,MAAM;QACvD;;;iBAvDiB9B;IA0DnBkB,OAAAA,GAQC,GARDA,SAAAA,IAAIF,GAAW;QACb,IAAMb,OAAO,IAAI,CAACA,IAAI;QACtB,IAAIA,MAAM;YACR,IAAI,CAACA,IAAI,GAAG,MAAM,qCAAqC;YACvDA,KAAKa,GAAG,GAAGA;YACXb,KAAK4B,OAAO,IAAI,8CAA8C;QAChE;QACA,IAAI,CAACrB,OAAO,GAAG;IACjB;WAlEmBV;EAAoBgC,4BAAY"}
|
package/dist/cjs/lib/Lock.d.cts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import BaseIterator from 'extract-base-iterator';
|
|
2
|
+
import type TarExtract from '../tar/TarExtract.js';
|
|
2
3
|
export default class Lock {
|
|
3
4
|
private count;
|
|
4
5
|
iterator: BaseIterator;
|
|
5
6
|
err: Error;
|
|
7
|
+
/** @internal @hidden */
|
|
8
|
+
extract: TarExtract;
|
|
9
|
+
sourceStream: NodeJS.ReadableStream;
|
|
6
10
|
retain(): void;
|
|
7
11
|
release(): void;
|
|
8
12
|
private __destroy;
|
package/dist/cjs/lib/Lock.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import BaseIterator from 'extract-base-iterator';
|
|
2
|
+
import type TarExtract from '../tar/TarExtract.js';
|
|
2
3
|
export default class Lock {
|
|
3
4
|
private count;
|
|
4
5
|
iterator: BaseIterator;
|
|
5
6
|
err: Error;
|
|
7
|
+
/** @internal @hidden */
|
|
8
|
+
extract: TarExtract;
|
|
9
|
+
sourceStream: NodeJS.ReadableStream;
|
|
6
10
|
retain(): void;
|
|
7
11
|
release(): void;
|
|
8
12
|
private __destroy;
|
package/dist/cjs/lib/Lock.js
CHANGED
|
@@ -27,6 +27,9 @@ var Lock = /*#__PURE__*/ function() {
|
|
|
27
27
|
// members
|
|
28
28
|
this.iterator = null;
|
|
29
29
|
this.err = null;
|
|
30
|
+
// cleanup resources
|
|
31
|
+
/** @internal @hidden */ this.extract = null;
|
|
32
|
+
this.sourceStream = null;
|
|
30
33
|
}
|
|
31
34
|
var _proto = Lock.prototype;
|
|
32
35
|
_proto.retain = function retain() {
|
|
@@ -38,6 +41,17 @@ var Lock = /*#__PURE__*/ function() {
|
|
|
38
41
|
if (this.count === 0) this.__destroy();
|
|
39
42
|
};
|
|
40
43
|
_proto.__destroy = function __destroy() {
|
|
44
|
+
// Destroy source stream FIRST to stop data flow
|
|
45
|
+
if (this.sourceStream) {
|
|
46
|
+
var stream = this.sourceStream;
|
|
47
|
+
if (typeof stream.destroy === 'function') stream.destroy();
|
|
48
|
+
this.sourceStream = null;
|
|
49
|
+
}
|
|
50
|
+
// Clear extract reference
|
|
51
|
+
if (this.extract) {
|
|
52
|
+
this.extract = null;
|
|
53
|
+
}
|
|
54
|
+
// Call BaseIterator.end() LAST
|
|
41
55
|
if (this.iterator) {
|
|
42
56
|
_extractbaseiterator.default.prototype.end.call(this.iterator, this.err || null);
|
|
43
57
|
this.iterator = null;
|
package/dist/cjs/lib/Lock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/lib/Lock.ts"],"sourcesContent":["import BaseIterator from 'extract-base-iterator';\n\nexport default class Lock {\n private count = 1;\n\n // members\n iterator: BaseIterator = null;\n err: Error = null;\n\n retain() {\n this.count++;\n }\n\n release() {\n if (this.count <= 0) throw new Error('Lock count is corrupted');\n this.count--;\n if (this.count === 0) this.__destroy();\n }\n\n private __destroy() {\n if (this.iterator) {\n BaseIterator.prototype.end.call(this.iterator, this.err || null);\n this.iterator = null;\n }\n }\n}\n"],"names":["Lock","count","iterator","err","retain","release","Error","__destroy","BaseIterator","prototype","end","call"],"mappings":";;;;;;;
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/lib/Lock.ts"],"sourcesContent":["import BaseIterator from 'extract-base-iterator';\nimport type TarExtract from '../tar/TarExtract.ts';\n\nexport default class Lock {\n private count = 1;\n\n // members\n iterator: BaseIterator = null;\n err: Error = null;\n\n // cleanup resources\n /** @internal @hidden */\n extract: TarExtract = null;\n sourceStream: NodeJS.ReadableStream = null;\n\n retain() {\n this.count++;\n }\n\n release() {\n if (this.count <= 0) throw new Error('Lock count is corrupted');\n this.count--;\n if (this.count === 0) this.__destroy();\n }\n\n private __destroy() {\n // Destroy source stream FIRST to stop data flow\n if (this.sourceStream) {\n const stream = this.sourceStream as NodeJS.ReadableStream & { destroy?: () => void };\n if (typeof stream.destroy === 'function') stream.destroy();\n this.sourceStream = null;\n }\n\n // Clear extract reference\n if (this.extract) {\n this.extract = null;\n }\n\n // Call BaseIterator.end() LAST\n if (this.iterator) {\n BaseIterator.prototype.end.call(this.iterator, this.err || null);\n this.iterator = null;\n }\n }\n}\n"],"names":["Lock","count","iterator","err","extract","sourceStream","retain","release","Error","__destroy","stream","destroy","BaseIterator","prototype","end","call"],"mappings":";;;;;;;eAGqBA;;;0EAHI;;;;;;;;;;;AAGV,IAAA,AAAMA,qBAAN;;aAAMA;gCAAAA;aACXC,QAAQ;QAEhB,UAAU;aACVC,WAAyB;aACzBC,MAAa;QAEb,oBAAoB;QACpB,sBAAsB,QACtBC,UAAsB;aACtBC,eAAsC;;iBAVnBL;IAYnBM,OAAAA,MAEC,GAFDA,SAAAA;QACE,IAAI,CAACL,KAAK;IACZ;IAEAM,OAAAA,OAIC,GAJDA,SAAAA;QACE,IAAI,IAAI,CAACN,KAAK,IAAI,GAAG,MAAM,IAAIO,MAAM;QACrC,IAAI,CAACP,KAAK;QACV,IAAI,IAAI,CAACA,KAAK,KAAK,GAAG,IAAI,CAACQ,SAAS;IACtC;IAEA,OAAQA,SAkBP,GAlBD,SAAQA;QACN,gDAAgD;QAChD,IAAI,IAAI,CAACJ,YAAY,EAAE;YACrB,IAAMK,SAAS,IAAI,CAACL,YAAY;YAChC,IAAI,OAAOK,OAAOC,OAAO,KAAK,YAAYD,OAAOC,OAAO;YACxD,IAAI,CAACN,YAAY,GAAG;QACtB;QAEA,0BAA0B;QAC1B,IAAI,IAAI,CAACD,OAAO,EAAE;YAChB,IAAI,CAACA,OAAO,GAAG;QACjB;QAEA,+BAA+B;QAC/B,IAAI,IAAI,CAACF,QAAQ,EAAE;YACjBU,4BAAY,CAACC,SAAS,CAACC,GAAG,CAACC,IAAI,CAAC,IAAI,CAACb,QAAQ,EAAE,IAAI,CAACC,GAAG,IAAI;YAC3D,IAAI,CAACD,QAAQ,GAAG;QAClB;IACF;WAxCmBF"}
|
package/dist/cjs/nextEntry.js
CHANGED
|
@@ -50,6 +50,13 @@ var _setImmediate = typeof setImmediate === 'function' ? setImmediate : typeof p
|
|
|
50
50
|
setTimeout(fn, 0);
|
|
51
51
|
};
|
|
52
52
|
function nextEntry(next, iterator, callback) {
|
|
53
|
+
// Guard: bail early if iterator already ended
|
|
54
|
+
if (!iterator.lock || iterator.isDone()) {
|
|
55
|
+
return callback(null, {
|
|
56
|
+
done: true,
|
|
57
|
+
value: null
|
|
58
|
+
});
|
|
59
|
+
}
|
|
53
60
|
var extract = iterator.extract;
|
|
54
61
|
if (!extract) return callback(new Error('Extract missing'));
|
|
55
62
|
var nextCallback = (0, _calloncefn.default)(function(err, entry, next) {
|
|
@@ -73,7 +80,9 @@ function nextEntry(next, iterator, callback) {
|
|
|
73
80
|
var onError = callback;
|
|
74
81
|
var onEnd = callback.bind(null, null);
|
|
75
82
|
var onEntry = function onEntry(header, stream, next) {
|
|
76
|
-
if
|
|
83
|
+
// Guard: skip if iterator already ended (stale lock)
|
|
84
|
+
if (!iterator.lock || iterator.isDone()) {
|
|
85
|
+
stream.resume(); // drain stream
|
|
77
86
|
return nextCallback(null, null, next);
|
|
78
87
|
}
|
|
79
88
|
var attributes = _object_spread({}, header);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/nextEntry.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport { DirectoryEntry, LinkEntry, SymbolicLinkEntry } from 'extract-base-iterator';\nimport path from 'path';\nimport FileEntry from './FileEntry.ts';\nimport type Iterator from './TarIterator.ts';\n\nimport type { Entry, EntryCallback } from './types.ts';\n\n// Node 0.8 compatible setImmediate (setImmediate was added in Node 0.10)\nconst _setImmediate =\n typeof setImmediate === 'function'\n ? setImmediate\n : typeof process !== 'undefined' && typeof process.nextTick === 'function'\n ? process.nextTick\n : function setImmediateFallback(fn: () => void) {\n setTimeout(fn, 0);\n };\n\nexport type TarNext = () => undefined;\nexport type NextCallback = (error?: Error, entry?: Entry, next?: TarNext) => undefined;\n\nexport default function nextEntry(next: TarNext, iterator: Iterator, callback: EntryCallback): undefined {\n const extract = iterator.extract;\n if (!extract) return callback(new Error('Extract missing'));\n\n const nextCallback = once((err?: Error, entry?: Entry, next?: TarNext) => {\n extract.removeListener('entry', onEntry);\n extract.removeListener('error', onError);\n extract.removeListener('finish', onEnd);\n\n // keep processing\n if (entry) iterator.push(nextEntry.bind(null, next));\n\n // Use setImmediate to defer the callback invocation\n // This ensures proper async behavior with the BaseIterator\n _setImmediate(() => {\n err ? callback(err) : callback(null, entry ? { done: false, value: entry } : { done: true, value: null });\n });\n }) as NextCallback;\n\n const onError = callback;\n const onEnd = callback.bind(null, null);\n const onEntry = function onEntry(header, stream, next: TarNext) {\n if (iterator.isDone()) {\n return nextCallback(null, null, next);\n }\n\n const attributes = { ...header };\n attributes.path = header.name.split(path.sep).filter(Boolean).join(path.sep);\n attributes.mtime = new Date(attributes.mtime);\n\n switch (attributes.type) {\n case 'directory':\n stream.resume(); // drain stream\n return nextCallback(null, new DirectoryEntry(attributes), next);\n case 'symlink':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new SymbolicLinkEntry(attributes), next);\n case 'link':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new LinkEntry(attributes), next);\n case 'file':\n return nextCallback(null, new FileEntry(attributes, stream, iterator.lock), next);\n }\n\n stream.resume(); // drain stream\n return nextCallback(new Error(`Unrecognized entry type: ${attributes.type}`), null, next);\n };\n\n extract.on('entry', onEntry);\n extract.on('error', onError);\n extract.on('finish', onEnd);\n\n // Resume parsing to emit any pending entry\n // For first call (next is null), this triggers the first entry emission\n // For subsequent calls, next() unlocks the parser which then processes the next header\n if (next) {\n next();\n } else {\n // First call - resume to emit any pending entry\n extract.resume();\n }\n}\n"],"names":["nextEntry","_setImmediate","setImmediate","process","nextTick","setImmediateFallback","fn","setTimeout","next","iterator","callback","extract","Error","nextCallback","once","err","entry","removeListener","onEntry","onError","onEnd","push","bind","
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/nextEntry.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport { DirectoryEntry, LinkEntry, SymbolicLinkEntry } from 'extract-base-iterator';\nimport path from 'path';\nimport FileEntry from './FileEntry.ts';\nimport type Iterator from './TarIterator.ts';\n\nimport type { Entry, EntryCallback } from './types.ts';\n\n// Node 0.8 compatible setImmediate (setImmediate was added in Node 0.10)\nconst _setImmediate =\n typeof setImmediate === 'function'\n ? setImmediate\n : typeof process !== 'undefined' && typeof process.nextTick === 'function'\n ? process.nextTick\n : function setImmediateFallback(fn: () => void) {\n setTimeout(fn, 0);\n };\n\nexport type TarNext = () => undefined;\nexport type NextCallback = (error?: Error, entry?: Entry, next?: TarNext) => undefined;\n\nexport default function nextEntry(next: TarNext, iterator: Iterator, callback: EntryCallback): undefined {\n // Guard: bail early if iterator already ended\n if (!iterator.lock || iterator.isDone()) {\n return callback(null, { done: true, value: null });\n }\n\n const extract = iterator.extract;\n if (!extract) return callback(new Error('Extract missing'));\n\n const nextCallback = once((err?: Error, entry?: Entry, next?: TarNext) => {\n extract.removeListener('entry', onEntry);\n extract.removeListener('error', onError);\n extract.removeListener('finish', onEnd);\n\n // keep processing\n if (entry) iterator.push(nextEntry.bind(null, next));\n\n // Use setImmediate to defer the callback invocation\n // This ensures proper async behavior with the BaseIterator\n _setImmediate(() => {\n err ? callback(err) : callback(null, entry ? { done: false, value: entry } : { done: true, value: null });\n });\n }) as NextCallback;\n\n const onError = callback;\n const onEnd = callback.bind(null, null);\n const onEntry = function onEntry(header, stream, next: TarNext) {\n // Guard: skip if iterator already ended (stale lock)\n if (!iterator.lock || iterator.isDone()) {\n stream.resume(); // drain stream\n return nextCallback(null, null, next);\n }\n\n const attributes = { ...header };\n attributes.path = header.name.split(path.sep).filter(Boolean).join(path.sep);\n attributes.mtime = new Date(attributes.mtime);\n\n switch (attributes.type) {\n case 'directory':\n stream.resume(); // drain stream\n return nextCallback(null, new DirectoryEntry(attributes), next);\n case 'symlink':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new SymbolicLinkEntry(attributes), next);\n case 'link':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new LinkEntry(attributes), next);\n case 'file':\n return nextCallback(null, new FileEntry(attributes, stream, iterator.lock), next);\n }\n\n stream.resume(); // drain stream\n return nextCallback(new Error(`Unrecognized entry type: ${attributes.type}`), null, next);\n };\n\n extract.on('entry', onEntry);\n extract.on('error', onError);\n extract.on('finish', onEnd);\n\n // Resume parsing to emit any pending entry\n // For first call (next is null), this triggers the first entry emission\n // For subsequent calls, next() unlocks the parser which then processes the next header\n if (next) {\n next();\n } else {\n // First call - resume to emit any pending entry\n extract.resume();\n }\n}\n"],"names":["nextEntry","_setImmediate","setImmediate","process","nextTick","setImmediateFallback","fn","setTimeout","next","iterator","callback","lock","isDone","done","value","extract","Error","nextCallback","once","err","entry","removeListener","onEntry","onError","onEnd","push","bind","header","stream","resume","attributes","path","name","split","sep","filter","Boolean","join","mtime","Date","type","DirectoryEntry","linkpath","linkname","SymbolicLinkEntry","LinkEntry","FileEntry","on"],"mappings":";;;;+BAqBA;;;eAAwBA;;;iEArBP;mCAC4C;2DAC5C;kEACK;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKtB,yEAAyE;AACzE,IAAMC,gBACJ,OAAOC,iBAAiB,aACpBA,eACA,OAAOC,YAAY,eAAe,OAAOA,QAAQC,QAAQ,KAAK,aAC5DD,QAAQC,QAAQ,GAChB,SAASC,qBAAqBC,EAAc;IAC1CC,WAAWD,IAAI;AACjB;AAKO,SAASN,UAAUQ,IAAa,EAAEC,QAAkB,EAAEC,QAAuB;IAC1F,8CAA8C;IAC9C,IAAI,CAACD,SAASE,IAAI,IAAIF,SAASG,MAAM,IAAI;QACvC,OAAOF,SAAS,MAAM;YAAEG,MAAM;YAAMC,OAAO;QAAK;IAClD;IAEA,IAAMC,UAAUN,SAASM,OAAO;IAChC,IAAI,CAACA,SAAS,OAAOL,SAAS,IAAIM,MAAM;IAExC,IAA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,IAAI,eAAe;gBAChCC,WAAWY,QAAQ,GAAGf,OAAOgB,QAAQ;gBACrC,OAAO1B,aAAa,MAAM,IAAI4B,8BAAS,CAACf,aAAatB;YACvD,KAAK;gBACH,OAAOS,aAAa,MAAM,IAAI6B,oBAAS,CAAChB,YAAYF,QAAQnB,SAASE,IAAI,GAAGH;QAChF;QAEAoB,OAAOC,MAAM,IAAI,eAAe;QAChC,OAAOZ,aAAa,IAAID,MAAM,AAAC,4BAA2C,OAAhBc,WAAWU,IAAI,IAAK,MAAMhC;IACtF;IAEAO,QAAQgC,EAAE,CAAC,SAASzB;IACpBP,QAAQgC,EAAE,CAAC,SAASxB;IACpBR,QAAQgC,EAAE,CAAC,UAAUvB;IAErB,2CAA2C;IAC3C,wEAAwE;IACxE,uFAAuF;IACvF,IAAIhB,MAAM;QACRA;IACF,OAAO;QACL,gDAAgD;QAChDO,QAAQc,MAAM;IAChB;AACF"}
|
|
@@ -78,7 +78,7 @@ export declare class SparseStream extends EntryStream {
|
|
|
78
78
|
* Push data from the tar archive (actual sparse data chunk)
|
|
79
79
|
* Overrides EntryStream.push() to reconstruct sparse file with holes.
|
|
80
80
|
*/
|
|
81
|
-
push(data: Buffer):
|
|
81
|
+
push(data: Buffer | null): boolean;
|
|
82
82
|
/**
|
|
83
83
|
* End the stream - emit any trailing zeros
|
|
84
84
|
* Overrides EntryStream.end() to emit trailing zeros first.
|
package/dist/cjs/tar/sparse.d.ts
CHANGED
|
@@ -78,7 +78,7 @@ export declare class SparseStream extends EntryStream {
|
|
|
78
78
|
* Push data from the tar archive (actual sparse data chunk)
|
|
79
79
|
* Overrides EntryStream.push() to reconstruct sparse file with holes.
|
|
80
80
|
*/
|
|
81
|
-
push(data: Buffer):
|
|
81
|
+
push(data: Buffer | null): boolean;
|
|
82
82
|
/**
|
|
83
83
|
* End the stream - emit any trailing zeros
|
|
84
84
|
* Overrides EntryStream.end() to emit trailing zeros first.
|
package/dist/cjs/tar/sparse.js
CHANGED
|
@@ -212,8 +212,11 @@ var SparseStream = /*#__PURE__*/ function(EntryStream) {
|
|
|
212
212
|
* Push data from the tar archive (actual sparse data chunk)
|
|
213
213
|
* Overrides EntryStream.push() to reconstruct sparse file with holes.
|
|
214
214
|
*/ _proto.push = function push(data) {
|
|
215
|
-
|
|
215
|
+
// Allow null through to signal end
|
|
216
|
+
if (data === null) return _get(_get_prototype_of(SparseStream.prototype), "push", this).call(this, null);
|
|
217
|
+
if (this.ended) return false;
|
|
216
218
|
var dataOffset = 0;
|
|
219
|
+
var result = true;
|
|
217
220
|
while(dataOffset < data.length && this.currentEntry < this.entries.length){
|
|
218
221
|
var entry = this.entries[this.currentEntry];
|
|
219
222
|
// First, emit zeros for any hole before current entry
|
|
@@ -226,7 +229,7 @@ var SparseStream = /*#__PURE__*/ function(EntryStream) {
|
|
|
226
229
|
var toEmit = Math.min(this.entryBytesRemaining, data.length - dataOffset);
|
|
227
230
|
if (toEmit > 0) {
|
|
228
231
|
var chunk = data.slice(dataOffset, dataOffset + toEmit);
|
|
229
|
-
_get(_get_prototype_of(SparseStream.prototype), "push", this).call(this, chunk);
|
|
232
|
+
result = _get(_get_prototype_of(SparseStream.prototype), "push", this).call(this, chunk);
|
|
230
233
|
dataOffset += toEmit;
|
|
231
234
|
this.virtualPosition += toEmit;
|
|
232
235
|
this.entryBytesRemaining -= toEmit;
|
|
@@ -239,6 +242,7 @@ var SparseStream = /*#__PURE__*/ function(EntryStream) {
|
|
|
239
242
|
}
|
|
240
243
|
}
|
|
241
244
|
}
|
|
245
|
+
return result;
|
|
242
246
|
};
|
|
243
247
|
/**
|
|
244
248
|
* End the stream - emit any trailing zeros
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/tar/sparse.ts"],"sourcesContent":["/**\n * GNU Sparse File Support\n *\n * Handles parsing of GNU sparse file headers and stream reconstruction.\n *\n * GNU sparse format stores:\n * 1. A sparse map in the header (offset 386) with up to 4 entries\n * 2. Extended sparse headers (512-byte blocks) if more entries needed\n * 3. The actual data chunks (only non-zero portions of the file)\n *\n * Each sparse entry contains:\n * - offset: position in the virtual file where this data chunk belongs\n * - numbytes: size of this data chunk\n *\n * Node 0.8 compatible - uses only basic Buffer operations.\n */\n\nimport { SPARSE_ENTRIES_IN_HEADER, SPARSE_ENTRY_NUMBYTES_SIZE, SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_SIZE, SPARSE_EXTENDED_ENTRIES, SPARSE_EXTENDED_ISEXTENDED_OFFSET, SPARSE_ISEXTENDED_OFFSET, SPARSE_OFFSET, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE } from './constants.ts';\nimport EntryStream from './EntryStream.ts';\nimport { decodeOct } from './headers.ts';\n\n// Reusable zero buffer for sparse hole emission (64KB)\nconst ZERO_BUFFER_SIZE = 65536;\nlet zeroBuffer: Buffer | null = null;\n\nfunction getZeroBuffer(): Buffer {\n if (!zeroBuffer) {\n zeroBuffer = new Buffer(ZERO_BUFFER_SIZE);\n for (let i = 0; i < ZERO_BUFFER_SIZE; i++) {\n zeroBuffer[i] = 0;\n }\n }\n return zeroBuffer;\n}\n\n/**\n * Represents a region of actual data in a sparse file\n */\nexport interface SparseEntry {\n /** Offset in the reconstructed (virtual) file */\n offset: number;\n /** Number of bytes of actual data */\n numbytes: number;\n}\n\n/**\n * Parsed sparse header information\n */\nexport interface SparseInfo {\n /** The actual (reconstructed) file size */\n realSize: number;\n /** Sparse map entries */\n entries: SparseEntry[];\n /** Whether there are more entries in extended headers */\n isExtended: boolean;\n}\n\n/**\n * Parse sparse entries from a buffer starting at given offset\n *\n * @param buf - Buffer containing sparse entries\n * @param startOffset - Offset in buffer where entries begin\n * @param maxEntries - Maximum number of entries to read\n * @returns Array of valid sparse entries (stops at first zero entry)\n */\nfunction parseSparseEntries(buf: Buffer, startOffset: number, maxEntries: number): SparseEntry[] {\n const entries: SparseEntry[] = [];\n\n for (let i = 0; i < maxEntries; i++) {\n const entryOffset = startOffset + i * SPARSE_ENTRY_SIZE;\n const offset = decodeOct(buf, entryOffset, SPARSE_ENTRY_OFFSET_SIZE);\n const numbytes = decodeOct(buf, entryOffset + SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_NUMBYTES_SIZE);\n\n // Stop at first zero entry (end of sparse map)\n if (offset === 0 && numbytes === 0) {\n break;\n }\n\n entries.push({ offset, numbytes });\n }\n\n return entries;\n}\n\n/**\n * Parse GNU sparse header information from a tar header block\n *\n * @param headerBuf - The 512-byte tar header buffer\n * @returns Sparse info including real size, entries, and extended flag\n */\nexport function parseGnuSparseHeader(headerBuf: Buffer): SparseInfo {\n // Parse sparse entries from header (up to 4)\n const entries = parseSparseEntries(headerBuf, SPARSE_OFFSET, SPARSE_ENTRIES_IN_HEADER);\n\n // Parse isextended flag\n const isExtended = headerBuf[SPARSE_ISEXTENDED_OFFSET] !== 0;\n\n // Parse real file size\n const realSize = decodeOct(headerBuf, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE);\n\n return { realSize, entries, isExtended };\n}\n\n/**\n * Parse GNU sparse extended header block\n *\n * @param extBuf - The 512-byte extended sparse header buffer\n * @returns Object with entries and whether more extended blocks follow\n */\nexport function parseGnuSparseExtended(extBuf: Buffer): { entries: SparseEntry[]; isExtended: boolean } {\n // Parse sparse entries from extended block (up to 21)\n const entries = parseSparseEntries(extBuf, 0, SPARSE_EXTENDED_ENTRIES);\n\n // Parse isextended flag\n const isExtended = extBuf[SPARSE_EXTENDED_ISEXTENDED_OFFSET] !== 0;\n\n return { entries, isExtended };\n}\n\n/**\n * Calculate total data size from sparse map\n * This is the actual size of data stored in the archive (sum of all numbytes)\n */\nexport function sparseDataSize(entries: SparseEntry[]): number {\n let total = 0;\n for (let i = 0; i < entries.length; i++) {\n total += entries[i].numbytes;\n }\n return total;\n}\n\n/**\n * Stream that reconstructs a sparse file from data chunks\n *\n * Takes the sparse map and actual data, outputs reconstructed file\n * with zeros inserted for holes.\n *\n * Extends EntryStream to inherit pause/resume/pipe behavior.\n *\n * @internal\n */\nexport class SparseStream extends EntryStream {\n private entries: SparseEntry[];\n private realSize: number;\n private currentEntry: number;\n private virtualPosition: number;\n private entryBytesRemaining: number;\n\n constructor(entries: SparseEntry[], realSize: number) {\n super();\n this.entries = entries;\n this.realSize = realSize;\n this.currentEntry = 0;\n this.virtualPosition = 0;\n this.entryBytesRemaining = entries.length > 0 ? entries[0].numbytes : 0;\n }\n\n /**\n * Push data from the tar archive (actual sparse data chunk)\n * Overrides EntryStream.push() to reconstruct sparse file with holes.\n */\n push(data: Buffer): void {\n if (this.ended) return;\n\n let dataOffset = 0;\n\n while (dataOffset < data.length && this.currentEntry < this.entries.length) {\n const entry = this.entries[this.currentEntry];\n\n // First, emit zeros for any hole before current entry\n if (this.virtualPosition < entry.offset) {\n const holeSize = entry.offset - this.virtualPosition;\n this._emitZeros(holeSize);\n this.virtualPosition = entry.offset;\n }\n\n // Now emit actual data for this entry\n const toEmit = Math.min(this.entryBytesRemaining, data.length - dataOffset);\n if (toEmit > 0) {\n const chunk = data.slice(dataOffset, dataOffset + toEmit);\n super.push(chunk);\n dataOffset += toEmit;\n this.virtualPosition += toEmit;\n this.entryBytesRemaining -= toEmit;\n }\n\n // Move to next entry if current is exhausted\n if (this.entryBytesRemaining <= 0) {\n this.currentEntry++;\n if (this.currentEntry < this.entries.length) {\n this.entryBytesRemaining = this.entries[this.currentEntry].numbytes;\n }\n }\n }\n }\n\n /**\n * End the stream - emit any trailing zeros\n * Overrides EntryStream.end() to emit trailing zeros first.\n */\n end(): void {\n if (this.ended) return;\n\n // Emit remaining zeros to reach real file size\n if (this.virtualPosition < this.realSize) {\n this._emitZeros(this.realSize - this.virtualPosition);\n this.virtualPosition = this.realSize;\n }\n\n super.end();\n }\n\n /**\n * Emit zeros for a hole, reusing the shared zero buffer\n */\n private _emitZeros(size: number): void {\n const zeros = getZeroBuffer();\n let remaining = size;\n\n while (remaining > 0) {\n const toEmit = Math.min(remaining, ZERO_BUFFER_SIZE);\n // Slice from the reusable buffer to emit exact size needed\n super.push(zeros.slice(0, toEmit));\n remaining -= toEmit;\n }\n }\n}\n"],"names":["SparseStream","parseGnuSparseExtended","parseGnuSparseHeader","sparseDataSize","ZERO_BUFFER_SIZE","zeroBuffer","getZeroBuffer","Buffer","i","parseSparseEntries","buf","startOffset","maxEntries","entries","entryOffset","SPARSE_ENTRY_SIZE","offset","decodeOct","SPARSE_ENTRY_OFFSET_SIZE","numbytes","SPARSE_ENTRY_NUMBYTES_SIZE","push","headerBuf","SPARSE_OFFSET","SPARSE_ENTRIES_IN_HEADER","isExtended","SPARSE_ISEXTENDED_OFFSET","realSize","SPARSE_REALSIZE_OFFSET","SPARSE_REALSIZE_SIZE","extBuf","SPARSE_EXTENDED_ENTRIES","SPARSE_EXTENDED_ISEXTENDED_OFFSET","total","length","currentEntry","virtualPosition","entryBytesRemaining","data","ended","dataOffset","entry","holeSize","_emitZeros","toEmit","Math","min","chunk","slice","end","size","zeros","remaining","EntryStream"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC;;;;;;;;;;;QA8HYA;eAAAA;;QAhCGC;eAAAA;;QAnBAC;eAAAA;;QAiCAC;eAAAA;;;2BA1GqP;oEAC7O;yBACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE1B,uDAAuD;AACvD,IAAMC,mBAAmB;AACzB,IAAIC,aAA4B;AAEhC,SAASC;IACP,IAAI,CAACD,YAAY;QACfA,aAAa,IAAIE,OAAOH;QACxB,IAAK,IAAII,IAAI,GAAGA,IAAIJ,kBAAkBI,IAAK;YACzCH,UAAU,CAACG,EAAE,GAAG;QAClB;IACF;IACA,OAAOH;AACT;AAwBA;;;;;;;CAOC,GACD,SAASI,mBAAmBC,GAAW,EAAEC,WAAmB,EAAEC,UAAkB;IAC9E,IAAMC,UAAyB,EAAE;IAEjC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AAmB,GAAG,IAAI,CAACxB,OAAO,CAAC,IAAI,CAACsB,YAAY,CAAC,CAAChB,QAAQ;gBACrE;YACF;QACF;IACF;IAEA;;;GAGC,GACD8B,OAAAA,GAUC,GAVDA,SAAAA;QACE,IAAI,IAAI,CAACV,KAAK,EAAE;QAEhB,+CAA+C;QAC/C,IAAI,IAAI,CAACH,eAAe,GAAG,IAAI,CAACT,QAAQ,EAAE;YACxC,IAAI,CAACgB,UAAU,CAAC,IAAI,CAAChB,QAAQ,GAAG,IAAI,CAACS,eAAe;YACpD,IAAI,CAACA,eAAe,GAAG,IAAI,CAACT,QAAQ;QACtC;QAEA,uBApES3B,yBAoEHiD,OAAN,IAAK;IACP;IAEA;;GAEC,GACD,OAAQN,UAUP,GAVD,SAAQA,WAAWO,IAAY;QAC7B,IAAMC,QAAQ7C;QACd,IAAI8C,YAAYF;QAEhB,MAAOE,YAAY,EAAG;YACpB,IAAMR,SAASC,KAAKC,GAAG,CAACM,WAAWhD;YACnC,2DAA2D;YAC3D,uBAjFOJ,yBAiFDqB,QAAN,IAAK,aAAM8B,MAAMH,KAAK,CAAC,GAAGJ;YAC1BQ,aAAaR;QACf;IACF;WApFW5C;EAAqBqD,sBAAW"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/tar/sparse.ts"],"sourcesContent":["/**\n * GNU Sparse File Support\n *\n * Handles parsing of GNU sparse file headers and stream reconstruction.\n *\n * GNU sparse format stores:\n * 1. A sparse map in the header (offset 386) with up to 4 entries\n * 2. Extended sparse headers (512-byte blocks) if more entries needed\n * 3. The actual data chunks (only non-zero portions of the file)\n *\n * Each sparse entry contains:\n * - offset: position in the virtual file where this data chunk belongs\n * - numbytes: size of this data chunk\n *\n * Node 0.8 compatible - uses only basic Buffer operations.\n */\n\nimport { SPARSE_ENTRIES_IN_HEADER, SPARSE_ENTRY_NUMBYTES_SIZE, SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_SIZE, SPARSE_EXTENDED_ENTRIES, SPARSE_EXTENDED_ISEXTENDED_OFFSET, SPARSE_ISEXTENDED_OFFSET, SPARSE_OFFSET, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE } from './constants.ts';\nimport EntryStream from './EntryStream.ts';\nimport { decodeOct } from './headers.ts';\n\n// Reusable zero buffer for sparse hole emission (64KB)\nconst ZERO_BUFFER_SIZE = 65536;\nlet zeroBuffer: Buffer | null = null;\n\nfunction getZeroBuffer(): Buffer {\n if (!zeroBuffer) {\n zeroBuffer = new Buffer(ZERO_BUFFER_SIZE);\n for (let i = 0; i < ZERO_BUFFER_SIZE; i++) {\n zeroBuffer[i] = 0;\n }\n }\n return zeroBuffer;\n}\n\n/**\n * Represents a region of actual data in a sparse file\n */\nexport interface SparseEntry {\n /** Offset in the reconstructed (virtual) file */\n offset: number;\n /** Number of bytes of actual data */\n numbytes: number;\n}\n\n/**\n * Parsed sparse header information\n */\nexport interface SparseInfo {\n /** The actual (reconstructed) file size */\n realSize: number;\n /** Sparse map entries */\n entries: SparseEntry[];\n /** Whether there are more entries in extended headers */\n isExtended: boolean;\n}\n\n/**\n * Parse sparse entries from a buffer starting at given offset\n *\n * @param buf - Buffer containing sparse entries\n * @param startOffset - Offset in buffer where entries begin\n * @param maxEntries - Maximum number of entries to read\n * @returns Array of valid sparse entries (stops at first zero entry)\n */\nfunction parseSparseEntries(buf: Buffer, startOffset: number, maxEntries: number): SparseEntry[] {\n const entries: SparseEntry[] = [];\n\n for (let i = 0; i < maxEntries; i++) {\n const entryOffset = startOffset + i * SPARSE_ENTRY_SIZE;\n const offset = decodeOct(buf, entryOffset, SPARSE_ENTRY_OFFSET_SIZE);\n const numbytes = decodeOct(buf, entryOffset + SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_NUMBYTES_SIZE);\n\n // Stop at first zero entry (end of sparse map)\n if (offset === 0 && numbytes === 0) {\n break;\n }\n\n entries.push({ offset, numbytes });\n }\n\n return entries;\n}\n\n/**\n * Parse GNU sparse header information from a tar header block\n *\n * @param headerBuf - The 512-byte tar header buffer\n * @returns Sparse info including real size, entries, and extended flag\n */\nexport function parseGnuSparseHeader(headerBuf: Buffer): SparseInfo {\n // Parse sparse entries from header (up to 4)\n const entries = parseSparseEntries(headerBuf, SPARSE_OFFSET, SPARSE_ENTRIES_IN_HEADER);\n\n // Parse isextended flag\n const isExtended = headerBuf[SPARSE_ISEXTENDED_OFFSET] !== 0;\n\n // Parse real file size\n const realSize = decodeOct(headerBuf, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE);\n\n return { realSize, entries, isExtended };\n}\n\n/**\n * Parse GNU sparse extended header block\n *\n * @param extBuf - The 512-byte extended sparse header buffer\n * @returns Object with entries and whether more extended blocks follow\n */\nexport function parseGnuSparseExtended(extBuf: Buffer): { entries: SparseEntry[]; isExtended: boolean } {\n // Parse sparse entries from extended block (up to 21)\n const entries = parseSparseEntries(extBuf, 0, SPARSE_EXTENDED_ENTRIES);\n\n // Parse isextended flag\n const isExtended = extBuf[SPARSE_EXTENDED_ISEXTENDED_OFFSET] !== 0;\n\n return { entries, isExtended };\n}\n\n/**\n * Calculate total data size from sparse map\n * This is the actual size of data stored in the archive (sum of all numbytes)\n */\nexport function sparseDataSize(entries: SparseEntry[]): number {\n let total = 0;\n for (let i = 0; i < entries.length; i++) {\n total += entries[i].numbytes;\n }\n return total;\n}\n\n/**\n * Stream that reconstructs a sparse file from data chunks\n *\n * Takes the sparse map and actual data, outputs reconstructed file\n * with zeros inserted for holes.\n *\n * Extends EntryStream to inherit pause/resume/pipe behavior.\n *\n * @internal\n */\nexport class SparseStream extends EntryStream {\n private entries: SparseEntry[];\n private realSize: number;\n private currentEntry: number;\n private virtualPosition: number;\n private entryBytesRemaining: number;\n\n constructor(entries: SparseEntry[], realSize: number) {\n super();\n this.entries = entries;\n this.realSize = realSize;\n this.currentEntry = 0;\n this.virtualPosition = 0;\n this.entryBytesRemaining = entries.length > 0 ? entries[0].numbytes : 0;\n }\n\n /**\n * Push data from the tar archive (actual sparse data chunk)\n * Overrides EntryStream.push() to reconstruct sparse file with holes.\n */\n push(data: Buffer | null): boolean {\n // Allow null through to signal end\n if (data === null) return super.push(null);\n if (this.ended) return false;\n\n let dataOffset = 0;\n let result = true;\n\n while (dataOffset < data.length && this.currentEntry < this.entries.length) {\n const entry = this.entries[this.currentEntry];\n\n // First, emit zeros for any hole before current entry\n if (this.virtualPosition < entry.offset) {\n const holeSize = entry.offset - this.virtualPosition;\n this._emitZeros(holeSize);\n this.virtualPosition = entry.offset;\n }\n\n // Now emit actual data for this entry\n const toEmit = Math.min(this.entryBytesRemaining, data.length - dataOffset);\n if (toEmit > 0) {\n const chunk = data.slice(dataOffset, dataOffset + toEmit);\n result = super.push(chunk);\n dataOffset += toEmit;\n this.virtualPosition += toEmit;\n this.entryBytesRemaining -= toEmit;\n }\n\n // Move to next entry if current is exhausted\n if (this.entryBytesRemaining <= 0) {\n this.currentEntry++;\n if (this.currentEntry < this.entries.length) {\n this.entryBytesRemaining = this.entries[this.currentEntry].numbytes;\n }\n }\n }\n return result;\n }\n\n /**\n * End the stream - emit any trailing zeros\n * Overrides EntryStream.end() to emit trailing zeros first.\n */\n end(): void {\n if (this.ended) return;\n\n // Emit remaining zeros to reach real file size\n if (this.virtualPosition < this.realSize) {\n this._emitZeros(this.realSize - this.virtualPosition);\n this.virtualPosition = this.realSize;\n }\n\n super.end();\n }\n\n /**\n * Emit zeros for a hole, reusing the shared zero buffer\n */\n private _emitZeros(size: number): void {\n const zeros = getZeroBuffer();\n let remaining = size;\n\n while (remaining > 0) {\n const toEmit = Math.min(remaining, ZERO_BUFFER_SIZE);\n // Slice from the reusable buffer to emit exact size needed\n super.push(zeros.slice(0, toEmit));\n remaining -= toEmit;\n }\n }\n}\n"],"names":["SparseStream","parseGnuSparseExtended","parseGnuSparseHeader","sparseDataSize","ZERO_BUFFER_SIZE","zeroBuffer","getZeroBuffer","Buffer","i","parseSparseEntries","buf","startOffset","maxEntries","entries","entryOffset","SPARSE_ENTRY_SIZE","offset","decodeOct","SPARSE_ENTRY_OFFSET_SIZE","numbytes","SPARSE_ENTRY_NUMBYTES_SIZE","push","headerBuf","SPARSE_OFFSET","SPARSE_ENTRIES_IN_HEADER","isExtended","SPARSE_ISEXTENDED_OFFSET","realSize","SPARSE_REALSIZE_OFFSET","SPARSE_REALSIZE_SIZE","extBuf","SPARSE_EXTENDED_ENTRIES","SPARSE_EXTENDED_ISEXTENDED_OFFSET","total","length","currentEntry","virtualPosition","entryBytesRemaining","data","ended","dataOffset","result","entry","holeSize","_emitZeros","toEmit","Math","min","chunk","slice","end","size","zeros","remaining","EntryStream"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC;;;;;;;;;;;QA8HYA;eAAAA;;QAhCGC;eAAAA;;QAnBAC;eAAAA;;QAiCAC;eAAAA;;;2BA1GqP;oEAC7O;yBACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAE1B,uDAAuD;AACvD,IAAMC,mBAAmB;AACzB,IAAIC,aAA4B;AAEhC,SAASC;IACP,IAAI,CAACD,YAAY;QACfA,aAAa,IAAIE,OAAOH;QACxB,IAAK,IAAII,IAAI,GAAGA,IAAIJ,kBAAkBI,IAAK;YACzCH,UAAU,CAACG,EAAE,GAAG;QAClB;IACF;IACA,OAAOH;AACT;AAwBA;;;;;;;CAOC,GACD,SAASI,mBAAmBC,GAAW,EAAEC,WAAmB,EAAEC,UAAkB;IAC9E,IAAMC,UAAyB,EAAE;IAEjC,IAAK,IAAIL,IAAI,GAAGA,IAAII,YAAYJ,IAAK;QACnC,IAAMM,cAAcH,cAAcH,IAAIO,8BAAiB;QACvD,IAAMC,SAASC,IAAAA,oBAAS,EAACP,KAAKI,aAAaI,qCAAwB;QACnE,IAAMC,WAAWF,IAAAA,oBAAS,EAACP,KAAKI,cAAcI,qCAAwB,EAAEE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A,OAAOsB;IACT;IAEA;;;GAGC,GACDS,OAAAA,GAUC,GAVDA,SAAAA;QACE,IAAI,IAAI,CAACX,KAAK,EAAE;QAEhB,+CAA+C;QAC/C,IAAI,IAAI,CAACH,eAAe,GAAG,IAAI,CAACT,QAAQ,EAAE;YACxC,IAAI,CAACiB,UAAU,CAAC,IAAI,CAACjB,QAAQ,GAAG,IAAI,CAACS,eAAe;YACpD,IAAI,CAACA,eAAe,GAAG,IAAI,CAACT,QAAQ;QACtC;QAEA,uBAxES3B,yBAwEHkD,OAAN,IAAK;IACP;IAEA;;GAEC,GACD,OAAQN,UAUP,GAVD,SAAQA,WAAWO,IAAY;QAC7B,IAAMC,QAAQ9C;QACd,IAAI+C,YAAYF;QAEhB,MAAOE,YAAY,EAAG;YACpB,IAAMR,SAASC,KAAKC,GAAG,CAACM,WAAWjD;YACnC,2DAA2D;YAC3D,uBArFOJ,yBAqFDqB,QAAN,IAAK,aAAM+B,MAAMH,KAAK,CAAC,GAAGJ;YAC1BQ,aAAaR;QACf;IACF;WAxFW7C;EAAqBsD,sBAAW"}
|
package/dist/esm/TarIterator.js
CHANGED
|
@@ -6,12 +6,11 @@ import nextEntry from './nextEntry.js';
|
|
|
6
6
|
import TarExtract from './tar/TarExtract.js';
|
|
7
7
|
let TarIterator = class TarIterator extends BaseIterator {
|
|
8
8
|
end(err) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this.lock
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
BaseIterator.prototype.end.call(this, err); // call in lock release so end is properly handled
|
|
9
|
+
const lock = this.lock;
|
|
10
|
+
if (lock) {
|
|
11
|
+
this.lock = null; // Clear FIRST to prevent re-entrancy
|
|
12
|
+
lock.err = err;
|
|
13
|
+
lock.release(); // Lock.__destroy() handles BaseIterator.end()
|
|
15
14
|
}
|
|
16
15
|
this.extract = null;
|
|
17
16
|
}
|
|
@@ -28,12 +27,15 @@ let TarIterator = class TarIterator extends BaseIterator {
|
|
|
28
27
|
// Note: options passed here are ExtractOptions (strip, force, etc.)
|
|
29
28
|
// TarExtract uses TarExtractOptions (filenameEncoding, etc.) which is different
|
|
30
29
|
this.extract = new TarExtract();
|
|
30
|
+
this.lock.extract = this.extract;
|
|
31
31
|
const pipe = (cb)=>{
|
|
32
32
|
try {
|
|
33
33
|
if (typeof source === 'string') source = fs.createReadStream(source);
|
|
34
34
|
} catch (err) {
|
|
35
35
|
cb(err);
|
|
36
36
|
}
|
|
37
|
+
// Store source stream in lock for cleanup
|
|
38
|
+
this.lock.sourceStream = source;
|
|
37
39
|
const end = once(cb);
|
|
38
40
|
const self = this;
|
|
39
41
|
source.on('data', function onData(chunk) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/TarIterator.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport BaseIterator from 'extract-base-iterator';\nimport fs from 'fs';\n\nimport Lock from './lib/Lock.ts';\nimport nextEntry from './nextEntry.ts';\nimport TarExtract from './tar/TarExtract.ts';\n\nimport type { ExtractOptions } from './types.ts';\n\nexport default class TarIterator extends BaseIterator {\n /** @internal @hidden */\n lock: Lock;\n /** @internal @hidden */\n extract: TarExtract;\n\n constructor(source: string | NodeJS.ReadableStream, options: ExtractOptions = {}) {\n super(options);\n this.lock = new Lock();\n this.lock.iterator = this;\n\n let cancelled = false;\n const setup = (): undefined => {\n cancelled = true;\n };\n this.processing.push(setup);\n\n // Use our pure TarExtract instead of tar-stream-compat\n // Note: options passed here are ExtractOptions (strip, force, etc.)\n // TarExtract uses TarExtractOptions (filenameEncoding, etc.) which is different\n this.extract = new TarExtract();\n\n const pipe = (cb) => {\n try {\n if (typeof source === 'string') source = fs.createReadStream(source);\n } catch (err) {\n cb(err);\n }\n\n const end = once(cb);\n const self = this;\n (source as NodeJS.ReadableStream).on('data', function onData(chunk) {\n try {\n if (self.extract) self.extract.write(chunk);\n } catch (err) {\n // Handle synchronous errors from TarExtract (e.g., invalid format)\n end(err);\n }\n });\n (source as NodeJS.ReadableStream).on('data', function onFirstData() {\n end();\n });\n (source as NodeJS.ReadableStream).on('error', end);\n (source as NodeJS.ReadableStream).on('end', function onEnd() {\n if (self.extract) self.extract.end();\n });\n };\n pipe((err) => {\n this.processing.remove(setup);\n if (this.done || cancelled) return; // done\n err ? this.end(err) : this.push(nextEntry.bind(null, null));\n });\n }\n\n end(err?: Error) {\n
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/TarIterator.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport BaseIterator from 'extract-base-iterator';\nimport fs from 'fs';\n\nimport Lock from './lib/Lock.ts';\nimport nextEntry from './nextEntry.ts';\nimport TarExtract from './tar/TarExtract.ts';\n\nimport type { ExtractOptions } from './types.ts';\n\nexport default class TarIterator extends BaseIterator {\n /** @internal @hidden */\n lock: Lock;\n /** @internal @hidden */\n extract: TarExtract;\n\n constructor(source: string | NodeJS.ReadableStream, options: ExtractOptions = {}) {\n super(options);\n this.lock = new Lock();\n this.lock.iterator = this;\n\n let cancelled = false;\n const setup = (): undefined => {\n cancelled = true;\n };\n this.processing.push(setup);\n\n // Use our pure TarExtract instead of tar-stream-compat\n // Note: options passed here are ExtractOptions (strip, force, etc.)\n // TarExtract uses TarExtractOptions (filenameEncoding, etc.) which is different\n this.extract = new TarExtract();\n this.lock.extract = this.extract;\n\n const pipe = (cb) => {\n try {\n if (typeof source === 'string') source = fs.createReadStream(source);\n } catch (err) {\n cb(err);\n }\n\n // Store source stream in lock for cleanup\n this.lock.sourceStream = source as NodeJS.ReadableStream;\n\n const end = once(cb);\n const self = this;\n (source as NodeJS.ReadableStream).on('data', function onData(chunk) {\n try {\n if (self.extract) self.extract.write(chunk);\n } catch (err) {\n // Handle synchronous errors from TarExtract (e.g., invalid format)\n end(err);\n }\n });\n (source as NodeJS.ReadableStream).on('data', function onFirstData() {\n end();\n });\n (source as NodeJS.ReadableStream).on('error', end);\n (source as NodeJS.ReadableStream).on('end', function onEnd() {\n if (self.extract) self.extract.end();\n });\n };\n pipe((err) => {\n this.processing.remove(setup);\n if (this.done || cancelled) return; // done\n err ? this.end(err) : this.push(nextEntry.bind(null, null));\n });\n }\n\n end(err?: Error) {\n const lock = this.lock;\n if (lock) {\n this.lock = null; // Clear FIRST to prevent re-entrancy\n lock.err = err;\n lock.release(); // Lock.__destroy() handles BaseIterator.end()\n }\n this.extract = null;\n }\n}\n"],"names":["once","BaseIterator","fs","Lock","nextEntry","TarExtract","TarIterator","end","err","lock","release","extract","source","options","iterator","cancelled","setup","processing","push","pipe","cb","createReadStream","sourceStream","self","on","onData","chunk","write","onFirstData","onEnd","remove","done","bind"],"mappings":"AAAA,OAAOA,UAAU,eAAe;AAChC,OAAOC,kBAAkB,wBAAwB;AACjD,OAAOC,QAAQ,KAAK;AAEpB,OAAOC,UAAU,gBAAgB;AACjC,OAAOC,eAAe,iBAAiB;AACvC,OAAOC,gBAAgB,sBAAsB;AAI9B,IAAA,AAAMC,cAAN,MAAMA,oBAAoBL;IA0DvCM,IAAIC,GAAW,EAAE;QACf,MAAMC,OAAO,IAAI,CAACA,IAAI;QACtB,IAAIA,MAAM;YACR,IAAI,CAACA,IAAI,GAAG,MAAM,qCAAqC;YACvDA,KAAKD,GAAG,GAAGA;YACXC,KAAKC,OAAO,IAAI,8CAA8C;QAChE;QACA,IAAI,CAACC,OAAO,GAAG;IACjB;IA5DA,YAAYC,MAAsC,EAAEC,UAA0B,CAAC,CAAC,CAAE;QAChF,KAAK,CAACA;QACN,IAAI,CAACJ,IAAI,GAAG,IAAIN;QAChB,IAAI,CAACM,IAAI,CAACK,QAAQ,GAAG,IAAI;QAEzB,IAAIC,YAAY;QAChB,MAAMC,QAAQ;YACZD,YAAY;QACd;QACA,IAAI,CAACE,UAAU,CAACC,IAAI,CAACF;QAErB,uDAAuD;QACvD,oEAAoE;QACpE,gFAAgF;QAChF,IAAI,CAACL,OAAO,GAAG,IAAIN;QACnB,IAAI,CAACI,IAAI,CAACE,OAAO,GAAG,IAAI,CAACA,OAAO;QAEhC,MAAMQ,OAAO,CAACC;YACZ,IAAI;gBACF,IAAI,OAAOR,WAAW,UAAUA,SAASV,GAAGmB,gBAAgB,CAACT;YAC/D,EAAE,OAAOJ,KAAK;gBACZY,GAAGZ;YACL;YAEA,0CAA0C;YAC1C,IAAI,CAACC,IAAI,CAACa,YAAY,GAAGV;YAEzB,MAAML,MAAMP,KAAKoB;YACjB,MAAMG,OAAO,IAAI;YAChBX,OAAiCY,EAAE,CAAC,QAAQ,SAASC,OAAOC,KAAK;gBAChE,IAAI;oBACF,IAAIH,KAAKZ,OAAO,EAAEY,KAAKZ,OAAO,CAACgB,KAAK,CAACD;gBACvC,EAAE,OAAOlB,KAAK;oBACZ,mEAAmE;oBACnED,IAAIC;gBACN;YACF;YACCI,OAAiCY,EAAE,CAAC,QAAQ,SAASI;gBACpDrB;YACF;YACCK,OAAiCY,EAAE,CAAC,SAASjB;YAC7CK,OAAiCY,EAAE,CAAC,OAAO,SAASK;gBACnD,IAAIN,KAAKZ,OAAO,EAAEY,KAAKZ,OAAO,CAACJ,GAAG;YACpC;QACF;QACAY,KAAK,CAACX;YACJ,IAAI,CAACS,UAAU,CAACa,MAAM,CAACd;YACvB,IAAI,IAAI,CAACe,IAAI,IAAIhB,WAAW,QAAQ,OAAO;YAC3CP,MAAM,IAAI,CAACD,GAAG,CAACC,OAAO,IAAI,CAACU,IAAI,CAACd,UAAU4B,IAAI,CAAC,MAAM;QACvD;IACF;AAWF;AAnEA,SAAqB1B,yBAmEpB"}
|
package/dist/esm/lib/Lock.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import BaseIterator from 'extract-base-iterator';
|
|
2
|
+
import type TarExtract from '../tar/TarExtract.js';
|
|
2
3
|
export default class Lock {
|
|
3
4
|
private count;
|
|
4
5
|
iterator: BaseIterator;
|
|
5
6
|
err: Error;
|
|
7
|
+
/** @internal @hidden */
|
|
8
|
+
extract: TarExtract;
|
|
9
|
+
sourceStream: NodeJS.ReadableStream;
|
|
6
10
|
retain(): void;
|
|
7
11
|
release(): void;
|
|
8
12
|
private __destroy;
|
package/dist/esm/lib/Lock.js
CHANGED
|
@@ -9,6 +9,17 @@ let Lock = class Lock {
|
|
|
9
9
|
if (this.count === 0) this.__destroy();
|
|
10
10
|
}
|
|
11
11
|
__destroy() {
|
|
12
|
+
// Destroy source stream FIRST to stop data flow
|
|
13
|
+
if (this.sourceStream) {
|
|
14
|
+
const stream = this.sourceStream;
|
|
15
|
+
if (typeof stream.destroy === 'function') stream.destroy();
|
|
16
|
+
this.sourceStream = null;
|
|
17
|
+
}
|
|
18
|
+
// Clear extract reference
|
|
19
|
+
if (this.extract) {
|
|
20
|
+
this.extract = null;
|
|
21
|
+
}
|
|
22
|
+
// Call BaseIterator.end() LAST
|
|
12
23
|
if (this.iterator) {
|
|
13
24
|
BaseIterator.prototype.end.call(this.iterator, this.err || null);
|
|
14
25
|
this.iterator = null;
|
|
@@ -19,6 +30,9 @@ let Lock = class Lock {
|
|
|
19
30
|
// members
|
|
20
31
|
this.iterator = null;
|
|
21
32
|
this.err = null;
|
|
33
|
+
// cleanup resources
|
|
34
|
+
/** @internal @hidden */ this.extract = null;
|
|
35
|
+
this.sourceStream = null;
|
|
22
36
|
}
|
|
23
37
|
};
|
|
24
38
|
export { Lock as default };
|
package/dist/esm/lib/Lock.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/lib/Lock.ts"],"sourcesContent":["import BaseIterator from 'extract-base-iterator';\n\nexport default class Lock {\n private count = 1;\n\n // members\n iterator: BaseIterator = null;\n err: Error = null;\n\n retain() {\n this.count++;\n }\n\n release() {\n if (this.count <= 0) throw new Error('Lock count is corrupted');\n this.count--;\n if (this.count === 0) this.__destroy();\n }\n\n private __destroy() {\n if (this.iterator) {\n BaseIterator.prototype.end.call(this.iterator, this.err || null);\n this.iterator = null;\n }\n }\n}\n"],"names":["BaseIterator","Lock","retain","count","release","Error","__destroy","iterator","prototype","end","call","err"],"mappings":"AAAA,OAAOA,kBAAkB,wBAAwB;
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/lib/Lock.ts"],"sourcesContent":["import BaseIterator from 'extract-base-iterator';\nimport type TarExtract from '../tar/TarExtract.ts';\n\nexport default class Lock {\n private count = 1;\n\n // members\n iterator: BaseIterator = null;\n err: Error = null;\n\n // cleanup resources\n /** @internal @hidden */\n extract: TarExtract = null;\n sourceStream: NodeJS.ReadableStream = null;\n\n retain() {\n this.count++;\n }\n\n release() {\n if (this.count <= 0) throw new Error('Lock count is corrupted');\n this.count--;\n if (this.count === 0) this.__destroy();\n }\n\n private __destroy() {\n // Destroy source stream FIRST to stop data flow\n if (this.sourceStream) {\n const stream = this.sourceStream as NodeJS.ReadableStream & { destroy?: () => void };\n if (typeof stream.destroy === 'function') stream.destroy();\n this.sourceStream = null;\n }\n\n // Clear extract reference\n if (this.extract) {\n this.extract = null;\n }\n\n // Call BaseIterator.end() LAST\n if (this.iterator) {\n BaseIterator.prototype.end.call(this.iterator, this.err || null);\n this.iterator = null;\n }\n }\n}\n"],"names":["BaseIterator","Lock","retain","count","release","Error","__destroy","sourceStream","stream","destroy","extract","iterator","prototype","end","call","err"],"mappings":"AAAA,OAAOA,kBAAkB,wBAAwB;AAGlC,IAAA,AAAMC,OAAN,MAAMA;IAYnBC,SAAS;QACP,IAAI,CAACC,KAAK;IACZ;IAEAC,UAAU;QACR,IAAI,IAAI,CAACD,KAAK,IAAI,GAAG,MAAM,IAAIE,MAAM;QACrC,IAAI,CAACF,KAAK;QACV,IAAI,IAAI,CAACA,KAAK,KAAK,GAAG,IAAI,CAACG,SAAS;IACtC;IAEQA,YAAY;QAClB,gDAAgD;QAChD,IAAI,IAAI,CAACC,YAAY,EAAE;YACrB,MAAMC,SAAS,IAAI,CAACD,YAAY;YAChC,IAAI,OAAOC,OAAOC,OAAO,KAAK,YAAYD,OAAOC,OAAO;YACxD,IAAI,CAACF,YAAY,GAAG;QACtB;QAEA,0BAA0B;QAC1B,IAAI,IAAI,CAACG,OAAO,EAAE;YAChB,IAAI,CAACA,OAAO,GAAG;QACjB;QAEA,+BAA+B;QAC/B,IAAI,IAAI,CAACC,QAAQ,EAAE;YACjBX,aAAaY,SAAS,CAACC,GAAG,CAACC,IAAI,CAAC,IAAI,CAACH,QAAQ,EAAE,IAAI,CAACI,GAAG,IAAI;YAC3D,IAAI,CAACJ,QAAQ,GAAG;QAClB;IACF;;aAvCQR,QAAQ;QAEhB,UAAU;aACVQ,WAAyB;aACzBI,MAAa;QAEb,oBAAoB;QACpB,sBAAsB,QACtBL,UAAsB;aACtBH,eAAsC;;AA+BxC;AAzCA,SAAqBN,kBAyCpB"}
|
package/dist/esm/nextEntry.js
CHANGED
|
@@ -7,6 +7,13 @@ const _setImmediate = typeof setImmediate === 'function' ? setImmediate : typeof
|
|
|
7
7
|
setTimeout(fn, 0);
|
|
8
8
|
};
|
|
9
9
|
export default function nextEntry(next, iterator, callback) {
|
|
10
|
+
// Guard: bail early if iterator already ended
|
|
11
|
+
if (!iterator.lock || iterator.isDone()) {
|
|
12
|
+
return callback(null, {
|
|
13
|
+
done: true,
|
|
14
|
+
value: null
|
|
15
|
+
});
|
|
16
|
+
}
|
|
10
17
|
const extract = iterator.extract;
|
|
11
18
|
if (!extract) return callback(new Error('Extract missing'));
|
|
12
19
|
const nextCallback = once((err, entry, next)=>{
|
|
@@ -30,7 +37,9 @@ export default function nextEntry(next, iterator, callback) {
|
|
|
30
37
|
const onError = callback;
|
|
31
38
|
const onEnd = callback.bind(null, null);
|
|
32
39
|
const onEntry = function onEntry(header, stream, next) {
|
|
33
|
-
if
|
|
40
|
+
// Guard: skip if iterator already ended (stale lock)
|
|
41
|
+
if (!iterator.lock || iterator.isDone()) {
|
|
42
|
+
stream.resume(); // drain stream
|
|
34
43
|
return nextCallback(null, null, next);
|
|
35
44
|
}
|
|
36
45
|
const attributes = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/nextEntry.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport { DirectoryEntry, LinkEntry, SymbolicLinkEntry } from 'extract-base-iterator';\nimport path from 'path';\nimport FileEntry from './FileEntry.ts';\nimport type Iterator from './TarIterator.ts';\n\nimport type { Entry, EntryCallback } from './types.ts';\n\n// Node 0.8 compatible setImmediate (setImmediate was added in Node 0.10)\nconst _setImmediate =\n typeof setImmediate === 'function'\n ? setImmediate\n : typeof process !== 'undefined' && typeof process.nextTick === 'function'\n ? process.nextTick\n : function setImmediateFallback(fn: () => void) {\n setTimeout(fn, 0);\n };\n\nexport type TarNext = () => undefined;\nexport type NextCallback = (error?: Error, entry?: Entry, next?: TarNext) => undefined;\n\nexport default function nextEntry(next: TarNext, iterator: Iterator, callback: EntryCallback): undefined {\n const extract = iterator.extract;\n if (!extract) return callback(new Error('Extract missing'));\n\n const nextCallback = once((err?: Error, entry?: Entry, next?: TarNext) => {\n extract.removeListener('entry', onEntry);\n extract.removeListener('error', onError);\n extract.removeListener('finish', onEnd);\n\n // keep processing\n if (entry) iterator.push(nextEntry.bind(null, next));\n\n // Use setImmediate to defer the callback invocation\n // This ensures proper async behavior with the BaseIterator\n _setImmediate(() => {\n err ? callback(err) : callback(null, entry ? { done: false, value: entry } : { done: true, value: null });\n });\n }) as NextCallback;\n\n const onError = callback;\n const onEnd = callback.bind(null, null);\n const onEntry = function onEntry(header, stream, next: TarNext) {\n if (iterator.isDone()) {\n return nextCallback(null, null, next);\n }\n\n const attributes = { ...header };\n attributes.path = header.name.split(path.sep).filter(Boolean).join(path.sep);\n attributes.mtime = new Date(attributes.mtime);\n\n switch (attributes.type) {\n case 'directory':\n stream.resume(); // drain stream\n return nextCallback(null, new DirectoryEntry(attributes), next);\n case 'symlink':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new SymbolicLinkEntry(attributes), next);\n case 'link':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new LinkEntry(attributes), next);\n case 'file':\n return nextCallback(null, new FileEntry(attributes, stream, iterator.lock), next);\n }\n\n stream.resume(); // drain stream\n return nextCallback(new Error(`Unrecognized entry type: ${attributes.type}`), null, next);\n };\n\n extract.on('entry', onEntry);\n extract.on('error', onError);\n extract.on('finish', onEnd);\n\n // Resume parsing to emit any pending entry\n // For first call (next is null), this triggers the first entry emission\n // For subsequent calls, next() unlocks the parser which then processes the next header\n if (next) {\n next();\n } else {\n // First call - resume to emit any pending entry\n extract.resume();\n }\n}\n"],"names":["once","DirectoryEntry","LinkEntry","SymbolicLinkEntry","path","FileEntry","_setImmediate","setImmediate","process","nextTick","setImmediateFallback","fn","setTimeout","nextEntry","next","iterator","callback","extract","Error","nextCallback","err","entry","removeListener","onEntry","onError","onEnd","push","bind","
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/nextEntry.ts"],"sourcesContent":["import once from 'call-once-fn';\nimport { DirectoryEntry, LinkEntry, SymbolicLinkEntry } from 'extract-base-iterator';\nimport path from 'path';\nimport FileEntry from './FileEntry.ts';\nimport type Iterator from './TarIterator.ts';\n\nimport type { Entry, EntryCallback } from './types.ts';\n\n// Node 0.8 compatible setImmediate (setImmediate was added in Node 0.10)\nconst _setImmediate =\n typeof setImmediate === 'function'\n ? setImmediate\n : typeof process !== 'undefined' && typeof process.nextTick === 'function'\n ? process.nextTick\n : function setImmediateFallback(fn: () => void) {\n setTimeout(fn, 0);\n };\n\nexport type TarNext = () => undefined;\nexport type NextCallback = (error?: Error, entry?: Entry, next?: TarNext) => undefined;\n\nexport default function nextEntry(next: TarNext, iterator: Iterator, callback: EntryCallback): undefined {\n // Guard: bail early if iterator already ended\n if (!iterator.lock || iterator.isDone()) {\n return callback(null, { done: true, value: null });\n }\n\n const extract = iterator.extract;\n if (!extract) return callback(new Error('Extract missing'));\n\n const nextCallback = once((err?: Error, entry?: Entry, next?: TarNext) => {\n extract.removeListener('entry', onEntry);\n extract.removeListener('error', onError);\n extract.removeListener('finish', onEnd);\n\n // keep processing\n if (entry) iterator.push(nextEntry.bind(null, next));\n\n // Use setImmediate to defer the callback invocation\n // This ensures proper async behavior with the BaseIterator\n _setImmediate(() => {\n err ? callback(err) : callback(null, entry ? { done: false, value: entry } : { done: true, value: null });\n });\n }) as NextCallback;\n\n const onError = callback;\n const onEnd = callback.bind(null, null);\n const onEntry = function onEntry(header, stream, next: TarNext) {\n // Guard: skip if iterator already ended (stale lock)\n if (!iterator.lock || iterator.isDone()) {\n stream.resume(); // drain stream\n return nextCallback(null, null, next);\n }\n\n const attributes = { ...header };\n attributes.path = header.name.split(path.sep).filter(Boolean).join(path.sep);\n attributes.mtime = new Date(attributes.mtime);\n\n switch (attributes.type) {\n case 'directory':\n stream.resume(); // drain stream\n return nextCallback(null, new DirectoryEntry(attributes), next);\n case 'symlink':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new SymbolicLinkEntry(attributes), next);\n case 'link':\n stream.resume(); // drain stream\n attributes.linkpath = header.linkname;\n return nextCallback(null, new LinkEntry(attributes), next);\n case 'file':\n return nextCallback(null, new FileEntry(attributes, stream, iterator.lock), next);\n }\n\n stream.resume(); // drain stream\n return nextCallback(new Error(`Unrecognized entry type: ${attributes.type}`), null, next);\n };\n\n extract.on('entry', onEntry);\n extract.on('error', onError);\n extract.on('finish', onEnd);\n\n // Resume parsing to emit any pending entry\n // For first call (next is null), this triggers the first entry emission\n // For subsequent calls, next() unlocks the parser which then processes the next header\n if (next) {\n next();\n } else {\n // First call - resume to emit any pending entry\n extract.resume();\n }\n}\n"],"names":["once","DirectoryEntry","LinkEntry","SymbolicLinkEntry","path","FileEntry","_setImmediate","setImmediate","process","nextTick","setImmediateFallback","fn","setTimeout","nextEntry","next","iterator","callback","lock","isDone","done","value","extract","Error","nextCallback","err","entry","removeListener","onEntry","onError","onEnd","push","bind","header","stream","resume","attributes","name","split","sep","filter","Boolean","join","mtime","Date","type","linkpath","linkname","on"],"mappings":"AAAA,OAAOA,UAAU,eAAe;AAChC,SAASC,cAAc,EAAEC,SAAS,EAAEC,iBAAiB,QAAQ,wBAAwB;AACrF,OAAOC,UAAU,OAAO;AACxB,OAAOC,eAAe,iBAAiB;AAKvC,yEAAyE;AACzE,MAAMC,gBACJ,OAAOC,iBAAiB,aACpBA,eACA,OAAOC,YAAY,eAAe,OAAOA,QAAQC,QAAQ,KAAK,aAC5DD,QAAQC,QAAQ,GAChB,SAASC,qBAAqBC,EAAc;IAC1CC,WAAWD,IAAI;AACjB;AAKR,eAAe,SAASE,UAAUC,IAAa,EAAEC,QAAkB,EAAEC,QAAuB;IAC1F,8CAA8C;IAC9C,IAAI,CAACD,SAASE,IAAI,IAAIF,SAASG,MAAM,IAAI;QACvC,OAAOF,SAAS,MAAM;YAAEG,MAAM;YAAMC,OAAO;QAAK;IAClD;IAEA,MAAMC,UAAUN,SAASM,OAAO;IAChC,IAAI,CAACA,SAAS,OAAOL,SAAS,IAAIM,MAAM;IAExC,MAAMC,eAAevB,KAAK,CAACwB,KAAaC,OAAeX;QACrDO,QAAQK,cAAc,CAAC,SAASC;QAChCN,QAAQK,cAAc,CAAC,SAASE;QAChCP,QAAQK,cAAc,CAAC,UAAUG;QAEjC,kBAAkB;QAClB,IAAIJ,OAAOV,SAASe,IAAI,CAACjB,UAAUkB,IAAI,CAAC,MAAMjB;QAE9C,oDAAoD;QACpD,2DAA2D;QAC3DR,cAAc;YACZkB,MAAMR,SAASQ,OAAOR,SAAS,MAAMS,QAAQ;gBAAEN,MAAM;gBAAOC,OAAOK;YAAM,IAAI;gBAAEN,MAAM;gBAAMC,OAAO;YAAK;QACzG;IACF;IAEA,MAAMQ,UAAUZ;IAChB,MAAMa,QAAQb,SAASe,IAAI,CAAC,MAAM;IAClC,MAAMJ,UAAU,SAAS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OAAO;QACL,gDAAgD;QAChDO,QAAQa,MAAM;IAChB;AACF"}
|
package/dist/esm/tar/sparse.d.ts
CHANGED
|
@@ -78,7 +78,7 @@ export declare class SparseStream extends EntryStream {
|
|
|
78
78
|
* Push data from the tar archive (actual sparse data chunk)
|
|
79
79
|
* Overrides EntryStream.push() to reconstruct sparse file with holes.
|
|
80
80
|
*/
|
|
81
|
-
push(data: Buffer):
|
|
81
|
+
push(data: Buffer | null): boolean;
|
|
82
82
|
/**
|
|
83
83
|
* End the stream - emit any trailing zeros
|
|
84
84
|
* Overrides EntryStream.end() to emit trailing zeros first.
|
package/dist/esm/tar/sparse.js
CHANGED
|
@@ -109,8 +109,11 @@ function getZeroBuffer() {
|
|
|
109
109
|
* Push data from the tar archive (actual sparse data chunk)
|
|
110
110
|
* Overrides EntryStream.push() to reconstruct sparse file with holes.
|
|
111
111
|
*/ push(data) {
|
|
112
|
-
|
|
112
|
+
// Allow null through to signal end
|
|
113
|
+
if (data === null) return super.push(null);
|
|
114
|
+
if (this.ended) return false;
|
|
113
115
|
let dataOffset = 0;
|
|
116
|
+
let result = true;
|
|
114
117
|
while(dataOffset < data.length && this.currentEntry < this.entries.length){
|
|
115
118
|
const entry = this.entries[this.currentEntry];
|
|
116
119
|
// First, emit zeros for any hole before current entry
|
|
@@ -123,7 +126,7 @@ function getZeroBuffer() {
|
|
|
123
126
|
const toEmit = Math.min(this.entryBytesRemaining, data.length - dataOffset);
|
|
124
127
|
if (toEmit > 0) {
|
|
125
128
|
const chunk = data.slice(dataOffset, dataOffset + toEmit);
|
|
126
|
-
super.push(chunk);
|
|
129
|
+
result = super.push(chunk);
|
|
127
130
|
dataOffset += toEmit;
|
|
128
131
|
this.virtualPosition += toEmit;
|
|
129
132
|
this.entryBytesRemaining -= toEmit;
|
|
@@ -136,6 +139,7 @@ function getZeroBuffer() {
|
|
|
136
139
|
}
|
|
137
140
|
}
|
|
138
141
|
}
|
|
142
|
+
return result;
|
|
139
143
|
}
|
|
140
144
|
/**
|
|
141
145
|
* End the stream - emit any trailing zeros
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/tar/sparse.ts"],"sourcesContent":["/**\n * GNU Sparse File Support\n *\n * Handles parsing of GNU sparse file headers and stream reconstruction.\n *\n * GNU sparse format stores:\n * 1. A sparse map in the header (offset 386) with up to 4 entries\n * 2. Extended sparse headers (512-byte blocks) if more entries needed\n * 3. The actual data chunks (only non-zero portions of the file)\n *\n * Each sparse entry contains:\n * - offset: position in the virtual file where this data chunk belongs\n * - numbytes: size of this data chunk\n *\n * Node 0.8 compatible - uses only basic Buffer operations.\n */\n\nimport { SPARSE_ENTRIES_IN_HEADER, SPARSE_ENTRY_NUMBYTES_SIZE, SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_SIZE, SPARSE_EXTENDED_ENTRIES, SPARSE_EXTENDED_ISEXTENDED_OFFSET, SPARSE_ISEXTENDED_OFFSET, SPARSE_OFFSET, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE } from './constants.ts';\nimport EntryStream from './EntryStream.ts';\nimport { decodeOct } from './headers.ts';\n\n// Reusable zero buffer for sparse hole emission (64KB)\nconst ZERO_BUFFER_SIZE = 65536;\nlet zeroBuffer: Buffer | null = null;\n\nfunction getZeroBuffer(): Buffer {\n if (!zeroBuffer) {\n zeroBuffer = new Buffer(ZERO_BUFFER_SIZE);\n for (let i = 0; i < ZERO_BUFFER_SIZE; i++) {\n zeroBuffer[i] = 0;\n }\n }\n return zeroBuffer;\n}\n\n/**\n * Represents a region of actual data in a sparse file\n */\nexport interface SparseEntry {\n /** Offset in the reconstructed (virtual) file */\n offset: number;\n /** Number of bytes of actual data */\n numbytes: number;\n}\n\n/**\n * Parsed sparse header information\n */\nexport interface SparseInfo {\n /** The actual (reconstructed) file size */\n realSize: number;\n /** Sparse map entries */\n entries: SparseEntry[];\n /** Whether there are more entries in extended headers */\n isExtended: boolean;\n}\n\n/**\n * Parse sparse entries from a buffer starting at given offset\n *\n * @param buf - Buffer containing sparse entries\n * @param startOffset - Offset in buffer where entries begin\n * @param maxEntries - Maximum number of entries to read\n * @returns Array of valid sparse entries (stops at first zero entry)\n */\nfunction parseSparseEntries(buf: Buffer, startOffset: number, maxEntries: number): SparseEntry[] {\n const entries: SparseEntry[] = [];\n\n for (let i = 0; i < maxEntries; i++) {\n const entryOffset = startOffset + i * SPARSE_ENTRY_SIZE;\n const offset = decodeOct(buf, entryOffset, SPARSE_ENTRY_OFFSET_SIZE);\n const numbytes = decodeOct(buf, entryOffset + SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_NUMBYTES_SIZE);\n\n // Stop at first zero entry (end of sparse map)\n if (offset === 0 && numbytes === 0) {\n break;\n }\n\n entries.push({ offset, numbytes });\n }\n\n return entries;\n}\n\n/**\n * Parse GNU sparse header information from a tar header block\n *\n * @param headerBuf - The 512-byte tar header buffer\n * @returns Sparse info including real size, entries, and extended flag\n */\nexport function parseGnuSparseHeader(headerBuf: Buffer): SparseInfo {\n // Parse sparse entries from header (up to 4)\n const entries = parseSparseEntries(headerBuf, SPARSE_OFFSET, SPARSE_ENTRIES_IN_HEADER);\n\n // Parse isextended flag\n const isExtended = headerBuf[SPARSE_ISEXTENDED_OFFSET] !== 0;\n\n // Parse real file size\n const realSize = decodeOct(headerBuf, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE);\n\n return { realSize, entries, isExtended };\n}\n\n/**\n * Parse GNU sparse extended header block\n *\n * @param extBuf - The 512-byte extended sparse header buffer\n * @returns Object with entries and whether more extended blocks follow\n */\nexport function parseGnuSparseExtended(extBuf: Buffer): { entries: SparseEntry[]; isExtended: boolean } {\n // Parse sparse entries from extended block (up to 21)\n const entries = parseSparseEntries(extBuf, 0, SPARSE_EXTENDED_ENTRIES);\n\n // Parse isextended flag\n const isExtended = extBuf[SPARSE_EXTENDED_ISEXTENDED_OFFSET] !== 0;\n\n return { entries, isExtended };\n}\n\n/**\n * Calculate total data size from sparse map\n * This is the actual size of data stored in the archive (sum of all numbytes)\n */\nexport function sparseDataSize(entries: SparseEntry[]): number {\n let total = 0;\n for (let i = 0; i < entries.length; i++) {\n total += entries[i].numbytes;\n }\n return total;\n}\n\n/**\n * Stream that reconstructs a sparse file from data chunks\n *\n * Takes the sparse map and actual data, outputs reconstructed file\n * with zeros inserted for holes.\n *\n * Extends EntryStream to inherit pause/resume/pipe behavior.\n *\n * @internal\n */\nexport class SparseStream extends EntryStream {\n private entries: SparseEntry[];\n private realSize: number;\n private currentEntry: number;\n private virtualPosition: number;\n private entryBytesRemaining: number;\n\n constructor(entries: SparseEntry[], realSize: number) {\n super();\n this.entries = entries;\n this.realSize = realSize;\n this.currentEntry = 0;\n this.virtualPosition = 0;\n this.entryBytesRemaining = entries.length > 0 ? entries[0].numbytes : 0;\n }\n\n /**\n * Push data from the tar archive (actual sparse data chunk)\n * Overrides EntryStream.push() to reconstruct sparse file with holes.\n */\n push(data: Buffer): void {\n if (this.ended) return;\n\n let dataOffset = 0;\n\n while (dataOffset < data.length && this.currentEntry < this.entries.length) {\n const entry = this.entries[this.currentEntry];\n\n // First, emit zeros for any hole before current entry\n if (this.virtualPosition < entry.offset) {\n const holeSize = entry.offset - this.virtualPosition;\n this._emitZeros(holeSize);\n this.virtualPosition = entry.offset;\n }\n\n // Now emit actual data for this entry\n const toEmit = Math.min(this.entryBytesRemaining, data.length - dataOffset);\n if (toEmit > 0) {\n const chunk = data.slice(dataOffset, dataOffset + toEmit);\n super.push(chunk);\n dataOffset += toEmit;\n this.virtualPosition += toEmit;\n this.entryBytesRemaining -= toEmit;\n }\n\n // Move to next entry if current is exhausted\n if (this.entryBytesRemaining <= 0) {\n this.currentEntry++;\n if (this.currentEntry < this.entries.length) {\n this.entryBytesRemaining = this.entries[this.currentEntry].numbytes;\n }\n }\n }\n }\n\n /**\n * End the stream - emit any trailing zeros\n * Overrides EntryStream.end() to emit trailing zeros first.\n */\n end(): void {\n if (this.ended) return;\n\n // Emit remaining zeros to reach real file size\n if (this.virtualPosition < this.realSize) {\n this._emitZeros(this.realSize - this.virtualPosition);\n this.virtualPosition = this.realSize;\n }\n\n super.end();\n }\n\n /**\n * Emit zeros for a hole, reusing the shared zero buffer\n */\n private _emitZeros(size: number): void {\n const zeros = getZeroBuffer();\n let remaining = size;\n\n while (remaining > 0) {\n const toEmit = Math.min(remaining, ZERO_BUFFER_SIZE);\n // Slice from the reusable buffer to emit exact size needed\n super.push(zeros.slice(0, toEmit));\n remaining -= toEmit;\n }\n }\n}\n"],"names":["SPARSE_ENTRIES_IN_HEADER","SPARSE_ENTRY_NUMBYTES_SIZE","SPARSE_ENTRY_OFFSET_SIZE","SPARSE_ENTRY_SIZE","SPARSE_EXTENDED_ENTRIES","SPARSE_EXTENDED_ISEXTENDED_OFFSET","SPARSE_ISEXTENDED_OFFSET","SPARSE_OFFSET","SPARSE_REALSIZE_OFFSET","SPARSE_REALSIZE_SIZE","EntryStream","decodeOct","ZERO_BUFFER_SIZE","zeroBuffer","getZeroBuffer","Buffer","i","parseSparseEntries","buf","startOffset","maxEntries","entries","entryOffset","offset","numbytes","push","parseGnuSparseHeader","headerBuf","isExtended","realSize","parseGnuSparseExtended","extBuf","sparseDataSize","total","length","SparseStream","data","ended","dataOffset","currentEntry","entry","virtualPosition","holeSize","_emitZeros","toEmit","Math","min","entryBytesRemaining","chunk","slice","end","size","zeros","remaining"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,wBAAwB,EAAEC,0BAA0B,EAAEC,wBAAwB,EAAEC,iBAAiB,EAAEC,uBAAuB,EAAEC,iCAAiC,EAAEC,wBAAwB,EAAEC,aAAa,EAAEC,sBAAsB,EAAEC,oBAAoB,QAAQ,iBAAiB;AACtR,OAAOC,iBAAiB,mBAAmB;AAC3C,SAASC,SAAS,QAAQ,eAAe;AAEzC,uDAAuD;AACvD,MAAMC,mBAAmB;AACzB,IAAIC,aAA4B;AAEhC,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ACV,IAAI,IAAI,CAACb,KAAK,EAAE;QAEhB,+CAA+C;QAC/C,IAAI,IAAI,CAACI,eAAe,GAAG,IAAI,CAACZ,QAAQ,EAAE;YACxC,IAAI,CAACc,UAAU,CAAC,IAAI,CAACd,QAAQ,GAAG,IAAI,CAACY,eAAe;YACpD,IAAI,CAACA,eAAe,GAAG,IAAI,CAACZ,QAAQ;QACtC;QAEA,KAAK,CAACqB;IACR;IAEA;;GAEC,GACD,AAAQP,WAAWQ,IAAY,EAAQ;QACrC,MAAMC,QAAQtC;QACd,IAAIuC,YAAYF;QAEhB,MAAOE,YAAY,EAAG;YACpB,MAAMT,SAASC,KAAKC,GAAG,CAACO,WAAWzC;YACnC,2DAA2D;YAC3D,KAAK,CAACa,KAAK2B,MAAMH,KAAK,CAAC,GAAGL;YAC1BS,aAAaT;QACf;IACF;IA7EA,YAAYvB,OAAsB,EAAEQ,QAAgB,CAAE;QACpD,KAAK;QACL,IAAI,CAACR,OAAO,GAAGA;QACf,IAAI,CAACQ,QAAQ,GAAGA;QAChB,IAAI,CAACU,YAAY,GAAG;QACpB,IAAI,CAACE,eAAe,GAAG;QACvB,IAAI,CAACM,mBAAmB,GAAG1B,QAAQa,MAAM,GAAG,IAAIb,OAAO,CAAC,EAAE,CAACG,QAAQ,GAAG;IACxE;AAuEF"}
|
|
1
|
+
{"version":3,"sources":["/Users/kevin/Dev/OpenSource/iterators/tar-iterator/src/tar/sparse.ts"],"sourcesContent":["/**\n * GNU Sparse File Support\n *\n * Handles parsing of GNU sparse file headers and stream reconstruction.\n *\n * GNU sparse format stores:\n * 1. A sparse map in the header (offset 386) with up to 4 entries\n * 2. Extended sparse headers (512-byte blocks) if more entries needed\n * 3. The actual data chunks (only non-zero portions of the file)\n *\n * Each sparse entry contains:\n * - offset: position in the virtual file where this data chunk belongs\n * - numbytes: size of this data chunk\n *\n * Node 0.8 compatible - uses only basic Buffer operations.\n */\n\nimport { SPARSE_ENTRIES_IN_HEADER, SPARSE_ENTRY_NUMBYTES_SIZE, SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_SIZE, SPARSE_EXTENDED_ENTRIES, SPARSE_EXTENDED_ISEXTENDED_OFFSET, SPARSE_ISEXTENDED_OFFSET, SPARSE_OFFSET, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE } from './constants.ts';\nimport EntryStream from './EntryStream.ts';\nimport { decodeOct } from './headers.ts';\n\n// Reusable zero buffer for sparse hole emission (64KB)\nconst ZERO_BUFFER_SIZE = 65536;\nlet zeroBuffer: Buffer | null = null;\n\nfunction getZeroBuffer(): Buffer {\n if (!zeroBuffer) {\n zeroBuffer = new Buffer(ZERO_BUFFER_SIZE);\n for (let i = 0; i < ZERO_BUFFER_SIZE; i++) {\n zeroBuffer[i] = 0;\n }\n }\n return zeroBuffer;\n}\n\n/**\n * Represents a region of actual data in a sparse file\n */\nexport interface SparseEntry {\n /** Offset in the reconstructed (virtual) file */\n offset: number;\n /** Number of bytes of actual data */\n numbytes: number;\n}\n\n/**\n * Parsed sparse header information\n */\nexport interface SparseInfo {\n /** The actual (reconstructed) file size */\n realSize: number;\n /** Sparse map entries */\n entries: SparseEntry[];\n /** Whether there are more entries in extended headers */\n isExtended: boolean;\n}\n\n/**\n * Parse sparse entries from a buffer starting at given offset\n *\n * @param buf - Buffer containing sparse entries\n * @param startOffset - Offset in buffer where entries begin\n * @param maxEntries - Maximum number of entries to read\n * @returns Array of valid sparse entries (stops at first zero entry)\n */\nfunction parseSparseEntries(buf: Buffer, startOffset: number, maxEntries: number): SparseEntry[] {\n const entries: SparseEntry[] = [];\n\n for (let i = 0; i < maxEntries; i++) {\n const entryOffset = startOffset + i * SPARSE_ENTRY_SIZE;\n const offset = decodeOct(buf, entryOffset, SPARSE_ENTRY_OFFSET_SIZE);\n const numbytes = decodeOct(buf, entryOffset + SPARSE_ENTRY_OFFSET_SIZE, SPARSE_ENTRY_NUMBYTES_SIZE);\n\n // Stop at first zero entry (end of sparse map)\n if (offset === 0 && numbytes === 0) {\n break;\n }\n\n entries.push({ offset, numbytes });\n }\n\n return entries;\n}\n\n/**\n * Parse GNU sparse header information from a tar header block\n *\n * @param headerBuf - The 512-byte tar header buffer\n * @returns Sparse info including real size, entries, and extended flag\n */\nexport function parseGnuSparseHeader(headerBuf: Buffer): SparseInfo {\n // Parse sparse entries from header (up to 4)\n const entries = parseSparseEntries(headerBuf, SPARSE_OFFSET, SPARSE_ENTRIES_IN_HEADER);\n\n // Parse isextended flag\n const isExtended = headerBuf[SPARSE_ISEXTENDED_OFFSET] !== 0;\n\n // Parse real file size\n const realSize = decodeOct(headerBuf, SPARSE_REALSIZE_OFFSET, SPARSE_REALSIZE_SIZE);\n\n return { realSize, entries, isExtended };\n}\n\n/**\n * Parse GNU sparse extended header block\n *\n * @param extBuf - The 512-byte extended sparse header buffer\n * @returns Object with entries and whether more extended blocks follow\n */\nexport function parseGnuSparseExtended(extBuf: Buffer): { entries: SparseEntry[]; isExtended: boolean } {\n // Parse sparse entries from extended block (up to 21)\n const entries = parseSparseEntries(extBuf, 0, SPARSE_EXTENDED_ENTRIES);\n\n // Parse isextended flag\n const isExtended = extBuf[SPARSE_EXTENDED_ISEXTENDED_OFFSET] !== 0;\n\n return { entries, isExtended };\n}\n\n/**\n * Calculate total data size from sparse map\n * This is the actual size of data stored in the archive (sum of all numbytes)\n */\nexport function sparseDataSize(entries: SparseEntry[]): number {\n let total = 0;\n for (let i = 0; i < entries.length; i++) {\n total += entries[i].numbytes;\n }\n return total;\n}\n\n/**\n * Stream that reconstructs a sparse file from data chunks\n *\n * Takes the sparse map and actual data, outputs reconstructed file\n * with zeros inserted for holes.\n *\n * Extends EntryStream to inherit pause/resume/pipe behavior.\n *\n * @internal\n */\nexport class SparseStream extends EntryStream {\n private entries: SparseEntry[];\n private realSize: number;\n private currentEntry: number;\n private virtualPosition: number;\n private entryBytesRemaining: number;\n\n constructor(entries: SparseEntry[], realSize: number) {\n super();\n this.entries = entries;\n this.realSize = realSize;\n this.currentEntry = 0;\n this.virtualPosition = 0;\n this.entryBytesRemaining = entries.length > 0 ? entries[0].numbytes : 0;\n }\n\n /**\n * Push data from the tar archive (actual sparse data chunk)\n * Overrides EntryStream.push() to reconstruct sparse file with holes.\n */\n push(data: Buffer | null): boolean {\n // Allow null through to signal end\n if (data === null) return super.push(null);\n if (this.ended) return false;\n\n let dataOffset = 0;\n let result = true;\n\n while (dataOffset < data.length && this.currentEntry < this.entries.length) {\n const entry = this.entries[this.currentEntry];\n\n // First, emit zeros for any hole before current entry\n if (this.virtualPosition < entry.offset) {\n const holeSize = entry.offset - this.virtualPosition;\n this._emitZeros(holeSize);\n this.virtualPosition = entry.offset;\n }\n\n // Now emit actual data for this entry\n const toEmit = Math.min(this.entryBytesRemaining, data.length - dataOffset);\n if (toEmit > 0) {\n const chunk = data.slice(dataOffset, dataOffset + toEmit);\n result = super.push(chunk);\n dataOffset += toEmit;\n this.virtualPosition += toEmit;\n this.entryBytesRemaining -= toEmit;\n }\n\n // Move to next entry if current is exhausted\n if (this.entryBytesRemaining <= 0) {\n this.currentEntry++;\n if (this.currentEntry < this.entries.length) {\n this.entryBytesRemaining = this.entries[this.currentEntry].numbytes;\n }\n }\n }\n return result;\n }\n\n /**\n * End the stream - emit any trailing zeros\n * Overrides EntryStream.end() to emit trailing zeros first.\n */\n end(): void {\n if (this.ended) return;\n\n // Emit remaining zeros to reach real file size\n if (this.virtualPosition < this.realSize) {\n this._emitZeros(this.realSize - this.virtualPosition);\n this.virtualPosition = this.realSize;\n }\n\n super.end();\n }\n\n /**\n * Emit zeros for a hole, reusing the shared zero buffer\n */\n private _emitZeros(size: number): void {\n const zeros = getZeroBuffer();\n let remaining = size;\n\n while (remaining > 0) {\n const toEmit = Math.min(remaining, ZERO_BUFFER_SIZE);\n // Slice from the reusable buffer to emit exact size needed\n super.push(zeros.slice(0, toEmit));\n remaining -= toEmit;\n }\n }\n}\n"],"names":["SPARSE_ENTRIES_IN_HEADER","SPARSE_ENTRY_NUMBYTES_SIZE","SPARSE_ENTRY_OFFSET_SIZE","SPARSE_ENTRY_SIZE","SPARSE_EXTENDED_ENTRIES","SPARSE_EXTENDED_ISEXTENDED_OFFSET","SPARSE_ISEXTENDED_OFFSET","SPARSE_OFFSET","SPARSE_REALSIZE_OFFSET","SPARSE_REALSIZE_SIZE","EntryStream","decodeOct","ZERO_BUFFER_SIZE","zeroBuffer","getZeroBuffer","Buffer","i","parseSparseEntries","buf","startOffset","maxEntries","entries","entryOffset","offset","numbytes","push","parseGnuSparseHeader","headerBuf","isExtended","realSize","parseGnuSparseExtended","extBuf","sparseDataSize","total","length","SparseStream","data","ended","dataOffset","result","currentEntry","entry","virtualPosition","holeSize","_emitZeros","toEmit","Math","min","entryBytesRemaining","chunk","slice","end","size","zeros","remaining"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,wBAAwB,EAAEC,0BAA0B,EAAEC,wBAAwB,EAAEC,iBAAiB,EAAEC,uBAAuB,EAAEC,iCAAiC,EAAEC,wBAAwB,EAAEC,aAAa,EAAEC,sBAAsB,EAAEC,oBAAoB,QAAQ,iBAAiB;AACtR,OAAOC,iBAAiB,mBAAmB;AAC3C,SAASC,SAAS,QAAQ,eAAe;AAEzC,uDAAuD;AACvD,MAAMC,mBAAmB;AACzB,IAAIC,aAA4B;AAEhC,SAASC;IACP,IAAI,CAACD,YAAY;QACfA,aAAa,IAAIE,OAAOH;QACxB,IAAK,IAAII,IAAI,GAAGA,IAAIJ,kBAAkBI,IAAK;YACzCH,UAAU,CAACG,EAAE,GAAG;QAClB;IACF;IACA,OAAOH;AACT;AAwBA;;;;;;;CAOC,GACD,SAASI,mBAAmBC,GAAW,EAAEC,WAAmB,EAAEC,UAAkB;IAC9E,MAAMC,UAAyB,EAAE;IAEjC,IAAK,IAAIL,IAAI,GAAGA,IAAII,YAAYJ,IAAK;QACnC,MAAMM,cAAcH,cAAcH,IAAIb;QACtC,MAAMoB,SAASZ,UAAUO,KAAKI,aAAapB;QAC3C,MA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AQ;QACrC,MAAMC,QAAQvC;QACd,IAAIwC,YAAYF;QAEhB,MAAOE,YAAY,EAAG;YACpB,MAAMT,SAASC,KAAKC,GAAG,CAACO,WAAW1C;YACnC,2DAA2D;YAC3D,KAAK,CAACa,KAAK4B,MAAMH,KAAK,CAAC,GAAGL;YAC1BS,aAAaT;QACf;IACF;IAjFA,YAAYxB,OAAsB,EAAEQ,QAAgB,CAAE;QACpD,KAAK;QACL,IAAI,CAACR,OAAO,GAAGA;QACf,IAAI,CAACQ,QAAQ,GAAGA;QAChB,IAAI,CAACW,YAAY,GAAG;QACpB,IAAI,CAACE,eAAe,GAAG;QACvB,IAAI,CAACM,mBAAmB,GAAG3B,QAAQa,MAAM,GAAG,IAAIb,OAAO,CAAC,EAAE,CAACG,QAAQ,GAAG;IACxE;AA2EF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tar-iterator",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Extract contents from tar archive type using an iterator API using streams or paths. Use stream interface and pipe transforms to add decompression algorithms",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"extract",
|