test_utils.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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.TEST_PDFS_PATH = exports.buildGetDocumentParams = exports.XRefMock = exports.NodeCMapReaderFactory = exports.NodeFileReaderFactory = undefined;
  27. 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; }; }();
  28. var _util = require('../../shared/util');
  29. var _primitives = require('../../core/primitives');
  30. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  31. var NodeFileReaderFactory = function () {
  32. function NodeFileReaderFactory() {
  33. _classCallCheck(this, NodeFileReaderFactory);
  34. }
  35. _createClass(NodeFileReaderFactory, null, [{
  36. key: 'fetch',
  37. value: function fetch(params) {
  38. var fs = require('fs');
  39. var file = fs.readFileSync(params.path);
  40. return new Uint8Array(file);
  41. }
  42. }]);
  43. return NodeFileReaderFactory;
  44. }();
  45. var TEST_PDFS_PATH = {
  46. dom: '../pdfs/',
  47. node: './test/pdfs/'
  48. };
  49. function buildGetDocumentParams(filename, options) {
  50. var params = Object.create(null);
  51. if ((0, _util.isNodeJS)()) {
  52. params.data = NodeFileReaderFactory.fetch({ path: TEST_PDFS_PATH.node + filename });
  53. } else {
  54. params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
  55. }
  56. for (var option in options) {
  57. params[option] = options[option];
  58. }
  59. return params;
  60. }
  61. var NodeCMapReaderFactory = function () {
  62. function NodeCMapReaderFactory(_ref) {
  63. var _ref$baseUrl = _ref.baseUrl,
  64. baseUrl = _ref$baseUrl === undefined ? null : _ref$baseUrl,
  65. _ref$isCompressed = _ref.isCompressed,
  66. isCompressed = _ref$isCompressed === undefined ? false : _ref$isCompressed;
  67. _classCallCheck(this, NodeCMapReaderFactory);
  68. this.baseUrl = baseUrl;
  69. this.isCompressed = isCompressed;
  70. }
  71. _createClass(NodeCMapReaderFactory, [{
  72. key: 'fetch',
  73. value: function fetch(_ref2) {
  74. var _this = this;
  75. var name = _ref2.name;
  76. if (!this.baseUrl) {
  77. return Promise.reject(new Error('CMap baseUrl must be specified, ' + 'see "PDFJS.cMapUrl" (and also "PDFJS.cMapPacked").'));
  78. }
  79. if (!name) {
  80. return Promise.reject(new Error('CMap name must be specified.'));
  81. }
  82. return new Promise(function (resolve, reject) {
  83. var url = _this.baseUrl + name + (_this.isCompressed ? '.bcmap' : '');
  84. var fs = require('fs');
  85. fs.readFile(url, function (error, data) {
  86. if (error || !data) {
  87. reject(new Error('Unable to load ' + (_this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  88. return;
  89. }
  90. resolve({
  91. cMapData: new Uint8Array(data),
  92. compressionType: _this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE
  93. });
  94. });
  95. });
  96. }
  97. }]);
  98. return NodeCMapReaderFactory;
  99. }();
  100. var XRefMock = function () {
  101. function XRefMock(array) {
  102. _classCallCheck(this, XRefMock);
  103. this._map = Object.create(null);
  104. for (var key in array) {
  105. var obj = array[key];
  106. this._map[obj.ref.toString()] = obj.data;
  107. }
  108. }
  109. _createClass(XRefMock, [{
  110. key: 'fetch',
  111. value: function fetch(ref) {
  112. return this._map[ref.toString()];
  113. }
  114. }, {
  115. key: 'fetchAsync',
  116. value: function fetchAsync(ref) {
  117. return Promise.resolve(this.fetch(ref));
  118. }
  119. }, {
  120. key: 'fetchIfRef',
  121. value: function fetchIfRef(obj) {
  122. if (!(0, _primitives.isRef)(obj)) {
  123. return obj;
  124. }
  125. return this.fetch(obj);
  126. }
  127. }, {
  128. key: 'fetchIfRefAsync',
  129. value: function fetchIfRefAsync(obj) {
  130. return Promise.resolve(this.fetchIfRef(obj));
  131. }
  132. }]);
  133. return XRefMock;
  134. }();
  135. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  136. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  137. exports.XRefMock = XRefMock;
  138. exports.buildGetDocumentParams = buildGetDocumentParams;
  139. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;