Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Parse `data:` URLs
  2. This package helps you parse `data:` URLs [according to the WHATWG Fetch Standard](https://fetch.spec.whatwg.org/#data-urls):
  3. ```js
  4. const parseDataURL = require("data-urls");
  5. const textExample = parseDataURL("data:,Hello%2C%20World!");
  6. console.log(textExample.mimeType.toString()); // "text/plain;charset=US-ASCII"
  7. console.log(textExample.body); // Uint8Array(13) [ 72, 101, 108, 108, 111, 44, … ]
  8. const htmlExample = parseDataURL("data:text/html,%3Ch1%3EHello%2C%20World!%3C%2Fh1%3E");
  9. console.log(htmlExample.mimeType.toString()); // "text/html"
  10. console.log(htmlExample.body); // Uint8Array(22) [ 60, 104, 49, 62, 72, 101, … ]
  11. const pngExample = parseDataURL("data:image/png;base64,iVBORw0KGgoAAA" +
  12. "ANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4" +
  13. "//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU" +
  14. "5ErkJggg==");
  15. console.log(pngExample.mimeType.toString()); // "image/png"
  16. console.log(pngExample.body); // Uint8Array(85) [ 137, 80, 78, 71, 13, 10, … ]
  17. ```
  18. ## API
  19. 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.
  20. - The `mimeType` property is an instance of [whatwg-mimetype](https://www.npmjs.com/package/whatwg-mimetype)'s `MIMEType` class.
  21. - The `body` property is a `Uint8Array` instance.
  22. As shown in the examples above, you can easily get a stringified version of the MIME type using its `toString()` method. Read on for more on getting the stringified version of the body.
  23. ### Decoding the body
  24. To decode the body bytes of a parsed data URL, you'll need to use the `charset` parameter of the MIME type, if any. This contains an encoding [label](https://encoding.spec.whatwg.org/#label); there are [various possible labels](https://encoding.spec.whatwg.org/#names-and-labels) for a given encoding. We suggest using the [whatwg-encoding](https://www.npmjs.com/package/whatwg-encoding) package as follows:
  25. ```js
  26. const parseDataURL = require("data-urls");
  27. const { labelToName, decode } = require("whatwg-encoding");
  28. const dataURL = parseDataURL(arbitraryString);
  29. // If there's no charset parameter, let's just hope it's UTF-8; that seems like a good guess.
  30. const encodingName = labelToName(dataURL.mimeType.parameters.get("charset") || "utf-8");
  31. const bodyDecoded = decode(dataURL.body, encodingName);
  32. ```
  33. This is especially important since the default, if no parseable MIME type is given, is "US-ASCII", [aka windows-1252](https://encoding.spec.whatwg.org/#names-and-labels), not UTF-8 like you might asume. So for example given an `arbitraryString` of `"data:,Héllo!"`, the above code snippet will correctly produce a `bodyDecoded` of `"Héllo!"` by using the windows-1252 decoder, whereas if you used a UTF-8 decoder you'd get back `"Héllo!"`.
  34. ### Advanced functionality: parsing from a URL record
  35. 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:
  36. ```js
  37. const { parseURL } = require("whatwg-url");
  38. const dataURLFromURLRecord = require("data-urls").fromURLRecord;
  39. const urlRecord = parseURL("data:,Hello%2C%20World!");
  40. const dataURL = dataURLFromURLRecord(urlRecord);
  41. ```
  42. 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.