asn1-ts
Version:
ASN.1 encoding and decoding, including BER, CER, and DER.
15 lines (14 loc) • 592 B
JavaScript
import { ASN1Error } from "../errors.mjs";
import { Buffer } from "node:buffer";
export default function convertBytesToText(bytes, codec = "utf-8") {
if (typeof Buffer !== "undefined") {
if (bytes instanceof Buffer) {
return bytes.toString(codec);
}
return (Buffer.from(bytes.buffer, bytes.byteOffset, bytes.length)).toString(codec);
}
else if (typeof TextEncoder !== "undefined") {
return (new TextDecoder(codec)).decode(bytes);
}
throw new ASN1Error("Neither TextDecoder nor Buffer are defined to decode bytes into text.");
}