encoder.js 720 B

1234567891011121314151617181920212223
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const CRLF = '\r\n';
  4. function encodeCommand(args) {
  5. const toWrite = [];
  6. let strings = '*' + args.length + CRLF;
  7. for (let i = 0; i < args.length; i++) {
  8. const arg = args[i];
  9. if (typeof arg === 'string') {
  10. strings += '$' + Buffer.byteLength(arg) + CRLF + arg + CRLF;
  11. }
  12. else if (arg instanceof Buffer) {
  13. toWrite.push(strings + '$' + arg.length.toString() + CRLF, arg);
  14. strings = CRLF;
  15. }
  16. else {
  17. throw new TypeError('Invalid argument type');
  18. }
  19. }
  20. toWrite.push(strings);
  21. return toWrite;
  22. }
  23. exports.default = encodeCommand;