@d2m/april 0.0.0-stage → 0.1.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/dist/bridge.d.ts +116 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +1046 -0
- package/dist/bridge.js.map +1 -0
- package/dist/errors.d.ts +6 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +188 -0
- package/dist/errors.js.map +1 -0
- package/dist/events.d.ts +18 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +85 -0
- package/dist/events.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/initialization.d.ts +46 -0
- package/dist/internal/initialization.d.ts.map +1 -0
- package/dist/internal/initialization.js +569 -0
- package/dist/internal/initialization.js.map +1 -0
- package/dist/internal/modules.d.ts +54 -0
- package/dist/internal/modules.d.ts.map +1 -0
- package/dist/internal/modules.js +318 -0
- package/dist/internal/modules.js.map +1 -0
- package/dist/internal/mount.d.ts +21 -0
- package/dist/internal/mount.d.ts.map +1 -0
- package/dist/internal/mount.js +305 -0
- package/dist/internal/mount.js.map +1 -0
- package/dist/internal/shell-core-entry.d.ts +2 -0
- package/dist/internal/shell-core-entry.d.ts.map +1 -0
- package/dist/internal/shell-core-entry.js +63 -0
- package/dist/internal/shell-core-entry.js.map +1 -0
- package/dist/internal/shell-core-modules.d.ts +6 -0
- package/dist/internal/shell-core-modules.d.ts.map +1 -0
- package/dist/internal/shell-core-modules.js +218 -0
- package/dist/internal/shell-core-modules.js.map +1 -0
- package/dist/internal/shell-core.d.ts +17 -0
- package/dist/internal/shell-core.d.ts.map +1 -0
- package/dist/internal/shell-core.js +2 -0
- package/dist/internal/shell-core.js.map +1 -0
- package/dist/internal/shell-transport.d.ts +12 -0
- package/dist/internal/shell-transport.d.ts.map +1 -0
- package/dist/internal/shell-transport.js +33 -0
- package/dist/internal/shell-transport.js.map +1 -0
- package/dist/types.d.ts +123 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +21 -3
- package/README.md +0 -3
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
import { createAprilError, isAprilError, normalizeAprilError } from "./errors.js";
|
|
2
|
+
import { AprilInboundMessageQueue, MAX_INCOMING_QUEUE_MESSAGES } from "./events.js";
|
|
3
|
+
const MAX_WIRE_MESSAGE_BYTES = 1048576;
|
|
4
|
+
const MAX_SEQUENCE = 9007199254740991;
|
|
5
|
+
const SHELL_COMMAND_TIMEOUT_MS = 30000;
|
|
6
|
+
const inboundMessageKeys = new Set([
|
|
7
|
+
"protocolVersion",
|
|
8
|
+
"bridgeId",
|
|
9
|
+
"sender",
|
|
10
|
+
"pageSequence",
|
|
11
|
+
"shellSequence",
|
|
12
|
+
"callId",
|
|
13
|
+
"kind",
|
|
14
|
+
"invocationKind",
|
|
15
|
+
"correlationId",
|
|
16
|
+
"bridgeToken",
|
|
17
|
+
"capability",
|
|
18
|
+
"action",
|
|
19
|
+
"contractVersion",
|
|
20
|
+
"payload",
|
|
21
|
+
"connectionRef",
|
|
22
|
+
"requestKind",
|
|
23
|
+
"sessionId",
|
|
24
|
+
"pageContextId",
|
|
25
|
+
"eventName",
|
|
26
|
+
"succeeded",
|
|
27
|
+
"error",
|
|
28
|
+
"completed",
|
|
29
|
+
"reason",
|
|
30
|
+
"operation",
|
|
31
|
+
"moduleId",
|
|
32
|
+
"pluginCode",
|
|
33
|
+
"pluginVersion",
|
|
34
|
+
"mounts",
|
|
35
|
+
"dependsOn",
|
|
36
|
+
"capabilities",
|
|
37
|
+
"effectiveLocale",
|
|
38
|
+
"configuration",
|
|
39
|
+
]);
|
|
40
|
+
export class AprilBridgeClient {
|
|
41
|
+
session;
|
|
42
|
+
effectiveLocale;
|
|
43
|
+
coreModuleCommandHandler;
|
|
44
|
+
calls = new Map();
|
|
45
|
+
subscriptions = new Map();
|
|
46
|
+
inbound;
|
|
47
|
+
removeMessageListener;
|
|
48
|
+
closed = false;
|
|
49
|
+
pageSequence = 0;
|
|
50
|
+
lastDeliveredPageSequence = 0;
|
|
51
|
+
shellSequence = 0;
|
|
52
|
+
lastAcknowledgedPageSequence = 0;
|
|
53
|
+
outbound = Promise.resolve();
|
|
54
|
+
coreReadyPending;
|
|
55
|
+
shellCommands = new Map();
|
|
56
|
+
constructor(session, effectiveLocale, inboundQueueCapacity = MAX_INCOMING_QUEUE_MESSAGES, coreModuleCommandHandler) {
|
|
57
|
+
this.session = session;
|
|
58
|
+
this.effectiveLocale = effectiveLocale;
|
|
59
|
+
this.coreModuleCommandHandler = coreModuleCommandHandler;
|
|
60
|
+
this.inbound = new AprilInboundMessageQueue(inboundQueueCapacity, async (message) => this.receive(message), () => {
|
|
61
|
+
void this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.messageQueueOverflow", true));
|
|
62
|
+
}, () => {
|
|
63
|
+
void this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true));
|
|
64
|
+
});
|
|
65
|
+
this.removeMessageListener = this.session.transport.onMessage((message) => {
|
|
66
|
+
if (this.closed) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (!fitsMessageLimit(message)) {
|
|
70
|
+
void this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.payloadTooLarge", false));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (!isInboundBridgeMessage(message)) {
|
|
74
|
+
void this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.sequenceInvalid", false));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
if (!this.isValidInboundMessage(message)) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.inbound.enqueue(message);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
async call(descriptor, payload, signal, connectionRef) {
|
|
84
|
+
return await this.startCall(descriptor, payload, signal, connectionRef, "call");
|
|
85
|
+
}
|
|
86
|
+
async sendShellCommand(operation, configuration) {
|
|
87
|
+
const callId = createCallId();
|
|
88
|
+
return await new Promise((resolve, reject) => {
|
|
89
|
+
const pending = {
|
|
90
|
+
resolve,
|
|
91
|
+
reject,
|
|
92
|
+
timeout: setTimeout(() => {
|
|
93
|
+
if (!this.shellCommands.delete(callId)) {
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
pending.reject(createAprilError(this.effectiveLocale, "timeout", "timeout.operation", true));
|
|
97
|
+
}, SHELL_COMMAND_TIMEOUT_MS),
|
|
98
|
+
};
|
|
99
|
+
this.shellCommands.set(callId, pending);
|
|
100
|
+
void this.send({
|
|
101
|
+
kind: "shellCommand",
|
|
102
|
+
callId,
|
|
103
|
+
operation,
|
|
104
|
+
...(configuration === undefined ? {} : { configuration }),
|
|
105
|
+
})
|
|
106
|
+
.catch((error) => {
|
|
107
|
+
if (!this.shellCommands.delete(callId)) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
clearTimeout(pending.timeout);
|
|
111
|
+
reject(normalizeAprilError(error, createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true)));
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
async ensureCoreReady() {
|
|
116
|
+
if (this.coreReadyPending) {
|
|
117
|
+
return await this.coreReadyPending.promise;
|
|
118
|
+
}
|
|
119
|
+
let resolve;
|
|
120
|
+
let reject;
|
|
121
|
+
const callId = createCallId();
|
|
122
|
+
const correlationId = createCorrelationId();
|
|
123
|
+
const promise = new Promise((nextResolve, nextReject) => {
|
|
124
|
+
resolve = nextResolve;
|
|
125
|
+
reject = nextReject;
|
|
126
|
+
});
|
|
127
|
+
this.coreReadyPending = { callId, correlationId, promise, resolve, reject };
|
|
128
|
+
void this.send({ kind: "coreReady", callId, correlationId }).catch((error) => {
|
|
129
|
+
if (this.coreReadyPending?.callId !== callId) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
this.coreReadyPending = undefined;
|
|
133
|
+
reject(error);
|
|
134
|
+
});
|
|
135
|
+
return await promise;
|
|
136
|
+
}
|
|
137
|
+
async connect(descriptor, payload, signal) {
|
|
138
|
+
return await this.startCall(descriptor, payload, signal, undefined, "connect");
|
|
139
|
+
}
|
|
140
|
+
async startCall(descriptor, payload, signal, connectionRef, invocationKind) {
|
|
141
|
+
ensureJsonValue(payload, this.effectiveLocale);
|
|
142
|
+
const callId = createCallId();
|
|
143
|
+
return await new Promise((resolve, reject) => {
|
|
144
|
+
const pending = {
|
|
145
|
+
resolve,
|
|
146
|
+
reject,
|
|
147
|
+
expectsConnection: invocationKind === "connect",
|
|
148
|
+
removeAbortListener: () => undefined,
|
|
149
|
+
};
|
|
150
|
+
this.calls.set(callId, pending);
|
|
151
|
+
let requestSent = false;
|
|
152
|
+
try {
|
|
153
|
+
if (signal) {
|
|
154
|
+
const abort = () => {
|
|
155
|
+
const tracked = this.calls.delete(callId);
|
|
156
|
+
if (tracked) {
|
|
157
|
+
pending.removeAbortListener();
|
|
158
|
+
if (requestSent) {
|
|
159
|
+
void this.sendCancellation(callId, "pageCanceled");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
reject(createAprilError(this.effectiveLocale, "canceled", "canceled.byPage", false));
|
|
163
|
+
};
|
|
164
|
+
if (signal.aborted) {
|
|
165
|
+
this.calls.delete(callId);
|
|
166
|
+
reject(createAprilError(this.effectiveLocale, "canceled", "canceled.byPage", false));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
pending.removeAbortListener = () => {
|
|
170
|
+
try {
|
|
171
|
+
signal.removeEventListener("abort", abort);
|
|
172
|
+
}
|
|
173
|
+
catch {
|
|
174
|
+
// signal 释放异常不能改写调用终态。
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
178
|
+
if (!this.calls.has(callId)) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
requestSent = true;
|
|
183
|
+
void this.sendRequest("call", callId, descriptor, payload, connectionRef, invocationKind).catch((error) => {
|
|
184
|
+
if (!this.calls.delete(callId)) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
pending.removeAbortListener();
|
|
188
|
+
reject(error);
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
catch (error) {
|
|
192
|
+
if (this.calls.delete(callId)) {
|
|
193
|
+
pending.removeAbortListener();
|
|
194
|
+
reject(normalizeAprilError(error, createAprilError(this.effectiveLocale, "validation", "validation.schemaMismatch", false)));
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
async subscribe(descriptor, payload, observer, signal, connectionRef) {
|
|
200
|
+
ensureJsonValue(payload, this.effectiveLocale);
|
|
201
|
+
const callId = createCallId();
|
|
202
|
+
return await new Promise((resolve, reject) => {
|
|
203
|
+
const pending = {
|
|
204
|
+
descriptor,
|
|
205
|
+
observer,
|
|
206
|
+
resolve,
|
|
207
|
+
reject,
|
|
208
|
+
closed: false,
|
|
209
|
+
accepted: false,
|
|
210
|
+
errorPublished: false,
|
|
211
|
+
unsubscribe: undefined,
|
|
212
|
+
removeAbortListener: () => undefined,
|
|
213
|
+
};
|
|
214
|
+
this.subscriptions.set(callId, pending);
|
|
215
|
+
let requestSent = false;
|
|
216
|
+
try {
|
|
217
|
+
if (signal) {
|
|
218
|
+
const abort = () => {
|
|
219
|
+
this.cancelSubscriptionFromPage(callId, pending, requestSent);
|
|
220
|
+
};
|
|
221
|
+
if (signal.aborted) {
|
|
222
|
+
this.subscriptions.delete(callId);
|
|
223
|
+
reject(createAprilError(this.effectiveLocale, "canceled", "canceled.byPage", false));
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
pending.removeAbortListener = () => {
|
|
227
|
+
try {
|
|
228
|
+
signal.removeEventListener("abort", abort);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
// signal 释放异常不能改写订阅终态。
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
235
|
+
if (!this.subscriptions.has(callId)) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
requestSent = true;
|
|
240
|
+
void this.sendRequest("subscribe", callId, descriptor, payload, connectionRef).catch((error) => {
|
|
241
|
+
if (!this.subscriptions.delete(callId)) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
pending.removeAbortListener();
|
|
245
|
+
reject(error);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
if (this.subscriptions.delete(callId)) {
|
|
250
|
+
pending.removeAbortListener();
|
|
251
|
+
reject(normalizeAprilError(error, createAprilError(this.effectiveLocale, "validation", "validation.schemaMismatch", false)));
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
async dispose(error) {
|
|
257
|
+
await this.close(error ?? createAprilError(this.effectiveLocale, "canceled", "canceled.byPage", true));
|
|
258
|
+
}
|
|
259
|
+
async sendRequest(kind, callId, descriptor, payload, connectionRef, invocationKind) {
|
|
260
|
+
if ((kind === "call") !== (invocationKind !== undefined)) {
|
|
261
|
+
throw createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true);
|
|
262
|
+
}
|
|
263
|
+
await this.send({
|
|
264
|
+
kind,
|
|
265
|
+
callId,
|
|
266
|
+
capability: descriptor.capability,
|
|
267
|
+
action: descriptor.action,
|
|
268
|
+
contractVersion: descriptor.contractVersion,
|
|
269
|
+
payload,
|
|
270
|
+
connectionRef,
|
|
271
|
+
invocationKind,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
async sendCancellation(callId, reason) {
|
|
275
|
+
if (this.closed) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
await this.send({ kind: "cancel", callId, reason });
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
// 关闭链路不能把页面的既有取消结果改写为传输错误。
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
async sendUnsubscribe(callId, reason) {
|
|
286
|
+
await this.send({ kind: "unsubscribe", callId, reason });
|
|
287
|
+
}
|
|
288
|
+
cancelSubscriptionFromPage(callId, pending, requestSent) {
|
|
289
|
+
if (!this.subscriptions.delete(callId)) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
pending.removeAbortListener();
|
|
293
|
+
const error = createAprilError(this.effectiveLocale, "canceled", "canceled.byPage", false);
|
|
294
|
+
if (requestSent) {
|
|
295
|
+
void this.sendCancellation(callId, "pageCanceled");
|
|
296
|
+
}
|
|
297
|
+
if (!pending.accepted) {
|
|
298
|
+
pending.reject(error);
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
pending.closed = true;
|
|
302
|
+
if (!pending.errorPublished) {
|
|
303
|
+
pending.errorPublished = true;
|
|
304
|
+
invokeObserver(() => pending.observer.error?.(error));
|
|
305
|
+
}
|
|
306
|
+
invokeObserver(() => pending.observer.complete?.());
|
|
307
|
+
pending.unsubscribe?.resolve();
|
|
308
|
+
pending.unsubscribe = undefined;
|
|
309
|
+
}
|
|
310
|
+
async send(input) {
|
|
311
|
+
if (this.closed) {
|
|
312
|
+
throw createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true);
|
|
313
|
+
}
|
|
314
|
+
if (this.pageSequence >= MAX_SEQUENCE) {
|
|
315
|
+
const error = createAprilError(this.effectiveLocale, "bridge", "bridge.sequenceExhausted", false);
|
|
316
|
+
await this.close(error);
|
|
317
|
+
throw error;
|
|
318
|
+
}
|
|
319
|
+
const message = {
|
|
320
|
+
protocolVersion: this.session.protocolVersion,
|
|
321
|
+
bridgeId: this.session.bridgeId,
|
|
322
|
+
bridgeToken: this.session.bridgeToken,
|
|
323
|
+
sender: "page",
|
|
324
|
+
pageSequence: ++this.pageSequence,
|
|
325
|
+
shellSequence: this.shellSequence,
|
|
326
|
+
callId: input.callId,
|
|
327
|
+
kind: input.kind,
|
|
328
|
+
...(input.invocationKind === undefined ? {} : { invocationKind: input.invocationKind }),
|
|
329
|
+
correlationId: input.correlationId ?? createCorrelationId(),
|
|
330
|
+
capability: input.capability,
|
|
331
|
+
action: input.action,
|
|
332
|
+
contractVersion: input.contractVersion,
|
|
333
|
+
payload: input.payload,
|
|
334
|
+
...(input.connectionRef === undefined ? {} : { connectionRef: input.connectionRef }),
|
|
335
|
+
...(input.includePageContext === false
|
|
336
|
+
? {}
|
|
337
|
+
: {
|
|
338
|
+
sessionId: this.session.sessionId,
|
|
339
|
+
pageContextId: this.session.pageContextId,
|
|
340
|
+
}),
|
|
341
|
+
reason: input.reason,
|
|
342
|
+
operation: input.operation,
|
|
343
|
+
...(input.configuration === undefined ? {} : { configuration: input.configuration }),
|
|
344
|
+
moduleId: input.moduleId,
|
|
345
|
+
succeeded: input.succeeded,
|
|
346
|
+
error: input.error,
|
|
347
|
+
completed: input.completed,
|
|
348
|
+
};
|
|
349
|
+
const delivery = this.outbound.then(() => this.deliver({
|
|
350
|
+
...message,
|
|
351
|
+
shellSequence: this.shellSequence,
|
|
352
|
+
}));
|
|
353
|
+
this.outbound = delivery.catch(() => undefined);
|
|
354
|
+
await delivery;
|
|
355
|
+
}
|
|
356
|
+
async deliver(message) {
|
|
357
|
+
if (this.closed) {
|
|
358
|
+
throw createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true);
|
|
359
|
+
}
|
|
360
|
+
if (!fitsMessageLimit(message)) {
|
|
361
|
+
const error = createAprilError(this.effectiveLocale, "bridge", "bridge.payloadTooLarge", false);
|
|
362
|
+
await this.close(error);
|
|
363
|
+
throw error;
|
|
364
|
+
}
|
|
365
|
+
this.lastDeliveredPageSequence = message.pageSequence;
|
|
366
|
+
try {
|
|
367
|
+
await this.session.transport.send(message);
|
|
368
|
+
}
|
|
369
|
+
catch {
|
|
370
|
+
const error = createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true);
|
|
371
|
+
await this.close(error);
|
|
372
|
+
throw error;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async receive(message) {
|
|
376
|
+
if (this.closed) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
switch (message.kind) {
|
|
380
|
+
case "coreReadyResult":
|
|
381
|
+
this.completeCoreReady(message);
|
|
382
|
+
return;
|
|
383
|
+
case "coreModuleCommand":
|
|
384
|
+
await this.completeCoreModuleCommand(message);
|
|
385
|
+
return;
|
|
386
|
+
case "shellCommandResult":
|
|
387
|
+
this.completeShellCommand(message);
|
|
388
|
+
return;
|
|
389
|
+
case "accepted":
|
|
390
|
+
this.acceptRequest(message);
|
|
391
|
+
return;
|
|
392
|
+
case "result":
|
|
393
|
+
this.completeResult(message);
|
|
394
|
+
return;
|
|
395
|
+
case "error":
|
|
396
|
+
this.completeError(message);
|
|
397
|
+
return;
|
|
398
|
+
case "event":
|
|
399
|
+
this.publishEvent(message);
|
|
400
|
+
return;
|
|
401
|
+
case "complete":
|
|
402
|
+
this.completeSubscription(message);
|
|
403
|
+
return;
|
|
404
|
+
case "bridgeClosed":
|
|
405
|
+
await this.close(normalizeAprilError(message.error, createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true)), false);
|
|
406
|
+
return;
|
|
407
|
+
default:
|
|
408
|
+
await this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.sequenceInvalid", false));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
isValidInboundMessage(message) {
|
|
412
|
+
const sequenceIsValid = Number.isSafeInteger(message.shellSequence) &&
|
|
413
|
+
message.shellSequence === this.shellSequence + 1 &&
|
|
414
|
+
Number.isSafeInteger(message.pageSequence) &&
|
|
415
|
+
message.pageSequence >= this.lastAcknowledgedPageSequence &&
|
|
416
|
+
message.pageSequence <= this.lastDeliveredPageSequence;
|
|
417
|
+
if (message.sender !== "shell" ||
|
|
418
|
+
message.bridgeId !== this.session.bridgeId ||
|
|
419
|
+
message.protocolVersion !== this.session.protocolVersion ||
|
|
420
|
+
!sequenceIsValid) {
|
|
421
|
+
void this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.sequenceInvalid", false));
|
|
422
|
+
return false;
|
|
423
|
+
}
|
|
424
|
+
this.shellSequence = message.shellSequence;
|
|
425
|
+
this.lastAcknowledgedPageSequence = message.pageSequence;
|
|
426
|
+
return true;
|
|
427
|
+
}
|
|
428
|
+
acceptRequest(message) {
|
|
429
|
+
const callId = message.callId;
|
|
430
|
+
if (message.requestKind === "call") {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const pending = this.subscriptions.get(callId);
|
|
434
|
+
if (!pending) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (pending.closed || pending.accepted) {
|
|
438
|
+
this.closeInvalidInbound();
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
pending.accepted = true;
|
|
442
|
+
pending.resolve(this.createSubscription(callId, pending));
|
|
443
|
+
}
|
|
444
|
+
completeCoreReady(message) {
|
|
445
|
+
const pending = this.coreReadyPending;
|
|
446
|
+
if (!pending || message.callId !== pending.callId || message.correlationId !== pending.correlationId) {
|
|
447
|
+
this.closeInvalidInbound();
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
this.coreReadyPending = undefined;
|
|
451
|
+
if (message.succeeded === true) {
|
|
452
|
+
pending.resolve();
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
pending.reject(normalizeAprilError(message.error, createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true)));
|
|
456
|
+
}
|
|
457
|
+
async completeCoreModuleCommand(message) {
|
|
458
|
+
const command = readCoreModuleCommand(message);
|
|
459
|
+
if (!command || !this.coreModuleCommandHandler || !isNonEmptyString(message.callId) ||
|
|
460
|
+
!isNonEmptyString(message.correlationId)) {
|
|
461
|
+
await this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.sequenceInvalid", false));
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
let result;
|
|
465
|
+
try {
|
|
466
|
+
result = await this.coreModuleCommandHandler(command);
|
|
467
|
+
}
|
|
468
|
+
catch (error) {
|
|
469
|
+
result = {
|
|
470
|
+
operation: command.operation,
|
|
471
|
+
succeeded: false,
|
|
472
|
+
error: normalizeAprilError(error, createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true)),
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
if (!isCoreModuleCommandResult(result, command.operation)) {
|
|
476
|
+
await this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.sequenceInvalid", false));
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
await this.send({
|
|
480
|
+
kind: "coreModuleResult",
|
|
481
|
+
callId: message.callId,
|
|
482
|
+
correlationId: message.correlationId,
|
|
483
|
+
operation: result.operation,
|
|
484
|
+
succeeded: result.succeeded,
|
|
485
|
+
error: result.error,
|
|
486
|
+
moduleId: result.moduleId,
|
|
487
|
+
completed: true,
|
|
488
|
+
includePageContext: false,
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
completeShellCommand(message) {
|
|
492
|
+
if (!isNonEmptyString(message.callId) ||
|
|
493
|
+
!isNonEmptyString(message.correlationId) ||
|
|
494
|
+
message.completed !== true ||
|
|
495
|
+
typeof message.succeeded !== "boolean") {
|
|
496
|
+
this.closeInvalidInbound();
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
const pending = this.shellCommands.get(message.callId);
|
|
500
|
+
if (!pending) {
|
|
501
|
+
return;
|
|
502
|
+
}
|
|
503
|
+
this.shellCommands.delete(message.callId);
|
|
504
|
+
clearTimeout(pending.timeout);
|
|
505
|
+
if (message.succeeded) {
|
|
506
|
+
if (message.error !== undefined) {
|
|
507
|
+
this.closeInvalidInbound();
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
pending.resolve();
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
pending.reject(normalizeAprilError(message.error, createAprilError(this.effectiveLocale, "unknown", "unknown.failure", false)));
|
|
514
|
+
}
|
|
515
|
+
completeResult(message) {
|
|
516
|
+
const callId = message.callId;
|
|
517
|
+
if (message.requestKind === "call") {
|
|
518
|
+
const call = this.calls.get(callId);
|
|
519
|
+
if (!call) {
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (call.expectsConnection) {
|
|
523
|
+
if (!isNonEmptyString(message.connectionRef)) {
|
|
524
|
+
this.closeInvalidInbound();
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
this.calls.delete(callId);
|
|
528
|
+
call.removeAbortListener();
|
|
529
|
+
call.resolve(Object.freeze({
|
|
530
|
+
value: message.payload,
|
|
531
|
+
connectionRef: message.connectionRef,
|
|
532
|
+
}));
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
if (message.connectionRef !== undefined) {
|
|
536
|
+
this.closeInvalidInbound();
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
this.calls.delete(callId);
|
|
540
|
+
call.removeAbortListener();
|
|
541
|
+
call.resolve(message.payload);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
if (message.requestKind === "cancel") {
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
const subscription = this.subscriptions.get(callId);
|
|
548
|
+
if (!subscription) {
|
|
549
|
+
return;
|
|
550
|
+
}
|
|
551
|
+
if (!subscription.unsubscribe) {
|
|
552
|
+
this.closeInvalidInbound();
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
this.finishSubscription(callId, subscription, undefined, false);
|
|
556
|
+
}
|
|
557
|
+
completeError(message) {
|
|
558
|
+
const callId = message.callId;
|
|
559
|
+
const error = normalizeAprilError(message.error, createAprilError(this.effectiveLocale, "unknown", "unknown.failure", false));
|
|
560
|
+
if (message.requestKind === "call") {
|
|
561
|
+
const call = this.calls.get(callId);
|
|
562
|
+
if (!call) {
|
|
563
|
+
return;
|
|
564
|
+
}
|
|
565
|
+
this.calls.delete(callId);
|
|
566
|
+
call.removeAbortListener();
|
|
567
|
+
call.reject(error);
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if (message.requestKind === "cancel") {
|
|
571
|
+
return;
|
|
572
|
+
}
|
|
573
|
+
const subscription = this.subscriptions.get(callId);
|
|
574
|
+
if (!subscription) {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
if (message.requestKind === "unsubscribe") {
|
|
578
|
+
if (!subscription.unsubscribe) {
|
|
579
|
+
this.closeInvalidInbound();
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
subscription.unsubscribe.reject(error);
|
|
583
|
+
subscription.unsubscribe = undefined;
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
if (subscription.errorPublished) {
|
|
587
|
+
this.closeInvalidInbound();
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
if (!subscription.accepted) {
|
|
591
|
+
this.subscriptions.delete(callId);
|
|
592
|
+
subscription.removeAbortListener();
|
|
593
|
+
subscription.reject(error);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
subscription.errorPublished = true;
|
|
597
|
+
subscription.closed = true;
|
|
598
|
+
invokeObserver(() => subscription.observer.error?.(error));
|
|
599
|
+
}
|
|
600
|
+
publishEvent(message) {
|
|
601
|
+
const callId = message.callId;
|
|
602
|
+
const subscription = this.subscriptions.get(callId);
|
|
603
|
+
if (!subscription) {
|
|
604
|
+
return;
|
|
605
|
+
}
|
|
606
|
+
if (!subscription.accepted || subscription.closed || subscription.errorPublished) {
|
|
607
|
+
this.closeInvalidInbound();
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
invokeObserver(() => subscription.observer.next(message.payload));
|
|
611
|
+
}
|
|
612
|
+
completeSubscription(message) {
|
|
613
|
+
const callId = message.callId;
|
|
614
|
+
const subscription = this.subscriptions.get(callId);
|
|
615
|
+
if (!subscription) {
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (!subscription.accepted) {
|
|
619
|
+
this.closeInvalidInbound();
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
this.finishSubscription(callId, subscription, message.reason);
|
|
623
|
+
}
|
|
624
|
+
closeInvalidInbound() {
|
|
625
|
+
void this.close(createAprilError(this.effectiveLocale, "bridge", "bridge.sequenceInvalid", false));
|
|
626
|
+
}
|
|
627
|
+
createSubscription(callId, pending) {
|
|
628
|
+
return Object.freeze({
|
|
629
|
+
id: callId,
|
|
630
|
+
capability: pending.descriptor.capability,
|
|
631
|
+
action: pending.descriptor.action,
|
|
632
|
+
contractVersion: pending.descriptor.contractVersion,
|
|
633
|
+
get closed() {
|
|
634
|
+
return pending.closed;
|
|
635
|
+
},
|
|
636
|
+
unsubscribe: async (reason) => {
|
|
637
|
+
if (reason !== undefined && (typeof reason !== "string" || reason.trim().length === 0)) {
|
|
638
|
+
throw createAprilError(this.effectiveLocale, "validation", "validation.schemaMismatch", false);
|
|
639
|
+
}
|
|
640
|
+
if (pending.closed) {
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
if (pending.unsubscribe) {
|
|
644
|
+
await pending.unsubscribe.promise;
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
let resolve;
|
|
648
|
+
let reject;
|
|
649
|
+
const promise = new Promise((nextResolve, nextReject) => {
|
|
650
|
+
resolve = nextResolve;
|
|
651
|
+
reject = nextReject;
|
|
652
|
+
});
|
|
653
|
+
const unsubscribe = { promise, resolve, reject };
|
|
654
|
+
pending.unsubscribe = unsubscribe;
|
|
655
|
+
void this.sendUnsubscribe(callId, reason).catch((error) => {
|
|
656
|
+
if (pending.unsubscribe !== unsubscribe) {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
pending.unsubscribe = undefined;
|
|
660
|
+
unsubscribe.reject(normalizeAprilError(error, createAprilError(this.effectiveLocale, "bridge", "bridge.notReady", true)));
|
|
661
|
+
});
|
|
662
|
+
await promise;
|
|
663
|
+
},
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
finishSubscription(callId, pending, reason, publishComplete = true) {
|
|
667
|
+
if (!this.subscriptions.delete(callId)) {
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
pending.closed = true;
|
|
671
|
+
pending.removeAbortListener();
|
|
672
|
+
pending.unsubscribe?.resolve();
|
|
673
|
+
pending.unsubscribe = undefined;
|
|
674
|
+
if (publishComplete) {
|
|
675
|
+
invokeObserver(() => pending.observer.complete?.(reason));
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
async close(error, notifyTransport = true) {
|
|
679
|
+
if (this.closed) {
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
this.closed = true;
|
|
683
|
+
this.inbound.close();
|
|
684
|
+
try {
|
|
685
|
+
this.removeMessageListener();
|
|
686
|
+
}
|
|
687
|
+
catch {
|
|
688
|
+
// 监听器清理失败不能阻止本地等待项进入终态。
|
|
689
|
+
}
|
|
690
|
+
for (const [callId, pending] of this.calls) {
|
|
691
|
+
this.calls.delete(callId);
|
|
692
|
+
pending.removeAbortListener();
|
|
693
|
+
pending.reject(error);
|
|
694
|
+
}
|
|
695
|
+
for (const [callId, pending] of this.shellCommands) {
|
|
696
|
+
this.shellCommands.delete(callId);
|
|
697
|
+
clearTimeout(pending.timeout);
|
|
698
|
+
pending.reject(error);
|
|
699
|
+
}
|
|
700
|
+
if (this.coreReadyPending) {
|
|
701
|
+
const pending = this.coreReadyPending;
|
|
702
|
+
this.coreReadyPending = undefined;
|
|
703
|
+
pending.reject(error);
|
|
704
|
+
}
|
|
705
|
+
for (const [callId, pending] of this.subscriptions) {
|
|
706
|
+
this.subscriptions.delete(callId);
|
|
707
|
+
pending.removeAbortListener();
|
|
708
|
+
pending.closed = true;
|
|
709
|
+
if (!pending.accepted) {
|
|
710
|
+
pending.reject(error);
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
713
|
+
if (!pending.errorPublished) {
|
|
714
|
+
pending.errorPublished = true;
|
|
715
|
+
invokeObserver(() => pending.observer.error?.(error));
|
|
716
|
+
}
|
|
717
|
+
invokeObserver(() => pending.observer.complete?.());
|
|
718
|
+
pending.unsubscribe?.reject(error);
|
|
719
|
+
pending.unsubscribe = undefined;
|
|
720
|
+
}
|
|
721
|
+
if (notifyTransport) {
|
|
722
|
+
try {
|
|
723
|
+
await this.session.transport.close(error);
|
|
724
|
+
}
|
|
725
|
+
catch {
|
|
726
|
+
// Bridge 已经在本地原子关闭,传输释放失败不能改写公开终态。
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
function createCallId() {
|
|
732
|
+
return globalThis.crypto?.randomUUID?.() ?? `call-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
733
|
+
}
|
|
734
|
+
function createCorrelationId() {
|
|
735
|
+
return globalThis.crypto?.randomUUID?.() ?? `corr-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
736
|
+
}
|
|
737
|
+
function fitsMessageLimit(message) {
|
|
738
|
+
try {
|
|
739
|
+
return new TextEncoder().encode(JSON.stringify(message)).byteLength <= MAX_WIRE_MESSAGE_BYTES;
|
|
740
|
+
}
|
|
741
|
+
catch {
|
|
742
|
+
return false;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
function isInboundBridgeMessage(value) {
|
|
746
|
+
try {
|
|
747
|
+
if (!isPlainObject(value) || !Object.keys(value).every((key) => inboundMessageKeys.has(key))) {
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
const message = value;
|
|
751
|
+
if (!isNonEmptyString(message.protocolVersion) ||
|
|
752
|
+
!isNonEmptyString(message.bridgeId) ||
|
|
753
|
+
message.sender !== "shell" ||
|
|
754
|
+
!isSequence(message.pageSequence) ||
|
|
755
|
+
!isSequence(message.shellSequence) ||
|
|
756
|
+
(message.correlationId !== undefined && !isNonEmptyString(message.correlationId)) ||
|
|
757
|
+
message.bridgeToken !== undefined ||
|
|
758
|
+
message.capability !== undefined ||
|
|
759
|
+
message.action !== undefined ||
|
|
760
|
+
message.contractVersion !== undefined ||
|
|
761
|
+
message.invocationKind !== undefined ||
|
|
762
|
+
message.sessionId !== undefined ||
|
|
763
|
+
message.pageContextId !== undefined) {
|
|
764
|
+
return false;
|
|
765
|
+
}
|
|
766
|
+
switch (message.kind) {
|
|
767
|
+
case "coreReadyResult":
|
|
768
|
+
return hasCallId(message) &&
|
|
769
|
+
message.requestKind === undefined &&
|
|
770
|
+
(message.succeeded === true || message.succeeded === false) &&
|
|
771
|
+
message.completed === true &&
|
|
772
|
+
message.payload === undefined &&
|
|
773
|
+
message.connectionRef === undefined &&
|
|
774
|
+
message.eventName === undefined &&
|
|
775
|
+
message.reason === undefined &&
|
|
776
|
+
(message.succeeded ? message.error === undefined : isAprilError(message.error));
|
|
777
|
+
case "coreModuleCommand":
|
|
778
|
+
return hasCallId(message) &&
|
|
779
|
+
isNonEmptyString(message.correlationId) &&
|
|
780
|
+
message.requestKind === undefined &&
|
|
781
|
+
message.succeeded === undefined &&
|
|
782
|
+
message.completed === undefined &&
|
|
783
|
+
message.payload === undefined &&
|
|
784
|
+
message.connectionRef === undefined &&
|
|
785
|
+
message.eventName === undefined &&
|
|
786
|
+
message.error === undefined &&
|
|
787
|
+
message.reason === undefined &&
|
|
788
|
+
readCoreModuleCommand(message) !== undefined;
|
|
789
|
+
case "shellCommandResult":
|
|
790
|
+
return hasCallId(message) &&
|
|
791
|
+
isNonEmptyString(message.correlationId) &&
|
|
792
|
+
message.requestKind === undefined &&
|
|
793
|
+
(message.succeeded === true || message.succeeded === false) &&
|
|
794
|
+
message.completed === true &&
|
|
795
|
+
message.payload === undefined &&
|
|
796
|
+
message.connectionRef === undefined &&
|
|
797
|
+
message.eventName === undefined &&
|
|
798
|
+
message.reason === undefined &&
|
|
799
|
+
(message.succeeded ? message.error === undefined : isAprilError(message.error));
|
|
800
|
+
case "accepted":
|
|
801
|
+
return hasCallId(message) &&
|
|
802
|
+
(message.requestKind === "call" || message.requestKind === "subscribe") &&
|
|
803
|
+
message.succeeded === true &&
|
|
804
|
+
message.completed === false &&
|
|
805
|
+
message.payload === undefined &&
|
|
806
|
+
message.connectionRef === undefined &&
|
|
807
|
+
message.eventName === undefined &&
|
|
808
|
+
message.error === undefined &&
|
|
809
|
+
message.reason === undefined;
|
|
810
|
+
case "result":
|
|
811
|
+
return hasCallId(message) &&
|
|
812
|
+
(message.requestKind === "call" ||
|
|
813
|
+
message.requestKind === "unsubscribe" ||
|
|
814
|
+
message.requestKind === "cancel") &&
|
|
815
|
+
message.succeeded === true &&
|
|
816
|
+
message.completed === true &&
|
|
817
|
+
message.eventName === undefined &&
|
|
818
|
+
message.error === undefined &&
|
|
819
|
+
message.reason === undefined &&
|
|
820
|
+
(message.requestKind === "call"
|
|
821
|
+
? Object.hasOwn(message, "payload") &&
|
|
822
|
+
isJsonValue(message.payload) &&
|
|
823
|
+
(message.connectionRef === undefined || isNonEmptyString(message.connectionRef))
|
|
824
|
+
: message.payload === undefined && message.connectionRef === undefined);
|
|
825
|
+
case "error":
|
|
826
|
+
return hasCallId(message) &&
|
|
827
|
+
(message.requestKind === "call" ||
|
|
828
|
+
message.requestKind === "subscribe" ||
|
|
829
|
+
message.requestKind === "unsubscribe" ||
|
|
830
|
+
message.requestKind === "cancel") &&
|
|
831
|
+
message.succeeded === false &&
|
|
832
|
+
message.completed === true &&
|
|
833
|
+
isAprilError(message.error) &&
|
|
834
|
+
message.payload === undefined &&
|
|
835
|
+
message.connectionRef === undefined &&
|
|
836
|
+
message.eventName === undefined &&
|
|
837
|
+
message.reason === undefined;
|
|
838
|
+
case "event":
|
|
839
|
+
return hasCallId(message) &&
|
|
840
|
+
message.requestKind === undefined &&
|
|
841
|
+
message.succeeded === undefined &&
|
|
842
|
+
message.completed === false &&
|
|
843
|
+
isNonEmptyString(message.eventName) &&
|
|
844
|
+
Object.hasOwn(message, "payload") &&
|
|
845
|
+
isJsonValue(message.payload) &&
|
|
846
|
+
message.connectionRef === undefined &&
|
|
847
|
+
message.error === undefined &&
|
|
848
|
+
message.reason === undefined;
|
|
849
|
+
case "complete":
|
|
850
|
+
return hasCallId(message) &&
|
|
851
|
+
message.requestKind === undefined &&
|
|
852
|
+
message.succeeded === undefined &&
|
|
853
|
+
message.completed === true &&
|
|
854
|
+
message.payload === undefined &&
|
|
855
|
+
message.connectionRef === undefined &&
|
|
856
|
+
message.eventName === undefined &&
|
|
857
|
+
message.error === undefined &&
|
|
858
|
+
(message.reason === undefined || isNonEmptyString(message.reason));
|
|
859
|
+
case "bridgeClosed":
|
|
860
|
+
return message.callId === undefined &&
|
|
861
|
+
message.requestKind === undefined &&
|
|
862
|
+
message.succeeded === undefined &&
|
|
863
|
+
message.completed === true &&
|
|
864
|
+
message.payload === undefined &&
|
|
865
|
+
message.connectionRef === undefined &&
|
|
866
|
+
message.eventName === undefined &&
|
|
867
|
+
isAprilError(message.error) &&
|
|
868
|
+
message.reason === undefined;
|
|
869
|
+
default:
|
|
870
|
+
return false;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
catch {
|
|
874
|
+
return false;
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
function hasCallId(message) {
|
|
878
|
+
return isNonEmptyString(message.callId);
|
|
879
|
+
}
|
|
880
|
+
function readCoreModuleCommand(message) {
|
|
881
|
+
const operation = message.operation;
|
|
882
|
+
if (!isCoreModuleOperation(operation) ||
|
|
883
|
+
message.moduleId !== undefined && !isNonEmptyString(message.moduleId) ||
|
|
884
|
+
message.pluginCode !== undefined && !isNonEmptyString(message.pluginCode) ||
|
|
885
|
+
message.pluginVersion !== undefined && !isNonEmptyString(message.pluginVersion) ||
|
|
886
|
+
message.effectiveLocale !== undefined &&
|
|
887
|
+
message.effectiveLocale !== "zh-CN" && message.effectiveLocale !== "en-US" ||
|
|
888
|
+
message.mounts !== undefined && !isMountArray(message.mounts) ||
|
|
889
|
+
message.dependsOn !== undefined && !isStringArray(message.dependsOn) ||
|
|
890
|
+
message.capabilities !== undefined && !isCapabilityArray(message.capabilities)) {
|
|
891
|
+
return undefined;
|
|
892
|
+
}
|
|
893
|
+
const command = {
|
|
894
|
+
operation,
|
|
895
|
+
...(message.moduleId === undefined ? {} : { moduleId: message.moduleId }),
|
|
896
|
+
...(message.pluginCode === undefined ? {} : { pluginCode: message.pluginCode }),
|
|
897
|
+
...(message.pluginVersion === undefined ? {} : { pluginVersion: message.pluginVersion }),
|
|
898
|
+
...(message.mounts === undefined ? {} : { mounts: Object.freeze([...message.mounts]) }),
|
|
899
|
+
...(message.dependsOn === undefined ? {} : { dependsOn: Object.freeze([...message.dependsOn]) }),
|
|
900
|
+
...(message.capabilities === undefined ? {} : { capabilities: freezeCoreModuleCapabilities(message.capabilities) }),
|
|
901
|
+
...(message.effectiveLocale === undefined ? {} : { effectiveLocale: message.effectiveLocale }),
|
|
902
|
+
};
|
|
903
|
+
return isValidCoreModuleCommand(command) ? Object.freeze(command) : undefined;
|
|
904
|
+
}
|
|
905
|
+
function isCoreModuleCommandResult(result, operation) {
|
|
906
|
+
return isRecord(result) && result.operation === operation &&
|
|
907
|
+
typeof result.succeeded === "boolean" &&
|
|
908
|
+
(result.error === undefined || isAprilError(result.error)) &&
|
|
909
|
+
(result.succeeded ? result.error === undefined : result.error !== undefined) &&
|
|
910
|
+
(result.moduleId === undefined || isNonEmptyString(result.moduleId)) &&
|
|
911
|
+
(operation === "inspectRegistry"
|
|
912
|
+
? result.succeeded === (result.moduleId !== undefined)
|
|
913
|
+
: result.moduleId === undefined);
|
|
914
|
+
}
|
|
915
|
+
function isValidCoreModuleCommand(command) {
|
|
916
|
+
switch (command.operation) {
|
|
917
|
+
case "openRegistry":
|
|
918
|
+
case "closeRegistry":
|
|
919
|
+
case "abortInstall":
|
|
920
|
+
return isNonEmptyString(command.moduleId) && hasNoCoreModuleCommandFields(command, ["moduleId"]);
|
|
921
|
+
case "inspectRegistry":
|
|
922
|
+
return isNonEmptyString(command.moduleId) && hasNoCoreModuleCommandFields(command, ["moduleId"]);
|
|
923
|
+
case "install":
|
|
924
|
+
return isNonEmptyString(command.moduleId) &&
|
|
925
|
+
isNonEmptyString(command.pluginCode) &&
|
|
926
|
+
isNonEmptyString(command.pluginVersion) &&
|
|
927
|
+
isMountArray(command.mounts) &&
|
|
928
|
+
isStringArray(command.dependsOn) &&
|
|
929
|
+
isCapabilityArray(command.capabilities) &&
|
|
930
|
+
(command.effectiveLocale === "zh-CN" || command.effectiveLocale === "en-US");
|
|
931
|
+
case "openFinalization":
|
|
932
|
+
case "finalize":
|
|
933
|
+
case "abortFinalization":
|
|
934
|
+
case "dispose":
|
|
935
|
+
return hasNoCoreModuleCommandFields(command, []);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
function hasNoCoreModuleCommandFields(command, allowed) {
|
|
939
|
+
return Object.keys(command).every((key) => key === "operation" || allowed.includes(key));
|
|
940
|
+
}
|
|
941
|
+
function isCoreModuleOperation(value) {
|
|
942
|
+
return value === "openRegistry" || value === "inspectRegistry" || value === "closeRegistry" ||
|
|
943
|
+
value === "install" || value === "abortInstall" || value === "openFinalization" ||
|
|
944
|
+
value === "finalize" || value === "abortFinalization" || value === "dispose";
|
|
945
|
+
}
|
|
946
|
+
function isStringArray(value) {
|
|
947
|
+
return Array.isArray(value) && value.every(isNonEmptyString);
|
|
948
|
+
}
|
|
949
|
+
function isMountArray(value) {
|
|
950
|
+
return Array.isArray(value) && value.every((mount) => isRecord(mount) &&
|
|
951
|
+
mount.target === "april" &&
|
|
952
|
+
isNonEmptyString(mount.path) &&
|
|
953
|
+
Object.keys(mount).every((key) => key === "target" || key === "path"));
|
|
954
|
+
}
|
|
955
|
+
function isCapabilityArray(value) {
|
|
956
|
+
return Array.isArray(value) && value.every(isCapabilityReference);
|
|
957
|
+
}
|
|
958
|
+
function isCapabilityReference(value) {
|
|
959
|
+
if (!isRecord(value) ||
|
|
960
|
+
!isNonEmptyString(value.capability) ||
|
|
961
|
+
!isNonEmptyString(value.action) ||
|
|
962
|
+
!isNonEmptyString(value.contractVersion) ||
|
|
963
|
+
Object.keys(value).some((key) => !["capability", "action", "contractVersion"].includes(key))) {
|
|
964
|
+
return false;
|
|
965
|
+
}
|
|
966
|
+
return true;
|
|
967
|
+
}
|
|
968
|
+
function freezeCoreModuleCapabilities(capabilities) {
|
|
969
|
+
return Object.freeze(capabilities.map((capability) => Object.freeze({
|
|
970
|
+
capability: capability.capability,
|
|
971
|
+
action: capability.action,
|
|
972
|
+
contractVersion: capability.contractVersion,
|
|
973
|
+
})));
|
|
974
|
+
}
|
|
975
|
+
function isSequence(value) {
|
|
976
|
+
return Number.isSafeInteger(value) && value >= 0 && value <= MAX_SEQUENCE;
|
|
977
|
+
}
|
|
978
|
+
function isNonEmptyString(value) {
|
|
979
|
+
return typeof value === "string" && value.trim().length > 0;
|
|
980
|
+
}
|
|
981
|
+
function ensureJsonValue(value, locale) {
|
|
982
|
+
let valid = false;
|
|
983
|
+
try {
|
|
984
|
+
valid = isJsonValue(value);
|
|
985
|
+
}
|
|
986
|
+
catch {
|
|
987
|
+
valid = false;
|
|
988
|
+
}
|
|
989
|
+
if (!valid) {
|
|
990
|
+
throw createAprilError(locale, "validation", "validation.schemaMismatch", false);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
function isJsonValue(value, ancestors = new Set()) {
|
|
994
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") {
|
|
995
|
+
return true;
|
|
996
|
+
}
|
|
997
|
+
if (typeof value === "number") {
|
|
998
|
+
return Number.isFinite(value);
|
|
999
|
+
}
|
|
1000
|
+
if (Array.isArray(value)) {
|
|
1001
|
+
if (ancestors.has(value)) {
|
|
1002
|
+
return false;
|
|
1003
|
+
}
|
|
1004
|
+
ancestors.add(value);
|
|
1005
|
+
for (const item of value) {
|
|
1006
|
+
if (!isJsonValue(item, ancestors)) {
|
|
1007
|
+
return false;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
ancestors.delete(value);
|
|
1011
|
+
return true;
|
|
1012
|
+
}
|
|
1013
|
+
if (isPlainObject(value)) {
|
|
1014
|
+
if (ancestors.has(value)) {
|
|
1015
|
+
return false;
|
|
1016
|
+
}
|
|
1017
|
+
ancestors.add(value);
|
|
1018
|
+
for (const item of Object.values(value)) {
|
|
1019
|
+
if (!isJsonValue(item, ancestors)) {
|
|
1020
|
+
return false;
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
ancestors.delete(value);
|
|
1024
|
+
return true;
|
|
1025
|
+
}
|
|
1026
|
+
return false;
|
|
1027
|
+
}
|
|
1028
|
+
function isPlainObject(value) {
|
|
1029
|
+
if (typeof value !== "object" || value === null) {
|
|
1030
|
+
return false;
|
|
1031
|
+
}
|
|
1032
|
+
const prototype = Object.getPrototypeOf(value);
|
|
1033
|
+
return prototype === Object.prototype || prototype === null;
|
|
1034
|
+
}
|
|
1035
|
+
function invokeObserver(callback) {
|
|
1036
|
+
try {
|
|
1037
|
+
callback();
|
|
1038
|
+
}
|
|
1039
|
+
catch {
|
|
1040
|
+
// 页面观察者异常不能影响桥接调用表和后续队列。
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
function isRecord(value) {
|
|
1044
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1045
|
+
}
|
|
1046
|
+
//# sourceMappingURL=bridge.js.map
|