import { arrayBufferToBase64 } from 'nxdb-old/src/plugins/utils/utils-base64'; /** * Since NxDB 13.0.0 we only use Blob instead of falling back to Buffer, * because Node.js >18 supports Blobs anyway. */ /** * depending if we are on node or browser, * we have to use Buffer(node) or Blob(browser) */ export function createBlob( data: string, type: string ): Blob { const blob = new Blob([data], { type }); return blob; } export async function createBlobFromBase64( base64String: string, type: string ): Promise { const base64Response = await fetch(`data:${type};base64,${base64String}`); const blob = await base64Response.blob(); return blob; } export function isBlob(data: any): boolean { if (data instanceof Blob || (typeof Buffer !== 'undefined' && Buffer.isBuffer(data))) { return true; } else { return false; } } export function blobToString(blob: Blob | string): Promise { /** * in the electron-renderer we have a typed array instead of a blob * so we have to transform it. * @link https://github.com/nxpkg/nxdb/issues/1371 */ const blobType = Object.prototype.toString.call(blob); if (blobType === '[object Uint8Array]') { blob = new Blob([blob]); } if (typeof blob === 'string') { return Promise.resolve(blob); } return blob.text(); } export async function blobToBase64String(blob: Blob | string): Promise { if (typeof blob === 'string') { return blob; } /** * in the electron-renderer we have a typed array instead of a blob * so we have to transform it. * @link https://github.com/nxpkg/nxdb/issues/1371 */ const blobType = Object.prototype.toString.call(blob); if (blobType === '[object Uint8Array]') { blob = new Blob([blob]); } const fetchUrl = URL.createObjectURL(blob); const arrayBuffer = await fetch(fetchUrl).then(res => res.arrayBuffer()); return arrayBufferToBase64(arrayBuffer); } export function getBlobSize(blob: Blob): number { return blob.size; }