Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js can't create Blobs?

I am working with node.js and I streamed my Audio to my node.js server. Now I noticed during the process of building the audio blob:

var audioBlob = new Blob([dataview], { type: 'audio/wav' }); 

That I get a ReferenceError at new Blob. It seems that Blob is not supported. How can I create a blob which I would like to save with node.js fs module.

Thanks guys!

like image 536
zer02 Avatar asked Feb 01 '13 19:02

zer02


People also ask

How do you declare a blob in node JS?

The "Blob is not defined" error occurs when the Blob class is used without being imported in a Node. js application. To solve the error import the Blob class before using it, e.g. import { Blob } from 'buffer'; . To solve the error, import the Blob class before using it.

Does node support blob?

Yeah, but a blob isn't a native Node. js type.. You know, Number, String, Boolean, Object, Array, etc.

What is Blob () in JS?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.

What is Blob protocol?

Blob URL/Object URL is a pseudo protocol to allow Blob and File objects to be used as URL source for things like images, download links for binary data and so forth. For example, you can not hand an Image object raw byte-data as it would not know what to do with it.


2 Answers

The Solution to this problem is to create a function which can convert between Array Buffers and Node Buffers. :)

Convert a binary NodeJS Buffer to JavaScript ArrayBuffer

In recent node versions it's just:

let buffer = Buffer.from(arraybuffer); let arraybuffer = Uint8Array.from(buffer).buffer; 
like image 173
zer02 Avatar answered Sep 28 '22 05:09

zer02


Since Node.js 16, Blob can be imported:

import {Blob} from 'node:buffer';  new Blob([]); //=> Blob {size: 0, type: ''} 

Otherwise, just use cross-blob:

import Blob from 'cross-blob';   new Blob([]); //=> Blob {size: 0, type: ''}   // Global patch (to support external modules like is-blob). globalThis.Blob = Blob; 
like image 44
Richie Bendall Avatar answered Sep 28 '22 06:09

Richie Bendall