node package manager

Introducing npm Enterprise add-ons. Integrate third-party dev tools into npm…


Parser and builder for <code>data:</code> URIs


strong-data-uri implements a parser for retrieving data encoded in data: URIs specified by RFC2397, as well as an encoder for those URIs.

Call dataUri.decode(uri) to parse the payload of a data URI. The uri argument expects a string.

var dataUri = require('strong-data-uri');
var uri = 'data:text/plain;charset=iso-8859-1;base64,aGVsbG8gd29ybGQ=';
var buffer = dataUri.decode(uri);
// <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64> 
// Hello world 
console.log(buffer.mimetype);  // text/plain 
console.log(buffer.mediatype); // text/plain;charset=iso-8859-1 
console.log(buffer.charset);   // iso-8859-1 

Use dataUri.encode(data, mediatype) to build a new data URI. The data argument can be a Buffer or a String. Strings are converted to buffers using utf-8 encoding.

If mediatype is not specified, then application/octet-stream is used as a default if the data is a Buffer, and text/plain;charset=UTF-8 if the data is a String.

var dataUri = require('strong-data-uri');
uri = dataUri.encode('foo');
// data:text/plain;charset=UTF-8;base64,Zm9v 
uri = dataUri.encode(new Buffer('<foo/>', 'utf8'), 'text/xml');
// data:text/xml;base64,PGZvby8+ 

To keep this project small and light, no command-line tool is provided. If you need one, please consider data-colon.