@itwin/core-common
Version:
iTwin.js components common to frontend and backend
64 lines • 3.12 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Entities
*/
import { Base64 } from "js-base64";
/** Represents an array of bytes encoded in base-64 with a prefix indicating the encoding, as persisted in an [ECDb]($backend) for properties of `binary` type.
* @public
*/
export var Base64EncodedString;
(function (Base64EncodedString) {
/** The prefix prepended to the string identifying it as base-64-encoded. */
Base64EncodedString.prefix = "encoding=base64;";
/** Encode an array of bytes into a Base64EncodedString. */
function fromUint8Array(bytes) {
return `${Base64EncodedString.prefix}${Base64.fromUint8Array(bytes)}`;
}
Base64EncodedString.fromUint8Array = fromUint8Array;
/** Decode a Base64EncodedString into an array of bytes. */
function toUint8Array(base64) {
return Base64.toUint8Array(stripPrefix(base64));
}
Base64EncodedString.toUint8Array = toUint8Array;
/** Returns true if the input starts with [[Base64EncodedString.prefix]] indicating it is a well-formed Base64EncodedString. */
function hasPrefix(str) {
return str.startsWith(Base64EncodedString.prefix);
}
Base64EncodedString.hasPrefix = hasPrefix;
/** Ensure that the base-64-encoded string starts with the [[Base64EncodedString.prefix]]. */
function ensurePrefix(base64) {
return hasPrefix(base64) ? base64 : `${Base64EncodedString.prefix}${base64}`;
}
Base64EncodedString.ensurePrefix = ensurePrefix;
/** Remove the [[Base64EncodedString.prefix]] from the string if present. */
function stripPrefix(base64) {
return hasPrefix(base64) ? base64.substring(Base64EncodedString.prefix.length) : base64;
}
Base64EncodedString.stripPrefix = stripPrefix;
/** A function suitable for use with `JSON.parse` to revive a Base64EncodedString into a Uint8Array. */
Base64EncodedString.reviver = (_name, value) => {
if (typeof value === "string" && hasPrefix(value))
value = toUint8Array(value);
return value;
};
/** A function suitable for use with `JSON.stringify` to serialize a Uint8Array as a Base64EncodedString. */
Base64EncodedString.replacer = (_name, value) => {
if (value && value.constructor === Uint8Array)
value = fromUint8Array(value);
return value;
};
/* Encode a string into a Base64EncodedString. */
function encode(src, urlSafe) {
return Base64.encode(src, urlSafe);
}
Base64EncodedString.encode = encode;
/* Decode a Base64EncodedString into a string. */
function decode(src) {
return Base64.decode(src);
}
Base64EncodedString.decode = decode;
})(Base64EncodedString || (Base64EncodedString = {}));
//# sourceMappingURL=Base64EncodedString.js.map