@final-commerce/command-frame 0.1.51 → 0.1.52
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/actions/extension-refund/extension-refund-listener.d.ts +2 -2
- package/dist/actions/extension-refund/extension-refund-listener.js +3 -3
- package/dist/client.d.ts +1 -1
- package/dist/client.js +8 -8
- package/dist/hooks/index.js +4 -4
- package/dist/pubsub/subscriber.js +7 -7
- package/package.json +1 -1
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { ExtensionRefundParams, ExtensionRefundResponse } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* Install a message listener in the **extension iframe** so the host can request refunds.
|
|
4
|
-
* Validates `event.source === window.
|
|
4
|
+
* Validates `event.source === window.top` before handling.
|
|
5
5
|
*
|
|
6
|
-
* Replies with {@link PostMessageResponse}, using `event.origin` as the target origin when posting back to the
|
|
6
|
+
* Replies with {@link PostMessageResponse}, using `event.origin` as the target origin when posting back to the host.
|
|
7
7
|
*
|
|
8
8
|
* @param handler - Perform the extension-side refund (API call, etc.)
|
|
9
9
|
* @returns Unsubscribe function
|
|
@@ -4,9 +4,9 @@ function isRecord(value) {
|
|
|
4
4
|
}
|
|
5
5
|
/**
|
|
6
6
|
* Install a message listener in the **extension iframe** so the host can request refunds.
|
|
7
|
-
* Validates `event.source === window.
|
|
7
|
+
* Validates `event.source === window.top` before handling.
|
|
8
8
|
*
|
|
9
|
-
* Replies with {@link PostMessageResponse}, using `event.origin` as the target origin when posting back to the
|
|
9
|
+
* Replies with {@link PostMessageResponse}, using `event.origin` as the target origin when posting back to the host.
|
|
10
10
|
*
|
|
11
11
|
* @param handler - Perform the extension-side refund (API call, etc.)
|
|
12
12
|
* @returns Unsubscribe function
|
|
@@ -21,7 +21,7 @@ export function installExtensionRefundListener(handler) {
|
|
|
21
21
|
if (typeof requestId !== "string") {
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
|
-
if (event.source !== window.
|
|
24
|
+
if (event.source !== window.top || !event.source) {
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
27
|
const source = event.source;
|
package/dist/client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Command Frame Client for iframe communication
|
|
3
|
-
* Allows the iframe to call functions on the
|
|
3
|
+
* Allows the iframe to call functions on the host (top) window via postMessage
|
|
4
4
|
*/
|
|
5
5
|
export interface PostMessageRequest<T = any> {
|
|
6
6
|
action: string;
|
package/dist/client.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Command Frame Client for iframe communication
|
|
3
|
-
* Allows the iframe to call functions on the
|
|
3
|
+
* Allows the iframe to call functions on the host (top) window via postMessage
|
|
4
4
|
*/
|
|
5
5
|
export class CommandFrameClient {
|
|
6
6
|
constructor(options = {}) {
|
|
@@ -14,7 +14,7 @@ export class CommandFrameClient {
|
|
|
14
14
|
this.mockRegistry = options.mockRegistry || {};
|
|
15
15
|
// If running standalone (no parent window), force Mock Mode immediately
|
|
16
16
|
// This prevents the 2s delay and ensures immediate response in dev/standalone mode
|
|
17
|
-
if (typeof window !== 'undefined' && (!window.
|
|
17
|
+
if (typeof window !== 'undefined' && (!window.top || window.top === window)) {
|
|
18
18
|
if (this.isDebugEnabled()) {
|
|
19
19
|
console.log("[ActionsClient] Standalone mode detected. Enabling Mock Mode immediately.");
|
|
20
20
|
}
|
|
@@ -118,15 +118,15 @@ export class CommandFrameClient {
|
|
|
118
118
|
timestamp: new Date().toISOString()
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
|
-
if (typeof window !== 'undefined' && window.
|
|
122
|
-
window.
|
|
121
|
+
if (typeof window !== 'undefined' && window.top) {
|
|
122
|
+
window.top.postMessage(message, this.origin);
|
|
123
123
|
}
|
|
124
124
|
else {
|
|
125
125
|
clearTimeout(timeoutHandle);
|
|
126
126
|
this.pendingRequests.delete(requestId);
|
|
127
|
-
const error = new Error("No
|
|
127
|
+
const error = new Error("No host window found. This app must run in an iframe.");
|
|
128
128
|
if (this.isDebugEnabled()) {
|
|
129
|
-
console.error("[ActionsClient] No
|
|
129
|
+
console.error("[ActionsClient] No host window", error);
|
|
130
130
|
}
|
|
131
131
|
reject(error);
|
|
132
132
|
}
|
|
@@ -135,7 +135,7 @@ export class CommandFrameClient {
|
|
|
135
135
|
// Private check to determine environment
|
|
136
136
|
detectContext() {
|
|
137
137
|
return new Promise((resolve) => {
|
|
138
|
-
if (typeof window === 'undefined' || !window.
|
|
138
|
+
if (typeof window === 'undefined' || !window.top || window.top === window)
|
|
139
139
|
return resolve(null);
|
|
140
140
|
const requestId = this.generateRequestId();
|
|
141
141
|
const timeout = setTimeout(() => {
|
|
@@ -148,7 +148,7 @@ export class CommandFrameClient {
|
|
|
148
148
|
timeout
|
|
149
149
|
});
|
|
150
150
|
try {
|
|
151
|
-
window.
|
|
151
|
+
window.top.postMessage({ action: "getFinalContext", requestId }, this.origin);
|
|
152
152
|
}
|
|
153
153
|
catch {
|
|
154
154
|
clearTimeout(timeout);
|
package/dist/hooks/index.js
CHANGED
|
@@ -11,8 +11,8 @@ function getOrigin() {
|
|
|
11
11
|
function register(topic, callback, options) {
|
|
12
12
|
const hookId = options.hookId;
|
|
13
13
|
const functionBody = callback.toString();
|
|
14
|
-
if (typeof window !== "undefined" && window.
|
|
15
|
-
window.
|
|
14
|
+
if (typeof window !== "undefined" && window.top && window.top !== window) {
|
|
15
|
+
window.top.postMessage({
|
|
16
16
|
type: "hook-register",
|
|
17
17
|
topic,
|
|
18
18
|
functionBody,
|
|
@@ -26,8 +26,8 @@ function register(topic, callback, options) {
|
|
|
26
26
|
* Unregister a hook by ID. Sends a message to the host to remove the hook.
|
|
27
27
|
*/
|
|
28
28
|
function unregister(hookId) {
|
|
29
|
-
if (typeof window !== "undefined" && window.
|
|
30
|
-
window.
|
|
29
|
+
if (typeof window !== "undefined" && window.top && window.top !== window) {
|
|
30
|
+
window.top.postMessage({
|
|
31
31
|
type: "hook-unregister",
|
|
32
32
|
hookId
|
|
33
33
|
}, getOrigin());
|
|
@@ -12,8 +12,8 @@ export class TopicSubscriber {
|
|
|
12
12
|
this.origin = options.origin || "*";
|
|
13
13
|
this.debug = options.debug ?? false;
|
|
14
14
|
this.useGlobalDebug = options.debug === undefined;
|
|
15
|
-
// Detect standalone mode (
|
|
16
|
-
if (typeof window !== 'undefined' && (!window.
|
|
15
|
+
// Detect standalone mode (not inside any iframe)
|
|
16
|
+
if (typeof window !== 'undefined' && (!window.top || window.top === window)) {
|
|
17
17
|
this.mockMode = true;
|
|
18
18
|
if (this.isDebugEnabled()) {
|
|
19
19
|
console.log("[TopicSubscriber] Mock Mode enabled (standalone mode detected)");
|
|
@@ -70,7 +70,7 @@ export class TopicSubscriber {
|
|
|
70
70
|
* Request the list of available topics from the host
|
|
71
71
|
*/
|
|
72
72
|
requestTopics() {
|
|
73
|
-
if (typeof window !== "undefined" && window.
|
|
73
|
+
if (typeof window !== "undefined" && window.top && window.top !== window) {
|
|
74
74
|
const message = {
|
|
75
75
|
type: "pubsub-request-topics",
|
|
76
76
|
requestId: `topics_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
|
|
@@ -78,7 +78,7 @@ export class TopicSubscriber {
|
|
|
78
78
|
if (this.isDebugEnabled()) {
|
|
79
79
|
console.log("[TopicSubscriber] Requesting topics list", message);
|
|
80
80
|
}
|
|
81
|
-
window.
|
|
81
|
+
window.top.postMessage(message, this.origin);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
/**
|
|
@@ -177,7 +177,7 @@ export class TopicSubscriber {
|
|
|
177
177
|
* Notify host about subscription changes
|
|
178
178
|
*/
|
|
179
179
|
notifySubscription(topic, isSubscribed) {
|
|
180
|
-
if (typeof window !== "undefined" && window.
|
|
180
|
+
if (typeof window !== "undefined" && window.top && window.top !== window) {
|
|
181
181
|
const message = {
|
|
182
182
|
type: isSubscribed ? "pubsub-subscribe" : "pubsub-unsubscribe",
|
|
183
183
|
topic,
|
|
@@ -186,7 +186,7 @@ export class TopicSubscriber {
|
|
|
186
186
|
if (this.isDebugEnabled()) {
|
|
187
187
|
console.log("[TopicSubscriber] Notifying subscription change", message);
|
|
188
188
|
}
|
|
189
|
-
window.
|
|
189
|
+
window.top.postMessage(message, this.origin);
|
|
190
190
|
}
|
|
191
191
|
}
|
|
192
192
|
/**
|
|
@@ -203,7 +203,7 @@ export class TopicSubscriber {
|
|
|
203
203
|
return;
|
|
204
204
|
}
|
|
205
205
|
const data = event.data;
|
|
206
|
-
// Clear detection timeout on first valid message from
|
|
206
|
+
// Clear detection timeout on first valid message from host
|
|
207
207
|
if (this.detectionTimeout && data && (data.type === "pubsub-event" || data.type === "pubsub-topics-list")) {
|
|
208
208
|
clearTimeout(this.detectionTimeout);
|
|
209
209
|
this.detectionTimeout = undefined;
|