data.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. const debug_1 = __importDefault(require("debug"));
  16. const stream_1 = require("stream");
  17. const crypto_1 = require("crypto");
  18. const data_uri_to_buffer_1 = __importDefault(require("data-uri-to-buffer"));
  19. const notmodified_1 = __importDefault(require("./notmodified"));
  20. const debug = debug_1.default('get-uri:data');
  21. class DataReadable extends stream_1.Readable {
  22. constructor(hash, buf) {
  23. super();
  24. this.push(buf);
  25. this.push(null);
  26. this.hash = hash;
  27. }
  28. }
  29. /**
  30. * Returns a Readable stream from a "data:" URI.
  31. */
  32. function get({ href: uri }, { cache }) {
  33. return __awaiter(this, void 0, void 0, function* () {
  34. // need to create a SHA1 hash of the URI string, for cacheability checks
  35. // in future `getUri()` calls with the same data URI passed in.
  36. const shasum = crypto_1.createHash('sha1');
  37. shasum.update(uri);
  38. const hash = shasum.digest('hex');
  39. debug('generated SHA1 hash for "data:" URI: %o', hash);
  40. // check if the cache is the same "data:" URI that was previously passed in.
  41. if (cache && cache.hash === hash) {
  42. debug('got matching cache SHA1 hash: %o', hash);
  43. throw new notmodified_1.default();
  44. }
  45. else {
  46. debug('creating Readable stream from "data:" URI buffer');
  47. const buf = data_uri_to_buffer_1.default(uri);
  48. return new DataReadable(hash, buf);
  49. }
  50. });
  51. }
  52. exports.default = get;
  53. //# sourceMappingURL=data.js.map