Skip to content

Commit

Permalink
Making parseContentRange more error tolerant
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinius committed Mar 29, 2021
1 parent 34f2c48 commit 5c1d9aa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/source/client/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class FetchClient extends BaseClient {

async request({ headers, credentials, signal } = {}) {
const response = await fetch(this.url, {
headers, credentials, signal
headers, credentials, signal,
});
return new FetchResponse(response);
}
Expand Down
22 changes: 14 additions & 8 deletions src/source/httputils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ function parseHeaders(text) {
* @returns {Object} the parsed content type with the fields: type and params
*/
export function parseContentType(rawContentType) {
const [type, ...rawParams] = rawContentType.split(';').map(s => s.trim());
const paramsItems = rawParams.map(param => param.split('='));
const [type, ...rawParams] = rawContentType.split(';').map((s) => s.trim());
const paramsItems = rawParams.map((param) => param.split('='));
return { type, params: itemsToObject(paramsItems) };
}

Expand All @@ -49,12 +49,18 @@ export function parseContentType(rawContentType) {
* @returns {Object} the parsed parts
*/
export function parseContentRange(rawContentRange) {
const [, start, end, total] = rawContentRange.match(/bytes (\d+)-(\d+)\/(\d+)/);
return {
start: parseInt(start, 10),
end: parseInt(end, 10),
total: parseInt(total, 10),
};
let start;
let end;
let total;

if (rawContentRange) {
[, start, end, total] = rawContentRange.match(/bytes (\d+)-(\d+)\/(\d+)/);
start = parseInt(start, 10);
end = parseInt(end, 10);
total = parseInt(total, 10);
}

return { start, end, total };
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/source/remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RemoteSource extends BaseSource {
}`,
},
signal,
})
});

if (!response.ok) {
throw new Error('Error fetching data.');
Expand Down

0 comments on commit 5c1d9aa

Please sign in to comment.