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