@glassly/jspolyfill 0.1.0-dev.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/src/types.ts ADDED
@@ -0,0 +1,61 @@
1
+ /**
2
+ * Shared envelope types for the GlasslyJS runtime — the polyfill bundle
3
+ * uses these to drive globalThis.__deliver and the dispatch surface.
4
+ *
5
+ * Native (Swift on iOS, Kotlin on Android) injects:
6
+ * - __dispatch(iface: string, method: string, argsJson: string) → string
7
+ * - __hostLog(level: string, messageJson: string) → void
8
+ * - __hostError(payloadJson: string) → void
9
+ * - __hostUnhandledRejection(payloadJson: string) → void
10
+ *
11
+ * The polyfill (`startup.js`) installs:
12
+ * - globalThis.__deliver(envelopeJson: string) → void — host pushes here
13
+ * - globalThis.console, setTimeout, setInterval, fetch, WebSocket, etc.
14
+ * - rewires window.onerror / unhandledrejection to __hostError /
15
+ * __hostUnhandledRejection.
16
+ * - Calls __dispatch('__runtime', 'ready', []) once init is done so the
17
+ * host's signalReady NACK timer can be cleared.
18
+ *
19
+ * The shapes are JSON-serialisable strings on the wire — both sides pass
20
+ * JSON, never JS values directly. This keeps the bridge minimal and
21
+ * platform-symmetric.
22
+ */
23
+
24
+ export interface DispatchRequest {
25
+ iface: string
26
+ method: string
27
+ args: unknown[]
28
+ reqId?: string
29
+ }
30
+
31
+ export interface DispatchError {
32
+ code: string
33
+ message?: string
34
+ details?: Record<string, unknown>
35
+ }
36
+
37
+ export type DispatchResponse =
38
+ | {reqId: string; ok: true; result: unknown}
39
+ | {reqId: string; ok: false; error: DispatchError}
40
+
41
+ export type DeliverEnvelope =
42
+ | {kind: "init"; sessionId: string}
43
+ | {kind: "event"; iface: string; payload: unknown}
44
+ | {kind: "response"; reqId: string; ok: true; result?: unknown}
45
+ | {kind: "response"; reqId: string; ok: false; error: DispatchError}
46
+
47
+ /**
48
+ * Error codes returned via DispatchError.code. The native dispatcher and
49
+ * the SDK shim must agree on this vocabulary — adding a code without
50
+ * updating both sides is a wire-format break.
51
+ */
52
+ export const DISPATCH_ERROR_CODES = {
53
+ PERMISSION_NOT_DECLARED: "PERMISSION_NOT_DECLARED",
54
+ INTERFACE_NOT_FOUND: "INTERFACE_NOT_FOUND",
55
+ METHOD_NOT_FOUND: "METHOD_NOT_FOUND",
56
+ INVALID_ARGS: "INVALID_ARGS",
57
+ NATIVE_THROW: "NATIVE_THROW",
58
+ TIMEOUT: "TIMEOUT",
59
+ } as const
60
+
61
+ export type DispatchErrorCode = (typeof DISPATCH_ERROR_CODES)[keyof typeof DISPATCH_ERROR_CODES]