@ignite-ember/extension-bridge 0.0.0-stage → 0.2.0
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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +303 -2
- package/dist/bridge.d.ts +33 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +92 -0
- package/dist/bridge.js.map +1 -0
- package/dist/channel.d.ts +26 -0
- package/dist/channel.d.ts.map +1 -0
- package/dist/channel.js +36 -0
- package/dist/channel.js.map +1 -0
- package/dist/contract.d.ts +110 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +9 -0
- package/dist/contract.js.map +1 -0
- package/dist/correlator.d.ts +37 -0
- package/dist/correlator.d.ts.map +1 -0
- package/dist/correlator.js +67 -0
- package/dist/correlator.js.map +1 -0
- package/dist/guestBridge.d.ts +63 -0
- package/dist/guestBridge.d.ts.map +1 -0
- package/dist/guestBridge.js +156 -0
- package/dist/guestBridge.js.map +1 -0
- package/dist/hostModel.d.ts +155 -0
- package/dist/hostModel.d.ts.map +1 -0
- package/dist/hostModel.generated.d.ts +1870 -0
- package/dist/hostModel.generated.d.ts.map +1 -0
- package/dist/hostModel.generated.js +1241 -0
- package/dist/hostModel.generated.js.map +1 -0
- package/dist/hostModel.js +168 -0
- package/dist/hostModel.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/protocol.d.ts +338 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +135 -0
- package/dist/protocol.js.map +1 -0
- package/dist/service.d.ts +143 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +179 -0
- package/dist/service.js.map +1 -0
- package/dist/signals.d.ts +21 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +52 -0
- package/dist/signals.js.map +1 -0
- package/dist/tokens.d.ts +95 -0
- package/dist/tokens.d.ts.map +1 -0
- package/dist/tokens.js +158 -0
- package/dist/tokens.js.map +1 -0
- package/dist/watchers.d.ts +12 -0
- package/dist/watchers.d.ts.map +1 -0
- package/dist/watchers.js +18 -0
- package/dist/watchers.js.map +1 -0
- package/package.json +53 -4
- package/src/bridge.ts +112 -0
- package/src/channel.ts +48 -0
- package/src/contract.ts +130 -0
- package/src/correlator.ts +81 -0
- package/src/guestBridge.ts +261 -0
- package/src/hostModel.generated.ts +2649 -0
- package/src/hostModel.ts +209 -0
- package/src/index.ts +11 -0
- package/src/protocol.ts +475 -0
- package/src/service.ts +253 -0
- package/src/signals.ts +65 -0
- package/src/tokens.ts +165 -0
- package/src/watchers.ts +18 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The bridge an extension's page holds once the host has said hello.
|
|
3
|
+
*
|
|
4
|
+
* It owns everything the page learned from the host — params, theme,
|
|
5
|
+
* tokens — and the three kinds of request it can be waiting on, each
|
|
6
|
+
* with its own {@link Correlator}. Messages from the host are routed by
|
|
7
|
+
* kind through one table, {@link GuestBridge.handlers}.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { HostChannel } from "./channel.js";
|
|
11
|
+
import type { Bridge } from "./contract.js";
|
|
12
|
+
import { Correlator } from "./correlator.js";
|
|
13
|
+
import { HostError, type HostLink, type Signal } from "./hostModel.js";
|
|
14
|
+
import { Host } from "./hostModel.generated.js";
|
|
15
|
+
import type {
|
|
16
|
+
ExtensionSettings,
|
|
17
|
+
HelloMessage,
|
|
18
|
+
HostMessage,
|
|
19
|
+
HostReplyMessage,
|
|
20
|
+
HostRequestMessage,
|
|
21
|
+
LaunchParams,
|
|
22
|
+
LocationState,
|
|
23
|
+
ParamsMessage,
|
|
24
|
+
ResultMessage,
|
|
25
|
+
RpcArgs,
|
|
26
|
+
ServiceTokenReplyMessage,
|
|
27
|
+
ThemeMessage,
|
|
28
|
+
} from "./protocol.js";
|
|
29
|
+
import { type IssuedToken, ServiceError, ServiceLink } from "./service.js";
|
|
30
|
+
import { SignalSubscriptions } from "./signals.js";
|
|
31
|
+
import { applyThemeTokens, type ThemeTokens } from "./tokens.js";
|
|
32
|
+
import { Watchers } from "./watchers.js";
|
|
33
|
+
|
|
34
|
+
/** How long a request waits before giving up. The host has its own. */
|
|
35
|
+
export const DEFAULT_TIMEOUT_MS = 30_000;
|
|
36
|
+
|
|
37
|
+
/** Members whose answer is the person's, so no timeout applies to them. */
|
|
38
|
+
const WAITS_ON_THE_PERSON = new Set(["ui.show_dialog"]);
|
|
39
|
+
|
|
40
|
+
/** One handler per kind of message the host sends. */
|
|
41
|
+
type HostMessageHandlers = {
|
|
42
|
+
[K in HostMessage["kind"]]: (
|
|
43
|
+
message: Extract<HostMessage, { kind: K }>,
|
|
44
|
+
) => void;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** A host-model request, without the envelope and the id. */
|
|
48
|
+
type HostRequestBody = Omit<
|
|
49
|
+
HostRequestMessage,
|
|
50
|
+
"igni" | "kind" | "nonce" | "id"
|
|
51
|
+
>;
|
|
52
|
+
|
|
53
|
+
export class GuestBridge<
|
|
54
|
+
P extends LaunchParams,
|
|
55
|
+
S extends ExtensionSettings,
|
|
56
|
+
> implements Bridge<P, S> {
|
|
57
|
+
readonly extensionId: string;
|
|
58
|
+
readonly settings: S;
|
|
59
|
+
readonly host: Host;
|
|
60
|
+
readonly service: ServiceLink;
|
|
61
|
+
|
|
62
|
+
private currentParams: P;
|
|
63
|
+
private currentTheme: string;
|
|
64
|
+
private currentTokens: ThemeTokens;
|
|
65
|
+
private readonly themeWatchers = new Watchers<string>();
|
|
66
|
+
private readonly paramWatchers = new Watchers<P>();
|
|
67
|
+
/** One numbering for every request, whatever its kind. */
|
|
68
|
+
private requestCount = 0;
|
|
69
|
+
private readonly nextRequest = () => ++this.requestCount;
|
|
70
|
+
private readonly calls = new Correlator("", this.nextRequest);
|
|
71
|
+
private readonly hostRequests = new Correlator("h", this.nextRequest);
|
|
72
|
+
private readonly tokenRequests = new Correlator("s", this.nextRequest);
|
|
73
|
+
private readonly signals = new SignalSubscriptions((op, signal) =>
|
|
74
|
+
this.hostRequest({ op, path: "", signals: [signal] }),
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
private readonly handlers: HostMessageHandlers = {
|
|
78
|
+
// Connecting already consumed the greeting; a repeat changes nothing.
|
|
79
|
+
hello: () => {},
|
|
80
|
+
theme: (message) => this.applyTheme(message),
|
|
81
|
+
params: (message) => this.applyParams(message),
|
|
82
|
+
signal: (message) => this.signals.deliver(message),
|
|
83
|
+
"host-reply": (message) => this.settleHostRequest(message),
|
|
84
|
+
"service-token-reply": (message) => this.settleTokenRequest(message),
|
|
85
|
+
result: (message) => this.settleCall(message),
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* `params` and `settings` are the extension's to name (`P`, `S`): the
|
|
90
|
+
* host sends what the extension's own links and manifest put there.
|
|
91
|
+
*/
|
|
92
|
+
constructor(
|
|
93
|
+
private readonly channel: HostChannel,
|
|
94
|
+
hello: HelloMessage,
|
|
95
|
+
) {
|
|
96
|
+
this.extensionId = hello.extensionId;
|
|
97
|
+
this.settings = (hello.settings ?? {}) as S;
|
|
98
|
+
this.currentParams = hello.params as P;
|
|
99
|
+
this.currentTheme = hello.theme;
|
|
100
|
+
this.currentTokens = hello.tokens;
|
|
101
|
+
const link: HostLink = {
|
|
102
|
+
request: (op, path, options) =>
|
|
103
|
+
this.hostRequest({
|
|
104
|
+
op,
|
|
105
|
+
path,
|
|
106
|
+
window: options.window ?? null,
|
|
107
|
+
...(options.session ? { session: options.session } : {}),
|
|
108
|
+
...(options.params !== undefined ? { params: options.params } : {}),
|
|
109
|
+
...(op === "set" ? { value: options.value } : {}),
|
|
110
|
+
}),
|
|
111
|
+
};
|
|
112
|
+
this.host = new Host(link);
|
|
113
|
+
this.service = new ServiceLink(hello.serviceUrl ?? null, () => this.askForToken());
|
|
114
|
+
// Applied before the page renders anything, so an extension never
|
|
115
|
+
// paints once in the fallback palette and again in the real one.
|
|
116
|
+
applyThemeTokens(this.currentTokens, document.documentElement);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
get params(): P {
|
|
120
|
+
return this.currentParams;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
get theme(): string {
|
|
124
|
+
return this.currentTheme;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
get tokens(): ThemeTokens {
|
|
128
|
+
return this.currentTokens;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Act on one message the host sent. */
|
|
132
|
+
receive(message: HostMessage): void {
|
|
133
|
+
// The table is keyed by kind, so this handler takes this message;
|
|
134
|
+
// TypeScript cannot follow that correlation through an index.
|
|
135
|
+
const handle = this.handlers[message.kind] as (
|
|
136
|
+
message: HostMessage,
|
|
137
|
+
) => void;
|
|
138
|
+
handle(message);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
rpc<T = unknown>(
|
|
142
|
+
method: string,
|
|
143
|
+
args: RpcArgs = {},
|
|
144
|
+
timeoutMs: number = DEFAULT_TIMEOUT_MS,
|
|
145
|
+
): Promise<T> {
|
|
146
|
+
// `Number`, not `timeoutMs` as it is: null means "no limit" to a
|
|
147
|
+
// correlator, but an untyped caller's null (or "500") reaches a
|
|
148
|
+
// call's timer the way `setTimeout` always read it — as 0 (or 500).
|
|
149
|
+
return this.calls.send<T>(
|
|
150
|
+
Number(timeoutMs),
|
|
151
|
+
() => new Error(`igni bridge: ${method} timed out after ${timeoutMs}ms`),
|
|
152
|
+
(id) => this.channel.post({ kind: "call", id, method, args }),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
navigate(to: string): void {
|
|
157
|
+
this.channel.post({ kind: "navigate", to });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
close(): void {
|
|
161
|
+
this.channel.post({ kind: "close" });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
onTheme(fn: (theme: string) => void): () => void {
|
|
165
|
+
return this.themeWatchers.add(fn);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
onParams(fn: (params: P) => void): () => void {
|
|
169
|
+
return this.paramWatchers.add(fn);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
setLocation(state: LocationState): void {
|
|
173
|
+
this.channel.post({ kind: "location", state });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
on<T>(
|
|
177
|
+
signal: Signal<T>,
|
|
178
|
+
fn: (value: T) => void,
|
|
179
|
+
onRefused?: (error: HostError) => void,
|
|
180
|
+
): () => void {
|
|
181
|
+
return this.signals.on(signal, fn, onRefused);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
private applyTheme(message: ThemeMessage): void {
|
|
185
|
+
this.currentTheme = message.theme;
|
|
186
|
+
this.currentTokens = message.tokens;
|
|
187
|
+
applyThemeTokens(this.currentTokens, document.documentElement);
|
|
188
|
+
this.themeWatchers.emit(this.currentTheme);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private applyParams(message: ParamsMessage): void {
|
|
192
|
+
this.currentParams = message.params as P;
|
|
193
|
+
this.paramWatchers.emit(this.currentParams);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private hostRequest(body: HostRequestBody): Promise<unknown> {
|
|
197
|
+
// A dialog waits on the person, for as long as they take.
|
|
198
|
+
const waits = WAITS_ON_THE_PERSON.has(body.path);
|
|
199
|
+
return this.hostRequests.send(
|
|
200
|
+
waits ? null : DEFAULT_TIMEOUT_MS,
|
|
201
|
+
() =>
|
|
202
|
+
new HostError(
|
|
203
|
+
"failed",
|
|
204
|
+
`igni bridge: host.${body.path || body.op} timed out after ${DEFAULT_TIMEOUT_MS}ms`,
|
|
205
|
+
),
|
|
206
|
+
(id) => this.channel.post({ kind: "host", id, ...body }),
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** One token request to the host, which asks igni as this extension. */
|
|
211
|
+
private askForToken(): Promise<IssuedToken> {
|
|
212
|
+
return this.tokenRequests.send<IssuedToken>(
|
|
213
|
+
DEFAULT_TIMEOUT_MS,
|
|
214
|
+
() =>
|
|
215
|
+
new ServiceError(
|
|
216
|
+
"failed",
|
|
217
|
+
`igni bridge: no service token within ${DEFAULT_TIMEOUT_MS}ms`,
|
|
218
|
+
),
|
|
219
|
+
(id) => this.channel.post({ kind: "service-token", id }),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
private settleCall(reply: ResultMessage): void {
|
|
224
|
+
if (reply.ok) this.calls.resolve(reply.id, reply.value);
|
|
225
|
+
else
|
|
226
|
+
this.calls.reject(
|
|
227
|
+
reply.id,
|
|
228
|
+
new Error(
|
|
229
|
+
reply.error || "the host refused the call without saying why",
|
|
230
|
+
),
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
private settleHostRequest(reply: HostReplyMessage): void {
|
|
235
|
+
if (reply.ok) this.hostRequests.resolve(reply.id, reply.result);
|
|
236
|
+
else
|
|
237
|
+
this.hostRequests.reject(
|
|
238
|
+
reply.id,
|
|
239
|
+
new HostError(
|
|
240
|
+
reply.error?.code ?? "failed",
|
|
241
|
+
reply.error?.message ?? "the host refused without saying why",
|
|
242
|
+
),
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
private settleTokenRequest(reply: ServiceTokenReplyMessage): void {
|
|
247
|
+
if (reply.ok && typeof reply.token === "string")
|
|
248
|
+
this.tokenRequests.resolve(reply.id, {
|
|
249
|
+
token: reply.token,
|
|
250
|
+
expiresIn: reply.expiresIn ?? 0,
|
|
251
|
+
});
|
|
252
|
+
else
|
|
253
|
+
this.tokenRequests.reject(
|
|
254
|
+
reply.id,
|
|
255
|
+
new ServiceError(
|
|
256
|
+
"refused",
|
|
257
|
+
reply.error || "igni gave no token for this extension's service",
|
|
258
|
+
),
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
}
|