What is datauri?
The datauri npm package is used to convert file paths or file buffers to Data URI scheme. This is particularly useful for embedding file data directly into HTML or CSS files.
What are datauri's main functionalities?
Convert File Path to Data URI
This feature allows you to convert a file path to a Data URI. The `format` method takes the file extension and the file path as arguments and returns an object containing the Data URI.
const Datauri = require('datauri');
const dUri = new Datauri();
const result = dUri.format('.png', 'path/to/file.png');
console.log(result.content);
Convert Buffer to Data URI
This feature allows you to convert a buffer to a Data URI. The `format` method takes the file extension and the buffer as arguments and returns an object containing the Data URI.
const Datauri = require('datauri');
const dUri = new Datauri();
const buffer = Buffer.from('some data');
const result = dUri.format('.txt', buffer);
console.log(result.content);
Convert File Path to Data URI (Async)
This feature allows you to convert a file path to a Data URI using an asynchronous method. The `promise` method takes the file path as an argument and returns a promise that resolves to the Data URI.
const Datauri = require('datauri');
const dUri = new Datauri();
const result = await dUri.promise('path/to/file.png');
console.log(result);
Other packages similar to datauri
base64-img
The base64-img package allows you to convert images to base64 strings and vice versa. While it is more focused on image files, it provides similar functionality for converting files to Data URIs.
file-base64
The file-base64 package is another alternative that focuses on converting files to base64 strings. It provides a simple API for encoding and decoding files, similar to datauri.
Node.js Module and CLI 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
MODULE
npm install -S datauri
Getting started
By default, datauri module returns a promise, which is resolved with data:uri
string or rejected with file read error:
const datauri = require('datauri');
const content = await datauri('test/myfile.png');
console.log(content);
Callback style and meta data
const datauri = require('datauri');
datauri('test/myfile.png', (err, content, meta) => {
if (err) {
throw err;
}
console.log(content);
console.log(meta.mimetype);
console.log(meta.base64);
});
CSS parser
const datauriCSS = require('datauri/css');
await datauriCSS('test/myfile.png');
await datauriCSS('test/myfile.png', {
className: 'myClass',
width: true,
height: true
});
Synchronous calls
const Datauri = require('datauri/sync');
const meta = Datauri('test/myfile.png');
console.log(meta.content);
console.log(meta.mimetype);
console.log(meta.base64);
console.log(meta.getCSS());
console.log(meta.getCSS('myClass'));
From a Buffer
If you already have a file Buffer, that's the way to go:
const DatauriParser = require('datauri/parser');
const parser = new DatauriParser();
const buffer = fs.readFileSync('./hello');
parser.format('.png', buffer);
From a string
const DatauriParser = require('datauri/parser');
const parser = new DatauriParser();
parser.format('.png', 'xkcd');
Contribute
$ npm install
To run test specs
$ npm test
Requirements
Node.js 10+
Previous Node versions and deprecated features:
Node.js 8
npm install --save datauri@3
docs: https://github.com/data-uri/datauri/blob/v3.0.0/docs/datauri.md
Node.js 4+
npm install --save datauri@2
docs: https://github.com/data-uri/datauri/blob/v2.0.0/docs/datauri.md
License
MIT License
(c) Data-URI.js
(c) Helder Santana