Skip to content

Commit 2636066

Browse files
authored
Merge pull request #6 from rdkcentral/dev/thunder-token-fixes
- Move to window.thunder or options.thunder for token usage.
2 parents 69ec65f + fb39fbc commit 2636066

7 files changed

+52
-36
lines changed

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ module.exports = {
4141
sourceType: "module",
4242
},
4343
globals: {
44-
globalThis: true,
44+
window: true,
4545
}
4646
}

dist/thunderJS.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module/thunderJS.js

+7-17
Original file line numberDiff line numberDiff line change
@@ -256,25 +256,15 @@ const unregister = function(plugin, event) {
256256
}
257257
};
258258

259-
(function (Object) {
260-
typeof globalThis !== 'object' && (
261-
this ?
262-
get() :
263-
(Object.defineProperty(Object.prototype, '_T_', {
264-
configurable: true,
265-
get: get
266-
}), _T_)
267-
);
268-
function get() {
269-
this.globalThis = this;
270-
delete Object.prototype._T_;
271-
}
272-
}(Object));
273-
274259
let api;
275260
var thunderJS = options => {
276-
if (globalThis.thunder && typeof globalThis.thunder.token === 'function') {
277-
options.token = globalThis.thunder.token();
261+
if (
262+
options.token === undefined &&
263+
typeof window !== 'undefined' &&
264+
window.thunder &&
265+
typeof window.thunder.token === 'function'
266+
) {
267+
options.token = window.thunder.token();
278268
}
279269
api = API(options);
280270
return wrapper({ ...thunder(options), ...plugins })

package-lock.json

-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"Metrological, Wouter <[email protected]>"
55
],
66
"name": "ThunderJS",
7-
"version": "1.2.1",
7+
"version": "1.2.2",
88
"license": "apache",
99
"browser": "dist/thunderJS.js",
1010
"main": "src/thunderJS.js",
@@ -60,7 +60,6 @@
6060
"tape": "^4.13.2"
6161
},
6262
"dependencies": {
63-
"@ungap/global-this": "^0.3.1",
6463
"ws": "^7.2.3"
6564
}
6665
}

src/thunderJS.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
import API from './api'
2121
import plugins from './plugins/index'
2222
import listener from './listener'
23-
import '@ungap/global-this'
2423

2524
let api
2625

2726
export default options => {
2827
// add extra option with token when thunder.token() is available
29-
if (globalThis.thunder && typeof globalThis.thunder.token === 'function') {
30-
options.token = globalThis.thunder.token()
28+
if (
29+
options.token === undefined &&
30+
typeof window !== 'undefined' &&
31+
window.thunder &&
32+
typeof window.thunder.token === 'function'
33+
) {
34+
options.token = window.thunder.token()
3135
}
3236

3337
api = API(options)

tests/thunderJS.api.spec.js

+35-7
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ import * as ws from 'ws'
2626

2727
let wsStub
2828

29-
test('Setup - thunderJS - api', assert => {
30-
wsStub = sinon.stub(ws, 'default').callsFake(address => {
31-
return {
29+
const makeWsStub = () => {
30+
return sinon.stub(ws, 'default').callsFake(address => {
31+
const websocket = {
3232
addEventListener() {},
33+
readyState: 1,
3334
}
35+
36+
return websocket
3437
})
38+
}
39+
40+
test('Setup - thunderJS - api', assert => {
41+
wsStub = makeWsStub()
3542

3643
assert.end()
3744
})
@@ -68,11 +75,10 @@ test('makeWebsocketAddress - unit test', assert => {
6875
assert.end()
6976
})
7077

71-
test('thunderJS - api - custom websocket connection', assert => {
72-
wsStub.resetHistory()
73-
78+
test('thunderJS - api - window.thunder.token() websocket connection', assert => {
7479
// make thunder.token() available
75-
globalThis.thunder = {
80+
global.window = {}
81+
window.thunder = {
7682
token() {
7783
return 'thundertoken123'
7884
},
@@ -94,6 +100,28 @@ test('thunderJS - api - custom websocket connection', assert => {
94100
'Websocket with default custom address should be initiated'
95101
)
96102

103+
delete global.window
104+
assert.end()
105+
})
106+
107+
test('thunderJS - api - options.token websocket connection', assert => {
108+
const options = {
109+
host: '192.168.1.100',
110+
port: 2020,
111+
endpoint: '/api',
112+
protocol: 'wss://',
113+
token: 'thundertoken123',
114+
}
115+
let thunderJS = ThunderJS(options)
116+
117+
// make a call, to initiate a (stubbed) websocket connection
118+
thunderJS.DeviceInfo.systeminfo()
119+
120+
assert.ok(
121+
wsStub.calledWith('wss://192.168.1.100:2020/api?token=thundertoken123'),
122+
'Websocket with default custom address should be initiated'
123+
)
124+
97125
assert.end()
98126
})
99127

0 commit comments

Comments
 (0)