UNPKG

3.22 kBSource Map (JSON)View Raw
1{"version":3,"file":"virtual-buffer.js","names":["VirtualBuffer","constructor","readStream","_readStream","on","_tryToMoveQueue","error","_queue","forEach","e","reject","length","Error","position","queue","tryToRead","shift","readChunk","label","promise","Promise","resolve","Buffer","alloc","push","result","read","exports"],"sources":["../src/virtual-buffer.js"],"sourcesContent":["export class VirtualBuffer {\n constructor(readStream) {\n this._readStream = readStream\n readStream.on('readable', () => {\n this._tryToMoveQueue()\n })\n readStream.on('error', error => {\n this._queue.forEach(e => e.reject(error))\n this._queue.length = 0\n })\n readStream.on('end', () => {\n const error = new Error('stream ended')\n this._queue.forEach(e => e.reject(error))\n this._queue.length = 0\n })\n this._queue = []\n this.position = 0\n }\n\n _tryToMoveQueue() {\n const queue = this._queue\n while (queue.length > 0 && queue[0].tryToRead()) {\n queue.shift()\n }\n }\n\n async readChunk(length, label) {\n const promise = new Promise((resolve, reject) => {\n if (length === 0) {\n resolve(Buffer.alloc(0))\n } else {\n this._queue.push({\n reject,\n // putting the label in the _queue, it's useful when debugging.\n label,\n tryToRead: () => {\n const result = this._readStream.read(length)\n if (result !== null) {\n this.position += result.length\n resolve(result)\n }\n return !!result\n },\n })\n }\n })\n this._tryToMoveQueue()\n return promise\n }\n}\n"],"mappings":";;;;;;AAAO,MAAMA,aAAa,CAAC;EACzBC,WAAWA,CAACC,UAAU,EAAE;IACtB,IAAI,CAACC,WAAW,GAAGD,UAAU;IAC7BA,UAAU,CAACE,EAAE,CAAC,UAAU,EAAE,MAAM;MAC9B,IAAI,CAACC,eAAe,CAAC,CAAC;IACxB,CAAC,CAAC;IACFH,UAAU,CAACE,EAAE,CAAC,OAAO,EAAEE,KAAK,IAAI;MAC9B,IAAI,CAACC,MAAM,CAACC,OAAO,CAACC,CAAC,IAAIA,CAAC,CAACC,MAAM,CAACJ,KAAK,CAAC,CAAC;MACzC,IAAI,CAACC,MAAM,CAACI,MAAM,GAAG,CAAC;IACxB,CAAC,CAAC;IACFT,UAAU,C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