MCPcopy Index your code
hub / github.com/codetheweb/tuyapi / encode

Method encode

lib/message-parser.js:314–338  ·  view source on GitHub ↗

* Encodes a payload into a Tuya-protocol-compliant packet. * @param {Object} options Options for encoding * @param {Buffer|String|Object} options.data data to encode * @param {Boolean} options.encrypted whether or not to encrypt the data * @param {Number} options.commandByte * command

(options)

Source from the content-addressed store, hash-verified

312 * @returns {Buffer} Encoded Buffer
313 */
314 encode(options) {
315 // Check command byte
316 if (!Object.values(CommandType).includes(options.commandByte)) {
317 throw new TypeError('Command byte not defined.');
318 }
319
320 // Convert Objects to Strings, Strings to Buffers
321 if (!(options.data instanceof Buffer)) {
322 if (typeof options.data !== 'string') {
323 options.data = JSON.stringify(options.data);
324 }
325
326 options.data = Buffer.from(options.data);
327 }
328
329 if (this.version === '3.4') {
330 return this._encode34(options);
331 }
332
333 if (this.version === '3.5') {
334 return this._encode35(options);
335 }
336
337 return this._encodePre34(options);
338 }
339
340 /**
341 * Encodes a payload into a Tuya-protocol-compliant packet for protocol version 3.3 and below.

Callers 7

getMethod · 0.80
refreshMethod · 0.80
setMethod · 0.80
_sendPingMethod · 0.80
connectMethod · 0.80
_packetHandlerMethod · 0.80
parser.jsFile · 0.80

Calls 3

_encode34Method · 0.95
_encode35Method · 0.95
_encodePre34Method · 0.95

Tested by

no test coverage detected