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.

decodePacket.js 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.decodePacket = void 0;
  4. const commons_js_1 = require("./commons.js");
  5. const decodePacket = (encodedPacket, binaryType) => {
  6. if (typeof encodedPacket !== "string") {
  7. return {
  8. type: "message",
  9. data: mapBinary(encodedPacket, binaryType),
  10. };
  11. }
  12. const type = encodedPacket.charAt(0);
  13. if (type === "b") {
  14. const buffer = Buffer.from(encodedPacket.substring(1), "base64");
  15. return {
  16. type: "message",
  17. data: mapBinary(buffer, binaryType),
  18. };
  19. }
  20. if (!commons_js_1.PACKET_TYPES_REVERSE[type]) {
  21. return commons_js_1.ERROR_PACKET;
  22. }
  23. return encodedPacket.length > 1
  24. ? {
  25. type: commons_js_1.PACKET_TYPES_REVERSE[type],
  26. data: encodedPacket.substring(1),
  27. }
  28. : {
  29. type: commons_js_1.PACKET_TYPES_REVERSE[type],
  30. };
  31. };
  32. exports.decodePacket = decodePacket;
  33. const mapBinary = (data, binaryType) => {
  34. switch (binaryType) {
  35. case "arraybuffer":
  36. if (data instanceof ArrayBuffer) {
  37. // from WebSocket & binaryType "arraybuffer"
  38. return data;
  39. }
  40. else if (Buffer.isBuffer(data)) {
  41. // from HTTP long-polling
  42. return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
  43. }
  44. else {
  45. // from WebTransport (Uint8Array)
  46. return data.buffer;
  47. }
  48. case "nodebuffer":
  49. default:
  50. if (Buffer.isBuffer(data)) {
  51. // from HTTP long-polling or WebSocket & binaryType "nodebuffer" (default)
  52. return data;
  53. }
  54. else {
  55. // from WebTransport (Uint8Array)
  56. return Buffer.from(data);
  57. }
  58. }
  59. };