-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
258 lines (241 loc) · 7.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
*
* An abstraction for communications between JavaScript and host platforms.
*
* 1) First you construct your platform-specific configuration (eg: {@link WebkitMessagingConfig})
* 2) Then use that to get an instance of the Messaging utility which allows
* you to send and receive data in a unified way
* 3) Each platform implements {@link MessagingTransport} along with its own Configuration
* - For example, to learn what configuration is required for Webkit, see: {@link WebkitMessagingConfig}
* - Or, to learn about how messages are sent and received in Webkit, see {@link WebkitMessagingTransport}
*
* ## Links
* Please see the following links for examples
*
* - Windows: {@link WindowsMessagingConfig}
* - Webkit: {@link WebkitMessagingConfig}
* - Android: {@link AndroidMessagingConfig}
* - Schema: {@link "Messaging Schema"}
* - Implementation Guide: {@link "Messaging Implementation Guide"}
*
* @module Messaging
*/
import { WindowsMessagingConfig, WindowsMessagingTransport, WindowsInteropMethods, WindowsNotification, WindowsRequestMessage } from './lib/windows.js'
import { WebkitMessagingConfig, WebkitMessagingTransport } from './lib/webkit.js'
import { NotificationMessage, RequestMessage, Subscription, MessageResponse, MessageError, SubscriptionEvent } from './schema.js'
import { AndroidMessagingConfig, AndroidMessagingTransport } from './lib/android.js'
import { createTypedMessages } from './lib/typed-messages.js'
/**
* Common options/config that are *not* transport specific.
*/
export class MessagingContext {
/**
* @param {object} params
* @param {string} params.context
* @param {string} params.featureName
* @param {"production" | "development"} params.env
* @internal
*/
constructor (params) {
this.context = params.context
this.featureName = params.featureName
this.env = params.env
}
}
/**
* @typedef {WebkitMessagingConfig | WindowsMessagingConfig | AndroidMessagingConfig | TestTransportConfig} MessagingConfig
*/
/**
*
*/
export class Messaging {
/**
* @param {MessagingContext} messagingContext
* @param {MessagingConfig} config
*/
constructor (messagingContext, config) {
this.messagingContext = messagingContext
this.transport = getTransport(config, this.messagingContext)
}
/**
* Send a 'fire-and-forget' message.
* @throws {MissingHandler}
*
* @example
*
* ```ts
* const messaging = new Messaging(config)
* messaging.notify("foo", {bar: "baz"})
* ```
* @param {string} name
* @param {Record<string, any>} [data]
*/
notify (name, data = {}) {
const message = new NotificationMessage({
context: this.messagingContext.context,
featureName: this.messagingContext.featureName,
method: name,
params: data
})
this.transport.notify(message)
}
/**
* Send a request, and wait for a response
* @throws {MissingHandler}
*
* @example
* ```
* const messaging = new Messaging(config)
* const response = await messaging.request("foo", {bar: "baz"})
* ```
*
* @param {string} name
* @param {Record<string, any>} [data]
* @return {Promise<any>}
*/
request (name, data = {}) {
const id = globalThis?.crypto?.randomUUID?.() || name + '.response'
const message = new RequestMessage({
context: this.messagingContext.context,
featureName: this.messagingContext.featureName,
method: name,
params: data,
id
})
return this.transport.request(message)
}
/**
* @param {string} name
* @param {(value: unknown) => void} callback
* @return {() => void}
*/
subscribe (name, callback) {
const msg = new Subscription({
context: this.messagingContext.context,
featureName: this.messagingContext.featureName,
subscriptionName: name
})
return this.transport.subscribe(msg, callback)
}
}
/**
* @interface
*/
export class MessagingTransport {
/**
* @param {NotificationMessage} msg
* @returns {void}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
notify (msg) {
throw new Error("must implement 'notify'")
}
/**
* @param {RequestMessage} msg
* @param {{signal?: AbortSignal}} [options]
* @return {Promise<any>}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
request (msg, options = {}) {
throw new Error('must implement')
}
/**
* @param {Subscription} msg
* @param {(value: unknown) => void} callback
* @return {() => void}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
subscribe (msg, callback) {
throw new Error('must implement')
}
}
/**
* Use this to create testing transport on the fly.
* It's useful for debugging, and for enabling scripts to run in
* other environments - for example, testing in a browser without the need
* for a full integration
*/
export class TestTransportConfig {
/**
* @param {MessagingTransport} impl
*/
constructor (impl) {
this.impl = impl
}
}
/**
* @implements {MessagingTransport}
*/
export class TestTransport {
/**
* @param {TestTransportConfig} config
* @param {MessagingContext} messagingContext
*/
constructor (config, messagingContext) {
this.config = config
this.messagingContext = messagingContext
}
notify (msg) {
return this.config.impl.notify(msg)
}
request (msg) {
return this.config.impl.request(msg)
}
subscribe (msg, callback) {
return this.config.impl.subscribe(msg, callback)
}
}
/**
* @param {WebkitMessagingConfig | WindowsMessagingConfig | AndroidMessagingConfig | TestTransportConfig} config
* @param {MessagingContext} messagingContext
* @returns {MessagingTransport}
*/
function getTransport (config, messagingContext) {
if (config instanceof WebkitMessagingConfig) {
return new WebkitMessagingTransport(config, messagingContext)
}
if (config instanceof WindowsMessagingConfig) {
return new WindowsMessagingTransport(config, messagingContext)
}
if (config instanceof AndroidMessagingConfig) {
return new AndroidMessagingTransport(config, messagingContext)
}
if (config instanceof TestTransportConfig) {
return new TestTransport(config, messagingContext)
}
throw new Error('unreachable')
}
/**
* Thrown when a handler cannot be found
*/
export class MissingHandler extends Error {
/**
* @param {string} message
* @param {string} handlerName
*/
constructor (message, handlerName) {
super(message)
this.handlerName = handlerName
}
}
/**
* Some re-exports for convenience
*/
export {
WebkitMessagingConfig,
WebkitMessagingTransport,
WindowsMessagingConfig,
WindowsMessagingTransport,
WindowsInteropMethods,
NotificationMessage,
RequestMessage,
Subscription,
MessageResponse,
MessageError,
SubscriptionEvent,
WindowsNotification,
WindowsRequestMessage,
AndroidMessagingConfig,
AndroidMessagingTransport,
createTypedMessages
}