UNPKG

@datastax/astra-db-ts

Version:
145 lines (144 loc) 5.34 kB
"use strict"; // Copyright Datastax, Inc // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.DataAPIBlob = exports.blob = void 0; const constants_js_1 = require("../../lib/constants.js"); const constants_js_2 = require("../../documents/collections/ser-des/constants.js"); const constants_js_3 = require("../../documents/tables/ser-des/constants.js"); const utils_js_1 = require("../../lib/utils.js"); const ctx_js_1 = require("../../lib/api/ser-des/ctx.js"); const utils_js_2 = require("../../documents/utils.js"); const utils_js_3 = require("../../lib/api/ser-des/utils.js"); const blob = (blob) => (blob instanceof DataAPIBlob) ? blob : new DataAPIBlob(blob); exports.blob = blob; class DataAPIBlob { [constants_js_2.$SerializeForCollection]() { throw (0, utils_js_3.mkTypeUnsupportedForCollectionsError)('DataAPIBlob', '_blob', [ 'Use another blob representation, such as a base64 string', ]); } ; [constants_js_3.$SerializeForTable](ctx) { return ctx.done({ $binary: this.asBase64() }); } ; static [constants_js_3.$DeserializeForTable](value, ctx) { return ctx.done(new DataAPIBlob((ctx.target === ctx_js_1.SerDesTarget.InsertedId) ? { $binary: value } : value, false)); } constructor(blob, validate = true) { Object.defineProperty(this, "_raw", { enumerable: true, configurable: true, writable: true, value: void 0 }); if (validate && !DataAPIBlob.isBlobLike(blob)) { throw (0, utils_js_2.mkInvArgsError)('new DataAPIBlob', [['blob', 'DataAPIBlob | ArrayBuffer | Buffer | { $binary: string }']], blob); } Object.defineProperty(this, '_raw', { value: (blob instanceof DataAPIBlob) ? blob._raw : blob, }); Object.defineProperty(this, constants_js_1.$CustomInspect, { value: this.toString.bind(this), }); } get byteLength() { if (this._raw instanceof ArrayBuffer) { return this._raw.byteLength; } if ('$binary' in this._raw) { return ~~((this._raw.$binary.replace(/=+$/, '').length * 3) / 4); } return this._raw.length; } /** * Gets the raw underlying implementation of the blob. * * @returns The raw blob */ raw() { return this._raw; } /** * Returns the blob as an `ArrayBuffer`, converting between types if necessary. * * @returns The blob as an `ArrayBuffer` */ asArrayBuffer() { if (this._raw instanceof ArrayBuffer) { return this._raw; } if ('$binary' in this._raw) { return base64ToArrayBuffer(this._raw.$binary); } return bufferToArrayBuffer(this._raw); } asBuffer() { if (typeof Buffer === 'undefined') { throw new Error("Buffer is not available in this environment"); } if (this._raw instanceof Buffer) { return this._raw; } if (this._raw instanceof ArrayBuffer) { return Buffer.from(this._raw); } return Buffer.from(this._raw.$binary, 'base64'); } asBase64() { if (this._raw instanceof ArrayBuffer) { return arrayBufferToBase64(this._raw); } if ('$binary' in this._raw) { return this._raw.$binary; } return this._raw.toString('base64'); } toString() { const type = (this._raw instanceof ArrayBuffer && 'ArrayBuffer') || (this._raw instanceof Buffer && 'Buffer') || 'base64'; return `DataAPIBlob(typeof raw=${type}, byteLength=${this.byteLength})`; } static isBlobLike(value) { return !!value && typeof value === 'object' && (value instanceof DataAPIBlob || ('$binary' in value && typeof value.$binary === 'string') || value instanceof ArrayBuffer || value instanceof Buffer); } } exports.DataAPIBlob = DataAPIBlob; const base64ToArrayBuffer = (0, utils_js_1.forJSEnv)({ server: (base64) => { return bufferToArrayBuffer(Buffer.from(base64, 'base64')); }, browser: (base64) => { const binaryString = window.atob(base64); const len = binaryString.length; const bytes = new Uint8Array(len); for (let i = 0; i < len; i++) { bytes[i] = binaryString.charCodeAt(i); } return bytes.buffer; }, unknown: () => { throw new Error("Cannot convert base64 to Buffer/ArrayBuffer in this environment; please do so manually"); }, }); const arrayBufferToBase64 = (0, utils_js_1.forJSEnv)({ server: (buffer) => { return Buffer.from(buffer).toString('base64'); }, browser: (buffer) => { let binary = ''; const bytes = new Uint8Array(buffer); for (const byte of bytes) { binary += String.fromCharCode(byte); } return window.btoa(binary); }, unknown: () => { throw new Error("Cannot convert Buffer/ArrayBuffer to base64 in this environment; please do so manually"); }, }); function bufferToArrayBuffer(b) { return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength); }