1 | # Parse `data:` URLs
|
2 |
|
3 | This package helps you parse `data:` URLs [according to the WHATWG Fetch Standard](https://fetch.spec.whatwg.org/#data-urls):
|
4 |
|
5 | ```js
|
6 | const parseDataURL = require("data-url");
|
7 |
|
8 | const textExample = parseDataURL("data:,Hello%2C%20World!");
|
9 | console.log(textExample.mimeType.toString()); // "text/plain;charset=US-ASCII"
|
10 | console.log(textExample.body.toString()); // "Hello, World!"
|
11 |
|
12 | const htmlExample = dataURL("data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E");
|
13 | console.log(htmlExample.mimeType.toString()); // "text/html"
|
14 | console.log(htmlExample.body.toString()); // <h1>Hello, World!</h1>
|
15 |
|
16 | const pngExample = parseDataURL("data:image/png;base64,iVBORw0KGgoAAA" +
|
17 | "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
|
18 | "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
|
19 | "5ErkJggg==");
|
20 | console.log(pngExample.mimeType.toString()); // "image/png"
|
21 | console.log(pngExample.body); // <Buffer 89 50 4e 47 0d ... >
|
22 | ```
|
23 |
|
24 | ## API
|
25 |
|
26 | This package's main module's default export is a function that accepts a string and returns a `{ mimeType, body }` object, or `null` if the result cannot be parsed as a `data:` URL.
|
27 |
|
28 | - The `mimeType` property is an instance of [whatwg-mimetype](https://www.npmjs.com/package/whatwg-mimetype)'s `MIMEType` class.
|
29 | - The `body` property is a Node.js [`Buffer`](https://nodejs.org/docs/latest/api/buffer.html) instance.
|
30 |
|
31 | As shown in the examples above, both of these have useful `toString()` methods for manipulating them as string values. However…
|
32 |
|
33 | ### A word of caution on string decoding
|
34 |
|
35 | Because Node.js's `Buffer.prototype.toString()` assumes a UTF-8 encoding, simply doing `dataURL.body.toString()` may not work correctly if the `data:` URLs contents were not originally written in UTF-8. This includes if the encoding is "US-ASCII", [aka windows-1252](https://encoding.spec.whatwg.org/#names-and-labels), which is notable for being the default in many cases.
|
36 |
|
37 | A more complete decoding example would use the [whatwg-encoding](https://www.npmjs.com/package/whatwg-encoding) package as follows:
|
38 |
|
39 | ```js
|
40 | const parseDataURL = require("data-url");
|
41 | const { labelToName, decode } = require("whatwg-encoding");
|
42 |
|
43 | const dataURL = parseDataURL(arbitraryString);
|
44 | const encodingName = labelToName(dataURL.mimeType.parameters.get("charset"));
|
45 | const bodyDecoded = decode(dataURL.body, encodingName);
|
46 | ```
|
47 |
|
48 | For example, given an `arbitraryString` of `data:,Hello!`, this will produce a `bodyDecoded` of `"Hello!"`, as expected. But given an `arbitraryString` of `"data:,Héllo!"`, this will correctly produce a `bodyDecoded` of `"Héllo!"`, whereas just doing `dataURL.body.toString()` will give back `"Héllo!"`.
|
49 |
|
50 | In summary, only use `dataURL.body.toString()` when you are very certain your data is inside the ASCII range (i.e. code points within the range U+0000 to U+007F).
|
51 |
|
52 | ### Advanced functionality: parsing from a URL record
|
53 |
|
54 | If you are using the [whatwg-url](https://github.com/jsdom/whatwg-url) package, you may already have a "URL record" object on hand, as produced by that package's `parseURL` export. In that case, you can use this package's `fromURLRecord` export to save a bit of work:
|
55 |
|
56 | ```js
|
57 | const { parseURL } = require("whatwg-url");
|
58 | const dataURLFromURLRecord = require("data-url").fromURLRecord;
|
59 |
|
60 | const urlRecord = parseURL("data:,Hello%2C%20World!");
|
61 | const dataURL = dataURLFromURLRecord(urlRecord);
|
62 | ```
|
63 |
|
64 | In practice, we expect this functionality only to be used by consumers like [jsdom](https://www.npmjs.com/package/jsdom), which are using these packages at a very low level.
|