glitch-javascript-sdk 3.15.0 → 4.0.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.
@@ -68,6 +68,8 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
68
68
  if (!win || !doc.body) throw new Error('A mounted game document is required.');
69
69
  if (activeOverlays.has(doc)) throw new Error('A checkout is already open. Resume or close that same purchase first.');
70
70
  const session = options.session;
71
+ // Older purchase-session creation responses need not include an intent.
72
+ const restoring = session.intent === 'restore';
71
73
  const parsed = new URL(session.hosted_url);
72
74
  const path = `/games/${encodeURIComponent(options.titleId)}/checkout/${encodeURIComponent(session.id)}`;
73
75
  if (parsed.origin !== options.checkoutOrigin || parsed.pathname !== path || parsed.search || parsed.username || parsed.password ||
@@ -86,41 +88,55 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
86
88
  dialog.setAttribute('aria-modal', 'true');
87
89
  dialog.setAttribute('aria-labelledby', `${id}-title`);
88
90
  dialog.setAttribute('role', 'dialog');
89
- dialog.style.cssText = 'position:fixed;inset:16px;margin:auto;padding:0;border:1px solid #cbd5e1;border-radius:16px;width:min(1100px,calc(100vw - 32px));height:min(850px,calc(100dvh - 32px));max-width:none;max-height:none;background:#fff;color:#172033;box-shadow:0 24px 90px #0008;z-index:2147483647;overflow:hidden;';
91
+ dialog.style.cssText = 'position:fixed;inset:0;margin:auto;padding:0;border:1px solid #cbd5e1;border-radius:16px;box-sizing:border-box;display:flex;flex-direction:column;width:min(1100px,calc(100vw - 32px));height:min(850px,calc(100vh - 32px));min-width:0;min-height:0;max-width:none;max-height:none;background:#fff;color:#172033;box-shadow:0 24px 90px #0008;z-index:2147483647;overflow:hidden;';
90
92
  const style = doc.createElement('style');
91
- style.textContent = `#${id}::backdrop{background:rgba(8,16,32,.72)}#${id} button:focus-visible{outline:3px solid #4263eb;outline-offset:3px}@media(max-width:600px){#${id}{inset:0!important;width:100vw!important;height:100dvh!important;border-radius:0!important}}`;
93
+ style.textContent = `
94
+ #${id}::backdrop{background:rgba(8,16,32,.72)}
95
+ #${id} button:focus-visible{outline:3px solid #4263eb;outline-offset:3px}
96
+ @supports(height:100dvh){#${id}{height:min(850px,calc(100dvh - 32px))!important}}
97
+ @media(max-width:600px),(max-height:500px){
98
+ #${id}{width:100%!important;height:100vh!important;border:0!important;border-radius:0!important;padding:env(safe-area-inset-top,0px) env(safe-area-inset-right,0px) env(safe-area-inset-bottom,0px) env(safe-area-inset-left,0px)!important}
99
+ @supports(height:100dvh){#${id}{height:100dvh!important}}
100
+ }`;
101
+ // Natural-height chrome can wrap/scroll without consuming the entire frame.
102
+ // No fixed header/status subtraction; the iframe owns all remaining space.
103
+ const chrome = doc.createElement('div');
104
+ chrome.style.cssText = 'flex:0 1 auto;min-width:0;min-height:0;max-height:50%;overflow:auto;overscroll-behavior:contain;';
92
105
  const header = doc.createElement('div');
93
- header.style.cssText = 'display:flex;align-items:center;justify-content:space-between;gap:16px;padding:12px 16px;border-bottom:1px solid #e2e8f0;height:56px;box-sizing:border-box;';
106
+ header.style.cssText = 'display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:8px 16px;padding:12px 16px;border-bottom:1px solid #e2e8f0;box-sizing:border-box;min-width:0;';
94
107
  const heading = doc.createElement('h2');
95
108
  heading.id = `${id}-title`;
96
- heading.textContent = options.label || (session.intent === 'restore' ? 'Restore game purchases' : 'Secure game checkout');
97
- heading.style.cssText = 'margin:0;font:600 17px/1.3 system-ui,sans-serif;color:#172033;';
109
+ heading.textContent = options.label || (restoring ? 'Restore game purchases' : 'Secure game checkout');
110
+ heading.style.cssText = 'flex:1 1 10rem;min-width:0;margin:0;font:600 17px/1.3 system-ui,sans-serif;color:#172033;white-space:normal;overflow-wrap:anywhere;';
98
111
  const closeButton = doc.createElement('button');
99
112
  closeButton.type = 'button';
100
113
  closeButton.textContent = 'Close';
101
- closeButton.setAttribute('aria-label', 'Close checkout and return to game');
102
- closeButton.style.cssText = 'border:1px solid #cbd5e1;border-radius:8px;padding:7px 14px;background:#fff;color:#172033;font:600 14px system-ui,sans-serif;cursor:pointer;';
114
+ closeButton.setAttribute('aria-label', restoring ? 'Close restore and return to game' : 'Close checkout and return to game');
115
+ closeButton.style.cssText = 'box-sizing:border-box;flex:1 1 auto;min-width:44px;min-height:44px;max-width:100%;height:auto;margin:0;border:1px solid #cbd5e1;border-radius:8px;padding:10px 14px;background:#fff;color:#172033;font:600 14px/1.4 system-ui,sans-serif;white-space:normal;overflow-wrap:anywhere;cursor:pointer;';
103
116
  const retryButton = doc.createElement('button');
104
117
  retryButton.type = 'button';
105
118
  retryButton.textContent = 'Retry';
106
- retryButton.setAttribute('aria-label', 'Reload this same checkout without starting another purchase');
119
+ retryButton.setAttribute('aria-label', restoring ? 'Reload this same restore session' : 'Reload this same checkout without starting another purchase');
107
120
  retryButton.style.cssText = closeButton.style.cssText;
121
+ closeButton.style.background = '#172033';
122
+ closeButton.style.color = '#fff';
108
123
  const controls = doc.createElement('div');
109
- controls.style.cssText = 'display:flex;gap:8px;';
124
+ controls.style.cssText = 'display:flex;flex-wrap:wrap;gap:8px;min-width:0;max-width:100%;margin-left:auto;';
110
125
  controls.append(retryButton, closeButton);
111
126
  header.append(heading, controls);
112
127
  const status = doc.createElement('div');
113
128
  status.setAttribute('role', 'status');
114
- status.style.cssText = 'padding:6px 16px;font:12px/1.4 system-ui,sans-serif;background:#f1f5f9;color:#172033;min-height:28px;box-sizing:border-box;';
115
- status.textContent = 'Your game stays open. Loading secure checkout…';
129
+ status.style.cssText = 'padding:6px 16px;font:12px/1.4 system-ui,sans-serif;background:#f1f5f9;color:#172033;box-sizing:border-box;white-space:normal;overflow-wrap:anywhere;';
130
+ status.textContent = restoring ? 'Your game stays open. Loading secure restore…' : 'Your game stays open. Loading secure checkout…';
116
131
  const iframe = doc.createElement('iframe');
117
132
  iframe.title = heading.textContent;
118
133
  iframe.setAttribute('allow', 'payment');
119
134
  iframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin allow-popups allow-popups-to-escape-sandbox');
120
135
  iframe.referrerPolicy = 'no-referrer';
121
- iframe.style.cssText = 'display:block;width:100%;height:calc(100% - 84px);border:0;background:#fff;';
136
+ iframe.style.cssText = 'display:block;flex:1 1 0%;min-width:0;min-height:0;width:100%;height:100%;margin:0;padding:0;border:0;background:#fff;';
122
137
  iframe.src = session.hosted_url;
123
- dialog.append(style, header, status, iframe);
138
+ chrome.append(header, status);
139
+ dialog.append(style, chrome, iframe);
124
140
  doc.body.append(dialog);
125
141
  const frameWindow = iframe.contentWindow;
126
142
  if (!frameWindow) { dialog.remove(); throw new Error('In-game checkout embedding is unavailable. Your game has not navigated.'); }
@@ -132,6 +148,14 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
132
148
  let frameLoadTimer: number | undefined;
133
149
  let appReadyTimer: number | undefined;
134
150
  let applicationReady = false;
151
+ let retryPhase: 'idle' | 'blank' | 'checkout' | 'failed' = 'idle';
152
+ let retryReady = false;
153
+ const isBlankDocument = (): boolean => {
154
+ // about:blank inherits our origin. A src attribute alone says nothing about
155
+ // which document emitted load; an old checkout load can still be queued.
156
+ try { return iframe.contentDocument?.URL === 'about:blank' && iframe.contentDocument.readyState === 'complete'; }
157
+ catch { return false; }
158
+ };
135
159
  const blocked: Array<{ element: HTMLElement; inert: boolean; ariaHidden: string | null }> = [];
136
160
  const report = (error: unknown): void => { try { options.onError?.(error); } catch { /* Consumer error handlers cannot prevent modal cleanup. */ } };
137
161
  const clearWatchdogs = (): void => {
@@ -143,20 +167,27 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
143
167
  const unavailable = (reason: 'load' | 'ready' | 'error'): void => {
144
168
  if (closed) return;
145
169
  clearWatchdogs();
170
+ if (retryPhase !== 'idle') { retryPhase = 'failed'; retryReady = false; }
146
171
  status.setAttribute('role', 'alert');
172
+ const flow = restoring ? 'Restore' : 'Checkout';
147
173
  status.textContent = reason === 'ready'
148
- ? 'Checkout did not become ready. Retry this same checkout or Close; your game remains open.'
149
- : 'Checkout could not load here. Retry this same checkout or Close; your game remains open.';
150
- report(new Error(reason === 'ready'
151
- ? 'Embedded checkout readiness timed out; no navigation or new payment was attempted.'
152
- : 'Embedded checkout loading unavailable; no navigation or new payment was attempted.'));
174
+ ? `${flow} did not become ready. Retry this same session or Close; your game remains open.`
175
+ : `${flow} could not load here. Retry this same session or Close; your game remains open.`;
176
+ report(new Error(restoring
177
+ ? `Embedded restore ${reason === 'ready' ? 'readiness timed out' : 'loading unavailable'}; no navigation was attempted.`
178
+ : reason === 'ready'
179
+ ? 'Embedded checkout readiness timed out; no navigation or new payment was attempted.'
180
+ : 'Embedded checkout loading unavailable; no navigation or new payment was attempted.'));
153
181
  };
154
182
  const markApplicationReady = (): void => {
155
183
  if (closed) return;
184
+ // A verified claim from the previous document may finish while Retry is
185
+ // navigating. Keep that claim, but it cannot certify the new document UI.
186
+ if (retryPhase !== 'idle') return;
156
187
  applicationReady = true;
157
188
  clearWatchdogs();
158
189
  status.setAttribute('role', 'status');
159
- status.textContent = 'Checkout is ready. Your game remains open underneath.';
190
+ status.textContent = restoring ? 'Restore is ready. Your game remains open underneath.' : 'Checkout is ready. Your game remains open underneath.';
160
191
  };
161
192
  const armLoadWatchdog = (): void => {
162
193
  clearWatchdogs();
@@ -196,29 +227,54 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
196
227
  };
197
228
  const onKey = (event: KeyboardEvent): void => {
198
229
  if (event.key === 'Escape') { event.preventDefault(); void overlay.close(); }
199
- if (event.key === 'Tab' && !closed) {
230
+ // Keep the trap while close waits for an in-flight claim/inventory callback.
231
+ if (event.key === 'Tab' && dialog.isConnected) {
200
232
  if (event.shiftKey && doc.activeElement === retryButton) { event.preventDefault(); iframe.focus(); }
201
233
  else if (!event.shiftKey && doc.activeElement === iframe) { event.preventDefault(); retryButton.focus(); }
202
234
  }
203
235
  };
204
236
  const onCancel = (event: Event): void => { event.preventDefault(); void overlay.close(); };
237
+ // Keyboard events inside the cross-origin document do not reach onKey.
238
+ // The hosted UI forwards Escape via this existing, strictly bound close message.
205
239
  const onCloseMessage = (event: MessageEvent<unknown>): void => {
206
240
  if (closed || event.origin !== options.checkoutOrigin || event.source !== frameWindow || !event.data || typeof event.data !== 'object') return;
207
241
  const message = event.data as Record<string, unknown>;
208
242
  if (message.version !== 1 || message.title_id !== options.titleId ||
209
243
  message.checkout_session_id !== session.id || message.nonce !== session.nonce) return;
210
- if (message.type === 'glitch.microtransaction.ready') { markApplicationReady(); return; }
244
+ if (message.type === 'glitch.microtransaction.ready') {
245
+ if (retryPhase === 'checkout' && !isBlankDocument()) retryReady = true;
246
+ else if (retryPhase === 'idle') markApplicationReady();
247
+ return;
248
+ }
211
249
  if (message.type !== 'glitch.microtransaction.close') return;
212
250
  void overlay.close(verified ? 'completed' : 'dismissed');
213
251
  };
214
- const onFrameError = (): void => { unavailable('error'); };
252
+ const onFrameError = (): void => {
253
+ // Events from the document being left cannot fail the blank navigation.
254
+ // Its watchdog still bounds a missing/failed intermediate load.
255
+ if (retryPhase === 'blank' || (retryPhase === 'checkout' && isBlankDocument())) return;
256
+ unavailable('error');
257
+ };
215
258
  const onFrameLoad = (): void => {
216
259
  if (closed) return;
260
+ if (retryPhase === 'failed') return;
261
+ if (retryPhase === 'blank') {
262
+ if (!isBlankDocument()) return;
263
+ retryPhase = 'checkout';
264
+ armLoadWatchdog();
265
+ iframe.src = session.hosted_url;
266
+ return;
267
+ }
268
+ if (retryPhase === 'checkout') {
269
+ if (isBlankDocument()) return;
270
+ retryPhase = 'idle';
271
+ if (retryReady) { retryReady = false; markApplicationReady(); return; }
272
+ }
217
273
  if (frameLoadTimer !== undefined) win.clearTimeout(frameLoadTimer);
218
274
  frameLoadTimer = undefined;
219
275
  if (applicationReady) return;
220
276
  status.setAttribute('role', 'status');
221
- status.textContent = 'Checkout document loaded. Waiting for the secure checkout interface…';
277
+ status.textContent = restoring ? 'Restore document loaded. Waiting for the secure restore interface…' : 'Checkout document loaded. Waiting for the secure checkout interface…';
222
278
  // A document load is not proof the app rendered. Repeated loads cannot
223
279
  // indefinitely extend this bounded wait; only explicit Retry resets it.
224
280
  if (appReadyTimer === undefined) appReadyTimer = win.setTimeout(() => unavailable('ready'), frameTimeout);
@@ -226,18 +282,27 @@ export function openMicrotransactionOverlay(options: MicrotransactionOverlayOpti
226
282
  const overlay: MicrotransactionOverlay = {
227
283
  element: dialog, iframe, refresh,
228
284
  retry: () => {
229
- if (closed) return;
230
- status.textContent = 'Retrying the same secure checkout. Your game stays open.';
285
+ if (closed || retryPhase === 'blank' || retryPhase === 'checkout') return;
286
+ status.textContent = restoring ? 'Retrying the same secure restore session. Your game stays open.' : 'Retrying the same secure checkout. Your game stays open.';
231
287
  status.setAttribute('role', 'status');
288
+ retryPhase = 'blank';
289
+ retryReady = false;
232
290
  armLoadWatchdog();
233
- iframe.src = session.hosted_url;
291
+ // Bootstrap removes #token with replaceState. Reassigning the original
292
+ // URL then only changes the fragment; it does not reload checkout/API.
293
+ // Commit a harmless blank document first, then navigate this SAME iframe
294
+ // to the exact original capability URL. Its WindowProxy/bridge stay pinned.
295
+ iframe.src = 'about:blank';
234
296
  },
235
297
  close: (reason = 'dismissed') => {
236
298
  if (closing) return closing;
237
299
  if (closed) return Promise.resolve();
238
300
  closed = true;
239
301
  clearWatchdogs();
240
- status.textContent = 'Finishing secure purchase verification. Your game stays open.';
302
+ retryPhase = 'failed';
303
+ retryReady = false;
304
+ status.setAttribute('role', 'status');
305
+ status.textContent = restoring ? 'Finishing secure restore verification. Your game stays open.' : 'Finishing secure purchase verification. Your game stays open.';
241
306
  closing = (async () => {
242
307
  try {
243
308
  // The dialog remains mounted until in-flight claim AND inventory callback finish.
@@ -227,7 +227,7 @@ class Requests {
227
227
  data?: any,
228
228
  params?: Record<string, any>,
229
229
  onUploadProgress?: (progressEvent: AxiosProgressEvent) => void,
230
- options?: Pick<AxiosRequestConfig, 'signal' | 'timeout'>
230
+ options?: Pick<AxiosRequestConfig, 'signal' | 'timeout'> & { excludeCommunityContext?: boolean }
231
231
  ): AxiosPromise<Response<T>> {
232
232
  // Process URL and params
233
233
  if (params && Object.keys(params).length > 0) {
@@ -241,7 +241,7 @@ class Requests {
241
241
  const formData = new FormData();
242
242
  formData.append(filename, file);
243
243
 
244
- if (Requests.community_id) {
244
+ if (Requests.community_id && !options?.excludeCommunityContext) {
245
245
  data = {
246
246
  ...data,
247
247
  communities: [Requests.community_id],
@@ -1,5 +1,6 @@
1
1
  import { Config } from "../config";
2
2
  import Storage from "./Storage";
3
+ import CryptoJS from 'crypto-js';
3
4
 
4
5
  // Type declarations for crypto functionality
5
6
  interface HmacInterface {
@@ -16,7 +17,7 @@ class BrowserCrypto implements CryptoInterface {
16
17
  private CryptoJS: any;
17
18
 
18
19
  constructor() {
19
- this.CryptoJS = require('crypto-js');
20
+ this.CryptoJS = CryptoJS;
20
21
  }
21
22
 
22
23
  createHmac(algorithm: string, secret: string): HmacInterface {
@@ -188,4 +189,4 @@ class Session {
188
189
  }
189
190
  }
190
191
 
191
- export default Session;
192
+ export default Session;