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 = __importDefault(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 |
|
23 |
|
24 | const data = async ({ href: uri }, { cache } = {}) => {
|
25 |
|
26 |
|
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 |
|
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 | };
|
42 | exports.data = data;
|
43 |
|
\ | No newline at end of file |