test_utils.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.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. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  23. var NodeFileReaderFactory = function () {
  24. function NodeFileReaderFactory() {
  25. _classCallCheck(this, NodeFileReaderFactory);
  26. }
  27. _createClass(NodeFileReaderFactory, null, [{
  28. key: 'fetch',
  29. value: function fetch(params) {
  30. var fs = require('fs');
  31. var file = fs.readFileSync(params.path);
  32. return new Uint8Array(file);
  33. }
  34. }]);
  35. return NodeFileReaderFactory;
  36. }();
  37. var TEST_PDFS_PATH = {
  38. dom: '../pdfs/',
  39. node: './test/pdfs/'
  40. };
  41. function buildGetDocumentParams(filename, options) {
  42. var params = Object.create(null);
  43. if ((0, _util.isNodeJS)()) {
  44. params.data = NodeFileReaderFactory.fetch({ path: TEST_PDFS_PATH.node + filename });
  45. } else {
  46. params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
  47. }
  48. for (var option in options) {
  49. params[option] = options[option];
  50. }
  51. return params;
  52. }
  53. var NodeCMapReaderFactory = function () {
  54. function NodeCMapReaderFactory(_ref) {
  55. var _ref$baseUrl = _ref.baseUrl,
  56. baseUrl = _ref$baseUrl === undefined ? null : _ref$baseUrl,
  57. _ref$isCompressed = _ref.isCompressed,
  58. isCompressed = _ref$isCompressed === undefined ? false : _ref$isCompressed;
  59. _classCallCheck(this, NodeCMapReaderFactory);
  60. this.baseUrl = baseUrl;
  61. this.isCompressed = isCompressed;
  62. }
  63. _createClass(NodeCMapReaderFactory, [{
  64. key: 'fetch',
  65. value: function fetch(_ref2) {
  66. var _this = this;
  67. var name = _ref2.name;
  68. if (!name) {
  69. return Promise.reject(new Error('CMap name must be specified.'));
  70. }
  71. return new Promise(function (resolve, reject) {
  72. var url = _this.baseUrl + name + (_this.isCompressed ? '.bcmap' : '');
  73. var fs = require('fs');
  74. fs.readFile(url, function (error, data) {
  75. if (error || !data) {
  76. reject(new Error('Unable to load ' + (_this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  77. return;
  78. }
  79. resolve({
  80. cMapData: new Uint8Array(data),
  81. compressionType: _this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE
  82. });
  83. });
  84. });
  85. }
  86. }]);
  87. return NodeCMapReaderFactory;
  88. }();
  89. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  90. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;
  91. exports.buildGetDocumentParams = buildGetDocumentParams;
  92. exports.TEST_PDFS_PATH = TEST_PDFS_PATH;