UNPKG

1.63 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.data = void 0;
7const debug_1 = __importDefault(require("debug"));
8const stream_1 = require("stream");
9const crypto_1 = require("crypto");
10const data_uri_to_buffer_1 = __importDefault(require("data-uri-to-buffer"));
11const notmodified_1 = __importDefault(require("./notmodified"));
12const debug = (0, debug_1.default)('get-uri:data');
13class 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 */
24const 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 buf = (0, data_uri_to_buffer_1.default)(uri);
39 return new DataReadable(hash, buf);
40 }
41};
42exports.data = data;
43//# sourceMappingURL=data.js.map
\No newline at end of file