UNPKG

@tokenizer/http

Version:

HTTP tokenizer for Node.js and browser

43 lines 1.59 kB
import { parseContentRange as rangeParseContentRange } from '@tokenizer/range'; export class ResponseInfo { constructor(response) { this.response = response; } getContentLength() { const contentLength = this.response.headers.get('Content-Length'); return contentLength ? Number.parseInt(contentLength, 10) : undefined; } getAcceptRangesHeaderValue() { return this.response.headers.get('Accept-Ranges'); } getContentType() { return this.response.headers.get('Content-Type'); } acceptRanges() { const value = this.getAcceptRangesHeaderValue(); return value !== null && value.trim().toLowerCase() === 'bytes'; } getContentRange() { const contentRange = this.response.headers.get('Content-Range'); if (!contentRange) { return; } return rangeParseContentRange(contentRange); } toRangeRequestResponse() { const contentRange = this.getContentRange(); const size = contentRange ? contentRange.instanceLength : this.getContentLength(); if (typeof size !== 'number') { throw new Error('Could not determine file-size from HTTP response'); } return { url: this.response.url, size, mimeType: this.getContentType() ?? undefined, acceptPartialRequests: this.acceptRanges(), contentRange, arrayBuffer: () => this.response.arrayBuffer().then(res => new Uint8Array(res)) }; } } //# sourceMappingURL=response-info.js.map