@usions/sdk 2.24.1 → 3.0.0-next.7
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/README.md +45 -277
- package/dist/browser.global.js +1972 -0
- package/dist/browser.global.js.map +1 -0
- package/dist/index.d.ts +337 -0
- package/dist/index.js +1955 -0
- package/dist/index.js.map +1 -0
- package/dist/testing.d.ts +23 -0
- package/dist/testing.js +63 -0
- package/dist/testing.js.map +1 -0
- package/dist/types-G075L8Nl.d.ts +83 -0
- package/package.json +27 -43
- package/src/browser.js +0 -6665
- package/src/index.js +0 -10
- package/src/modules/back-button.js +0 -26
- package/src/modules/backend-channel.js +0 -109
- package/src/modules/bot.js +0 -52
- package/src/modules/chat.js +0 -35
- package/src/modules/cloud.js +0 -67
- package/src/modules/core.js +0 -424
- package/src/modules/errors.js +0 -120
- package/src/modules/file-storage.js +0 -46
- package/src/modules/game-core.js +0 -278
- package/src/modules/game-direct.js +0 -267
- package/src/modules/game-methods.js +0 -753
- package/src/modules/game-netcode.js +0 -349
- package/src/modules/game-proxy.js +0 -182
- package/src/modules/game-reliability.js +0 -278
- package/src/modules/game-resilience.js +0 -245
- package/src/modules/game-socket.js +0 -212
- package/src/modules/index.js +0 -78
- package/src/modules/leaderboard.js +0 -46
- package/src/modules/lobby.js +0 -103
- package/src/modules/matchmaking.js +0 -140
- package/src/modules/misc.js +0 -152
- package/src/modules/netcode/binary.js +0 -113
- package/src/modules/netcode/delta.js +0 -236
- package/src/modules/netcode/index.js +0 -43
- package/src/modules/netcode/interest-grid.js +0 -227
- package/src/modules/netcode/interpolation.js +0 -235
- package/src/modules/netcode/lagcomp.js +0 -104
- package/src/modules/netcode/lockstep.js +0 -103
- package/src/modules/netcode/mesh-network.js +0 -103
- package/src/modules/netcode/mesh.js +0 -188
- package/src/modules/netcode/netsim.js +0 -77
- package/src/modules/netcode/ping.js +0 -69
- package/src/modules/netcode/prediction.js +0 -113
- package/src/modules/netcode/sender.js +0 -102
- package/src/modules/netcode/webtransport.js +0 -195
- package/src/modules/notify.js +0 -70
- package/src/modules/permissions.js +0 -157
- package/src/modules/results.js +0 -34
- package/src/modules/session.js +0 -62
- package/src/modules/storage.js +0 -64
- package/src/modules/ui.js +0 -111
- package/src/modules/user.js +0 -61
- package/src/modules/wallet.js +0 -253
- package/types/browser.d.ts +0 -5
- package/types/index.d.ts +0 -1306
package/src/modules/user.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK User Module — user info and authentication
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param {object} Usion - Reference to the main Usion object
|
|
7
|
-
*/
|
|
8
|
-
export function createUserModule(Usion) {
|
|
9
|
-
return {
|
|
10
|
-
_id: null,
|
|
11
|
-
_name: null,
|
|
12
|
-
_avatar: null,
|
|
13
|
-
_token: null,
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Get the current user's ID
|
|
17
|
-
* @returns {string|null}
|
|
18
|
-
*/
|
|
19
|
-
getId: function() {
|
|
20
|
-
return this._id || Usion.config.userId || null;
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Get the current user's display name
|
|
25
|
-
* @returns {string|null}
|
|
26
|
-
*/
|
|
27
|
-
getName: function() {
|
|
28
|
-
return this._name || Usion.config.userName || null;
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Get the current user's avatar URL
|
|
33
|
-
* @returns {string|null}
|
|
34
|
-
*/
|
|
35
|
-
getAvatar: function() {
|
|
36
|
-
return this._avatar || Usion.config.userAvatar || null;
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Get the user's auth token for socket connections
|
|
41
|
-
* @returns {string|null}
|
|
42
|
-
*/
|
|
43
|
-
getToken: function() {
|
|
44
|
-
return this._token || Usion.config.authToken || null;
|
|
45
|
-
},
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get full user profile
|
|
49
|
-
* @returns {Promise<object>} User profile with id, name, avatar
|
|
50
|
-
*/
|
|
51
|
-
getProfile: function() {
|
|
52
|
-
return Usion._request('GET_USER_PROFILE', {}).then(function(response) {
|
|
53
|
-
return response.profile || {
|
|
54
|
-
id: Usion.user.getId(),
|
|
55
|
-
name: Usion.user.getName(),
|
|
56
|
-
avatar: Usion.user.getAvatar()
|
|
57
|
-
};
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
}
|
package/src/modules/wallet.js
DELETED
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Wallet Module — wallet and payment operations
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { getNextRequestId, isTrustedMessageSource } from './core.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @param {object} Usion - Reference to the main Usion object
|
|
9
|
-
*/
|
|
10
|
-
export function createWalletModule(Usion) {
|
|
11
|
-
return {
|
|
12
|
-
_balance: null,
|
|
13
|
-
_balanceChangeHandler: null,
|
|
14
|
-
|
|
15
|
-
// Payment timing knobs (overridable, mainly for tests). The defaults are
|
|
16
|
-
// the production values.
|
|
17
|
-
_paymentTimeoutMs: 60000, // wait this long for the direct PAYMENT_SUCCESS
|
|
18
|
-
_recoveryPollMs: 2000, // gap between result-recovery re-queries
|
|
19
|
-
_recoveryMaxPolls: 10, // bounded recovery window after the timeout
|
|
20
|
-
_recoveryQueryTimeoutMs: 8000, // per re-query round-trip budget
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Get current wallet balance
|
|
24
|
-
* @param {object} [opts]
|
|
25
|
-
* @param {boolean} [opts.fresh] - Bypass the cache and re-query the host.
|
|
26
|
-
* Use after out-of-band balance changes (e.g. a server-side settle).
|
|
27
|
-
* @returns {Promise<number>} Balance in credits
|
|
28
|
-
*/
|
|
29
|
-
getBalance: function(opts) {
|
|
30
|
-
const self = this;
|
|
31
|
-
const fresh = !!(opts && opts.fresh);
|
|
32
|
-
|
|
33
|
-
// If we have cached balance, return it
|
|
34
|
-
if (!fresh && self._balance !== null) {
|
|
35
|
-
return Promise.resolve(self._balance);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return Usion._request('GET_BALANCE', {}).then(function(response) {
|
|
39
|
-
self._balance = response.balance;
|
|
40
|
-
return response.balance;
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Check if user has enough credits
|
|
46
|
-
* @param {number} amount - Amount to check
|
|
47
|
-
* @returns {Promise<boolean>}
|
|
48
|
-
*/
|
|
49
|
-
hasCredits: function(amount) {
|
|
50
|
-
return this.getBalance().then(function(balance) {
|
|
51
|
-
return balance >= amount;
|
|
52
|
-
});
|
|
53
|
-
},
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Request payment from user with balance check.
|
|
57
|
-
*
|
|
58
|
-
* The charge is debited immediately by the host (escrow); the resolved
|
|
59
|
-
* value carries a `receiptToken` the mini-app server later settles/refunds.
|
|
60
|
-
*
|
|
61
|
-
* Reliability: if the host's PAYMENT_SUCCESS message is lost (network blip,
|
|
62
|
-
* backgrounded tab, race) the user may ALREADY be charged. Instead of
|
|
63
|
-
* rejecting at the timeout, this re-queries the host for the result and
|
|
64
|
-
* recovers the receipt token — so a dropped message never strands a paid
|
|
65
|
-
* charge. Pass `idempotencyKey` to additionally guarantee that any retry of
|
|
66
|
-
* the SAME intent reuses the SAME charge (the host + backend dedupe on it),
|
|
67
|
-
* so the user can never be charged twice even across reloads.
|
|
68
|
-
*
|
|
69
|
-
* @param {number} amount - Credit amount to charge
|
|
70
|
-
* @param {string} reason - Description shown to user
|
|
71
|
-
* @param {object} [data] - Optional options/payload. `data.idempotencyKey`
|
|
72
|
-
* (string) makes the charge safely retryable. Any other fields are
|
|
73
|
-
* forwarded to the host verbatim (backward compatible).
|
|
74
|
-
* @returns {Promise} Resolves on payment success, rejects on failure
|
|
75
|
-
*/
|
|
76
|
-
requestPayment: function(amount, reason, data) {
|
|
77
|
-
const self = this;
|
|
78
|
-
|
|
79
|
-
const opts = (data && typeof data === 'object') ? data : null;
|
|
80
|
-
const idempotencyKey = (opts && typeof opts.idempotencyKey === 'string' && opts.idempotencyKey)
|
|
81
|
-
? opts.idempotencyKey
|
|
82
|
-
: null;
|
|
83
|
-
|
|
84
|
-
return new Promise(function(resolve, reject) {
|
|
85
|
-
const requestId = getNextRequestId();
|
|
86
|
-
const timeoutMs = self._paymentTimeoutMs || 60000;
|
|
87
|
-
let settled = false;
|
|
88
|
-
let recoverTimer = null;
|
|
89
|
-
|
|
90
|
-
function cleanup() {
|
|
91
|
-
clearTimeout(timer);
|
|
92
|
-
if (recoverTimer) clearTimeout(recoverTimer);
|
|
93
|
-
window.removeEventListener('message', handler);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function finishResolve(response) {
|
|
97
|
-
if (settled) return;
|
|
98
|
-
settled = true;
|
|
99
|
-
cleanup();
|
|
100
|
-
// Update cached balance from the authoritative new balance, else
|
|
101
|
-
// best-effort subtract now and re-query the host so the cache
|
|
102
|
-
// converges on the real value (an estimate would otherwise stick
|
|
103
|
-
// until reload).
|
|
104
|
-
if (response && response.newBalance !== undefined) {
|
|
105
|
-
self._balance = response.newBalance;
|
|
106
|
-
} else {
|
|
107
|
-
if (self._balance !== null) self._balance -= amount;
|
|
108
|
-
self.getBalance({ fresh: true }).catch(function() { /* keep estimate */ });
|
|
109
|
-
}
|
|
110
|
-
resolve(response);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function finishReject(error) {
|
|
114
|
-
if (settled) return;
|
|
115
|
-
settled = true;
|
|
116
|
-
cleanup();
|
|
117
|
-
reject(error);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Direct response from the host. Kept registered through the recovery
|
|
121
|
-
// window so a late PAYMENT_SUCCESS still resolves.
|
|
122
|
-
function handler(event) {
|
|
123
|
-
// Only honor payment results from the trusted host shell — a forged
|
|
124
|
-
// PAYMENT_SUCCESS must never resolve this promise.
|
|
125
|
-
if (!isTrustedMessageSource(event)) return;
|
|
126
|
-
|
|
127
|
-
let response;
|
|
128
|
-
try {
|
|
129
|
-
response = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
130
|
-
} catch (e) {
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Only accept responses for this specific payment request.
|
|
135
|
-
if (!response || response._requestId !== requestId) {
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (response.type === 'PAYMENT_SUCCESS') {
|
|
140
|
-
finishResolve(response);
|
|
141
|
-
} else if (response.type === 'PAYMENT_FAILED') {
|
|
142
|
-
finishReject(new Error(response.reason || 'Payment failed'));
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// After the direct deadline, re-query the host for this payment's
|
|
147
|
-
// result instead of rejecting.
|
|
148
|
-
// succeeded -> resolve with the recovered token
|
|
149
|
-
// failed -> reject
|
|
150
|
-
// pending -> the modal is STILL OPEN / charge still in progress, so
|
|
151
|
-
// the charge is fully recoverable: keep waiting (do NOT
|
|
152
|
-
// count this toward the give-up cap). A slow user who
|
|
153
|
-
// confirms minutes later still resolves — via the next
|
|
154
|
-
// 'succeeded' poll or the direct handler, which stays
|
|
155
|
-
// registered. Without this, a charge made after we gave
|
|
156
|
-
// up would be stranded (the original bug, shifted to the
|
|
157
|
-
// slow-confirm case).
|
|
158
|
-
// none/other -> likely no charge; confirm twice then reject cleanly so
|
|
159
|
-
// a fresh requestPayment is safe.
|
|
160
|
-
// The bounded cap only governs the give-up paths (none-streak and a host
|
|
161
|
-
// that never answers the query at all, e.g. an older host shell).
|
|
162
|
-
function recoverPaymentResult() {
|
|
163
|
-
const pollMs = self._recoveryPollMs || 2000;
|
|
164
|
-
const maxPolls = self._recoveryMaxPolls || 10;
|
|
165
|
-
const queryTimeoutMs = self._recoveryQueryTimeoutMs || 8000;
|
|
166
|
-
let noneStreak = 0;
|
|
167
|
-
let errorStreak = 0;
|
|
168
|
-
|
|
169
|
-
function poll() {
|
|
170
|
-
if (settled) return;
|
|
171
|
-
Usion._request('PAYMENT_RESULT_QUERY', {
|
|
172
|
-
paymentRequestId: requestId,
|
|
173
|
-
idempotencyKey: idempotencyKey || undefined,
|
|
174
|
-
}, queryTimeoutMs).then(function(res) {
|
|
175
|
-
if (settled) return;
|
|
176
|
-
const status = res && res.status;
|
|
177
|
-
if (status === 'succeeded') {
|
|
178
|
-
finishResolve({
|
|
179
|
-
type: 'PAYMENT_SUCCESS',
|
|
180
|
-
_requestId: requestId,
|
|
181
|
-
newBalance: res.newBalance,
|
|
182
|
-
transactionId: res.transactionId,
|
|
183
|
-
receiptToken: res.receiptToken,
|
|
184
|
-
});
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
if (status === 'failed') {
|
|
188
|
-
finishReject(new Error(res.reason || 'Payment failed'));
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
if (status === 'pending') {
|
|
192
|
-
// Charge still recoverable — keep waiting, no give-up budget.
|
|
193
|
-
noneStreak = 0;
|
|
194
|
-
errorStreak = 0;
|
|
195
|
-
recoverTimer = setTimeout(poll, pollMs);
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
// 'none' or unknown — host is confident no charge exists. Confirm
|
|
199
|
-
// twice (guards a host that simply hasn't recorded yet) then reject.
|
|
200
|
-
errorStreak = 0;
|
|
201
|
-
noneStreak += 1;
|
|
202
|
-
if (noneStreak >= 2) {
|
|
203
|
-
finishReject(new Error('Payment confirmation timeout'));
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
recoverTimer = setTimeout(poll, pollMs);
|
|
207
|
-
}).catch(function() {
|
|
208
|
-
// Host didn't answer the query (e.g. an older host shell without
|
|
209
|
-
// PAYMENT_RESULT_QUERY support). Retry a bounded number of times,
|
|
210
|
-
// then fall back to the original timeout rejection.
|
|
211
|
-
if (settled) return;
|
|
212
|
-
errorStreak += 1;
|
|
213
|
-
if (errorStreak >= maxPolls) {
|
|
214
|
-
finishReject(new Error('Payment confirmation timeout'));
|
|
215
|
-
return;
|
|
216
|
-
}
|
|
217
|
-
recoverTimer = setTimeout(poll, pollMs);
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
poll();
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
window.addEventListener('message', handler);
|
|
225
|
-
const timer = setTimeout(recoverPaymentResult, timeoutMs);
|
|
226
|
-
|
|
227
|
-
// Send payment request
|
|
228
|
-
Usion._post({
|
|
229
|
-
type: 'PAYMENT_REQUEST',
|
|
230
|
-
_requestId: requestId,
|
|
231
|
-
amount: amount,
|
|
232
|
-
reason: reason,
|
|
233
|
-
data: data,
|
|
234
|
-
idempotencyKey: idempotencyKey || undefined,
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
},
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Listen for balance changes
|
|
241
|
-
* @param {function} callback - Called with new balance
|
|
242
|
-
*/
|
|
243
|
-
onBalanceChange: function(callback) {
|
|
244
|
-
const self = this;
|
|
245
|
-
self._balanceChangeHandler = callback;
|
|
246
|
-
return function() {
|
|
247
|
-
if (self._balanceChangeHandler === callback) {
|
|
248
|
-
self._balanceChangeHandler = null;
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
}
|