Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict'
const logger = require('../lib').logger;
const iotClient = require('./iotClient');
const deviceManager = require('./deviceManager');
const testService = require('./testService');
const async = require('async');
const errors = require('azure-iot-common').errors;
function runTest(deviceConnectionString, protocol, label, done) {
logger.info('');
logger.info('Starting ' + label + ' Test...');
iotClient.runTest(deviceConnectionString, protocol, function(err) {
if(err) {
logger.crit('--> Failed to run ' + label + ' test, error: ' + err);
} else {
logger.info('--> Successfully ran ' + label + ' test.');
}
// Don't pass out error (if the test completed, it will just state it failed, but still run the next test)
return done(null, deviceConnectionString);
});
}
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict'
const logger = require('../lib').logger;
const iothub = require('azure-iothub');
const errors = require('azure-iot-common').errors;
const Registry = iothub.Registry;
const ConnectionString = require('azure-iothub').ConnectionString;
function getDevice(iotHubConnectionString, deviceId, done) {
var registry = Registry.fromConnectionString(iotHubConnectionString);
registry.get(deviceId, done);
}
function getDeviceConnectionString(iotHubConnectionString, deviceInfo) {
var deviceConnectionString = 'HostName=' + ConnectionString.parse(iotHubConnectionString).HostName + ';DeviceId=' + deviceInfo.deviceId + ';SharedAccessKey=' + deviceInfo.authentication.symmetricKey.primaryKey;
logger.trace('Connectionstring: ' + deviceConnectionString);
return deviceConnectionString;
}
// Create a new device
function createDevice(iotHubConnectionString, deviceId, done) {
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var assert = require('chai').assert;
var Client = require('./client.js');
var errors = require('azure-iot-common').errors;
var Message = require('azure-iot-common').Message;
var AmqpReceiver = require('azure-iot-amqp-base').AmqpReceiver;
function transportSpecificTests(opts) {
describe('Client', function () {
var testSubject;
var deviceId;
before('prepare test subject', function (done) {
/*Tests_SRS_NODE_IOTHUB_CLIENT_05_008: [The open method shall open a connection to the IoT Hub that was identified when the Client object was created (e.g., in Client.fromConnectionString).]*/
/*Tests_SRS_NODE_IOTHUB_CLIENT_05_009: [When the open method completes, the callback function (indicated by the done argument) shall be invoked with the following arguments:
err - standard JavaScript Error object (or subclass)]*/
/*Tests_SRS_NODE_IOTHUB_CLIENT_05_010: [The argument err passed to the callback done shall be null if the protocol operation was successful.]*/
if (opts.transport && opts.connectionString) assert.fail('test setup error');
if (opts.transport) testSubject = new Client(opts.transport());
else testSubject = Client.fromConnectionString(opts.connectionString);
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var errors = require('azure-iot-common').errors;
var _ = require('lodash');
var traverse = require('traverse');
require('es5-shim');
var translateError = require('./twin_errors.js');
var Twin = function(client) {
EventEmitter.call(this);
this._client = client;
this._rid = 4200; // arbitrary starting value.
};
util.inherits(Twin, EventEmitter);
Twin.timeout = 120000;
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var events_1 = require("events");
var dbg = require("debug");
var machina = require("machina");
var async = require("async");
var azure_iot_common_1 = require("azure-iot-common");
var azure_iot_amqp_base_1 = require("azure-iot-amqp-base");
var amqp_service_errors_js_1 = require("./amqp_service_errors.js");
var UnauthorizedError = azure_iot_common_1.errors.UnauthorizedError;
var DeviceNotFoundError = azure_iot_common_1.errors.DeviceNotFoundError;
var NotConnectedError = azure_iot_common_1.errors.NotConnectedError;
var debug = dbg('azure-iothub:Amqp');
// tslint:disable-next-line:no-var-requires
var packageJson = require('../package.json');
function handleResult(errorMessage, done) {
return function (err, result) {
if (err) {
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_16_018: [All asynchronous instance methods shall call the `done` callback with either no arguments or a first null argument and a second argument that is the result of the operation if the operation succeeded.]*/
done(amqp_service_errors_js_1.translateError(errorMessage, err));
}
else {
/*Codes_SRS_NODE_IOTHUB_SERVICE_AMQP_16_017: [All asynchronous instance methods shall call the `done` callback with a single parameter that is derived from the standard Javascript `Error` object if the operation failed.]*/
done(null, result);
}
};
}
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var errors = require('azure-iot-common').errors;
var amqp10 = require('amqp10');
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_010: [ `translateError` shall accept 2 argument:
*- A custom error message to give context to the user.
*- the AMQP error object itself]
*/
var translateError = function translateError(message, amqpError) {
var error;
if (amqpError.constructor.name === 'AMQPError') {
switch (amqpError.condition.contents) {
case 'amqp:not-found':
/*Codes_SRS_NODE_DEVICE_AMQP_COMMON_ERRORS_16_006: [`translateError` shall return an `DeviceNotFoundError` if the AMQP error condition is `amqp:not-found`.]*/
error = new errors.DeviceNotFoundError(message);
break;
case 'amqp:unauthorized-access':
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var amqp10 = require('amqp10');
var Promise = require('bluebird');
var AmqpMessage = require('./amqp_message.js');
var AmqpReceiver = require('./amqp_receiver.js');
var errors = require('azure-iot-common').errors;
var results = require('azure-iot-common').results;
var uuid = require('uuid');
var debug = require('debug')('amqp-common');
var _putTokenSendingEndpoint = '$cbs';
var _putTokenReceivingEndpoint = '$cbs';
/**
* @class module:azure-iot-amqp-base.Amqp
* @classdesc Basic AMQP functionality used by higher-level IoT Hub libraries.
* Usually you'll want to avoid using this class and instead rely on higher-level implementations
* of the AMQP transport (see [azure-iot-device-amqp.Amqp]{@link module:azure-iot-device-amqp.Amqp} for example).
*
* @param {Boolean} autoSettleMessages Boolean indicating whether messages should be settled automatically or if the calling code will handle it.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var EventEmitter = require('events');
var Message = require('azure-iot-common').Message;
var ArgumentError = require('azure-iot-common').errors.ArgumentError;
var SharedAccessSignature = require('./shared_access_signature.js');
function Response(statusCode) {
this.statusCode = statusCode;
}
function makeError(statusCode) {
var err = new Error();
err.response = new Response(statusCode);
return err;
}
function SimulatedHttp(config) {
this._receiver = null;
this.handleRequest = function(done) {
var sig = SharedAccessSignature.parse(config.sharedAccessSignature);
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
'use strict';
var uuid = require('uuid');
var amqp10 = require('amqp10');
var Promise = require('bluebird');
var Receiver = require('./receiver.js');
var Sender = require('./sender.js');
var ConnectionConfig = require('./config.js');
var ArgumentError = require('azure-iot-common').errors.ArgumentError;
var MessagingEntityNotFoundError = require('./errors.js').MessagingEntityNotFoundError;
/**
* Instantiate a client pointing to the Event Hub given by this configuration.
*
* @param {ConnectionConfig} config
* @constructor
*/
function EventHubClient(config) {
var makeError = function (prop) {
return new ArgumentError('config is missing property ' + prop);
};
['host', 'path', 'keyName', 'key'].forEach(function (prop) {
if (!config[prop]) throw makeError(prop);
});
Client.prototype.setRetryPolicy = function (policy) {
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_027: [The `setRetryPolicy` method shall throw a `ReferenceError` if the `policy` argument is falsy.]*/
if (!policy) {
throw new ReferenceError('policy cannot be \'' + policy + '\'');
}
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_028: [The `setRetryPolicy` method shall throw an `ArgumentError` if the `policy` object does not have a `shouldRetry` method and a `nextRetryTimeout` method.]*/
if (!(typeof policy.shouldRetry === 'function') || !(typeof policy.nextRetryTimeout === 'function')) {
throw new azure_iot_common_1.errors.ArgumentError('policy should have a shouldRetry method and a nextRetryTimeout method');
}
/*Codes_SRS_NODE_IOTHUB_CLIENT_16_029: [Any operation (e.g. `send`, `getFeedbackReceiver`, etc) initiated after a call to `setRetryPolicy` shall use the policy passed as argument to retry.]*/
this._retryPolicy = policy;
};
Client.prototype._disconnectHandler = function (reason) {