MCPcopy Create free account
hub / github.com/codetheweb/tuyapi / getPayload

Method getPayload

lib/message-parser.js:235–267  ·  view source on GitHub ↗

* Attempts to decode a given payload into * an object or string. * @param {Buffer} data to decode * @returns {Object|String} * object if payload is JSON, otherwise string

(data)

Source from the content-addressed store, hash-verified

233 * object if payload is JSON, otherwise string
234 */
235 getPayload(data) {
236 if (data.length === 0) {
237 return false;
238 }
239
240 // Try to decrypt data first.
241 try {
242 if (!this.cipher) {
243 throw new Error('Missing key or version in constructor.');
244 }
245
246 data = this.cipher.decrypt(data);
247 } catch (_) {
248 data = data.toString('utf8');
249 }
250
251 // Incoming 3.5 data isn't 0 because of iv and tag so check size after
252 if (this.version === '3.5') {
253 if (data.length === 0) {
254 return false;
255 }
256 }
257
258 // Try to parse data as JSON.
259 // If error, return as string.
260 if (typeof data === 'string') {
261 try {
262 data = JSON.parse(data);
263 } catch (_) { }
264 }
265
266 return data;
267 }
268
269 /**
270 * Recursive function to parse

Callers 1

parseRecursiveMethod · 0.95

Calls 2

decryptMethod · 0.80
parseMethod · 0.80

Tested by

no test coverage detected