mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-04-13 08:44:02 +02:00
Given that there's no less than *five* different, but very similar, implementations this helps reduce code duplication and simplifies maintenance. Also, remove the `rangeChunkSize` not defined checks in all the relevant stream-constructor implementations. Note how the API, since some time, always validates *and* provides that parameter when creating a `BasePDFStreamReader`-instance.
196 lines
5.5 KiB
JavaScript
196 lines
5.5 KiB
JavaScript
/* Copyright 2012 Mozilla Foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import { AbortException, warn } from "../shared/util.js";
|
|
import {
|
|
BasePDFStream,
|
|
BasePDFStreamReader,
|
|
} from "../shared/base_pdf_stream.js";
|
|
import {
|
|
createHeaders,
|
|
createResponseError,
|
|
extractFilenameFromHeader,
|
|
getResponseOrigin,
|
|
validateRangeRequestCapabilities,
|
|
validateResponseStatus,
|
|
} from "./network_utils.js";
|
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|
throw new Error(
|
|
'Module "./fetch_stream.js" shall not be used with MOZCENTRAL builds.'
|
|
);
|
|
}
|
|
|
|
function fetchUrl(url, headers, withCredentials, abortController) {
|
|
return fetch(url, {
|
|
method: "GET",
|
|
headers,
|
|
signal: abortController.signal,
|
|
mode: "cors",
|
|
credentials: withCredentials ? "include" : "same-origin",
|
|
redirect: "follow",
|
|
});
|
|
}
|
|
|
|
function getArrayBuffer(val) {
|
|
if (val instanceof Uint8Array) {
|
|
return val.buffer;
|
|
}
|
|
if (val instanceof ArrayBuffer) {
|
|
return val;
|
|
}
|
|
warn(`getArrayBuffer - unexpected data format: ${val}`);
|
|
return new Uint8Array(val).buffer;
|
|
}
|
|
|
|
class PDFFetchStream extends BasePDFStream {
|
|
_responseOrigin = null;
|
|
|
|
constructor(source) {
|
|
super(source, PDFFetchStreamReader, PDFFetchStreamRangeReader);
|
|
this.isHttp = /^https?:/i.test(source.url);
|
|
this.headers = createHeaders(this.isHttp, source.httpHeaders);
|
|
}
|
|
}
|
|
|
|
class PDFFetchStreamReader extends BasePDFStreamReader {
|
|
_abortController = new AbortController();
|
|
|
|
_reader = null;
|
|
|
|
constructor(stream) {
|
|
super(stream);
|
|
const {
|
|
disableRange,
|
|
disableStream,
|
|
length,
|
|
rangeChunkSize,
|
|
url,
|
|
withCredentials,
|
|
} = stream._source;
|
|
|
|
this._contentLength = length;
|
|
this._isStreamingSupported = !disableStream;
|
|
this._isRangeSupported = !disableRange;
|
|
// Always create a copy of the headers.
|
|
const headers = new Headers(stream.headers);
|
|
|
|
fetchUrl(url, headers, withCredentials, this._abortController)
|
|
.then(response => {
|
|
stream._responseOrigin = getResponseOrigin(response.url);
|
|
|
|
if (!validateResponseStatus(response.status)) {
|
|
throw createResponseError(response.status, url);
|
|
}
|
|
this._reader = response.body.getReader();
|
|
|
|
const responseHeaders = response.headers;
|
|
|
|
const { allowRangeRequests, suggestedLength } =
|
|
validateRangeRequestCapabilities({
|
|
responseHeaders,
|
|
isHttp: stream.isHttp,
|
|
rangeChunkSize,
|
|
disableRange,
|
|
});
|
|
|
|
this._isRangeSupported = allowRangeRequests;
|
|
// Setting right content length.
|
|
this._contentLength = suggestedLength || this._contentLength;
|
|
|
|
this._filename = extractFilenameFromHeader(responseHeaders);
|
|
|
|
// We need to stop reading when range is supported and streaming is
|
|
// disabled.
|
|
if (!this._isStreamingSupported && this._isRangeSupported) {
|
|
this.cancel(new AbortException("Streaming is disabled."));
|
|
}
|
|
|
|
this._headersCapability.resolve();
|
|
})
|
|
.catch(this._headersCapability.reject);
|
|
}
|
|
|
|
async read() {
|
|
await this._headersCapability.promise;
|
|
const { value, done } = await this._reader.read();
|
|
if (done) {
|
|
return { value, done };
|
|
}
|
|
this._loaded += value.byteLength;
|
|
this.onProgress?.({
|
|
loaded: this._loaded,
|
|
total: this._contentLength,
|
|
});
|
|
|
|
return { value: getArrayBuffer(value), done: false };
|
|
}
|
|
|
|
cancel(reason) {
|
|
this._reader?.cancel(reason);
|
|
this._abortController.abort();
|
|
}
|
|
}
|
|
|
|
/** @implements {IPDFStreamRangeReader} */
|
|
class PDFFetchStreamRangeReader {
|
|
constructor(stream, begin, end) {
|
|
this._stream = stream;
|
|
this._reader = null;
|
|
const source = stream._source;
|
|
this._withCredentials = source.withCredentials || false;
|
|
this._readCapability = Promise.withResolvers();
|
|
|
|
this._abortController = new AbortController();
|
|
// Always create a copy of the headers.
|
|
const headers = new Headers(stream.headers);
|
|
headers.append("Range", `bytes=${begin}-${end - 1}`);
|
|
|
|
const url = source.url;
|
|
fetchUrl(url, headers, this._withCredentials, this._abortController)
|
|
.then(response => {
|
|
const responseOrigin = getResponseOrigin(response.url);
|
|
|
|
if (responseOrigin !== stream._responseOrigin) {
|
|
throw new Error(
|
|
`Expected range response-origin "${responseOrigin}" to match "${stream._responseOrigin}".`
|
|
);
|
|
}
|
|
if (!validateResponseStatus(response.status)) {
|
|
throw createResponseError(response.status, url);
|
|
}
|
|
this._readCapability.resolve();
|
|
this._reader = response.body.getReader();
|
|
})
|
|
.catch(this._readCapability.reject);
|
|
}
|
|
|
|
async read() {
|
|
await this._readCapability.promise;
|
|
const { value, done } = await this._reader.read();
|
|
if (done) {
|
|
return { value, done };
|
|
}
|
|
return { value: getArrayBuffer(value), done: false };
|
|
}
|
|
|
|
cancel(reason) {
|
|
this._reader?.cancel(reason);
|
|
this._abortController.abort();
|
|
}
|
|
}
|
|
|
|
export { PDFFetchStream };
|