MCPcopy Create free account
hub / github.com/TanStack/ai / base64ToArrayBuffer

Function base64ToArrayBuffer

packages/ai-utils/src/base64.ts:61–83  ·  view source on GitHub ↗
(base64: string)

Source from the content-addressed store, hash-verified

59 * Decode a base64 string into an `ArrayBuffer`.
60 */
61export function base64ToArrayBuffer(base64: string): ArrayBuffer {
62 // eslint-disable-next-line no-restricted-syntax -- feature-detecting Uint8Array.fromBase64 Stage-3 proposal not yet in lib.es types
63 const fast = (Uint8Array as unknown as Uint8ArrayWithBase64).fromBase64
64 if (typeof fast === 'function') {
65 return fast(base64).buffer as ArrayBuffer
66 }
67
68 if (typeof atob === 'function') {
69 const binary = atob(base64)
70 const bytes = new Uint8Array(binary.length)
71 for (let i = 0; i < binary.length; i++) {
72 bytes[i] = binary.charCodeAt(i)
73 }
74 return bytes.buffer
75 }
76
77 if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {
78 const buf = Buffer.from(base64, 'base64')
79 return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)
80 }
81
82 throw new Error('No base64 decoder available in this environment.')
83}

Callers 4

imagePartToFileFunction · 0.90
prepareAudioFileMethod · 0.90
prepareAudioFileMethod · 0.90
base64.test.tsFile · 0.90

Calls 1

fromMethod · 0.80

Tested by

no test coverage detected