@ndustrial/contxt-sdk
Advanced tools
Comparing version 0.0.39 to 0.0.40
@@ -0,1 +1,11 @@ | ||
## [v0.0.40](http://github.com/ndustrialio/contxt-sdk-js/tree/v0.0.40) (2019-02-18) | ||
**Added** | ||
- Added `Bus#connect` to connect to the message bus via WebSockets | ||
- Added `WebSocketConnection` class to wrap the open WebSocket connection | ||
- `authorize` for authorizing publish and/or subscribe for a particular channel | ||
- `close` for closing the WebSocket connection | ||
- `publish` for publishing a message to the message bus | ||
## [v0.0.39](http://github.com/ndustrialio/contxt-sdk-js/tree/v0.0.39) (2019-02-12) | ||
@@ -2,0 +12,0 @@ |
@@ -7,2 +7,8 @@ <a name="Bus"></a> | ||
**Kind**: global class | ||
* [Bus](#Bus) | ||
* [new Bus(sdk, request)](#new_Bus_new) | ||
* [.connect(organizationId)](#Bus+connect) ⇒ <code>Promise</code> | ||
* [.getWebSocketConnection(organizationId)](#Bus+getWebSocketConnection) ⇒ [<code>WebSocketConnection</code>](#WebSocketConnection) | ||
<a name="new_Bus_new"></a> | ||
@@ -17,1 +23,41 @@ | ||
<a name="Bus+connect"></a> | ||
### contxtSdk.bus.connect(organizationId) ⇒ <code>Promise</code> | ||
Connects to the message bus via websocket. | ||
If a connection already exists for that organization id, the connection is returned, otherwise a new connection is created and returned. | ||
**Kind**: instance method of [<code>Bus</code>](#Bus) | ||
**Fulfill**: [<code>WebSocketConnection</code>](#WebSocketConnection) | ||
**Reject**: <code>errorEvent</code> The error event | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| organizationId | <code>string</code> | UUID corresponding with an organization | | ||
**Example** | ||
```js | ||
contxtSdk.bus.connect('4f0e51c6-728b-4892-9863-6d002e61204d') | ||
.then((webSocket) => { | ||
console.log(webSocket); | ||
}) | ||
.catch((errorEvent) => { | ||
console.log(errorEvent); | ||
}); | ||
``` | ||
<a name="Bus+getWebSocketConnection"></a> | ||
### contxtSdk.bus.getWebSocketConnection(organizationId) ⇒ [<code>WebSocketConnection</code>](#WebSocketConnection) | ||
Gets the WebSocketConnection for an organization id | ||
If a connection already exists for that organization id, the connection is returned, otherwise returns undefined. | ||
**Kind**: instance method of [<code>Bus</code>](#Bus) | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| organizationId | <code>string</code> | UUID corresponding with an organization | | ||
**Example** | ||
```js | ||
const messageBusWebSocket = contxtSdk.bus.getWebSocketConnection('4f0e51c6-728b-4892-9863-6d002e61204d'); | ||
``` |
@@ -94,2 +94,6 @@ ## Classes | ||
<dd></dd> | ||
<dt><a href="./WebSocketConnection.md">WebSocketConnection</a></dt> | ||
<dd><p>Module that wraps the websocket connection to the message bus | ||
to provide the developer with a specific set of functionality</p> | ||
</dd> | ||
</dl> | ||
@@ -205,3 +209,9 @@ | ||
<dd></dd> | ||
<dt><a href="./Typedefs.md#WebSocket">WebSocket</a> : <code>Object</code></dt> | ||
<dd><p>The raw WebSocket created by ws</p> | ||
</dd> | ||
<dt><a href="./Typedefs.md#WebSocketConnection">WebSocketConnection</a> : <code>Object</code></dt> | ||
<dd><p>A wrapper around the raw WebSocket to provide a finite set of operations</p> | ||
</dd> | ||
</dl> | ||
@@ -706,1 +706,129 @@ <a name="Asset"></a> | ||
<a name="WebSocket"></a> | ||
## WebSocket : <code>Object</code> | ||
The raw WebSocket created by ws | ||
**Kind**: global typedef | ||
**Properties** | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| addEventListener | <code>function</code> | Register an event listener emulating the EventTarget interface | | ||
| binaryType | <code>string</code> | A string indicating the type of binary data being transmitted by the connection. This should be one of "nodebuffer", "arraybuffer" or "fragments". Defaults to "nodebuffer". | | ||
| bufferedAmount | <code>number</code> | The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. | | ||
| close | <code>function</code> | Initiate a closing handshake | | ||
| extensions | <code>Object</code> | An object containing the negotiated extensions | | ||
| onclose | <code>function</code> | An event listener to be called when connection is closed | | ||
| onerror | <code>function</code> | An event listener to be called when an error occurs | | ||
| onmessage | <code>function</code> | An event listener to be called when a message is received from the server | | ||
| onopen | <code>function</code> | An event listener to be called when the connection is established | | ||
| ping | <code>function</code> | Send a ping to the WebSocket server | | ||
| pong | <code>function</code> | Send a pong to the WebSocket server | | ||
| protocol | <code>string</code> | The subprotocol selected by the server | | ||
| readyState | <code>number</code> | The current state of the connection | | ||
| removeEventListener | <code>function</code> | Removes an event listener emulating the EventTarget interface | | ||
| send | <code>function</code> | Send data through the open WebSocket connection | | ||
| terminate | <code>function</code> | Forcibly close the connection | | ||
| url | <code>string</code> | The URL of the WebSocket server | | ||
<a name="WebSocketConnection"></a> | ||
## WebSocketConnection : <code>Object</code> | ||
A wrapper around the raw WebSocket to provide a finite set of operations | ||
**Kind**: global typedef | ||
**Properties** | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| close | <code>function</code> | Closes the WebSocket connection to the message bus server | | ||
| _organizationId | <code>string</code> | The organization id for the open WebSocket connection | | ||
| _webSocket | [<code>WebSocket</code>](./Typedefs.md#WebSocket) | The raw WebSocket connection to the message bus | | ||
* [WebSocketConnection](#WebSocketConnection) : <code>Object</code> | ||
* [new WebSocketConnection(webSocket, organizationId)](#new_WebSocketConnection_new) | ||
* [.authorize(token)](#WebSocketConnection+authorize) ⇒ <code>Promise</code> | ||
* [.close()](#WebSocketConnection+close) | ||
* [.publish(serviceClientId, channel, message)](#WebSocketConnection+publish) ⇒ <code>Promise</code> | ||
<a name="new_WebSocketConnection_new"></a> | ||
### new WebSocketConnection(webSocket, organizationId) | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| webSocket | [<code>WebSocket</code>](./Typedefs.md#WebSocket) | A WebSocket connection to the message bus | | ||
| organizationId | <code>string</code> | UUID corresponding with an organization | | ||
<a name="WebSocketConnection+authorize"></a> | ||
### webSocketConnection.authorize(token) ⇒ <code>Promise</code> | ||
Sends a message to the message bus to authorize a channel | ||
**Kind**: instance method of [<code>WebSocketConnection</code>](#WebSocketConnection) | ||
**Fulfill**: | ||
**Reject**: <code>error</code> The error event from the WebSocket or the error message from the message bus | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| token | <code>string</code> | JSON Web Signature containing the channel and actions needed for authorization | | ||
**Example** | ||
```js | ||
contxtSdk.bus.connect('4f0e51c6-728b-4892-9863-6d002e61204d') | ||
.then((webSocket) => { | ||
webSocket.authorize(token).then(() => { | ||
console.log("authorization successful") | ||
}) | ||
.catch((authError) => { | ||
console.log(authError) | ||
}); | ||
}) | ||
}); | ||
``` | ||
<a name="WebSocketConnection+close"></a> | ||
### webSocketConnection.close() | ||
Closes the websocket connection | ||
**Kind**: instance method of [<code>WebSocketConnection</code>](#WebSocketConnection) | ||
**Example** | ||
```js | ||
contxtSdk.bus.connect('4f0e51c6-728b-4892-9863-6d002e61204d') | ||
.then((webSocket) => { | ||
webSocket.close() | ||
}) | ||
.catch((errorEvent) => { | ||
console.log(errorEvent); | ||
}); | ||
``` | ||
<a name="WebSocketConnection+publish"></a> | ||
### webSocketConnection.publish(serviceClientId, channel, message) ⇒ <code>Promise</code> | ||
Publishes a message to a specific channel on the message bus | ||
**Kind**: instance method of [<code>WebSocketConnection</code>](#WebSocketConnection) | ||
**Fulfill**: | ||
**Reject**: <code>error</code> The error event from the WebSocket or the error message from the message bus | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| serviceClientId | <code>string</code> | Client ID of the message bus service | | ||
| channel | <code>string</code> | Message bus channel the message is being sent to | | ||
| message | <code>Any</code> | Message being sent to the message bus. Must be valid JSON. | | ||
**Example** | ||
```js | ||
contxtSdk.bus.connect('4f0e51c6-728b-4892-9863-6d002e61204d') | ||
.then((webSocket) => { | ||
webSocket.publish('GCXd2bwE9fgvqxygrx2J7TkDJ3ef', 'feed:1', {"example": 1}).then(() => { | ||
console.log("publish successful") | ||
}) | ||
.catch((error) => { | ||
console.log(error) | ||
}); | ||
}) | ||
}); | ||
``` |
{ | ||
"name": "@ndustrial/contxt-sdk", | ||
"version": "0.0.39", | ||
"version": "0.0.40", | ||
"description": "", | ||
@@ -41,3 +41,3 @@ "main": "lib/index.js", | ||
"babel-plugin-istanbul": "^4.1.5", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-stage-2": "^6.24.1", | ||
@@ -60,5 +60,7 @@ "babel-register": "^6.26.0", | ||
"mocha": "^4.1.0", | ||
"nyc": "^13.0.1", | ||
"mock-socket": "^8.0.5", | ||
"nyc": "^13.3.0", | ||
"prettier": "^1.14.3", | ||
"pretty-quick": "^1.4.1", | ||
"proxyquire": "^2.1.0", | ||
"rollup": "^0.54.1", | ||
@@ -72,8 +74,10 @@ "rollup-plugin-babel": "^3.0.3", | ||
"sinon": "^4.1.6", | ||
"sinon-chai": "^2.14.0" | ||
"sinon-chai": "^2.14.0", | ||
"ws": "~6.1.3" | ||
}, | ||
"peerDependencies": { | ||
"auth0-js": "^9.3.0", | ||
"axios": "~0.17.0" | ||
"axios": "~0.17.0", | ||
"ws": "~6.1.3" | ||
} | ||
} |
@@ -11,6 +11,6 @@ # contxt-sdk [![wercker status](https://app.wercker.com/status/869ef086297da79ddd0cbf3564f7cba6/s/master 'wercker status')](https://app.wercker.com/project/byKey/869ef086297da79ddd0cbf3564f7cba6) | ||
There are two peer dependencies for `contxt-sdk`, `auth0-js` and `axios`. If you don't already have a compatible version installed, run: | ||
There are three peer dependencies for `contxt-sdk`: `auth0-js`, `axios`, and `ws`. If you don't already have a compatible version installed, run: | ||
```bash | ||
npm install --save auth0-js@^9.0.0 axios@~0.17.0 | ||
npm install --save auth0-js@^9.0.0 axios@~0.17.0 ws@~6.1.3 | ||
``` | ||
@@ -98,26 +98,26 @@ | ||
* `npm test` - Lints, sets up tracking for Istanbul coverage reports, and runs the test suite | ||
* `npm run test:js` - Runs the test suite | ||
* `npm run test:js:dev` - Runs the test suite in watch mode, where the tests are re-run whenever a file changes | ||
* `npm run test:js:inspect` - Runs the test suite in inspect/inspect-brk mode. Insert a `debugger` somewhere in your code and connect to the debugger with this command. (Node 8: visit `chrome://inspect` to connect. Node 6: Copy and paste the blob provided in the terminal into Chrome to connect. Older versions also have ways to connect.) | ||
* `npm run lint` - Lints the source code | ||
* `npm run coverage` - Sets up tracking for Istanbul coverage reports and runs the test suite | ||
* `npm run report` - Parses the Istanbul coverage reports and writes them to file (in `./coverage`) and displays them in terminal | ||
- `npm test` - Lints, sets up tracking for Istanbul coverage reports, and runs the test suite | ||
- `npm run test:js` - Runs the test suite | ||
- `npm run test:js:dev` - Runs the test suite in watch mode, where the tests are re-run whenever a file changes | ||
- `npm run test:js:inspect` - Runs the test suite in inspect/inspect-brk mode. Insert a `debugger` somewhere in your code and connect to the debugger with this command. (Node 8: visit `chrome://inspect` to connect. Node 6: Copy and paste the blob provided in the terminal into Chrome to connect. Older versions also have ways to connect.) | ||
- `npm run lint` - Lints the source code | ||
- `npm run coverage` - Sets up tracking for Istanbul coverage reports and runs the test suite | ||
- `npm run report` - Parses the Istanbul coverage reports and writes them to file (in `./coverage`) and displays them in terminal | ||
Some tools used for testing and ensuring code quality include: | ||
* [Chai.js](http://chaijs.com/) (specifically the `expect` syntax) | ||
* [chai-as-promised](https://github.com/domenic/chai-as-promised) | ||
* [ESLint](https://eslint.org/) | ||
* [eslint-config-ndustrial](https://github.com/ndustrialio/eslint-config-ndustrial) | ||
* [faker.js](https://github.com/marak/Faker.js/) | ||
* [Mocha](https://mochajs.org/) | ||
* [Sinon.JS](http://sinonjs.org/) | ||
* [sinon-chai](http://chaijs.com/plugins/sinon-chai/) | ||
- [Chai.js](http://chaijs.com/) (specifically the `expect` syntax) | ||
- [chai-as-promised](https://github.com/domenic/chai-as-promised) | ||
- [ESLint](https://eslint.org/) | ||
- [eslint-config-ndustrial](https://github.com/ndustrialio/eslint-config-ndustrial) | ||
- [faker.js](https://github.com/marak/Faker.js/) | ||
- [Mocha](https://mochajs.org/) | ||
- [Sinon.JS](http://sinonjs.org/) | ||
- [sinon-chai](http://chaijs.com/plugins/sinon-chai/) | ||
Additionally, some globals have been added to the testing environment to streamline the process slightly: | ||
* `expect` - Corresponds with `require('chai').expect`. [Info](http://chaijs.com/api/bdd/) | ||
* `faker` - Corresponds with `require('faker')`. [Info](https://github.com/marak/Faker.js/) | ||
* `sandbox` - Corresponds with `require('sinon').sandbox`. Should be used anytime when wanting to create a `sinon.spy` or `sinon.stub` as it can be easily used to reset/restore multiple spys and stubs. [Info](http://sinonjs.org/releases/v4.1.6/sandbox/) | ||
- `expect` - Corresponds with `require('chai').expect`. [Info](http://chaijs.com/api/bdd/) | ||
- `faker` - Corresponds with `require('faker')`. [Info](https://github.com/marak/Faker.js/) | ||
- `sandbox` - Corresponds with `require('sinon').sandbox`. Should be used anytime when wanting to create a `sinon.spy` or `sinon.stub` as it can be easily used to reset/restore multiple spys and stubs. [Info](http://sinonjs.org/releases/v4.1.6/sandbox/) | ||
@@ -141,4 +141,4 @@ ## Contributing/Publishing | ||
1. Run `npm version x.x.x` in your terminal on `master` where the `x`'s are the new version numbers. | ||
* This sets the new version in `package.json` and `package-lock.json` and also creates a new `git tag`. | ||
* Example `npm version 0.30.1` | ||
- This sets the new version in `package.json` and `package-lock.json` and also creates a new `git tag`. | ||
- Example `npm version 0.30.1` | ||
1. Perform a `git push --tags origin master` while on your local copy of `master`. | ||
@@ -145,0 +145,0 @@ 1. Perform an `npm publish` to publish the updated package to NPM. |
@@ -7,3 +7,3 @@ import babel from 'rollup-plugin-babel'; | ||
export default { | ||
external: ['auth0-js', 'axios'], | ||
external: ['auth0-js', 'axios', 'ws'], | ||
input: 'src/index.js', | ||
@@ -10,0 +10,0 @@ output: [ |
@@ -5,3 +5,3 @@ import omit from 'lodash.omit'; | ||
describe('Channels', function() { | ||
describe('Bus/Channels', function() { | ||
let baseRequest; | ||
@@ -8,0 +8,0 @@ let baseSdk; |
@@ -0,4 +1,39 @@ | ||
import WebSocket from 'ws'; | ||
import Channels from './channels'; | ||
import WebSocketConnection from './webSocketConnection'; | ||
/** | ||
* The raw WebSocket created by ws | ||
* | ||
* @typedef {Object} WebSocket | ||
* @property {function} addEventListener Register an event listener emulating the EventTarget interface | ||
* @property {string} binaryType A string indicating the type of binary data being transmitted by the connection. This should be one of "nodebuffer", "arraybuffer" or "fragments". Defaults to "nodebuffer". | ||
* @property {number} bufferedAmount The number of bytes of data that have been queued using calls to send() but not yet transmitted to the network. | ||
* @property {function} close Initiate a closing handshake | ||
* @property {Object} extensions An object containing the negotiated extensions | ||
* @property {function} onclose An event listener to be called when connection is closed | ||
* @property {function} onerror An event listener to be called when an error occurs | ||
* @property {function} onmessage An event listener to be called when a message is received from the server | ||
* @property {function} onopen An event listener to be called when the connection is established | ||
* @property {function} ping Send a ping to the WebSocket server | ||
* @property {function} pong Send a pong to the WebSocket server | ||
* @property {string} protocol The subprotocol selected by the server | ||
* @property {number} readyState The current state of the connection | ||
* @property {function} removeEventListener Removes an event listener emulating the EventTarget interface | ||
* @property {function} send Send data through the open WebSocket connection | ||
* @property {function} terminate Forcibly close the connection | ||
* @property {string} url The URL of the WebSocket server | ||
*/ | ||
/** | ||
* A wrapper around the raw WebSocket to provide a finite set of operations | ||
* | ||
* @typedef {Object} WebSocketConnection | ||
* @property {function} close Closes the WebSocket connection to the message bus server | ||
* @property {string} _organizationId The organization id for the open WebSocket connection | ||
* @property {WebSocket} _webSocket The raw WebSocket connection to the message bus | ||
*/ | ||
/** | ||
* Module that provides access to the message bus | ||
@@ -15,11 +50,90 @@ * | ||
const baseUrl = `${sdk.config.audiences.bus.host}`; | ||
const baseWebSocketUrl = `${sdk.config.audiences.bus.webSocket}`; | ||
this._baseWebSocketUrl = baseWebSocketUrl; | ||
this._baseUrl = baseUrl; | ||
this._request = request; | ||
this._sdk = sdk; | ||
this._webSockets = {}; | ||
this.channels = new Channels(sdk, request, baseUrl); | ||
} | ||
/** | ||
* Connects to the message bus via websocket. | ||
* If a connection already exists for that organization id, the connection is returned, otherwise a new connection is created and returned. | ||
* | ||
* @param {string} organizationId UUID corresponding with an organization | ||
* | ||
* @returns {Promise} | ||
* @fulfill {WebSocketConnection} | ||
* @reject {errorEvent} The error event | ||
* | ||
* @example | ||
* contxtSdk.bus.connect('4f0e51c6-728b-4892-9863-6d002e61204d') | ||
* .then((webSocket) => { | ||
* console.log(webSocket); | ||
* }) | ||
* .catch((errorEvent) => { | ||
* console.log(errorEvent); | ||
* }); | ||
*/ | ||
connect(organizationId) { | ||
return new Promise((resolve, reject) => { | ||
if (this._webSockets[organizationId]) { | ||
return resolve(this._webSockets[organizationId]); | ||
} | ||
return this._sdk.auth | ||
.getCurrentApiToken('contxtAuth') | ||
.then((apiToken) => { | ||
const ws = new WebSocket( | ||
`${this._baseWebSocketUrl}/organizations/${organizationId}/stream`, | ||
[], | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${apiToken}` | ||
} | ||
} | ||
); | ||
ws.onopen = (event) => { | ||
this._webSockets[organizationId] = new WebSocketConnection( | ||
ws, | ||
organizationId | ||
); | ||
resolve(this._webSockets[organizationId]); | ||
}; | ||
ws.onclose = (event) => { | ||
this._webSockets[organizationId] = null; | ||
}; | ||
ws.onerror = (errorEvent) => { | ||
reject(errorEvent); | ||
}; | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
/** | ||
* Gets the WebSocketConnection for an organization id | ||
* If a connection already exists for that organization id, the connection is returned, otherwise returns undefined. | ||
* | ||
* @param {string} organizationId UUID corresponding with an organization | ||
* | ||
* @returns {WebSocketConnection} | ||
* | ||
* @example | ||
* const messageBusWebSocket = contxtSdk.bus.getWebSocketConnection('4f0e51c6-728b-4892-9863-6d002e61204d'); | ||
*/ | ||
getWebSocketConnection(organizationId) { | ||
return this._webSockets[organizationId]; | ||
} | ||
} | ||
export default Bus; |
@@ -5,7 +5,9 @@ export default { | ||
clientId: 'bmoJ2jhfyIP5s4IZx5Ss6xQ9lmkoDixO', | ||
host: 'https://bus.ndustrial.io' | ||
host: 'https://bus.ndustrial.io', | ||
webSocket: 'wss://bus.ndustrial.io' | ||
}, | ||
staging: { | ||
clientId: '1HD1NG1VTBtkqRt2HRRj3E3hdmqmwzoz', | ||
host: 'https://bus-staging.ndustrial.io' | ||
clientId: 'WLlg0VHqGneLuPNMFKCoJDjUlhocDFEF', | ||
host: 'https://bus-staging.ndustrial.io', | ||
webSocket: 'wss://bus-staging.ndustrial.io' | ||
} | ||
@@ -12,0 +14,0 @@ }, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1859205
154
24827
7
38