node package manager

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

datauri

Create DataURI scheme easily





Node.js Module and Client to generate Data URI scheme.

The data URI scheme is a uniform resource identifier (URI) scheme that provides a way to include data in-line in web pages as if they were external resources.

from: Wikipedia

npm install --save datauri

  1. From file path
  1. From a Buffer
  2. From a String
  3. Method chaining
  4. Task plugins using datauri
  1. Develop
  2. License
  3. ChangeLog
  4. Tools using datauri
const Datauri = require('datauri');
const datauri = new Datauri();
 
datauri.pipe(process.stdout);
datauri.encode('test/myfile.png');
const Datauri = require('datauri');
const datauri = new Datauri();
 
datauri.on('encoded', content => console.log(content));
//=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."; 
 
datauri.on('error', err => console.log(err));
datauri.encode('test/myfile.png');
'use strict';
 
const DataURI = require('datauri').promise;
// babelers: import { promise as DataURI } from 'datauri'; 
 
DataURI('test/myfile.png')
  .then(content => console.log(content))
  //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." 
  .catch(err => { throw err; });
const DataURI = require('datauri');
const datauri = new DataURI();
 
datauri.encode('test/myfile.png', (err, content) => {
  if (err) {
      throw err;
  }
 
  console.log(content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." 
 
  console.log(this.mimetype); //=> "image/png" 
  console.log(this.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..." 
  console.log(this.getCSS()); //=> "\n.case {\n    background-image: url('data:image/png;base64,iVBORw..." 
  console.log(this.getCSS({
    class: "myClass",
    width: true,
    height: true
  })); //=> adds image width and height and custom class name 
});
 

If DataURI class is instanciated with a file path, the same will be processed synchronously.

const Datauri = require('datauri');
let   datauri = new Datauri('test/myfile.png');
 
console.log(datauri.content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." 
console.log(datauri.mimetype); //=> "image/png" 
console.log(datauri.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..." 
console.log(datauri.getCSS()); //=> "\n.case {\n    background-image: url('data:image/png;base64,iVBORw..." 
console.log(datauri.getCSS("myClass")); //=> "\n.myClass {\n    background-image: url('data:image/png;base64,iVBORw..." 
const Datauri = require('datauri').sync;
 
console.log(Datauri('test/myfile.png')); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." 

or for ES2015/6 lovers

import { sync as DataURI } from 'datauri';
 
console.log(DataURI('test/myfile.png')); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." 

If you already have your file as a Buffer, use this. It's much faster than passing a string.

const Datauri = require('datauri'),
const datauri = new Datauri();
 
//... 
const buffer = fs.readFileSync('./hello');
//... 
 
datauri.format('.png', buffer);
 
console.log(datauri.content); //=> "data:image/png;base64,eGtjZA==" 
console.log(datauri.mimetype); //=> "image/png" 
console.log(datauri.base64); //=> "eGtjZA==" 
console.log(datauri.getCSS({
  class: "myClass",
  width: true,
  height: true
})); //=> adds image width and height and custom class name 
 
const DataURI = require('datauri');
const datauri = new Datauri();
 
datauri.format('.png', 'xkcd');
 
console.log(datauri.content); //=> "data:image/png;base64,eGtjZA==" 
console.log(datauri.mimetype); //=> "image/png" 
console.log(datauri.base64); //=> "eGtjZA==" 
console.log(datauri.getCSS({
  class: "myClass",
  width: true,
  height: true
})); //=> adds image width and height and custom class name 
 
//... 
datauri
  .on('encoded', content => {
    console.log(content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." 
    console.log(this.mimetype); //=> "image/png" 
    console.log(this.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..." 
    console.log(this.getCSS()); //=> "\n.case {\n    background-image: url('data:image/png;base64,iVBORw..." 
    console.log(this.getCSS({
      class: "myClass"
    }); //=> "\n.myClass {\n    background-image: url('data:image/png;base64,iVBORw..." 
  })
  .on('error', err => console.error(err))
  .encode('test/myfile.png');
$ npm install
$ npm run check

To run test specs

$ npm run spec

If you'd like to test the full process including npm installer, just run:

$ npm run fulltest

MIT License (c) Helder Santana