2
0

object_loader.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.ObjectLoader = void 0;
  27. var _primitives = require("./primitives.js");
  28. var _core_utils = require("./core_utils.js");
  29. var _util = require("../shared/util.js");
  30. function mayHaveChildren(value) {
  31. return value instanceof _primitives.Ref || value instanceof _primitives.Dict || Array.isArray(value) || (0, _primitives.isStream)(value);
  32. }
  33. function addChildren(node, nodesToVisit) {
  34. if (node instanceof _primitives.Dict) {
  35. node = node.getRawValues();
  36. } else if ((0, _primitives.isStream)(node)) {
  37. node = node.dict.getRawValues();
  38. } else if (!Array.isArray(node)) {
  39. return;
  40. }
  41. for (const rawValue of node) {
  42. if (mayHaveChildren(rawValue)) {
  43. nodesToVisit.push(rawValue);
  44. }
  45. }
  46. }
  47. class ObjectLoader {
  48. constructor(dict, keys, xref) {
  49. this.dict = dict;
  50. this.keys = keys;
  51. this.xref = xref;
  52. this.refSet = null;
  53. }
  54. async load() {
  55. if (this.xref.stream.isDataLoaded) {
  56. return undefined;
  57. }
  58. const {
  59. keys,
  60. dict
  61. } = this;
  62. this.refSet = new _primitives.RefSet();
  63. const nodesToVisit = [];
  64. for (let i = 0, ii = keys.length; i < ii; i++) {
  65. const rawValue = dict.getRaw(keys[i]);
  66. if (rawValue !== undefined) {
  67. nodesToVisit.push(rawValue);
  68. }
  69. }
  70. return this._walk(nodesToVisit);
  71. }
  72. async _walk(nodesToVisit) {
  73. const nodesToRevisit = [];
  74. const pendingRequests = [];
  75. while (nodesToVisit.length) {
  76. let currentNode = nodesToVisit.pop();
  77. if (currentNode instanceof _primitives.Ref) {
  78. if (this.refSet.has(currentNode)) {
  79. continue;
  80. }
  81. try {
  82. this.refSet.put(currentNode);
  83. currentNode = this.xref.fetch(currentNode);
  84. } catch (ex) {
  85. if (!(ex instanceof _core_utils.MissingDataException)) {
  86. (0, _util.warn)(`ObjectLoader._walk - requesting all data: "${ex}".`);
  87. this.refSet = null;
  88. const {
  89. manager
  90. } = this.xref.stream;
  91. return manager.requestAllChunks();
  92. }
  93. nodesToRevisit.push(currentNode);
  94. pendingRequests.push({
  95. begin: ex.begin,
  96. end: ex.end
  97. });
  98. }
  99. }
  100. if ((0, _primitives.isStream)(currentNode)) {
  101. const baseStreams = currentNode.getBaseStreams();
  102. if (baseStreams) {
  103. let foundMissingData = false;
  104. for (const stream of baseStreams) {
  105. if (stream.isDataLoaded) {
  106. continue;
  107. }
  108. foundMissingData = true;
  109. pendingRequests.push({
  110. begin: stream.start,
  111. end: stream.end
  112. });
  113. }
  114. if (foundMissingData) {
  115. nodesToRevisit.push(currentNode);
  116. }
  117. }
  118. }
  119. addChildren(currentNode, nodesToVisit);
  120. }
  121. if (pendingRequests.length) {
  122. await this.xref.stream.manager.requestRanges(pendingRequests);
  123. for (const node of nodesToRevisit) {
  124. if (node instanceof _primitives.Ref) {
  125. this.refSet.remove(node);
  126. }
  127. }
  128. return this._walk(nodesToRevisit);
  129. }
  130. this.refSet = null;
  131. return undefined;
  132. }
  133. }
  134. exports.ObjectLoader = ObjectLoader;