base64-encoding
Version:
Fast Base64 encoding and decoding powered by WebAssembly.
126 lines • 4.85 kB
JavaScript
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Base64Encoder_provider, _Base64Encoder_urlFriendly, _Base64Decoder_provider;
import { jsImpl, WASMImpl } from './base64.js';
class Base64Provider {
constructor() {
Object.defineProperty(this, "impl", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
if (typeof Uint8Array === 'undefined' || typeof DataView === 'undefined') {
throw Error('Platform unsupported. Make sure Uint8Array and DataView exist.');
}
this.impl = jsImpl;
}
async init() {
if (typeof WebAssembly !== 'undefined') {
try {
this.impl = await new WASMImpl().init();
}
catch (err) {
throw new Error('WASM instantiation failed: ' + err.message);
}
}
else {
throw new Error('WebAssembly missing in global scope.');
}
}
encode(input, url) {
return this.impl.encode(input, url);
}
decode(input) {
return this.impl.decode(input);
}
}
/**
* Base64 encoder class to encode binary data in Base64 strings,
* similar to `TextEncoder`.
*/
export class Base64Encoder {
/**
* Creates a new encoder object with underlying WebAssembly instance.
*
* Note that the WASM instance might grow its internal memory to fit large
* array buffers.
*/
constructor(options = {}) {
var _a;
_Base64Encoder_provider.set(this, new Base64Provider());
_Base64Encoder_urlFriendly.set(this, void 0);
const { url, urlFriendly } = options;
__classPrivateFieldSet(this, _Base64Encoder_urlFriendly, (_a = url !== null && url !== void 0 ? url : urlFriendly) !== null && _a !== void 0 ? _a : false, "f");
}
/**
* Whether this encoder is set to encode data as URL-friendly Base64.
*/
get url() {
return __classPrivateFieldGet(this, _Base64Encoder_urlFriendly, "f");
}
;
/**
* @deprecated Use `url` instead.
*/
get urlFriendly() {
return this.url;
}
;
/**
* Optimize this encoder to use the faster WASM implementation.
* @returns This encoder after WASM initialization has completed.
*/
async optimize() {
await __classPrivateFieldGet(this, _Base64Encoder_provider, "f").init();
return this;
}
/**
* Encodes an array buffer into a Base64 string.
*
* @param input Binary data to be Base64 encoded
* @returns The provided array buffer encoded as a Base64 string
*/
encode(input) {
return __classPrivateFieldGet(this, _Base64Encoder_provider, "f").encode(input, this.url);
}
}
_Base64Encoder_provider = new WeakMap(), _Base64Encoder_urlFriendly = new WeakMap();
/**
* Base64 Decoder class to convert Base64 strings into array buffers,
* similar to `TextDecoder`.
*/
export class Base64Decoder {
constructor() {
_Base64Decoder_provider.set(this, new Base64Provider());
}
/**
* Optimize this decoder to use the faster WASM implementation.
* @returns This decoder after WASM initialization has completed.
*/
async optimize() {
await __classPrivateFieldGet(this, _Base64Decoder_provider, "f").init();
return this;
}
/**
* Decodes a Base64 string and returns a new array buffer.
*
* @param input A Base64 string. Can be either URL friendly or not and
* padding may be omitted.
* @returns The binary data as an array buffer.
*/
decode(input) {
return __classPrivateFieldGet(this, _Base64Decoder_provider, "f").decode(input);
}
}
_Base64Decoder_provider = new WeakMap();
//# sourceMappingURL=index.js.map