@tokenizer/http
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -1,3 +0,2 @@ | ||
import { IRangeRequestClient, IContentRangeType, IHeadRequestInfo, IRangeRequestResponse } from '@tokenizer/range'; | ||
export declare function parseContentRange(contentRange: string): IContentRangeType; | ||
import { IHeadRequestInfo, IRangeRequestResponse, IRangeRequestClient } from '@tokenizer/range'; | ||
/** | ||
@@ -11,2 +10,3 @@ * Simple HTTP-client, which both works in node.js and browser | ||
private static makeResponse; | ||
resolvedUrl: string; | ||
constructor(url: string); | ||
@@ -13,0 +13,0 @@ getHeadInfo(): Promise<IHeadRequestInfo>; |
@@ -5,21 +5,4 @@ "use strict"; | ||
const _fetch = require("node-fetch"); | ||
const range_1 = require("@tokenizer/range"); // Add 'fetch' API for node.js | ||
const debug = initDebug('streaming-http-token-reader:http-client'); | ||
function parseContentRange(contentRange) { | ||
debug(`_parseContentRang response: contentRange=${contentRange}`); | ||
if (contentRange) { | ||
const parsedContentRange = contentRange.match(/bytes (\d+)-(\d+)\/(?:(\d+)|\*)/i); | ||
if (!parsedContentRange) { | ||
throw new Error('FIXME: Unknown Content-Range syntax: ' + contentRange); | ||
} | ||
return { | ||
firstBytePosition: parseInt(parsedContentRange[1], 10), | ||
lastBytePosition: parseInt(parsedContentRange[2], 10), | ||
instanceLength: parsedContentRange[3] ? parseInt(parsedContentRange[3], 10) : null | ||
}; | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
exports.parseContentRange = parseContentRange; | ||
/** | ||
@@ -38,3 +21,3 @@ * Simple HTTP-client, which both works in node.js and browser | ||
const contentRange = headers.get('Content-Range'); | ||
return parseContentRange(contentRange); | ||
return range_1.parseContentRange(contentRange); | ||
} | ||
@@ -44,2 +27,3 @@ static makeResponse(resp) { | ||
return { | ||
url: resp.url, | ||
size: contentRange ? contentRange.instanceLength : HttpClient.getContentLength(resp.headers), | ||
@@ -51,6 +35,8 @@ mimeType: resp.headers.get('Content-Type'), | ||
} | ||
getHeadInfo() { | ||
return _fetch(this.url, { method: 'HEAD' }).then(resp => HttpClient.makeResponse(resp)); | ||
async getHeadInfo() { | ||
const response = await _fetch(this.url, { method: 'HEAD' }); | ||
this.resolvedUrl = response.url; | ||
return HttpClient.makeResponse(response); | ||
} | ||
getResponse(method, range) { | ||
async getResponse(method, range) { | ||
if (range) { | ||
@@ -64,12 +50,12 @@ debug(`_getResponse ${method} ${range[0]}..${range[1]}`); | ||
headers.set('Range', 'bytes=' + range[0] + '-' + range[1]); | ||
return _fetch(this.url, { method, headers }).then(response => { | ||
if (response.ok) { | ||
return HttpClient.makeResponse(response); | ||
} | ||
else { | ||
throw new Error(`Unexpected HTTP response status=${response.status}`); | ||
} | ||
}); | ||
const response = await _fetch(this.resolvedUrl || this.url, { method, headers }); | ||
if (response.ok) { | ||
this.resolvedUrl = response.url; | ||
return HttpClient.makeResponse(response); | ||
} | ||
else { | ||
throw new Error(`Unexpected HTTP response status=${response.status}`); | ||
} | ||
} | ||
} | ||
exports.HttpClient = HttpClient; |
{ | ||
"name": "@tokenizer/http", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "HTTP tokenizer for Node.js and browser", | ||
@@ -65,3 +65,3 @@ "main": "lib/index.js", | ||
"karma-jasmine": "^2.0.1", | ||
"karma-jasmine-html-reporter": "^1.4.2", | ||
"karma-jasmine-html-reporter": "^1.5.1", | ||
"karma-spec-reporter": "^0.0.32", | ||
@@ -73,6 +73,6 @@ "karma-webpack": "^4.0.2", | ||
"typescript": "^3.7.2", | ||
"webpack": "^4.41.2" | ||
"webpack": "^4.41.5" | ||
}, | ||
"dependencies": { | ||
"@tokenizer/range": "^0.2.0", | ||
"@tokenizer/range": "^0.2.1", | ||
"debug": "^4.1.1", | ||
@@ -79,0 +79,0 @@ "node-fetch": "^2.6.0", |
@@ -16,2 +16,4 @@ [![Build Status](https://travis-ci.org/Borewit/tokenizer-http.svg?branch=master)](https://travis-ci.org/Borewit/tokenizer-http) | ||
The range request mechanism is derived from [@tokenizer/range](https://www.npmjs.com/package/@tokenizer/range). | ||
## Installation | ||
@@ -76,2 +78,6 @@ | ||
## Dependencies | ||
![dependency graph](https://i.imgur.com/35dN4si.png) | ||
## Licence | ||
@@ -78,0 +84,0 @@ |
92
18547
8
269
Updated@tokenizer/range@^0.2.1