2017-08-10 16:30:54 +09:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2017-08-31 00:53:38 +09:00
|
|
|
import {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
AbortException,
|
|
|
|
assert,
|
2022-03-08 01:41:41 +09:00
|
|
|
PromiseCapability,
|
[api-minor] Enabling transferring of data fetched with the `PDFFetchStream` implementation
Note how in the API we're transferring the PDF data that's fetched over the network[1]:
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2467-L2480
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2553-L2564
To support that functionality we have the `PDFDataTransportStream`, `PDFFetchStream`, `PDFNetworkStream`, and `PDFNodeStream` implementations. Here these stream-implementations vary slightly in how they handle `ArrayBuffer`s internally, w.r.t. transferring or copying the data:
- In `PDFDataTransportStream` we optionally, after PR 15908, allow transferring of the PDF data as provided externally (used e.g. in the Firefox PDF Viewer).
- In `PDFFetchStream` we're currenly always copying the PDF data returned by the Fetch API, which seems unnecessary. As discussed in PR 15908, it'd seem very weird if this sort of browser API didn't allow transferring of the returned data.
- In `PDFNetworkStream` we're already, since many years, transferring the PDF data returned by the `XMLHttpRequest` functionality. Note how the `getArrayBuffer` helper function simply returns an `ArrayBuffer` response as-is.
- In `PDFNodeStream` we're currently copying the PDF data, however this is unfortunately necessary since Node.js returns data as a `Buffer` object[2].
Given that the `PDFNetworkStream` has been, indirectly, supporting transferring of PDF data for years it would seem really strange if this didn't also apply to the `PDFFetchStream`-implementation.
Hence this patch simply enables transferring of PDF data, when accessed using the Fetch API, unconditionally to help reduced main-thread memory usage since the `PDFFetchStream`-implementation is used *by default* in browsers (for the GENERIC build).
---
[1] As opposed to PDF data being provided as e.g. a TypedArray when calling `getDocument` in the API.
[2] This is a "special" Node.js object, see https://nodejs.org/api/buffer.html#buffer, which doesn't exist in browsers.
2023-01-12 06:03:36 +09:00
|
|
|
warn,
|
2020-01-02 20:00:16 +09:00
|
|
|
} from "../shared/util.js";
|
2017-08-10 16:30:54 +09:00
|
|
|
import {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
createResponseStatusError,
|
|
|
|
extractFilenameFromHeader,
|
|
|
|
validateRangeRequestCapabilities,
|
|
|
|
validateResponseStatus,
|
2020-01-02 20:00:16 +09:00
|
|
|
} from "./network_utils.js";
|
2017-08-10 16:30:54 +09:00
|
|
|
|
2020-05-19 22:10:05 +09:00
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|
|
|
throw new Error(
|
|
|
|
'Module "./fetch_stream.js" shall not be used with MOZCENTRAL builds.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-04 05:28:34 +09:00
|
|
|
function createFetchOptions(headers, withCredentials, abortController) {
|
2017-08-10 16:30:54 +09:00
|
|
|
return {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
method: "GET",
|
2017-08-10 16:30:54 +09:00
|
|
|
headers,
|
2022-08-24 19:10:07 +09:00
|
|
|
signal: abortController.signal,
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
mode: "cors",
|
|
|
|
credentials: withCredentials ? "include" : "same-origin",
|
|
|
|
redirect: "follow",
|
2017-08-10 16:30:54 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-23 06:04:25 +09:00
|
|
|
function createHeaders(httpHeaders) {
|
|
|
|
const headers = new Headers();
|
|
|
|
for (const property in httpHeaders) {
|
|
|
|
const value = httpHeaders[property];
|
2022-11-28 01:16:46 +09:00
|
|
|
if (value === undefined) {
|
2019-12-23 06:04:25 +09:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
headers.append(property, value);
|
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
[api-minor] Enabling transferring of data fetched with the `PDFFetchStream` implementation
Note how in the API we're transferring the PDF data that's fetched over the network[1]:
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2467-L2480
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2553-L2564
To support that functionality we have the `PDFDataTransportStream`, `PDFFetchStream`, `PDFNetworkStream`, and `PDFNodeStream` implementations. Here these stream-implementations vary slightly in how they handle `ArrayBuffer`s internally, w.r.t. transferring or copying the data:
- In `PDFDataTransportStream` we optionally, after PR 15908, allow transferring of the PDF data as provided externally (used e.g. in the Firefox PDF Viewer).
- In `PDFFetchStream` we're currenly always copying the PDF data returned by the Fetch API, which seems unnecessary. As discussed in PR 15908, it'd seem very weird if this sort of browser API didn't allow transferring of the returned data.
- In `PDFNetworkStream` we're already, since many years, transferring the PDF data returned by the `XMLHttpRequest` functionality. Note how the `getArrayBuffer` helper function simply returns an `ArrayBuffer` response as-is.
- In `PDFNodeStream` we're currently copying the PDF data, however this is unfortunately necessary since Node.js returns data as a `Buffer` object[2].
Given that the `PDFNetworkStream` has been, indirectly, supporting transferring of PDF data for years it would seem really strange if this didn't also apply to the `PDFFetchStream`-implementation.
Hence this patch simply enables transferring of PDF data, when accessed using the Fetch API, unconditionally to help reduced main-thread memory usage since the `PDFFetchStream`-implementation is used *by default* in browsers (for the GENERIC build).
---
[1] As opposed to PDF data being provided as e.g. a TypedArray when calling `getDocument` in the API.
[2] This is a "special" Node.js object, see https://nodejs.org/api/buffer.html#buffer, which doesn't exist in browsers.
2023-01-12 06:03:36 +09:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:13:55 +09:00
|
|
|
/** @implements {IPDFStream} */
|
2017-08-10 16:30:54 +09:00
|
|
|
class PDFFetchStream {
|
2017-10-16 22:52:20 +09:00
|
|
|
constructor(source) {
|
|
|
|
this.source = source;
|
|
|
|
this.isHttp = /^https?:/i.test(source.url);
|
|
|
|
this.httpHeaders = (this.isHttp && source.httpHeaders) || {};
|
2017-08-10 16:30:54 +09:00
|
|
|
|
|
|
|
this._fullRequestReader = null;
|
|
|
|
this._rangeRequestReaders = [];
|
|
|
|
}
|
|
|
|
|
2019-03-28 01:54:05 +09:00
|
|
|
get _progressiveDataLength() {
|
2021-01-20 00:28:47 +09:00
|
|
|
return this._fullRequestReader?._loaded ?? 0;
|
2019-03-28 01:54:05 +09:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:54 +09:00
|
|
|
getFullReader() {
|
2020-05-05 19:40:01 +09:00
|
|
|
assert(
|
|
|
|
!this._fullRequestReader,
|
|
|
|
"PDFFetchStream.getFullReader can only be called once."
|
|
|
|
);
|
2017-08-10 16:30:54 +09:00
|
|
|
this._fullRequestReader = new PDFFetchStreamReader(this);
|
|
|
|
return this._fullRequestReader;
|
|
|
|
}
|
|
|
|
|
|
|
|
getRangeReader(begin, end) {
|
2019-03-28 01:54:05 +09:00
|
|
|
if (end <= this._progressiveDataLength) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-05-16 16:13:55 +09:00
|
|
|
const reader = new PDFFetchStreamRangeReader(this, begin, end);
|
2017-08-10 16:30:54 +09:00
|
|
|
this._rangeRequestReaders.push(reader);
|
|
|
|
return reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelAllRequests(reason) {
|
2022-09-05 22:36:04 +09:00
|
|
|
this._fullRequestReader?.cancel(reason);
|
|
|
|
|
2021-04-24 19:36:01 +09:00
|
|
|
for (const reader of this._rangeRequestReaders.slice(0)) {
|
2017-08-10 16:30:54 +09:00
|
|
|
reader.cancel(reason);
|
2021-04-24 19:36:01 +09:00
|
|
|
}
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:13:55 +09:00
|
|
|
/** @implements {IPDFStreamReader} */
|
2017-08-10 16:30:54 +09:00
|
|
|
class PDFFetchStreamReader {
|
|
|
|
constructor(stream) {
|
|
|
|
this._stream = stream;
|
|
|
|
this._reader = null;
|
|
|
|
this._loaded = 0;
|
2018-01-17 00:24:36 +09:00
|
|
|
this._filename = null;
|
2019-05-16 16:13:55 +09:00
|
|
|
const source = stream.source;
|
|
|
|
this._withCredentials = source.withCredentials || false;
|
2017-10-16 22:52:20 +09:00
|
|
|
this._contentLength = source.length;
|
2022-03-08 01:41:41 +09:00
|
|
|
this._headersCapability = new PromiseCapability();
|
2018-02-09 21:39:38 +09:00
|
|
|
this._disableRange = source.disableRange || false;
|
2017-10-16 22:52:20 +09:00
|
|
|
this._rangeChunkSize = source.rangeChunkSize;
|
2017-08-10 16:30:54 +09:00
|
|
|
if (!this._rangeChunkSize && !this._disableRange) {
|
|
|
|
this._disableRange = true;
|
|
|
|
}
|
|
|
|
|
2022-08-24 19:10:07 +09:00
|
|
|
this._abortController = new AbortController();
|
2017-10-16 22:52:20 +09:00
|
|
|
this._isStreamingSupported = !source.disableStream;
|
2018-02-09 21:39:38 +09:00
|
|
|
this._isRangeSupported = !source.disableRange;
|
2017-08-10 16:30:54 +09:00
|
|
|
|
2019-12-23 06:04:25 +09:00
|
|
|
this._headers = createHeaders(this._stream.httpHeaders);
|
2017-08-10 16:30:54 +09:00
|
|
|
|
2019-05-16 16:13:55 +09:00
|
|
|
const url = source.url;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
fetch(
|
|
|
|
url,
|
|
|
|
createFetchOptions(
|
|
|
|
this._headers,
|
|
|
|
this._withCredentials,
|
|
|
|
this._abortController
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.then(response => {
|
|
|
|
if (!validateResponseStatus(response.status)) {
|
|
|
|
throw createResponseStatusError(response.status, url);
|
|
|
|
}
|
|
|
|
this._reader = response.body.getReader();
|
|
|
|
this._headersCapability.resolve();
|
|
|
|
|
|
|
|
const getResponseHeader = name => {
|
|
|
|
return response.headers.get(name);
|
|
|
|
};
|
2021-05-16 17:58:34 +09:00
|
|
|
const { allowRangeRequests, suggestedLength } =
|
|
|
|
validateRangeRequestCapabilities({
|
|
|
|
getResponseHeader,
|
|
|
|
isHttp: this._stream.isHttp,
|
|
|
|
rangeChunkSize: this._rangeChunkSize,
|
|
|
|
disableRange: this._disableRange,
|
|
|
|
});
|
2017-08-10 16:30:54 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._isRangeSupported = allowRangeRequests;
|
|
|
|
// Setting right content length.
|
|
|
|
this._contentLength = suggestedLength || this._contentLength;
|
2018-01-17 00:24:36 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._filename = extractFilenameFromHeader(getResponseHeader);
|
2017-08-31 00:53:38 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
// 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."));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(this._headersCapability.reject);
|
2017-08-10 16:30:54 +09:00
|
|
|
|
|
|
|
this.onProgress = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get headersReady() {
|
|
|
|
return this._headersCapability.promise;
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:24:36 +09:00
|
|
|
get filename() {
|
|
|
|
return this._filename;
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
|
2018-01-17 00:24:36 +09:00
|
|
|
get contentLength() {
|
|
|
|
return this._contentLength;
|
2018-01-13 17:01:50 +09:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:30:54 +09:00
|
|
|
get isRangeSupported() {
|
|
|
|
return this._isRangeSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isStreamingSupported() {
|
|
|
|
return this._isStreamingSupported;
|
|
|
|
}
|
|
|
|
|
2018-07-30 20:58:09 +09:00
|
|
|
async read() {
|
|
|
|
await this._headersCapability.promise;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const { value, done } = await this._reader.read();
|
2018-07-30 20:58:09 +09:00
|
|
|
if (done) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
return { value, done };
|
2018-07-30 20:58:09 +09:00
|
|
|
}
|
|
|
|
this._loaded += value.byteLength;
|
2022-09-21 23:47:36 +09:00
|
|
|
this.onProgress?.({
|
|
|
|
loaded: this._loaded,
|
|
|
|
total: this._contentLength,
|
|
|
|
});
|
|
|
|
|
[api-minor] Enabling transferring of data fetched with the `PDFFetchStream` implementation
Note how in the API we're transferring the PDF data that's fetched over the network[1]:
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2467-L2480
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2553-L2564
To support that functionality we have the `PDFDataTransportStream`, `PDFFetchStream`, `PDFNetworkStream`, and `PDFNodeStream` implementations. Here these stream-implementations vary slightly in how they handle `ArrayBuffer`s internally, w.r.t. transferring or copying the data:
- In `PDFDataTransportStream` we optionally, after PR 15908, allow transferring of the PDF data as provided externally (used e.g. in the Firefox PDF Viewer).
- In `PDFFetchStream` we're currenly always copying the PDF data returned by the Fetch API, which seems unnecessary. As discussed in PR 15908, it'd seem very weird if this sort of browser API didn't allow transferring of the returned data.
- In `PDFNetworkStream` we're already, since many years, transferring the PDF data returned by the `XMLHttpRequest` functionality. Note how the `getArrayBuffer` helper function simply returns an `ArrayBuffer` response as-is.
- In `PDFNodeStream` we're currently copying the PDF data, however this is unfortunately necessary since Node.js returns data as a `Buffer` object[2].
Given that the `PDFNetworkStream` has been, indirectly, supporting transferring of PDF data for years it would seem really strange if this didn't also apply to the `PDFFetchStream`-implementation.
Hence this patch simply enables transferring of PDF data, when accessed using the Fetch API, unconditionally to help reduced main-thread memory usage since the `PDFFetchStream`-implementation is used *by default* in browsers (for the GENERIC build).
---
[1] As opposed to PDF data being provided as e.g. a TypedArray when calling `getDocument` in the API.
[2] This is a "special" Node.js object, see https://nodejs.org/api/buffer.html#buffer, which doesn't exist in browsers.
2023-01-12 06:03:36 +09:00
|
|
|
return { value: getArrayBuffer(value), done: false };
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel(reason) {
|
2022-09-05 22:36:04 +09:00
|
|
|
this._reader?.cancel(reason);
|
2022-08-24 19:10:07 +09:00
|
|
|
this._abortController.abort();
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:13:55 +09:00
|
|
|
/** @implements {IPDFStreamRangeReader} */
|
2017-08-10 16:30:54 +09:00
|
|
|
class PDFFetchStreamRangeReader {
|
|
|
|
constructor(stream, begin, end) {
|
|
|
|
this._stream = stream;
|
|
|
|
this._reader = null;
|
|
|
|
this._loaded = 0;
|
2019-05-16 16:13:55 +09:00
|
|
|
const source = stream.source;
|
|
|
|
this._withCredentials = source.withCredentials || false;
|
2022-03-08 01:41:41 +09:00
|
|
|
this._readCapability = new PromiseCapability();
|
2017-10-16 22:52:20 +09:00
|
|
|
this._isStreamingSupported = !source.disableStream;
|
2017-08-10 16:30:54 +09:00
|
|
|
|
2022-08-24 19:10:07 +09:00
|
|
|
this._abortController = new AbortController();
|
2019-12-23 06:04:25 +09:00
|
|
|
this._headers = createHeaders(this._stream.httpHeaders);
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._headers.append("Range", `bytes=${begin}-${end - 1}`);
|
2017-08-10 16:30:54 +09:00
|
|
|
|
2019-05-16 16:13:55 +09:00
|
|
|
const url = source.url;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
fetch(
|
|
|
|
url,
|
|
|
|
createFetchOptions(
|
|
|
|
this._headers,
|
|
|
|
this._withCredentials,
|
|
|
|
this._abortController
|
|
|
|
)
|
2020-07-29 00:03:50 +09:00
|
|
|
)
|
|
|
|
.then(response => {
|
|
|
|
if (!validateResponseStatus(response.status)) {
|
|
|
|
throw createResponseStatusError(response.status, url);
|
|
|
|
}
|
|
|
|
this._readCapability.resolve();
|
|
|
|
this._reader = response.body.getReader();
|
|
|
|
})
|
2021-07-03 01:36:34 +09:00
|
|
|
.catch(this._readCapability.reject);
|
2017-08-10 16:30:54 +09:00
|
|
|
|
|
|
|
this.onProgress = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isStreamingSupported() {
|
|
|
|
return this._isStreamingSupported;
|
|
|
|
}
|
|
|
|
|
2018-07-30 20:58:09 +09:00
|
|
|
async read() {
|
|
|
|
await this._readCapability.promise;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const { value, done } = await this._reader.read();
|
2018-07-30 20:58:09 +09:00
|
|
|
if (done) {
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
return { value, done };
|
2018-07-30 20:58:09 +09:00
|
|
|
}
|
|
|
|
this._loaded += value.byteLength;
|
2022-09-05 22:36:04 +09:00
|
|
|
this.onProgress?.({ loaded: this._loaded });
|
|
|
|
|
[api-minor] Enabling transferring of data fetched with the `PDFFetchStream` implementation
Note how in the API we're transferring the PDF data that's fetched over the network[1]:
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2467-L2480
- https://github.com/mozilla/pdf.js/blob/f28bf23a314e36d9e255037f5716ae1eb8e16fbf/src/display/api.js#L2553-L2564
To support that functionality we have the `PDFDataTransportStream`, `PDFFetchStream`, `PDFNetworkStream`, and `PDFNodeStream` implementations. Here these stream-implementations vary slightly in how they handle `ArrayBuffer`s internally, w.r.t. transferring or copying the data:
- In `PDFDataTransportStream` we optionally, after PR 15908, allow transferring of the PDF data as provided externally (used e.g. in the Firefox PDF Viewer).
- In `PDFFetchStream` we're currenly always copying the PDF data returned by the Fetch API, which seems unnecessary. As discussed in PR 15908, it'd seem very weird if this sort of browser API didn't allow transferring of the returned data.
- In `PDFNetworkStream` we're already, since many years, transferring the PDF data returned by the `XMLHttpRequest` functionality. Note how the `getArrayBuffer` helper function simply returns an `ArrayBuffer` response as-is.
- In `PDFNodeStream` we're currently copying the PDF data, however this is unfortunately necessary since Node.js returns data as a `Buffer` object[2].
Given that the `PDFNetworkStream` has been, indirectly, supporting transferring of PDF data for years it would seem really strange if this didn't also apply to the `PDFFetchStream`-implementation.
Hence this patch simply enables transferring of PDF data, when accessed using the Fetch API, unconditionally to help reduced main-thread memory usage since the `PDFFetchStream`-implementation is used *by default* in browsers (for the GENERIC build).
---
[1] As opposed to PDF data being provided as e.g. a TypedArray when calling `getDocument` in the API.
[2] This is a "special" Node.js object, see https://nodejs.org/api/buffer.html#buffer, which doesn't exist in browsers.
2023-01-12 06:03:36 +09:00
|
|
|
return { value: getArrayBuffer(value), done: false };
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel(reason) {
|
2022-09-05 22:36:04 +09:00
|
|
|
this._reader?.cancel(reason);
|
2022-08-24 19:10:07 +09:00
|
|
|
this._abortController.abort();
|
2017-08-10 16:30:54 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
export { PDFFetchStream };
|