Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private _enableInputMessages(callback: (err?: Error) => void): void {
if (!this._inputMessagesEnabled) {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
/* Codes_SRS_NODE_MODULE_CLIENT_18_016: [ The client shall connect the transport if needed in order to receive inputMessages. ]*/
this._transport.enableInputMessages(opCallback);
}, (err) => {
if (!err) {
this._inputMessagesEnabled = true;
}
callback(err);
});
} else {
callback();
}
}
private _invokeSetOptions(options: DeviceClientOptions, done?: (err?: Error, result?: results.TransportConfigured) => void): void {
// Making this an operation that can be retried because we cannot assume the transport's behavior (whether it's going to disconnect/reconnect, etc).
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
this._transport.setOptions(options, opCallback);
}, (err) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_043: [The `done` callback shall be invoked no parameters when it has successfully finished setting the client and/or transport options.]*/
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_044: [The `done` callback shall be invoked with a standard javascript `Error` object and no result object if the client could not be configured as requested.]*/
safeCallback(done, err);
});
}
sendOutputEventBatch(outputName: string, messages: Message[], callback: (err?: Error, result?: results.MessageEnqueued) => void): void {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
/* Codes_SRS_NODE_MODULE_CLIENT_18_011: [ The `sendOutputEventBatch` method shall send the list of events (indicated by the `messages` argument) via the transport associated with the Client instance. ]*/
this._transport.sendOutputEventBatch(outputName, messages, opCallback);
}, (err, result) => {
/*Codes_SRS_NODE_MODULE_CLIENT_18_021: [ When the `sendOutputEventBatch` method completes the `callback` function shall be invoked with the same arguments as the underlying transport method's callback. ]*/
/*Codes_SRS_NODE_MODULE_CLIENT_18_022: [ The `sendOutputEventBatch` method shall not throw if the `callback` is not passed. ]*/
safeCallback(callback, err, result);
});
}
private _enableMethods(callback: (err?: Error) => void): void {
if (!this._methodsEnabled) {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
this._transport.enableMethods(opCallback);
}, (err) => {
if (!err) {
this._methodsEnabled = true;
}
callback(err);
});
} else {
callback();
}
}
enableTwinDesiredPropertiesUpdates(callback: (err?: Error) => void): void {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
this._transport.enableTwinDesiredPropertiesUpdates((err) => {
this.desiredPropertiesUpdatesEnabled = !err;
opCallback(err);
});
}, callback);
}
get(callback: (err: Error, twin?: Twin) => void): void {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
this._clearCachedProperties();
/*Codes_SRS_NODE_DEVICE_TWIN_16_002: [The `get` method shall call the `getTwin` method of the `Transport` object with a callback.]*/
this._transport.getTwin((err, twinProperties) => {
if (err) {
/*Codes_SRS_NODE_DEVICE_TWIN_16_003: [If the callback passed to the `getTwin` method is called with an error, the `callback` passed to the call to the `get` method shall be called with that error.]*/
opCallback(err);
} else {
/*Codes_SRS_NODE_DEVICE_TWIN_16_004: [If the callback passed to the `getTwin` method is called with no error and a `TwinProperties` object, these properties shall be merged with the current instance properties.]*/
this._mergePatch(this.properties.desired, twinProperties.desired);
this._mergePatch(this.properties.reported, twinProperties.reported);
/*Codes_SRS_NODE_DEVICE_TWIN_16_006: [For each desired property that is part of the `TwinProperties` object received, an event named after the path to this property shall be fired and passed the property value as argument.]*/
this._fireChangeEvents(this.properties.desired);
/*Codes_SRS_NODE_DEVICE_TWIN_16_005: [Once the properties have been merged the `callback` method passed to the call to `get` shall be called with a first argument that is `null` and a second argument that is the current `Twin` instance (`this`).]*/
opCallback(null, this);
}
setTransportOptions(options: any, done?: (err?: Error, result?: results.TransportConfigured) => void): void {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_024: [The ‘setTransportOptions’ method shall throw a ‘ReferenceError’ if the options object is falsy] */
if (!options) throw new ReferenceError('options cannot be falsy.');
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_025: [The ‘setTransportOptions’ method shall throw a ‘NotImplementedError’ if the transport doesn’t implement a ‘setOption’ method.] */
if (typeof this._transport.setOptions !== 'function') throw new errors.NotImplementedError('setOptions does not exist on this transport');
const clientOptions = {
http: {
receivePolicy: options
}
};
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
/*Codes_SRS_NODE_INTERNAL_CLIENT_16_021: [The ‘setTransportOptions’ method shall call the ‘setOptions’ method on the transport object.]*/
this._transport.setOptions(clientOptions, opCallback);
}, (err) => {
if (err) {
safeCallback(done, err);
} else {
safeCallback(done, null, new results.TransportConfigured());
}
});
}
private _enableC2D(callback: (err?: Error) => void): void {
if (!this._c2dEnabled) {
const retryOp = new RetryOperation(this._retryPolicy, this._maxOperationTimeout);
retryOp.retry((opCallback) => {
this._transport.enableC2D(opCallback);
}, (err) => {
if (!err) {
this._c2dEnabled = true;
}
callback(err);
});
} else {
callback();
}
}