test_utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 (!this.baseUrl) {
  70. return Promise.reject(new Error('CMap baseUrl must be specified, ' + 'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").'));
  71. }
  72. if (!name) {
  73. return Promise.reject(new Error('CMap name must be specified.'));
  74. }
  75. return new Promise(function (resolve, reject) {
  76. var url = _this.baseUrl + name + (_this.isCompressed ? '.bcmap' : '');
  77. var fs = require('fs');
  78. fs.readFile(url, function (error, data) {
  79. if (error || !data) {
  80. reject(new Error('Unable to load ' + (_this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  81. return;
  82. }
  83. resolve({
  84. cMapData: new Uint8Array(data),
  85. compressionType: _this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE
  86. });
  87. });
  88. });
  89. }
  90. }]);
  91. return NodeCMapReaderFactory;
  92. }();
  93. var XRefMock = function () {
  94. function XRefMock(array) {
  95. _classCallCheck(this, XRefMock);
  96. this._map = Object.create(null);
  97. for (var key in array) {
  98. var obj = array[key];
  99. this._map[obj.ref.toString()] = obj.data;
  100. }
  101. }
  102. _createClass(XRefMock, [{
  103. key: 'fetch',
  104. value: function fetch(ref) {
  105. return this._map[ref.toString()];
  106. }
  107. }, {
  108. key: 'fetchAsync',
  109. value: function fetchAsync(ref) {
  110. return Promise.resolve(this.fetch(ref));
  111. }
  112. }, {
  113. key: 'fetchIfRef',
  114. value: function fetchIfRef(obj) {
  115. if (!(0, _primitives.isRef)(obj)) {
  116. return obj;
  117. }
  118. return this.fetch(obj);
  119. }
  120. }, {
  121. key: 'fetchIfRefAsync',
  122. value: function fetchIfRefAsync(obj) {
  123. return Promise.resolve(this.fetchIfRef(obj));
  124. }
  125. }]);
  126. return XRefMock;
  127. }();
  128. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  129. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  130. exports.XRefMock = XRefMock;
  131. exports.buildGetDocumentParams = buildGetDocumentParams;
  132. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;