iframe-pubsub 1.0.22 → 1.0.24

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/index.d.mts CHANGED
@@ -30,10 +30,29 @@ declare class Client {
30
30
  private isIframe;
31
31
  private isMainApp;
32
32
  private boundHandleMessage;
33
+ /**
34
+ * Create a new client instance.
35
+ *
36
+ * @param pageId The ID of the page or component to register to.
37
+ */
33
38
  constructor(pageId: string, targetAIChatClientId?: string);
39
+ /**
40
+ * Unregister the client from the pubsub.
41
+ */
34
42
  unregister(): void;
35
43
  cleanAIChat(): boolean;
44
+ /**
45
+ * Listen for messages from the parent page with a callback.
46
+ *
47
+ * @param callback The callback function to register.
48
+ */
36
49
  onMessage(callback: MessageCallback): void;
50
+ /**
51
+ * Send a message to the another page or component.
52
+ *
53
+ * @param to The ID of the page or component to send the message to.
54
+ * @param payload The payload of the message.
55
+ */
37
56
  sendMessage(to: string, payload: any): void;
38
57
  /**
39
58
  * Check if a client with the given ID exists in the PubSub system.
package/dist/index.d.ts CHANGED
@@ -30,10 +30,29 @@ declare class Client {
30
30
  private isIframe;
31
31
  private isMainApp;
32
32
  private boundHandleMessage;
33
+ /**
34
+ * Create a new client instance.
35
+ *
36
+ * @param pageId The ID of the page or component to register to.
37
+ */
33
38
  constructor(pageId: string, targetAIChatClientId?: string);
39
+ /**
40
+ * Unregister the client from the pubsub.
41
+ */
34
42
  unregister(): void;
35
43
  cleanAIChat(): boolean;
44
+ /**
45
+ * Listen for messages from the parent page with a callback.
46
+ *
47
+ * @param callback The callback function to register.
48
+ */
36
49
  onMessage(callback: MessageCallback): void;
50
+ /**
51
+ * Send a message to the another page or component.
52
+ *
53
+ * @param to The ID of the page or component to send the message to.
54
+ * @param payload The payload of the message.
55
+ */
37
56
  sendMessage(to: string, payload: any): void;
38
57
  /**
39
58
  * Check if a client with the given ID exists in the PubSub system.
package/dist/index.js CHANGED
@@ -29,6 +29,11 @@ __export(index_exports, {
29
29
  });
30
30
  module.exports = __toCommonJS(index_exports);
31
31
 
32
+ // src/cors.ts
33
+ function isAllowedOrigin(origin) {
34
+ return origin === "null" || origin.startsWith("http://localhost") || origin.endsWith(".bkpi.co") || origin.endsWith(".bookipi.com");
35
+ }
36
+
32
37
  // src/PubSub.ts
33
38
  var _PubSub = class _PubSub {
34
39
  constructor() {
@@ -83,7 +88,7 @@ var _PubSub = class _PubSub {
83
88
  const subscriber = this.subscribers.get(message.to);
84
89
  if (subscriber) {
85
90
  if (subscriber.source) {
86
- subscriber.source.postMessage(message, "*");
91
+ subscriber.source.postMessage(message, subscriber.origin || "*");
87
92
  } else {
88
93
  subscriber.callback(message);
89
94
  }
@@ -100,12 +105,15 @@ var _PubSub = class _PubSub {
100
105
  handleMessage(event) {
101
106
  const data = event.data;
102
107
  const source = event.source;
108
+ const origin = event.origin;
109
+ if (!isAllowedOrigin(origin)) return;
103
110
  if (data?.type === "REGISTER") {
104
111
  const registration = data;
105
112
  this.subscribers.set(registration.pageId, {
106
113
  source,
114
+ origin: registration.origin,
107
115
  callback: (message2) => {
108
- source.postMessage(message2, "*");
116
+ source.postMessage(message2, registration.origin || "*");
109
117
  }
110
118
  });
111
119
  return;
@@ -120,7 +128,7 @@ var _PubSub = class _PubSub {
120
128
  type: "CLIENT_EXISTS_RESPONSE",
121
129
  requestId: data.requestId,
122
130
  exists: this.subscribers.has(data.clientId)
123
- }, "*");
131
+ }, data.origin || "*");
124
132
  return;
125
133
  }
126
134
  if (!data || !data.from || !data.to) return;
@@ -130,8 +138,8 @@ var _PubSub = class _PubSub {
130
138
  }
131
139
  const subscriber = this.subscribers.get(message.to);
132
140
  if (!subscriber) return;
133
- if (subscriber.source) {
134
- subscriber.source.postMessage(message, "*");
141
+ if (subscriber?.source) {
142
+ subscriber.source.postMessage(message, subscriber.origin || "*");
135
143
  } else {
136
144
  subscriber.callback(message);
137
145
  }
@@ -151,6 +159,11 @@ var PubSub = _PubSub;
151
159
 
152
160
  // src/Client.ts
153
161
  var Client = class {
162
+ /**
163
+ * Create a new client instance.
164
+ *
165
+ * @param pageId The ID of the page or component to register to.
166
+ */
154
167
  constructor(pageId, targetAIChatClientId = "aichat" /* AI_CHAT_CLIENT_ID */) {
155
168
  __publicField(this, "targetAIChatClientId");
156
169
  __publicField(this, "pageId");
@@ -175,13 +188,17 @@ var Client = class {
175
188
  window.top.postMessage(
176
189
  {
177
190
  type: "REGISTER",
178
- pageId: this.pageId
191
+ pageId: this.pageId,
192
+ origin: window.location.origin
179
193
  },
180
194
  "*"
181
195
  );
182
196
  window.addEventListener("message", this.boundHandleMessage);
183
197
  }
184
198
  }
199
+ /**
200
+ * Unregister the client from the pubsub.
201
+ */
185
202
  unregister() {
186
203
  if (this.isMainApp && this.pubsub) {
187
204
  this.pubsub.unregister(this.pageId);
@@ -189,7 +206,8 @@ var Client = class {
189
206
  window.top.postMessage(
190
207
  {
191
208
  type: "UNREGISTER",
192
- pageId: this.pageId
209
+ pageId: this.pageId,
210
+ origin: window.location.origin
193
211
  },
194
212
  "*"
195
213
  );
@@ -202,9 +220,20 @@ var Client = class {
202
220
  console.warn(`\u203C\uFE0F Unsupported operation cleanAIChat has been called from ${this.pageId}.`);
203
221
  return false;
204
222
  }
223
+ /**
224
+ * Listen for messages from the parent page with a callback.
225
+ *
226
+ * @param callback The callback function to register.
227
+ */
205
228
  onMessage(callback) {
206
229
  this.callback = callback;
207
230
  }
231
+ /**
232
+ * Send a message to the another page or component.
233
+ *
234
+ * @param to The ID of the page or component to send the message to.
235
+ * @param payload The payload of the message.
236
+ */
208
237
  sendMessage(to, payload) {
209
238
  const message = {
210
239
  from: this.pageId,
@@ -262,6 +291,7 @@ var Client = class {
262
291
  } else {
263
292
  const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
264
293
  const messageHandler = (event) => {
294
+ if (!isAllowedOrigin(event.origin)) return;
265
295
  const data = event.data;
266
296
  if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
267
297
  window.removeEventListener("message", messageHandler);
package/dist/index.mjs CHANGED
@@ -2,6 +2,11 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
 
5
+ // src/cors.ts
6
+ function isAllowedOrigin(origin) {
7
+ return origin === "null" || origin.startsWith("http://localhost") || origin.endsWith(".bkpi.co") || origin.endsWith(".bookipi.com");
8
+ }
9
+
5
10
  // src/PubSub.ts
6
11
  var _PubSub = class _PubSub {
7
12
  constructor() {
@@ -56,7 +61,7 @@ var _PubSub = class _PubSub {
56
61
  const subscriber = this.subscribers.get(message.to);
57
62
  if (subscriber) {
58
63
  if (subscriber.source) {
59
- subscriber.source.postMessage(message, "*");
64
+ subscriber.source.postMessage(message, subscriber.origin || "*");
60
65
  } else {
61
66
  subscriber.callback(message);
62
67
  }
@@ -73,12 +78,15 @@ var _PubSub = class _PubSub {
73
78
  handleMessage(event) {
74
79
  const data = event.data;
75
80
  const source = event.source;
81
+ const origin = event.origin;
82
+ if (!isAllowedOrigin(origin)) return;
76
83
  if (data?.type === "REGISTER") {
77
84
  const registration = data;
78
85
  this.subscribers.set(registration.pageId, {
79
86
  source,
87
+ origin: registration.origin,
80
88
  callback: (message2) => {
81
- source.postMessage(message2, "*");
89
+ source.postMessage(message2, registration.origin || "*");
82
90
  }
83
91
  });
84
92
  return;
@@ -93,7 +101,7 @@ var _PubSub = class _PubSub {
93
101
  type: "CLIENT_EXISTS_RESPONSE",
94
102
  requestId: data.requestId,
95
103
  exists: this.subscribers.has(data.clientId)
96
- }, "*");
104
+ }, data.origin || "*");
97
105
  return;
98
106
  }
99
107
  if (!data || !data.from || !data.to) return;
@@ -103,8 +111,8 @@ var _PubSub = class _PubSub {
103
111
  }
104
112
  const subscriber = this.subscribers.get(message.to);
105
113
  if (!subscriber) return;
106
- if (subscriber.source) {
107
- subscriber.source.postMessage(message, "*");
114
+ if (subscriber?.source) {
115
+ subscriber.source.postMessage(message, subscriber.origin || "*");
108
116
  } else {
109
117
  subscriber.callback(message);
110
118
  }
@@ -124,6 +132,11 @@ var PubSub = _PubSub;
124
132
 
125
133
  // src/Client.ts
126
134
  var Client = class {
135
+ /**
136
+ * Create a new client instance.
137
+ *
138
+ * @param pageId The ID of the page or component to register to.
139
+ */
127
140
  constructor(pageId, targetAIChatClientId = "aichat" /* AI_CHAT_CLIENT_ID */) {
128
141
  __publicField(this, "targetAIChatClientId");
129
142
  __publicField(this, "pageId");
@@ -148,13 +161,17 @@ var Client = class {
148
161
  window.top.postMessage(
149
162
  {
150
163
  type: "REGISTER",
151
- pageId: this.pageId
164
+ pageId: this.pageId,
165
+ origin: window.location.origin
152
166
  },
153
167
  "*"
154
168
  );
155
169
  window.addEventListener("message", this.boundHandleMessage);
156
170
  }
157
171
  }
172
+ /**
173
+ * Unregister the client from the pubsub.
174
+ */
158
175
  unregister() {
159
176
  if (this.isMainApp && this.pubsub) {
160
177
  this.pubsub.unregister(this.pageId);
@@ -162,7 +179,8 @@ var Client = class {
162
179
  window.top.postMessage(
163
180
  {
164
181
  type: "UNREGISTER",
165
- pageId: this.pageId
182
+ pageId: this.pageId,
183
+ origin: window.location.origin
166
184
  },
167
185
  "*"
168
186
  );
@@ -175,9 +193,20 @@ var Client = class {
175
193
  console.warn(`\u203C\uFE0F Unsupported operation cleanAIChat has been called from ${this.pageId}.`);
176
194
  return false;
177
195
  }
196
+ /**
197
+ * Listen for messages from the parent page with a callback.
198
+ *
199
+ * @param callback The callback function to register.
200
+ */
178
201
  onMessage(callback) {
179
202
  this.callback = callback;
180
203
  }
204
+ /**
205
+ * Send a message to the another page or component.
206
+ *
207
+ * @param to The ID of the page or component to send the message to.
208
+ * @param payload The payload of the message.
209
+ */
181
210
  sendMessage(to, payload) {
182
211
  const message = {
183
212
  from: this.pageId,
@@ -235,6 +264,7 @@ var Client = class {
235
264
  } else {
236
265
  const requestId = `check-client-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
237
266
  const messageHandler = (event) => {
267
+ if (!isAllowedOrigin(event.origin)) return;
238
268
  const data = event.data;
239
269
  if (data && data.type === "CLIENT_EXISTS_RESPONSE" && data.requestId === requestId) {
240
270
  window.removeEventListener("message", messageHandler);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "iframe-pubsub",
3
- "version": "1.0.22",
3
+ "version": "1.0.24",
4
4
  "description": "A PubSub library for iframe communication.",
5
5
  "author": "Lap Tran",
6
6
  "license": "ISC",