# datauri

> Create DataURI scheme easily

Latest version **4.1.0** (published 2021-08-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install datauri
pnpm add datauri
yarn add datauri
bun add datauri
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2021-08-22 |
| First published | 2012-09-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 10 |
| Dependencies | 2 |
| Unpacked size | 10.3 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 228 |
| Author | Helder Santana |
| Maintainers | ruyadorno, helder, caiogondim |
| Keywords | datauri, data uri, data, uri, data-uri, optimization, uri, optimize, inline, png, jpg, woff, base64 |

## Links

- npm: https://www.npmjs.com/package/datauri
- Repository: https://github.com/data-uri/datauri
- Homepage: https://github.com/data-uri/datauri#readme
- Issues: https://github.com/data-uri/datauri/issues
- npm.io page: https://npm.io/package/datauri

## Dependencies (2)

- [mimer](https://npm.io/package/mimer.md) ^2.0.2
- [image-size](https://npm.io/package/image-size.md) 1.0.0

## Alternatives

- [flatbuffers](https://npm.io/package/flatbuffers.md) — 6.0M weekly downloads
- [jwt-simple](https://npm.io/package/jwt-simple.md) — 259.5K weekly downloads
- [@exodus/patch-broken-hermes-typed-arrays](https://npm.io/package/@exodus/patch-broken-hermes-typed-arrays.md) — 28.5K weekly downloads
- [@native-to-anchor/buffer-layout](https://npm.io/package/@native-to-anchor/buffer-layout.md) — 12.2K weekly downloads
- [binary-parser-encoder](https://npm.io/package/binary-parser-encoder.md) — 5.3K weekly downloads

## Recent versions

- 4.1.0 (latest) — 2021-08-22
- 4.0.1 — 2021-08-08
- 4.0.0 — 2021-08-08
- 3.0.0 — 2020-05-03
- 2.0.0 — 2019-04-17
- 1.1.0 — 2018-02-11
- 1.0.5 — 2016-12-12
- 1.0.4 — 2016-02-12
- 1.0.3 — 2016-02-12
- 1.0.2 — 2016-02-12
- 1.0.1 — 2016-02-10
- 1.0.0 — 2016-02-10
- 1.0.0-alpha.5 — 2015-11-11
- 1.0.0-alpha.4 — 2015-11-07
- 1.0.0-alpha.3 — 2015-11-07
- … 22 more at https://npm.io/package/datauri/versions

## README

<h1 align="center">
  <br>
  <img width="365" src="https://cdn.rawgit.com/data-uri/datauri/master/media/datauri.svg" alt="datauri">
  <br>
  <br>
  <br>
</h1>

Node.js [Module](#module) and [CLI](http://npm.im/datauri-cli) to generate [Data URI scheme](http://en.wikipedia.org/wiki/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](http://en.wikipedia.org/wiki/Data_URI_scheme)

## MODULE [![Build Status](https://github.com/data-uri/datauri/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/data-uri/datauri/actions/workflows/main.yml?query=branch%3Amain)

`npm install datauri`

### Getting started

By default, datauri module returns a promise, which is resolved with `data:uri` string or rejected with read file error:

```js
const datauri = require('datauri');

const content = await datauri('test/myfile.png');

console.log(content);
//=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
```

### Callback style and meta data

```js
const datauri = require('datauri');

datauri('test/myfile.png', (err, content, meta) => {
  if (err) {
    throw err;
  }

  console.log(content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."

  console.log(meta.mimetype); //=> "image/png"
  console.log(meta.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..."
  console.log(meta.buffer); //=> file buffer
});
```

### CSS parser

```js
const datauriCSS = require('datauri/css');

await datauriCSS('test/myfile.png');
//=> "\n.case {\n    background-image: url('data:image/png; base64,iVBORw..."

await datauriCSS('test/myfile.png', {
  className: 'myClass',
  width: true,
  height: true
});
//=> adds image width and height and custom class name
```

### Synchronous calls

```js
const Datauri = require('datauri/sync');
const meta = Datauri('test/myfile.png');

console.log(meta.content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
console.log(meta.mimetype); //=> "image/png"
console.log(meta.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..."
console.log(meta.buffer); //=> file buffer
```

### From a Buffer

If you already have a file Buffer, that's the way to go:

```js
const DatauriParser = require('datauri/parser');
const parser = new DatauriParser();

const buffer = fs.readFileSync('./hello');

parser.format('.png', buffer); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
```

### From a string

```js
const DatauriParser = require('datauri/parser');
const parser = new DatauriParser();

parser.format('.png', 'xkcd'); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
```

## Contribute

```CLI
$ npm install
```

To run test specs

```CLI
$ npm test
```

## [ChangeLog](https://github.com/data-uri/datauri/releases)

## 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](https://github.com/data-uri)

(c) [Helder Santana](https://heldr.com)

---
_Source: https://npm.io/package/datauri · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
