@swiftbrowser/native 0.17.6 → 0.17.8
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/README.md +101 -2
- package/dist/blob.d.ts +60 -0
- package/dist/blob.d.ts.map +1 -0
- package/dist/blob.js +153 -0
- package/dist/blob.js.map +1 -0
- package/dist/bridge.d.ts +47 -1
- package/dist/bridge.d.ts.map +1 -1
- package/dist/bridge.js.map +1 -1
- package/dist/fetch.d.ts +11 -3
- package/dist/fetch.d.ts.map +1 -1
- package/dist/fetch.js +26 -2
- package/dist/fetch.js.map +1 -1
- package/dist/hostRequests.d.ts +49 -0
- package/dist/hostRequests.d.ts.map +1 -0
- package/dist/hostRequests.js +134 -0
- package/dist/hostRequests.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -1
- package/dist/matchMedia.d.ts +43 -0
- package/dist/matchMedia.d.ts.map +1 -0
- package/dist/matchMedia.js +63 -0
- package/dist/matchMedia.js.map +1 -0
- package/dist/mediaSession.d.ts +59 -0
- package/dist/mediaSession.d.ts.map +1 -0
- package/dist/mediaSession.js +119 -0
- package/dist/mediaSession.js.map +1 -0
- package/dist/notifications.d.ts +108 -0
- package/dist/notifications.d.ts.map +1 -0
- package/dist/notifications.js +251 -0
- package/dist/notifications.js.map +1 -0
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +9 -1
- package/dist/run.js.map +1 -1
- package/dist/runtime.d.ts +3 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/runtime.js +72 -3
- package/dist/runtime.js.map +1 -1
- package/dist/secureStorage.d.ts +20 -0
- package/dist/secureStorage.d.ts.map +1 -0
- package/dist/secureStorage.js +28 -0
- package/dist/secureStorage.js.map +1 -0
- package/dist/subtle.d.ts +38 -0
- package/dist/subtle.d.ts.map +1 -0
- package/dist/subtle.js +72 -0
- package/dist/subtle.js.map +1 -0
- package/dist/webSocket.d.ts +73 -0
- package/dist/webSocket.d.ts.map +1 -0
- package/dist/webSocket.js +145 -0
- package/dist/webSocket.js.map +1 -0
- package/package.json +3 -3
- package/src/blob.ts +186 -0
- package/src/bridge.ts +57 -1
- package/src/fetch.ts +29 -3
- package/src/hostRequests.ts +148 -0
- package/src/index.ts +8 -0
- package/src/matchMedia.ts +79 -0
- package/src/mediaSession.ts +163 -0
- package/src/notifications.ts +317 -0
- package/src/run.ts +10 -1
- package/src/runtime.ts +73 -3
- package/src/secureStorage.ts +39 -0
- package/src/subtle.ts +87 -0
- package/src/webSocket.ts +175 -0
- package/swift/Sources/SwiftBrowserNative/Host/HostBridge.swift +180 -0
- package/swift/Sources/SwiftBrowserNative/Host/HostRequestBridge.swift +437 -0
- package/swift/Sources/SwiftBrowserNative/Host/NotificationBridge.swift +304 -0
- package/swift/Sources/SwiftBrowserNative/Host/NowPlayingBridge.swift +190 -0
- package/swift/Sources/SwiftBrowserNative/Host/SecureStorage.swift +60 -0
- package/swift/Sources/SwiftBrowserNative/Host/ShimAliases.swift +3 -0
- package/swift/Sources/SwiftBrowserNative/Host/WebSocketBridge.swift +118 -0
- package/swift/Sources/SwiftBrowserNative/Runtime/NativeApp.swift +26 -0
- package/swift/Sources/SwiftBrowserNative/Runtime/NativeAppDelegate.swift +36 -0
- package/swift/Sources/SwiftBrowserNative/Views/ElementView.swift +2 -0
- package/swift/Sources/SwiftBrowserNative/Views/Kinds/Controls.swift +22 -9
- package/swift/Sources/SwiftBrowserNative/Views/Kinds/Navigation.swift +8 -5
- package/swift/Sources/SwiftBrowserNative/Views/Kinds/Video.swift +304 -0
- package/swift/Sources/SwiftBrowserProtocol/Props.swift +43 -0
- package/swift/Sources/SwiftBrowserProtocol/RendererEvent.swift +12 -2
- package/wasm/SwiftBrowserNative.wasm +2 -2
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Notifications in the native app: the browser's `Notification` (the
|
|
3
|
+
* permission, local notifications and their clicks) and `navigator.setAppBadge`
|
|
4
|
+
* over the host's UserNotifications (Native/Sources/SwiftBrowserNative/Host/NotificationBridge.swift),
|
|
5
|
+
* and remote notifications: `registerForPush` resolves with the device token
|
|
6
|
+
* (APNs, as hex) and `onNotification` delivers what the viewer opened and what
|
|
7
|
+
* arrived while the app is open.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const token = await registerForPush();
|
|
11
|
+
* await fetch('/api/devices', { method: 'POST', body: JSON.stringify({ token }) });
|
|
12
|
+
* useEffect(() => onNotification((n) => { if (n.event === 'opened') show(n.data.postId); }), []);
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* In the page (the round trip, or any browser) the host has no bridge for
|
|
16
|
+
* these: `Notification` is the browser's own, and `registerForPush` subscribes
|
|
17
|
+
* to Web Push through the site's service worker (`sw.js`, which the web
|
|
18
|
+
* renderer ships: docs/ops-protocol.md, "Push notifications") with the
|
|
19
|
+
* `applicationServerKey` given, resolving with the subscription's JSON as
|
|
20
|
+
* the token; the worker's messages become `onNotification` events. A server
|
|
21
|
+
* sends to both with one payload: APNs' (`aps.alert`, `aps.badge`), which
|
|
22
|
+
* the worker reads too.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { NativeBridge } from './bridge';
|
|
26
|
+
|
|
27
|
+
export type NotificationPermissionState = 'default' | 'granted' | 'denied';
|
|
28
|
+
|
|
29
|
+
/** What the viewer did with a notification, or one that arrived while the app is open. */
|
|
30
|
+
export interface NotificationEvent {
|
|
31
|
+
event: 'opened' | 'received';
|
|
32
|
+
/** Whether it came from the push service. */
|
|
33
|
+
remote: boolean;
|
|
34
|
+
/** The notification's identifier (`tag`, or the push payload's `id`). */
|
|
35
|
+
id: string;
|
|
36
|
+
title: string;
|
|
37
|
+
body: string;
|
|
38
|
+
/** A push's payload, or what `new Notification(title, { data })` gave. */
|
|
39
|
+
data: Record<string, unknown>;
|
|
40
|
+
/** `opened`: the action the viewer chose, empty for the notification itself. */
|
|
41
|
+
action?: string;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface NativeNotificationOptions {
|
|
45
|
+
body?: string;
|
|
46
|
+
/** The notification's identifier: a later one with the same tag replaces it. */
|
|
47
|
+
tag?: string;
|
|
48
|
+
data?: unknown;
|
|
49
|
+
silent?: boolean;
|
|
50
|
+
/** The app badge to set with it. */
|
|
51
|
+
badge?: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface RegisterForPushOptions {
|
|
55
|
+
/** The Web Push application server key (the server's VAPID public key, base64url), for the page. */
|
|
56
|
+
applicationServerKey?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
type Listener = (notification: NotificationEvent) => void;
|
|
60
|
+
|
|
61
|
+
/** The message types the web renderer's service worker posts (Web/public/sw.js). */
|
|
62
|
+
const PUSH_MESSAGE = 'swiftbrowser:push';
|
|
63
|
+
const CLICK_MESSAGE = 'swiftbrowser:notificationclick';
|
|
64
|
+
|
|
65
|
+
/** The bridge functions notifications need. */
|
|
66
|
+
export type NotificationBridge = Pick<NativeBridge, 'notificationsReady' | 'notificationPermission' | 'notificationShow' | 'notificationClose' | 'setBadge' | 'pushRegister'>;
|
|
67
|
+
|
|
68
|
+
export interface NotificationGlobals {
|
|
69
|
+
Notification: NativeNotificationClass;
|
|
70
|
+
__sbNotificationPermission: (id: number, permission: NotificationPermissionState) => void;
|
|
71
|
+
__sbPushRegistered: (id: number, result: { token?: string; error?: string }) => void;
|
|
72
|
+
__sbNotification: (json: string) => void;
|
|
73
|
+
/** `navigator.setAppBadge` and `clearAppBadge`. */
|
|
74
|
+
setAppBadge: (count?: number) => Promise<void>;
|
|
75
|
+
clearAppBadge: () => Promise<void>;
|
|
76
|
+
/** The native `registerForPush`. */
|
|
77
|
+
registerForPush: (options?: RegisterForPushOptions) => Promise<string>;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface NativeNotificationClass {
|
|
81
|
+
new (title: string, options?: NativeNotificationOptions): NativeNotificationInstance;
|
|
82
|
+
readonly permission: NotificationPermissionState;
|
|
83
|
+
requestPermission(): Promise<NotificationPermissionState>;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface NativeNotificationInstance {
|
|
87
|
+
readonly title: string;
|
|
88
|
+
readonly body: string;
|
|
89
|
+
readonly tag: string;
|
|
90
|
+
readonly data: unknown;
|
|
91
|
+
onclick: ((event: { notification: NativeNotificationInstance; action: string }) => void) | null;
|
|
92
|
+
close(): void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const listeners = new Set<Listener>();
|
|
96
|
+
let pending: NotificationEvent[] = [];
|
|
97
|
+
/** Where the events come from once installed: the bridge, or the page's service worker. */
|
|
98
|
+
let installed: 'native' | 'page' | null = null;
|
|
99
|
+
let nativeRegister: ((options?: RegisterForPushOptions) => Promise<string>) | null = null;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Listens for notifications the viewer opened and ones that arrived while
|
|
103
|
+
* the app is open; returns what stops listening. Events before any listener
|
|
104
|
+
* (a tap that launched the app) wait for the first one.
|
|
105
|
+
*/
|
|
106
|
+
export function onNotification(listener: Listener): () => void {
|
|
107
|
+
listeners.add(listener);
|
|
108
|
+
if (installed === null && typeof navigator !== 'undefined') listenToPage();
|
|
109
|
+
if (pending.length > 0) {
|
|
110
|
+
const events = pending;
|
|
111
|
+
pending = [];
|
|
112
|
+
for (const event of events) listener(event);
|
|
113
|
+
}
|
|
114
|
+
return () => {
|
|
115
|
+
listeners.delete(listener);
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** The runtime's `__sbNotification(json)`, and what the page's worker messages become. */
|
|
120
|
+
export function receiveNotification(event: NotificationEvent): void {
|
|
121
|
+
if (listeners.size === 0) {
|
|
122
|
+
pending.push(event);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
for (const listener of listeners) listener(event);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Registers for remote notifications and resolves with the device token:
|
|
130
|
+
* APNs' as hex in the native app, the Web Push subscription's JSON in the
|
|
131
|
+
* page (which needs `applicationServerKey`). Rejects with the reason the
|
|
132
|
+
* system or the browser refused.
|
|
133
|
+
*/
|
|
134
|
+
export function registerForPush(options: RegisterForPushOptions = {}): Promise<string> {
|
|
135
|
+
if (nativeRegister) return nativeRegister(options);
|
|
136
|
+
return registerForWebPush(options);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Resets the listeners and the delivery source, for tests. */
|
|
140
|
+
export function resetNotifications(): void {
|
|
141
|
+
listeners.clear();
|
|
142
|
+
pending = [];
|
|
143
|
+
installed = null;
|
|
144
|
+
nativeRegister = null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// MARK: The native app
|
|
148
|
+
|
|
149
|
+
/** The globals for a host with the notification bridge. */
|
|
150
|
+
export function createNotificationGlobals(native: NotificationBridge): NotificationGlobals {
|
|
151
|
+
installed = 'native';
|
|
152
|
+
let permission: NotificationPermissionState = 'default';
|
|
153
|
+
let nextId = 1;
|
|
154
|
+
const permissionWaits = new Map<number, (permission: NotificationPermissionState) => void>();
|
|
155
|
+
const registrations = new Map<number, { resolve: (token: string) => void; reject: (error: Error) => void }>();
|
|
156
|
+
const open = new Map<string, NativeNotification>();
|
|
157
|
+
|
|
158
|
+
const askPermission = (request: boolean): Promise<NotificationPermissionState> =>
|
|
159
|
+
new Promise((resolve) => {
|
|
160
|
+
const id = nextId++;
|
|
161
|
+
permissionWaits.set(id, resolve);
|
|
162
|
+
native.notificationPermission?.(id, request);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
class NativeNotification implements NativeNotificationInstance {
|
|
166
|
+
static get permission(): NotificationPermissionState {
|
|
167
|
+
return permission;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
static requestPermission(): Promise<NotificationPermissionState> {
|
|
171
|
+
return askPermission(true);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
readonly title: string;
|
|
175
|
+
readonly body: string;
|
|
176
|
+
readonly tag: string;
|
|
177
|
+
readonly data: unknown;
|
|
178
|
+
onclick: NativeNotificationInstance['onclick'] = null;
|
|
179
|
+
|
|
180
|
+
constructor(title: string, options: NativeNotificationOptions = {}) {
|
|
181
|
+
this.title = String(title);
|
|
182
|
+
this.body = options.body ?? '';
|
|
183
|
+
this.tag = options.tag ?? `notification-${nextId++}`;
|
|
184
|
+
this.data = options.data;
|
|
185
|
+
const show: Record<string, unknown> = { id: this.tag, title: this.title, body: this.body, silent: options.silent ?? false };
|
|
186
|
+
if (options.badge !== undefined) show['badge'] = options.badge;
|
|
187
|
+
if (options.data !== undefined && typeof options.data === 'object' && options.data !== null) show['data'] = options.data;
|
|
188
|
+
const problem = native.notificationShow?.(JSON.stringify(show));
|
|
189
|
+
if (problem) throw new TypeError(problem);
|
|
190
|
+
open.set(this.tag, this);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
close(): void {
|
|
194
|
+
if (open.get(this.tag) === this) open.delete(this.tag);
|
|
195
|
+
native.notificationClose?.(JSON.stringify([this.tag]));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const setAppBadge = async (count = 0): Promise<void> => {
|
|
200
|
+
native.setBadge?.(Number.isFinite(count) ? Math.max(0, Math.floor(count)) : 0);
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
nativeRegister = (options = {}) =>
|
|
204
|
+
new Promise<string>((resolve, reject) => {
|
|
205
|
+
const id = nextId++;
|
|
206
|
+
registrations.set(id, { resolve, reject });
|
|
207
|
+
native.pushRegister?.(id, options.applicationServerKey);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// The permission as it stands, so `Notification.permission` reads right
|
|
211
|
+
// from the first render; and the app listens, so what came before arrives.
|
|
212
|
+
void askPermission(false);
|
|
213
|
+
native.notificationsReady?.();
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
Notification: NativeNotification,
|
|
217
|
+
__sbNotificationPermission: (id, state) => {
|
|
218
|
+
permission = state;
|
|
219
|
+
const resolve = permissionWaits.get(id);
|
|
220
|
+
permissionWaits.delete(id);
|
|
221
|
+
resolve?.(state);
|
|
222
|
+
},
|
|
223
|
+
__sbPushRegistered: (id, result) => {
|
|
224
|
+
const registration = registrations.get(id);
|
|
225
|
+
registrations.delete(id);
|
|
226
|
+
if (!registration) return;
|
|
227
|
+
if (typeof result.token === 'string') registration.resolve(result.token);
|
|
228
|
+
else registration.reject(new Error(result.error ?? 'push: registration failed'));
|
|
229
|
+
},
|
|
230
|
+
__sbNotification: (json) => {
|
|
231
|
+
let event: NotificationEvent;
|
|
232
|
+
try {
|
|
233
|
+
event = JSON.parse(json) as NotificationEvent;
|
|
234
|
+
} catch {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (event.event === 'opened') {
|
|
238
|
+
const notification = open.get(event.id);
|
|
239
|
+
if (notification) {
|
|
240
|
+
open.delete(event.id);
|
|
241
|
+
notification.onclick?.({ notification, action: event.action ?? '' });
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
receiveNotification(event);
|
|
245
|
+
},
|
|
246
|
+
setAppBadge,
|
|
247
|
+
clearAppBadge: () => setAppBadge(0),
|
|
248
|
+
registerForPush: nativeRegister,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// MARK: The page
|
|
253
|
+
|
|
254
|
+
/** The event a service worker message carries (Web/src/notifications.ts), or null. */
|
|
255
|
+
export function notificationEventOf(data: unknown): NotificationEvent | null {
|
|
256
|
+
if (typeof data !== 'object' || data === null) return null;
|
|
257
|
+
const message = data as { type?: unknown; payload?: unknown; action?: unknown };
|
|
258
|
+
if (message.type !== PUSH_MESSAGE && message.type !== CLICK_MESSAGE) return null;
|
|
259
|
+
const payload = typeof message.payload === 'object' && message.payload !== null ? (message.payload as Record<string, unknown>) : {};
|
|
260
|
+
const aps = typeof payload['aps'] === 'object' && payload['aps'] !== null ? (payload['aps'] as Record<string, unknown>) : {};
|
|
261
|
+
const alert = typeof aps['alert'] === 'object' && aps['alert'] !== null ? (aps['alert'] as Record<string, unknown>) : {};
|
|
262
|
+
const text = (value: unknown): string | undefined => (typeof value === 'string' ? value : undefined);
|
|
263
|
+
const event: NotificationEvent = {
|
|
264
|
+
event: message.type === CLICK_MESSAGE ? 'opened' : 'received',
|
|
265
|
+
remote: true,
|
|
266
|
+
id: text(payload['id']) ?? text(payload['tag']) ?? '',
|
|
267
|
+
title: text(payload['title']) ?? text(alert['title']) ?? '',
|
|
268
|
+
body: text(payload['body']) ?? text(alert['body']) ?? text(aps['alert']) ?? '',
|
|
269
|
+
data: payload,
|
|
270
|
+
};
|
|
271
|
+
if (message.type === CLICK_MESSAGE) event.action = typeof message.action === 'string' ? message.action : '';
|
|
272
|
+
return event;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function listenToPage(): void {
|
|
276
|
+
installed = 'page';
|
|
277
|
+
const container = navigator.serviceWorker;
|
|
278
|
+
if (!container || typeof container.addEventListener !== 'function') return;
|
|
279
|
+
container.addEventListener('message', (message: MessageEvent) => {
|
|
280
|
+
const event = notificationEventOf(message.data);
|
|
281
|
+
if (event) receiveNotification(event);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** A VAPID public key (base64url) as the bytes `subscribe` takes. */
|
|
286
|
+
function keyBytes(key: string): Uint8Array<ArrayBuffer> {
|
|
287
|
+
const base64 = key.replace(/-/g, '+').replace(/_/g, '/');
|
|
288
|
+
const binary = atob(base64 + '='.repeat((4 - (base64.length % 4)) % 4));
|
|
289
|
+
const bytes = new Uint8Array(new ArrayBuffer(binary.length));
|
|
290
|
+
for (let i = 0; i < binary.length; i += 1) bytes[i] = binary.charCodeAt(i);
|
|
291
|
+
return bytes;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async function registerForWebPush(options: RegisterForPushOptions): Promise<string> {
|
|
295
|
+
if (typeof navigator === 'undefined' || !navigator.serviceWorker || typeof PushManager === 'undefined') {
|
|
296
|
+
throw new Error('push: this browser has no push service');
|
|
297
|
+
}
|
|
298
|
+
if (!options.applicationServerKey) throw new Error('push: no applicationServerKey');
|
|
299
|
+
if (typeof Notification !== 'undefined' && Notification.permission === 'default') await Notification.requestPermission();
|
|
300
|
+
if (typeof Notification !== 'undefined' && Notification.permission !== 'granted') throw new Error('push: notifications are not allowed');
|
|
301
|
+
if (installed === null) listenToPage();
|
|
302
|
+
const scope = new URL('./', typeof document !== 'undefined' ? document.baseURI : location.href).href;
|
|
303
|
+
const existing = await navigator.serviceWorker.getRegistration(scope);
|
|
304
|
+
const registration = existing?.active && existing.scope === scope ? existing : await navigator.serviceWorker.register(new URL('sw.js', scope).href, { scope });
|
|
305
|
+
await navigator.serviceWorker.ready;
|
|
306
|
+
const init: PushSubscriptionOptionsInit = { userVisibleOnly: true, applicationServerKey: keyBytes(options.applicationServerKey) };
|
|
307
|
+
const current = await registration.pushManager.getSubscription();
|
|
308
|
+
let subscription: PushSubscription;
|
|
309
|
+
try {
|
|
310
|
+
subscription = current ?? (await registration.pushManager.subscribe(init));
|
|
311
|
+
} catch (error) {
|
|
312
|
+
if (!current) throw error;
|
|
313
|
+
await current.unsubscribe().catch(() => undefined);
|
|
314
|
+
subscription = await registration.pushManager.subscribe(init);
|
|
315
|
+
}
|
|
316
|
+
return JSON.stringify(subscription.toJSON());
|
|
317
|
+
}
|
package/src/run.ts
CHANGED
|
@@ -11,7 +11,10 @@
|
|
|
11
11
|
import { createSwiftUIRoot, type SwiftUIRoot } from '@swiftbrowser/react';
|
|
12
12
|
import type { Op, RendererEvent } from '@swiftbrowser/web/src/protocol';
|
|
13
13
|
import type { ReactNode } from 'react';
|
|
14
|
+
import { browserEnvironment } from '@swiftbrowser/web/src/hostRequests';
|
|
14
15
|
import { nativeBridge, type NativeBridge } from './bridge';
|
|
16
|
+
import { installHostEnvironment } from './hostRequests';
|
|
17
|
+
import { updateMediaEnvironment } from './matchMedia';
|
|
15
18
|
|
|
16
19
|
export interface RunOptions {
|
|
17
20
|
/** The bridge to render through; the global `__sbNative` by default. */
|
|
@@ -41,6 +44,9 @@ export function run(element: ReactNode, options: RunOptions = {}): Running {
|
|
|
41
44
|
const native = options.native ?? nativeBridge();
|
|
42
45
|
if (!native) throw new Error('run(): no native bridge (__sbNative); this entry runs inside a SwiftBrowser Native app');
|
|
43
46
|
const report = options.onError ?? ((error: unknown) => native.log('error', error instanceof Error ? (error.stack ?? error.message) : String(error)));
|
|
47
|
+
// What `ShareLink`, `fileImporter` and the clipboard ask the browser for,
|
|
48
|
+
// answered by the host where the page has nothing (hostRequests.ts).
|
|
49
|
+
installHostEnvironment(browserEnvironment as unknown as Record<string, unknown>);
|
|
44
50
|
|
|
45
51
|
if (current) {
|
|
46
52
|
// Fast Refresh (devBuild.ts): the app bundle was evaluated again. Its
|
|
@@ -68,7 +74,10 @@ export function run(element: ReactNode, options: RunOptions = {}): Running {
|
|
|
68
74
|
if (parsed.type === 'environment') {
|
|
69
75
|
const fields = parsed as EnvironmentFields;
|
|
70
76
|
const next: Partial<{ colorScheme: 'light' | 'dark'; dynamicTypeSize: never; horizontalSizeClass: 'compact' | 'regular' }> = {};
|
|
71
|
-
if (fields.colorScheme)
|
|
77
|
+
if (fields.colorScheme) {
|
|
78
|
+
next.colorScheme = fields.colorScheme;
|
|
79
|
+
updateMediaEnvironment({ colorScheme: fields.colorScheme });
|
|
80
|
+
}
|
|
72
81
|
if (fields.dynamicTypeSize) (next as Record<string, unknown>)['dynamicTypeSize'] = fields.dynamicTypeSize;
|
|
73
82
|
if (fields.horizontalSizeClass) next.horizontalSizeClass = fields.horizontalSizeClass;
|
|
74
83
|
if (Object.keys(next).length > 0) root.setEnvironment(next);
|
package/src/runtime.ts
CHANGED
|
@@ -11,9 +11,11 @@
|
|
|
11
11
|
* `atob`, `btoa`, `fetch` with `Headers`, `Request`, `Response`,
|
|
12
12
|
* `AbortController` and `AbortSignal`, `URL`, `URLSearchParams`, `crypto`
|
|
13
13
|
* (`getRandomValues`, `randomUUID`), `localStorage`, `structuredClone`,
|
|
14
|
-
* `navigator
|
|
14
|
+
* `navigator` (with `mediaSession` and `MediaMetadata` on a bridge with
|
|
15
|
+
* `mediaSession`, mediaSession.ts), `Image` (image.ts, on a bridge with `probeImage`) with the
|
|
15
16
|
* bridge's `__sbImageProbed`, `Worker` (worker.ts) with the bridge's
|
|
16
17
|
* `__sbWorkerMessage` and `__sbWorkerError`, `__sbOpenURL` (openURL.ts),
|
|
18
|
+
* `__sbNotificationPermission`, `__sbPushRegistered` and `__sbNotification` (notifications.ts),
|
|
17
19
|
* and the bridge's callbacks `__sbTimerFired` and `__sbFetchResult`. In a
|
|
18
20
|
* worker's own context (its bridge has
|
|
19
21
|
* `workerPostMessage`), `self`, `postMessage`, `addEventListener` and
|
|
@@ -28,7 +30,15 @@ import { createFetch } from './fetch';
|
|
|
28
30
|
import { createImageGlobals } from './image';
|
|
29
31
|
import { SimpleURL, SimpleURLSearchParams } from './url';
|
|
30
32
|
import { installWorkerScope, NativeWorker } from './worker';
|
|
33
|
+
import { createMediaSession, NativeMediaMetadata } from './mediaSession';
|
|
34
|
+
import { createNotificationGlobals } from './notifications';
|
|
31
35
|
import { receiveOpenURL } from './openURL';
|
|
36
|
+
import { objectURL, SimpleBlob, SimpleFile, SimpleFormData } from './blob';
|
|
37
|
+
import { setBodyRandomness } from './fetch';
|
|
38
|
+
import { NativeWebSocket } from './webSocket';
|
|
39
|
+
import { createSubtleCrypto } from './subtle';
|
|
40
|
+
import { matchMedia } from './matchMedia';
|
|
41
|
+
import { installHostRequests, receiveHostResponse } from './hostRequests';
|
|
32
42
|
|
|
33
43
|
type AnyGlobal = Record<string, unknown>;
|
|
34
44
|
|
|
@@ -170,6 +180,35 @@ export function installRuntime(native: NativeBridge, target: AnyGlobal = globalT
|
|
|
170
180
|
define('URL', SimpleURL);
|
|
171
181
|
define('URLSearchParams', SimpleURLSearchParams);
|
|
172
182
|
|
|
183
|
+
// Bytes with a type, files and forms (blob.ts): what `fetch` uploads, and
|
|
184
|
+
// what a picked file or a built image is. `URL.createObjectURL` is a `data:` URL.
|
|
185
|
+
define('Blob', SimpleBlob);
|
|
186
|
+
define('File', SimpleFile);
|
|
187
|
+
define('FormData', SimpleFormData);
|
|
188
|
+
const urlStatics = target['URL'] as { createObjectURL?: unknown; revokeObjectURL?: unknown };
|
|
189
|
+
if (urlStatics && typeof urlStatics.createObjectURL !== 'function') {
|
|
190
|
+
urlStatics.createObjectURL = (blob: SimpleBlob) => objectURL(blob);
|
|
191
|
+
urlStatics.revokeObjectURL = () => {};
|
|
192
|
+
}
|
|
193
|
+
const random = (count: number): Uint8Array => base64Decode(native.randomBytes(count));
|
|
194
|
+
setBodyRandomness(random);
|
|
195
|
+
|
|
196
|
+
// WebSockets (webSocket.ts) over the host's `URLSessionWebSocketTask`.
|
|
197
|
+
if (native.wsOpen && native.wsSend && native.wsClose) {
|
|
198
|
+
NativeWebSocket.install({ wsOpen: native.wsOpen.bind(native), wsSend: native.wsSend.bind(native), wsClose: native.wsClose.bind(native) });
|
|
199
|
+
define('WebSocket', NativeWebSocket);
|
|
200
|
+
define('__sbWebSocketEvent', NativeWebSocket.receive);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// `matchMedia`: the color scheme the host sends (run.ts), nothing else.
|
|
204
|
+
define('matchMedia', matchMedia);
|
|
205
|
+
|
|
206
|
+
// Host requests (hostRequests.ts): the share sheet, the pickers, the pasteboard, a sign-in.
|
|
207
|
+
if (native.hostRequest && !native.workerPostMessage) {
|
|
208
|
+
installHostRequests({ hostRequest: native.hostRequest.bind(native) });
|
|
209
|
+
define('__sbHostResponse', receiveHostResponse);
|
|
210
|
+
}
|
|
211
|
+
|
|
173
212
|
// `Image`, for code that loads a picture to learn its size (React's
|
|
174
213
|
// `AsyncImage`): the host reads it through its image loader.
|
|
175
214
|
if (native.probeImage && !native.workerPostMessage) {
|
|
@@ -184,6 +223,8 @@ export function installRuntime(native: NativeBridge, target: AnyGlobal = globalT
|
|
|
184
223
|
new Uint8Array(array.buffer, array.byteOffset, array.byteLength).set(bytes.subarray(0, array.byteLength));
|
|
185
224
|
return array;
|
|
186
225
|
},
|
|
226
|
+
// Digests and HMAC on the host's CryptoKit (subtle.ts).
|
|
227
|
+
subtle: createSubtleCrypto(native),
|
|
187
228
|
randomUUID: (): string => {
|
|
188
229
|
const bytes = base64Decode(native.randomBytes(16));
|
|
189
230
|
bytes[6] = (bytes[6]! & 0x0f) | 0x40;
|
|
@@ -207,14 +248,43 @@ export function installRuntime(native: NativeBridge, target: AnyGlobal = globalT
|
|
|
207
248
|
|
|
208
249
|
define('structuredClone', <T>(value: T): T => (value === undefined ? value : (JSON.parse(JSON.stringify(value)) as T)));
|
|
209
250
|
|
|
210
|
-
|
|
251
|
+
const navigator: Record<string, unknown> = {
|
|
211
252
|
userAgent: 'SwiftBrowserNative',
|
|
212
253
|
language: native.locale().replace('_', '-'),
|
|
213
254
|
languages: [native.locale().replace('_', '-')],
|
|
214
255
|
onLine: true,
|
|
215
256
|
platform: 'iOS',
|
|
216
257
|
hardwareConcurrency: 1,
|
|
217
|
-
|
|
258
|
+
// A haptic for a vibration; the pattern's length does not matter.
|
|
259
|
+
vibrate: (pattern: number | number[]): boolean => {
|
|
260
|
+
if (!native.haptic) return false;
|
|
261
|
+
const length = Array.isArray(pattern) ? pattern.reduce((sum, ms) => sum + Number(ms), 0) : Number(pattern);
|
|
262
|
+
if (!(length > 0)) return true;
|
|
263
|
+
native.haptic(length >= 200 ? 'impact' : 'selection');
|
|
264
|
+
return true;
|
|
265
|
+
},
|
|
266
|
+
};
|
|
267
|
+
// What plays, for the lock screen and the media keys (mediaSession.ts); not in a worker.
|
|
268
|
+
const mediaSession = native.mediaSession && !native.workerPostMessage ? createMediaSession(native) : null;
|
|
269
|
+
if (mediaSession) navigator['mediaSession'] = mediaSession.session;
|
|
270
|
+
// Notifications (notifications.ts): the browser's `Notification` and the
|
|
271
|
+
// badge over UserNotifications, and the push registration; not in a worker.
|
|
272
|
+
const notifications = native.notificationShow && !native.workerPostMessage ? createNotificationGlobals(native) : null;
|
|
273
|
+
if (notifications) {
|
|
274
|
+
navigator['setAppBadge'] = notifications.setAppBadge;
|
|
275
|
+
navigator['clearAppBadge'] = notifications.clearAppBadge;
|
|
276
|
+
}
|
|
277
|
+
define('navigator', navigator);
|
|
278
|
+
if (mediaSession) {
|
|
279
|
+
define('MediaMetadata', NativeMediaMetadata);
|
|
280
|
+
define('__sbMediaSessionCommand', mediaSession.receive);
|
|
281
|
+
}
|
|
282
|
+
if (notifications) {
|
|
283
|
+
define('Notification', notifications.Notification);
|
|
284
|
+
define('__sbNotificationPermission', notifications.__sbNotificationPermission);
|
|
285
|
+
define('__sbPushRegistered', notifications.__sbPushRegistered);
|
|
286
|
+
define('__sbNotification', notifications.__sbNotification);
|
|
287
|
+
}
|
|
218
288
|
|
|
219
289
|
if (native.workerPostMessage) {
|
|
220
290
|
installed.push(...installWorkerScope(native, target));
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `secureStorage`: `localStorage`'s shape over the Keychain, for what must
|
|
3
|
+
* not sit in plain text (a session token). The host's `secureGet`,
|
|
4
|
+
* `secureSet` and `secureRemove` are generic-password items of the app's
|
|
5
|
+
* (`kSecAttrAccessibleAfterFirstUnlock`, so a background fetch can read
|
|
6
|
+
* them). In the page (the round trip, or a browser) there is no Keychain:
|
|
7
|
+
* the values go to `localStorage` under a prefix, as a web app keeps them.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { nativeBridge, type NativeBridge } from './bridge';
|
|
11
|
+
|
|
12
|
+
export type SecureStorageBridge = Pick<NativeBridge, 'secureGet' | 'secureSet' | 'secureRemove'>;
|
|
13
|
+
|
|
14
|
+
export interface SecureStorage {
|
|
15
|
+
getItem(key: string): string | null;
|
|
16
|
+
setItem(key: string, value: string): void;
|
|
17
|
+
removeItem(key: string): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const PREFIX = 'swiftbrowser.secure.';
|
|
21
|
+
|
|
22
|
+
/** Secure storage over `native`, or over `localStorage` when the host has no Keychain. */
|
|
23
|
+
export function createSecureStorage(native: SecureStorageBridge | undefined, fallback: () => Storage | undefined): SecureStorage {
|
|
24
|
+
if (native?.secureGet && native.secureSet && native.secureRemove) {
|
|
25
|
+
return {
|
|
26
|
+
getItem: (key) => native.secureGet!(String(key)),
|
|
27
|
+
setItem: (key, value) => native.secureSet!(String(key), String(value)),
|
|
28
|
+
removeItem: (key) => native.secureRemove!(String(key)),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
getItem: (key) => fallback()?.getItem(PREFIX + String(key)) ?? null,
|
|
33
|
+
setItem: (key, value) => fallback()?.setItem(PREFIX + String(key), String(value)),
|
|
34
|
+
removeItem: (key) => fallback()?.removeItem(PREFIX + String(key)),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The app's secure storage: the Keychain in the native app, `localStorage` in the page. */
|
|
39
|
+
export const secureStorage: SecureStorage = createSecureStorage(nativeBridge(), () => (typeof localStorage === 'undefined' ? undefined : localStorage));
|
package/src/subtle.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `crypto.subtle` for the native app, as far as apps use it for a PKCE
|
|
3
|
+
* challenge or a signature: `digest` (SHA-1, SHA-256, SHA-384, SHA-512) and
|
|
4
|
+
* HMAC keys with `importKey`, `sign` and `verify`, over the bridge's
|
|
5
|
+
* `digest` and `hmac` (CryptoKit on the Swift side). Both are synchronous
|
|
6
|
+
* there; the promises resolve at once.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { NativeBridge } from './bridge';
|
|
10
|
+
import { base64Decode, base64Encode } from './encoding';
|
|
11
|
+
|
|
12
|
+
export type SubtleBridge = Pick<NativeBridge, 'digest' | 'hmac'>;
|
|
13
|
+
|
|
14
|
+
type Algorithm = string | { name: string; hash?: string | { name: string } };
|
|
15
|
+
|
|
16
|
+
interface HmacKey {
|
|
17
|
+
type: 'secret';
|
|
18
|
+
extractable: boolean;
|
|
19
|
+
algorithm: { name: 'HMAC'; hash: { name: string }; length: number };
|
|
20
|
+
usages: string[];
|
|
21
|
+
/** The key's bytes, kept here: the host signs with them each time. */
|
|
22
|
+
readonly bytes: Uint8Array;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const DIGESTS = new Set(['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']);
|
|
26
|
+
|
|
27
|
+
function algorithmName(algorithm: Algorithm): string {
|
|
28
|
+
return (typeof algorithm === 'string' ? algorithm : algorithm.name).toUpperCase();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function hashName(algorithm: Algorithm): string {
|
|
32
|
+
if (typeof algorithm === 'string') return algorithm.toUpperCase();
|
|
33
|
+
const hash = algorithm.hash;
|
|
34
|
+
return (typeof hash === 'string' ? hash : hash?.name ?? '').toUpperCase();
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function bytesOf(data: ArrayBuffer | ArrayBufferView): Uint8Array {
|
|
38
|
+
return data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function toBuffer(base64: string): ArrayBuffer {
|
|
42
|
+
const bytes = base64Decode(base64);
|
|
43
|
+
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) as ArrayBuffer;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function notSupported(what: string): Error {
|
|
47
|
+
const error = new Error(`crypto.subtle: ${what} is not supported in the native app`);
|
|
48
|
+
error.name = 'NotSupportedError';
|
|
49
|
+
return error;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** `crypto.subtle` over the bridge. */
|
|
53
|
+
export function createSubtleCrypto(native: SubtleBridge) {
|
|
54
|
+
return {
|
|
55
|
+
async digest(algorithm: Algorithm, data: ArrayBuffer | ArrayBufferView): Promise<ArrayBuffer> {
|
|
56
|
+
const name = algorithmName(algorithm);
|
|
57
|
+
if (!DIGESTS.has(name)) throw notSupported(`the ${name} digest`);
|
|
58
|
+
const result = native.digest?.(name, base64Encode(bytesOf(data)));
|
|
59
|
+
if (!result) throw notSupported(`the ${name} digest`);
|
|
60
|
+
return toBuffer(result);
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
async importKey(format: string, keyData: ArrayBuffer | ArrayBufferView, algorithm: Algorithm, extractable: boolean, usages: string[]): Promise<HmacKey> {
|
|
64
|
+
if (format !== 'raw' || algorithmName(algorithm) !== 'HMAC') throw notSupported(`importing a ${format} ${algorithmName(algorithm)} key`);
|
|
65
|
+
const hash = hashName(algorithm);
|
|
66
|
+
if (!DIGESTS.has(hash)) throw notSupported(`HMAC with ${hash}`);
|
|
67
|
+
const bytes = bytesOf(keyData);
|
|
68
|
+
return { type: 'secret', extractable, algorithm: { name: 'HMAC', hash: { name: hash }, length: bytes.byteLength * 8 }, usages: [...usages], bytes };
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
async sign(algorithm: Algorithm, key: HmacKey, data: ArrayBuffer | ArrayBufferView): Promise<ArrayBuffer> {
|
|
72
|
+
if (algorithmName(algorithm) !== 'HMAC' || key.algorithm?.name !== 'HMAC') throw notSupported(`signing with ${algorithmName(algorithm)}`);
|
|
73
|
+
const result = native.hmac?.(key.algorithm.hash.name, base64Encode(key.bytes), base64Encode(bytesOf(data)));
|
|
74
|
+
if (!result) throw notSupported('HMAC');
|
|
75
|
+
return toBuffer(result);
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
async verify(algorithm: Algorithm, key: HmacKey, signature: ArrayBuffer | ArrayBufferView, data: ArrayBuffer | ArrayBufferView): Promise<boolean> {
|
|
79
|
+
const expected = new Uint8Array(await this.sign(algorithm, key, data));
|
|
80
|
+
const given = bytesOf(signature);
|
|
81
|
+
if (expected.byteLength !== given.byteLength) return false;
|
|
82
|
+
let difference = 0;
|
|
83
|
+
for (let i = 0; i < expected.byteLength; i += 1) difference |= expected[i]! ^ given[i]!;
|
|
84
|
+
return difference === 0;
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|