Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable SDKs to run w/out window global, e.g. node #120

Merged
merged 8 commits into from
Jul 31, 2023
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## [2.0.3-next.2](https://github.com/rdkcentral/firebolt-openrpc/compare/v2.0.3-next.1...v2.0.3-next.2) (2023-07-28)


### Bug Fixes

* Use empty object instead of this for window fallback ([c3cece1](https://github.com/rdkcentral/firebolt-openrpc/commit/c3cece17b8ec487a7043190d2932f4bdd70b3ab9))

## [2.0.3-next.1](https://github.com/rdkcentral/firebolt-openrpc/compare/v2.0.2...v2.0.3-next.1) (2023-07-27)


### Bug Fixes

* Actually fixing window issue ([91fa4d0](https://github.com/rdkcentral/firebolt-openrpc/commit/91fa4d0f43aa49ec6db6e27e76e33dff0dcba6b4))
* Fix Server-side rendering reference error on `window` ([0812ae0](https://github.com/rdkcentral/firebolt-openrpc/commit/0812ae075698ec1180d03a3693a322fb8cf1efa6))

## [2.0.2](https://github.com/rdkcentral/firebolt-openrpc/compare/v2.0.1...v2.0.2) (2023-06-22)


Expand Down
12 changes: 7 additions & 5 deletions languages/javascript/src/shared/Transport/LegacyTransport.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* SPDX-License-Identifier: Apache-2.0
*/

const win = typeof window !== 'undefined' ? window : {}

export default class LegacyTransport {
constructor (bridge) {
this.bridge = bridge
Expand All @@ -26,18 +28,18 @@ export default class LegacyTransport {
}

receive (callback) {
window.$badger = window.$badger || {}
win.$badger = win.$badger || {}
/** Hold on to real $badger callback and event methods so they can be called for non-jsonrpc messages */
const badgerCallback = window.$badger.callback ? window.$badger.callback.bind(window.$badger) : null
const badgerEvent = window.$badger.event ? window.$badger.event.bind(window.$badger) : null
window.$badger.callback = (pid, success, json) => {
const badgerCallback = win.$badger.callback ? win.$badger.callback.bind(win.$badger) : null
const badgerEvent = win.$badger.event ? win.$badger.event.bind(win.$badger) : null
win.$badger.callback = (pid, success, json) => {
if (json.jsonrpc) {
callback(JSON.stringify(json))
} else if (badgerCallback) {
badgerCallback(pid, success, json)
}
}
window.$badger.event = (handlerId, json) => {
win.$badger.event = (handlerId, json) => {
if (json.jsonrpc) {
callback(JSON.stringify(json))
} else if (badgerEvent) {
Expand Down
7 changes: 5 additions & 2 deletions languages/javascript/src/shared/Transport/MockTransport.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
* SPDX-License-Identifier: Apache-2.0
*/


const win = typeof window !== 'undefined' ? window : {}

let listener
export const setMockListener = func => { listener = func }

Expand All @@ -26,8 +29,8 @@ const eventMap = {}
let callback
let testHarness

if (window.__firebolt && window.__firebolt.testHarness) {
testHarness = window.__firebolt.testHarness
if (win.__firebolt && win.__firebolt.testHarness) {
testHarness = win.__firebolt.testHarness
}

function send(message) {
Expand Down
38 changes: 20 additions & 18 deletions languages/javascript/src/shared/Transport/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ let moduleInstance = null

const isEventSuccess = x => x && (typeof x.event === 'string') && (typeof x.listening === 'boolean')

const win = typeof window !== 'undefined' ? window : {}

export default class Transport {
constructor () {
this._promises = []
Expand All @@ -51,8 +53,8 @@ export default class Transport {
}

_endpoint () {
if (window.__firebolt && window.__firebolt.endpoint) {
return window.__firebolt.endpoint
if (win.__firebolt && win.__firebolt.endpoint) {
return win.__firebolt.endpoint
}
return null
}
Expand All @@ -64,14 +66,14 @@ export default class Transport {
transport = new WebsocketTransport(endpoint)
transport.receive(this.receiveHandler.bind(this))
} else if (
typeof window.ServiceManager !== 'undefined' &&
window.ServiceManager &&
window.ServiceManager.version
typeof win.ServiceManager !== 'undefined' &&
win.ServiceManager &&
win.ServiceManager.version
) {
// Wire up the queue
transport = this._queue
// get the default bridge service, and flush the queue
window.ServiceManager.getServiceForJavaScript(LEGACY_TRANSPORT_SERVICE_NAME, service => {
win.ServiceManager.getServiceForJavaScript(LEGACY_TRANSPORT_SERVICE_NAME, service => {
if (LegacyTransport.isLegacy(service)) {
transport = new LegacyTransport(service)
} else {
Expand Down Expand Up @@ -194,20 +196,20 @@ export default class Transport {
*/
static get () {
/** Set up singleton and initialize it */
window.__firebolt = window.__firebolt || {}
if ((window.__firebolt.transport == null) && (moduleInstance == null)) {
win.__firebolt = win.__firebolt || {}
if ((win.__firebolt.transport == null) && (moduleInstance == null)) {
const transport = new Transport()
transport.init()
if (transport.isMock) {
/** We should use the mock transport built with the SDK, not a global */
moduleInstance = transport
} else {
window.__firebolt = window.__firebolt || {}
window.__firebolt.transport = transport
win.__firebolt = win.__firebolt || {}
win.__firebolt.transport = transport
}
window.__firebolt.setTransportLayer = transport.setTransportLayer.bind(transport)
win.__firebolt.setTransportLayer = transport.setTransportLayer.bind(transport)
}
return window.__firebolt.transport ? window.__firebolt.transport : moduleInstance
return win.__firebolt.transport ? win.__firebolt.transport : moduleInstance
}

receiveHandler (message) {
Expand Down Expand Up @@ -248,20 +250,20 @@ export default class Transport {
init () {
initSettings({}, { log: true })
this._queue.receive(this.receiveHandler.bind(this))
if (window.__firebolt) {
if (window.__firebolt.mockTransportLayer === true) {
if (win.__firebolt) {
if (win.__firebolt.mockTransportLayer === true) {
this.isMock = true
this.setTransportLayer(mock)
} else if (window.__firebolt.getTransportLayer) {
this.setTransportLayer(window.__firebolt.getTransportLayer())
} else if (win.__firebolt.getTransportLayer) {
this.setTransportLayer(win.__firebolt.getTransportLayer())
}
}
if (this._transport == null) {
this._transport = this.constructTransportLayer()
}
}
}
window.__firebolt = window.__firebolt || {}
window.__firebolt.setTransportLayer = transport => {
win.__firebolt = win.__firebolt || {}
win.__firebolt.setTransportLayer = transport => {
Transport.get().setTransportLayer(transport)
}
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@firebolt-js/openrpc",
"version": "2.0.2",
"version": "2.0.3-next.2",
"description": "The Firebolt SDK Code & Doc Generator",
"main": "languages/javascript/src/sdk.mjs",
"type": "module",
Expand Down