fortify2-js
Version:
MOST POWERFUL JavaScript Security Library! Military-grade cryptography + 19 enhanced object methods + quantum-resistant algorithms + perfect TypeScript support. More powerful than Lodash with built-in security.
75 lines (71 loc) • 2.64 kB
JavaScript
;
var nehonixUriProcessor = require('nehonix-uri-processor');
var encoding = require('./encoding.js');
require('./memory/index.js');
function bufferDataConverter(input, outputFormat, options = {}) {
const { onError, onBuffer, onResult } = options;
try {
let result = null;
switch (outputFormat) {
case "hex":
result = encoding.bufferToHex(input);
onResult?.(result);
break;
case "base64":
result = encoding.bufferToBase64(input);
onResult?.(result);
break;
case "base58":
result = encoding.bufferToBase58(input);
onResult?.(result);
break;
case "binary":
result = encoding.bufferToBinary(input);
onResult?.(result);
break;
case "buffer":
result = null;
onBuffer?.(input);
break;
case "base64url":
result = encoding.bufferToBase64Url(input);
onResult?.(result);
break;
case "utf8":
result = encoding.bufferToString(input);
onResult?.(result);
break;
default:
try {
// Convert Uint8Array to string for processor.encode()
// Since processor.encode expects a string input, we need to convert binary data to string first
let inputString;
// Try UTF-8 decoding first (best for text data)
try {
inputString = new TextDecoder("utf-8", {
fatal: true,
}).decode(input);
}
catch {
// If UTF-8 decoding fails (binary data), convert to Base64 first
// This ensures the processor gets valid string input
inputString = encoding.bufferToBase64(input);
}
result = nehonixUriProcessor.__processor__.encode(inputString, outputFormat);
onResult?.(result);
}
catch (error) {
onError?.(error);
throw error;
}
break;
}
return result;
}
catch (error) {
onError?.(error);
throw error;
}
}
exports.bufferDataConverter = bufferDataConverter;
//# sourceMappingURL=dataConverter.js.map