@criipto/signatures
Version:
A Node.JS SDK for Criipto Signatures
59 lines (58 loc) • 1.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function tryBase64Decode(input) {
try {
const decoded = Buffer.from(input, 'base64');
if (decoded.toString('base64') === input)
return decoded;
return null;
}
catch {
return null;
}
}
function parseBlobs(input) {
if (Array.isArray(input)) {
return input.map(i => parseBlobs(i));
}
if (input instanceof Object && !Array.isArray(input)) {
return Object.keys(input).reduce((memo, key) => {
if (key === 'blob') {
const decoded = tryBase64Decode(input[key]);
if (decoded) {
memo[key] = decoded;
return memo;
}
}
if (input[key] instanceof Object) {
memo[key] = parseBlobs(input[key]);
return memo;
}
memo[key] = input[key];
return memo;
}, {});
}
return input;
}
const jsonSerializer = {
stringify(obj) {
return JSON.stringify(obj, (key, value) => {
if (value instanceof Object && !Array.isArray(value)) {
return Object.keys(value).reduce((memo, key) => {
if (Buffer.isBuffer(value[key])) {
memo[key] = value[key].toString('base64');
}
else {
memo[key] = value[key];
}
return memo;
}, {});
}
return value;
});
},
parse(obj) {
return parseBlobs(JSON.parse(obj));
}
};
exports.default = jsonSerializer;