UNPKG

2.93 kBMarkdownView Raw
1data-uri-to-buffer
2==================
3### Generate a Buffer instance from a [Data URI][rfc] string
4[![Build Status](https://travis-ci.org/TooTallNate/node-data-uri-to-buffer.svg?branch=master)](https://travis-ci.org/TooTallNate/node-data-uri-to-buffer)
5
6This module accepts a ["data" URI][rfc] String of data, and returns a
7node.js `Buffer` instance with the decoded data.
8
9
10Installation
11------------
12
13Install with `npm`:
14
15``` bash
16$ npm install data-uri-to-buffer
17```
18
19
20Example
21-------
22
23``` js
24var dataUriToBuffer = require('data-uri-to-buffer');
25
26// plain-text data is supported
27var uri = 'data:,Hello%2C%20World!';
28var decoded = dataUriToBuffer(uri);
29console.log(decoded.toString());
30// 'Hello, World!'
31
32// base64-encoded data is supported
33uri = 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D';
34decoded = dataUriToBuffer(uri);
35console.log(decoded.toString());
36// 'Hello, World!'
37```
38
39
40API
41---
42
43### dataUriToBuffer(String uri) → Buffer
44
45The `type` property on the Buffer instance gets set to the main type portion of
46the "mediatype" portion of the "data" URI, or defaults to `"text/plain"` if not
47specified.
48
49The `typeFull` property on the Buffer instance gets set to the entire
50"mediatype" portion of the "data" URI (including all parameters), or defaults
51to `"text/plain;charset=US-ASCII"` if not specified.
52
53The `charset` property on the Buffer instance gets set to the Charset portion of
54the "mediatype" portion of the "data" URI, or defaults to `"US-ASCII"` if the
55entire type is not specified, or defaults to `""` otherwise.
56
57*Note*: If the only the main type is specified but not the charset, e.g.
58`"data:text/plain,abc"`, the charset is set to the empty string. The spec only
59defaults to US-ASCII as charset if the entire type is not specified.
60
61
62License
63-------
64
65(The MIT License)
66
67Copyright (c) 2014 Nathan Rajlich <nathan@tootallnate.net>
68
69Permission is hereby granted, free of charge, to any person obtaining
70a copy of this software and associated documentation files (the
71'Software'), to deal in the Software without restriction, including
72without limitation the rights to use, copy, modify, merge, publish,
73distribute, sublicense, and/or sell copies of the Software, and to
74permit persons to whom the Software is furnished to do so, subject to
75the following conditions:
76
77The above copyright notice and this permission notice shall be
78included in all copies or substantial portions of the Software.
79
80THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
81EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
82MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
83IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
84CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
85TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
86SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
87
88[rfc]: http://tools.ietf.org/html/rfc2397