97
votes

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!

4
What is Blob? Where does it come from? - Pete
Yeah, but a blob isn't a native Node.js type.. You know, Number, String, Boolean, Object, Array, etc. Why don't you create an object prototype or a module for NPM? Did you check NPM for anything like what you need? - sent1nel
To me it seems that those who downvoted didn´t get that there is apparently no solution to this question, that´s why I am asking. - zer02
@Jonathan I will have a look on this, but is it possible to define the MIME type? - zer02
@zer02 Yes and no. You can always add a mime property to the Buffer since it's still dynamic. But, MIME types are for communicating binary data; not so much for saving to disk. - Jonathan Lonowski

4 Answers

36
votes

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;
13
votes

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
0
votes

Another solution to consider is to use a Base64 String to transfer data from the Server to the client.

I am working on a Node.js project where I receive audio data in the form of an ArrayBuffer, and I want to send and play that data in the browser. Most of my struggles came from trying to send the ArrayBuffer to the client or trying to convert the ArrayBuffer and send a Buffer.

What ended up being a simple solution for me was to

  1. Convert the ArrayBuffer to a Base64 encoded String on the Server
  2. Return/Send the Base64 String to the client from the server
  3. Create an Audio element/object on the client side and play the sound

I used base64-arraybuffer to perform the ArrayBuffer > Base64 String conversion (though, it may be simple to do this without a package).

I used tips from here to create the audio element on the client side.

*I haven't done much in the way of testing limits - so I don't know how this might handle large audio files.

-4
votes

As a suggestion, you might want to read this: http://howtonode.org/really-simple-file-uploads

I mean I guess I don't know what you're trying to do. There may not be a module for blobs, but if you want to just write something to disk, there's the fs module.. This code won't work directly, but..

var fs = require('fs')
  , express = require('express')

app.post('/upload', function (req, res) {
  // asynch call to write file to disk
  fs.write("/tmp/file.mp3", req.params.body, function (err) {
    if (err) console.log(err)
  });
  res.end();
});

Simply post an mp3, or anything really, to /upload, and it'll write it to disk. You can do whatever validation you want.