@coasys/ad4m-connect 0.13.0-postmessage-ws-proxy.0 → 0.13.0-postmessage-ws-proxy.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/dist/PostMessageWebSocket.d.ts +5 -1
- package/dist/core.js +46 -7
- package/dist/core.js.map +3 -3
- package/dist/index.js +46 -7
- package/dist/index.js.map +3 -3
- package/package.json +16 -16
|
@@ -20,7 +20,11 @@ export declare class PostMessageWebSocket {
|
|
|
20
20
|
onerror: ((e: Event) => void) | null;
|
|
21
21
|
onclose: ((e: CloseEvent) => void) | null;
|
|
22
22
|
private readonly _messageHandler;
|
|
23
|
-
|
|
23
|
+
private _connectTimeout;
|
|
24
|
+
private readonly _targetOrigin;
|
|
25
|
+
static readonly CONNECT_TIMEOUT_MS = 30000;
|
|
26
|
+
constructor(_url: string, targetOrigin: string);
|
|
27
|
+
private _clearConnectTimeout;
|
|
24
28
|
send(data: string): void;
|
|
25
29
|
close(code?: number, reason?: string): void;
|
|
26
30
|
}
|
package/dist/core.js
CHANGED
|
@@ -73,21 +73,26 @@ function checkConnection(baseUrl, timeout = 1e4) {
|
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
// src/PostMessageWebSocket.ts
|
|
76
|
-
var
|
|
77
|
-
constructor(_url) {
|
|
76
|
+
var _PostMessageWebSocket = class {
|
|
77
|
+
constructor(_url, targetOrigin) {
|
|
78
78
|
this.readyState = 0;
|
|
79
79
|
this.onopen = null;
|
|
80
80
|
this.onmessage = null;
|
|
81
81
|
this.onerror = null;
|
|
82
82
|
this.onclose = null;
|
|
83
|
+
this._connectTimeout = null;
|
|
84
|
+
this._targetOrigin = targetOrigin;
|
|
83
85
|
this._messageHandler = (e) => {
|
|
84
86
|
var _a, _b, _c, _d;
|
|
85
87
|
if (e.source !== window.parent)
|
|
86
88
|
return;
|
|
89
|
+
if (e.origin !== this._targetOrigin)
|
|
90
|
+
return;
|
|
87
91
|
const msg = e.data;
|
|
88
92
|
if (!msg || typeof msg.type !== "string")
|
|
89
93
|
return;
|
|
90
94
|
if (msg.type === "AD4M_PROXY_WS_OPEN") {
|
|
95
|
+
this._clearConnectTimeout();
|
|
91
96
|
this.readyState = 1;
|
|
92
97
|
(_a = this.onopen) == null ? void 0 : _a.call(this, new Event("open"));
|
|
93
98
|
return;
|
|
@@ -97,10 +102,12 @@ var PostMessageWebSocket = class {
|
|
|
97
102
|
return;
|
|
98
103
|
}
|
|
99
104
|
if (msg.type === "AD4M_PROXY_WS_ERROR") {
|
|
105
|
+
this._clearConnectTimeout();
|
|
100
106
|
(_c = this.onerror) == null ? void 0 : _c.call(this, new Event("error"));
|
|
101
107
|
return;
|
|
102
108
|
}
|
|
103
109
|
if (msg.type === "AD4M_PROXY_WS_CLOSED") {
|
|
110
|
+
this._clearConnectTimeout();
|
|
104
111
|
this.readyState = 3;
|
|
105
112
|
(_d = this.onclose) == null ? void 0 : _d.call(
|
|
106
113
|
this,
|
|
@@ -114,21 +121,37 @@ var PostMessageWebSocket = class {
|
|
|
114
121
|
}
|
|
115
122
|
};
|
|
116
123
|
window.addEventListener("message", this._messageHandler);
|
|
117
|
-
window.parent.postMessage({ type: "AD4M_PROXY_WS_CONNECT" },
|
|
124
|
+
window.parent.postMessage({ type: "AD4M_PROXY_WS_CONNECT" }, this._targetOrigin);
|
|
125
|
+
this._connectTimeout = setTimeout(() => {
|
|
126
|
+
var _a, _b;
|
|
127
|
+
this._connectTimeout = null;
|
|
128
|
+
window.removeEventListener("message", this._messageHandler);
|
|
129
|
+
this.readyState = 3;
|
|
130
|
+
(_a = this.onerror) == null ? void 0 : _a.call(this, new Event("error"));
|
|
131
|
+
(_b = this.onclose) == null ? void 0 : _b.call(this, new CloseEvent("close", { code: 1006, reason: "Connection timeout", wasClean: false }));
|
|
132
|
+
}, _PostMessageWebSocket.CONNECT_TIMEOUT_MS);
|
|
133
|
+
}
|
|
134
|
+
_clearConnectTimeout() {
|
|
135
|
+
if (this._connectTimeout !== null) {
|
|
136
|
+
clearTimeout(this._connectTimeout);
|
|
137
|
+
this._connectTimeout = null;
|
|
138
|
+
}
|
|
118
139
|
}
|
|
119
140
|
send(data) {
|
|
120
|
-
window.parent.postMessage({ type: "AD4M_PROXY_WS_SEND", data },
|
|
141
|
+
window.parent.postMessage({ type: "AD4M_PROXY_WS_SEND", data }, this._targetOrigin);
|
|
121
142
|
}
|
|
122
143
|
close(code2, reason) {
|
|
123
144
|
this.readyState = 2;
|
|
124
|
-
window.parent.postMessage({ type: "AD4M_PROXY_WS_CLOSE", code: code2, reason },
|
|
145
|
+
window.parent.postMessage({ type: "AD4M_PROXY_WS_CLOSE", code: code2, reason }, this._targetOrigin);
|
|
125
146
|
window.removeEventListener("message", this._messageHandler);
|
|
126
147
|
}
|
|
127
148
|
};
|
|
149
|
+
var PostMessageWebSocket = _PostMessageWebSocket;
|
|
128
150
|
PostMessageWebSocket.CONNECTING = 0;
|
|
129
151
|
PostMessageWebSocket.OPEN = 1;
|
|
130
152
|
PostMessageWebSocket.CLOSING = 2;
|
|
131
153
|
PostMessageWebSocket.CLOSED = 3;
|
|
154
|
+
PostMessageWebSocket.CONNECT_TIMEOUT_MS = 3e4;
|
|
132
155
|
|
|
133
156
|
// ../core/lib/index.js
|
|
134
157
|
var RpcError = class extends Error {
|
|
@@ -11143,7 +11166,18 @@ var Ad4mConnect = class extends EventTarget {
|
|
|
11143
11166
|
console.warn("[Ad4m Connect] Rejected AD4M_CONFIG from invalid source (not parent window)");
|
|
11144
11167
|
return;
|
|
11145
11168
|
}
|
|
11146
|
-
if (
|
|
11169
|
+
if (event.data.proxy) {
|
|
11170
|
+
if (!this.options.allowedOrigins || this.options.allowedOrigins.length === 0) {
|
|
11171
|
+
console.error("[Ad4m Connect] proxy mode requires allowedOrigins to be configured. Rejecting AD4M_CONFIG to prevent arbitrary sites from embedding this app.");
|
|
11172
|
+
this.rejectEmbedded(new Error("proxy mode requires allowedOrigins"));
|
|
11173
|
+
return;
|
|
11174
|
+
}
|
|
11175
|
+
if (!event.origin || !this.options.allowedOrigins.includes(event.origin)) {
|
|
11176
|
+
console.warn("[Ad4m Connect] Rejected AD4M_CONFIG from unauthorized origin:", event.origin);
|
|
11177
|
+
this.rejectEmbedded(new Error(`Unauthorized origin: ${event.origin}`));
|
|
11178
|
+
return;
|
|
11179
|
+
}
|
|
11180
|
+
} else if (this.options.allowedOrigins && this.options.allowedOrigins.length > 0) {
|
|
11147
11181
|
if (!event.origin || !this.options.allowedOrigins.includes(event.origin)) {
|
|
11148
11182
|
console.warn("[Ad4m Connect] Rejected AD4M_CONFIG from unauthorized origin:", event.origin);
|
|
11149
11183
|
this.rejectEmbedded(new Error(`Unauthorized origin: ${event.origin}`));
|
|
@@ -11161,12 +11195,17 @@ var Ad4mConnect = class extends EventTarget {
|
|
|
11161
11195
|
} else {
|
|
11162
11196
|
removeLocal("ad4m-token");
|
|
11163
11197
|
}
|
|
11198
|
+
if (!event.origin || event.origin === "null") {
|
|
11199
|
+
throw new Error("AD4M proxy mode requires a non-opaque parent origin. Ensure the host iframe is not sandboxed without allow-same-origin.");
|
|
11200
|
+
}
|
|
11201
|
+
const parentOrigin2 = event.origin;
|
|
11202
|
+
const wsImpl = (url) => new PostMessageWebSocket(url, parentOrigin2);
|
|
11164
11203
|
this.notifyConnectionChange("connecting");
|
|
11165
11204
|
this.ad4mClient = new Ad4mClient(
|
|
11166
11205
|
"http://proxy",
|
|
11167
11206
|
normalizedToken,
|
|
11168
11207
|
false,
|
|
11169
|
-
{ webSocketImpl:
|
|
11208
|
+
{ webSocketImpl: wsImpl }
|
|
11170
11209
|
);
|
|
11171
11210
|
this.notifyConnectionChange("connected");
|
|
11172
11211
|
yield this.checkAuth();
|