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.

decodePacket.browser.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const { PACKET_TYPES_REVERSE, ERROR_PACKET } = require("./commons");
  2. const withNativeArrayBuffer = typeof ArrayBuffer === "function";
  3. let base64decoder;
  4. if (withNativeArrayBuffer) {
  5. base64decoder = require("base64-arraybuffer");
  6. }
  7. const decodePacket = (encodedPacket, binaryType) => {
  8. if (typeof encodedPacket !== "string") {
  9. return {
  10. type: "message",
  11. data: mapBinary(encodedPacket, binaryType)
  12. };
  13. }
  14. const type = encodedPacket.charAt(0);
  15. if (type === "b") {
  16. return {
  17. type: "message",
  18. data: decodeBase64Packet(encodedPacket.substring(1), binaryType)
  19. };
  20. }
  21. const packetType = PACKET_TYPES_REVERSE[type];
  22. if (!packetType) {
  23. return ERROR_PACKET;
  24. }
  25. return encodedPacket.length > 1
  26. ? {
  27. type: PACKET_TYPES_REVERSE[type],
  28. data: encodedPacket.substring(1)
  29. }
  30. : {
  31. type: PACKET_TYPES_REVERSE[type]
  32. };
  33. };
  34. const decodeBase64Packet = (data, binaryType) => {
  35. if (base64decoder) {
  36. const decoded = base64decoder.decode(data);
  37. return mapBinary(decoded, binaryType);
  38. } else {
  39. return { base64: true, data }; // fallback for old browsers
  40. }
  41. };
  42. const mapBinary = (data, binaryType) => {
  43. switch (binaryType) {
  44. case "blob":
  45. return data instanceof ArrayBuffer ? new Blob([data]) : data;
  46. case "arraybuffer":
  47. default:
  48. return data; // assuming the data is already an ArrayBuffer
  49. }
  50. };
  51. module.exports = decodePacket;