test_utils.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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 _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
  30. var _util = require("../../shared/util");
  31. var _is_node = _interopRequireDefault(require("../../shared/is_node"));
  32. var _primitives = require("../../core/primitives");
  33. var _document = require("../../core/document");
  34. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  35. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  36. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  37. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  38. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  39. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  40. var DOMFileReaderFactory =
  41. /*#__PURE__*/
  42. function () {
  43. function DOMFileReaderFactory() {
  44. _classCallCheck(this, DOMFileReaderFactory);
  45. }
  46. _createClass(DOMFileReaderFactory, null, [{
  47. key: "fetch",
  48. value: function (_fetch) {
  49. function fetch(_x) {
  50. return _fetch.apply(this, arguments);
  51. }
  52. fetch.toString = function () {
  53. return _fetch.toString();
  54. };
  55. return fetch;
  56. }(
  57. /*#__PURE__*/
  58. function () {
  59. var _ref = _asyncToGenerator(
  60. /*#__PURE__*/
  61. _regenerator["default"].mark(function _callee(params) {
  62. var response;
  63. return _regenerator["default"].wrap(function _callee$(_context) {
  64. while (1) {
  65. switch (_context.prev = _context.next) {
  66. case 0:
  67. _context.next = 2;
  68. return fetch(params.path);
  69. case 2:
  70. response = _context.sent;
  71. if (response.ok) {
  72. _context.next = 5;
  73. break;
  74. }
  75. throw new Error(response.statusText);
  76. case 5:
  77. _context.t0 = Uint8Array;
  78. _context.next = 8;
  79. return response.arrayBuffer();
  80. case 8:
  81. _context.t1 = _context.sent;
  82. return _context.abrupt("return", new _context.t0(_context.t1));
  83. case 10:
  84. case "end":
  85. return _context.stop();
  86. }
  87. }
  88. }, _callee);
  89. }));
  90. return function (_x2) {
  91. return _ref.apply(this, arguments);
  92. };
  93. }())
  94. }]);
  95. return DOMFileReaderFactory;
  96. }();
  97. exports.DOMFileReaderFactory = DOMFileReaderFactory;
  98. var NodeFileReaderFactory =
  99. /*#__PURE__*/
  100. function () {
  101. function NodeFileReaderFactory() {
  102. _classCallCheck(this, NodeFileReaderFactory);
  103. }
  104. _createClass(NodeFileReaderFactory, null, [{
  105. key: "fetch",
  106. value: function () {
  107. var _fetch2 = _asyncToGenerator(
  108. /*#__PURE__*/
  109. _regenerator["default"].mark(function _callee2(params) {
  110. var fs;
  111. return _regenerator["default"].wrap(function _callee2$(_context2) {
  112. while (1) {
  113. switch (_context2.prev = _context2.next) {
  114. case 0:
  115. fs = require('fs');
  116. return _context2.abrupt("return", new Promise(function (resolve, reject) {
  117. fs.readFile(params.path, function (error, data) {
  118. if (error || !data) {
  119. reject(error || new Error("Empty file for: ".concat(params.path)));
  120. return;
  121. }
  122. resolve(new Uint8Array(data));
  123. });
  124. }));
  125. case 2:
  126. case "end":
  127. return _context2.stop();
  128. }
  129. }
  130. }, _callee2);
  131. }));
  132. function fetch(_x3) {
  133. return _fetch2.apply(this, arguments);
  134. }
  135. return fetch;
  136. }()
  137. }]);
  138. return NodeFileReaderFactory;
  139. }();
  140. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  141. var TEST_PDFS_PATH = {
  142. dom: '../pdfs/',
  143. node: './test/pdfs/'
  144. };
  145. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;
  146. function buildGetDocumentParams(filename, options) {
  147. var params = Object.create(null);
  148. if ((0, _is_node["default"])()) {
  149. params.url = TEST_PDFS_PATH.node + filename;
  150. } else {
  151. params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
  152. }
  153. for (var option in options) {
  154. params[option] = options[option];
  155. }
  156. return params;
  157. }
  158. var NodeCanvasFactory =
  159. /*#__PURE__*/
  160. function () {
  161. function NodeCanvasFactory() {
  162. _classCallCheck(this, NodeCanvasFactory);
  163. }
  164. _createClass(NodeCanvasFactory, [{
  165. key: "create",
  166. value: function create(width, height) {
  167. (0, _util.assert)(width > 0 && height > 0, 'Invalid canvas size');
  168. var Canvas = require('canvas');
  169. var canvas = Canvas.createCanvas(width, height);
  170. return {
  171. canvas: canvas,
  172. context: canvas.getContext('2d')
  173. };
  174. }
  175. }, {
  176. key: "reset",
  177. value: function reset(canvasAndContext, width, height) {
  178. (0, _util.assert)(canvasAndContext.canvas, 'Canvas is not specified');
  179. (0, _util.assert)(width > 0 && height > 0, 'Invalid canvas size');
  180. canvasAndContext.canvas.width = width;
  181. canvasAndContext.canvas.height = height;
  182. }
  183. }, {
  184. key: "destroy",
  185. value: function destroy(canvasAndContext) {
  186. (0, _util.assert)(canvasAndContext.canvas, 'Canvas is not specified');
  187. canvasAndContext.canvas.width = 0;
  188. canvasAndContext.canvas.height = 0;
  189. canvasAndContext.canvas = null;
  190. canvasAndContext.context = null;
  191. }
  192. }]);
  193. return NodeCanvasFactory;
  194. }();
  195. exports.NodeCanvasFactory = NodeCanvasFactory;
  196. var NodeCMapReaderFactory =
  197. /*#__PURE__*/
  198. function () {
  199. function NodeCMapReaderFactory(_ref2) {
  200. var _ref2$baseUrl = _ref2.baseUrl,
  201. baseUrl = _ref2$baseUrl === void 0 ? null : _ref2$baseUrl,
  202. _ref2$isCompressed = _ref2.isCompressed,
  203. isCompressed = _ref2$isCompressed === void 0 ? false : _ref2$isCompressed;
  204. _classCallCheck(this, NodeCMapReaderFactory);
  205. this.baseUrl = baseUrl;
  206. this.isCompressed = isCompressed;
  207. }
  208. _createClass(NodeCMapReaderFactory, [{
  209. key: "fetch",
  210. value: function () {
  211. var _fetch3 = _asyncToGenerator(
  212. /*#__PURE__*/
  213. _regenerator["default"].mark(function _callee3(_ref3) {
  214. var _this = this;
  215. var name, url, compressionType;
  216. return _regenerator["default"].wrap(function _callee3$(_context3) {
  217. while (1) {
  218. switch (_context3.prev = _context3.next) {
  219. case 0:
  220. name = _ref3.name;
  221. if (this.baseUrl) {
  222. _context3.next = 3;
  223. break;
  224. }
  225. throw new Error('The CMap "baseUrl" parameter must be specified, ensure that ' + 'the "cMapUrl" and "cMapPacked" API parameters are provided.');
  226. case 3:
  227. if (name) {
  228. _context3.next = 5;
  229. break;
  230. }
  231. throw new Error('CMap name must be specified.');
  232. case 5:
  233. url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
  234. compressionType = this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE;
  235. return _context3.abrupt("return", new Promise(function (resolve, reject) {
  236. var fs = require('fs');
  237. fs.readFile(url, function (error, data) {
  238. if (error || !data) {
  239. reject(new Error(error));
  240. return;
  241. }
  242. resolve({
  243. cMapData: new Uint8Array(data),
  244. compressionType: compressionType
  245. });
  246. });
  247. })["catch"](function (reason) {
  248. throw new Error("Unable to load ".concat(_this.isCompressed ? 'binary ' : '') + "CMap at: ".concat(url));
  249. }));
  250. case 8:
  251. case "end":
  252. return _context3.stop();
  253. }
  254. }
  255. }, _callee3, this);
  256. }));
  257. function fetch(_x4) {
  258. return _fetch3.apply(this, arguments);
  259. }
  260. return fetch;
  261. }()
  262. }]);
  263. return NodeCMapReaderFactory;
  264. }();
  265. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  266. var XRefMock =
  267. /*#__PURE__*/
  268. function () {
  269. function XRefMock(array) {
  270. _classCallCheck(this, XRefMock);
  271. this._map = Object.create(null);
  272. for (var key in array) {
  273. var obj = array[key];
  274. this._map[obj.ref.toString()] = obj.data;
  275. }
  276. }
  277. _createClass(XRefMock, [{
  278. key: "fetch",
  279. value: function fetch(ref) {
  280. return this._map[ref.toString()];
  281. }
  282. }, {
  283. key: "fetchAsync",
  284. value: function fetchAsync(ref) {
  285. return Promise.resolve(this.fetch(ref));
  286. }
  287. }, {
  288. key: "fetchIfRef",
  289. value: function fetchIfRef(obj) {
  290. if (!(0, _primitives.isRef)(obj)) {
  291. return obj;
  292. }
  293. return this.fetch(obj);
  294. }
  295. }, {
  296. key: "fetchIfRefAsync",
  297. value: function fetchIfRefAsync(obj) {
  298. return Promise.resolve(this.fetchIfRef(obj));
  299. }
  300. }]);
  301. return XRefMock;
  302. }();
  303. exports.XRefMock = XRefMock;
  304. function createIdFactory(pageIndex) {
  305. var page = new _document.Page({
  306. pdfManager: {
  307. get docId() {
  308. return 'd0';
  309. }
  310. },
  311. pageIndex: pageIndex
  312. });
  313. return page.idFactory;
  314. }