123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585 |
- /**
- * @licstart The following is the entire license notice for the
- * Javascript code in this page
- *
- * Copyright 2021 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.
- *
- * @licend The above is the entire license notice for the
- * Javascript code in this page
- */
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.ChunkedStreamManager = exports.ChunkedStream = void 0;
- var _util = require("../shared/util.js");
- var _core_utils = require("./core_utils.js");
- var _stream = require("./stream.js");
- class ChunkedStream extends _stream.Stream {
- constructor(length, chunkSize, manager) {
- super(new Uint8Array(length), 0, length, null);
- this.chunkSize = chunkSize;
- this._loadedChunks = new Set();
- this.numChunks = Math.ceil(length / chunkSize);
- this.manager = manager;
- this.progressiveDataLength = 0;
- this.lastSuccessfulEnsureByteChunk = -1;
- }
- getMissingChunks() {
- const chunks = [];
- for (let chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
- if (!this._loadedChunks.has(chunk)) {
- chunks.push(chunk);
- }
- }
- return chunks;
- }
- get numChunksLoaded() {
- return this._loadedChunks.size;
- }
- get isDataLoaded() {
- return this.numChunksLoaded === this.numChunks;
- }
- onReceiveData(begin, chunk) {
- const chunkSize = this.chunkSize;
- if (begin % chunkSize !== 0) {
- throw new Error(`Bad begin offset: ${begin}`);
- }
- const end = begin + chunk.byteLength;
- if (end % chunkSize !== 0 && end !== this.bytes.length) {
- throw new Error(`Bad end offset: ${end}`);
- }
- this.bytes.set(new Uint8Array(chunk), begin);
- const beginChunk = Math.floor(begin / chunkSize);
- const endChunk = Math.floor((end - 1) / chunkSize) + 1;
- for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
- this._loadedChunks.add(curChunk);
- }
- }
- onReceiveProgressiveData(data) {
- let position = this.progressiveDataLength;
- const beginChunk = Math.floor(position / this.chunkSize);
- this.bytes.set(new Uint8Array(data), position);
- position += data.byteLength;
- this.progressiveDataLength = position;
- const endChunk = position >= this.end ? this.numChunks : Math.floor(position / this.chunkSize);
- for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
- this._loadedChunks.add(curChunk);
- }
- }
- ensureByte(pos) {
- if (pos < this.progressiveDataLength) {
- return;
- }
- const chunk = Math.floor(pos / this.chunkSize);
- if (chunk === this.lastSuccessfulEnsureByteChunk) {
- return;
- }
- if (!this._loadedChunks.has(chunk)) {
- throw new _core_utils.MissingDataException(pos, pos + 1);
- }
- this.lastSuccessfulEnsureByteChunk = chunk;
- }
- ensureRange(begin, end) {
- if (begin >= end) {
- return;
- }
- if (end <= this.progressiveDataLength) {
- return;
- }
- const chunkSize = this.chunkSize;
- const beginChunk = Math.floor(begin / chunkSize);
- const endChunk = Math.floor((end - 1) / chunkSize) + 1;
- for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
- if (!this._loadedChunks.has(chunk)) {
- throw new _core_utils.MissingDataException(begin, end);
- }
- }
- }
- nextEmptyChunk(beginChunk) {
- const numChunks = this.numChunks;
- for (let i = 0; i < numChunks; ++i) {
- const chunk = (beginChunk + i) % numChunks;
- if (!this._loadedChunks.has(chunk)) {
- return chunk;
- }
- }
- return null;
- }
- hasChunk(chunk) {
- return this._loadedChunks.has(chunk);
- }
- getByte() {
- const pos = this.pos;
- if (pos >= this.end) {
- return -1;
- }
- if (pos >= this.progressiveDataLength) {
- this.ensureByte(pos);
- }
- return this.bytes[this.pos++];
- }
- getBytes(length, forceClamped = false) {
- const bytes = this.bytes;
- const pos = this.pos;
- const strEnd = this.end;
- if (!length) {
- if (strEnd > this.progressiveDataLength) {
- this.ensureRange(pos, strEnd);
- }
- const subarray = bytes.subarray(pos, strEnd);
- return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
- }
- let end = pos + length;
- if (end > strEnd) {
- end = strEnd;
- }
- if (end > this.progressiveDataLength) {
- this.ensureRange(pos, end);
- }
- this.pos = end;
- const subarray = bytes.subarray(pos, end);
- return forceClamped ? new Uint8ClampedArray(subarray) : subarray;
- }
- getByteRange(begin, end) {
- if (begin < 0) {
- begin = 0;
- }
- if (end > this.end) {
- end = this.end;
- }
- if (end > this.progressiveDataLength) {
- this.ensureRange(begin, end);
- }
- return this.bytes.subarray(begin, end);
- }
- makeSubStream(start, length, dict = null) {
- if (length) {
- if (start + length > this.progressiveDataLength) {
- this.ensureRange(start, start + length);
- }
- } else {
- if (start >= this.progressiveDataLength) {
- this.ensureByte(start);
- }
- }
- function ChunkedStreamSubstream() {}
- ChunkedStreamSubstream.prototype = Object.create(this);
- ChunkedStreamSubstream.prototype.getMissingChunks = function () {
- const chunkSize = this.chunkSize;
- const beginChunk = Math.floor(this.start / chunkSize);
- const endChunk = Math.floor((this.end - 1) / chunkSize) + 1;
- const missingChunks = [];
- for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
- if (!this._loadedChunks.has(chunk)) {
- missingChunks.push(chunk);
- }
- }
- return missingChunks;
- };
- Object.defineProperty(ChunkedStreamSubstream.prototype, "isDataLoaded", {
- get() {
- if (this.numChunksLoaded === this.numChunks) {
- return true;
- }
- return this.getMissingChunks().length === 0;
- },
- configurable: true
- });
- const subStream = new ChunkedStreamSubstream();
- subStream.pos = subStream.start = start;
- subStream.end = start + length || this.end;
- subStream.dict = dict;
- return subStream;
- }
- getBaseStreams() {
- return [this];
- }
- }
- exports.ChunkedStream = ChunkedStream;
- class ChunkedStreamManager {
- constructor(pdfNetworkStream, args) {
- this.length = args.length;
- this.chunkSize = args.rangeChunkSize;
- this.stream = new ChunkedStream(this.length, this.chunkSize, this);
- this.pdfNetworkStream = pdfNetworkStream;
- this.disableAutoFetch = args.disableAutoFetch;
- this.msgHandler = args.msgHandler;
- this.currRequestId = 0;
- this._chunksNeededByRequest = new Map();
- this._requestsByChunk = new Map();
- this._promisesByRequest = new Map();
- this.progressiveDataLength = 0;
- this.aborted = false;
- this._loadedStreamCapability = (0, _util.createPromiseCapability)();
- }
- onLoadedStream() {
- return this._loadedStreamCapability.promise;
- }
- sendRequest(begin, end) {
- const rangeReader = this.pdfNetworkStream.getRangeReader(begin, end);
- if (!rangeReader.isStreamingSupported) {
- rangeReader.onProgress = this.onProgress.bind(this);
- }
- let chunks = [],
- loaded = 0;
- const promise = new Promise((resolve, reject) => {
- const readChunk = chunk => {
- try {
- if (!chunk.done) {
- const data = chunk.value;
- chunks.push(data);
- loaded += (0, _util.arrayByteLength)(data);
- if (rangeReader.isStreamingSupported) {
- this.onProgress({
- loaded
- });
- }
- rangeReader.read().then(readChunk, reject);
- return;
- }
- const chunkData = (0, _util.arraysToBytes)(chunks);
- chunks = null;
- resolve(chunkData);
- } catch (e) {
- reject(e);
- }
- };
- rangeReader.read().then(readChunk, reject);
- });
- promise.then(data => {
- if (this.aborted) {
- return;
- }
- this.onReceiveData({
- chunk: data,
- begin
- });
- });
- }
- requestAllChunks() {
- const missingChunks = this.stream.getMissingChunks();
- this._requestChunks(missingChunks);
- return this._loadedStreamCapability.promise;
- }
- _requestChunks(chunks) {
- const requestId = this.currRequestId++;
- const chunksNeeded = new Set();
- this._chunksNeededByRequest.set(requestId, chunksNeeded);
- for (const chunk of chunks) {
- if (!this.stream.hasChunk(chunk)) {
- chunksNeeded.add(chunk);
- }
- }
- if (chunksNeeded.size === 0) {
- return Promise.resolve();
- }
- const capability = (0, _util.createPromiseCapability)();
- this._promisesByRequest.set(requestId, capability);
- const chunksToRequest = [];
- for (const chunk of chunksNeeded) {
- let requestIds = this._requestsByChunk.get(chunk);
- if (!requestIds) {
- requestIds = [];
- this._requestsByChunk.set(chunk, requestIds);
- chunksToRequest.push(chunk);
- }
- requestIds.push(requestId);
- }
- if (chunksToRequest.length > 0) {
- const groupedChunksToRequest = this.groupChunks(chunksToRequest);
- for (const groupedChunk of groupedChunksToRequest) {
- const begin = groupedChunk.beginChunk * this.chunkSize;
- const end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
- this.sendRequest(begin, end);
- }
- }
- return capability.promise.catch(reason => {
- if (this.aborted) {
- return;
- }
- throw reason;
- });
- }
- getStream() {
- return this.stream;
- }
- requestRange(begin, end) {
- end = Math.min(end, this.length);
- const beginChunk = this.getBeginChunk(begin);
- const endChunk = this.getEndChunk(end);
- const chunks = [];
- for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
- chunks.push(chunk);
- }
- return this._requestChunks(chunks);
- }
- requestRanges(ranges = []) {
- const chunksToRequest = [];
- for (const range of ranges) {
- const beginChunk = this.getBeginChunk(range.begin);
- const endChunk = this.getEndChunk(range.end);
- for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
- if (!chunksToRequest.includes(chunk)) {
- chunksToRequest.push(chunk);
- }
- }
- }
- chunksToRequest.sort(function (a, b) {
- return a - b;
- });
- return this._requestChunks(chunksToRequest);
- }
- groupChunks(chunks) {
- const groupedChunks = [];
- let beginChunk = -1;
- let prevChunk = -1;
- for (let i = 0, ii = chunks.length; i < ii; ++i) {
- const chunk = chunks[i];
- if (beginChunk < 0) {
- beginChunk = chunk;
- }
- if (prevChunk >= 0 && prevChunk + 1 !== chunk) {
- groupedChunks.push({
- beginChunk,
- endChunk: prevChunk + 1
- });
- beginChunk = chunk;
- }
- if (i + 1 === chunks.length) {
- groupedChunks.push({
- beginChunk,
- endChunk: chunk + 1
- });
- }
- prevChunk = chunk;
- }
- return groupedChunks;
- }
- onProgress(args) {
- this.msgHandler.send("DocProgress", {
- loaded: this.stream.numChunksLoaded * this.chunkSize + args.loaded,
- total: this.length
- });
- }
- onReceiveData(args) {
- const chunk = args.chunk;
- const isProgressive = args.begin === undefined;
- const begin = isProgressive ? this.progressiveDataLength : args.begin;
- const end = begin + chunk.byteLength;
- const beginChunk = Math.floor(begin / this.chunkSize);
- const endChunk = end < this.length ? Math.floor(end / this.chunkSize) : Math.ceil(end / this.chunkSize);
- if (isProgressive) {
- this.stream.onReceiveProgressiveData(chunk);
- this.progressiveDataLength = end;
- } else {
- this.stream.onReceiveData(begin, chunk);
- }
- if (this.stream.isDataLoaded) {
- this._loadedStreamCapability.resolve(this.stream);
- }
- const loadedRequests = [];
- for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
- const requestIds = this._requestsByChunk.get(curChunk);
- if (!requestIds) {
- continue;
- }
- this._requestsByChunk.delete(curChunk);
- for (const requestId of requestIds) {
- const chunksNeeded = this._chunksNeededByRequest.get(requestId);
- if (chunksNeeded.has(curChunk)) {
- chunksNeeded.delete(curChunk);
- }
- if (chunksNeeded.size > 0) {
- continue;
- }
- loadedRequests.push(requestId);
- }
- }
- if (!this.disableAutoFetch && this._requestsByChunk.size === 0) {
- let nextEmptyChunk;
- if (this.stream.numChunksLoaded === 1) {
- const lastChunk = this.stream.numChunks - 1;
- if (!this.stream.hasChunk(lastChunk)) {
- nextEmptyChunk = lastChunk;
- }
- } else {
- nextEmptyChunk = this.stream.nextEmptyChunk(endChunk);
- }
- if (Number.isInteger(nextEmptyChunk)) {
- this._requestChunks([nextEmptyChunk]);
- }
- }
- for (const requestId of loadedRequests) {
- const capability = this._promisesByRequest.get(requestId);
- this._promisesByRequest.delete(requestId);
- capability.resolve();
- }
- this.msgHandler.send("DocProgress", {
- loaded: this.stream.numChunksLoaded * this.chunkSize,
- total: this.length
- });
- }
- onError(err) {
- this._loadedStreamCapability.reject(err);
- }
- getBeginChunk(begin) {
- return Math.floor(begin / this.chunkSize);
- }
- getEndChunk(end) {
- return Math.floor((end - 1) / this.chunkSize) + 1;
- }
- abort(reason) {
- this.aborted = true;
- if (this.pdfNetworkStream) {
- this.pdfNetworkStream.cancelAllRequests(reason);
- }
- for (const capability of this._promisesByRequest.values()) {
- capability.reject(reason);
- }
- }
- }
- exports.ChunkedStreamManager = ChunkedStreamManager;
|