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.

encodePacket.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.encodePacket = void 0;
  4. exports.encodePacketToBinary = encodePacketToBinary;
  5. const commons_js_1 = require("./commons.js");
  6. const encodePacket = ({ type, data }, supportsBinary, callback) => {
  7. if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) {
  8. return callback(supportsBinary ? data : "b" + toBuffer(data, true).toString("base64"));
  9. }
  10. // plain string
  11. return callback(commons_js_1.PACKET_TYPES[type] + (data || ""));
  12. };
  13. exports.encodePacket = encodePacket;
  14. const toBuffer = (data, forceBufferConversion) => {
  15. if (Buffer.isBuffer(data) ||
  16. (data instanceof Uint8Array && !forceBufferConversion)) {
  17. return data;
  18. }
  19. else if (data instanceof ArrayBuffer) {
  20. return Buffer.from(data);
  21. }
  22. else {
  23. return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
  24. }
  25. };
  26. let TEXT_ENCODER;
  27. function encodePacketToBinary(packet, callback) {
  28. if (packet.data instanceof ArrayBuffer || ArrayBuffer.isView(packet.data)) {
  29. return callback(toBuffer(packet.data, false));
  30. }
  31. (0, exports.encodePacket)(packet, true, (encoded) => {
  32. if (!TEXT_ENCODER) {
  33. // lazily created for compatibility with Node.js 10
  34. TEXT_ENCODER = new TextEncoder();
  35. }
  36. callback(TEXT_ENCODER.encode(encoded));
  37. });
  38. }