data.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.data = void 0;
  7. const debug_1 = __importDefault(require("debug"));
  8. const stream_1 = require("stream");
  9. const crypto_1 = require("crypto");
  10. const data_uri_to_buffer_1 = require("data-uri-to-buffer");
  11. const notmodified_1 = __importDefault(require("./notmodified"));
  12. const debug = (0, debug_1.default)('get-uri:data');
  13. class DataReadable extends stream_1.Readable {
  14. constructor(hash, buf) {
  15. super();
  16. this.push(buf);
  17. this.push(null);
  18. this.hash = hash;
  19. }
  20. }
  21. /**
  22. * Returns a Readable stream from a "data:" URI.
  23. */
  24. const data = async ({ href: uri }, { cache } = {}) => {
  25. // need to create a SHA1 hash of the URI string, for cacheability checks
  26. // in future `getUri()` calls with the same data URI passed in.
  27. const shasum = (0, crypto_1.createHash)('sha1');
  28. shasum.update(uri);
  29. const hash = shasum.digest('hex');
  30. debug('generated SHA1 hash for "data:" URI: %o', hash);
  31. // check if the cache is the same "data:" URI that was previously passed in.
  32. if (cache?.hash === hash) {
  33. debug('got matching cache SHA1 hash: %o', hash);
  34. throw new notmodified_1.default();
  35. }
  36. else {
  37. debug('creating Readable stream from "data:" URI buffer');
  38. const { buffer } = (0, data_uri_to_buffer_1.dataUriToBuffer)(uri);
  39. return new DataReadable(hash, Buffer.from(buffer));
  40. }
  41. };
  42. exports.data = data;
  43. //# sourceMappingURL=data.js.map