test_utils.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.TEST_PDFS_PATH = exports.buildGetDocumentParams = exports.XRefMock = exports.NodeCMapReaderFactory = exports.NodeFileReaderFactory = undefined;
  20. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _util = require('../../shared/util');
  22. var _primitives = require('../../core/primitives');
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. var NodeFileReaderFactory = function () {
  25. function NodeFileReaderFactory() {
  26. _classCallCheck(this, NodeFileReaderFactory);
  27. }
  28. _createClass(NodeFileReaderFactory, null, [{
  29. key: 'fetch',
  30. value: function fetch(params) {
  31. var fs = require('fs');
  32. var file = fs.readFileSync(params.path);
  33. return new Uint8Array(file);
  34. }
  35. }]);
  36. return NodeFileReaderFactory;
  37. }();
  38. var TEST_PDFS_PATH = {
  39. dom: '../pdfs/',
  40. node: './test/pdfs/'
  41. };
  42. function buildGetDocumentParams(filename, options) {
  43. var params = Object.create(null);
  44. if ((0, _util.isNodeJS)()) {
  45. params.data = NodeFileReaderFactory.fetch({ path: TEST_PDFS_PATH.node + filename });
  46. } else {
  47. params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
  48. }
  49. for (var option in options) {
  50. params[option] = options[option];
  51. }
  52. return params;
  53. }
  54. var NodeCMapReaderFactory = function () {
  55. function NodeCMapReaderFactory(_ref) {
  56. var _ref$baseUrl = _ref.baseUrl,
  57. baseUrl = _ref$baseUrl === undefined ? null : _ref$baseUrl,
  58. _ref$isCompressed = _ref.isCompressed,
  59. isCompressed = _ref$isCompressed === undefined ? false : _ref$isCompressed;
  60. _classCallCheck(this, NodeCMapReaderFactory);
  61. this.baseUrl = baseUrl;
  62. this.isCompressed = isCompressed;
  63. }
  64. _createClass(NodeCMapReaderFactory, [{
  65. key: 'fetch',
  66. value: function fetch(_ref2) {
  67. var _this = this;
  68. var name = _ref2.name;
  69. if (!name) {
  70. return Promise.reject(new Error('CMap name must be specified.'));
  71. }
  72. return new Promise(function (resolve, reject) {
  73. var url = _this.baseUrl + name + (_this.isCompressed ? '.bcmap' : '');
  74. var fs = require('fs');
  75. fs.readFile(url, function (error, data) {
  76. if (error || !data) {
  77. reject(new Error('Unable to load ' + (_this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  78. return;
  79. }
  80. resolve({
  81. cMapData: new Uint8Array(data),
  82. compressionType: _this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE
  83. });
  84. });
  85. });
  86. }
  87. }]);
  88. return NodeCMapReaderFactory;
  89. }();
  90. var XRefMock = function () {
  91. function XRefMock(array) {
  92. _classCallCheck(this, XRefMock);
  93. this._map = Object.create(null);
  94. for (var key in array) {
  95. var obj = array[key];
  96. this._map[obj.ref.toString()] = obj.data;
  97. }
  98. }
  99. _createClass(XRefMock, [{
  100. key: 'fetch',
  101. value: function fetch(ref) {
  102. return this._map[ref.toString()];
  103. }
  104. }, {
  105. key: 'fetchAsync',
  106. value: function fetchAsync(ref) {
  107. return Promise.resolve(this.fetch(ref));
  108. }
  109. }, {
  110. key: 'fetchIfRef',
  111. value: function fetchIfRef(obj) {
  112. if (!(0, _primitives.isRef)(obj)) {
  113. return obj;
  114. }
  115. return this.fetch(obj);
  116. }
  117. }, {
  118. key: 'fetchIfRefAsync',
  119. value: function fetchIfRefAsync(obj) {
  120. return Promise.resolve(this.fetchIfRef(obj));
  121. }
  122. }]);
  123. return XRefMock;
  124. }();
  125. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  126. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  127. exports.XRefMock = XRefMock;
  128. exports.buildGetDocumentParams = buildGetDocumentParams;
  129. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;