Skip to content

Commit

Permalink
pass rest options to MQTT Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitriy Nevzorov committed Sep 13, 2016
1 parent 308b1f5 commit cab9eb6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"presets": [
"es2015"
"es2015",
"stage-0"
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"devDependencies": {
"babel-cli": "^6.14.0",
"babel-preset-es2015": "^6.14.0",
"babel-preset-stage-0": "^6.5.0",
"babel-register": "^6.14.0",
"chai": "^3.5.0",
"chai-string": "^1.2.0",
Expand Down
28 changes: 19 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,36 @@ import websocket from 'websocket-stream';

class AWSMqtt extends MqttClient {
constructor(options = {}) {
const {
endpointAddress,
accessKeyId,
secretAccessKey,
sessionToken,
region,
expires = 15,
wsOptions,
...mqttOptions
} = options;
super(() => {
let url = v4.createPresignedURL(
'GET',
options.endpointAddress,
endpointAddress,
'/mqtt',
'iotdevicegateway',
crypto.createHash('sha256').update('', 'utf8').digest('hex'),
{
key: options.accessKeyId,
secret: options.secretAccessKey,
region: options.region,
expires: options.expires || 15,
key: accessKeyId,
secret: secretAccessKey,
region,
expires,
protocol: 'wss'
}
);
if (options.sessionToken) {
url += '&X-Amz-Security-Token=' + encodeURIComponent(options.sessionToken);
if (sessionToken) {
url += '&X-Amz-Security-Token=' + encodeURIComponent(sessionToken);
}
return websocket(url, ['mqttv3.1'], options.wsOptions);
});
return websocket(url, ['mqttv3.1'], wsOptions);
}, mqttOptions);
}
}

Expand Down
9 changes: 8 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ describe("AWSMqtt", () => {
secretAccessKey: 'NDHu8pMF7MusU5ObAXS3nHTZHBNg/1dz6J/TVjE6',
sessionToken: 'FQoDYXdzEC4aDAXdeKfijZ+FZEDOwyKuAnvcowRhlgZjcsitQh5ICV+TBwrfd1K65A8rzWV6X7tR3nOJSq6YB/QQmWak7D4+7FXNiaLa2szf6YeOaSm6pb6gervq+vi/TJH4mQ38HXM0mHsceqmx28T3Hj7enqCNmEp8C/tIPRfnyQ0jhfvdS9FKoURKPgRU1m/1BZku0Q+tUirFZcHu8mCEjqAAUG3OWcfNaYyhMoYUnEPmQVBWKs2vYzgObC3sDxQq8glXSms5u8/djCWxM1bvpZbvQhll8QfSFUV0ov59DLz7CS51pomLGSkbEoJC5fb+v2KeGLLAbv3hwP6RfkRodjF/H0PkjHzVyfWry5xfbFaoi65eQ/xexBvvZf8NAYWZuNl7jnzAKVL4xIHZWKm/SeOmi/5+C07xm+kqeZJRNmaUeZtfKNa0jrwF',
endpointAddress: 'bfurjhgcnbvcx.iot.eu-west-1.amazonaws.com',
region: 'eu-west-1'
region: 'eu-west-1',
reconnectPeriod: 60000,
connectTimeout: 60000
})
});

it("should be an instance of MqttClient", () => {
expect(awsMqttClient).instanceof(MqttClient);
});

it("should pass options to MqttClient", () => {
expect(awsMqttClient.options.reconnectPeriod).to.equal(60000);
expect(awsMqttClient.options.connectTimeout).to.equal(60000);
});

it("should contain correct url", () => {
expect(awsMqttClient.stream.socket.url).to.startsWith('wss://bfurjhgcnbvcx.iot.eu-west-1.amazonaws.com/mqtt');
});
Expand Down

0 comments on commit cab9eb6

Please sign in to comment.