@open-jsbridge/web 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/LICENSE +21 -0
- package/README.md +50 -2
- package/package.json +25 -4
- package/packages/protocol/src/index.d.ts +67 -0
- package/packages/protocol/src/index.js +109 -0
- package/packages/web-mock/src/index.d.ts +31 -0
- package/packages/web-mock/src/index.js +305 -0
- package/packages/web-sdk/src/errors.d.ts +14 -0
- package/packages/web-sdk/src/errors.js +10 -0
- package/packages/web-sdk/src/events.d.ts +32 -0
- package/packages/web-sdk/src/events.js +219 -0
- package/packages/web-sdk/src/index.d.ts +39 -0
- package/packages/web-sdk/src/index.js +466 -0
- package/packages/web-sdk/src/session.d.ts +21 -0
- package/packages/web-sdk/src/session.js +54 -0
- package/packages/web-sdk/src/transport.d.ts +20 -0
- package/packages/web-sdk/src/transport.js +57 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { ErrorCode } from '../../protocol/src/index.js';
|
|
2
|
+
import { BridgeError } from './errors.js';
|
|
3
|
+
import { isObject } from './session.js';
|
|
4
|
+
const eventNamePattern = /^(?!rpc\.)[a-z][a-zA-Z0-9]*(?:\.[a-z][a-zA-Z0-9]*)+$/;
|
|
5
|
+
function isEventName(name) {
|
|
6
|
+
return typeof name === 'string' && eventNamePattern.test(name);
|
|
7
|
+
}
|
|
8
|
+
export function hasSessionEvent(session, name) {
|
|
9
|
+
return isEventName(name) && session?.events.has(name) === true;
|
|
10
|
+
}
|
|
11
|
+
function register(dispose) {
|
|
12
|
+
let resolve;
|
|
13
|
+
let reject;
|
|
14
|
+
const ready = new Promise((accept, fail) => { resolve = accept; reject = fail; });
|
|
15
|
+
void ready.catch(() => { }); // Callback-only callers can ignore readiness.
|
|
16
|
+
const disposer = Object.defineProperty(dispose, 'ready', { value: ready });
|
|
17
|
+
return { disposer, acknowledged: false, resolve, reject };
|
|
18
|
+
}
|
|
19
|
+
function subscribeError(result, name) {
|
|
20
|
+
const context = { method: 'bridge.subscribe', data: { event: name } };
|
|
21
|
+
if (isObject(result) && Array.isArray(result.subscribed) && Array.isArray(result.denied)) {
|
|
22
|
+
const { subscribed, denied } = result;
|
|
23
|
+
if (subscribed.length === 1 && subscribed[0] === name && denied.length === 0)
|
|
24
|
+
return;
|
|
25
|
+
if (denied.length === 1 && denied[0] === name && subscribed.length === 0) {
|
|
26
|
+
return new BridgeError(ErrorCode.PermissionDenied, 'Event subscription denied', context);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return new BridgeError(ErrorCode.InvalidRequest, 'Invalid event subscription acknowledgement', context);
|
|
30
|
+
}
|
|
31
|
+
export class EventSubscriptions {
|
|
32
|
+
constructor(request, connect) {
|
|
33
|
+
this.request = request;
|
|
34
|
+
this.connect = connect;
|
|
35
|
+
this.subscriptions = new Map();
|
|
36
|
+
}
|
|
37
|
+
on(name, listener) {
|
|
38
|
+
if (!isEventName(name) || typeof listener !== 'function') {
|
|
39
|
+
const invalid = register(() => { });
|
|
40
|
+
invalid.reject(new BridgeError(ErrorCode.InvalidParams, 'Expected a qualified event name and a listener'));
|
|
41
|
+
return invalid.disposer;
|
|
42
|
+
}
|
|
43
|
+
const callback = listener;
|
|
44
|
+
let subscription = this.subscriptions.get(name);
|
|
45
|
+
if (!subscription) {
|
|
46
|
+
subscription = { listeners: new Map(), phase: 'idle' };
|
|
47
|
+
this.subscriptions.set(name, subscription);
|
|
48
|
+
}
|
|
49
|
+
const { listeners } = subscription;
|
|
50
|
+
let registration = listeners.get(callback);
|
|
51
|
+
if (!registration) {
|
|
52
|
+
registration = register(() => {
|
|
53
|
+
if (listeners.get(callback) === registration)
|
|
54
|
+
this.off(name, callback);
|
|
55
|
+
});
|
|
56
|
+
listeners.set(callback, registration);
|
|
57
|
+
}
|
|
58
|
+
if (this.session)
|
|
59
|
+
this.reconcile(name, subscription);
|
|
60
|
+
else
|
|
61
|
+
this.connect();
|
|
62
|
+
return registration.disposer;
|
|
63
|
+
}
|
|
64
|
+
off(name, listener) {
|
|
65
|
+
const subscription = this.subscriptions.get(name);
|
|
66
|
+
if (!subscription)
|
|
67
|
+
return;
|
|
68
|
+
const { listeners } = subscription;
|
|
69
|
+
const remove = (callback) => {
|
|
70
|
+
const registration = listeners.get(callback);
|
|
71
|
+
if (!registration)
|
|
72
|
+
return;
|
|
73
|
+
listeners.delete(callback);
|
|
74
|
+
if (!registration.acknowledged) {
|
|
75
|
+
registration.reject(new BridgeError(ErrorCode.Cancelled, 'Event subscription cancelled'));
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
if (listener === undefined) {
|
|
79
|
+
for (const callback of listeners.keys())
|
|
80
|
+
remove(callback);
|
|
81
|
+
}
|
|
82
|
+
else
|
|
83
|
+
remove(listener);
|
|
84
|
+
this.reconcile(name, subscription);
|
|
85
|
+
}
|
|
86
|
+
start(session) {
|
|
87
|
+
this.session = session;
|
|
88
|
+
for (const [name, subscription] of this.subscriptions)
|
|
89
|
+
this.reconcile(name, subscription);
|
|
90
|
+
}
|
|
91
|
+
reset() {
|
|
92
|
+
this.session = undefined;
|
|
93
|
+
for (const [name, subscription] of this.subscriptions) {
|
|
94
|
+
subscription.phase = 'idle';
|
|
95
|
+
if (subscription.listeners.size === 0)
|
|
96
|
+
this.subscriptions.delete(name);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
rejectWaiting(error) {
|
|
100
|
+
for (const [name, subscription] of this.subscriptions)
|
|
101
|
+
this.rejectListeners(name, subscription, error);
|
|
102
|
+
}
|
|
103
|
+
deliver(params) {
|
|
104
|
+
if (!params || !isEventName(params.name) || !Object.prototype.hasOwnProperty.call(params, 'payload'))
|
|
105
|
+
return;
|
|
106
|
+
const subscription = this.subscriptions.get(params.name);
|
|
107
|
+
if (subscription?.phase !== 'subscribed')
|
|
108
|
+
return;
|
|
109
|
+
const { session } = this;
|
|
110
|
+
const { listeners } = subscription;
|
|
111
|
+
for (const [callback, registration] of [...listeners]) {
|
|
112
|
+
if (this.session !== session || subscription.phase !== 'subscribed')
|
|
113
|
+
break;
|
|
114
|
+
if (listeners.get(callback) !== registration)
|
|
115
|
+
continue;
|
|
116
|
+
try {
|
|
117
|
+
callback(params.payload);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
queueMicrotask(() => { throw error; });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
rejectListeners(name, subscription, error) {
|
|
125
|
+
for (const [callback, registration] of subscription.listeners) {
|
|
126
|
+
if (registration.acknowledged)
|
|
127
|
+
continue;
|
|
128
|
+
subscription.listeners.delete(callback);
|
|
129
|
+
registration.reject(error);
|
|
130
|
+
}
|
|
131
|
+
if (subscription.listeners.size === 0 && subscription.phase === 'idle')
|
|
132
|
+
this.subscriptions.delete(name);
|
|
133
|
+
}
|
|
134
|
+
reconcile(name, subscription) {
|
|
135
|
+
const { session } = this;
|
|
136
|
+
const { listeners, phase } = subscription;
|
|
137
|
+
// Serialize controls for the same event, including removal during a subscribe.
|
|
138
|
+
if (phase === 'subscribing' || phase === 'unsubscribing')
|
|
139
|
+
return;
|
|
140
|
+
if (listeners.size === 0) {
|
|
141
|
+
if (phase === 'subscribed' && session)
|
|
142
|
+
this.unsubscribe(name, subscription, session);
|
|
143
|
+
else
|
|
144
|
+
this.subscriptions.delete(name);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (!session)
|
|
148
|
+
return;
|
|
149
|
+
if (phase === 'subscribed') {
|
|
150
|
+
for (const registration of listeners.values()) {
|
|
151
|
+
registration.acknowledged = true;
|
|
152
|
+
registration.resolve();
|
|
153
|
+
}
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (!session.events.has(name)) {
|
|
157
|
+
this.rejectListeners(name, subscription, new BridgeError(ErrorCode.MethodNotFound, 'Event not available', {
|
|
158
|
+
method: 'bridge.subscribe', data: { event: name },
|
|
159
|
+
}));
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
subscription.phase = 'subscribing';
|
|
163
|
+
const fail = (error, cleanup) => {
|
|
164
|
+
if (this.session !== session)
|
|
165
|
+
return;
|
|
166
|
+
if (!cleanup)
|
|
167
|
+
subscription.phase = 'idle';
|
|
168
|
+
this.rejectListeners(name, subscription, error);
|
|
169
|
+
// An ambiguous outcome may have installed the remote subscription.
|
|
170
|
+
if (cleanup)
|
|
171
|
+
this.unsubscribe(name, subscription, session);
|
|
172
|
+
};
|
|
173
|
+
let dispatched = false;
|
|
174
|
+
void this.request('bridge.subscribe', { events: [name] }, {
|
|
175
|
+
waitForSlot: [...listeners.values()].some((registration) => registration.acknowledged),
|
|
176
|
+
onDispatch: () => { dispatched = true; },
|
|
177
|
+
onReply: (result) => {
|
|
178
|
+
if (this.session !== session)
|
|
179
|
+
return;
|
|
180
|
+
const error = subscribeError(result, name);
|
|
181
|
+
if (error) {
|
|
182
|
+
fail(error, error.code === ErrorCode.InvalidRequest);
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
subscription.phase = 'subscribed';
|
|
186
|
+
this.reconcile(name, subscription);
|
|
187
|
+
},
|
|
188
|
+
}).catch((error) => fail(error, dispatched && error.code === ErrorCode.Timeout));
|
|
189
|
+
}
|
|
190
|
+
unsubscribe(name, subscription, session) {
|
|
191
|
+
subscription.phase = 'unsubscribing';
|
|
192
|
+
const finish = (error) => {
|
|
193
|
+
if (this.session !== session)
|
|
194
|
+
return;
|
|
195
|
+
subscription.phase = 'idle';
|
|
196
|
+
if (error) {
|
|
197
|
+
this.rejectListeners(name, subscription, error);
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const { listeners } = subscription;
|
|
201
|
+
// Retained listeners alone must not cause a failed resubscribe to loop.
|
|
202
|
+
if (listeners.size === 0 || [...listeners.values()].some((registration) => !registration.acknowledged)) {
|
|
203
|
+
this.reconcile(name, subscription);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
void this.request('bridge.unsubscribe', { events: [name] }, {
|
|
207
|
+
waitForSlot: true,
|
|
208
|
+
onReply: (result) => {
|
|
209
|
+
const names = isObject(result) ? result.unsubscribed : undefined;
|
|
210
|
+
if (Array.isArray(names) && names.length === 1 && names[0] === name)
|
|
211
|
+
finish();
|
|
212
|
+
else
|
|
213
|
+
finish(new BridgeError(ErrorCode.InvalidRequest, 'Invalid event unsubscription acknowledgement', {
|
|
214
|
+
method: 'bridge.unsubscribe', data: { event: name },
|
|
215
|
+
}));
|
|
216
|
+
},
|
|
217
|
+
}).catch(finish);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { ErrorCode, type JsonObject } from '../../protocol/src/index.js';
|
|
2
|
+
import { BridgeError } from './errors.js';
|
|
3
|
+
import { type EventListener, type EventSubscription } from './events.js';
|
|
4
|
+
import { type HostInfo } from './session.js';
|
|
5
|
+
import { type Adapter } from './transport.js';
|
|
6
|
+
export { BridgeError, ErrorCode };
|
|
7
|
+
export type { EventListener, EventSubscription };
|
|
8
|
+
export type { Adapter, HostInfo };
|
|
9
|
+
export type BridgeState = 'idle' | 'connecting' | 'ready' | 'unavailable';
|
|
10
|
+
export interface BridgeOptions {
|
|
11
|
+
adapter?: Adapter;
|
|
12
|
+
handshakeTimeout?: number;
|
|
13
|
+
queueLimit?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface CallOptions {
|
|
16
|
+
timeout?: number;
|
|
17
|
+
signal?: AbortSignal;
|
|
18
|
+
traceId?: string;
|
|
19
|
+
version?: number;
|
|
20
|
+
}
|
|
21
|
+
export interface Bridge {
|
|
22
|
+
readonly state: BridgeState;
|
|
23
|
+
readonly host: HostInfo | undefined;
|
|
24
|
+
ready(): Promise<HostInfo>;
|
|
25
|
+
has(method: string, version?: number): boolean;
|
|
26
|
+
call<T = unknown>(method: string, params?: JsonObject, options?: CallOptions): Promise<T>;
|
|
27
|
+
hasEvent(name: string): boolean;
|
|
28
|
+
on<T = unknown>(name: string, listener: EventListener<T>): EventSubscription;
|
|
29
|
+
off<T = unknown>(name: string, listener?: EventListener<T>): void;
|
|
30
|
+
}
|
|
31
|
+
export declare function createBridge(options?: BridgeOptions): Bridge;
|
|
32
|
+
export declare const bridge: Bridge;
|
|
33
|
+
export declare const ui: {
|
|
34
|
+
toast: {
|
|
35
|
+
show(params: {
|
|
36
|
+
text: string;
|
|
37
|
+
}, options?: Omit<CallOptions, "version">): Promise<Record<string, never>>;
|
|
38
|
+
};
|
|
39
|
+
};
|