@aspectly/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +188 -0
- package/dist/AspectlyBridge-hGuDjcI6.d.mts +242 -0
- package/dist/AspectlyBridge-hGuDjcI6.d.ts +242 -0
- package/dist/browser.d.mts +24 -0
- package/dist/browser.d.ts +24 -0
- package/dist/browser.js +398 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +396 -0
- package/dist/browser.mjs.map +1 -0
- package/dist/index.d.mts +54 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +420 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +412 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +71 -0
- package/src/AspectlyBridge.test.ts +78 -0
- package/src/AspectlyBridge.ts +44 -0
- package/src/BridgeBase.test.ts +132 -0
- package/src/BridgeBase.ts +63 -0
- package/src/BridgeCore.test.ts +254 -0
- package/src/BridgeCore.ts +138 -0
- package/src/BridgeInternal.test.ts +403 -0
- package/src/BridgeInternal.ts +305 -0
- package/src/browser.ts +27 -0
- package/src/index.ts +28 -0
- package/src/types.ts +134 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Zhan Isaakian
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# @aspectly/core
|
|
2
|
+
|
|
3
|
+
The core bridge framework for cross-platform communication between WebViews, iframes, and web applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# npm
|
|
9
|
+
npm install @aspectly/core
|
|
10
|
+
|
|
11
|
+
# pnpm
|
|
12
|
+
pnpm add @aspectly/core
|
|
13
|
+
|
|
14
|
+
# yarn
|
|
15
|
+
yarn add @aspectly/core
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Overview
|
|
19
|
+
|
|
20
|
+
`@aspectly/core` provides the foundational bridge communication layer that enables bidirectional message passing between different execution contexts:
|
|
21
|
+
|
|
22
|
+
- **WebView ↔ React Native**: Communication between web content and React Native apps
|
|
23
|
+
- **iframe ↔ Parent Window**: Communication between iframes and their parent pages
|
|
24
|
+
- **Browser Context**: Standalone browser usage with global bridge instance
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
### Inside a WebView or iframe
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { AspectlyBridge } from '@aspectly/core';
|
|
32
|
+
|
|
33
|
+
// Create a bridge instance
|
|
34
|
+
const bridge = new AspectlyBridge();
|
|
35
|
+
|
|
36
|
+
// Initialize with handlers for incoming requests
|
|
37
|
+
await bridge.init({
|
|
38
|
+
greet: async (params: { name: string }) => {
|
|
39
|
+
return { message: `Hello, ${params.name}!` };
|
|
40
|
+
},
|
|
41
|
+
getData: async () => {
|
|
42
|
+
return { timestamp: Date.now() };
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Send requests to the parent container
|
|
47
|
+
const result = await bridge.send('parentMethod', { data: 'value' });
|
|
48
|
+
console.log(result);
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Browser Script Tag Usage
|
|
52
|
+
|
|
53
|
+
```html
|
|
54
|
+
<script src="https://unpkg.com/@aspectly/core/dist/browser.js"></script>
|
|
55
|
+
<script>
|
|
56
|
+
window.aspectlyBridge.init({
|
|
57
|
+
greet: async (params) => ({ message: `Hello, ${params.name}!` })
|
|
58
|
+
}).then(() => {
|
|
59
|
+
console.log('Bridge initialized');
|
|
60
|
+
});
|
|
61
|
+
</script>
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## API Reference
|
|
65
|
+
|
|
66
|
+
### AspectlyBridge
|
|
67
|
+
|
|
68
|
+
The main class for bridge communication.
|
|
69
|
+
|
|
70
|
+
#### Constructor
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
const bridge = new AspectlyBridge(options?: BridgeOptions);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Options:**
|
|
77
|
+
- `timeout?: number` - Handler execution timeout in ms (default: 100000)
|
|
78
|
+
|
|
79
|
+
#### Methods
|
|
80
|
+
|
|
81
|
+
##### `init(handlers?: BridgeHandlers): Promise<boolean>`
|
|
82
|
+
|
|
83
|
+
Initialize the bridge with handlers for incoming requests.
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
await bridge.init({
|
|
87
|
+
methodName: async (params) => {
|
|
88
|
+
// Handle request and return result
|
|
89
|
+
return { success: true };
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
##### `send<T>(method: string, params?: object): Promise<T>`
|
|
95
|
+
|
|
96
|
+
Send a request to the other side.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const result = await bridge.send<{ data: string }>('getData', { id: 123 });
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
##### `supports(method: string): boolean`
|
|
103
|
+
|
|
104
|
+
Check if a method is supported by the other side.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
if (bridge.supports('getData')) {
|
|
108
|
+
const data = await bridge.send('getData');
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
##### `isAvailable(): boolean`
|
|
113
|
+
|
|
114
|
+
Check if the bridge is initialized and ready.
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
if (bridge.isAvailable()) {
|
|
118
|
+
// Bridge is ready
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
##### `subscribe(listener: BridgeListener): number`
|
|
123
|
+
|
|
124
|
+
Subscribe to all result events for monitoring/debugging.
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
bridge.subscribe((result) => {
|
|
128
|
+
console.log('Event:', result.method, result.data);
|
|
129
|
+
});
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
##### `unsubscribe(listener: BridgeListener): void`
|
|
133
|
+
|
|
134
|
+
Remove a result event listener.
|
|
135
|
+
|
|
136
|
+
##### `destroy(): void`
|
|
137
|
+
|
|
138
|
+
Cleanup bridge subscriptions.
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
bridge.destroy();
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## Error Handling
|
|
145
|
+
|
|
146
|
+
The bridge provides typed errors for different failure scenarios:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { BridgeErrorType } from '@aspectly/core';
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
await bridge.send('unknownMethod');
|
|
153
|
+
} catch (error) {
|
|
154
|
+
switch (error.error_type) {
|
|
155
|
+
case BridgeErrorType.UNSUPPORTED_METHOD:
|
|
156
|
+
console.log('Method not registered');
|
|
157
|
+
break;
|
|
158
|
+
case BridgeErrorType.METHOD_EXECUTION_TIMEOUT:
|
|
159
|
+
console.log('Handler timed out');
|
|
160
|
+
break;
|
|
161
|
+
case BridgeErrorType.REJECTED:
|
|
162
|
+
console.log('Handler threw error:', error.error_message);
|
|
163
|
+
break;
|
|
164
|
+
case BridgeErrorType.BRIDGE_NOT_AVAILABLE:
|
|
165
|
+
console.log('Bridge not initialized');
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## Architecture
|
|
172
|
+
|
|
173
|
+
The package consists of four layers:
|
|
174
|
+
|
|
175
|
+
1. **BridgeCore** - Low-level platform detection and message serialization
|
|
176
|
+
2. **BridgeInternal** - Protocol management and request/response lifecycle
|
|
177
|
+
3. **BridgeBase** - Clean public API interface
|
|
178
|
+
4. **AspectlyBridge** - Main entry point composing all layers
|
|
179
|
+
|
|
180
|
+
## Related Packages
|
|
181
|
+
|
|
182
|
+
- [`@aspectly/web`](../web) - Web/iframe integration with React hooks
|
|
183
|
+
- [`@aspectly/react-native`](../react-native) - React Native WebView integration
|
|
184
|
+
- [`@aspectly/react-native-web`](../react-native-web) - React Native Web + iframe support
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event types used in bridge communication protocol
|
|
3
|
+
*/
|
|
4
|
+
declare enum BridgeEventType {
|
|
5
|
+
/** Request to invoke a method on the other side */
|
|
6
|
+
Request = "Request",
|
|
7
|
+
/** Response to a request */
|
|
8
|
+
Result = "Result",
|
|
9
|
+
/** Initialization handshake */
|
|
10
|
+
Init = "Init",
|
|
11
|
+
/** Response to initialization */
|
|
12
|
+
InitResult = "InitResult"
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Result types for bridge responses
|
|
16
|
+
*/
|
|
17
|
+
declare enum BridgeResultType {
|
|
18
|
+
/** Successful response */
|
|
19
|
+
Success = "Success",
|
|
20
|
+
/** Error response */
|
|
21
|
+
Error = "Error"
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Error types that can occur during bridge communication
|
|
25
|
+
*/
|
|
26
|
+
declare enum BridgeErrorType {
|
|
27
|
+
/** Handler took longer than timeout (default: 100s) */
|
|
28
|
+
METHOD_EXECUTION_TIMEOUT = "METHOD_EXECUTION_TIMEOUT",
|
|
29
|
+
/** Method is not registered on the receiving side */
|
|
30
|
+
UNSUPPORTED_METHOD = "UNSUPPORTED_METHOD",
|
|
31
|
+
/** Handler threw an error */
|
|
32
|
+
REJECTED = "REJECTED",
|
|
33
|
+
/** Bridge is not initialized or unavailable */
|
|
34
|
+
BRIDGE_NOT_AVAILABLE = "BRIDGE_NOT_AVAILABLE"
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Error data returned when a bridge call fails
|
|
38
|
+
*/
|
|
39
|
+
interface BridgeResultError {
|
|
40
|
+
error_type: BridgeErrorType;
|
|
41
|
+
error_message?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Successful result data (any object)
|
|
45
|
+
*/
|
|
46
|
+
type BridgeResultSuccess = object;
|
|
47
|
+
/**
|
|
48
|
+
* Union type for result data
|
|
49
|
+
*/
|
|
50
|
+
type BridgeResultData = BridgeResultError | undefined | BridgeResultSuccess;
|
|
51
|
+
/**
|
|
52
|
+
* Result event sent back after processing a request
|
|
53
|
+
*/
|
|
54
|
+
interface BridgeResultEvent {
|
|
55
|
+
type?: BridgeResultType;
|
|
56
|
+
method?: string;
|
|
57
|
+
request_id?: string;
|
|
58
|
+
data?: BridgeResultData;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Request event sent to invoke a method
|
|
62
|
+
*/
|
|
63
|
+
interface BridgeRequestEvent {
|
|
64
|
+
method: string;
|
|
65
|
+
params: object;
|
|
66
|
+
request_id?: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Initialization event with available methods
|
|
70
|
+
*/
|
|
71
|
+
interface BridgeInitEvent {
|
|
72
|
+
methods: string[];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Initialization result (success/failure)
|
|
76
|
+
*/
|
|
77
|
+
type BridgeInitResultEvent = boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Union of all possible bridge data payloads
|
|
80
|
+
*/
|
|
81
|
+
type BridgeData = BridgeResultEvent | BridgeInitEvent | BridgeInitResultEvent | BridgeRequestEvent;
|
|
82
|
+
/**
|
|
83
|
+
* Complete bridge event with type and data
|
|
84
|
+
*/
|
|
85
|
+
interface BridgeEvent {
|
|
86
|
+
type: BridgeEventType;
|
|
87
|
+
data: BridgeData;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handler function for processing incoming requests
|
|
91
|
+
*/
|
|
92
|
+
type BridgeHandler<TParams = object, TResult = unknown> = (params: TParams) => Promise<TResult>;
|
|
93
|
+
/**
|
|
94
|
+
* Map of method names to their handlers
|
|
95
|
+
*/
|
|
96
|
+
interface BridgeHandlers {
|
|
97
|
+
[key: string]: BridgeHandler<any, any>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Listener function for bridge result events
|
|
101
|
+
*/
|
|
102
|
+
type BridgeListener = (result: BridgeResultEvent) => void;
|
|
103
|
+
/**
|
|
104
|
+
* Options for bridge initialization
|
|
105
|
+
*/
|
|
106
|
+
interface BridgeOptions {
|
|
107
|
+
/** Timeout for method execution in milliseconds (default: 100000) */
|
|
108
|
+
timeout?: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type InternalEventSender = (event: BridgeEvent) => void;
|
|
112
|
+
/**
|
|
113
|
+
* BridgeInternal handles the business logic of the bridge protocol.
|
|
114
|
+
* It manages request/response lifecycle, handler registration, and event routing.
|
|
115
|
+
*/
|
|
116
|
+
declare class BridgeInternal {
|
|
117
|
+
private requests;
|
|
118
|
+
private handlers;
|
|
119
|
+
private available;
|
|
120
|
+
private supportedMethods;
|
|
121
|
+
private listeners;
|
|
122
|
+
private initPromise?;
|
|
123
|
+
private readonly sendEvent;
|
|
124
|
+
private readonly timeout;
|
|
125
|
+
constructor(sendEvent: InternalEventSender, options?: BridgeOptions);
|
|
126
|
+
/**
|
|
127
|
+
* Subscribe to all result events
|
|
128
|
+
*/
|
|
129
|
+
subscribe: (listener: BridgeListener) => number;
|
|
130
|
+
/**
|
|
131
|
+
* Unsubscribe from result events
|
|
132
|
+
*/
|
|
133
|
+
unsubscribe: (listener: BridgeListener) => void;
|
|
134
|
+
private checkDiff;
|
|
135
|
+
/**
|
|
136
|
+
* Initialize the bridge with handlers
|
|
137
|
+
* @param handlers Map of method names to handler functions
|
|
138
|
+
* @returns Promise that resolves when the other side acknowledges
|
|
139
|
+
*/
|
|
140
|
+
init: (handlers?: BridgeHandlers) => Promise<boolean>;
|
|
141
|
+
/**
|
|
142
|
+
* Handle incoming bridge events
|
|
143
|
+
*/
|
|
144
|
+
handleCoreEvent: (event: BridgeEvent) => void;
|
|
145
|
+
/**
|
|
146
|
+
* Handle incoming requests and execute the appropriate handler
|
|
147
|
+
*/
|
|
148
|
+
handleRequest: (request: BridgeRequestEvent) => void;
|
|
149
|
+
private handleResult;
|
|
150
|
+
private handleRequestResult;
|
|
151
|
+
private handleInit;
|
|
152
|
+
private handleInitResult;
|
|
153
|
+
/**
|
|
154
|
+
* Send a request to the other side
|
|
155
|
+
* @param method Method name to invoke
|
|
156
|
+
* @param params Parameters to pass to the method
|
|
157
|
+
* @returns Promise that resolves with the result
|
|
158
|
+
*/
|
|
159
|
+
send: <TResult = unknown>(method: string, params: object) => Promise<TResult>;
|
|
160
|
+
/**
|
|
161
|
+
* Check if a method is supported by the other side
|
|
162
|
+
*/
|
|
163
|
+
supports: (method: string) => boolean;
|
|
164
|
+
/**
|
|
165
|
+
* Check if the bridge is available (initialized)
|
|
166
|
+
*/
|
|
167
|
+
isAvailable: () => boolean;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* BridgeBase provides the public API for bridge communication.
|
|
172
|
+
* It wraps BridgeInternal and exposes a clean interface for consumers.
|
|
173
|
+
*/
|
|
174
|
+
declare class BridgeBase {
|
|
175
|
+
protected bridge: BridgeInternal;
|
|
176
|
+
constructor(bridge: BridgeInternal);
|
|
177
|
+
/**
|
|
178
|
+
* Check if a method is supported by the other side
|
|
179
|
+
* @param method Method name to check
|
|
180
|
+
*/
|
|
181
|
+
supports: (method: string) => boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Check if the bridge is available (initialized)
|
|
184
|
+
*/
|
|
185
|
+
isAvailable: () => boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Send a request to invoke a method on the other side
|
|
188
|
+
* @param method Method name to invoke
|
|
189
|
+
* @param params Parameters to pass
|
|
190
|
+
* @returns Promise resolving with the result
|
|
191
|
+
*/
|
|
192
|
+
send: <TResult = unknown>(method: string, params?: object) => Promise<TResult>;
|
|
193
|
+
/**
|
|
194
|
+
* Subscribe to all result events
|
|
195
|
+
* @param listener Callback for result events
|
|
196
|
+
* @returns Subscription index
|
|
197
|
+
*/
|
|
198
|
+
subscribe: (listener: BridgeListener) => number;
|
|
199
|
+
/**
|
|
200
|
+
* Unsubscribe from result events
|
|
201
|
+
* @param listener The listener to remove
|
|
202
|
+
*/
|
|
203
|
+
unsubscribe: (listener: BridgeListener) => void;
|
|
204
|
+
/**
|
|
205
|
+
* Initialize the bridge with handlers
|
|
206
|
+
* @param handlers Map of method names to handler functions
|
|
207
|
+
* @returns Promise resolving when initialization is complete
|
|
208
|
+
*/
|
|
209
|
+
init: (handlers?: BridgeHandlers) => Promise<boolean>;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* AspectlyBridge is the main entry point for bridge communication.
|
|
214
|
+
* Use this class when running inside a WebView or iframe that needs
|
|
215
|
+
* to communicate with its parent container.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* // Inside a WebView or iframe
|
|
220
|
+
* const bridge = new AspectlyBridge();
|
|
221
|
+
*
|
|
222
|
+
* // Initialize with handlers
|
|
223
|
+
* await bridge.init({
|
|
224
|
+
* greet: async (params) => {
|
|
225
|
+
* return { message: `Hello, ${params.name}!` };
|
|
226
|
+
* }
|
|
227
|
+
* });
|
|
228
|
+
*
|
|
229
|
+
* // Send messages to parent
|
|
230
|
+
* const result = await bridge.send('someMethod', { data: 'value' });
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
declare class AspectlyBridge extends BridgeBase {
|
|
234
|
+
private cleanupSubscription;
|
|
235
|
+
constructor(options?: BridgeOptions);
|
|
236
|
+
/**
|
|
237
|
+
* Cleanup bridge subscriptions
|
|
238
|
+
*/
|
|
239
|
+
destroy: () => void;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export { AspectlyBridge as A, BridgeBase as B, BridgeInternal as a, type BridgeEvent as b, type BridgeData as c, type BridgeHandler as d, type BridgeHandlers as e, type BridgeListener as f, type BridgeOptions as g, type BridgeRequestEvent as h, type BridgeResultEvent as i, type BridgeResultError as j, type BridgeResultSuccess as k, type BridgeResultData as l, type BridgeInitEvent as m, type BridgeInitResultEvent as n, BridgeEventType as o, BridgeResultType as p, BridgeErrorType as q };
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event types used in bridge communication protocol
|
|
3
|
+
*/
|
|
4
|
+
declare enum BridgeEventType {
|
|
5
|
+
/** Request to invoke a method on the other side */
|
|
6
|
+
Request = "Request",
|
|
7
|
+
/** Response to a request */
|
|
8
|
+
Result = "Result",
|
|
9
|
+
/** Initialization handshake */
|
|
10
|
+
Init = "Init",
|
|
11
|
+
/** Response to initialization */
|
|
12
|
+
InitResult = "InitResult"
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Result types for bridge responses
|
|
16
|
+
*/
|
|
17
|
+
declare enum BridgeResultType {
|
|
18
|
+
/** Successful response */
|
|
19
|
+
Success = "Success",
|
|
20
|
+
/** Error response */
|
|
21
|
+
Error = "Error"
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Error types that can occur during bridge communication
|
|
25
|
+
*/
|
|
26
|
+
declare enum BridgeErrorType {
|
|
27
|
+
/** Handler took longer than timeout (default: 100s) */
|
|
28
|
+
METHOD_EXECUTION_TIMEOUT = "METHOD_EXECUTION_TIMEOUT",
|
|
29
|
+
/** Method is not registered on the receiving side */
|
|
30
|
+
UNSUPPORTED_METHOD = "UNSUPPORTED_METHOD",
|
|
31
|
+
/** Handler threw an error */
|
|
32
|
+
REJECTED = "REJECTED",
|
|
33
|
+
/** Bridge is not initialized or unavailable */
|
|
34
|
+
BRIDGE_NOT_AVAILABLE = "BRIDGE_NOT_AVAILABLE"
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Error data returned when a bridge call fails
|
|
38
|
+
*/
|
|
39
|
+
interface BridgeResultError {
|
|
40
|
+
error_type: BridgeErrorType;
|
|
41
|
+
error_message?: string;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Successful result data (any object)
|
|
45
|
+
*/
|
|
46
|
+
type BridgeResultSuccess = object;
|
|
47
|
+
/**
|
|
48
|
+
* Union type for result data
|
|
49
|
+
*/
|
|
50
|
+
type BridgeResultData = BridgeResultError | undefined | BridgeResultSuccess;
|
|
51
|
+
/**
|
|
52
|
+
* Result event sent back after processing a request
|
|
53
|
+
*/
|
|
54
|
+
interface BridgeResultEvent {
|
|
55
|
+
type?: BridgeResultType;
|
|
56
|
+
method?: string;
|
|
57
|
+
request_id?: string;
|
|
58
|
+
data?: BridgeResultData;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Request event sent to invoke a method
|
|
62
|
+
*/
|
|
63
|
+
interface BridgeRequestEvent {
|
|
64
|
+
method: string;
|
|
65
|
+
params: object;
|
|
66
|
+
request_id?: string;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Initialization event with available methods
|
|
70
|
+
*/
|
|
71
|
+
interface BridgeInitEvent {
|
|
72
|
+
methods: string[];
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Initialization result (success/failure)
|
|
76
|
+
*/
|
|
77
|
+
type BridgeInitResultEvent = boolean;
|
|
78
|
+
/**
|
|
79
|
+
* Union of all possible bridge data payloads
|
|
80
|
+
*/
|
|
81
|
+
type BridgeData = BridgeResultEvent | BridgeInitEvent | BridgeInitResultEvent | BridgeRequestEvent;
|
|
82
|
+
/**
|
|
83
|
+
* Complete bridge event with type and data
|
|
84
|
+
*/
|
|
85
|
+
interface BridgeEvent {
|
|
86
|
+
type: BridgeEventType;
|
|
87
|
+
data: BridgeData;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Handler function for processing incoming requests
|
|
91
|
+
*/
|
|
92
|
+
type BridgeHandler<TParams = object, TResult = unknown> = (params: TParams) => Promise<TResult>;
|
|
93
|
+
/**
|
|
94
|
+
* Map of method names to their handlers
|
|
95
|
+
*/
|
|
96
|
+
interface BridgeHandlers {
|
|
97
|
+
[key: string]: BridgeHandler<any, any>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Listener function for bridge result events
|
|
101
|
+
*/
|
|
102
|
+
type BridgeListener = (result: BridgeResultEvent) => void;
|
|
103
|
+
/**
|
|
104
|
+
* Options for bridge initialization
|
|
105
|
+
*/
|
|
106
|
+
interface BridgeOptions {
|
|
107
|
+
/** Timeout for method execution in milliseconds (default: 100000) */
|
|
108
|
+
timeout?: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
type InternalEventSender = (event: BridgeEvent) => void;
|
|
112
|
+
/**
|
|
113
|
+
* BridgeInternal handles the business logic of the bridge protocol.
|
|
114
|
+
* It manages request/response lifecycle, handler registration, and event routing.
|
|
115
|
+
*/
|
|
116
|
+
declare class BridgeInternal {
|
|
117
|
+
private requests;
|
|
118
|
+
private handlers;
|
|
119
|
+
private available;
|
|
120
|
+
private supportedMethods;
|
|
121
|
+
private listeners;
|
|
122
|
+
private initPromise?;
|
|
123
|
+
private readonly sendEvent;
|
|
124
|
+
private readonly timeout;
|
|
125
|
+
constructor(sendEvent: InternalEventSender, options?: BridgeOptions);
|
|
126
|
+
/**
|
|
127
|
+
* Subscribe to all result events
|
|
128
|
+
*/
|
|
129
|
+
subscribe: (listener: BridgeListener) => number;
|
|
130
|
+
/**
|
|
131
|
+
* Unsubscribe from result events
|
|
132
|
+
*/
|
|
133
|
+
unsubscribe: (listener: BridgeListener) => void;
|
|
134
|
+
private checkDiff;
|
|
135
|
+
/**
|
|
136
|
+
* Initialize the bridge with handlers
|
|
137
|
+
* @param handlers Map of method names to handler functions
|
|
138
|
+
* @returns Promise that resolves when the other side acknowledges
|
|
139
|
+
*/
|
|
140
|
+
init: (handlers?: BridgeHandlers) => Promise<boolean>;
|
|
141
|
+
/**
|
|
142
|
+
* Handle incoming bridge events
|
|
143
|
+
*/
|
|
144
|
+
handleCoreEvent: (event: BridgeEvent) => void;
|
|
145
|
+
/**
|
|
146
|
+
* Handle incoming requests and execute the appropriate handler
|
|
147
|
+
*/
|
|
148
|
+
handleRequest: (request: BridgeRequestEvent) => void;
|
|
149
|
+
private handleResult;
|
|
150
|
+
private handleRequestResult;
|
|
151
|
+
private handleInit;
|
|
152
|
+
private handleInitResult;
|
|
153
|
+
/**
|
|
154
|
+
* Send a request to the other side
|
|
155
|
+
* @param method Method name to invoke
|
|
156
|
+
* @param params Parameters to pass to the method
|
|
157
|
+
* @returns Promise that resolves with the result
|
|
158
|
+
*/
|
|
159
|
+
send: <TResult = unknown>(method: string, params: object) => Promise<TResult>;
|
|
160
|
+
/**
|
|
161
|
+
* Check if a method is supported by the other side
|
|
162
|
+
*/
|
|
163
|
+
supports: (method: string) => boolean;
|
|
164
|
+
/**
|
|
165
|
+
* Check if the bridge is available (initialized)
|
|
166
|
+
*/
|
|
167
|
+
isAvailable: () => boolean;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* BridgeBase provides the public API for bridge communication.
|
|
172
|
+
* It wraps BridgeInternal and exposes a clean interface for consumers.
|
|
173
|
+
*/
|
|
174
|
+
declare class BridgeBase {
|
|
175
|
+
protected bridge: BridgeInternal;
|
|
176
|
+
constructor(bridge: BridgeInternal);
|
|
177
|
+
/**
|
|
178
|
+
* Check if a method is supported by the other side
|
|
179
|
+
* @param method Method name to check
|
|
180
|
+
*/
|
|
181
|
+
supports: (method: string) => boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Check if the bridge is available (initialized)
|
|
184
|
+
*/
|
|
185
|
+
isAvailable: () => boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Send a request to invoke a method on the other side
|
|
188
|
+
* @param method Method name to invoke
|
|
189
|
+
* @param params Parameters to pass
|
|
190
|
+
* @returns Promise resolving with the result
|
|
191
|
+
*/
|
|
192
|
+
send: <TResult = unknown>(method: string, params?: object) => Promise<TResult>;
|
|
193
|
+
/**
|
|
194
|
+
* Subscribe to all result events
|
|
195
|
+
* @param listener Callback for result events
|
|
196
|
+
* @returns Subscription index
|
|
197
|
+
*/
|
|
198
|
+
subscribe: (listener: BridgeListener) => number;
|
|
199
|
+
/**
|
|
200
|
+
* Unsubscribe from result events
|
|
201
|
+
* @param listener The listener to remove
|
|
202
|
+
*/
|
|
203
|
+
unsubscribe: (listener: BridgeListener) => void;
|
|
204
|
+
/**
|
|
205
|
+
* Initialize the bridge with handlers
|
|
206
|
+
* @param handlers Map of method names to handler functions
|
|
207
|
+
* @returns Promise resolving when initialization is complete
|
|
208
|
+
*/
|
|
209
|
+
init: (handlers?: BridgeHandlers) => Promise<boolean>;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* AspectlyBridge is the main entry point for bridge communication.
|
|
214
|
+
* Use this class when running inside a WebView or iframe that needs
|
|
215
|
+
* to communicate with its parent container.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* // Inside a WebView or iframe
|
|
220
|
+
* const bridge = new AspectlyBridge();
|
|
221
|
+
*
|
|
222
|
+
* // Initialize with handlers
|
|
223
|
+
* await bridge.init({
|
|
224
|
+
* greet: async (params) => {
|
|
225
|
+
* return { message: `Hello, ${params.name}!` };
|
|
226
|
+
* }
|
|
227
|
+
* });
|
|
228
|
+
*
|
|
229
|
+
* // Send messages to parent
|
|
230
|
+
* const result = await bridge.send('someMethod', { data: 'value' });
|
|
231
|
+
* ```
|
|
232
|
+
*/
|
|
233
|
+
declare class AspectlyBridge extends BridgeBase {
|
|
234
|
+
private cleanupSubscription;
|
|
235
|
+
constructor(options?: BridgeOptions);
|
|
236
|
+
/**
|
|
237
|
+
* Cleanup bridge subscriptions
|
|
238
|
+
*/
|
|
239
|
+
destroy: () => void;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export { AspectlyBridge as A, BridgeBase as B, BridgeInternal as a, type BridgeEvent as b, type BridgeData as c, type BridgeHandler as d, type BridgeHandlers as e, type BridgeListener as f, type BridgeOptions as g, type BridgeRequestEvent as h, type BridgeResultEvent as i, type BridgeResultError as j, type BridgeResultSuccess as k, type BridgeResultData as l, type BridgeInitEvent as m, type BridgeInitResultEvent as n, BridgeEventType as o, BridgeResultType as p, BridgeErrorType as q };
|