2020-08-25 23:57:08 +00:00
|
|
|
/**
|
|
|
|
* utilities for hashing config objects.
|
|
|
|
* basically iteratively updates hash with a JSON-like format
|
|
|
|
*/
|
2022-11-10 10:43:16 +00:00
|
|
|
'use strict';
|
|
|
|
exports.__esModule = true;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const createHash = require('crypto').createHash;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const stringify = JSON.stringify;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
function hashify(value, hash) {
|
2022-11-10 10:43:16 +00:00
|
|
|
if (!hash) hash = createHash('sha256');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
hashArray(value, hash);
|
2020-08-25 23:57:08 +00:00
|
|
|
} else if (value instanceof Object) {
|
2022-11-10 10:43:16 +00:00
|
|
|
hashObject(value, hash);
|
2020-08-25 23:57:08 +00:00
|
|
|
} else {
|
2022-11-10 10:43:16 +00:00
|
|
|
hash.update(stringify(value) || 'undefined');
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
return hash;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
exports.default = hashify;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
function hashArray(array, hash) {
|
2022-11-10 10:43:16 +00:00
|
|
|
if (!hash) hash = createHash('sha256');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
hash.update('[');
|
2020-08-25 23:57:08 +00:00
|
|
|
for (let i = 0; i < array.length; i++) {
|
2022-11-10 10:43:16 +00:00
|
|
|
hashify(array[i], hash);
|
|
|
|
hash.update(',');
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
hash.update(']');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
return hash;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
hashify.array = hashArray;
|
|
|
|
exports.hashArray = hashArray;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
function hashObject(object, hash) {
|
2022-11-10 10:43:16 +00:00
|
|
|
if (!hash) hash = createHash('sha256');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
hash.update('{');
|
2020-08-25 23:57:08 +00:00
|
|
|
Object.keys(object).sort().forEach(key => {
|
2022-11-10 10:43:16 +00:00
|
|
|
hash.update(stringify(key));
|
|
|
|
hash.update(':');
|
|
|
|
hashify(object[key], hash);
|
|
|
|
hash.update(',');
|
|
|
|
});
|
|
|
|
hash.update('}');
|
|
|
|
|
|
|
|
return hash;
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
hashify.object = hashObject;
|
|
|
|
exports.hashObject = hashObject;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
|