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.

parser.js 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. const MIMEType = require("whatwg-mimetype");
  3. const { parseURL, serializeURL, percentDecodeString } = require("whatwg-url");
  4. const { stripLeadingAndTrailingASCIIWhitespace, isomorphicDecode, forgivingBase64Decode } = require("./utils.js");
  5. module.exports = stringInput => {
  6. const urlRecord = parseURL(stringInput);
  7. if (urlRecord === null) {
  8. return null;
  9. }
  10. return module.exports.fromURLRecord(urlRecord);
  11. };
  12. module.exports.fromURLRecord = urlRecord => {
  13. if (urlRecord.scheme !== "data") {
  14. return null;
  15. }
  16. const input = serializeURL(urlRecord, true).substring("data:".length);
  17. let position = 0;
  18. let mimeType = "";
  19. while (position < input.length && input[position] !== ",") {
  20. mimeType += input[position];
  21. ++position;
  22. }
  23. mimeType = stripLeadingAndTrailingASCIIWhitespace(mimeType);
  24. if (position === input.length) {
  25. return null;
  26. }
  27. ++position;
  28. const encodedBody = input.substring(position);
  29. let body = percentDecodeString(encodedBody);
  30. // Can't use /i regexp flag because it isn't restricted to ASCII.
  31. const mimeTypeBase64MatchResult = /(.*); *[Bb][Aa][Ss][Ee]64$/u.exec(mimeType);
  32. if (mimeTypeBase64MatchResult) {
  33. const stringBody = isomorphicDecode(body);
  34. body = forgivingBase64Decode(stringBody);
  35. if (body === null) {
  36. return null;
  37. }
  38. mimeType = mimeTypeBase64MatchResult[1];
  39. }
  40. if (mimeType.startsWith(";")) {
  41. mimeType = `text/plain${mimeType}`;
  42. }
  43. let mimeTypeRecord;
  44. try {
  45. mimeTypeRecord = new MIMEType(mimeType);
  46. } catch (e) {
  47. mimeTypeRecord = new MIMEType("text/plain;charset=US-ASCII");
  48. }
  49. return {
  50. mimeType: mimeTypeRecord,
  51. body
  52. };
  53. };