@vidra-dev/sdk 0.6.0 → 0.6.1
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 +43 -0
- package/dist/bridge.d.ts +15 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +16 -0
- package/dist/bridge.js.map +1 -0
- package/dist/client.d.ts +60 -52
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +133 -161
- package/dist/client.js.map +1 -1
- package/dist/generated/app.d.ts +6 -7
- package/dist/generated/app.d.ts.map +1 -1
- package/dist/generated/app.js +4 -11
- package/dist/generated/app.js.map +1 -1
- package/dist/generated/appWindow.d.ts +16 -17
- package/dist/generated/appWindow.d.ts.map +1 -1
- package/dist/generated/appWindow.js +14 -41
- package/dist/generated/appWindow.js.map +1 -1
- package/dist/generated/battery.d.ts +6 -7
- package/dist/generated/battery.d.ts.map +1 -1
- package/dist/generated/battery.js +4 -11
- package/dist/generated/battery.js.map +1 -1
- package/dist/generated/browser.d.ts +5 -6
- package/dist/generated/browser.d.ts.map +1 -1
- package/dist/generated/browser.js +3 -8
- package/dist/generated/browser.js.map +1 -1
- package/dist/generated/clipboard.d.ts +7 -8
- package/dist/generated/clipboard.d.ts.map +1 -1
- package/dist/generated/clipboard.js +5 -14
- package/dist/generated/clipboard.js.map +1 -1
- package/dist/generated/connectivity.d.ts +6 -7
- package/dist/generated/connectivity.d.ts.map +1 -1
- package/dist/generated/connectivity.js +4 -11
- package/dist/generated/connectivity.js.map +1 -1
- package/dist/generated/device.d.ts +6 -7
- package/dist/generated/device.d.ts.map +1 -1
- package/dist/generated/device.js +4 -11
- package/dist/generated/device.js.map +1 -1
- package/dist/generated/dialogs.d.ts +7 -8
- package/dist/generated/dialogs.d.ts.map +1 -1
- package/dist/generated/dialogs.js +5 -14
- package/dist/generated/dialogs.js.map +1 -1
- package/dist/generated/email.d.ts +5 -6
- package/dist/generated/email.d.ts.map +1 -1
- package/dist/generated/email.js +3 -8
- package/dist/generated/email.js.map +1 -1
- package/dist/generated/essentials.d.ts +5 -6
- package/dist/generated/essentials.d.ts.map +1 -1
- package/dist/generated/essentials.js +3 -8
- package/dist/generated/essentials.js.map +1 -1
- package/dist/generated/filePicker.d.ts +6 -7
- package/dist/generated/filePicker.d.ts.map +1 -1
- package/dist/generated/filePicker.js +4 -11
- package/dist/generated/filePicker.js.map +1 -1
- package/dist/generated/filesystem.d.ts +9 -10
- package/dist/generated/filesystem.d.ts.map +1 -1
- package/dist/generated/filesystem.js +7 -20
- package/dist/generated/filesystem.js.map +1 -1
- package/dist/generated/index.d.ts +90 -38
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +40 -40
- package/dist/generated/index.js.map +1 -1
- package/dist/generated/launcher.d.ts +6 -7
- package/dist/generated/launcher.d.ts.map +1 -1
- package/dist/generated/launcher.js +4 -11
- package/dist/generated/launcher.js.map +1 -1
- package/dist/generated/notifications.d.ts +6 -7
- package/dist/generated/notifications.d.ts.map +1 -1
- package/dist/generated/notifications.js +4 -11
- package/dist/generated/notifications.js.map +1 -1
- package/dist/generated/preferences.d.ts +9 -10
- package/dist/generated/preferences.d.ts.map +1 -1
- package/dist/generated/preferences.js +7 -20
- package/dist/generated/preferences.js.map +1 -1
- package/dist/generated/runtime.d.ts +5 -6
- package/dist/generated/runtime.d.ts.map +1 -1
- package/dist/generated/runtime.js +3 -8
- package/dist/generated/runtime.js.map +1 -1
- package/dist/generated/secureStorage.d.ts +8 -9
- package/dist/generated/secureStorage.d.ts.map +1 -1
- package/dist/generated/secureStorage.js +6 -17
- package/dist/generated/secureStorage.js.map +1 -1
- package/dist/generated/share.d.ts +5 -6
- package/dist/generated/share.d.ts.map +1 -1
- package/dist/generated/share.js +3 -8
- package/dist/generated/share.js.map +1 -1
- package/dist/generated/textToSpeech.d.ts +5 -6
- package/dist/generated/textToSpeech.d.ts.map +1 -1
- package/dist/generated/textToSpeech.js +3 -8
- package/dist/generated/textToSpeech.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/transport.d.ts +38 -21
- package/dist/transport.d.ts.map +1 -1
- package/dist/transport.js +62 -54
- package/dist/transport.js.map +1 -1
- package/package.json +1 -1
- package/dist/singleton.d.ts +0 -9
- package/dist/singleton.d.ts.map +0 -1
- package/dist/singleton.js +0 -8
- package/dist/singleton.js.map +0 -1
package/README.md
CHANGED
|
@@ -34,6 +34,49 @@ Built-in native methods and events are generated from C# contracts, providing
|
|
|
34
34
|
typed payloads, results, and editor completion. App-owned contracts are
|
|
35
35
|
generated into the application.
|
|
36
36
|
|
|
37
|
+
## How the SDK is put together
|
|
38
|
+
|
|
39
|
+
The SDK is written as arrow-function factories over plain objects; there are
|
|
40
|
+
no classes. Every generated contract is a factory such as
|
|
41
|
+
`createClipboard(bridge)`, and the exports above are those factories bound to
|
|
42
|
+
the page's bridge.
|
|
43
|
+
|
|
44
|
+
That bridge is built in one module, `bridge.ts`, which is the SDK's only
|
|
45
|
+
side effect: it calls `createBridge(...)`, installs the `window.__vidra_*`
|
|
46
|
+
callbacks the native host talks to, and exports `bridge`, `expectations`, and
|
|
47
|
+
`vidra`. The host protocol has exactly one callback set per page, so there is
|
|
48
|
+
one bridge per page.
|
|
49
|
+
|
|
50
|
+
Generated barrels and the Vidra CLI record the fingerprints they were produced
|
|
51
|
+
against with `expectations.declare(scope, fingerprint)`. Declarations are
|
|
52
|
+
write-once per scope: declaring the same value again is a no-op, a different
|
|
53
|
+
value throws because two generated modules for the same scope are loaded. When
|
|
54
|
+
the host sends its handshake, the bridge compares the declared `core`, `app`,
|
|
55
|
+
and `access` fingerprints and renders a visible error on mismatch.
|
|
56
|
+
|
|
57
|
+
### Isolated bridges
|
|
58
|
+
|
|
59
|
+
For tests, or to supply a custom transport, build a bridge yourself. Nothing
|
|
60
|
+
touches `window` unless you ask it to:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import {
|
|
64
|
+
createBridge,
|
|
65
|
+
createClipboard,
|
|
66
|
+
createExpectations,
|
|
67
|
+
installWindowCallbacks,
|
|
68
|
+
} from "@vidra-dev/sdk";
|
|
69
|
+
|
|
70
|
+
const bridge = createBridge({ transport: myTransport, timeout: 5_000 });
|
|
71
|
+
const clipboard = createClipboard(bridge);
|
|
72
|
+
|
|
73
|
+
// Only if the native host should reach this bridge:
|
|
74
|
+
const uninstall = installWindowCallbacks(bridge);
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`installWindowCallbacks` returns an uninstaller that removes only the callbacks
|
|
78
|
+
it installed.
|
|
79
|
+
|
|
37
80
|
## Documentation
|
|
38
81
|
|
|
39
82
|
- [Getting started](https://vidra.build/docs/getting-started/)
|
package/dist/bridge.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type VidraBridge } from "./client.js";
|
|
2
|
+
export type { VidraBridge } from "./client.js";
|
|
3
|
+
/**
|
|
4
|
+
* The page's bridge. This module is the SDK's composition root and its only
|
|
5
|
+
* side effect: the host protocol has one set of `window.__vidra_*` callbacks,
|
|
6
|
+
* so one bridge is built here and the generated barrels bind to it.
|
|
7
|
+
* Lives in its own module to avoid circular dependencies with the generated
|
|
8
|
+
* barrel (generated/index.ts).
|
|
9
|
+
*/
|
|
10
|
+
export declare const expectations: import("./client.js").Expectations;
|
|
11
|
+
export declare const bridge: VidraBridge;
|
|
12
|
+
/** The dynamic, untyped surface of the page's bridge. */
|
|
13
|
+
export type Vidra = Pick<VidraBridge, "unsafe" | "capabilities">;
|
|
14
|
+
export declare const vidra: Vidra;
|
|
15
|
+
//# sourceMappingURL=bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.d.ts","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,WAAW,EAIjB,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,oCAAuB,CAAC;AACjD,eAAO,MAAM,MAAM,aAAiC,CAAC;AAGrD,yDAAyD;AACzD,MAAM,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,QAAQ,GAAG,cAAc,CAAC,CAAC;AAEjE,eAAO,MAAM,KAAK,EAAE,KAGnB,CAAC"}
|
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createBridge, createExpectations, installWindowCallbacks, } from "./client.js";
|
|
2
|
+
/**
|
|
3
|
+
* The page's bridge. This module is the SDK's composition root and its only
|
|
4
|
+
* side effect: the host protocol has one set of `window.__vidra_*` callbacks,
|
|
5
|
+
* so one bridge is built here and the generated barrels bind to it.
|
|
6
|
+
* Lives in its own module to avoid circular dependencies with the generated
|
|
7
|
+
* barrel (generated/index.ts).
|
|
8
|
+
*/
|
|
9
|
+
export const expectations = createExpectations();
|
|
10
|
+
export const bridge = createBridge({ expectations });
|
|
11
|
+
installWindowCallbacks(bridge);
|
|
12
|
+
export const vidra = {
|
|
13
|
+
unsafe: bridge.unsafe,
|
|
14
|
+
capabilities: bridge.capabilities,
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bridge.js","sourceRoot":"","sources":["../src/bridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EACZ,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,aAAa,CAAC;AAIrB;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,kBAAkB,EAAE,CAAC;AACjD,MAAM,CAAC,MAAM,MAAM,GAAG,YAAY,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC;AACrD,sBAAsB,CAAC,MAAM,CAAC,CAAC;AAK/B,MAAM,CAAC,MAAM,KAAK,GAAU;IAC1B,MAAM,EAAE,MAAM,CAAC,MAAM;IACrB,YAAY,EAAE,MAAM,CAAC,YAAY;CAClC,CAAC"}
|
package/dist/client.d.ts
CHANGED
|
@@ -1,57 +1,65 @@
|
|
|
1
|
-
import type { EventHandler, Capabilities, JsHandler } from "./types.js";
|
|
2
|
-
import { Transport } from "./transport.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
private coreFingerprint;
|
|
17
|
-
private appFingerprint;
|
|
18
|
-
private expectedAccessFingerprint;
|
|
19
|
-
readonly unsafe: UnsafeVidraClient;
|
|
20
|
-
constructor(options?: VidraClientOptions);
|
|
21
|
-
/** @internal Used by generated contract barrels. */
|
|
22
|
-
registerGeneratedManifest(scope: "core" | "app", fingerprint: string): void;
|
|
23
|
-
/** @internal Stamped by the Vidra CLI into the frontend build. */
|
|
24
|
-
registerExpectedAccessFingerprint(fingerprint: string): void;
|
|
1
|
+
import type { BridgeResponse, BridgeEvent, EventHandler, Capabilities, ReverseRequest, JsHandler, BridgeHandshake } from "./types.js";
|
|
2
|
+
import { type Transport } from "./transport.js";
|
|
3
|
+
/**
|
|
4
|
+
* Which build artefact a fingerprint declaration describes:
|
|
5
|
+
* - `core`: the SDK's generated built-in contracts
|
|
6
|
+
* - `app`: the application's generated contracts
|
|
7
|
+
* - `access`: the bridge access policy the frontend was built against
|
|
8
|
+
*/
|
|
9
|
+
export type ExpectationScope = "core" | "app" | "access";
|
|
10
|
+
/**
|
|
11
|
+
* Write-once record of the fingerprints this frontend was generated against.
|
|
12
|
+
* Generated barrels and the CLI declare into it while modules evaluate; the
|
|
13
|
+
* bridge reads it when the host sends its handshake.
|
|
14
|
+
*/
|
|
15
|
+
export interface Expectations {
|
|
25
16
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* 3. the custom-scheme transport when the host marker is set
|
|
30
|
-
* 4. the browser-only fallback
|
|
31
|
-
*
|
|
32
|
-
* The browser fallback is intentionally never cached: a native channel can
|
|
33
|
-
* become available shortly after load (e.g. the WKWebView script-message
|
|
34
|
-
* handler registers just after the page loads), so we keep re-detecting until
|
|
35
|
-
* a real native transport is found.
|
|
17
|
+
* Declare the fingerprint for a scope. Declaring the same value again is a
|
|
18
|
+
* no-op; a different value for an already-declared scope throws, because two
|
|
19
|
+
* generated modules for the same scope are loaded in one page.
|
|
36
20
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
/** Query which modules and methods are available on the native side. */
|
|
46
|
-
capabilities(): Promise<Capabilities>;
|
|
47
|
-
private handleResponse;
|
|
48
|
-
private handleEvent;
|
|
49
|
-
private handleReverseCall;
|
|
50
|
-
private initialize;
|
|
21
|
+
declare: (scope: ExpectationScope, fingerprint: string) => void;
|
|
22
|
+
get: (scope: ExpectationScope) => string | undefined;
|
|
23
|
+
}
|
|
24
|
+
export declare const createExpectations: () => Expectations;
|
|
25
|
+
export interface UnsafeVidra {
|
|
26
|
+
invoke: <T = unknown>(contract: string, member: string, payload?: unknown) => Promise<T>;
|
|
27
|
+
on: <T = unknown>(contract: string, member: string, handler: EventHandler<T>) => () => void;
|
|
28
|
+
handle: <TPayload = unknown, TResult = unknown>(contract: string, member: string, handler: JsHandler<TPayload, TResult>) => () => void;
|
|
51
29
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
30
|
+
/** Entry points the native host calls into; `installWindowCallbacks` exposes
|
|
31
|
+
* them as the `window.__vidra_*` functions the host protocol expects. */
|
|
32
|
+
export interface HostEntryPoints {
|
|
33
|
+
deliverResponse: (response: BridgeResponse) => void;
|
|
34
|
+
dispatchEvent: (event: BridgeEvent) => void;
|
|
35
|
+
invokeHandler: (request: ReverseRequest) => Promise<void>;
|
|
36
|
+
initialize: (handshake: BridgeHandshake) => void;
|
|
37
|
+
}
|
|
38
|
+
export interface VidraBridge {
|
|
39
|
+
readonly unsafe: UnsafeVidra;
|
|
40
|
+
/** Query which contracts and members are available on the native side. */
|
|
41
|
+
readonly capabilities: () => Promise<Capabilities>;
|
|
42
|
+
readonly host: HostEntryPoints;
|
|
43
|
+
}
|
|
44
|
+
export interface BridgeOptions {
|
|
45
|
+
/** Override the transport layer (auto-detected on first use by default). */
|
|
46
|
+
transport?: Transport;
|
|
47
|
+
/** Timeout in ms for invoke calls. Default: 30_000 */
|
|
48
|
+
timeout?: number;
|
|
49
|
+
/** Fingerprints to compare against the host handshake. Default: none declared. */
|
|
50
|
+
expectations?: Expectations;
|
|
56
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Build a bridge with its own request correlation, listeners, and handlers.
|
|
54
|
+
* Pure apart from the transport it eventually sends on: nothing is written to
|
|
55
|
+
* `window` until `installWindowCallbacks` is called.
|
|
56
|
+
*/
|
|
57
|
+
export declare const createBridge: (options?: BridgeOptions) => VidraBridge;
|
|
58
|
+
/**
|
|
59
|
+
* Expose a bridge's host entry points as the `window.__vidra_*` functions the
|
|
60
|
+
* native host calls. Returns an uninstaller that removes only the functions it
|
|
61
|
+
* installed: when another bridge has been installed since (tests install one
|
|
62
|
+
* per case), its callbacks are left in place.
|
|
63
|
+
*/
|
|
64
|
+
export declare const installWindowCallbacks: (bridge: VidraBridge) => (() => void);
|
|
57
65
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEV,cAAc,EACd,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,cAAc,EAEd,SAAS,EACT,eAAe,EAChB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,KAAK,SAAS,EAAmB,MAAM,gBAAgB,CAAC;AAQjE;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,KAAK,GAAG,QAAQ,CAAC;AAEzD;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;OAIG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,KAAK,IAAI,CAAC;IAChE,GAAG,EAAE,CAAC,KAAK,EAAE,gBAAgB,KAAK,MAAM,GAAG,SAAS,CAAC;CACtD;AAED,eAAO,MAAM,kBAAkB,QAAO,YAerC,CAAC;AAEF,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,CAAC,CAAC,GAAG,OAAO,EAClB,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,OAAO,KACd,OAAO,CAAC,CAAC,CAAC,CAAC;IAEhB,EAAE,EAAE,CAAC,CAAC,GAAG,OAAO,EACd,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,KACrB,MAAM,IAAI,CAAC;IAEhB,MAAM,EAAE,CAAC,QAAQ,GAAG,OAAO,EAAE,OAAO,GAAG,OAAO,EAC5C,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,SAAS,CAAC,QAAQ,EAAE,OAAO,CAAC,KAClC,MAAM,IAAI,CAAC;CACjB;AAED;yEACyE;AACzE,MAAM,WAAW,eAAe;IAC9B,eAAe,EAAE,CAAC,QAAQ,EAAE,cAAc,KAAK,IAAI,CAAC;IACpD,aAAa,EAAE,CAAC,KAAK,EAAE,WAAW,KAAK,IAAI,CAAC;IAC5C,aAAa,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1D,UAAU,EAAE,CAAC,SAAS,EAAE,eAAe,KAAK,IAAI,CAAC;CAClD;AAED,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,0EAA0E;IAC1E,QAAQ,CAAC,YAAY,EAAE,MAAM,OAAO,CAAC,YAAY,CAAC,CAAC;IACnD,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC;CAChC;AAED,MAAM,WAAW,aAAa;IAC5B,4EAA4E;IAC5E,SAAS,CAAC,EAAE,SAAS,CAAC;IACtB,sDAAsD;IACtD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,kFAAkF;IAClF,YAAY,CAAC,EAAE,YAAY,CAAC;CAC7B;AAOD;;;;GAIG;AACH,eAAO,MAAM,YAAY,aAAa,aAAa,KAAQ,WA0L1D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,WAAY,WAAW,KAAG,CAAC,MAAM,IAAI,CAoBvE,CAAC"}
|
package/dist/client.js
CHANGED
|
@@ -1,153 +1,106 @@
|
|
|
1
|
-
import {
|
|
2
|
-
let _idCounter = 0;
|
|
1
|
+
import { detectTransport } from "./transport.js";
|
|
3
2
|
const PROTOCOL_VERSION = 2;
|
|
3
|
+
// SHA-256 of an empty byte sequence (`printf '' | shasum -a 256`).
|
|
4
|
+
// The host uses this value when a manifest scope contains no contracts.
|
|
4
5
|
const EMPTY_FINGERPRINT = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
|
|
5
|
-
const
|
|
6
|
-
|
|
6
|
+
export const createExpectations = () => {
|
|
7
|
+
const declared = new Map();
|
|
8
|
+
return {
|
|
9
|
+
declare: (scope, fingerprint) => {
|
|
10
|
+
const existing = declared.get(scope);
|
|
11
|
+
if (existing !== undefined && existing !== fingerprint) {
|
|
12
|
+
throw new Error(`[vidra] Conflicting ${scope} fingerprint declarations (${existing} vs ${fingerprint}). Two generated modules for the same scope are loaded; regenerate TypeScript contracts.`);
|
|
13
|
+
}
|
|
14
|
+
declared.set(scope, fingerprint);
|
|
15
|
+
},
|
|
16
|
+
get: (scope) => declared.get(scope),
|
|
17
|
+
};
|
|
7
18
|
};
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
this.handleEvent(event);
|
|
30
|
-
};
|
|
31
|
-
// The C# side calls window.__vidra_invoke(request) for reverse RPC.
|
|
32
|
-
window.__vidra_invoke = (request) => {
|
|
33
|
-
this.handleReverseCall(request);
|
|
34
|
-
};
|
|
35
|
-
window.__vidra_initialize = (handshake) => {
|
|
36
|
-
this.initialize(handshake);
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
/** @internal Used by generated contract barrels. */
|
|
40
|
-
registerGeneratedManifest(scope, fingerprint) {
|
|
41
|
-
if (scope === "core") {
|
|
42
|
-
this.coreFingerprint = fingerprint;
|
|
19
|
+
/**
|
|
20
|
+
* Build a bridge with its own request correlation, listeners, and handlers.
|
|
21
|
+
* Pure apart from the transport it eventually sends on: nothing is written to
|
|
22
|
+
* `window` until `installWindowCallbacks` is called.
|
|
23
|
+
*/
|
|
24
|
+
export const createBridge = (options = {}) => {
|
|
25
|
+
const timeout = options.timeout ?? 30000;
|
|
26
|
+
const expectations = options.expectations ?? createExpectations();
|
|
27
|
+
const pending = new Map();
|
|
28
|
+
const listeners = new Map();
|
|
29
|
+
const handlers = new Map();
|
|
30
|
+
let idCounter = 0;
|
|
31
|
+
let cachedTransport = options.transport;
|
|
32
|
+
const nextId = () => `req_${++idCounter}_${Date.now()}`;
|
|
33
|
+
const deliverResponse = (response) => {
|
|
34
|
+
const entry = pending.get(response.id);
|
|
35
|
+
if (!entry)
|
|
36
|
+
return;
|
|
37
|
+
pending.delete(response.id);
|
|
38
|
+
if (response.success) {
|
|
39
|
+
entry.resolve(response.data);
|
|
43
40
|
}
|
|
44
41
|
else {
|
|
45
|
-
|
|
42
|
+
entry.reject(new Error(response.error
|
|
43
|
+
? `[${response.error.code}] ${response.error.message}`
|
|
44
|
+
: "Unknown native error"));
|
|
46
45
|
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
* 1. an explicitly supplied transport
|
|
55
|
-
* 2. the native message channel (WKWebView / WebView2) when present
|
|
56
|
-
* 3. the custom-scheme transport when the host marker is set
|
|
57
|
-
* 4. the browser-only fallback
|
|
58
|
-
*
|
|
59
|
-
* The browser fallback is intentionally never cached: a native channel can
|
|
60
|
-
* become available shortly after load (e.g. the WKWebView script-message
|
|
61
|
-
* handler registers just after the page loads), so we keep re-detecting until
|
|
62
|
-
* a real native transport is found.
|
|
63
|
-
*/
|
|
64
|
-
get transport() {
|
|
65
|
-
if (this._explicitTransport)
|
|
66
|
-
return this._explicitTransport;
|
|
67
|
-
if (this._transport)
|
|
68
|
-
return this._transport;
|
|
69
|
-
const detected = this.detectTransport();
|
|
70
|
-
if (!(detected instanceof BrowserFallbackTransport)) {
|
|
71
|
-
this._transport = detected;
|
|
46
|
+
};
|
|
47
|
+
const transport = () => {
|
|
48
|
+
if (cachedTransport)
|
|
49
|
+
return cachedTransport;
|
|
50
|
+
const detected = detectTransport(deliverResponse);
|
|
51
|
+
if (detected.cacheable) {
|
|
52
|
+
cachedTransport = detected.transport;
|
|
72
53
|
}
|
|
73
|
-
return detected;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
reject(
|
|
89
|
-
},
|
|
90
|
-
this.pending.set(id, {
|
|
91
|
-
resolve: (data) => {
|
|
92
|
-
clearTimeout(timer);
|
|
93
|
-
resolve(data);
|
|
94
|
-
},
|
|
95
|
-
reject: (err) => {
|
|
96
|
-
clearTimeout(timer);
|
|
97
|
-
reject(err);
|
|
98
|
-
},
|
|
99
|
-
});
|
|
100
|
-
const request = { id, contract, member, payload };
|
|
101
|
-
this.transport.send(request);
|
|
54
|
+
return detected.transport;
|
|
55
|
+
};
|
|
56
|
+
const invoke = (contract, member, payload) => new Promise((resolve, reject) => {
|
|
57
|
+
const id = nextId();
|
|
58
|
+
const timer = setTimeout(() => {
|
|
59
|
+
pending.delete(id);
|
|
60
|
+
reject(new Error(`[vidra] Timeout after ${timeout}ms for ${contract}.${member}`));
|
|
61
|
+
}, timeout);
|
|
62
|
+
pending.set(id, {
|
|
63
|
+
resolve: (data) => {
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
resolve(data);
|
|
66
|
+
},
|
|
67
|
+
reject: (error) => {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
reject(error);
|
|
70
|
+
},
|
|
102
71
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
72
|
+
const request = { id, contract, member, payload };
|
|
73
|
+
transport().send(request);
|
|
74
|
+
});
|
|
75
|
+
const on = (contract, member, handler) => {
|
|
106
76
|
const key = contractKey(contract, member);
|
|
107
|
-
|
|
77
|
+
let set = listeners.get(key);
|
|
78
|
+
if (!set) {
|
|
79
|
+
set = new Set();
|
|
80
|
+
listeners.set(key, set);
|
|
81
|
+
}
|
|
82
|
+
set.add(handler);
|
|
83
|
+
return () => listeners.get(key)?.delete(handler);
|
|
84
|
+
};
|
|
85
|
+
const handle = (contract, member, handler) => {
|
|
86
|
+
const key = contractKey(contract, member);
|
|
87
|
+
if (handlers.has(key)) {
|
|
108
88
|
throw new Error(`[vidra] JavaScript handler '${contract}.${member}' is already registered.`);
|
|
109
89
|
}
|
|
110
90
|
const registered = handler;
|
|
111
|
-
|
|
91
|
+
handlers.set(key, registered);
|
|
112
92
|
return () => {
|
|
113
|
-
if (
|
|
114
|
-
|
|
93
|
+
if (handlers.get(key) === registered) {
|
|
94
|
+
handlers.delete(key);
|
|
115
95
|
}
|
|
116
96
|
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
if (!
|
|
122
|
-
this.listeners.set(key, new Set());
|
|
123
|
-
}
|
|
124
|
-
this.listeners.get(key).add(handler);
|
|
125
|
-
return () => this.listeners.get(key)?.delete(handler);
|
|
126
|
-
}
|
|
127
|
-
/** Query which modules and methods are available on the native side. */
|
|
128
|
-
async capabilities() {
|
|
129
|
-
return this.invokeUnsafe("__bridge", "capabilities");
|
|
130
|
-
}
|
|
131
|
-
handleResponse(response) {
|
|
132
|
-
const entry = this.pending.get(response.id);
|
|
133
|
-
if (!entry)
|
|
97
|
+
};
|
|
98
|
+
const capabilities = () => invoke("__bridge", "capabilities");
|
|
99
|
+
const dispatchEvent = (event) => {
|
|
100
|
+
const set = listeners.get(contractKey(event.contract, event.member));
|
|
101
|
+
if (!set)
|
|
134
102
|
return;
|
|
135
|
-
|
|
136
|
-
if (response.success) {
|
|
137
|
-
entry.resolve(response.data);
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
entry.reject(new Error(response.error
|
|
141
|
-
? `[${response.error.code}] ${response.error.message}`
|
|
142
|
-
: "Unknown native error"));
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
handleEvent(event) {
|
|
146
|
-
const key = contractKey(event.contract, event.member);
|
|
147
|
-
const handlers = this.listeners.get(key);
|
|
148
|
-
if (!handlers)
|
|
149
|
-
return;
|
|
150
|
-
for (const handler of handlers) {
|
|
103
|
+
for (const handler of set) {
|
|
151
104
|
try {
|
|
152
105
|
handler(event.payload);
|
|
153
106
|
}
|
|
@@ -155,56 +108,49 @@ export class VidraClient {
|
|
|
155
108
|
console.error(`[vidra] Error in event handler for '${event.contract}.${event.member}':`, e);
|
|
156
109
|
}
|
|
157
110
|
}
|
|
158
|
-
}
|
|
159
|
-
async
|
|
160
|
-
const
|
|
161
|
-
const
|
|
111
|
+
};
|
|
112
|
+
const invokeHandler = async (request) => {
|
|
113
|
+
const handler = handlers.get(contractKey(request.contract, request.member));
|
|
114
|
+
const respond = (response) => transport().sendReverse(response);
|
|
162
115
|
if (!handler) {
|
|
163
|
-
|
|
116
|
+
respond({
|
|
164
117
|
id: request.id,
|
|
165
118
|
success: false,
|
|
166
119
|
error: {
|
|
167
120
|
code: "JS_HANDLER_NOT_FOUND",
|
|
168
121
|
message: `No JavaScript handler registered for '${request.contract}.${request.member}'.`,
|
|
169
122
|
},
|
|
170
|
-
};
|
|
171
|
-
this.transport.sendReverse(response);
|
|
123
|
+
});
|
|
172
124
|
return;
|
|
173
125
|
}
|
|
174
126
|
try {
|
|
175
127
|
const result = await handler(request.payload);
|
|
176
|
-
|
|
177
|
-
id: request.id,
|
|
178
|
-
success: true,
|
|
179
|
-
data: result,
|
|
180
|
-
};
|
|
181
|
-
this.transport.sendReverse(response);
|
|
128
|
+
respond({ id: request.id, success: true, data: result });
|
|
182
129
|
}
|
|
183
130
|
catch (e) {
|
|
184
|
-
|
|
131
|
+
respond({
|
|
185
132
|
id: request.id,
|
|
186
133
|
success: false,
|
|
187
134
|
error: {
|
|
188
135
|
code: "JS_HANDLER_ERROR",
|
|
189
136
|
message: e instanceof Error ? e.message : String(e),
|
|
190
137
|
},
|
|
191
|
-
};
|
|
192
|
-
this.transport.sendReverse(response);
|
|
138
|
+
});
|
|
193
139
|
}
|
|
194
|
-
}
|
|
195
|
-
initialize(handshake) {
|
|
140
|
+
};
|
|
141
|
+
const initialize = (handshake) => {
|
|
196
142
|
const mismatches = [];
|
|
197
143
|
if (handshake.protocolVersion !== PROTOCOL_VERSION) {
|
|
198
144
|
mismatches.push(`protocol host=${handshake.protocolVersion} sdk=${PROTOCOL_VERSION}`);
|
|
199
145
|
}
|
|
200
|
-
if (handshake.coreFingerprint !==
|
|
146
|
+
if (handshake.coreFingerprint !== (expectations.get("core") ?? EMPTY_FINGERPRINT)) {
|
|
201
147
|
mismatches.push("core contract fingerprint");
|
|
202
148
|
}
|
|
203
|
-
if (handshake.appFingerprint !==
|
|
149
|
+
if (handshake.appFingerprint !== (expectations.get("app") ?? EMPTY_FINGERPRINT)) {
|
|
204
150
|
mismatches.push("app contract fingerprint");
|
|
205
151
|
}
|
|
206
|
-
|
|
207
|
-
|
|
152
|
+
const expectedAccess = expectations.get("access");
|
|
153
|
+
if (expectedAccess !== undefined && handshake.accessFingerprint !== expectedAccess) {
|
|
208
154
|
mismatches.push("bridge access fingerprint");
|
|
209
155
|
}
|
|
210
156
|
if (mismatches.length === 0)
|
|
@@ -212,8 +158,38 @@ export class VidraClient {
|
|
|
212
158
|
const message = `[vidra] Bridge contract mismatch: ${mismatches.join(", ")}. Rebuild the host and regenerate TypeScript contracts.`;
|
|
213
159
|
renderBridgeError(message);
|
|
214
160
|
throw new Error(message);
|
|
161
|
+
};
|
|
162
|
+
return {
|
|
163
|
+
unsafe: { invoke, on, handle },
|
|
164
|
+
capabilities,
|
|
165
|
+
host: { deliverResponse, dispatchEvent, invokeHandler, initialize },
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Expose a bridge's host entry points as the `window.__vidra_*` functions the
|
|
170
|
+
* native host calls. Returns an uninstaller that removes only the functions it
|
|
171
|
+
* installed: when another bridge has been installed since (tests install one
|
|
172
|
+
* per case), its callbacks are left in place.
|
|
173
|
+
*/
|
|
174
|
+
export const installWindowCallbacks = (bridge) => {
|
|
175
|
+
const target = window;
|
|
176
|
+
const callbacks = {
|
|
177
|
+
__vidra_callback: bridge.host.deliverResponse,
|
|
178
|
+
__vidra_onevent: bridge.host.dispatchEvent,
|
|
179
|
+
__vidra_invoke: bridge.host.invokeHandler,
|
|
180
|
+
__vidra_initialize: bridge.host.initialize,
|
|
181
|
+
};
|
|
182
|
+
for (const [name, callback] of Object.entries(callbacks)) {
|
|
183
|
+
target[name] = callback;
|
|
215
184
|
}
|
|
216
|
-
|
|
185
|
+
return () => {
|
|
186
|
+
for (const [name, callback] of Object.entries(callbacks)) {
|
|
187
|
+
if (target[name] === callback) {
|
|
188
|
+
delete target[name];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
};
|
|
217
193
|
const contractKey = (contract, member) => `${contract}\u0000${member}`;
|
|
218
194
|
const renderBridgeError = (message) => {
|
|
219
195
|
if (typeof document === "undefined" || !document.body)
|
|
@@ -225,8 +201,4 @@ const renderBridgeError = (message) => {
|
|
|
225
201
|
pre.textContent = message;
|
|
226
202
|
document.body.replaceChildren(pre);
|
|
227
203
|
};
|
|
228
|
-
const isNativeHost = () => {
|
|
229
|
-
// The native host injects this marker before the web content loads.
|
|
230
|
-
return typeof window.__vidra_native !== "undefined";
|
|
231
|
-
};
|
|
232
204
|
//# sourceMappingURL=client.js.map
|