forgeframe 1.0.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/dist/communication/bridge.d.ts +167 -0
- package/dist/communication/index.d.ts +12 -0
- package/dist/communication/messenger.d.ts +142 -0
- package/dist/communication/protocol.d.ts +75 -0
- package/dist/constants.d.ts +202 -0
- package/dist/core/child.d.ts +223 -0
- package/dist/core/component.d.ts +135 -0
- package/dist/core/index.d.ts +12 -0
- package/dist/core/parent.d.ts +262 -0
- package/dist/drivers/index.d.ts +18 -0
- package/dist/drivers/react.d.ts +225 -0
- package/dist/events/emitter.d.ts +150 -0
- package/dist/forgeframe.js +2419 -0
- package/dist/index.d.ts +169 -0
- package/dist/props/definitions.d.ts +72 -0
- package/dist/props/index.d.ts +11 -0
- package/dist/props/normalize.d.ts +59 -0
- package/dist/props/serialize.d.ts +60 -0
- package/dist/render/iframe.d.ts +213 -0
- package/dist/render/index.d.ts +38 -0
- package/dist/render/popup.d.ts +215 -0
- package/dist/render/templates.d.ts +202 -0
- package/dist/types.d.ts +906 -0
- package/dist/utils/cleanup.d.ts +122 -0
- package/dist/utils/dimension.d.ts +50 -0
- package/dist/utils/index.d.ts +37 -0
- package/dist/utils/promise.d.ts +155 -0
- package/dist/utils/uid.d.ts +60 -0
- package/dist/window/helpers.d.ts +316 -0
- package/dist/window/index.d.ts +13 -0
- package/dist/window/name-payload.d.ts +188 -0
- package/dist/window/proxy.d.ts +168 -0
- package/package.json +45 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import type { DomainMatcher } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Gets the domain (origin) of the specified window.
|
|
4
|
+
*
|
|
5
|
+
* @param win - The window to get the domain from. Defaults to the current window.
|
|
6
|
+
* @returns The origin string (e.g., "https://example.com") or an empty string if cross-origin access is denied.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* This function safely handles cross-origin windows by catching security exceptions
|
|
10
|
+
* and returning an empty string instead of throwing.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* // Get current window's domain
|
|
15
|
+
* const domain = getDomain();
|
|
16
|
+
*
|
|
17
|
+
* // Get an iframe's domain
|
|
18
|
+
* const iframeDomain = getDomain(iframe.contentWindow);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export declare function getDomain(win?: Window): string;
|
|
24
|
+
/**
|
|
25
|
+
* Checks if a window is from the same domain as a reference window.
|
|
26
|
+
*
|
|
27
|
+
* @param win - The window to check.
|
|
28
|
+
* @param reference - The reference window to compare against. Defaults to the current window.
|
|
29
|
+
* @returns `true` if both windows share the same origin, `false` otherwise.
|
|
30
|
+
*
|
|
31
|
+
* @remarks
|
|
32
|
+
* This function compares the origin of both windows. If cross-origin access
|
|
33
|
+
* throws a security exception, it returns `false`.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* // Check if an iframe is same-origin
|
|
38
|
+
* if (isSameDomain(iframe.contentWindow)) {
|
|
39
|
+
* // Safe to access iframe's DOM directly
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export declare function isSameDomain(win: Window, reference?: Window): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Checks if a domain matches a given pattern.
|
|
48
|
+
*
|
|
49
|
+
* @param pattern - The pattern to match against. Can be a string (exact match or `"*"` wildcard),
|
|
50
|
+
* a RegExp, or an array of patterns.
|
|
51
|
+
* @param domain - The domain string to test.
|
|
52
|
+
* @returns `true` if the domain matches the pattern, `false` otherwise.
|
|
53
|
+
*
|
|
54
|
+
* @remarks
|
|
55
|
+
* Pattern matching rules:
|
|
56
|
+
* - `"*"` matches any domain
|
|
57
|
+
* - String patterns require exact match
|
|
58
|
+
* - RegExp patterns use `.test()` method
|
|
59
|
+
* - Array patterns return `true` if any element matches (OR logic)
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```typescript
|
|
63
|
+
* // Wildcard - matches everything
|
|
64
|
+
* matchDomain('*', 'https://example.com'); // true
|
|
65
|
+
*
|
|
66
|
+
* // Exact match
|
|
67
|
+
* matchDomain('https://example.com', 'https://example.com'); // true
|
|
68
|
+
*
|
|
69
|
+
* // RegExp match
|
|
70
|
+
* matchDomain(/\.example\.com$/, 'https://sub.example.com'); // true
|
|
71
|
+
*
|
|
72
|
+
* // Array of patterns
|
|
73
|
+
* matchDomain(['https://a.com', 'https://b.com'], 'https://b.com'); // true
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
export declare function matchDomain(pattern: DomainMatcher, domain: string): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Checks if a window is closed or inaccessible.
|
|
81
|
+
*
|
|
82
|
+
* @param win - The window to check, or `null`.
|
|
83
|
+
* @returns `true` if the window is `null`, closed, or inaccessible; `false` otherwise.
|
|
84
|
+
*
|
|
85
|
+
* @remarks
|
|
86
|
+
* This function safely handles cross-origin errors. If accessing the window's
|
|
87
|
+
* `closed` property throws, the window is considered closed or inaccessible.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const popup = window.open('https://example.com');
|
|
92
|
+
* // Later...
|
|
93
|
+
* if (isWindowClosed(popup)) {
|
|
94
|
+
* console.log('Popup was closed');
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
100
|
+
export declare function isWindowClosed(win: Window | null): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Gets the opener window for a popup window.
|
|
103
|
+
*
|
|
104
|
+
* @param win - The popup window. Defaults to the current window.
|
|
105
|
+
* @returns The opener window, or `null` if not a popup or cross-origin access is denied.
|
|
106
|
+
*
|
|
107
|
+
* @remarks
|
|
108
|
+
* The opener is the window that called `window.open()` to create this popup.
|
|
109
|
+
* This function safely handles cross-origin errors.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // In a popup window
|
|
114
|
+
* const parent = getOpener();
|
|
115
|
+
* if (parent) {
|
|
116
|
+
* // Communicate with opener
|
|
117
|
+
* }
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @public
|
|
121
|
+
*/
|
|
122
|
+
export declare function getOpener(win?: Window): Window | null;
|
|
123
|
+
/**
|
|
124
|
+
* Gets the parent window for an iframe.
|
|
125
|
+
*
|
|
126
|
+
* @param win - The iframe window. Defaults to the current window.
|
|
127
|
+
* @returns The parent window, or `null` if not in an iframe or cross-origin access is denied.
|
|
128
|
+
*
|
|
129
|
+
* @remarks
|
|
130
|
+
* Returns `null` if the window is the top-level window (i.e., `parent === self`).
|
|
131
|
+
* This function safely handles cross-origin errors.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```typescript
|
|
135
|
+
* // In an iframe
|
|
136
|
+
* const parent = getParent();
|
|
137
|
+
* if (parent) {
|
|
138
|
+
* // Communicate with parent frame
|
|
139
|
+
* }
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
144
|
+
export declare function getParent(win?: Window): Window | null;
|
|
145
|
+
/**
|
|
146
|
+
* Gets the top-level window in the frame hierarchy.
|
|
147
|
+
*
|
|
148
|
+
* @param win - The window to get the top from. Defaults to the current window.
|
|
149
|
+
* @returns The top-level window, or `null` if cross-origin access is denied.
|
|
150
|
+
*
|
|
151
|
+
* @remarks
|
|
152
|
+
* The top window is the outermost window in a nested iframe hierarchy.
|
|
153
|
+
* This function safely handles cross-origin errors.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const topWindow = getTop();
|
|
158
|
+
* if (topWindow === window) {
|
|
159
|
+
* console.log('We are the top-level window');
|
|
160
|
+
* }
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @public
|
|
164
|
+
*/
|
|
165
|
+
export declare function getTop(win?: Window): Window | null;
|
|
166
|
+
/**
|
|
167
|
+
* Checks if the specified window is running inside an iframe.
|
|
168
|
+
*
|
|
169
|
+
* @param win - The window to check. Defaults to the current window.
|
|
170
|
+
* @returns `true` if the window is in an iframe, `false` if it's the top-level window.
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* A window is considered to be in an iframe if `parent !== self`.
|
|
174
|
+
* If cross-origin access throws an exception, returns `true` as a conservative assumption.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* if (isIframe()) {
|
|
179
|
+
* console.log('Running inside an iframe');
|
|
180
|
+
* } else {
|
|
181
|
+
* console.log('Running as top-level window');
|
|
182
|
+
* }
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
* @public
|
|
186
|
+
*/
|
|
187
|
+
export declare function isIframe(win?: Window): boolean;
|
|
188
|
+
/**
|
|
189
|
+
* Checks if the specified window is a popup (opened via `window.open()`).
|
|
190
|
+
*
|
|
191
|
+
* @param win - The window to check. Defaults to the current window.
|
|
192
|
+
* @returns `true` if the window has an opener (is a popup), `false` otherwise.
|
|
193
|
+
*
|
|
194
|
+
* @remarks
|
|
195
|
+
* A popup window is one that was opened using `window.open()` and has an `opener` reference.
|
|
196
|
+
* This function safely handles cross-origin errors by returning `false`.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```typescript
|
|
200
|
+
* if (isPopup()) {
|
|
201
|
+
* console.log('This window was opened as a popup');
|
|
202
|
+
* }
|
|
203
|
+
* ```
|
|
204
|
+
*
|
|
205
|
+
* @public
|
|
206
|
+
*/
|
|
207
|
+
export declare function isPopup(win?: Window): boolean;
|
|
208
|
+
/**
|
|
209
|
+
* Gets an ancestor window at a specific distance in the frame hierarchy.
|
|
210
|
+
*
|
|
211
|
+
* @param win - The starting window. Defaults to the current window.
|
|
212
|
+
* @param distance - The number of levels to traverse up. 1 = parent, 2 = grandparent, etc.
|
|
213
|
+
* @returns The ancestor window at the specified distance, or `null` if not found.
|
|
214
|
+
*
|
|
215
|
+
* @remarks
|
|
216
|
+
* This function traverses up the parent chain the specified number of times.
|
|
217
|
+
* Returns `null` if the chain ends before reaching the target distance.
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```typescript
|
|
221
|
+
* // Get the grandparent window (2 levels up)
|
|
222
|
+
* const grandparent = getAncestor(window, 2);
|
|
223
|
+
*
|
|
224
|
+
* // Get the parent window (equivalent to getParent())
|
|
225
|
+
* const parent = getAncestor(window, 1);
|
|
226
|
+
* ```
|
|
227
|
+
*
|
|
228
|
+
* @public
|
|
229
|
+
*/
|
|
230
|
+
export declare function getAncestor(win: Window | undefined, distance: number): Window | null;
|
|
231
|
+
/**
|
|
232
|
+
* Calculates the distance (number of levels) from a child window to a parent window.
|
|
233
|
+
*
|
|
234
|
+
* @param child - The child window to start from.
|
|
235
|
+
* @param parent - The target parent window.
|
|
236
|
+
* @returns The number of levels between child and parent, or `-1` if the parent is not an ancestor.
|
|
237
|
+
*
|
|
238
|
+
* @remarks
|
|
239
|
+
* This function traverses up the parent chain from the child window, counting levels
|
|
240
|
+
* until it finds the target parent. Has a safety limit of 100 levels to prevent infinite loops.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* // If iframe is nested 2 levels deep
|
|
245
|
+
* const distance = getDistanceToParent(iframe.contentWindow, window.top);
|
|
246
|
+
* console.log(distance); // 2
|
|
247
|
+
*
|
|
248
|
+
* // If not an ancestor
|
|
249
|
+
* const notFound = getDistanceToParent(windowA, windowB);
|
|
250
|
+
* console.log(notFound); // -1
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* @public
|
|
254
|
+
*/
|
|
255
|
+
export declare function getDistanceToParent(child: Window, parent: Window): number;
|
|
256
|
+
/**
|
|
257
|
+
* Attempts to focus a window.
|
|
258
|
+
*
|
|
259
|
+
* @param win - The window to focus.
|
|
260
|
+
*
|
|
261
|
+
* @remarks
|
|
262
|
+
* This function safely attempts to focus the specified window.
|
|
263
|
+
* Focus may fail silently due to browser restrictions or cross-origin policies.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* const popup = window.open('https://example.com');
|
|
268
|
+
* focusWindow(popup);
|
|
269
|
+
* ```
|
|
270
|
+
*
|
|
271
|
+
* @public
|
|
272
|
+
*/
|
|
273
|
+
export declare function focusWindow(win: Window): void;
|
|
274
|
+
/**
|
|
275
|
+
* Attempts to close a window.
|
|
276
|
+
*
|
|
277
|
+
* @param win - The window to close.
|
|
278
|
+
*
|
|
279
|
+
* @remarks
|
|
280
|
+
* This function safely attempts to close the specified window.
|
|
281
|
+
* Close may fail silently due to browser restrictions (e.g., scripts can only
|
|
282
|
+
* close windows they opened).
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const popup = window.open('https://example.com');
|
|
287
|
+
* // Later, close the popup
|
|
288
|
+
* closeWindow(popup);
|
|
289
|
+
* ```
|
|
290
|
+
*
|
|
291
|
+
* @public
|
|
292
|
+
*/
|
|
293
|
+
export declare function closeWindow(win: Window): void;
|
|
294
|
+
/**
|
|
295
|
+
* Gets all child frames (iframes) within a window.
|
|
296
|
+
*
|
|
297
|
+
* @param win - The window to get frames from. Defaults to the current window.
|
|
298
|
+
* @returns An array of Window objects representing the child frames.
|
|
299
|
+
*
|
|
300
|
+
* @remarks
|
|
301
|
+
* This function iterates over the window's frames collection and returns them as an array.
|
|
302
|
+
* If cross-origin access is denied, returns an empty array.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```typescript
|
|
306
|
+
* const childFrames = getFrames();
|
|
307
|
+
* console.log(`Found ${childFrames.length} iframes`);
|
|
308
|
+
*
|
|
309
|
+
* childFrames.forEach((frame, index) => {
|
|
310
|
+
* console.log(`Frame ${index}:`, getDomain(frame));
|
|
311
|
+
* });
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @public
|
|
315
|
+
*/
|
|
316
|
+
export declare function getFrames(win?: Window): Window[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Window utilities for ForgeFrame cross-window communication.
|
|
3
|
+
*
|
|
4
|
+
* @remarks
|
|
5
|
+
* This module provides utilities for working with browser windows in cross-origin
|
|
6
|
+
* contexts. It includes helpers for domain matching, window hierarchy navigation,
|
|
7
|
+
* window name payload encoding/decoding, and window reference management.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
export { getDomain, isSameDomain, matchDomain, isWindowClosed, getOpener, getParent, getTop, isIframe, isPopup, getAncestor, getDistanceToParent, focusWindow, closeWindow, getFrames, } from './helpers';
|
|
12
|
+
export { buildWindowName, parseWindowName, isForgeFrameWindow, isChildOfComponent, createWindowPayload, updateWindowName, getInitialPayload, } from './name-payload';
|
|
13
|
+
export { registerWindow, unregisterWindow, getWindowByUID, createWindowRef, resolveWindowRef, serializeWindowRef, clearWindowRegistry, } from './proxy';
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type { WindowNamePayload, SerializedProps, ParentExports, ChildComponentRef } from '../types';
|
|
2
|
+
import type { ContextType } from '../constants';
|
|
3
|
+
/**
|
|
4
|
+
* Builds a window name string with an encoded payload.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam P - The type of the props included in the payload.
|
|
7
|
+
* @param payload - The payload to encode into the window name.
|
|
8
|
+
* @returns A window name string with the ForgeFrame prefix and base64-encoded payload.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* This is the primary mechanism for passing initial props from a parent window
|
|
12
|
+
* to a child window (iframe or popup). The payload is JSON-serialized and base64-encoded
|
|
13
|
+
* to ensure safe transport via the window.name property.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const payload: WindowNamePayload<MyProps> = {
|
|
18
|
+
* uid: 'child-123',
|
|
19
|
+
* tag: 'my-component',
|
|
20
|
+
* version: '1.0.0',
|
|
21
|
+
* context: 'iframe',
|
|
22
|
+
* parentDomain: 'https://parent.com',
|
|
23
|
+
* props: { message: 'Hello' },
|
|
24
|
+
* exports: {}
|
|
25
|
+
* };
|
|
26
|
+
*
|
|
27
|
+
* const windowName = buildWindowName(payload);
|
|
28
|
+
* // Use when creating iframe: iframe.name = windowName;
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
*/
|
|
33
|
+
export declare function buildWindowName<P>(payload: WindowNamePayload<P>): string;
|
|
34
|
+
/**
|
|
35
|
+
* Parses a window name to extract the encoded payload.
|
|
36
|
+
*
|
|
37
|
+
* @typeParam P - The expected type of the props in the payload.
|
|
38
|
+
* @param name - The window name string to parse.
|
|
39
|
+
* @returns The decoded WindowNamePayload, or `null` if the name is not a valid ForgeFrame window name.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* This function checks if the window name starts with the ForgeFrame prefix,
|
|
43
|
+
* then decodes the base64-encoded payload. Returns `null` if the name doesn't
|
|
44
|
+
* have the expected format or if decoding fails.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // In a child window
|
|
49
|
+
* const payload = parseWindowName<MyProps>(window.name);
|
|
50
|
+
* if (payload) {
|
|
51
|
+
* console.log('Received props:', payload.props);
|
|
52
|
+
* console.log('Parent domain:', payload.parentDomain);
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
export declare function parseWindowName<P>(name: string): WindowNamePayload<P> | null;
|
|
59
|
+
/**
|
|
60
|
+
* Checks if the specified window is a ForgeFrame child window.
|
|
61
|
+
*
|
|
62
|
+
* @param win - The window to check. Defaults to the current window.
|
|
63
|
+
* @returns `true` if the window's name starts with the ForgeFrame prefix, `false` otherwise.
|
|
64
|
+
*
|
|
65
|
+
* @remarks
|
|
66
|
+
* A ForgeFrame child window is identified by having a window name that starts
|
|
67
|
+
* with the ForgeFrame prefix. This function safely handles cross-origin errors.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```typescript
|
|
71
|
+
* if (isForgeFrameWindow()) {
|
|
72
|
+
* // This window was created by ForgeFrame
|
|
73
|
+
* const payload = getInitialPayload();
|
|
74
|
+
* }
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export declare function isForgeFrameWindow(win?: Window): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Checks if the current window is a child of a specific component tag.
|
|
82
|
+
*
|
|
83
|
+
* @param tag - The component tag to check for.
|
|
84
|
+
* @param win - The window to check. Defaults to the current window.
|
|
85
|
+
* @returns `true` if the window's payload has the specified tag, `false` otherwise.
|
|
86
|
+
*
|
|
87
|
+
* @remarks
|
|
88
|
+
* This function parses the window name and checks if the tag in the payload
|
|
89
|
+
* matches the specified tag. Useful for component-specific initialization logic.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* if (isChildOfComponent('payment-form')) {
|
|
94
|
+
* // Initialize payment-specific features
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* @public
|
|
99
|
+
*/
|
|
100
|
+
export declare function isChildOfComponent(tag: string, win?: Window): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Creates a window payload object for encoding into a window name.
|
|
103
|
+
*
|
|
104
|
+
* @typeParam P - The type of the props (used for type inference in the returned payload).
|
|
105
|
+
* @param options - The options for creating the payload.
|
|
106
|
+
* @param options.uid - Unique identifier for the child window.
|
|
107
|
+
* @param options.tag - Component tag name.
|
|
108
|
+
* @param options.context - The context type (e.g., 'iframe' or 'popup').
|
|
109
|
+
* @param options.parentDomain - The origin of the parent window.
|
|
110
|
+
* @param options.props - Serialized props to pass to the child.
|
|
111
|
+
* @param options.exports - Functions exported by the parent for the child to call.
|
|
112
|
+
* @param options.children - Optional map of child component references.
|
|
113
|
+
* @returns A WindowNamePayload object ready for encoding.
|
|
114
|
+
*
|
|
115
|
+
* @remarks
|
|
116
|
+
* This is a factory function that creates a properly structured payload with
|
|
117
|
+
* the current ForgeFrame version automatically included.
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const payload = createWindowPayload<MyProps>({
|
|
122
|
+
* uid: generateUID(),
|
|
123
|
+
* tag: 'checkout-form',
|
|
124
|
+
* context: 'iframe',
|
|
125
|
+
* parentDomain: window.location.origin,
|
|
126
|
+
* props: { amount: 100 },
|
|
127
|
+
* exports: { onSuccess: true, onError: true }
|
|
128
|
+
* });
|
|
129
|
+
*
|
|
130
|
+
* const windowName = buildWindowName(payload);
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
135
|
+
export declare function createWindowPayload<P>(options: {
|
|
136
|
+
uid: string;
|
|
137
|
+
tag: string;
|
|
138
|
+
context: ContextType;
|
|
139
|
+
parentDomain: string;
|
|
140
|
+
props: SerializedProps;
|
|
141
|
+
exports: ParentExports;
|
|
142
|
+
children?: Record<string, ChildComponentRef>;
|
|
143
|
+
}): WindowNamePayload<P>;
|
|
144
|
+
/**
|
|
145
|
+
* Updates a window's name with a new encoded payload.
|
|
146
|
+
*
|
|
147
|
+
* @typeParam P - The type of the props in the payload.
|
|
148
|
+
* @param win - The window whose name should be updated.
|
|
149
|
+
* @param payload - The new payload to encode into the window name.
|
|
150
|
+
*
|
|
151
|
+
* @remarks
|
|
152
|
+
* This function is used when the parent needs to update the child's reference
|
|
153
|
+
* or pass updated data. It safely handles cross-origin errors by silently failing.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* // Update child window with new props
|
|
158
|
+
* const updatedPayload = { ...existingPayload, props: { newValue: 42 } };
|
|
159
|
+
* updateWindowName(childWindow, updatedPayload);
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @public
|
|
163
|
+
*/
|
|
164
|
+
export declare function updateWindowName<P>(win: Window, payload: WindowNamePayload<P>): void;
|
|
165
|
+
/**
|
|
166
|
+
* Gets the initial payload from a window's name.
|
|
167
|
+
*
|
|
168
|
+
* @typeParam P - The expected type of the props in the payload.
|
|
169
|
+
* @param win - The window to read the payload from. Defaults to the current window.
|
|
170
|
+
* @returns The decoded WindowNamePayload, or `null` if not a ForgeFrame window.
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* This is a convenience function typically used by child components during
|
|
174
|
+
* initialization to read the data passed by the parent window.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```typescript
|
|
178
|
+
* // In child window initialization
|
|
179
|
+
* const payload = getInitialPayload<MyProps>();
|
|
180
|
+
* if (payload) {
|
|
181
|
+
* initializeComponent(payload.props);
|
|
182
|
+
* setupParentCommunication(payload.parentDomain);
|
|
183
|
+
* }
|
|
184
|
+
* ```
|
|
185
|
+
*
|
|
186
|
+
* @public
|
|
187
|
+
*/
|
|
188
|
+
export declare function getInitialPayload<P>(win?: Window): WindowNamePayload<P> | null;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import type { WindowRef } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Registers a window with a unique identifier for later retrieval.
|
|
4
|
+
*
|
|
5
|
+
* @param uid - The unique identifier for the window.
|
|
6
|
+
* @param win - The window to register.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* Registered windows can be retrieved later using {@link getWindowByUID}.
|
|
10
|
+
* This is useful for maintaining references to windows across different contexts.
|
|
11
|
+
*
|
|
12
|
+
* The registry automatically cleans up closed windows when new windows are
|
|
13
|
+
* registered to prevent memory leaks. A maximum of 100 windows can be
|
|
14
|
+
* registered at a time.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const popup = window.open('https://example.com');
|
|
19
|
+
* registerWindow('my-popup-123', popup);
|
|
20
|
+
*
|
|
21
|
+
* // Later, retrieve the window
|
|
22
|
+
* const retrievedPopup = getWindowByUID('my-popup-123');
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export declare function registerWindow(uid: string, win: Window): void;
|
|
28
|
+
/**
|
|
29
|
+
* Unregisters a window from the registry.
|
|
30
|
+
*
|
|
31
|
+
* @param uid - The unique identifier of the window to unregister.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* Call this function when a window is closed or no longer needed to prevent memory leaks.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* // When done with a popup
|
|
39
|
+
* unregisterWindow('my-popup-123');
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export declare function unregisterWindow(uid: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* Retrieves a registered window by its unique identifier.
|
|
47
|
+
*
|
|
48
|
+
* @param uid - The unique identifier of the window to retrieve.
|
|
49
|
+
* @returns The registered window, or `null` if not found.
|
|
50
|
+
*
|
|
51
|
+
* @remarks
|
|
52
|
+
* Windows must be registered using {@link registerWindow} before they can be retrieved.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* const popup = getWindowByUID('my-popup-123');
|
|
57
|
+
* if (popup) {
|
|
58
|
+
* popup.postMessage({ type: 'hello' }, '*');
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @public
|
|
63
|
+
*/
|
|
64
|
+
export declare function getWindowByUID(uid: string): Window | null;
|
|
65
|
+
/**
|
|
66
|
+
* Creates a serializable reference to a target window.
|
|
67
|
+
*
|
|
68
|
+
* @param targetWin - The window to create a reference for.
|
|
69
|
+
* @param sourceWin - The source window context. Defaults to the current window.
|
|
70
|
+
* @returns A WindowRef object that describes how to reach the target window.
|
|
71
|
+
*
|
|
72
|
+
* @remarks
|
|
73
|
+
* This function analyzes the relationship between the source and target windows
|
|
74
|
+
* and creates an appropriate reference type:
|
|
75
|
+
* - `opener`: Target is the source's opener (for popups)
|
|
76
|
+
* - `parent`: Target is an ancestor in the frame hierarchy
|
|
77
|
+
* - `direct`: Direct window reference (only works same-origin)
|
|
78
|
+
*
|
|
79
|
+
* The returned reference can be serialized for cross-window communication,
|
|
80
|
+
* except for `direct` type which contains an actual Window object.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* // Create a reference to the parent window
|
|
85
|
+
* const parentRef = createWindowRef(window.parent);
|
|
86
|
+
* console.log(parentRef); // { type: 'parent', distance: 1 }
|
|
87
|
+
*
|
|
88
|
+
* // Create a reference to the opener
|
|
89
|
+
* const openerRef = createWindowRef(window.opener);
|
|
90
|
+
* console.log(openerRef); // { type: 'opener' }
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
export declare function createWindowRef(targetWin: Window, sourceWin?: Window): WindowRef;
|
|
96
|
+
/**
|
|
97
|
+
* Resolves a window reference to an actual Window object.
|
|
98
|
+
*
|
|
99
|
+
* @param ref - The window reference to resolve.
|
|
100
|
+
* @param sourceWin - The source window context. Defaults to the current window.
|
|
101
|
+
* @returns The resolved Window object, or `null` if resolution fails.
|
|
102
|
+
*
|
|
103
|
+
* @remarks
|
|
104
|
+
* This function is the inverse of {@link createWindowRef}. It takes a WindowRef
|
|
105
|
+
* and returns the actual Window object it references. Supports all reference types:
|
|
106
|
+
* - `opener`: Returns the opener window
|
|
107
|
+
* - `parent`: Returns the ancestor at the specified distance
|
|
108
|
+
* - `global`: Looks up the window in the registry by UID
|
|
109
|
+
* - `direct`: Returns the directly referenced window
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```typescript
|
|
113
|
+
* // Resolve a parent reference
|
|
114
|
+
* const ref: WindowRef = { type: 'parent', distance: 1 };
|
|
115
|
+
* const parentWindow = resolveWindowRef(ref);
|
|
116
|
+
*
|
|
117
|
+
* // Resolve a global reference
|
|
118
|
+
* const globalRef: WindowRef = { type: 'global', uid: 'my-popup-123' };
|
|
119
|
+
* const popup = resolveWindowRef(globalRef);
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @public
|
|
123
|
+
*/
|
|
124
|
+
export declare function resolveWindowRef(ref: WindowRef, sourceWin?: Window): Window | null;
|
|
125
|
+
/**
|
|
126
|
+
* Serializes a window reference for cross-domain transfer.
|
|
127
|
+
*
|
|
128
|
+
* @param ref - The window reference to serialize.
|
|
129
|
+
* @returns The serialized window reference.
|
|
130
|
+
* @throws Error if the reference is a `direct` type, which cannot be serialized.
|
|
131
|
+
*
|
|
132
|
+
* @remarks
|
|
133
|
+
* Direct window references contain actual Window objects which cannot be
|
|
134
|
+
* transferred via postMessage. Convert direct references to global references
|
|
135
|
+
* using {@link registerWindow} before serialization.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* // This works - parent reference is serializable
|
|
140
|
+
* const parentRef: WindowRef = { type: 'parent', distance: 1 };
|
|
141
|
+
* const serialized = serializeWindowRef(parentRef);
|
|
142
|
+
*
|
|
143
|
+
* // This throws - direct references are not serializable
|
|
144
|
+
* const directRef: WindowRef = { type: 'direct', win: someWindow };
|
|
145
|
+
* serializeWindowRef(directRef); // Error!
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* @public
|
|
149
|
+
*/
|
|
150
|
+
export declare function serializeWindowRef(ref: WindowRef): WindowRef;
|
|
151
|
+
/**
|
|
152
|
+
* Clears all registered windows from the registry.
|
|
153
|
+
*
|
|
154
|
+
* @remarks
|
|
155
|
+
* This function removes all window entries from the global registry.
|
|
156
|
+
* Primarily useful for cleanup during testing or when resetting application state.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* // In a test teardown
|
|
161
|
+
* afterEach(() => {
|
|
162
|
+
* clearWindowRegistry();
|
|
163
|
+
* });
|
|
164
|
+
* ```
|
|
165
|
+
*
|
|
166
|
+
* @public
|
|
167
|
+
*/
|
|
168
|
+
export declare function clearWindowRegistry(): void;
|