2
0

test_utils.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. if (!params.name) {
  26. return Promise.reject(new Error('CMap name must be specified.'));
  27. }
  28. return new Promise(function (resolve, reject) {
  29. var url = this.baseUrl + params.name;
  30. var fs = require('fs');
  31. if (this.isCompressed) {
  32. url += '.bcmap';
  33. }
  34. fs.readFile(url, function (error, data) {
  35. if (error || !data) {
  36. reject(new Error('Unable to load ' + (this.isCompressed ? 'binary' : '') + ' CMap at: ' + url));
  37. return;
  38. }
  39. resolve({
  40. cMapData: new Uint8Array(data),
  41. compressionType: this.isCompressed ? CMapCompressionType.BINARY : CMapCompressionType.NONE
  42. });
  43. }.bind(this));
  44. }.bind(this));
  45. }
  46. };
  47. return NodeCMapReaderFactory;
  48. }();
  49. exports.NodeCMapReaderFactory = NodeCMapReaderFactory;