strong-data-uri
Parser and builder for `data:` URIs
Last updated 8 years ago by bajtos .
Artistic-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install strong-data-uri 
SYNC missed versions from official npm registry.

strong-data-uri

Build Status NPM version

Overview

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

API

decode(uri)

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);
console.log(buffer);
// <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
console.log(buffer.toString('ascii'));
// 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

encode(data, [mediatype])

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');
console.log(uri);
// data:text/plain;charset=UTF-8;base64,Zm9v

uri = dataUri.encode(new Buffer('<foo/>', 'utf8'), 'text/xml');
console.log(uri);
// data:text/xml;base64,PGZvby8+

Command-line access

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

Current Tags

  • 1.0.6                                ...           latest (8 years ago)

10 Versions

  • 1.0.6                                ...           8 years ago
  • 1.0.5                                ...           8 years ago
  • 1.0.4                                ...           10 years ago
  • 1.0.3                                ...           11 years ago
  • 1.0.2                                ...           11 years ago
  • 1.0.1                                ...           11 years ago
  • 1.0.0                                ...           11 years ago
  • 0.2.0                                ...           11 years ago
  • 0.1.1                                ...           12 years ago
  • 0.1.0                                ...           13 years ago
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (1)
Dev Dependencies (4)
Dependents (2)

Copyright 2013 - present © cnpmjs.org | Home |