@open-jsbridge/web 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 +21 -0
- package/README.md +54 -2
- package/package.json +50 -4
- package/packages/protocol/src/index.d.ts +68 -0
- package/packages/protocol/src/index.js +165 -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/app.d.ts +5 -0
- package/packages/web-sdk/src/app.js +9 -0
- package/packages/web-sdk/src/auto.d.ts +6 -0
- package/packages/web-sdk/src/auto.js +6 -0
- package/packages/web-sdk/src/bridge.d.ts +35 -0
- package/packages/web-sdk/src/bridge.js +517 -0
- package/packages/web-sdk/src/device.d.ts +8 -0
- package/packages/web-sdk/src/device.js +6 -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 +41 -0
- package/packages/web-sdk/src/events.js +226 -0
- package/packages/web-sdk/src/index.d.ts +4 -0
- package/packages/web-sdk/src/index.js +1 -0
- package/packages/web-sdk/src/media-profile.d.ts +4 -0
- package/packages/web-sdk/src/media-profile.js +3 -0
- package/packages/web-sdk/src/media.d.ts +29 -0
- package/packages/web-sdk/src/media.js +76 -0
- package/packages/web-sdk/src/network.d.ts +7 -0
- package/packages/web-sdk/src/network.js +9 -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/singleton.d.ts +1 -0
- package/packages/web-sdk/src/singleton.js +2 -0
- package/packages/web-sdk/src/transport.d.ts +20 -0
- package/packages/web-sdk/src/transport.js +57 -0
- package/packages/web-sdk/src/ui.d.ts +8 -0
- package/packages/web-sdk/src/ui.js +8 -0
|
@@ -0,0 +1,226 @@
|
|
|
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
|
+
close() {
|
|
100
|
+
this.reset();
|
|
101
|
+
this.rejectWaiting(new BridgeError(ErrorCode.Cancelled, 'Bridge closed'));
|
|
102
|
+
for (const subscription of this.subscriptions.values())
|
|
103
|
+
subscription.listeners.clear();
|
|
104
|
+
this.subscriptions.clear();
|
|
105
|
+
}
|
|
106
|
+
rejectWaiting(error) {
|
|
107
|
+
for (const [name, subscription] of this.subscriptions)
|
|
108
|
+
this.rejectListeners(name, subscription, error);
|
|
109
|
+
}
|
|
110
|
+
deliver(params) {
|
|
111
|
+
if (!params || !isEventName(params.name) || !Object.prototype.hasOwnProperty.call(params, 'payload'))
|
|
112
|
+
return;
|
|
113
|
+
const subscription = this.subscriptions.get(params.name);
|
|
114
|
+
if (subscription?.phase !== 'subscribed')
|
|
115
|
+
return;
|
|
116
|
+
const { session } = this;
|
|
117
|
+
const { listeners } = subscription;
|
|
118
|
+
for (const [callback, registration] of [...listeners]) {
|
|
119
|
+
if (this.session !== session || subscription.phase !== 'subscribed')
|
|
120
|
+
break;
|
|
121
|
+
if (listeners.get(callback) !== registration)
|
|
122
|
+
continue;
|
|
123
|
+
try {
|
|
124
|
+
callback(params.payload);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
queueMicrotask(() => { throw error; });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
rejectListeners(name, subscription, error) {
|
|
132
|
+
for (const [callback, registration] of subscription.listeners) {
|
|
133
|
+
if (registration.acknowledged)
|
|
134
|
+
continue;
|
|
135
|
+
subscription.listeners.delete(callback);
|
|
136
|
+
registration.reject(error);
|
|
137
|
+
}
|
|
138
|
+
if (subscription.listeners.size === 0 && subscription.phase === 'idle')
|
|
139
|
+
this.subscriptions.delete(name);
|
|
140
|
+
}
|
|
141
|
+
reconcile(name, subscription) {
|
|
142
|
+
const { session } = this;
|
|
143
|
+
const { listeners, phase } = subscription;
|
|
144
|
+
// Serialize controls for the same event, including removal during a subscribe.
|
|
145
|
+
if (phase === 'subscribing' || phase === 'unsubscribing')
|
|
146
|
+
return;
|
|
147
|
+
if (listeners.size === 0) {
|
|
148
|
+
if (phase === 'subscribed' && session)
|
|
149
|
+
this.unsubscribe(name, subscription, session);
|
|
150
|
+
else
|
|
151
|
+
this.subscriptions.delete(name);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (!session)
|
|
155
|
+
return;
|
|
156
|
+
if (phase === 'subscribed') {
|
|
157
|
+
for (const registration of listeners.values()) {
|
|
158
|
+
registration.acknowledged = true;
|
|
159
|
+
registration.resolve();
|
|
160
|
+
}
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (!session.events.has(name)) {
|
|
164
|
+
this.rejectListeners(name, subscription, new BridgeError(ErrorCode.MethodNotFound, 'Event not available', {
|
|
165
|
+
method: 'bridge.subscribe', data: { event: name },
|
|
166
|
+
}));
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
subscription.phase = 'subscribing';
|
|
170
|
+
const fail = (error, cleanup) => {
|
|
171
|
+
if (this.session !== session)
|
|
172
|
+
return;
|
|
173
|
+
if (!cleanup)
|
|
174
|
+
subscription.phase = 'idle';
|
|
175
|
+
this.rejectListeners(name, subscription, error);
|
|
176
|
+
// An ambiguous outcome may have installed the remote subscription.
|
|
177
|
+
if (cleanup)
|
|
178
|
+
this.unsubscribe(name, subscription, session);
|
|
179
|
+
};
|
|
180
|
+
let dispatched = false;
|
|
181
|
+
void this.request('bridge.subscribe', { events: [name] }, {
|
|
182
|
+
waitForSlot: [...listeners.values()].some((registration) => registration.acknowledged),
|
|
183
|
+
onDispatch: () => { dispatched = true; },
|
|
184
|
+
onReply: (result) => {
|
|
185
|
+
if (this.session !== session)
|
|
186
|
+
return;
|
|
187
|
+
const error = subscribeError(result, name);
|
|
188
|
+
if (error) {
|
|
189
|
+
fail(error, error.code === ErrorCode.InvalidRequest);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
subscription.phase = 'subscribed';
|
|
193
|
+
this.reconcile(name, subscription);
|
|
194
|
+
},
|
|
195
|
+
}).catch((error) => fail(error, dispatched && error.code === ErrorCode.Timeout));
|
|
196
|
+
}
|
|
197
|
+
unsubscribe(name, subscription, session) {
|
|
198
|
+
subscription.phase = 'unsubscribing';
|
|
199
|
+
const finish = (error) => {
|
|
200
|
+
if (this.session !== session)
|
|
201
|
+
return;
|
|
202
|
+
subscription.phase = 'idle';
|
|
203
|
+
if (error) {
|
|
204
|
+
this.rejectListeners(name, subscription, error);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const { listeners } = subscription;
|
|
208
|
+
// Retained listeners alone must not cause a failed resubscribe to loop.
|
|
209
|
+
if (listeners.size === 0 || [...listeners.values()].some((registration) => !registration.acknowledged)) {
|
|
210
|
+
this.reconcile(name, subscription);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
void this.request('bridge.unsubscribe', { events: [name] }, {
|
|
214
|
+
waitForSlot: true,
|
|
215
|
+
onReply: (result) => {
|
|
216
|
+
const names = isObject(result) ? result.unsubscribed : undefined;
|
|
217
|
+
if (Array.isArray(names) && names.length === 1 && names[0] === name)
|
|
218
|
+
finish();
|
|
219
|
+
else
|
|
220
|
+
finish(new BridgeError(ErrorCode.InvalidRequest, 'Invalid event unsubscription acknowledgement', {
|
|
221
|
+
method: 'bridge.unsubscribe', data: { event: name },
|
|
222
|
+
}));
|
|
223
|
+
},
|
|
224
|
+
}).catch(finish);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './bridge.js';
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export type { ChooseMediaParams, ChooseMediaResult, ImageResource } from './media.js';
|
|
2
|
+
export declare const media: {
|
|
3
|
+
chooseMedia(params?: import("./media.js").ChooseMediaParams, options?: Omit<import("./bridge.js").CallOptions, "version">): Promise<import("./media.js").ChooseMediaResult>;
|
|
4
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Bridge, CallOptions } from './bridge.js';
|
|
2
|
+
export interface ImageResource {
|
|
3
|
+
type: 'image';
|
|
4
|
+
id: string;
|
|
5
|
+
mime: string;
|
|
6
|
+
size: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
thumbUrl: string;
|
|
11
|
+
}
|
|
12
|
+
export type ChooseMediaParams = {
|
|
13
|
+
mediaType?: ['image'];
|
|
14
|
+
sourceType?: ['album'];
|
|
15
|
+
count?: 1;
|
|
16
|
+
};
|
|
17
|
+
export type ChooseMediaResult = {
|
|
18
|
+
status: 'cancelled';
|
|
19
|
+
files: [];
|
|
20
|
+
} | {
|
|
21
|
+
status: 'selected';
|
|
22
|
+
files: [ImageResource];
|
|
23
|
+
};
|
|
24
|
+
export declare function revokeMediaPreviews(client: Bridge): void;
|
|
25
|
+
export declare function createMediaHelpers(client: Bridge): {
|
|
26
|
+
media: {
|
|
27
|
+
chooseMedia(params?: ChooseMediaParams, options?: Omit<CallOptions, "version">): Promise<ChooseMediaResult>;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { ErrorCode } from '../../protocol/src/index.js';
|
|
2
|
+
import { BridgeError } from './errors.js';
|
|
3
|
+
import { isObject } from './session.js';
|
|
4
|
+
const chooseMethod = 'media.chooseMedia@1';
|
|
5
|
+
const maxImageBytes = 20 * 1024 * 1024;
|
|
6
|
+
const maxThumbnailBytes = 32 * 1024;
|
|
7
|
+
const previewLifetime = 300000;
|
|
8
|
+
const mediaId = /^[0-9a-f]{32}$/;
|
|
9
|
+
const imageMime = /^image\/[a-z0-9!#$&^_.+-]+$/i;
|
|
10
|
+
const jpegBase64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/][AQgw]==|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=)?(?![\s\S])/;
|
|
11
|
+
const previews = new WeakMap();
|
|
12
|
+
export function revokeMediaPreviews(client) {
|
|
13
|
+
const active = previews.get(client);
|
|
14
|
+
if (!active)
|
|
15
|
+
return;
|
|
16
|
+
previews.delete(client);
|
|
17
|
+
for (const [url, timer] of active) {
|
|
18
|
+
clearTimeout(timer);
|
|
19
|
+
URL.revokeObjectURL(url);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function isPositiveInteger(value) {
|
|
23
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
|
|
24
|
+
}
|
|
25
|
+
function invalidSelection() {
|
|
26
|
+
return new BridgeError(ErrorCode.InternalError, 'Invalid media selection', { method: chooseMethod });
|
|
27
|
+
}
|
|
28
|
+
function thumbnailBlob(encoded) {
|
|
29
|
+
if (typeof encoded !== 'string' || encoded.length > 4 * Math.ceil(maxThumbnailBytes / 3) ||
|
|
30
|
+
!jpegBase64.test(encoded))
|
|
31
|
+
throw invalidSelection();
|
|
32
|
+
const binary = atob(encoded);
|
|
33
|
+
if (binary.length < 4 || binary.length > maxThumbnailBytes ||
|
|
34
|
+
binary.charCodeAt(0) !== 255 || binary.charCodeAt(1) !== 216 ||
|
|
35
|
+
binary.charCodeAt(binary.length - 2) !== 255 || binary.charCodeAt(binary.length - 1) !== 217) {
|
|
36
|
+
throw invalidSelection();
|
|
37
|
+
}
|
|
38
|
+
return new Blob([Uint8Array.from(binary, (byte) => byte.charCodeAt(0))], { type: 'image/jpeg' });
|
|
39
|
+
}
|
|
40
|
+
export function createMediaHelpers(client) {
|
|
41
|
+
return {
|
|
42
|
+
media: {
|
|
43
|
+
async chooseMedia(params, options) {
|
|
44
|
+
const active = previews.get(client) ?? new Map();
|
|
45
|
+
previews.set(client, active);
|
|
46
|
+
const result = await client.call(chooseMethod, params, { ...options, timeout: options?.timeout ?? 120000 });
|
|
47
|
+
if (previews.get(client) !== active) {
|
|
48
|
+
throw new BridgeError(ErrorCode.Cancelled, 'Media selection retired', { method: chooseMethod });
|
|
49
|
+
}
|
|
50
|
+
if (!isObject(result) || !Array.isArray(result.files))
|
|
51
|
+
throw invalidSelection();
|
|
52
|
+
if (result.status === 'cancelled' && result.files.length === 0)
|
|
53
|
+
return { status: 'cancelled', files: [] };
|
|
54
|
+
const [file] = result.files;
|
|
55
|
+
if (result.status !== 'selected' || result.files.length !== 1 || !isObject(file))
|
|
56
|
+
throw invalidSelection();
|
|
57
|
+
const { type, id, mime, size, width, height, expiresAt, thumbnailBase64 } = file;
|
|
58
|
+
if (type !== 'image' || typeof id !== 'string' || !mediaId.test(id) ||
|
|
59
|
+
typeof mime !== 'string' || mime.length > 128 || !imageMime.test(mime) ||
|
|
60
|
+
!isPositiveInteger(size) || size > maxImageBytes ||
|
|
61
|
+
!isPositiveInteger(width) || !isPositiveInteger(height) ||
|
|
62
|
+
width > 2147483647 || height > 2147483647 || !isPositiveInteger(expiresAt))
|
|
63
|
+
throw invalidSelection();
|
|
64
|
+
const ttl = Math.min(expiresAt - Date.now(), previewLifetime);
|
|
65
|
+
if (ttl <= 0)
|
|
66
|
+
throw new BridgeError(ErrorCode.PermissionDenied, 'Media selection expired', { method: chooseMethod });
|
|
67
|
+
const thumbUrl = URL.createObjectURL(thumbnailBlob(thumbnailBase64));
|
|
68
|
+
active.set(thumbUrl, setTimeout(() => {
|
|
69
|
+
active.delete(thumbUrl);
|
|
70
|
+
URL.revokeObjectURL(thumbUrl);
|
|
71
|
+
}, ttl));
|
|
72
|
+
return { status: 'selected', files: [{ type, id, mime, size, width, height, expiresAt, thumbUrl }] };
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CallOptions } from './bridge.js';
|
|
2
|
+
import type { EventListener, NetworkStatus } from './events.js';
|
|
3
|
+
export type { NetworkStatus } from './events.js';
|
|
4
|
+
export declare const network: {
|
|
5
|
+
getStatus(options?: Omit<CallOptions, "version">): Promise<NetworkStatus>;
|
|
6
|
+
onStatusChanged(listener: EventListener<NetworkStatus>): import("./events.js").EventSubscription;
|
|
7
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { JsonObject, JsonValue } from '../../protocol/src/index.js';
|
|
2
|
+
export interface HostInfo {
|
|
3
|
+
readonly platform: string;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly version: string;
|
|
6
|
+
readonly appId?: string;
|
|
7
|
+
readonly appVersion?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface Session {
|
|
10
|
+
sid: string;
|
|
11
|
+
host: HostInfo;
|
|
12
|
+
methods: Map<string, ReadonlySet<number>>;
|
|
13
|
+
events: ReadonlySet<string>;
|
|
14
|
+
features: readonly string[];
|
|
15
|
+
defaultTimeout: number;
|
|
16
|
+
maxMessageBytes: number;
|
|
17
|
+
maxInflight: number;
|
|
18
|
+
inflight: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function isObject(value: unknown): value is JsonObject;
|
|
21
|
+
export declare function readSession(result: JsonValue, sid?: string): Session | undefined;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export function isObject(value) {
|
|
2
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
3
|
+
}
|
|
4
|
+
function isPositiveInteger(value) {
|
|
5
|
+
return typeof value === 'number' && Number.isSafeInteger(value) && value > 0;
|
|
6
|
+
}
|
|
7
|
+
function isStringArray(value) {
|
|
8
|
+
return Array.isArray(value) && value.every((entry) => typeof entry === 'string') &&
|
|
9
|
+
new Set(value).size === value.length;
|
|
10
|
+
}
|
|
11
|
+
export function readSession(result, sid) {
|
|
12
|
+
if (!isObject(result) || result.jsb !== '1.0' || typeof result.sid !== 'string' || !result.sid ||
|
|
13
|
+
(sid !== undefined && sid !== result.sid))
|
|
14
|
+
return;
|
|
15
|
+
const { host, capabilities, limits } = result;
|
|
16
|
+
if (!isObject(host) || typeof host.platform !== 'string' ||
|
|
17
|
+
typeof host.name !== 'string' || typeof host.version !== 'string' ||
|
|
18
|
+
(host.appId !== undefined && typeof host.appId !== 'string') ||
|
|
19
|
+
(host.appVersion !== undefined && typeof host.appVersion !== 'string'))
|
|
20
|
+
return;
|
|
21
|
+
if (!isObject(capabilities) || !isObject(capabilities.methods) ||
|
|
22
|
+
!isStringArray(capabilities.events) || !isStringArray(capabilities.features))
|
|
23
|
+
return;
|
|
24
|
+
if (!isObject(limits) || !isPositiveInteger(limits.maxMessageBytes) ||
|
|
25
|
+
!isPositiveInteger(limits.maxInflight) || !isPositiveInteger(limits.defaultTimeoutMs) ||
|
|
26
|
+
limits.defaultTimeoutMs > 2147483647)
|
|
27
|
+
return;
|
|
28
|
+
const methods = new Map();
|
|
29
|
+
for (const [method, entry] of Object.entries(capabilities.methods)) {
|
|
30
|
+
if (!isObject(entry) || !Array.isArray(entry.versions) || !entry.versions.length ||
|
|
31
|
+
!entry.versions.every(isPositiveInteger))
|
|
32
|
+
return;
|
|
33
|
+
const versions = new Set(entry.versions);
|
|
34
|
+
if (versions.size !== entry.versions.length)
|
|
35
|
+
return;
|
|
36
|
+
methods.set(method, versions);
|
|
37
|
+
}
|
|
38
|
+
const hostInfo = { platform: host.platform, name: host.name, version: host.version };
|
|
39
|
+
return {
|
|
40
|
+
sid: result.sid,
|
|
41
|
+
host: {
|
|
42
|
+
...hostInfo,
|
|
43
|
+
...(host.appId === undefined ? {} : { appId: host.appId }),
|
|
44
|
+
...(host.appVersion === undefined ? {} : { appVersion: host.appVersion }),
|
|
45
|
+
},
|
|
46
|
+
methods,
|
|
47
|
+
events: new Set(capabilities.events),
|
|
48
|
+
features: capabilities.features,
|
|
49
|
+
defaultTimeout: limits.defaultTimeoutMs,
|
|
50
|
+
maxMessageBytes: limits.maxMessageBytes,
|
|
51
|
+
maxInflight: limits.maxInflight,
|
|
52
|
+
inflight: 0,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const bridge: import("./bridge.js").Bridge;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface Adapter {
|
|
2
|
+
readonly name?: string;
|
|
3
|
+
readonly protocols?: readonly number[];
|
|
4
|
+
detect?(): boolean;
|
|
5
|
+
send(raw: string): void | Promise<string | null>;
|
|
6
|
+
listen(onMessage: (raw: string) => void): () => void;
|
|
7
|
+
}
|
|
8
|
+
interface OJSBTransport {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly protocols: readonly number[];
|
|
11
|
+
postMessage(raw: string): void | Promise<string | null>;
|
|
12
|
+
}
|
|
13
|
+
declare global {
|
|
14
|
+
interface Window {
|
|
15
|
+
__OJSB_TRANSPORT__?: Readonly<OJSBTransport>;
|
|
16
|
+
__OJSB_RECEIVE__?: (raw: string) => void;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export declare function findBrowserAdapter(timeout: number, signal: AbortSignal): Promise<Adapter | undefined>;
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const receivers = new WeakMap();
|
|
2
|
+
export function findBrowserAdapter(timeout, signal) {
|
|
3
|
+
if (typeof window === 'undefined' || signal.aborted)
|
|
4
|
+
return Promise.resolve(undefined);
|
|
5
|
+
const target = window;
|
|
6
|
+
const detect = () => {
|
|
7
|
+
const transport = target.__OJSB_TRANSPORT__;
|
|
8
|
+
if (typeof transport?.postMessage !== 'function')
|
|
9
|
+
return;
|
|
10
|
+
return {
|
|
11
|
+
name: transport.name,
|
|
12
|
+
protocols: transport.protocols,
|
|
13
|
+
send: (raw) => transport.postMessage(raw),
|
|
14
|
+
listen(onMessage) {
|
|
15
|
+
if (receivers.has(target))
|
|
16
|
+
throw new Error('The injected shim already has a receiver');
|
|
17
|
+
receivers.set(target, onMessage);
|
|
18
|
+
try {
|
|
19
|
+
target.__OJSB_RECEIVE__ = onMessage;
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
receivers.delete(target);
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
return () => {
|
|
26
|
+
// Native shims expose a forwarding getter, so ownership is tracked independently.
|
|
27
|
+
if (receivers.get(target) !== onMessage)
|
|
28
|
+
return;
|
|
29
|
+
receivers.delete(target);
|
|
30
|
+
target.__OJSB_RECEIVE__ = undefined;
|
|
31
|
+
};
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
const existing = detect();
|
|
36
|
+
if (existing)
|
|
37
|
+
return Promise.resolve(existing);
|
|
38
|
+
return new Promise((resolve) => {
|
|
39
|
+
const finish = (adapter) => {
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
target.removeEventListener('ojsb:transportready', onReady);
|
|
42
|
+
signal.removeEventListener('abort', onAbort);
|
|
43
|
+
resolve(adapter);
|
|
44
|
+
};
|
|
45
|
+
const onAbort = () => finish();
|
|
46
|
+
const onReady = () => {
|
|
47
|
+
const adapter = detect();
|
|
48
|
+
if (adapter)
|
|
49
|
+
finish(adapter);
|
|
50
|
+
};
|
|
51
|
+
const timer = setTimeout(finish, timeout);
|
|
52
|
+
target.addEventListener('ojsb:transportready', onReady);
|
|
53
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
54
|
+
if (signal.aborted)
|
|
55
|
+
onAbort();
|
|
56
|
+
});
|
|
57
|
+
}
|