Skip to content

Commit

Permalink
Merge pull request #6 from rdkcentral/dev/thunder-token-fixes
Browse files Browse the repository at this point in the history
- Move to window.thunder or options.thunder for token usage.
  • Loading branch information
wouterlucas authored Apr 16, 2020
2 parents 69ec65f + fb39fbc commit 2636066
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ module.exports = {
sourceType: "module",
},
globals: {
globalThis: true,
window: true,
}
}
2 changes: 1 addition & 1 deletion dist/thunderJS.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 7 additions & 17 deletions module/thunderJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,25 +256,15 @@ const unregister = function(plugin, event) {
}
};

(function (Object) {
typeof globalThis !== 'object' && (
this ?
get() :
(Object.defineProperty(Object.prototype, '_T_', {
configurable: true,
get: get
}), _T_)
);
function get() {
this.globalThis = this;
delete Object.prototype._T_;
}
}(Object));

let api;
var thunderJS = options => {
if (globalThis.thunder && typeof globalThis.thunder.token === 'function') {
options.token = globalThis.thunder.token();
if (
options.token === undefined &&
typeof window !== 'undefined' &&
window.thunder &&
typeof window.thunder.token === 'function'
) {
options.token = window.thunder.token();
}
api = API(options);
return wrapper({ ...thunder(options), ...plugins })
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Metrological, Wouter <[email protected]>"
],
"name": "ThunderJS",
"version": "1.2.1",
"version": "1.2.2",
"license": "apache",
"browser": "dist/thunderJS.js",
"main": "src/thunderJS.js",
Expand Down Expand Up @@ -60,7 +60,6 @@
"tape": "^4.13.2"
},
"dependencies": {
"@ungap/global-this": "^0.3.1",
"ws": "^7.2.3"
}
}
10 changes: 7 additions & 3 deletions src/thunderJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
import API from './api'
import plugins from './plugins/index'
import listener from './listener'
import '@ungap/global-this'

let api

export default options => {
// add extra option with token when thunder.token() is available
if (globalThis.thunder && typeof globalThis.thunder.token === 'function') {
options.token = globalThis.thunder.token()
if (
options.token === undefined &&
typeof window !== 'undefined' &&
window.thunder &&
typeof window.thunder.token === 'function'
) {
options.token = window.thunder.token()
}

api = API(options)
Expand Down
42 changes: 35 additions & 7 deletions tests/thunderJS.api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ import * as ws from 'ws'

let wsStub

test('Setup - thunderJS - api', assert => {
wsStub = sinon.stub(ws, 'default').callsFake(address => {
return {
const makeWsStub = () => {
return sinon.stub(ws, 'default').callsFake(address => {
const websocket = {
addEventListener() {},
readyState: 1,
}

return websocket
})
}

test('Setup - thunderJS - api', assert => {
wsStub = makeWsStub()

assert.end()
})
Expand Down Expand Up @@ -68,11 +75,10 @@ test('makeWebsocketAddress - unit test', assert => {
assert.end()
})

test('thunderJS - api - custom websocket connection', assert => {
wsStub.resetHistory()

test('thunderJS - api - window.thunder.token() websocket connection', assert => {
// make thunder.token() available
globalThis.thunder = {
global.window = {}
window.thunder = {
token() {
return 'thundertoken123'
},
Expand All @@ -94,6 +100,28 @@ test('thunderJS - api - custom websocket connection', assert => {
'Websocket with default custom address should be initiated'
)

delete global.window
assert.end()
})

test('thunderJS - api - options.token websocket connection', assert => {
const options = {
host: '192.168.1.100',
port: 2020,
endpoint: '/api',
protocol: 'wss://',
token: 'thundertoken123',
}
let thunderJS = ThunderJS(options)

// make a call, to initiate a (stubbed) websocket connection
thunderJS.DeviceInfo.systeminfo()

assert.ok(
wsStub.calledWith('wss://192.168.1.100:2020/api?token=thundertoken123'),
'Websocket with default custom address should be initiated'
)

assert.end()
})

Expand Down

0 comments on commit 2636066

Please sign in to comment.