Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
stream.content.on('data', (chunk) => {
// console.log('chunk ', chunk)
// if (chunk && chunk.length > 0) {
if (chunk) {
metrics.queue.push(chunk)
metrics.received += chunk.length
const lastHr = (metrics.chunks.length > 0)
? metrics.chunks[metrics.chunks.length - 1].hrtime
: metrics.started
metrics.chunks.push({
time: Date.now(),
hrtime: hrtime(),
bytes: chunk.length,
elaspedSinceLast: utils.duration(lastHr),
rate: utils.speed(metrics.received, utils.duration(metrics.started))
})
metrics.rates = utils.calcRates(metrics.chunks.map(chunk => chunk.rate))
metrics.overallRate = utils.speed(metrics.received, utils.duration(metrics.started))
if (!metrics.started) {
metrics.started = hrtime()
}
if (!streamStarted) {
templateDict.set('status', 'awaiting first fragment (' + (chunksPerAppend - metrics.queue.length + 1) + ' chunks )')
// stream hasn't started yet. this is first fragment.
if (metrics.queue.length - lastChunkIndex === chunksPerAppend) {
// there are enough for first append.
? metrics.chunks[metrics.chunks.length - 1].hrtime
: metrics.started
metrics.chunks.push({
time: Date.now(),
hrtime: hrtime(),
bytes: chunk.length,
elaspedSinceLast: utils.duration(lastHr),
rate: utils.speed(metrics.received, utils.duration(metrics.started))
})
metrics.rates = utils.calcRates(metrics.chunks.map(chunk => chunk.rate))
metrics.overallRate = utils.speed(metrics.received, utils.duration(metrics.started))
if (!metrics.started) {
metrics.started = hrtime()
}
if (!streamStarted) {
templateDict.set('status', 'awaiting first fragment (' + (chunksPerAppend - metrics.queue.length + 1) + ' chunks )')
// stream hasn't started yet. this is first fragment.
if (metrics.queue.length - lastChunkIndex === chunksPerAppend) {
// there are enough for first append.
console.log('first chunk(s) appending')
appendBuffer()
streamStarted = true
templateDict.set('status', 'Buffering Chunks...')
}
}
if (waiting) {
// we were waiting for enough chunks.
// append what we got.