object_loader.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2022 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 _base_stream = require("./base_stream.js");
  29. var _core_utils = require("./core_utils.js");
  30. var _util = require("../shared/util.js");
  31. function mayHaveChildren(value) {
  32. return value instanceof _primitives.Ref || value instanceof _primitives.Dict || value instanceof _base_stream.BaseStream || Array.isArray(value);
  33. }
  34. function addChildren(node, nodesToVisit) {
  35. if (node instanceof _primitives.Dict) {
  36. node = node.getRawValues();
  37. } else if (node instanceof _base_stream.BaseStream) {
  38. node = node.dict.getRawValues();
  39. } else if (!Array.isArray(node)) {
  40. return;
  41. }
  42. for (const rawValue of node) {
  43. if (mayHaveChildren(rawValue)) {
  44. nodesToVisit.push(rawValue);
  45. }
  46. }
  47. }
  48. class ObjectLoader {
  49. constructor(dict, keys, xref) {
  50. this.dict = dict;
  51. this.keys = keys;
  52. this.xref = xref;
  53. this.refSet = null;
  54. }
  55. async load() {
  56. if (this.xref.stream.isDataLoaded) {
  57. return undefined;
  58. }
  59. const {
  60. keys,
  61. dict
  62. } = this;
  63. this.refSet = new _primitives.RefSet();
  64. const nodesToVisit = [];
  65. for (let i = 0, ii = keys.length; i < ii; i++) {
  66. const rawValue = dict.getRaw(keys[i]);
  67. if (rawValue !== undefined) {
  68. nodesToVisit.push(rawValue);
  69. }
  70. }
  71. return this._walk(nodesToVisit);
  72. }
  73. async _walk(nodesToVisit) {
  74. const nodesToRevisit = [];
  75. const pendingRequests = [];
  76. while (nodesToVisit.length) {
  77. let currentNode = nodesToVisit.pop();
  78. if (currentNode instanceof _primitives.Ref) {
  79. if (this.refSet.has(currentNode)) {
  80. continue;
  81. }
  82. try {
  83. this.refSet.put(currentNode);
  84. currentNode = this.xref.fetch(currentNode);
  85. } catch (ex) {
  86. if (!(ex instanceof _core_utils.MissingDataException)) {
  87. (0, _util.warn)(`ObjectLoader._walk - requesting all data: "${ex}".`);
  88. this.refSet = null;
  89. const {
  90. manager
  91. } = this.xref.stream;
  92. return manager.requestAllChunks();
  93. }
  94. nodesToRevisit.push(currentNode);
  95. pendingRequests.push({
  96. begin: ex.begin,
  97. end: ex.end
  98. });
  99. }
  100. }
  101. if (currentNode instanceof _base_stream.BaseStream) {
  102. const baseStreams = currentNode.getBaseStreams();
  103. if (baseStreams) {
  104. let foundMissingData = false;
  105. for (const stream of baseStreams) {
  106. if (stream.isDataLoaded) {
  107. continue;
  108. }
  109. foundMissingData = true;
  110. pendingRequests.push({
  111. begin: stream.start,
  112. end: stream.end
  113. });
  114. }
  115. if (foundMissingData) {
  116. nodesToRevisit.push(currentNode);
  117. }
  118. }
  119. }
  120. addChildren(currentNode, nodesToVisit);
  121. }
  122. if (pendingRequests.length) {
  123. await this.xref.stream.manager.requestRanges(pendingRequests);
  124. for (const node of nodesToRevisit) {
  125. if (node instanceof _primitives.Ref) {
  126. this.refSet.remove(node);
  127. }
  128. }
  129. return this._walk(nodesToRevisit);
  130. }
  131. this.refSet = null;
  132. return undefined;
  133. }
  134. }
  135. exports.ObjectLoader = ObjectLoader;