@toruslabs/metadata-helpers
Version:
Helper methods for metadata
65 lines (57 loc) • 1.98 kB
JavaScript
import { bytesToUtf8, base64ToBytes, utf8ToBytes, bytesToBase64 } from './bytes.js';
/**
* Pad a string to a multiple of segment length (4 for base64url).
* @param input - The string to pad.
* @returns The padded string.
*/
function padString(input) {
const segmentLength = 4;
const diff = input.length % segmentLength;
if (!diff) {
return input;
}
return input + "=".repeat(segmentLength - diff);
}
/**
* Encode a string or Uint8Array to a base64url string.
* @param input - The string (only support utf8 encoding) or Uint8Array to encode.
* @returns The base64url string.
*/
function encodeBase64Url(input) {
const bytes = typeof input === "string" ? utf8ToBytes(input) : input;
return fromBase64(bytesToBase64(bytes));
}
/**
* Decode a base64url string to a string or Uint8Array.
* @param base64url - The base64url string to decode.
* @returns The decoded string or Uint8Array.
*/
function decodeBase64Url(base64url) {
return bytesToUtf8(base64ToBytes(toBase64(base64url)));
}
/**
* Convert a base64url string to a base64 string.
* @param base64url - The base64url string or Uint8Array (will be converted to string utf8 encoding) to convert.
* @returns The base64 string.
*/
function toBase64(base64url) {
const urlString = base64url instanceof Uint8Array ? bytesToUtf8(base64url) : base64url;
return padString(urlString).replace(/-/g, "+").replace(/_/g, "/");
}
/**
* Decode a base64 string to a base64url string.
* @param base64 - The base64 string to decode.
* @returns The base64url string.
*/
function fromBase64(base64) {
return base64.replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
}
/**
* Convert a base64url string to a buffer-like object.
* @param base64url - The base64url string to convert.
* @returns The buffer-like object.
*/
function toBufferLike(base64url) {
return base64ToBytes(toBase64(base64url));
}
export { decodeBase64Url, encodeBase64Url, fromBase64, toBase64, toBufferLike };