Skip to content

Commit d943b8e

Browse files
committed
Added handling for HTTP 206 responses
See #491
1 parent 1c30821 commit d943b8e

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/waveform-builder.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,30 @@ WaveformBuilder.prototype.init = function(options, callback) {
122122
}
123123
};
124124

125+
function hasValidContentRangeHeader(xhr) {
126+
const contentRange = xhr.getResponseHeader('content-range');
127+
128+
if (!contentRange) {
129+
return false;
130+
}
131+
132+
const matches = contentRange.match(/^bytes (\d+)-(\d+)\/(\d+)$/);
133+
134+
if (matches && matches.length === 4) {
135+
const firstPos = parseInt(matches[1], 10);
136+
const lastPos = parseInt(matches[2], 10);
137+
const length = parseInt(matches[3], 10);
138+
139+
if (firstPos === 0 && (lastPos + 1) === length) {
140+
return true;
141+
}
142+
143+
return false;
144+
}
145+
146+
return false;
147+
}
148+
125149
/* eslint-disable max-len */
126150

127151
/**
@@ -178,7 +202,10 @@ WaveformBuilder.prototype._getRemoteWaveformData = function(options, callback) {
178202
return;
179203
}
180204

181-
if (this.status !== 200) {
205+
// See https://github.com/bbc/peaks.js/issues/491
206+
207+
if (this.status !== 200 &&
208+
!(this.status === 206 && hasValidContentRangeHeader(this))) {
182209
callback(
183210
new Error('Unable to fetch remote data. HTTP status ' + this.status)
184211
);
@@ -368,7 +395,10 @@ WaveformBuilder.prototype._requestAudioAndBuildWaveformData = function(url,
368395
return;
369396
}
370397

371-
if (this.status !== 200) {
398+
// See https://github.com/bbc/peaks.js/issues/491
399+
400+
if (this.status !== 200 &&
401+
!(this.status === 206 && hasValidContentRangeHeader(this))) {
372402
callback(
373403
new Error('Unable to fetch remote data. HTTP status ' + this.status)
374404
);

0 commit comments

Comments
 (0)