Skip to content

Commit fd0807e

Browse files
committed
Release of version 2.0.0
1 parent fca6f8c commit fd0807e

26 files changed

+869
-288
lines changed

CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
## [2.0.0](https://github.com/aws/aws-iot-device-sdk-js/releases/tag/v2.0.0) (Mar 21, 2017)
2+
3+
API Changes
4+
- Deprecated region option(-g) in device configuration.
5+
- Added host endpoint option(-H) to connect to custom host endpoint
6+
7+
Features
8+
- Added support for browserify on Windows CMD. [#74](https://github.com/aws/aws-iot-device-sdk-js/issues/74)
9+
- Added support for loading IAM credentials from aws credential files.
10+
- Added sample for using Node.js SDK with webpack.
11+
12+
Bugfixes/Imporovements
13+
- Fixed README.md typo [#101](https://github.com/aws/aws-iot-device-sdk-js/issues/101)
14+
- Fixed thing.register() API to have independent optional parameters.[#106](https://github.com/aws/aws-iot-device-sdk-js/issues/106)
15+
- Upgrade MQTT.js to v2.2.1 and gulp dependencies.
16+
- Fixed npm test failure in node version above 4.
17+
118
## [1.0.14](https://github.com/aws/aws-iot-device-sdk-js/releases/tag/v1.0.14) (Dec 7, 2016)
219

320
Bugfixes/Improvements

README.md

+116-91
Large diffs are not rendered by default.

common/lib/is-undefined.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@
2828
* @access public
2929
*/
3030
module.exports = function(value) {
31-
return typeof value === 'undefined' || typeof value === null;
31+
return typeof value === 'undefined' || value === null;
3232
};

common/lib/tls-reader.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
*/
1515

1616
//node.js deps
17-
const filesys = require('fs');
17+
var filesys = require('fs');
1818

1919
//npm deps
2020

2121
//app deps
22-
const isUndefined = require('./is-undefined');
23-
const exceptions = require('./exceptions');
22+
var isUndefined = require('./is-undefined');
23+
var exceptions = require('./exceptions');
2424

2525
//begin module
2626
/**

device/index.js

+78-22
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
*/
1515

1616
//node.js deps
17-
const events = require('events');
18-
const inherits = require('util').inherits;
17+
var events = require('events');
18+
var inherits = require('util').inherits;
1919

2020
//npm deps
21-
const mqtt = require('mqtt');
22-
const crypto = require('crypto-js');
21+
var mqtt = require('mqtt');
22+
var crypto = require('crypto-js');
2323

2424
//app deps
25-
const exceptions = require('./lib/exceptions'),
26-
isUndefined = require('../common/lib/is-undefined'),
27-
tlsReader = require('../common/lib/tls-reader');
25+
var exceptions = require('./lib/exceptions');
26+
var isUndefined = require('../common/lib/is-undefined');
27+
var tlsReader = require('../common/lib/tls-reader');
28+
var path = require('path');
29+
var fs = require('fs');
2830

2931
//begin module
3032
function makeTwoDigits(n) {
@@ -157,6 +159,34 @@ function prepareWebSocketUrl(options, awsAccessId, awsSecretKey, awsSTSToken) {
157159
return signUrl('GET', 'wss://', hostName, path, queryParams,
158160
awsAccessId, awsSecretKey, options.region, awsServiceName, '', today, now, options.debug, awsSTSToken);
159161
}
162+
163+
function arrayEach(array, iterFunction) {
164+
for (var idx in array) {
165+
if (Object.prototype.hasOwnProperty.call(array, idx)) {
166+
iterFunction.call(this, array[idx], parseInt(idx, 10));
167+
}
168+
}
169+
}
170+
function getCredentials(ini) {
171+
//Get shared credential function from AWS SDK.
172+
var map = {};
173+
var currentSection ={};
174+
arrayEach(ini.split(/\r?\n/), function(line) {
175+
line = line.split(/(^|\s)[;#]/)[0]; // remove comments
176+
var section = line.match(/^\s*\[([^\[\]]+)\]\s*$/);
177+
if (section) {
178+
currentSection = section[1];
179+
} else if (currentSection) {
180+
var item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/);
181+
if (item) {
182+
map[currentSection] = map[currentSection] || {};
183+
map[currentSection][item[1]] = item[2];
184+
}
185+
}
186+
});
187+
return map;
188+
}
189+
160190
//
161191
// This method is the exposed module; it validates the mqtt options,
162192
// creates a secure mqtt connection via TLS, and returns the mqtt
@@ -309,7 +339,6 @@ function DeviceClient(options) {
309339
var awsAccessId;
310340
var awsSecretKey;
311341
var awsSTSToken;
312-
313342
//
314343
// Validate options, set default reconnect period if not specified.
315344
//
@@ -374,11 +403,7 @@ function DeviceClient(options) {
374403
}
375404

376405
if (isUndefined(options.host)) {
377-
if (!(isUndefined(options.region))) {
378-
options.host = 'data.iot.' + options.region + '.amazonaws.com';
379-
} else {
380-
throw new Error(exceptions.INVALID_CONNECT_OPTIONS);
381-
}
406+
throw new Error(exceptions.INVALID_CONNECT_OPTIONS);
382407
}
383408

384409
if (options.protocol === 'mqtts') {
@@ -391,9 +416,9 @@ function DeviceClient(options) {
391416
tlsReader(options);
392417
} else if (options.protocol === 'wss') {
393418
//
394-
// AWS access id and secret key must be available as either
395-
// options or in the environment.
396-
//
419+
// AWS access id and secret key
420+
// It first check Input options and Environment variables
421+
// If that not available, it will try to load credentials from default credential file
397422
if (!isUndefined(options.accessKeyId)) {
398423
awsAccessId = options.accessKeyId;
399424
} else {
@@ -409,10 +434,34 @@ function DeviceClient(options) {
409434
} else {
410435
awsSTSToken = process.env.AWS_SESSION_TOKEN;
411436
}
412-
// AWS region must be defined when connecting via WebSocket/SigV4
413-
if (isUndefined(options.region)) {
414-
console.log('AWS region must be defined when connecting via WebSocket/SigV4; see README.md');
415-
throw new Error(exceptions.INVALID_CONNECT_OPTIONS);
437+
if (isUndefined(awsAccessId) || isUndefined(awsSecretKey)) {
438+
var filename;
439+
try {
440+
if(!isUndefined(options.filename)) {
441+
filename = options.filename;
442+
} else {
443+
filename = _loadDefaultFilename();
444+
}
445+
var user_profile = options.profile || process.env.AWS_PROFILE || 'default';
446+
var creds = getCredentials(fs.readFileSync(filename, 'utf-8'));
447+
var profile = creds[user_profile];
448+
awsAccessId = profile.aws_access_key_id;
449+
awsSecretKey = profile.aws_secret_access_key;
450+
awsSTSToken = profile.aws_session_token;
451+
} catch (e) {
452+
console.log(e);
453+
console.log("Failed to read credentials from " + filename);
454+
}
455+
}
456+
if (!isUndefined(options.host)) {
457+
var pattern =/[a-zA-Z0-9]+\.iot\.([a-z]+-[a-z]+-[0-9]+)\.amazonaws\.com/;
458+
var region = pattern.exec(options.host);
459+
if (region === null) {
460+
console.log('Host endpoint is not valid');
461+
throw new Error(exceptions.INVALID_CONNECT_OPTIONS);
462+
} else {
463+
options.region = region[1];
464+
}
416465
}
417466
// AWS Access Key ID and AWS Secret Key must be defined
418467
if (isUndefined(awsAccessId) || (isUndefined(awsSecretKey))) {
@@ -443,6 +492,13 @@ function DeviceClient(options) {
443492
protocols.mqtts = require('./lib/tls');
444493
protocols.wss = require('./lib/ws');
445494

495+
function _loadDefaultFilename() {
496+
var home = process.env.HOME ||
497+
process.env.USERPROFILE ||
498+
(process.env.HOMEPATH ? ((process.env.HOMEDRIVE || 'C:/') + process.env.HOMEPATH) : null);
499+
return path.join(home, '.aws', 'credentials');
500+
501+
}
446502
function _addToSubscriptionCache(topic, options) {
447503
var matches = activeSubscriptions.filter(function(element) {
448504
return element.topic === topic;
@@ -521,7 +577,7 @@ function DeviceClient(options) {
521577
return protocols[options.protocol](client, options);
522578
}
523579

524-
const device = new mqtt.MqttClient(_wrapper, options);
580+
var device = new mqtt.MqttClient(_wrapper, options);
525581

526582
//handle events from the mqtt client
527583

@@ -730,7 +786,7 @@ function DeviceClient(options) {
730786
device.subscribe(topics, options, callback);
731787
} else {
732788
device.subscribe(topics, options);
733-
}
789+
}
734790
} else {
735791
// we're offline - queue this subscription request
736792
if (offlineSubscriptionQueue.length < offlineSubscriptionQueueMaxSize) {

examples/browser/lifecycle/aws-configuration.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
var awsConfiguration = {
2121
poolId: YOUR_COGNITO_IDENTITY_POOL_ID_GOES_HERE, // 'YourCognitoIdentityPoolId'
22+
host: YOUR_AWS_IOT_ENDPOINT_GOES_HERE, // 'YourAWSIoTEndpoint', e.g. 'prefix.iot.us-east-1.amazonaws.com'
2223
region: YOUR_AWS_REGION_GOES_HERE // 'YourAwsRegion', e.g. 'us-east-1'
2324
};
2425
module.exports = awsConfiguration;
26+

examples/browser/lifecycle/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ const mqttClient = AWSIoTData.device({
6060
//
6161
region: AWS.config.region,
6262
//
63+
// Set the AWS IoT Host Endpoint
64+
// //
65+
host:AWSConfiguration.host,
66+
//
6367
// Use the clientId created earlier.
6468
//
6569
clientId: clientId,

examples/browser/mqtt-explorer/aws-configuration.js

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
var awsConfiguration = {
2121
poolId: YOUR_COGNITO_IDENTITY_POOL_ID_GOES_HERE, // 'YourCognitoIdentityPoolId'
22+
host: YOUR_AWS_IOT_ENDPOINT_GOES_HERE, // 'YourAwsIoTEndpoint', e.g. 'prefix.iot.us-east-1.amazonaws.com'
2223
region: YOUR_AWS_REGION_GOES_HERE // 'YourAwsRegion', e.g. 'us-east-1'
2324
};
2425
module.exports = awsConfiguration;
26+

examples/browser/mqtt-explorer/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ const mqttClient = AWSIoTData.device({
6060
//
6161
region: AWS.config.region,
6262
//
63+
////Set the AWS IoT Host Endpoint
64+
host:AWSConfiguration.host,
65+
//
6366
// Use the clientId created earlier.
6467
//
6568
clientId: clientId,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
/*
17+
* NOTE: You must set the following string constants prior to running this
18+
* example application.
19+
*/
20+
var awsConfiguration = {
21+
poolId: YOUR_COGNITO_IDENTITY_POOL_ID_GOES_HERE, // 'YourCognitoIdentityPoolId'
22+
host: YOUR_AWS_IOT_ENDPOINT_GOES_HERE, // 'YourAwsIoTEndpoint', e.g. 'prefix.iot.us-east-1.amazonaws.com'
23+
region: YOUR_AWS_REGION_GOES_HERE // 'YourAwsRegion', e.g. 'us-east-1'
24+
};
25+
module.exports = awsConfiguration;
26+

0 commit comments

Comments
 (0)