Node-Red configuration
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.

base64-arraybuffer.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // imported from https://github.com/socketio/base64-arraybuffer
  2. const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  3. // Use a lookup table to find the index.
  4. const lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
  5. for (let i = 0; i < chars.length; i++) {
  6. lookup[chars.charCodeAt(i)] = i;
  7. }
  8. export const encode = (arraybuffer) => {
  9. let bytes = new Uint8Array(arraybuffer), i, len = bytes.length, base64 = '';
  10. for (i = 0; i < len; i += 3) {
  11. base64 += chars[bytes[i] >> 2];
  12. base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
  13. base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
  14. base64 += chars[bytes[i + 2] & 63];
  15. }
  16. if (len % 3 === 2) {
  17. base64 = base64.substring(0, base64.length - 1) + '=';
  18. }
  19. else if (len % 3 === 1) {
  20. base64 = base64.substring(0, base64.length - 2) + '==';
  21. }
  22. return base64;
  23. };
  24. export const decode = (base64) => {
  25. let bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
  26. if (base64[base64.length - 1] === '=') {
  27. bufferLength--;
  28. if (base64[base64.length - 2] === '=') {
  29. bufferLength--;
  30. }
  31. }
  32. const arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer);
  33. for (i = 0; i < len; i += 4) {
  34. encoded1 = lookup[base64.charCodeAt(i)];
  35. encoded2 = lookup[base64.charCodeAt(i + 1)];
  36. encoded3 = lookup[base64.charCodeAt(i + 2)];
  37. encoded4 = lookup[base64.charCodeAt(i + 3)];
  38. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  39. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  40. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  41. }
  42. return arraybuffer;
  43. };