|
@@ -21,7 +21,7 @@
|
|
|
const uuid = require('uuid')
|
|
|
const streamBuf = require('stream-buffers')
|
|
|
|
|
|
-const debug = require('debug')('joystream:util:ranges')
|
|
|
+// const debug = require('debug')('joystream:util:ranges')
|
|
|
|
|
|
/*
|
|
|
* Range parsing
|
|
@@ -56,7 +56,7 @@ function parseRange(range) {
|
|
|
*/
|
|
|
function parse(rangeStr) {
|
|
|
const res = {}
|
|
|
- debug('Parse range header value:', rangeStr)
|
|
|
+ // debug('Parse range header value:', rangeStr)
|
|
|
const matches = rangeStr.match(/^(([^\s]+)=)?((?:(?:\d+-\d+|-\d+|\d+-),?)+)$/u)
|
|
|
if (!matches) {
|
|
|
throw new Error(`Not a valid range header: ${rangeStr}`)
|
|
@@ -74,7 +74,7 @@ function parse(rangeStr) {
|
|
|
|
|
|
// Merge ranges into result.
|
|
|
ranges.forEach((newRange) => {
|
|
|
- debug('Found range:', newRange)
|
|
|
+ // debug('Found range:', newRange)
|
|
|
|
|
|
let isMerged = false
|
|
|
for (const i in res.ranges) {
|
|
@@ -82,7 +82,7 @@ function parse(rangeStr) {
|
|
|
|
|
|
// Skip if the new range is fully separate from the old range.
|
|
|
if (oldRange[1] + 1 < newRange[0] || newRange[1] + 1 < oldRange[0]) {
|
|
|
- debug('Range does not overlap with', oldRange)
|
|
|
+ // debug('Range does not overlap with', oldRange)
|
|
|
continue
|
|
|
}
|
|
|
|
|
@@ -92,11 +92,11 @@ function parse(rangeStr) {
|
|
|
const merged = [Math.min(oldRange[0], newRange[0]), Math.max(oldRange[1], newRange[1])]
|
|
|
res.ranges[i] = merged
|
|
|
isMerged = true
|
|
|
- debug('Merged', newRange, 'into', oldRange, 'as', merged)
|
|
|
+ // debug('Merged', newRange, 'into', oldRange, 'as', merged)
|
|
|
}
|
|
|
|
|
|
if (!isMerged) {
|
|
|
- debug('Non-overlapping range!')
|
|
|
+ // debug('Non-overlapping range!')
|
|
|
res.ranges.push(newRange)
|
|
|
}
|
|
|
})
|
|
@@ -110,7 +110,7 @@ function parse(rangeStr) {
|
|
|
return first[0] < second[0] ? -1 : 1
|
|
|
})
|
|
|
|
|
|
- debug('Result of parse is', res)
|
|
|
+ // debug('Result of parse is', res)
|
|
|
return res
|
|
|
}
|
|
|
|
|
@@ -159,9 +159,9 @@ class RangeSender {
|
|
|
this.handlers = {}
|
|
|
this.opened = false
|
|
|
|
|
|
- debug('RangeSender:', this)
|
|
|
+ // debug('RangeSender:', this)
|
|
|
if (opts.ranges) {
|
|
|
- debug('Parsed ranges:', opts.ranges.ranges)
|
|
|
+ // debug('Parsed ranges:', opts.ranges.ranges)
|
|
|
}
|
|
|
|
|
|
// Parameters
|
|
@@ -173,7 +173,7 @@ class RangeSender {
|
|
|
|
|
|
onError(err) {
|
|
|
// Assume hiding the actual error is best, and default to 404.
|
|
|
- debug('Error:', err)
|
|
|
+ // debug('Error:', err)
|
|
|
if (!this.response.headersSent) {
|
|
|
this.response.status(err.code || 404).send({
|
|
|
message: err.message || `File not found: ${this.name}`,
|
|
@@ -185,7 +185,7 @@ class RangeSender {
|
|
|
}
|
|
|
|
|
|
onEnd() {
|
|
|
- debug('End of stream.')
|
|
|
+ // debug('End of stream.')
|
|
|
this.response.end()
|
|
|
if (this.endCallback) {
|
|
|
this.endCallback()
|
|
@@ -195,7 +195,7 @@ class RangeSender {
|
|
|
// **** No ranges
|
|
|
onOpenNoRange() {
|
|
|
// File got opened, so we can set headers/status
|
|
|
- debug('Open succeeded:', this.name, this.type)
|
|
|
+ // debug('Open succeeded:', this.name, this.type)
|
|
|
this.opened = true
|
|
|
|
|
|
this.response.status(200)
|
|
@@ -228,7 +228,7 @@ class RangeSender {
|
|
|
// Next range
|
|
|
this.rangeIndex += 1
|
|
|
if (this.rangeIndex >= this.ranges.ranges.length) {
|
|
|
- debug('Cannot advance range index; we are done.')
|
|
|
+ // debug('Cannot advance range index; we are done.')
|
|
|
return undefined
|
|
|
}
|
|
|
|
|
@@ -276,7 +276,7 @@ class RangeSender {
|
|
|
|
|
|
nextRange() {
|
|
|
if (this.ranges.ranges.length === 1) {
|
|
|
- debug('Cannot start new range; only one requested.')
|
|
|
+ // debug('Cannot start new range; only one requested.')
|
|
|
this.stream.off('data', this.handlers.data)
|
|
|
return false
|
|
|
}
|
|
@@ -294,20 +294,20 @@ class RangeSender {
|
|
|
}
|
|
|
onDataRanges.write('\r\n')
|
|
|
this.response.write(onDataRanges.getContents())
|
|
|
- debug('New range started.')
|
|
|
+ // debug('New range started.')
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
// No headers means we're finishing the last range.
|
|
|
this.response.write(`\r\n--${this.rangeBoundary}--\r\n`)
|
|
|
- debug('End of ranges sent.')
|
|
|
+ // debug('End of ranges sent.')
|
|
|
this.stream.off('data', this.handlers.data)
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
onOpenRanges() {
|
|
|
// File got opened, so we can set headers/status
|
|
|
- debug('Open succeeded:', this.name, this.type)
|
|
|
+ // debug('Open succeeded:', this.name, this.type)
|
|
|
this.opened = true
|
|
|
|
|
|
this.response.header('Accept-Ranges', 'bytes')
|
|
@@ -347,34 +347,34 @@ class RangeSender {
|
|
|
// The simplest optimization would be at ever range start to seek() to the
|
|
|
// start.
|
|
|
const chunkRange = [this.readOffset, this.readOffset + chunk.length - 1]
|
|
|
- debug('= Got chunk with byte range', chunkRange)
|
|
|
+ // debug('= Got chunk with byte range', chunkRange)
|
|
|
while (true) {
|
|
|
let reqRange = this.ranges.ranges[this.rangeIndex]
|
|
|
if (!reqRange) {
|
|
|
break
|
|
|
}
|
|
|
- debug('Current requested range is', reqRange)
|
|
|
+ // debug('Current requested range is', reqRange)
|
|
|
if (!reqRange[1]) {
|
|
|
reqRange = [reqRange[0], Number.MAX_SAFE_INTEGER]
|
|
|
- debug('Treating as', reqRange)
|
|
|
+ // debug('Treating as', reqRange)
|
|
|
}
|
|
|
|
|
|
// No overlap in the chunk and requested range; don't write.
|
|
|
if (chunkRange[1] < reqRange[0] || chunkRange[0] > reqRange[1]) {
|
|
|
- debug('Ignoring chunk; it is out of range.')
|
|
|
+ // debug('Ignoring chunk; it is out of range.')
|
|
|
break
|
|
|
}
|
|
|
|
|
|
// Since there is overlap, find the segment that's entirely within the
|
|
|
// chunk.
|
|
|
const segment = [Math.max(chunkRange[0], reqRange[0]), Math.min(chunkRange[1], reqRange[1])]
|
|
|
- debug('Segment to send within chunk is', segment)
|
|
|
+ // debug('Segment to send within chunk is', segment)
|
|
|
|
|
|
// Normalize the segment to a chunk offset
|
|
|
const start = segment[0] - this.readOffset
|
|
|
const end = segment[1] - this.readOffset
|
|
|
const len = end - start + 1
|
|
|
- debug('Offsets into buffer are', [start, end], 'with length', len)
|
|
|
+ // debug('Offsets into buffer are', [start, end], 'with length', len)
|
|
|
|
|
|
// Write the slice that we want to write. We first create a buffer from the
|
|
|
// chunk. Then we slice a new buffer from the same underlying ArrayBuffer,
|
|
@@ -385,12 +385,12 @@ class RangeSender {
|
|
|
|
|
|
// If the requested range is finished, we should start the next one.
|
|
|
if (reqRange[1] > chunkRange[1]) {
|
|
|
- debug('Chunk is finished, but the requested range is missing bytes.')
|
|
|
+ // debug('Chunk is finished, but the requested range is missing bytes.')
|
|
|
break
|
|
|
}
|
|
|
|
|
|
if (reqRange[1] <= chunkRange[1]) {
|
|
|
- debug('Range is finished.')
|
|
|
+ // debug('Range is finished.')
|
|
|
if (!this.nextRange(segment)) {
|
|
|
break
|
|
|
}
|
|
@@ -424,11 +424,11 @@ class RangeSender {
|
|
|
this.handlers.end = this.onEnd.bind(this)
|
|
|
|
|
|
if (this.ranges) {
|
|
|
- debug('Preparing to handle ranges.')
|
|
|
+ // debug('Preparing to handle ranges.')
|
|
|
this.handlers.open = this.onOpenRanges.bind(this)
|
|
|
this.handlers.data = this.onDataRanges.bind(this)
|
|
|
} else {
|
|
|
- debug('No ranges, just send the whole file.')
|
|
|
+ // debug('No ranges, just send the whole file.')
|
|
|
this.handlers.open = this.onOpenNoRange.bind(this)
|
|
|
this.handlers.data = this.onDataNoRange.bind(this)
|
|
|
}
|