test_utils.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.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 NodeCMapReaderFactory = function () {
  38. function NodeCMapReaderFactory(_ref) {
  39. var _ref$baseUrl = _ref.baseUrl,
  40. baseUrl = _ref$baseUrl === undefined ? null : _ref$baseUrl,
  41. _ref$isCompressed = _ref.isCompressed,
  42. isCompressed = _ref$isCompressed === undefined ? false : _ref$isCompressed;
  43. _classCallCheck(this, NodeCMapReaderFactory);
  44. this.baseUrl = baseUrl;
  45. this.isCompressed = isCompressed;
  46. }
  47. _createClass(NodeCMapReaderFactory, [{
  48. key: 'fetch',
  49. value: function fetch(_ref2) {
  50. var _this = this;
  51. var name = _ref2.name;
  52. if (!name) {
  53. return Promise.reject(new Error('CMap name must be specified.'));
  54. }
  55. return new Promise(function (resolve, reject) {
  56. var url = _this.baseUrl + name + (_this.isCompressed ? '.bcmap' : '');
  57. var fs = require('fs');
  58. fs.readFile(url, function (error, data) {
  59. if (error || !data) {
  60. reject(new Error('Unable to load ' + (_this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  61. return;
  62. }
  63. resolve({
  64. cMapData: new Uint8Array(data),
  65. compressionType: _this.isCompressed ? _util.CMapCompressionType.BINARY : _util.CMapCompressionType.NONE
  66. });
  67. });
  68. });
  69. }
  70. }]);
  71. return NodeCMapReaderFactory;
  72. }();
  73. exports.NodeFileReaderFactory = NodeFileReaderFactory;
  74. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;