test_utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.TEST_PDFS_PATH = exports.XRefMock = exports.NodeCMapReaderFactory = exports.NodeCanvasFactory = exports.NodeFileReaderFactory = exports.DOMFileReaderFactory = void 0;
  29. var _util = require("../../shared/util.js");
  30. var _is_node = require("../../shared/is_node.js");
  31. var _primitives = require("../../core/primitives.js");
  32. var _document = require("../../core/document.js");
  33. class DOMFileReaderFactory {
  34. static async fetch(params) {
  35. const response = await fetch(params.path);
  36. if (!response.ok) {
  37. throw new Error(response.statusText);
  38. }
  39. return new Uint8Array(await response.arrayBuffer());
  40. }
  41. }
  42. exports.DOMFileReaderFactory = DOMFileReaderFactory;
  43. class NodeFileReaderFactory {
  44. static async fetch(params) {
  45. const fs = require("fs");
  46. return new Promise((resolve, reject) => {
  47. fs.readFile(params.path, (error, data) => {
  48. if (error || !data) {
  49. reject(error || new Error(`Empty file for: ${params.path}`));
  50. return;
  51. }
  52. resolve(new Uint8Array(data));
  53. });
  54. });
  55. }
  56. }
  57. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  58. const TEST_PDFS_PATH = {
  59. dom: "../pdfs/",
  60. node: "./test/pdfs/"
  61. };
  62. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;
  63. function buildGetDocumentParams(filename, options) {
  64. const params = Object.create(null);
  65. if (_is_node.isNodeJS) {
  66. params.url = TEST_PDFS_PATH.node + filename;
  67. } else {
  68. params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
  69. }
  70. for (const option in options) {
  71. params[option] = options[option];
  72. }
  73. return params;
  74. }
  75. class NodeCanvasFactory {
  76. create(width, height) {
  77. (0, _util.assert)(width > 0 && height > 0, "Invalid canvas size");
  78. const Canvas = require("canvas");
  79. const canvas = Canvas.createCanvas(width, height);
  80. return {
  81. canvas,
  82. context: canvas.getContext("2d")
  83. };
  84. }
  85. reset(canvasAndContext, width, height) {
  86. (0, _util.assert)(canvasAndContext.canvas, "Canvas is not specified");
  87. (0, _util.assert)(width > 0 && height > 0, "Invalid canvas size");
  88. canvasAndContext.canvas.width = width;
  89. canvasAndContext.canvas.height = height;
  90. }
  91. destroy(canvasAndContext) {
  92. (0, _util.assert)(canvasAndContext.canvas, "Canvas is not specified");
  93. canvasAndContext.canvas.width = 0;
  94. canvasAndContext.canvas.height = 0;
  95. canvasAndContext.canvas = null;
  96. canvasAndContext.context = null;
  97. }
  98. }
  99. exports.NodeCanvasFactory = NodeCanvasFactory;
  100. class NodeCMapReaderFactory {
  101. constructor({
  102. baseUrl = null,
  103. isCompressed = false
  104. }) {
  105. this.baseUrl = baseUrl;
  106. this.isCompressed = isCompressed;
  107. }
  108. async fetch({
  109. name
  110. }) {
  111. if (!this.baseUrl) {
  112. throw new Error('The CMap "baseUrl" parameter must be specified, ensure that ' + 'the "cMapUrl" and "cMapPacked" API parameters are provided.');
  113. }
  114. if (!name) {
  115. throw new Error("CMap name must be specified.");
  116. }
  117. const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
  118. const compressionType = this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE;
  119. return new Promise((resolve, reject) => {
  120. const fs = require("fs");
  121. fs.readFile(url, (error, data) => {
  122. if (error || !data) {
  123. reject(new Error(error));
  124. return;
  125. }
  126. resolve({
  127. cMapData: new Uint8Array(data),
  128. compressionType
  129. });
  130. });
  131. }).catch(reason => {
  132. throw new Error(`Unable to load ${this.isCompressed ? "binary " : ""}` + `CMap at: ${url}`);
  133. });
  134. }
  135. }
  136. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  137. class XRefMock {
  138. constructor(array) {
  139. this._map = Object.create(null);
  140. for (const key in array) {
  141. const obj = array[key];
  142. this._map[obj.ref.toString()] = obj.data;
  143. }
  144. }
  145. fetch(ref) {
  146. return this._map[ref.toString()];
  147. }
  148. fetchAsync(ref) {
  149. return Promise.resolve(this.fetch(ref));
  150. }
  151. fetchIfRef(obj) {
  152. if (!(0, _primitives.isRef)(obj)) {
  153. return obj;
  154. }
  155. return this.fetch(obj);
  156. }
  157. fetchIfRefAsync(obj) {
  158. return Promise.resolve(this.fetchIfRef(obj));
  159. }
  160. }
  161. exports.XRefMock = XRefMock;
  162. function createIdFactory(pageIndex) {
  163. const page = new _document.Page({
  164. pdfManager: {
  165. get docId() {
  166. return "d0";
  167. }
  168. },
  169. pageIndex
  170. });
  171. return page.idFactory;
  172. }