test_utils.js 4.1 KB

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