@ksaa-nlp/gamehub-sdk 3.7.0 → 3.11.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.
Files changed (40) hide show
  1. package/README.md +105 -11
  2. package/SPEC.md +201 -84
  3. package/dist/gamehub-sdk.js +174 -83
  4. package/dist/gamehub-sdk.js.map +3 -3
  5. package/dist/src/api-client.d.ts +6 -5
  6. package/dist/src/api-client.d.ts.map +1 -1
  7. package/dist/src/api-client.js +9 -8
  8. package/dist/src/api-client.js.map +1 -1
  9. package/dist/src/bridge.d.ts +10 -6
  10. package/dist/src/bridge.d.ts.map +1 -1
  11. package/dist/src/bridge.js +90 -59
  12. package/dist/src/bridge.js.map +1 -1
  13. package/dist/src/browser-entry.d.ts.map +1 -1
  14. package/dist/src/browser-entry.js +6 -5
  15. package/dist/src/browser-entry.js.map +1 -1
  16. package/dist/src/constants.d.ts +1 -1
  17. package/dist/src/constants.d.ts.map +1 -1
  18. package/dist/src/constants.js +1 -1
  19. package/dist/src/constants.js.map +1 -1
  20. package/dist/src/init.d.ts.map +1 -1
  21. package/dist/src/init.js +21 -9
  22. package/dist/src/init.js.map +1 -1
  23. package/dist/src/main.d.ts +1 -1
  24. package/dist/src/main.d.ts.map +1 -1
  25. package/dist/src/main.js +1 -0
  26. package/dist/src/main.js.map +1 -1
  27. package/dist/src/session.d.ts.map +1 -1
  28. package/dist/src/session.js +8 -3
  29. package/dist/src/session.js.map +1 -1
  30. package/dist/src/state.d.ts +2 -1
  31. package/dist/src/state.d.ts.map +1 -1
  32. package/dist/src/state.js +1 -0
  33. package/dist/src/state.js.map +1 -1
  34. package/dist/src/transport.d.ts +52 -0
  35. package/dist/src/transport.d.ts.map +1 -0
  36. package/dist/src/transport.js +130 -0
  37. package/dist/src/transport.js.map +1 -0
  38. package/dist/types/bootstrap.d.ts +9 -0
  39. package/dist/types/bootstrap.d.ts.map +1 -1
  40. package/package.json +2 -1
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  (() => {
3
3
  // src/constants.ts
4
- var SDK_VERSION = "3.7.0";
4
+ var SDK_VERSION = "3.11.0";
5
5
  var MESSAGE_PREFIX = "GAMEHUB_SDK";
6
6
  var PARENT_REQUEST_TIMEOUT_MS = 2e3;
7
7
  var HOST_API_TIMEOUT_MS = 15e3;
@@ -20,6 +20,7 @@
20
20
  leagueId: null,
21
21
  gameId: null,
22
22
  matchId: null,
23
+ bootstrapSessionMode: null,
23
24
  bootstrapMode: null,
24
25
  bootstrapPlayerName: null,
25
26
  bootstrapPlayerCount: null,
@@ -65,6 +66,82 @@
65
66
  }
66
67
  }
67
68
 
69
+ // src/transport.ts
70
+ var PostMessageTransport = class {
71
+ constructor() {
72
+ this.kind = "postmessage";
73
+ }
74
+ send(envelope) {
75
+ window.parent.postMessage(envelope, state.parentOrigin || "*");
76
+ }
77
+ subscribe(handler) {
78
+ const listener = (event) => {
79
+ if (event.source !== window.parent) return;
80
+ if (state.parentOrigin && event.origin !== state.parentOrigin) return;
81
+ handler({ data: event.data, origin: event.origin });
82
+ };
83
+ window.addEventListener("message", listener);
84
+ return () => window.removeEventListener("message", listener);
85
+ }
86
+ };
87
+ function acceptParentOrigin(origin) {
88
+ if (origin === null) return true;
89
+ if (!origin || origin === "null") return false;
90
+ if (state.parentOrigin === null) {
91
+ state.parentOrigin = origin;
92
+ return true;
93
+ }
94
+ return state.parentOrigin === origin;
95
+ }
96
+ var NativeBridgeTransport = class {
97
+ constructor(host) {
98
+ this.host = host;
99
+ this.kind = "native";
100
+ this.handlers = /* @__PURE__ */ new Set();
101
+ host.receive = (envelope) => {
102
+ let data = envelope;
103
+ if (typeof envelope === "string") {
104
+ try {
105
+ data = JSON.parse(envelope);
106
+ } catch {
107
+ return;
108
+ }
109
+ }
110
+ if (!data || typeof data !== "object") return;
111
+ this.handlers.forEach((h) => h({ data, origin: null }));
112
+ };
113
+ }
114
+ send(envelope) {
115
+ this.host.postMessage(JSON.stringify(envelope));
116
+ }
117
+ subscribe(handler) {
118
+ this.handlers.add(handler);
119
+ return () => {
120
+ this.handlers.delete(handler);
121
+ };
122
+ }
123
+ };
124
+ var active;
125
+ function getTransport() {
126
+ if (active !== void 0) return active;
127
+ if (!isBrowser()) {
128
+ active = null;
129
+ return active;
130
+ }
131
+ const nativeHost = window.GameHubNativeHost;
132
+ if (nativeHost && typeof nativeHost.postMessage === "function") {
133
+ active = new NativeBridgeTransport(nativeHost);
134
+ } else if (window.parent !== window) {
135
+ active = new PostMessageTransport();
136
+ } else {
137
+ active = null;
138
+ }
139
+ return active;
140
+ }
141
+ function hasHost() {
142
+ return getTransport() !== null;
143
+ }
144
+
68
145
  // src/bridge.ts
69
146
  function trimToNullable(value) {
70
147
  if (value === null) return null;
@@ -75,13 +152,16 @@
75
152
  function parseMultiplayer(value) {
76
153
  if (value === null) return null;
77
154
  if (!value || typeof value !== "object") return void 0;
78
- const { reservation, wsEndpoint } = value;
155
+ const { reservation, wsEndpoint, roomCode, role, connectionScope } = value;
79
156
  if (!reservation || typeof reservation !== "object" || typeof reservation.name !== "string" || typeof reservation.sessionId !== "string" || typeof reservation.roomId !== "string" || typeof wsEndpoint !== "string") {
80
157
  return void 0;
81
158
  }
82
159
  return {
83
160
  reservation,
84
- wsEndpoint: wsEndpoint.trim()
161
+ wsEndpoint: wsEndpoint.trim(),
162
+ ...typeof roomCode === "string" && /^[A-Za-z0-9]{4,12}$/.test(roomCode) ? { roomCode: roomCode.toUpperCase() } : {},
163
+ ...role === "host" || role === "player" || role === "spectator" ? { role } : {},
164
+ ...typeof connectionScope === "string" && connectionScope.length > 0 && connectionScope.length <= 160 ? { connectionScope } : {}
85
165
  };
86
166
  }
87
167
  function mergeBootstrapConfig(config = {}) {
@@ -96,6 +176,11 @@
96
176
  apply("leagueId", config.leagueId);
97
177
  apply("gameId", config.gameId);
98
178
  apply("matchId", config.matchId);
179
+ if (config.sessionMode === null || ["authenticated", "embed-guest", "league-guest"].includes(
180
+ config.sessionMode
181
+ )) {
182
+ state.bootstrapSessionMode = config.sessionMode ?? null;
183
+ }
99
184
  const applyBootstrap = (key, raw) => {
100
185
  const next = trimToNullable(raw);
101
186
  if (next === void 0) return;
@@ -119,6 +204,7 @@
119
204
  leagueId: state.leagueId,
120
205
  gameId: state.gameId,
121
206
  matchId: state.matchId,
207
+ sessionMode: state.bootstrapSessionMode,
122
208
  mode: state.bootstrapMode,
123
209
  playerName: state.bootstrapPlayerName,
124
210
  playerCount: state.bootstrapPlayerCount,
@@ -129,7 +215,8 @@
129
215
  };
130
216
  }
131
217
  function requestBootstrapFromParent() {
132
- if (!isBrowser() || window.parent === window) {
218
+ const transport = getTransport();
219
+ if (!transport) {
133
220
  return Promise.resolve(null);
134
221
  }
135
222
  if (state.bootstrapReceived) {
@@ -143,33 +230,30 @@
143
230
  const messageId = Date.now().toString();
144
231
  let settled = false;
145
232
  let timeoutId = null;
146
- const handler = (event) => {
147
- if (event.data && event.data.type === MESSAGE_PREFIX + "_BOOTSTRAP" && event.data.payload) {
148
- if (event.data.messageId && event.data.messageId !== messageId) {
149
- return;
150
- }
233
+ let unsubscribe = () => {
234
+ };
235
+ const handler = (msg) => {
236
+ const data = msg.data;
237
+ if (data && data.type === MESSAGE_PREFIX + "_BOOTSTRAP" && data.payload && data.messageId === messageId) {
151
238
  if (settled) return;
239
+ if (!acceptParentOrigin(msg.origin)) return;
152
240
  settled = true;
153
- window.removeEventListener("message", handler);
241
+ unsubscribe();
154
242
  if (timeoutId) clearTimeout(timeoutId);
155
- state.parentOrigin = event.origin;
156
243
  state.bootstrapReceived = true;
157
- mergeBootstrapConfig(event.data.payload);
158
- resolve(event.data.payload);
244
+ mergeBootstrapConfig(data.payload);
245
+ resolve(data.payload);
159
246
  }
160
247
  };
161
- window.addEventListener("message", handler);
162
- window.parent.postMessage(
163
- {
164
- type: MESSAGE_PREFIX + "_BOOTSTRAP_REQUEST",
165
- messageId
166
- },
167
- "*"
168
- );
248
+ unsubscribe = transport.subscribe(handler);
249
+ transport.send({
250
+ type: MESSAGE_PREFIX + "_BOOTSTRAP_REQUEST",
251
+ messageId
252
+ });
169
253
  timeoutId = setTimeout(() => {
170
254
  if (settled) return;
171
255
  settled = true;
172
- window.removeEventListener("message", handler);
256
+ unsubscribe();
173
257
  resolve(null);
174
258
  }, PARENT_REQUEST_TIMEOUT_MS);
175
259
  }
@@ -180,35 +264,36 @@
180
264
  }
181
265
  function getAuthTokenFromParent() {
182
266
  return new Promise((resolve) => {
183
- if (!isBrowser() || window.parent === window) {
267
+ const transport = getTransport();
268
+ if (!transport) {
184
269
  resolve(null);
185
270
  return;
186
271
  }
187
272
  const messageId = Date.now().toString();
188
273
  let settled = false;
189
274
  let timeoutId = null;
190
- const handler = (event) => {
191
- if (event.data && event.data.type === MESSAGE_PREFIX + "_AUTH_RESPONSE" && event.data.messageId === messageId) {
275
+ let unsubscribe = () => {
276
+ };
277
+ const handler = (msg) => {
278
+ const data = msg.data;
279
+ if (data && data.type === MESSAGE_PREFIX + "_AUTH_RESPONSE" && data.messageId === messageId) {
192
280
  if (settled) return;
281
+ if (!acceptParentOrigin(msg.origin)) return;
193
282
  settled = true;
194
- window.removeEventListener("message", handler);
283
+ unsubscribe();
195
284
  if (timeoutId) clearTimeout(timeoutId);
196
- state.parentOrigin = event.origin;
197
- if (event.data.leagueId) state.leagueId = event.data.leagueId;
198
- if (event.data.gameId) state.gameId = event.data.gameId;
199
- if (event.data.matchId) state.matchId = event.data.matchId;
200
- resolve(event.data.token || null);
285
+ if (data.leagueId) state.leagueId = data.leagueId;
286
+ if (data.gameId) state.gameId = data.gameId;
287
+ if (data.matchId) state.matchId = data.matchId;
288
+ resolve(data.token || null);
201
289
  }
202
290
  };
203
- window.addEventListener("message", handler);
204
- window.parent.postMessage(
205
- { type: MESSAGE_PREFIX + "_AUTH_REQUEST", messageId },
206
- "*"
207
- );
291
+ unsubscribe = transport.subscribe(handler);
292
+ transport.send({ type: MESSAGE_PREFIX + "_AUTH_REQUEST", messageId });
208
293
  timeoutId = setTimeout(() => {
209
294
  if (settled) return;
210
295
  settled = true;
211
- window.removeEventListener("message", handler);
296
+ unsubscribe();
212
297
  resolve(null);
213
298
  }, PARENT_REQUEST_TIMEOUT_MS);
214
299
  });
@@ -216,62 +301,61 @@
216
301
  var apiRequestSeq = 0;
217
302
  function requestApiFromParent(endpoint, options = {}) {
218
303
  return new Promise((resolve, reject) => {
219
- if (!isBrowser() || window.parent === window) {
220
- reject(new Error("Host bridge unavailable: not running in an iframe"));
304
+ const transport = getTransport();
305
+ if (!transport) {
306
+ reject(new Error("Host bridge unavailable: no host transport"));
221
307
  return;
222
308
  }
223
309
  const messageId = "api_" + Date.now().toString() + "_" + ++apiRequestSeq;
224
310
  let settled = false;
225
311
  let timeoutId = null;
226
- const handler = (event) => {
227
- if (!event.data || event.data.type !== MESSAGE_PREFIX + "_API_RESPONSE" || event.data.messageId !== messageId) {
312
+ let unsubscribe = () => {
313
+ };
314
+ const handler = (msg) => {
315
+ const data = msg.data;
316
+ if (!data || data.type !== MESSAGE_PREFIX + "_API_RESPONSE" || data.messageId !== messageId) {
228
317
  return;
229
318
  }
230
319
  if (settled) return;
320
+ if (!acceptParentOrigin(msg.origin)) return;
231
321
  settled = true;
232
- window.removeEventListener("message", handler);
322
+ unsubscribe();
233
323
  if (timeoutId) clearTimeout(timeoutId);
234
- state.parentOrigin = event.origin;
235
- if (event.data.ok) {
236
- resolve(event.data.result ?? {});
324
+ if (data.ok) {
325
+ resolve(data.result ?? {});
237
326
  } else {
238
- reject(new Error(event.data.error || "Host API request failed"));
327
+ reject(new Error(data.error || "Host API request failed"));
239
328
  }
240
329
  };
241
- window.addEventListener("message", handler);
242
- window.parent.postMessage(
243
- {
244
- type: MESSAGE_PREFIX + "_API_REQUEST",
245
- messageId,
246
- endpoint,
247
- method: options.method || "GET",
248
- body: options.body ?? null
249
- },
250
- state.parentOrigin || "*"
251
- );
330
+ unsubscribe = transport.subscribe(handler);
331
+ transport.send({
332
+ type: MESSAGE_PREFIX + "_API_REQUEST",
333
+ messageId,
334
+ endpoint,
335
+ method: options.method || "GET",
336
+ body: options.body ?? null
337
+ });
252
338
  timeoutId = setTimeout(() => {
253
339
  if (settled) return;
254
340
  settled = true;
255
- window.removeEventListener("message", handler);
341
+ unsubscribe();
256
342
  reject(new Error("Host API request timed out"));
257
343
  }, HOST_API_TIMEOUT_MS);
258
344
  });
259
345
  }
260
346
  function sendToParent(type, data) {
261
- if (!isBrowser() || window.parent === window) return;
262
- window.parent.postMessage(
263
- {
264
- type: MESSAGE_PREFIX + "_" + type,
265
- gameSlug: state.gameSlug,
266
- ...data
267
- },
268
- state.parentOrigin || "*"
269
- );
347
+ const transport = getTransport();
348
+ if (!transport) return;
349
+ transport.send({
350
+ type: MESSAGE_PREFIX + "_" + type,
351
+ gameSlug: state.gameSlug,
352
+ ...data
353
+ });
270
354
  }
271
355
 
272
356
  // src/api-client.ts
273
357
  async function apiRequest(endpoint, options = {}) {
274
- if (isBrowser() && window.parent !== window) {
358
+ if (hasHost()) {
275
359
  return requestApiFromParent(endpoint, options);
276
360
  }
277
361
  const url = state.apiBase + endpoint;
@@ -297,7 +381,9 @@
297
381
  const isLeagueMatchSession = Boolean(
298
382
  state.leagueId && state.gameId && state.matchId
299
383
  );
300
- if (!state.authToken && !isLeagueMatchSession) {
384
+ const isEmbedGuestSession = state.bootstrapSessionMode === "embed-guest";
385
+ const isHostAuthenticatedSession = state.bootstrapSessionMode === "authenticated";
386
+ if (!state.authToken && !isLeagueMatchSession && !isEmbedGuestSession && !isHostAuthenticatedSession) {
301
387
  return { success: false, error: "User not authenticated" };
302
388
  }
303
389
  if (state.sessionToken) return { success: true, token: state.sessionToken };
@@ -411,7 +497,7 @@
411
497
  }
412
498
  async function getQuestions(options = {}) {
413
499
  await ensureInitialized();
414
- const gameIdentifier = state.gameId || state.gameSlug;
500
+ const gameIdentifier = state.gameSlug || state.gameId;
415
501
  if (!gameIdentifier) return [];
416
502
  const { difficulty, category, limit } = options;
417
503
  if (difficulty && !["easy", "medium", "hard"].includes(difficulty)) {
@@ -450,7 +536,7 @@
450
536
  }
451
537
  async function getWords(options = {}) {
452
538
  await ensureInitialized();
453
- const gameIdentifier = state.gameId || state.gameSlug;
539
+ const gameIdentifier = state.gameSlug || state.gameId;
454
540
  if (!gameIdentifier) return [];
455
541
  const { difficulty, category, tags, limit } = options;
456
542
  if (difficulty && !["easy", "medium", "hard"].includes(difficulty)) {
@@ -560,7 +646,7 @@
560
646
  slug: state.gameSlug,
561
647
  auto: true
562
648
  });
563
- if (state.authToken || state.leagueId && state.gameId && state.matchId) {
649
+ if (state.authToken || state.bootstrapSessionMode === "authenticated" || state.bootstrapSessionMode === "embed-guest" || state.leagueId && state.gameId && state.matchId) {
564
650
  state.sessionStartPromise = startSession();
565
651
  }
566
652
  return true;
@@ -580,24 +666,28 @@
580
666
  }
581
667
  function installHostMessageListener() {
582
668
  if (!isBrowser() || hostListenerInstalled) return;
669
+ const transport = getTransport();
670
+ if (!transport) return;
583
671
  hostListenerInstalled = true;
584
- window.addEventListener("message", (event) => {
585
- if (!event.data || !event.data.type) return;
586
- const type = event.data.type;
587
- if (type === MESSAGE_PREFIX + "_BOOTSTRAP" && event.data.payload) {
588
- state.parentOrigin = event.origin;
672
+ transport.subscribe((msg) => {
673
+ const data = msg.data;
674
+ if (!data || !data.type) return;
675
+ if (transport.kind !== "native" && (!state.parentOrigin || !acceptParentOrigin(msg.origin))) {
676
+ return;
677
+ }
678
+ const type = data.type;
679
+ if (type === MESSAGE_PREFIX + "_BOOTSTRAP" && data.payload) {
589
680
  state.bootstrapReceived = true;
590
- mergeBootstrapConfig(event.data.payload);
681
+ mergeBootstrapConfig(data.payload);
591
682
  }
592
683
  if (type === MESSAGE_PREFIX + "_SET_AUTH") {
593
- state.authToken = event.data.token;
594
- state.parentOrigin = event.origin;
684
+ state.authToken = data.token;
595
685
  if (state.gameSlug && !state.sessionToken && !state.sessionStartPromise && (state.authToken || state.leagueId && state.gameId && state.matchId)) {
596
686
  state.sessionStartPromise = startSession();
597
687
  }
598
688
  }
599
689
  if (type === MESSAGE_PREFIX + "_COMMAND") {
600
- const command = event.data.command;
690
+ const command = data.command;
601
691
  const w = window;
602
692
  if (command === "pause" && typeof w.onGameHubPause === "function") {
603
693
  w.onGameHubPause();
@@ -710,6 +800,7 @@
710
800
  leagueId: state.leagueId,
711
801
  gameId: state.gameId,
712
802
  matchId: state.matchId,
803
+ sessionMode: state.bootstrapSessionMode,
713
804
  mode: state.bootstrapMode,
714
805
  playerName: state.bootstrapPlayerName,
715
806
  playerCount: state.bootstrapPlayerCount,
@@ -737,10 +828,10 @@
737
828
  installHostMessageListener();
738
829
  const sdk = createSdk();
739
830
  window.GameHubSDK = sdk;
740
- if (window.parent !== window) {
831
+ if (hasHost()) {
741
832
  sendToParent("READY", { version: SDK_VERSION });
742
833
  }
743
- const hasBootSignal = !!state.gameSlug || window.parent !== window;
834
+ const hasBootSignal = !!state.gameSlug || hasHost();
744
835
  if (!hasBootSignal) return;
745
836
  if (document.readyState === "loading") {
746
837
  document.addEventListener("DOMContentLoaded", () => {