test_utils.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.buildGetDocumentParams = buildGetDocumentParams;
  27. exports.createIdFactory = createIdFactory;
  28. exports.isEmptyObj = isEmptyObj;
  29. exports.TEST_PDFS_PATH = exports.XRefMock = exports.NodeFileReaderFactory = exports.DOMFileReaderFactory = void 0;
  30. var _primitives = require("../../core/primitives.js");
  31. var _document = require("../../core/document.js");
  32. var _util = require("../../shared/util.js");
  33. var _is_node = require("../../shared/is_node.js");
  34. var _stream = require("../../core/stream.js");
  35. class DOMFileReaderFactory {
  36. static async fetch(params) {
  37. const response = await fetch(params.path);
  38. if (!response.ok) {
  39. throw new Error(response.statusText);
  40. }
  41. return new Uint8Array(await response.arrayBuffer());
  42. }
  43. }
  44. exports.DOMFileReaderFactory = DOMFileReaderFactory;
  45. class NodeFileReaderFactory {
  46. static async fetch(params) {
  47. const fs = require("fs");
  48. return new Promise((resolve, reject) => {
  49. fs.readFile(params.path, (error, data) => {
  50. if (error || !data) {
  51. reject(error || new Error(`Empty file for: ${params.path}`));
  52. return;
  53. }
  54. resolve(new Uint8Array(data));
  55. });
  56. });
  57. }
  58. }
  59. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  60. const TEST_PDFS_PATH = {
  61. dom: "../pdfs/",
  62. node: "./test/pdfs/"
  63. };
  64. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;
  65. function buildGetDocumentParams(filename, options) {
  66. const params = Object.create(null);
  67. if (_is_node.isNodeJS) {
  68. params.url = TEST_PDFS_PATH.node + filename;
  69. } else {
  70. params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
  71. }
  72. for (const option in options) {
  73. params[option] = options[option];
  74. }
  75. return params;
  76. }
  77. class XRefMock {
  78. constructor(array) {
  79. this._map = Object.create(null);
  80. this.stats = {
  81. streamTypes: Object.create(null),
  82. fontTypes: Object.create(null)
  83. };
  84. this._newRefNum = null;
  85. for (const key in array) {
  86. const obj = array[key];
  87. this._map[obj.ref.toString()] = obj.data;
  88. }
  89. }
  90. getNewRef() {
  91. if (this._newRefNum === null) {
  92. this._newRefNum = Object.keys(this._map).length;
  93. }
  94. return _primitives.Ref.get(this._newRefNum++, 0);
  95. }
  96. resetNewRef() {
  97. this.newRef = null;
  98. }
  99. fetch(ref) {
  100. return this._map[ref.toString()];
  101. }
  102. fetchAsync(ref) {
  103. return Promise.resolve(this.fetch(ref));
  104. }
  105. fetchIfRef(obj) {
  106. if (!(0, _primitives.isRef)(obj)) {
  107. return obj;
  108. }
  109. return this.fetch(obj);
  110. }
  111. fetchIfRefAsync(obj) {
  112. return Promise.resolve(this.fetchIfRef(obj));
  113. }
  114. }
  115. exports.XRefMock = XRefMock;
  116. function createIdFactory(pageIndex) {
  117. const pdfManager = {
  118. get docId() {
  119. return "d0";
  120. }
  121. };
  122. const stream = new _stream.StringStream("Dummy_PDF_data");
  123. const pdfDocument = new _document.PDFDocument(pdfManager, stream);
  124. const page = new _document.Page({
  125. pdfManager: pdfDocument.pdfManager,
  126. xref: pdfDocument.xref,
  127. pageIndex,
  128. globalIdFactory: pdfDocument._globalIdFactory
  129. });
  130. return page._localIdFactory;
  131. }
  132. function isEmptyObj(obj) {
  133. (0, _util.assert)(typeof obj === "object" && obj !== null, "isEmptyObj - invalid argument.");
  134. return Object.keys(obj).length === 0;
  135. }