2021-04-14 01:25:34 +09:00
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2022-02-17 21:45:42 +09:00
|
|
|
import { Dict, Ref, RefSet } from "./primitives.js";
|
|
|
|
import { BaseStream } from "./base_stream.js";
|
2021-04-14 01:25:34 +09:00
|
|
|
import { MissingDataException } from "./core_utils.js";
|
|
|
|
import { warn } from "../shared/util.js";
|
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
function mayHaveChildren(value) {
|
|
|
|
return (
|
|
|
|
value instanceof Ref ||
|
|
|
|
value instanceof Dict ||
|
2022-02-17 21:45:42 +09:00
|
|
|
value instanceof BaseStream ||
|
|
|
|
Array.isArray(value)
|
2021-04-14 01:25:42 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addChildren(node, nodesToVisit) {
|
|
|
|
if (node instanceof Dict) {
|
|
|
|
node = node.getRawValues();
|
2022-02-17 21:45:42 +09:00
|
|
|
} else if (node instanceof BaseStream) {
|
2021-04-14 01:25:42 +09:00
|
|
|
node = node.dict.getRawValues();
|
|
|
|
} else if (!Array.isArray(node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (const rawValue of node) {
|
|
|
|
if (mayHaveChildren(rawValue)) {
|
|
|
|
nodesToVisit.push(rawValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-14 01:25:34 +09:00
|
|
|
/**
|
|
|
|
* A helper for loading missing data in `Dict` graphs. It traverses the graph
|
|
|
|
* depth first and queues up any objects that have missing data. Once it has
|
|
|
|
* has traversed as many objects that are available it attempts to bundle the
|
|
|
|
* missing data requests and then resume from the nodes that weren't ready.
|
|
|
|
*
|
|
|
|
* NOTE: It provides protection from circular references by keeping track of
|
|
|
|
* loaded references. However, you must be careful not to load any graphs
|
|
|
|
* that have references to the catalog or other pages since that will cause the
|
|
|
|
* entire PDF document object graph to be traversed.
|
|
|
|
*/
|
2021-04-14 01:25:42 +09:00
|
|
|
class ObjectLoader {
|
|
|
|
constructor(dict, keys, xref) {
|
2021-04-14 01:25:34 +09:00
|
|
|
this.dict = dict;
|
|
|
|
this.keys = keys;
|
|
|
|
this.xref = xref;
|
|
|
|
this.refSet = null;
|
|
|
|
}
|
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
async load() {
|
2021-04-28 00:08:54 +09:00
|
|
|
// Don't walk the graph if all the data is already loaded.
|
|
|
|
if (this.xref.stream.isDataLoaded) {
|
2021-04-14 01:25:42 +09:00
|
|
|
return undefined;
|
|
|
|
}
|
2021-04-14 01:25:34 +09:00
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
const { keys, dict } = this;
|
|
|
|
this.refSet = new RefSet();
|
|
|
|
// Setup the initial nodes to visit.
|
|
|
|
const nodesToVisit = [];
|
|
|
|
for (let i = 0, ii = keys.length; i < ii; i++) {
|
|
|
|
const rawValue = dict.getRaw(keys[i]);
|
|
|
|
// Skip nodes that are guaranteed to be empty.
|
|
|
|
if (rawValue !== undefined) {
|
|
|
|
nodesToVisit.push(rawValue);
|
2021-04-14 01:25:34 +09:00
|
|
|
}
|
2021-04-14 01:25:42 +09:00
|
|
|
}
|
|
|
|
return this._walk(nodesToVisit);
|
|
|
|
}
|
2021-04-14 01:25:34 +09:00
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
async _walk(nodesToVisit) {
|
|
|
|
const nodesToRevisit = [];
|
|
|
|
const pendingRequests = [];
|
|
|
|
// DFS walk of the object graph.
|
|
|
|
while (nodesToVisit.length) {
|
|
|
|
let currentNode = nodesToVisit.pop();
|
2021-04-14 01:25:34 +09:00
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
// Only references or chunked streams can cause missing data exceptions.
|
|
|
|
if (currentNode instanceof Ref) {
|
|
|
|
// Skip nodes that have already been visited.
|
|
|
|
if (this.refSet.has(currentNode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
this.refSet.put(currentNode);
|
|
|
|
currentNode = this.xref.fetch(currentNode);
|
|
|
|
} catch (ex) {
|
|
|
|
if (!(ex instanceof MissingDataException)) {
|
|
|
|
warn(`ObjectLoader._walk - requesting all data: "${ex}".`);
|
|
|
|
this.refSet = null;
|
2021-04-14 01:25:34 +09:00
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
const { manager } = this.xref.stream;
|
|
|
|
return manager.requestAllChunks();
|
2021-04-14 01:25:34 +09:00
|
|
|
}
|
2021-04-14 01:25:42 +09:00
|
|
|
nodesToRevisit.push(currentNode);
|
|
|
|
pendingRequests.push({ begin: ex.begin, end: ex.end });
|
2021-04-14 01:25:34 +09:00
|
|
|
}
|
2021-04-14 01:25:42 +09:00
|
|
|
}
|
2022-02-17 21:45:42 +09:00
|
|
|
if (currentNode instanceof BaseStream) {
|
2021-04-14 01:25:42 +09:00
|
|
|
const baseStreams = currentNode.getBaseStreams();
|
2021-04-28 18:57:29 +09:00
|
|
|
if (baseStreams) {
|
|
|
|
let foundMissingData = false;
|
|
|
|
for (const stream of baseStreams) {
|
|
|
|
if (stream.isDataLoaded) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foundMissingData = true;
|
|
|
|
pendingRequests.push({ begin: stream.start, end: stream.end });
|
|
|
|
}
|
|
|
|
if (foundMissingData) {
|
|
|
|
nodesToRevisit.push(currentNode);
|
2021-04-14 01:25:34 +09:00
|
|
|
}
|
2021-04-14 01:25:42 +09:00
|
|
|
}
|
2021-04-14 01:25:34 +09:00
|
|
|
}
|
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
addChildren(currentNode, nodesToVisit);
|
|
|
|
}
|
2021-04-14 01:25:34 +09:00
|
|
|
|
2021-04-14 01:25:42 +09:00
|
|
|
if (pendingRequests.length) {
|
|
|
|
await this.xref.stream.manager.requestRanges(pendingRequests);
|
|
|
|
|
2021-04-28 00:08:54 +09:00
|
|
|
for (const node of nodesToRevisit) {
|
2021-04-14 01:25:42 +09:00
|
|
|
// Remove any reference nodes from the current `RefSet` so they
|
|
|
|
// aren't skipped when we revist them.
|
|
|
|
if (node instanceof Ref) {
|
|
|
|
this.refSet.remove(node);
|
2021-04-14 01:25:34 +09:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 01:25:42 +09:00
|
|
|
return this._walk(nodesToRevisit);
|
|
|
|
}
|
|
|
|
// Everything is loaded.
|
|
|
|
this.refSet = null;
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
}
|
2021-04-14 01:25:34 +09:00
|
|
|
|
|
|
|
export { ObjectLoader };
|