Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

blob-polyfill.ts

24 lines1011 Bytes

1// jsdom does not implement Blob.prototype.arrayBuffer, which AudioRecorder's
2// finalize() path relies on. Polyfill it (a no-op when the runtime already has
3// it, e.g. Node) so the framework hook tests can run under jsdom without
4// switching the whole suite to a Node environment. Registered once per package
5// via Vitest `setupFiles`.
6//
7// The `Partial<Blob>` view types `arrayBuffer` as optional so the feature check
8// isn't flagged "always falsy" — lib.dom declares it as always present.
9if (typeof Blob !== 'undefined') {
10 const blobProto = Blob.prototype as Partial<Blob>
11 if (typeof blobProto.arrayBuffer !== 'function') {
12 blobProto.arrayBuffer = function (this: Blob) {
13 return new Promise<ArrayBuffer>((resolve, reject) => {
14 const reader = new FileReader()
15 reader.onload = () => resolve(reader.result as ArrayBuffer)
16 reader.onerror = () => reject(reader.error)
17 reader.readAsArrayBuffer(this)
18 })
19 }
20 }
22​
23// Side-effect-only module.
24export {}