@firebolt-js/core-client 1.0.0-next.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/CONTRIBUTING.md +4 -0
  3. package/LICENSE +202 -0
  4. package/NOTICE +24 -0
  5. package/README.md +26 -0
  6. package/dist/docs/Accessibility/index.md +384 -0
  7. package/dist/docs/Accessibility/schemas/index.md +52 -0
  8. package/dist/docs/Advertising/index.md +120 -0
  9. package/dist/docs/Device/index.md +297 -0
  10. package/dist/docs/Discovery/index.md +128 -0
  11. package/dist/docs/Display/index.md +87 -0
  12. package/dist/docs/Internal/index.md +68 -0
  13. package/dist/docs/Lifecycle2/index.md +160 -0
  14. package/dist/docs/Localization/index.md +314 -0
  15. package/dist/docs/Localization/schemas/index.md +41 -0
  16. package/dist/docs/Metrics/index.md +987 -0
  17. package/dist/docs/Network/index.md +128 -0
  18. package/dist/docs/Policies/schemas/index.md +25 -0
  19. package/dist/docs/Presentation/index.md +53 -0
  20. package/dist/docs/Stats/index.md +63 -0
  21. package/dist/docs/TextToSpeech/index.md +524 -0
  22. package/dist/docs/Types/schemas/index.md +37 -0
  23. package/dist/firebolt-core-app-open-rpc.json +1082 -0
  24. package/dist/firebolt-core-open-rpc.json +3773 -0
  25. package/dist/lib/Accessibility/defaults.mjs +61 -0
  26. package/dist/lib/Accessibility/index.mjs +148 -0
  27. package/dist/lib/Advertising/defaults.mjs +26 -0
  28. package/dist/lib/Advertising/index.mjs +31 -0
  29. package/dist/lib/Device/defaults.mjs +34 -0
  30. package/dist/lib/Device/index.mjs +84 -0
  31. package/dist/lib/Discovery/defaults.mjs +22 -0
  32. package/dist/lib/Discovery/index.mjs +34 -0
  33. package/dist/lib/Events/index.mjs +345 -0
  34. package/dist/lib/Gateway/AppApi.mjs +125 -0
  35. package/dist/lib/Gateway/Bidirectional.mjs +113 -0
  36. package/dist/lib/Gateway/PlatformApi.mjs +130 -0
  37. package/dist/lib/Gateway/index.mjs +48 -0
  38. package/dist/lib/Localization/defaults.mjs +44 -0
  39. package/dist/lib/Localization/index.mjs +123 -0
  40. package/dist/lib/Log/index.mjs +57 -0
  41. package/dist/lib/Metrics/defaults.mjs +40 -0
  42. package/dist/lib/Metrics/index.mjs +206 -0
  43. package/dist/lib/Network/defaults.mjs +24 -0
  44. package/dist/lib/Network/index.mjs +70 -0
  45. package/dist/lib/Platform/defaults.mjs +27 -0
  46. package/dist/lib/Platform/index.mjs +28 -0
  47. package/dist/lib/Prop/MockProps.mjs +28 -0
  48. package/dist/lib/Prop/Router.mjs +25 -0
  49. package/dist/lib/Prop/index.mjs +60 -0
  50. package/dist/lib/Settings/index.mjs +86 -0
  51. package/dist/lib/Transport/MockTransport.mjs +191 -0
  52. package/dist/lib/Transport/WebsocketTransport.mjs +55 -0
  53. package/dist/lib/Transport/index.mjs +81 -0
  54. package/dist/lib/firebolt.d.ts +795 -0
  55. package/dist/lib/firebolt.mjs +51 -0
  56. package/firebolt-js-core-client-1.0.0-next.5.tgz +0 -0
  57. package/package.json +54 -0
@@ -0,0 +1,25 @@
1
+ export default function (params, callbackOrValue, contextParameterCount) {
2
+ const numArgs = params ? Object.values(params).length : 0
3
+
4
+ if (numArgs === contextParameterCount && callbackOrValue === undefined) {
5
+ // getter
6
+ return 'getter'
7
+ } else if (
8
+ numArgs === contextParameterCount &&
9
+ typeof callbackOrValue === 'function'
10
+ ) {
11
+ // subscribe
12
+ return 'subscriber'
13
+ } else if (numArgs === 0 && typeof callbackOrValue === 'function') {
14
+ // subscribe
15
+ return 'subscriber'
16
+ } else if (
17
+ numArgs === contextParameterCount &&
18
+ callbackOrValue !== undefined
19
+ ) {
20
+ // setter
21
+ return 'setter'
22
+ }
23
+
24
+ return null
25
+ }
@@ -0,0 +1,60 @@
1
+ import Gateway from '../Gateway/index.mjs'
2
+ import Events from '../Events/index.mjs'
3
+ import router from './Router.mjs'
4
+
5
+ function prop(
6
+ moduleName,
7
+ key,
8
+ params,
9
+ callbackOrValue = undefined,
10
+ immutable,
11
+ readonly,
12
+ contextParameterCount,
13
+ ) {
14
+ const numArgs = Object.values(params).length
15
+ const type = router(params, callbackOrValue, contextParameterCount)
16
+
17
+ if (type === 'getter') {
18
+ return Gateway.request(moduleName + '.' + key, params)
19
+ } else if (type === 'subscriber') {
20
+ // subscriber
21
+ if (immutable) {
22
+ throw new Error('Cannot subscribe to an immutable property')
23
+ }
24
+ const subscriber =
25
+ 'on' + key[0].toUpperCase() + key.substring(1) + 'Changed'
26
+ return Events.listen(
27
+ moduleName,
28
+ subscriber,
29
+ ...Object.values(params),
30
+ callbackOrValue,
31
+ )
32
+ } else if (type === 'setter') {
33
+ // setter
34
+ if (immutable) {
35
+ throw new Error('Cannot set a value to an immutable property')
36
+ }
37
+ if (readonly) {
38
+ throw new Error('Cannot set a value to a readonly property')
39
+ }
40
+ return Gateway.request(
41
+ moduleName + '.set' + key[0].toUpperCase() + key.substring(1),
42
+ Object.assign(
43
+ {
44
+ value: callbackOrValue,
45
+ },
46
+ params,
47
+ ),
48
+ )
49
+ } else if (numArgs < contextParameterCount) {
50
+ throw new Error(
51
+ 'Cannot get a value without all required context parameters.',
52
+ )
53
+ } else {
54
+ throw new Error('Property accessed with unexpected number of parameters.')
55
+ }
56
+ }
57
+
58
+ export default {
59
+ prop: prop,
60
+ }
@@ -0,0 +1,86 @@
1
+ /*
2
+ * Copyright 2021 Comcast Cable Communications Management, LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * SPDX-License-Identifier: Apache-2.0
17
+ */
18
+
19
+ const settings = { platform: {} }
20
+ const subscribers = {}
21
+
22
+ export const initSettings = (appSettings, platformSettings) => {
23
+ settings['app'] = appSettings
24
+ settings['platform'] = {
25
+ logLevel: 'WARN',
26
+ ...platformSettings,
27
+ }
28
+ settings['user'] = {}
29
+ }
30
+
31
+ const publish = (key, value) => {
32
+ subscribers[key] &&
33
+ subscribers[key].forEach((subscriber) => subscriber(value))
34
+ }
35
+
36
+ const dotGrab = (obj = {}, key) => {
37
+ const keys = key.split('.')
38
+ for (let i = 0; i < keys.length; i++) {
39
+ obj = obj[keys[i]] = obj[keys[i]] !== undefined ? obj[keys[i]] : {}
40
+ }
41
+ return typeof obj === 'object'
42
+ ? Object.keys(obj).length
43
+ ? obj
44
+ : undefined
45
+ : obj
46
+ }
47
+
48
+ export default {
49
+ get(type, key, fallback = undefined) {
50
+ const val = dotGrab(settings[type], key)
51
+ return val !== undefined ? val : fallback
52
+ },
53
+ has(type, key) {
54
+ return !!this.get(type, key)
55
+ },
56
+ set(key, value) {
57
+ settings['user'][key] = value
58
+ publish(key, value)
59
+ },
60
+ subscribe(key, callback) {
61
+ subscribers[key] = subscribers[key] || []
62
+ subscribers[key].push(callback)
63
+ },
64
+ unsubscribe(key, callback) {
65
+ if (callback) {
66
+ const index =
67
+ subscribers[key] && subscribers[key].findIndex((cb) => cb === callback)
68
+ index > -1 && subscribers[key].splice(index, 1)
69
+ } else {
70
+ if (key in subscribers) {
71
+ subscribers[key] = []
72
+ }
73
+ }
74
+ },
75
+ clearSubscribers() {
76
+ for (const key of Object.getOwnPropertyNames(subscribers)) {
77
+ delete subscribers[key]
78
+ }
79
+ },
80
+ setLogLevel(logLevel) {
81
+ settings.platform.logLevel = logLevel
82
+ },
83
+ getLogLevel() {
84
+ return settings.platform.logLevel
85
+ },
86
+ }
@@ -0,0 +1,191 @@
1
+ /*
2
+ * Copyright 2021 Comcast Cable Communications Management, LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * SPDX-License-Identifier: Apache-2.0
17
+ */
18
+
19
+ import Gateway from '../Gateway/index.mjs'
20
+
21
+ const win = typeof window !== 'undefined' ? window : {}
22
+
23
+ let listener
24
+ export const setMockListener = (func) => {
25
+ listener = func
26
+ }
27
+
28
+ let mock
29
+ const pending = []
30
+
31
+ let callback
32
+ let testHarness
33
+
34
+ if (win.__firebolt && win.__firebolt.testHarness) {
35
+ testHarness = win.__firebolt.testHarness
36
+ }
37
+
38
+ function send(message) {
39
+ const json = JSON.parse(message)
40
+ // handle bulk sends
41
+ if (Array.isArray(json)) {
42
+ json.forEach((json) => send(JSON.stringify(json)))
43
+ return
44
+ }
45
+
46
+ //let [module, method] = json.method.split('.')
47
+
48
+ if (testHarness && testHarness.send) {
49
+ testHarness.send(message)
50
+ }
51
+
52
+ if (json.method) {
53
+ if (mock) handle(json)
54
+ else pending.push(json)
55
+ } else if (json.id !== undefined && requests[json.id]) {
56
+ const promise = requests[json.id]
57
+ if (json.error !== undefined) {
58
+ promise.reject(json.error)
59
+ } else {
60
+ promise.resolve(json.result)
61
+ }
62
+
63
+ delete requests[json.id]
64
+ }
65
+ }
66
+
67
+ function handle(json) {
68
+ let result
69
+ try {
70
+ result = getResult(json.method, json.params)
71
+ setTimeout(() =>
72
+ callback(
73
+ JSON.stringify({
74
+ jsonrpc: '2.0',
75
+ result: result,
76
+ id: json.id,
77
+ }),
78
+ ),
79
+ )
80
+ } catch (error) {
81
+ setTimeout(() =>
82
+ callback(
83
+ JSON.stringify({
84
+ jsonrpc: '2.0',
85
+ error: {
86
+ code: -32602,
87
+ message:
88
+ 'Invalid params (this is a mock error from the mock transport layer)',
89
+ },
90
+ id: json.id,
91
+ }),
92
+ ),
93
+ )
94
+ }
95
+ }
96
+
97
+ function receive(_callback) {
98
+ callback = _callback
99
+
100
+ if (testHarness && typeof testHarness.initialize === 'function') {
101
+ testHarness.initialize({
102
+ emit: (module, method, value) => {
103
+ Gateway.simulate(`${module}.${method}`, value)
104
+ },
105
+ listen: function (...args) {
106
+ listener(...args)
107
+ },
108
+ })
109
+ }
110
+ }
111
+
112
+ function event(module, event, data) {
113
+ callback(
114
+ JSON.stringify({
115
+ jsonrpc: '2.0',
116
+ method: `${module}.${event}`,
117
+ params: {
118
+ value: data,
119
+ },
120
+ }),
121
+ )
122
+ }
123
+
124
+ function receiveMessage(message) {
125
+ callback(message)
126
+ }
127
+
128
+ let id = 0
129
+ const requests = []
130
+
131
+ function request(method, params) {
132
+ const requestId = id++
133
+ const promise = new Promise((resolve, reject) => {
134
+ requests[requestId] = { resolve, reject }
135
+ })
136
+ callback(
137
+ JSON.stringify({
138
+ jsonrpc: '2.0',
139
+ id: requestId,
140
+ method: `${method}`,
141
+ params: params,
142
+ }),
143
+ )
144
+
145
+ return promise
146
+ }
147
+
148
+ function dotGrab(obj = {}, key) {
149
+ const keys = key.split('.')
150
+ let ref = obj
151
+ for (let i = 0; i < keys.length; i++) {
152
+ ref = (Object.entries(ref).find(
153
+ ([k, v]) => k.toLowerCase() === keys[i].toLowerCase(),
154
+ ) || [null, {}])[1]
155
+ }
156
+ return ref
157
+ }
158
+
159
+ function getResult(method, params) {
160
+ let api = dotGrab(mock, method)
161
+
162
+ if (method.match(/^[a-zA-Z]+\.on[A-Za-z]+$/)) {
163
+ api = {
164
+ event: method,
165
+ listening: true,
166
+ }
167
+ }
168
+
169
+ if (typeof api === 'function') {
170
+ let result = params == null ? api() : api(params)
171
+ if (result === undefined) {
172
+ result = null
173
+ }
174
+ return result
175
+ } else return api
176
+ }
177
+
178
+ export function setMockResponses(m) {
179
+ mock = m
180
+
181
+ pending.forEach((json) => handle(json))
182
+ pending.length = 0
183
+ }
184
+
185
+ export default {
186
+ send: send,
187
+ receiveMessage: receiveMessage,
188
+ handle: handle,
189
+ event: event,
190
+ receive: receive,
191
+ }
@@ -0,0 +1,55 @@
1
+ const MAX_QUEUED_MESSAGES = 100
2
+
3
+ export default class WebsocketTransport {
4
+ constructor(endpoint) {
5
+ this._endpoint = endpoint
6
+ this._ws = null
7
+ this._connected = false
8
+ this._queue = []
9
+ this._callbacks = []
10
+ }
11
+
12
+ send(msg) {
13
+ this._connect()
14
+
15
+ if (this._connected) {
16
+ this._ws.send(msg)
17
+ } else {
18
+ if (this._queue.length < MAX_QUEUED_MESSAGES) {
19
+ this._queue.push(msg)
20
+ }
21
+ }
22
+ }
23
+
24
+ receive(callback) {
25
+ if (!callback) return
26
+ this._connect()
27
+ this._callbacks.push(callback)
28
+ }
29
+
30
+ _notifyCallbacks(message) {
31
+ for (let i = 0; i < this._callbacks.length; i++) {
32
+ setTimeout(() => this._callbacks[i](message), 1)
33
+ }
34
+ }
35
+
36
+ _connect() {
37
+ if (this._ws) return
38
+ this._ws = new WebSocket(this._endpoint, ['jsonrpc'])
39
+ this._ws.addEventListener('message', (message) => {
40
+ this._notifyCallbacks(message.data)
41
+ })
42
+ this._ws.addEventListener('error', (message) => {})
43
+ this._ws.addEventListener('close', (message) => {
44
+ this._ws = null
45
+ this._connected = false
46
+ })
47
+ this._ws.addEventListener('open', (message) => {
48
+ this._connected = true
49
+ for (let i = 0; i < this._queue.length; i++) {
50
+ this._ws.send(this._queue[i])
51
+ }
52
+ this._queue = []
53
+ })
54
+ }
55
+ }
@@ -0,0 +1,81 @@
1
+ /*
2
+ * Copyright 2021 Comcast Cable Communications Management, LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ *
16
+ * SPDX-License-Identifier: Apache-2.0
17
+ */
18
+
19
+ import MockTransport from './MockTransport.mjs'
20
+ import Settings, { initSettings } from '../Settings/index.mjs'
21
+ import WebsocketTransport from './WebsocketTransport.mjs'
22
+
23
+ const win = typeof window !== 'undefined' ? window : {}
24
+ win.__firebolt = win.__firebolt || {}
25
+
26
+ initSettings({}, { log: true })
27
+
28
+ let implementation
29
+ let _callback
30
+
31
+ export function send(json) {
32
+ implementation = getImplementation()
33
+
34
+ if (Settings.getLogLevel() === 'DEBUG') {
35
+ console.debug(
36
+ 'Sending message to transport: \n' +
37
+ JSON.stringify(json, { indent: '\t' }),
38
+ )
39
+ }
40
+
41
+ implementation.send(JSON.stringify(json))
42
+ }
43
+
44
+ export function receive(callback) {
45
+ if (implementation) {
46
+ implementation.receive(callback)
47
+ } else {
48
+ _callback = callback
49
+ }
50
+ }
51
+
52
+ function getImplementation() {
53
+ if (implementation) {
54
+ return implementation
55
+ }
56
+
57
+ if (win.__firebolt.transport) {
58
+ implementation = win.__firebolt.transport
59
+ } else if (win.__firebolt.endpoint) {
60
+ // Only adds RPCv2=true query parameter when using bidirectional SDK.
61
+ // This parameter will not be present when using unidirectional SDK.
62
+ // Unidirectional endpoint is handled in Gateway/Unidirectional.mjs
63
+ const endpoint = win.__firebolt.endpoint
64
+ const url = endpoint + (endpoint.includes('?') ? '&' : '?') + 'RPCv2=true'
65
+ implementation = new WebsocketTransport(url)
66
+ } else {
67
+ implementation = MockTransport
68
+ }
69
+
70
+ win.__firebolt.transport = implementation
71
+ implementation.receive(_callback)
72
+
73
+ _callback = undefined
74
+
75
+ return implementation
76
+ }
77
+
78
+ export default {
79
+ send,
80
+ receive,
81
+ }