UNPKG

extract-base-iterator

Version:

Base iterator for extract iterators like tar-iterator and zip-iterator

48 lines (47 loc) 1.37 kB
/** * EntryStream - Simple readable stream for entry content * * Extends Readable to emit 'data', 'end', 'error' events. * Node 0.8 compatible via readable-stream polyfill. * * This base class is designed to be used by: * - zip-iterator * - 7z-iterator * - tar-iterator * - Any other archive iterator library */ import Stream from 'stream'; // Use native streams when available, readable-stream only for Node 0.x const major = +process.versions.node.split('.')[0]; let ReadableBase; if (major > 0) { ReadableBase = Stream.Readable; } else { ReadableBase = require('readable-stream').Readable; } let EntryStream = class EntryStream extends ReadableBase { /** * Signal end of stream by pushing null */ end() { if (this._ended) return; this._ended = true; this.push(null); } /** * Check if stream has ended */ get ended() { return this._ended; } /** * Required by Readable - called when consumer wants data. * Data is pushed externally via push(), nothing to do here. */ _read(_size) { // Data is pushed externally, nothing to do here } constructor(...args){ super(...args), this._ended = false; } }; /** * Base stream class for archive entry content. * Can be extended for special entry types like sparse files. */ export { EntryStream as default };