Last change
on this file since 8d391a1 was 6a3a178, checked in by Ema <ema_spirova@…>, 4 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Line | |
---|
1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const createHash = require("../util/createHash");
|
---|
9 |
|
---|
10 | /** @typedef {import("../util/Hash")} Hash */
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * @typedef {Object} HashableObject
|
---|
14 | * @property {function(Hash): void} updateHash
|
---|
15 | */
|
---|
16 |
|
---|
17 | class LazyHashedEtag {
|
---|
18 | /**
|
---|
19 | * @param {HashableObject} obj object with updateHash method
|
---|
20 | */
|
---|
21 | constructor(obj) {
|
---|
22 | this._obj = obj;
|
---|
23 | this._hash = undefined;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @returns {string} hash of object
|
---|
28 | */
|
---|
29 | toString() {
|
---|
30 | if (this._hash === undefined) {
|
---|
31 | const hash = createHash("md4");
|
---|
32 | this._obj.updateHash(hash);
|
---|
33 | this._hash = /** @type {string} */ (hash.digest("base64"));
|
---|
34 | }
|
---|
35 | return this._hash;
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | /** @type {WeakMap<HashableObject, LazyHashedEtag>} */
|
---|
40 | const map = new WeakMap();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @param {HashableObject} obj object with updateHash method
|
---|
44 | * @returns {LazyHashedEtag} etag
|
---|
45 | */
|
---|
46 | const getter = obj => {
|
---|
47 | const hash = map.get(obj);
|
---|
48 | if (hash !== undefined) return hash;
|
---|
49 | const newHash = new LazyHashedEtag(obj);
|
---|
50 | map.set(obj, newHash);
|
---|
51 | return newHash;
|
---|
52 | };
|
---|
53 |
|
---|
54 | module.exports = getter;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.