Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
log.debug('Received command result from bootstrap');
try {
streamData = JSON.parse(streamData + data);
// we successfully parsed JSON so we've got all the data,
// remove the socket listener and evaluate
this.socketClient.removeAllListeners('data');
if (streamData.status === 0) {
return resolve(streamData.value);
}
reject(errorFromCode(streamData.status, streamData.value));
} catch (err) {
if (!_.isString(streamData)) {
log.error('Got an unexpected error inside socket listener');
log.error(err.stack);
return reject(errorFromCode(13, err.message));
}
log.debug(`Stream still not complete, waiting up to ${SEND_COMMAND_TIMEOUT}ms for the data to arrive`);
streamData += data;
sendCommandTimeoutHandler = setTimeout(() => {
const errMsg = `Server socket stopped responding. The recent response was '${streamData}'`;
log.error(errMsg);
this.socketClient.removeAllListeners('data');
reject(errorFromCode(13, errMsg));
}, SEND_COMMAND_TIMEOUT);
}
});
});
this.socketClient.on('data', (data) => {
if (sendCommandTimeoutHandler) {
clearTimeout(sendCommandTimeoutHandler);
}
log.debug('Received command result from bootstrap');
try {
streamData = JSON.parse(streamData + data);
// we successfully parsed JSON so we've got all the data,
// remove the socket listener and evaluate
this.socketClient.removeAllListeners('data');
if (streamData.status === 0) {
return resolve(streamData.value);
}
reject(errorFromCode(streamData.status, streamData.value));
} catch (err) {
if (!_.isString(streamData)) {
log.error('Got an unexpected error inside socket listener');
log.error(err.stack);
return reject(errorFromCode(13, err.message));
}
log.debug(`Stream still not complete, waiting up to ${SEND_COMMAND_TIMEOUT}ms for the data to arrive`);
streamData += data;
sendCommandTimeoutHandler = setTimeout(() => {
const errMsg = `Server socket stopped responding. The recent response was '${streamData}'`;
log.error(errMsg);
this.socketClient.removeAllListeners('data');
reject(errorFromCode(13, errMsg));
}, SEND_COMMAND_TIMEOUT);
}
});
let cb = (result) => {
// get back a JSONWP object, so decode and
// just return the value
if (result.status === 0) {
resolve(result.value);
} else if (result.status) {
let jsonwpError = errorFromCode(result.status, result.value);
reject(jsonwpError);
} else {
reject(new Error(result.value));
}
};
this.commandQueue.push({cmd, cb});
this.socketClient.on('data', (data) => {
if (sendCommandTimeoutHandler) {
clearTimeout(sendCommandTimeoutHandler);
}
log.debug("Received command result from bootstrap");
try {
streamData = JSON.parse(streamData + data);
// we successfully parsed JSON so we've got all the data,
// remove the socket listener and evaluate
this.socketClient.removeAllListeners('data');
if (streamData.status === 0) {
return resolve(streamData.value);
}
reject(errorFromCode(streamData.status, streamData.value));
} catch (err) {
if (!_.isString(streamData)) {
log.error('Got an unexpected error inside socket listener');
log.error(err.stack);
return reject(errorFromCode(13, err.message));
}
log.debug(`Stream still not complete, waiting up to ${SEND_COMMAND_TIMEOUT}ms for the data to arrive`);
streamData += data;
sendCommandTimeoutHandler = setTimeout(() => {
const errMsg = `Server socket stopped responding. The recent response was '${streamData}'`;
log.error(errMsg);
this.socketClient.removeAllListeners('data');
reject(errorFromCode(13, errMsg));
}, SEND_COMMAND_TIMEOUT);
}
});
}
log.debug("Received command result from bootstrap");
try {
streamData = JSON.parse(streamData + data);
// we successfully parsed JSON so we've got all the data,
// remove the socket listener and evaluate
this.socketClient.removeAllListeners('data');
if (streamData.status === 0) {
return resolve(streamData.value);
}
reject(errorFromCode(streamData.status, streamData.value));
} catch (err) {
if (!_.isString(streamData)) {
log.error('Got an unexpected error inside socket listener');
log.error(err.stack);
return reject(errorFromCode(13, err.message));
}
log.debug(`Stream still not complete, waiting up to ${SEND_COMMAND_TIMEOUT}ms for the data to arrive`);
streamData += data;
sendCommandTimeoutHandler = setTimeout(() => {
const errMsg = `Server socket stopped responding. The recent response was '${streamData}'`;
log.error(errMsg);
this.socketClient.removeAllListeners('data');
reject(errorFromCode(13, errMsg));
}, SEND_COMMAND_TIMEOUT);
}
});
});
});
this.socketClient.setTimeout(15000);
this.socketClient.on('timeout', () => {
this.socketClient.destroy();
this.socketClient = null;
this.isRestart = true;
reject(errorFromCode(-1, "No response from Server"));
});
this.socketClient.on('end', () => {
this.socketClient.destroy();
this.socketClient = null;
this.isRestart = true;
reject(errorFromCode(-1, "Socket ended by Server"));
});
} catch (err) {
reject(errorFromCode(-1, err));
}
});
}
this.socketClient.on('end', () => {
this.socketClient.destroy();
this.socketClient = null;
this.isRestart = true;
reject(errorFromCode(-1, "Socket ended by Server"));
});
} catch (err) {
this.socketClient.on('data', (data) => {
let streamData = '';
log.debug(`Received command result from bootstrap : ${data}`);
try {
streamData = JSON.parse(streamData + data);
this.socketClient.removeAllListeners('data');
if (streamData.status === 0) {
resolve(streamData.value);
} else if (streamData.status === 44) {
this.restartUIAutomator();
resolve(false);
}
reject(errorFromCode(streamData.status));
} catch (ign) {
log.debug("Stream still not complete, waiting");
streamData += data;
}
});
this.socketClient.setTimeout(15000);
sendCommandTimeoutHandler = setTimeout(() => {
const errMsg = `Server socket stopped responding. The recent response was '${streamData}'`;
log.error(errMsg);
this.socketClient.removeAllListeners('data');
reject(errorFromCode(13, errMsg));
}, SEND_COMMAND_TIMEOUT);
}
sendCommandTimeoutHandler = setTimeout(() => {
const errMsg = `Server socket stopped responding. The recent response was '${streamData}'`;
log.error(errMsg);
this.socketClient.removeAllListeners('data');
reject(errorFromCode(13, errMsg));
}, SEND_COMMAND_TIMEOUT);
}