test_utils.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. var sharedUtil = require('../../shared/util.js');
  17. var CMapCompressionType = sharedUtil.CMapCompressionType;
  18. var NodeCMapReaderFactory = function NodeCMapReaderFactoryClosure() {
  19. function NodeCMapReaderFactory(params) {
  20. this.baseUrl = params.baseUrl || null;
  21. this.isCompressed = params.isCompressed || false;
  22. }
  23. NodeCMapReaderFactory.prototype = {
  24. fetch: function (params) {
  25. var name = params.name;
  26. if (!name) {
  27. return Promise.reject(new Error('CMap name must be specified.'));
  28. }
  29. return new Promise(function (resolve, reject) {
  30. var url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
  31. var fs = require('fs');
  32. fs.readFile(url, function (error, data) {
  33. if (error || !data) {
  34. reject(new Error('Unable to load ' + (this.isCompressed ? 'binary ' : '') + 'CMap at: ' + url));
  35. return;
  36. }
  37. resolve({
  38. cMapData: new Uint8Array(data),
  39. compressionType: this.isCompressed ? CMapCompressionType.BINARY : CMapCompressionType.NONE
  40. });
  41. }.bind(this));
  42. }.bind(this));
  43. }
  44. };
  45. return NodeCMapReaderFactory;
  46. }();
  47. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;