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.

encodePacket.browser.js 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const { PACKET_TYPES } = require("./commons");
  2. const withNativeBlob =
  3. typeof Blob === "function" ||
  4. (typeof Blob !== "undefined" &&
  5. Object.prototype.toString.call(Blob) === "[object BlobConstructor]");
  6. const withNativeArrayBuffer = typeof ArrayBuffer === "function";
  7. // ArrayBuffer.isView method is not defined in IE10
  8. const isView = obj => {
  9. return typeof ArrayBuffer.isView === "function"
  10. ? ArrayBuffer.isView(obj)
  11. : obj && obj.buffer instanceof ArrayBuffer;
  12. };
  13. const encodePacket = ({ type, data }, supportsBinary, callback) => {
  14. if (withNativeBlob && data instanceof Blob) {
  15. if (supportsBinary) {
  16. return callback(data);
  17. } else {
  18. return encodeBlobAsBase64(data, callback);
  19. }
  20. } else if (
  21. withNativeArrayBuffer &&
  22. (data instanceof ArrayBuffer || isView(data))
  23. ) {
  24. if (supportsBinary) {
  25. return callback(data);
  26. } else {
  27. return encodeBlobAsBase64(new Blob([data]), callback);
  28. }
  29. }
  30. // plain string
  31. return callback(PACKET_TYPES[type] + (data || ""));
  32. };
  33. const encodeBlobAsBase64 = (data, callback) => {
  34. const fileReader = new FileReader();
  35. fileReader.onload = function() {
  36. const content = fileReader.result.split(",")[1];
  37. callback("b" + content);
  38. };
  39. return fileReader.readAsDataURL(data);
  40. };
  41. module.exports = encodePacket;