Skip to content

Commit 85f1db4

Browse files
Merge pull request #33 from devicehive/development
Development
2 parents 0d0fe22 + 8d3c8ae commit 85f1db4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2160
-149
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ DeviceHive module
137137
| [options.accessToken] | <code>string</code> | Access token |
138138
| [options.refreshToken] | <code>string</code> | Refresh token |
139139
| [options.login] | <code>string</code> | Login |
140-
| [options.password] | <code>string</code> | Paaword |
140+
| [options.password] | <code>string</code> | Password |
141141
| options.mainServiceURL | <code>string</code> | Main Service URL |
142142
| [options.authServiceURL] | <code>string</code> | Auth Service URL (required only for http) |
143-
| [options.pluginServiceURL] | <code>string</code> | Alug inServi ceURL (required only for http) |
143+
| [options.pluginServiceURL] | <code>string</code> | Plugin Service URL (required only for http) |
144+
| [options.autoUpdateSession] | <code>boolean</code> | Flag to enable/disable autoupdating session. Default: true |
144145

145146
<a name="DeviceHive+connect"></a>
146147

@@ -2552,7 +2553,7 @@ Creates Plugin Update Query model
25522553
| [options.returnCommands] | <code>boolean</code> | Checks if commands should be returned |
25532554
| [options.returnUpdatedCommands] | <code>boolean</code> | Checks if updated commands should be returned |
25542555
| [options.returnNotifications] | <code>boolean</code> | Checks if commands should be returned |
2555-
| [options.status] | <code>string</code> | Plugin status - active or disabled (ACTIVE | DISABLED | CREATED) |
2556+
| [options.status] | <code>string</code> | Plugin status - active or disabled (ACTIVE | INACTIVE | CREATED) |
25562557
| [options.name] | <code>string</code> | Plugin name |
25572558
| [options.description] | <code>string</code> | Plugin description |
25582559
| [options.parameters] | <code>string</code> | Plugin parameters |
@@ -2621,12 +2622,3 @@ Creates User List Query
26212622

26222623
### userListQuery.toObject() ⇒ <code>Object</code>
26232624
Returns instance as a plain JS object
2624-
2625-
2626-
2627-
## License
2628-
2629-
[DeviceHive] is developed by [DataArt] Apps and distributed under Open Source
2630-
[Apache 2.0 license](https://en.wikipedia.org/wiki/Apache_License).
2631-
2632-
© Copyright 2018 [DataArt] Apps © All Rights Reservedstrong text

example/node/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const DeviceHive = require(`../../index`);
22

3+
34
const httpDeviceHive = new DeviceHive({
45
login: `dhadmin`,
56
password: `dhadmin_#911`,
@@ -11,7 +12,8 @@ const httpDeviceHive = new DeviceHive({
1112
const wsDeviceHive = new DeviceHive({
1213
login: `dhadmin`,
1314
password: `dhadmin_#911`,
14-
mainServiceURL: 'ws://localhost:8080/dh/websocket'
15+
mainServiceURL: 'ws://localhost:8080/dh/websocket',
16+
autoUpdateSession: true
1517
});
1618

1719
const DeviceListQuery = DeviceHive.models.query.DeviceListQuery;
@@ -48,7 +50,10 @@ void async function start () {
4850
console.log(await wsDeviceHive.network.list(networkListQuery));
4951
console.log(await wsDeviceHive.token.refresh(refreshToken));
5052
}
53+
5154
} catch (error) {
5255
console.warn(error);
5356
}
57+
58+
process.exit(1);
5459
}();

package-lock.json

Lines changed: 35 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"main": "index.js",
66
"scripts": {
77
"build": "node build.js",
8-
"test": "node_modules/.bin/mocha test/unit/**/**.spec.js --exit"
8+
"test": "npm run unitTest & npm run integrationTest",
9+
"unitTest": "node_modules/.bin/mocha test/unit/**/**.spec.js --exit",
10+
"integrationTest": "node_modules/.bin/mocha test/integration/**/**.spec.js --timeout 10000 --exit"
911
},
1012
"repository": {
1113
"type": "git",
@@ -15,21 +17,23 @@
1517
"license": "Apache-2.0",
1618
"dependencies": {
1719
"isomorphic-fetch": "^2.2.1",
20+
"jwt-decode": "^2.2.0",
1821
"query-string": "^5.1.0",
22+
"randomstring": "^1.1.5",
1923
"string-template": "^1.0.0",
2024
"universal-websocket-client": "^1.0.1"
2125
},
2226
"devDependencies": {
2327
"@babel/core": "^7.0.0-beta.40",
2428
"@babel/preset-env": "^7.0.0-beta.40",
2529
"babel-core": "^6.26.0",
26-
"babel-loader": "^8.0.0-beta.0",
30+
"babel-loader": "^8.0.0-beta.2",
2731
"babelify": "^8.0.0",
2832
"browserify": "^15.2.0",
2933
"chai": "^4.1.2",
3034
"exorcist": "^1.0.1",
3135
"mocha": "^5.0.1",
3236
"tinyify": "^2.4.0",
33-
"ws": "^4.0.0"
37+
"ws": "^4.1.0"
3438
}
3539
}

src/ApiStrategy.js

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class ApiStrategy extends EventEmitter {
2020
static getType(url) {
2121
let result;
2222

23-
if (url.startsWith('http') || url.startsWith('https')) {
23+
if (url.startsWith(HTTP.TYPE)) {
2424
result = HTTP;
25-
} else if (url.startsWith('ws') || url.startsWith('wss')) {
25+
} else if (url.startsWith(WS.TYPE)) {
2626
result = WS;
2727
} else {
2828
throw new UnsupportedTransportError();
@@ -40,6 +40,8 @@ class ApiStrategy extends EventEmitter {
4040

4141
const me = this;
4242

43+
me.reconnectionHandler = null;
44+
4345
me.urlsMap = new Map();
4446

4547
me.urlsMap.set(API.MAIN_BASE, mainServiceURL);
@@ -48,7 +50,20 @@ class ApiStrategy extends EventEmitter {
4850

4951
me.strategy = new (ApiStrategy.getType(mainServiceURL))({ mainServiceURL, authServiceURL, pluginServiceURL });
5052

51-
me.strategy.on(`message`, (message) => { me.emit(`message`, message) });
53+
me.strategy.on(`message`, (message) => {
54+
switch (me.strategy.type) {
55+
case HTTP.TYPE:
56+
me.emit(`message`, message);
57+
break;
58+
case WS.TYPE:
59+
if (message.subscriptionId && message.action) {
60+
me.emit(`message`, message[message.action.split(`/`)[0]]);
61+
} else {
62+
me.emit(`message`, message);
63+
}
64+
break;
65+
}
66+
});
5267
}
5368

5469

@@ -83,7 +98,25 @@ class ApiStrategy extends EventEmitter {
8398
}
8499

85100
return me.strategy.send(sendData)
86-
.then((response) => API.normalizeResponse(me.strategy.type, key, response));
101+
.then((response) => API.normalizeResponse(me.strategy.type, key, response))
102+
.catch(error => {
103+
if (error === Utils.TOKEN_EXPIRED_MARK && me.reconnectionHandler) {
104+
return me.reconnectionHandler()
105+
.then(() => me.strategy.send(sendData))
106+
.then((response) => API.normalizeResponse(me.strategy.type, key, response));
107+
} else {
108+
throw error;
109+
}
110+
});
111+
}
112+
113+
/**
114+
* Disconnects transport
115+
*/
116+
disconnect() {
117+
const me = this;
118+
119+
me.strategy.disconnect();
87120
}
88121
}
89122

src/DeviceHive.js

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const Utils = require(`./utils/Utils`);
12
const EventEmitter = require('events');
23
const APIStrategy = require('./ApiStrategy');
34
const InfoAPI = require('./controllers/ServerInfoAPI');
@@ -98,7 +99,7 @@ class DeviceHive extends EventEmitter {
9899
* @param {string} [options.authServiceURL] - Auth Service URL (required only for http)
99100
* @param {string} [options.pluginServiceURL] - Alug inServi ceURL (required only for http)
100101
*/
101-
constructor({ accessToken, refreshToken, login, password, mainServiceURL, authServiceURL, pluginServiceURL }) {
102+
constructor({ mainServiceURL, authServiceURL, pluginServiceURL, accessToken, refreshToken, login, password, autoUpdateSession = true }) {
102103
super();
103104

104105
const me = this;
@@ -107,6 +108,7 @@ class DeviceHive extends EventEmitter {
107108
me.refreshToken = refreshToken;
108109
me.login = login;
109110
me.password = password;
111+
me.autoUpdateSession = autoUpdateSession;
110112

111113
me.strategy = new APIStrategy({ mainServiceURL, authServiceURL, pluginServiceURL });
112114

@@ -130,29 +132,65 @@ class DeviceHive extends EventEmitter {
130132
* Connect to the DeviceHive service
131133
* @returns {Promise<DeviceHive>}
132134
*/
133-
async connect() {
135+
async connect({ accessToken, refreshToken, login, password } = {}) {
134136
const me = this;
135137

136-
if (me.accessToken || me.refreshToken || (me.login && me.password)) {
138+
if (!accessToken && !refreshToken && !(login && password)) {
139+
accessToken = accessToken || me.accessToken;
140+
refreshToken = refreshToken || me.refreshToken;
141+
login = login || me.login;
142+
password = password || me.password;
143+
}
144+
145+
if (accessToken || refreshToken || (login && password)) {
137146
try {
138-
if (me.accessToken) {
139-
await me.strategy.authorize(me.accessToken);
140-
} else if (me.refreshToken) {
141-
const accessToken = await me.token.refresh(me.refreshToken);
147+
if (login && password) {
148+
const { accessToken, refreshToken } = await me.token.login(login, password);
149+
142150
await me.strategy.authorize(accessToken);
143-
} else if (me.login && me.password) {
144-
const { accessToken } = await me.token.login(me.login, me.password);
151+
152+
me.accessToken = accessToken;
153+
me.refreshToken = refreshToken;
154+
} else if (refreshToken) {
155+
const { accessToken } = await me.token.refresh(refreshToken);
156+
145157
await me.strategy.authorize(accessToken);
158+
159+
me.accessToken = accessToken;
160+
me.refreshToken = refreshToken;
161+
} else if (accessToken) {
162+
await me.strategy.authorize(accessToken);
163+
164+
me.accessToken = accessToken;
165+
}
166+
167+
if (me.autoUpdateSession === true) {
168+
const userTokens = await me.token.createUserToken(
169+
Utils.createUserTokenFromJWT(me.accessToken));
170+
171+
me.accessToken = userTokens.accessToken;
172+
me.refreshToken = userTokens.refreshToken;
173+
me.strategy.reconnectionHandler = () => me.connect({ refreshToken: me.refreshToken });
146174
}
147175
} catch (error) {
148-
throw new InvalidCredentialsError();
176+
throw new InvalidCredentialsError(error);
149177
}
150178
} else {
151179
throw new NoAuthCredentialsError();
152180
}
153181

154182
return me;
155183
}
184+
185+
/**
186+
* Disconnects from DeviceHive server
187+
* @returns {*|void}
188+
*/
189+
disconnect() {
190+
const me = this;
191+
192+
return me.strategy.disconnect();
193+
}
156194
}
157195

158196

0 commit comments

Comments
 (0)