@swiftbrowser/native 0.17.7 → 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.
Files changed (70) hide show
  1. package/README.md +79 -2
  2. package/dist/blob.d.ts +60 -0
  3. package/dist/blob.d.ts.map +1 -0
  4. package/dist/blob.js +153 -0
  5. package/dist/blob.js.map +1 -0
  6. package/dist/bridge.d.ts +39 -1
  7. package/dist/bridge.d.ts.map +1 -1
  8. package/dist/bridge.js.map +1 -1
  9. package/dist/fetch.d.ts +11 -3
  10. package/dist/fetch.d.ts.map +1 -1
  11. package/dist/fetch.js +26 -2
  12. package/dist/fetch.js.map +1 -1
  13. package/dist/hostRequests.d.ts +49 -0
  14. package/dist/hostRequests.d.ts.map +1 -0
  15. package/dist/hostRequests.js +134 -0
  16. package/dist/hostRequests.js.map +1 -0
  17. package/dist/index.d.ts +7 -0
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +7 -0
  20. package/dist/index.js.map +1 -1
  21. package/dist/matchMedia.d.ts +43 -0
  22. package/dist/matchMedia.d.ts.map +1 -0
  23. package/dist/matchMedia.js +63 -0
  24. package/dist/matchMedia.js.map +1 -0
  25. package/dist/notifications.d.ts +108 -0
  26. package/dist/notifications.d.ts.map +1 -0
  27. package/dist/notifications.js +251 -0
  28. package/dist/notifications.js.map +1 -0
  29. package/dist/run.d.ts.map +1 -1
  30. package/dist/run.js +9 -1
  31. package/dist/run.js.map +1 -1
  32. package/dist/runtime.d.ts +1 -0
  33. package/dist/runtime.d.ts.map +1 -1
  34. package/dist/runtime.js +58 -0
  35. package/dist/runtime.js.map +1 -1
  36. package/dist/secureStorage.d.ts +20 -0
  37. package/dist/secureStorage.d.ts.map +1 -0
  38. package/dist/secureStorage.js +28 -0
  39. package/dist/secureStorage.js.map +1 -0
  40. package/dist/subtle.d.ts +38 -0
  41. package/dist/subtle.d.ts.map +1 -0
  42. package/dist/subtle.js +72 -0
  43. package/dist/subtle.js.map +1 -0
  44. package/dist/webSocket.d.ts +73 -0
  45. package/dist/webSocket.d.ts.map +1 -0
  46. package/dist/webSocket.js +145 -0
  47. package/dist/webSocket.js.map +1 -0
  48. package/package.json +3 -3
  49. package/src/blob.ts +186 -0
  50. package/src/bridge.ts +49 -1
  51. package/src/fetch.ts +29 -3
  52. package/src/hostRequests.ts +148 -0
  53. package/src/index.ts +7 -0
  54. package/src/matchMedia.ts +79 -0
  55. package/src/notifications.ts +317 -0
  56. package/src/run.ts +10 -1
  57. package/src/runtime.ts +60 -0
  58. package/src/secureStorage.ts +39 -0
  59. package/src/subtle.ts +87 -0
  60. package/src/webSocket.ts +175 -0
  61. package/swift/Sources/SwiftBrowserNative/Host/HostBridge.swift +166 -0
  62. package/swift/Sources/SwiftBrowserNative/Host/HostRequestBridge.swift +437 -0
  63. package/swift/Sources/SwiftBrowserNative/Host/NotificationBridge.swift +304 -0
  64. package/swift/Sources/SwiftBrowserNative/Host/SecureStorage.swift +60 -0
  65. package/swift/Sources/SwiftBrowserNative/Host/ShimAliases.swift +1 -0
  66. package/swift/Sources/SwiftBrowserNative/Host/WebSocketBridge.swift +118 -0
  67. package/swift/Sources/SwiftBrowserNative/Runtime/NativeApp.swift +26 -0
  68. package/swift/Sources/SwiftBrowserNative/Runtime/NativeAppDelegate.swift +36 -0
  69. package/swift/Sources/SwiftBrowserNative/Views/Kinds/Controls.swift +16 -5
  70. package/wasm/SwiftBrowserNative.wasm +2 -2
package/dist/subtle.js ADDED
@@ -0,0 +1,72 @@
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
+ import { base64Decode, base64Encode } from './encoding';
9
+ const DIGESTS = new Set(['SHA-1', 'SHA-256', 'SHA-384', 'SHA-512']);
10
+ function algorithmName(algorithm) {
11
+ return (typeof algorithm === 'string' ? algorithm : algorithm.name).toUpperCase();
12
+ }
13
+ function hashName(algorithm) {
14
+ if (typeof algorithm === 'string')
15
+ return algorithm.toUpperCase();
16
+ const hash = algorithm.hash;
17
+ return (typeof hash === 'string' ? hash : hash?.name ?? '').toUpperCase();
18
+ }
19
+ function bytesOf(data) {
20
+ return data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
21
+ }
22
+ function toBuffer(base64) {
23
+ const bytes = base64Decode(base64);
24
+ return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
25
+ }
26
+ function notSupported(what) {
27
+ const error = new Error(`crypto.subtle: ${what} is not supported in the native app`);
28
+ error.name = 'NotSupportedError';
29
+ return error;
30
+ }
31
+ /** `crypto.subtle` over the bridge. */
32
+ export function createSubtleCrypto(native) {
33
+ return {
34
+ async digest(algorithm, data) {
35
+ const name = algorithmName(algorithm);
36
+ if (!DIGESTS.has(name))
37
+ throw notSupported(`the ${name} digest`);
38
+ const result = native.digest?.(name, base64Encode(bytesOf(data)));
39
+ if (!result)
40
+ throw notSupported(`the ${name} digest`);
41
+ return toBuffer(result);
42
+ },
43
+ async importKey(format, keyData, algorithm, extractable, usages) {
44
+ if (format !== 'raw' || algorithmName(algorithm) !== 'HMAC')
45
+ throw notSupported(`importing a ${format} ${algorithmName(algorithm)} key`);
46
+ const hash = hashName(algorithm);
47
+ if (!DIGESTS.has(hash))
48
+ throw notSupported(`HMAC with ${hash}`);
49
+ const bytes = bytesOf(keyData);
50
+ return { type: 'secret', extractable, algorithm: { name: 'HMAC', hash: { name: hash }, length: bytes.byteLength * 8 }, usages: [...usages], bytes };
51
+ },
52
+ async sign(algorithm, key, data) {
53
+ if (algorithmName(algorithm) !== 'HMAC' || key.algorithm?.name !== 'HMAC')
54
+ throw notSupported(`signing with ${algorithmName(algorithm)}`);
55
+ const result = native.hmac?.(key.algorithm.hash.name, base64Encode(key.bytes), base64Encode(bytesOf(data)));
56
+ if (!result)
57
+ throw notSupported('HMAC');
58
+ return toBuffer(result);
59
+ },
60
+ async verify(algorithm, key, signature, data) {
61
+ const expected = new Uint8Array(await this.sign(algorithm, key, data));
62
+ const given = bytesOf(signature);
63
+ if (expected.byteLength !== given.byteLength)
64
+ return false;
65
+ let difference = 0;
66
+ for (let i = 0; i < expected.byteLength; i += 1)
67
+ difference |= expected[i] ^ given[i];
68
+ return difference === 0;
69
+ },
70
+ };
71
+ }
72
+ //# sourceMappingURL=subtle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"subtle.js","sourceRoot":"","sources":["../src/subtle.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAexD,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;AAEpE,SAAS,aAAa,CAAC,SAAoB;IACzC,OAAO,CAAC,OAAO,SAAS,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;AACpF,CAAC;AAED,SAAS,QAAQ,CAAC,SAAoB;IACpC,IAAI,OAAO,SAAS,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC,WAAW,EAAE,CAAC;IAClE,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC;IAC5B,OAAO,CAAC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;AAC5E,CAAC;AAED,SAAS,OAAO,CAAC,IAAmC;IAClD,OAAO,IAAI,YAAY,WAAW,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,IAA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,QAAQ,GAAG,IAAI,UAAU,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC;YACvE,MAAM,KAAK,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC;YACjC,IAAI,QAAQ,CAAC,UAAU,KAAK,KAAK,CAAC,UAAU;gBAAE,OAAO,KAAK,CAAC;YAC3D,IAAI,UAAU,GAAG,CAAC,CAAC;YACnB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,UAAU,EAAE,CAAC,IAAI,CAAC;gBAAE,UAAU,IAAI,QAAQ,CAAC,CAAC,CAAE,GAAG,KAAK,CAAC,CAAC,CAAE,CAAC;YACxF,OAAO,UAAU,KAAK,CAAC,CAAC;QAC1B,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * `WebSocket` for the native app over the bridge's `wsOpen`, `wsSend` and
3
+ * `wsClose` (`URLSessionWebSocketTask` on the Swift side). What the socket
4
+ * receives arrives at `__sbWebSocketEvent(id, json)`: `{ type: "open",
5
+ * protocol }`, `{ type: "message", text }` or `{ type: "message", binary }`
6
+ * (base64), `{ type: "error", message }` and `{ type: "close", code, reason,
7
+ * wasClean }`. The browser's event model as apps use it: `onopen`,
8
+ * `onmessage`, `onerror`, `onclose`, `addEventListener`, `readyState`,
9
+ * `binaryType` (`blob` by default, or `arraybuffer`).
10
+ */
11
+ import { SimpleBlob } from './blob';
12
+ import type { NativeBridge } from './bridge';
13
+ export interface WebSocketEventLike {
14
+ type: string;
15
+ target: NativeWebSocket;
16
+ data?: unknown;
17
+ code?: number;
18
+ reason?: string;
19
+ wasClean?: boolean;
20
+ message?: string;
21
+ }
22
+ type Handler = (event: WebSocketEventLike) => void;
23
+ /** The bridge's event for socket `id`. */
24
+ export interface NativeWebSocketEvent {
25
+ type: 'open' | 'message' | 'error' | 'close';
26
+ protocol?: string;
27
+ text?: string;
28
+ /** Base64. */
29
+ binary?: string;
30
+ message?: string;
31
+ code?: number;
32
+ reason?: string;
33
+ wasClean?: boolean;
34
+ }
35
+ export type WebSocketBridge = Required<Pick<NativeBridge, 'wsOpen' | 'wsSend' | 'wsClose'>>;
36
+ export declare class NativeWebSocket {
37
+ static readonly CONNECTING = 0;
38
+ static readonly OPEN = 1;
39
+ static readonly CLOSING = 2;
40
+ static readonly CLOSED = 3;
41
+ readonly CONNECTING = 0;
42
+ readonly OPEN = 1;
43
+ readonly CLOSING = 2;
44
+ readonly CLOSED = 3;
45
+ readonly url: string;
46
+ readyState: number;
47
+ protocol: string;
48
+ extensions: string;
49
+ bufferedAmount: number;
50
+ binaryType: 'blob' | 'arraybuffer';
51
+ onopen: Handler | null;
52
+ onmessage: Handler | null;
53
+ onerror: Handler | null;
54
+ onclose: Handler | null;
55
+ private readonly id;
56
+ private readonly listeners;
57
+ constructor(url: string | {
58
+ href: string;
59
+ }, protocols?: string | string[]);
60
+ send(data: string | ArrayBuffer | ArrayBufferView | SimpleBlob): void;
61
+ close(code?: number, reason?: string): void;
62
+ addEventListener(type: string, handler: Handler): void;
63
+ removeEventListener(type: string, handler: Handler): void;
64
+ dispatchEvent(event: WebSocketEventLike): boolean;
65
+ /** The bridge's event for this socket. */
66
+ receive(event: NativeWebSocketEvent): void;
67
+ /** The bridge's `__sbWebSocketEvent(id, json)`. */
68
+ static receive(id: number, json: string | NativeWebSocketEvent): void;
69
+ /** Points the sockets at the bridge (the runtime, once per install). */
70
+ static install(native: WebSocketBridge): void;
71
+ }
72
+ export {};
73
+ //# sourceMappingURL=webSocket.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webSocket.d.ts","sourceRoot":"","sources":["../src/webSocket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAG7C,MAAM,WA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AU,SAAM;IAChB,cAAc,SAAK;IACnB,UAAU,EAAE,MAAM,GAAG,aAAa,CAAU;IAC5C,MAAM,EAAE,OAAO,GAAG,IAAI,CAAQ;IAC9B,SAAS,EAAE,OAAO,GAAG,IAAI,CAAQ;IACjC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAQ;IAC/B,OAAO,EAAE,OAAO,GAAG,IAAI,CAAQ;IAE/B,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAmC;gBAEjD,GAAG,EAAE,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAA;KAAE,EAAE,SAAS,GAAE,MAAM,GAAG,MAAM,EAAO;IAU7E,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,WAAW,GAAG,eAAe,GAAG,UAAU,GAAG,IAAI;IAWrE,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI;IAO3C,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;IAStD,mBAAmB,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;IAIzD,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO;IAOjD,0CAA0C;IAC1C,OAAO,CAAC,KAAK,EAAE,oBAAoB,GAAG,IAAI;IA8B1C,mDAAmD;IACnD,MAAM,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,oBAAoB,GAAG,IAAI;IAYrE,wEAAwE;IACxE,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI;CAG9C"}
@@ -0,0 +1,145 @@
1
+ /**
2
+ * `WebSocket` for the native app over the bridge's `wsOpen`, `wsSend` and
3
+ * `wsClose` (`URLSessionWebSocketTask` on the Swift side). What the socket
4
+ * receives arrives at `__sbWebSocketEvent(id, json)`: `{ type: "open",
5
+ * protocol }`, `{ type: "message", text }` or `{ type: "message", binary }`
6
+ * (base64), `{ type: "error", message }` and `{ type: "close", code, reason,
7
+ * wasClean }`. The browser's event model as apps use it: `onopen`,
8
+ * `onmessage`, `onerror`, `onclose`, `addEventListener`, `readyState`,
9
+ * `binaryType` (`blob` by default, or `arraybuffer`).
10
+ */
11
+ import { SimpleBlob } from './blob';
12
+ import { base64Decode, base64Encode } from './encoding';
13
+ const sockets = new Map();
14
+ let nextId = 1;
15
+ let bridge = null;
16
+ export class NativeWebSocket {
17
+ static CONNECTING = 0;
18
+ static OPEN = 1;
19
+ static CLOSING = 2;
20
+ static CLOSED = 3;
21
+ CONNECTING = 0;
22
+ OPEN = 1;
23
+ CLOSING = 2;
24
+ CLOSED = 3;
25
+ url;
26
+ readyState = 0;
27
+ protocol = '';
28
+ extensions = '';
29
+ bufferedAmount = 0;
30
+ binaryType = 'blob';
31
+ onopen = null;
32
+ onmessage = null;
33
+ onerror = null;
34
+ onclose = null;
35
+ id;
36
+ listeners = new Map();
37
+ constructor(url, protocols = []) {
38
+ if (!bridge)
39
+ throw new TypeError('WebSocket: no native bridge');
40
+ const href = typeof url === 'string' ? url : url.href;
41
+ if (!/^wss?:\/\//i.test(href))
42
+ throw new SyntaxError(`WebSocket: the URL's scheme must be ws or wss: ${href}`);
43
+ this.url = href;
44
+ this.id = nextId++;
45
+ sockets.set(this.id, this);
46
+ bridge.wsOpen(this.id, href, typeof protocols === 'string' ? [protocols] : [...protocols]);
47
+ }
48
+ send(data) {
49
+ if (this.readyState === NativeWebSocket.CONNECTING)
50
+ throw new DOMExceptionLike('InvalidStateError', 'WebSocket: still connecting');
51
+ if (this.readyState !== NativeWebSocket.OPEN)
52
+ return;
53
+ if (typeof data === 'string') {
54
+ bridge?.wsSend(this.id, JSON.stringify({ text: data }));
55
+ return;
56
+ }
57
+ const bytes = data instanceof SimpleBlob ? data.bytes() : data instanceof ArrayBuffer ? new Uint8Array(data) : new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
58
+ bridge?.wsSend(this.id, JSON.stringify({ binary: base64Encode(bytes) }));
59
+ }
60
+ close(code, reason) {
61
+ if (code !== undefined && code !== 1000 && !(code >= 3000 && code <= 4999))
62
+ throw new DOMExceptionLike('InvalidAccessError', 'WebSocket: the close code must be 1000 or in 3000–4999');
63
+ if (this.readyState === NativeWebSocket.CLOSING || this.readyState === NativeWebSocket.CLOSED)
64
+ return;
65
+ this.readyState = NativeWebSocket.CLOSING;
66
+ bridge?.wsClose(this.id, code ?? 1000, reason ?? '');
67
+ }
68
+ addEventListener(type, handler) {
69
+ let set = this.listeners.get(type);
70
+ if (!set) {
71
+ set = new Set();
72
+ this.listeners.set(type, set);
73
+ }
74
+ set.add(handler);
75
+ }
76
+ removeEventListener(type, handler) {
77
+ this.listeners.get(type)?.delete(handler);
78
+ }
79
+ dispatchEvent(event) {
80
+ const own = { open: this.onopen, message: this.onmessage, error: this.onerror, close: this.onclose }[event.type];
81
+ own?.(event);
82
+ for (const handler of this.listeners.get(event.type) ?? [])
83
+ handler(event);
84
+ return true;
85
+ }
86
+ /** The bridge's event for this socket. */
87
+ receive(event) {
88
+ switch (event.type) {
89
+ case 'open':
90
+ if (this.readyState !== NativeWebSocket.CONNECTING)
91
+ return;
92
+ this.readyState = NativeWebSocket.OPEN;
93
+ this.protocol = event.protocol ?? '';
94
+ this.dispatchEvent({ type: 'open', target: this });
95
+ return;
96
+ case 'message': {
97
+ if (this.readyState !== NativeWebSocket.OPEN)
98
+ return;
99
+ let data = event.text ?? '';
100
+ if (event.binary !== undefined) {
101
+ const bytes = base64Decode(event.binary);
102
+ data = this.binaryType === 'arraybuffer' ? bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength) : new SimpleBlob([bytes]);
103
+ }
104
+ this.dispatchEvent({ type: 'message', target: this, data });
105
+ return;
106
+ }
107
+ case 'error':
108
+ this.dispatchEvent({ type: 'error', target: this, message: event.message ?? '' });
109
+ return;
110
+ case 'close':
111
+ if (this.readyState === NativeWebSocket.CLOSED)
112
+ return;
113
+ this.readyState = NativeWebSocket.CLOSED;
114
+ sockets.delete(this.id);
115
+ this.dispatchEvent({ type: 'close', target: this, code: event.code ?? 1006, reason: event.reason ?? '', wasClean: event.wasClean ?? false });
116
+ return;
117
+ }
118
+ }
119
+ /** The bridge's `__sbWebSocketEvent(id, json)`. */
120
+ static receive(id, json) {
121
+ const socket = sockets.get(id);
122
+ if (!socket)
123
+ return;
124
+ let event;
125
+ try {
126
+ event = typeof json === 'string' ? JSON.parse(json) : json;
127
+ }
128
+ catch {
129
+ return;
130
+ }
131
+ socket.receive(event);
132
+ }
133
+ /** Points the sockets at the bridge (the runtime, once per install). */
134
+ static install(native) {
135
+ bridge = native;
136
+ }
137
+ }
138
+ /** A `DOMException` stand-in: an error with the browser's name. */
139
+ class DOMExceptionLike extends Error {
140
+ constructor(name, message) {
141
+ super(message);
142
+ this.name = name;
143
+ }
144
+ }
145
+ //# sourceMappingURL=webSocket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"webSocket.js","sourceRoot":"","sources":["../src/webSocket.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAEpC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AA6BxD,MAAM,OAAO,GAAG,IAAI,GAAG,EAA2B,CAAC;AACnD,IAAI,MAAM,GAAG,CAAC,CAAC;AACf,IAAI,MAAM,GAA2B,IAAI,CAAC;AAE1C,MAAM,OAAO,eAAe;IAC1B,MAAM,CAAU,UAAU,GAAG,CAAC,CAAC;IAC/B,MAAM,CAAU,IAAI,GAAG,CAAC,CAAC;IACzB,MAAM,CAAU,OAAO,GAAG,CAAC,CAAC;IAC5B,MAAM,CAAU,MAAM,GAAG,CAAC,CAAC;IAClB,UAAU,GAAG,CAAC,CAAC;IACf,IAAI,GAAG,CAAC,CAAC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OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,IAAI,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,IAAI,EAAE,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,IAAI,KAAK,EAAE,CAAC,CAAC;gBAC7I,OAAO;QACX,CAAC;IACH,CAAC;IAED,mDAAmD;IACnD,MAAM,CAAC,OAAO,CAAC,EAAU,EAAE,IAAmC;QAC5D,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC/B,IAAI,CAAC,MAAM;YAAE,OAAO;QACpB,IAAI,KAA2B,CAAC;QAChC,IAAI,CAAC;YACH,KAAK,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAA0B,CAAC,CAAC,CAAC,IAAI,CAAC;QACvF,CAAC;QAAC,MAAM,CAAC;YACP,OAAO;QACT,CAAC;QACD,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IACxB,CAAC;IAED,wEAAwE;IACxE,MAAM,CAAC,OAAO,CAAC,MAAuB;QACpC,MAAM,GAAG,MAAM,CAAC;IAClB,CAAC;;AAGH,mEAAmE;AACnE,MAAM,gBAAiB,SAAQ,KAAK;IAClC,YAAY,IAAY,EAAE,OAAe;QACvC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swiftbrowser/native",
3
- "version": "0.17.7",
3
+ "version": "0.17.8",
4
4
  "type": "module",
5
5
  "description": "Run a @swiftbrowser/react app natively: the runtime a JavaScriptCore-hosted app needs (timers, console, fetch and the other web globals over the SwiftBrowser Native bridge), the `run` entry that renders into the native SwiftUI renderer, and the bundler that builds app.js for the Xcode target.",
6
6
  "repository": {
@@ -59,8 +59,8 @@
59
59
  },
60
60
  "dependencies": {
61
61
  "@babel/core": "^7.29.7",
62
- "@swiftbrowser/react": "0.17.7",
63
- "@swiftbrowser/web": "0.17.7",
62
+ "@swiftbrowser/react": "0.17.8",
63
+ "@swiftbrowser/web": "0.17.8",
64
64
  "esbuild": "^0.28.2",
65
65
  "react-refresh": "^0.17.0"
66
66
  },
package/src/blob.ts ADDED
@@ -0,0 +1,186 @@
1
+ /**
2
+ * `Blob`, `File` and `FormData` for the native app: bytes with a type, a
3
+ * named one, and a form whose entries `fetch` sends as multipart. A body is
4
+ * still whole (no streams); `URL.createObjectURL` gives a `data:` URL, which
5
+ * the host's image loader and `URLSession` both read, and
6
+ * `revokeObjectURL` has nothing to free.
7
+ */
8
+
9
+ import { base64Encode, utf8Decode, utf8Encode } from './encoding';
10
+
11
+ export type BlobPartLike = string | ArrayBuffer | ArrayBufferView | SimpleBlob;
12
+
13
+ export interface BlobOptions {
14
+ type?: string;
15
+ }
16
+
17
+ export interface FileOptions extends BlobOptions {
18
+ lastModified?: number;
19
+ }
20
+
21
+ function partBytes(part: BlobPartLike): Uint8Array {
22
+ if (typeof part === 'string') return utf8Encode(part);
23
+ if (part instanceof SimpleBlob) return part.bytes();
24
+ if (part instanceof ArrayBuffer) return new Uint8Array(part.slice(0));
25
+ if (ArrayBuffer.isView(part)) return new Uint8Array(part.buffer.slice(part.byteOffset, part.byteOffset + part.byteLength));
26
+ return utf8Encode(String(part));
27
+ }
28
+
29
+ function concat(parts: Uint8Array[]): Uint8Array {
30
+ const total = parts.reduce((sum, part) => sum + part.byteLength, 0);
31
+ const out = new Uint8Array(total);
32
+ let offset = 0;
33
+ for (const part of parts) {
34
+ out.set(part, offset);
35
+ offset += part.byteLength;
36
+ }
37
+ return out;
38
+ }
39
+
40
+ /** The browser's `Blob`: bytes and a MIME type. */
41
+ export class SimpleBlob {
42
+ private readonly data: Uint8Array;
43
+ readonly type: string;
44
+
45
+ constructor(parts: Iterable<BlobPartLike> = [], options: BlobOptions = {}) {
46
+ this.data = concat([...parts].map(partBytes));
47
+ // As the browser does: lowercased, and empty when it is not printable ASCII.
48
+ const type = options.type ?? '';
49
+ this.type = /^[\x20-\x7e]*$/.test(type) ? type.toLowerCase() : '';
50
+ }
51
+
52
+ get size(): number {
53
+ return this.data.byteLength;
54
+ }
55
+
56
+ /** The bytes (a view; callers must not write into it). */
57
+ bytes(): Uint8Array {
58
+ return this.data;
59
+ }
60
+
61
+ slice(start = 0, end = this.size, contentType = ''): SimpleBlob {
62
+ const from = start < 0 ? Math.max(this.size + start, 0) : Math.min(start, this.size);
63
+ const to = end < 0 ? Math.max(this.size + end, 0) : Math.min(end, this.size);
64
+ return new SimpleBlob([this.data.subarray(from, Math.max(from, to))], { type: contentType });
65
+ }
66
+
67
+ async arrayBuffer(): Promise<ArrayBuffer> {
68
+ return this.data.buffer.slice(this.data.byteOffset, this.data.byteOffset + this.data.byteLength) as ArrayBuffer;
69
+ }
70
+
71
+ async text(): Promise<string> {
72
+ return utf8Decode(this.data);
73
+ }
74
+
75
+ get [Symbol.toStringTag](): string {
76
+ return 'Blob';
77
+ }
78
+ }
79
+
80
+ /** The browser's `File`: a `Blob` with a name. */
81
+ export class SimpleFile extends SimpleBlob {
82
+ readonly name: string;
83
+ readonly lastModified: number;
84
+
85
+ constructor(parts: Iterable<BlobPartLike>, name: string, options: FileOptions = {}) {
86
+ super(parts, options);
87
+ this.name = String(name);
88
+ this.lastModified = options.lastModified ?? Date.now();
89
+ }
90
+
91
+ override get [Symbol.toStringTag](): string {
92
+ return 'File';
93
+ }
94
+ }
95
+
96
+ export type FormDataEntryValueLike = string | SimpleFile;
97
+
98
+ /** The browser's `FormData`, as far as a request body needs it. */
99
+ export class SimpleFormData {
100
+ private entries_: [string, FormDataEntryValueLike][] = [];
101
+
102
+ private static value(value: unknown, filename?: string): FormDataEntryValueLike {
103
+ if (value instanceof SimpleBlob) {
104
+ const name = filename ?? (value instanceof SimpleFile ? value.name : 'blob');
105
+ return value instanceof SimpleFile && filename === undefined ? value : new SimpleFile([value], name, { type: value.type });
106
+ }
107
+ return String(value);
108
+ }
109
+
110
+ append(name: string, value: unknown, filename?: string): void {
111
+ this.entries_.push([String(name), SimpleFormData.value(value, filename)]);
112
+ }
113
+
114
+ set(name: string, value: unknown, filename?: string): void {
115
+ const key = String(name);
116
+ const entry: [string, FormDataEntryValueLike] = [key, SimpleFormData.value(value, filename)];
117
+ const index = this.entries_.findIndex(([entryName]) => entryName === key);
118
+ if (index < 0) {
119
+ this.entries_.push(entry);
120
+ return;
121
+ }
122
+ this.entries_ = this.entries_.filter(([entryName], position) => position === index || entryName !== key);
123
+ this.entries_[index] = entry;
124
+ }
125
+
126
+ get(name: string): FormDataEntryValueLike | null {
127
+ return this.entries_.find(([entryName]) => entryName === String(name))?.[1] ?? null;
128
+ }
129
+
130
+ getAll(name: string): FormDataEntryValueLike[] {
131
+ return this.entries_.filter(([entryName]) => entryName === String(name)).map(([, value]) => value);
132
+ }
133
+
134
+ has(name: string): boolean {
135
+ return this.entries_.some(([entryName]) => entryName === String(name));
136
+ }
137
+
138
+ delete(name: string): void {
139
+ this.entries_ = this.entries_.filter(([entryName]) => entryName !== String(name));
140
+ }
141
+
142
+ *entries(): IterableIterator<[string, FormDataEntryValueLike]> {
143
+ yield* this.entries_;
144
+ }
145
+
146
+ *keys(): IterableIterator<string> {
147
+ for (const [name] of this.entries_) yield name;
148
+ }
149
+
150
+ *values(): IterableIterator<FormDataEntryValueLike> {
151
+ for (const [, value] of this.entries_) yield value;
152
+ }
153
+
154
+ forEach(callback: (value: FormDataEntryValueLike, name: string, form: SimpleFormData) => void): void {
155
+ for (const [name, value] of this.entries_) callback(value, name, this);
156
+ }
157
+
158
+ [Symbol.iterator](): IterableIterator<[string, FormDataEntryValueLike]> {
159
+ return this.entries();
160
+ }
161
+
162
+ get [Symbol.toStringTag](): string {
163
+ return 'FormData';
164
+ }
165
+ }
166
+
167
+ /** A multipart/form-data body for the form, with the boundary `random` makes unique. */
168
+ export function encodeMultipart(form: SimpleFormData, random: (count: number) => Uint8Array): { bytes: Uint8Array; contentType: string } {
169
+ const boundary = `----SwiftBrowserFormBoundary${Array.from(random(16), (byte) => byte.toString(16).padStart(2, '0')).join('')}`;
170
+ const escape = (text: string): string => text.replace(/\r/g, '%0D').replace(/\n/g, '%0A').replace(/"/g, '%22');
171
+ const parts: Uint8Array[] = [];
172
+ for (const [name, value] of form.entries()) {
173
+ const head = [`--${boundary}`, `Content-Disposition: form-data; name="${escape(name)}"` + (typeof value === 'string' ? '' : `; filename="${escape(value.name)}"`)];
174
+ if (typeof value !== 'string') head.push(`Content-Type: ${value.type || 'application/octet-stream'}`);
175
+ parts.push(utf8Encode(`${head.join('\r\n')}\r\n\r\n`));
176
+ parts.push(typeof value === 'string' ? utf8Encode(value) : value.bytes());
177
+ parts.push(utf8Encode('\r\n'));
178
+ }
179
+ parts.push(utf8Encode(`--${boundary}--\r\n`));
180
+ return { bytes: concat(parts), contentType: `multipart/form-data; boundary=${boundary}` };
181
+ }
182
+
183
+ /** `URL.createObjectURL`: a `data:` URL holding the blob. */
184
+ export function objectURL(blob: SimpleBlob): string {
185
+ return `data:${blob.type || 'application/octet-stream'};base64,${base64Encode(blob.bytes())}`;
186
+ }
package/src/bridge.ts CHANGED
@@ -3,7 +3,9 @@
3
3
  * (Native/Sources/SwiftBrowserNative/Host/HostBridge.swift). Every call is
4
4
  * synchronous; the ones that answer later do so by calling a global this
5
5
  * package defines (`__sbTimerFired`, `__sbFetchResult`, `__sbImageProbed`,
6
- * `__sbWorkerMessage`, `__sbOpenURL`, `__sbMediaSessionCommand`).
6
+ * `__sbWorkerMessage`, `__sbOpenURL`, `__sbMediaSessionCommand`,
7
+ * `__sbNotificationPermission`, `__sbPushRegistered`, `__sbNotification`,
8
+ * `__sbWebSocketEvent`, `__sbHostResponse`).
7
9
  */
8
10
  export interface NativeBridge {
9
11
  /** Applies a render pass's ops (a JSON array). Returns an error message, or nothing. */
@@ -44,6 +46,52 @@ export interface NativeBridge {
44
46
  */
45
47
  mediaSession?(update: string): void;
46
48
 
49
+ // Notifications (notifications.ts), on UserNotifications. Hosts before 0.20 lack them.
50
+ /** The app listens: what came before it did (a tap that launched it) arrives at `__sbNotification(json)`. */
51
+ notificationsReady?(): void;
52
+ /** Answers `__sbNotificationPermission(id, 'default' | 'granted' | 'denied')`, after asking the viewer when `request` is set. */
53
+ notificationPermission?(id: number, request: boolean): void;
54
+ /** Shows `{ id, title, subtitle?, body?, data?, badge?, silent? }` now; returns what was wrong with it, or nothing. */
55
+ notificationShow?(json: string): string | null | undefined;
56
+ /** Closes the notifications with these identifiers (a JSON array). */
57
+ notificationClose?(json: string): void;
58
+ /** The app badge (0 clears it). */
59
+ setBadge?(count: number): void;
60
+ /**
61
+ * Registers for remote notifications; the device token (hex) or the
62
+ * failure answers `__sbPushRegistered(id, { token } | { error })`.
63
+ * `applicationServerKey` is for the page (the round trip's Web Push).
64
+ */
65
+ pushRegister?(id: number, applicationServerKey?: string): void;
66
+
67
+ // WebSockets (webSocket.ts) on `URLSessionWebSocketTask`; events come to `__sbWebSocketEvent(id, json)`. Hosts before 0.20 lack them.
68
+ wsOpen?(id: number, url: string, protocols: string[]): void;
69
+ /** `{ text }` or `{ binary }` (base64), as JSON. */
70
+ wsSend?(id: number, json: string): void;
71
+ wsClose?(id: number, code: number, reason: string): void;
72
+
73
+ // `crypto.subtle` (subtle.ts) on CryptoKit, synchronously. Hosts before 0.20 lack them.
74
+ /** The base64 digest of base64 `data` under `algorithm` (`SHA-256`, …), or nothing when the host cannot. */
75
+ digest?(algorithm: string, data: string): string | null | undefined;
76
+ /** The base64 HMAC of base64 `data` with base64 `key` under the hash `algorithm`, or nothing. */
77
+ hmac?(algorithm: string, key: string, data: string): string | null | undefined;
78
+
79
+ /** `navigator.vibrate`: a haptic (`impact`, `selection`, `success`, `warning`, `error`). Hosts before 0.20 lack it. */
80
+ haptic?(kind: string): void;
81
+
82
+ /**
83
+ * A host request of the web renderer's kinds (hostRequests.ts): `share`,
84
+ * `pickFiles`, `clipboard`, `pickColor` and `webAuth`, with the request's
85
+ * params as JSON; the answer arrives at `__sbHostResponse(id, { value } | { error })`.
86
+ * Hosts before 0.20 lack it.
87
+ */
88
+ hostRequest?(id: number, kind: string, json: string): void;
89
+
90
+ // The Keychain (secureStorage.ts). Hosts before 0.20 lack them.
91
+ secureGet?(key: string): string | null;
92
+ secureSet?(key: string, value: string): void;
93
+ secureRemove?(key: string): void;
94
+
47
95
  // Workers (worker.ts): a second context on its own thread, from a bundle
48
96
  // `swiftbrowser-native build --worker <name>=<entry>` wrote next to app.js
49
97
  // (`<name>.worker.js`). The app's context has the first three; a worker's
package/src/fetch.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  /**
2
2
  * `fetch`, `Headers`, `Request`, `Response` and `AbortController` over the
3
3
  * bridge's `fetch` primitive (`URLSession` on the Swift side). Bodies cross
4
- * as base64; a `Response` reads them back as text, JSON or bytes. Streams
5
- * and `Blob` are not here: a body is whole.
4
+ * as base64; a `Response` reads them back as text, JSON, bytes or a `Blob`.
5
+ * A `Blob` or `FormData` body (blob.ts) is sent whole, the form as
6
+ * multipart; there are no streams.
6
7
  */
7
8
 
8
9
  import type { NativeBridge, NativeFetchResult } from './bridge';
10
+ import { encodeMultipart, SimpleBlob, SimpleFormData } from './blob';
9
11
  import { base64Decode, base64Encode, utf8Decode, utf8Encode } from './encoding';
10
12
 
11
13
  type HeadersInitLike = SimpleHeaders | Record<string, string> | [string, string][];
@@ -114,16 +116,25 @@ function timeoutError(): Error {
114
116
  return error;
115
117
  }
116
118
 
117
- type BodyInitLike = string | ArrayBuffer | ArrayBufferView | SimpleURLSearchParamsLike | null | undefined;
119
+ type BodyInitLike = string | ArrayBuffer | ArrayBufferView | SimpleURLSearchParamsLike | SimpleBlob | SimpleFormData | null | undefined;
118
120
  interface SimpleURLSearchParamsLike {
119
121
  toString(): string;
120
122
  [Symbol.iterator](): IterableIterator<[string, string]>;
121
123
  }
122
124
 
125
+ /** The multipart boundary's randomness: the bridge's bytes once the runtime is installed, a counter before. */
126
+ let randomBytes: (count: number) => Uint8Array = (count) => Uint8Array.from({ length: count }, (_, i) => (i * 37 + 11) & 255);
127
+
128
+ export function setBodyRandomness(random: (count: number) => Uint8Array): void {
129
+ randomBytes = random;
130
+ }
131
+
123
132
  /** Bytes of a body, and the content type it implies when the caller set none. */
124
133
  function encodeBody(body: BodyInitLike): { bytes: Uint8Array | null; contentType: string | null } {
125
134
  if (body === null || body === undefined) return { bytes: null, contentType: null };
126
135
  if (typeof body === 'string') return { bytes: utf8Encode(body), contentType: 'text/plain;charset=UTF-8' };
136
+ if (body instanceof SimpleBlob) return { bytes: body.bytes(), contentType: body.type || null };
137
+ if (body instanceof SimpleFormData) return encodeMultipart(body, randomBytes);
127
138
  if (body instanceof ArrayBuffer) return { bytes: new Uint8Array(body), contentType: null };
128
139
  if (ArrayBuffer.isView(body)) return { bytes: new Uint8Array(body.buffer, body.byteOffset, body.byteLength), contentType: null };
129
140
  if (typeof body === 'object' && Symbol.iterator in body && typeof body.toString === 'function') {
@@ -160,6 +171,13 @@ class Body {
160
171
  async json(): Promise<unknown> {
161
172
  return JSON.parse(utf8Decode(this.take()));
162
173
  }
174
+ async blob(): Promise<SimpleBlob> {
175
+ return new SimpleBlob([this.take()], { type: this.contentType() });
176
+ }
177
+ /** The body's content type, for `blob()`; a Request or Response reads its headers. */
178
+ protected contentType(): string {
179
+ return '';
180
+ }
163
181
  }
164
182
 
165
183
  export interface RequestInitLike {
@@ -190,6 +208,10 @@ export class SimpleRequest extends Body {
190
208
  rawBytes(): Uint8Array | null {
191
209
  return this.bytes;
192
210
  }
211
+ protected override contentType(): string {
212
+ return this.headers.get('content-type') ?? '';
213
+ }
214
+
193
215
  clone(): SimpleRequest {
194
216
  return new SimpleRequest(this, { body: this.bytes });
195
217
  }
@@ -222,6 +244,10 @@ export class SimpleResponse extends Body {
222
244
  get ok(): boolean {
223
245
  return this.status >= 200 && this.status < 300;
224
246
  }
247
+ protected override contentType(): string {
248
+ return this.headers.get('content-type') ?? '';
249
+ }
250
+
225
251
  clone(): SimpleResponse {
226
252
  return new SimpleResponse(this.bytes, { status: this.status, statusText: this.statusText, headers: this.headers, url: this.url });
227
253
  }