@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.
Files changed (58) hide show
  1. package/README.md +45 -277
  2. package/dist/browser.global.js +1972 -0
  3. package/dist/browser.global.js.map +1 -0
  4. package/dist/index.d.ts +337 -0
  5. package/dist/index.js +1955 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/testing.d.ts +23 -0
  8. package/dist/testing.js +63 -0
  9. package/dist/testing.js.map +1 -0
  10. package/dist/types-G075L8Nl.d.ts +83 -0
  11. package/package.json +27 -43
  12. package/src/browser.js +0 -6665
  13. package/src/index.js +0 -10
  14. package/src/modules/back-button.js +0 -26
  15. package/src/modules/backend-channel.js +0 -109
  16. package/src/modules/bot.js +0 -52
  17. package/src/modules/chat.js +0 -35
  18. package/src/modules/cloud.js +0 -67
  19. package/src/modules/core.js +0 -424
  20. package/src/modules/errors.js +0 -120
  21. package/src/modules/file-storage.js +0 -46
  22. package/src/modules/game-core.js +0 -278
  23. package/src/modules/game-direct.js +0 -267
  24. package/src/modules/game-methods.js +0 -753
  25. package/src/modules/game-netcode.js +0 -349
  26. package/src/modules/game-proxy.js +0 -182
  27. package/src/modules/game-reliability.js +0 -278
  28. package/src/modules/game-resilience.js +0 -245
  29. package/src/modules/game-socket.js +0 -212
  30. package/src/modules/index.js +0 -78
  31. package/src/modules/leaderboard.js +0 -46
  32. package/src/modules/lobby.js +0 -103
  33. package/src/modules/matchmaking.js +0 -140
  34. package/src/modules/misc.js +0 -152
  35. package/src/modules/netcode/binary.js +0 -113
  36. package/src/modules/netcode/delta.js +0 -236
  37. package/src/modules/netcode/index.js +0 -43
  38. package/src/modules/netcode/interest-grid.js +0 -227
  39. package/src/modules/netcode/interpolation.js +0 -235
  40. package/src/modules/netcode/lagcomp.js +0 -104
  41. package/src/modules/netcode/lockstep.js +0 -103
  42. package/src/modules/netcode/mesh-network.js +0 -103
  43. package/src/modules/netcode/mesh.js +0 -188
  44. package/src/modules/netcode/netsim.js +0 -77
  45. package/src/modules/netcode/ping.js +0 -69
  46. package/src/modules/netcode/prediction.js +0 -113
  47. package/src/modules/netcode/sender.js +0 -102
  48. package/src/modules/netcode/webtransport.js +0 -195
  49. package/src/modules/notify.js +0 -70
  50. package/src/modules/permissions.js +0 -157
  51. package/src/modules/results.js +0 -34
  52. package/src/modules/session.js +0 -62
  53. package/src/modules/storage.js +0 -64
  54. package/src/modules/ui.js +0 -111
  55. package/src/modules/user.js +0 -61
  56. package/src/modules/wallet.js +0 -253
  57. package/types/browser.d.ts +0 -5
  58. package/types/index.d.ts +0 -1306
package/src/index.js DELETED
@@ -1,10 +0,0 @@
1
- /**
2
- * @usions/sdk — ES module entry point for the Usion Mini App SDK
3
- *
4
- * Usage:
5
- * import Usion from '@usions/sdk';
6
- * // or
7
- * import { Usion } from '@usions/sdk';
8
- */
9
-
10
- export { default as Usion, default } from './modules/index.js';
@@ -1,26 +0,0 @@
1
- /**
2
- * Usion SDK Back Button — claim/release host back button for in-app navigation
3
- */
4
-
5
- export function createBackButtonMethods(Usion) {
6
- return {
7
- /**
8
- * Claim the host app's back button for one-time in-app navigation.
9
- * When claimed, pressing back sends BACK_BUTTON_PRESSED to the mini app
10
- * instead of closing it. Automatically resets after one press.
11
- * @param {function} callback - Called when the user presses the claimed back button
12
- */
13
- claimBackButton: function(callback) {
14
- Usion._backButtonCallback = callback;
15
- Usion._post({ type: 'CLAIM_BACK_BUTTON' });
16
- },
17
-
18
- /**
19
- * Release a previously claimed back button, restoring default close behavior.
20
- */
21
- releaseBackButton: function() {
22
- Usion._backButtonCallback = null;
23
- Usion._post({ type: 'RELEASE_BACK_BUTTON' });
24
- }
25
- };
26
- }
@@ -1,109 +0,0 @@
1
- /**
2
- * Usion SDK — unified backend channel.
3
- *
4
- * One switchboard for backend request/response + server push that works in
5
- * every mode, so features (lobby, matchmaking, presence, …) never reach for a
6
- * specific socket:
7
- *
8
- * - standalone / platform : uses the SDK's own Socket.IO socket (game.socket)
9
- * - embedded (iframe/WebView): relays through the parent app via postMessage
10
- * (BACKEND_EMIT request → host emits on its authenticated socket; the host
11
- * forwards allow-listed server pushes back as BACKEND_EVENT)
12
- *
13
- * Security: in embedded mode the host MUST restrict which events it relays to a
14
- * safe, namespaced allow-list (e.g. lobby:* / mm:*) so a mini-app can't abuse
15
- * the user's authenticated connection. The backend re-validates every call.
16
- */
17
- import { UsionError, toUsionError, ERROR_CODES } from './errors.js';
18
-
19
- export function applyBackendChannel(Usion) {
20
- Usion._backendHandlers = {};
21
- Usion._boundSockets = typeof WeakSet !== 'undefined' ? new WeakSet() : null;
22
-
23
- /**
24
- * Bind a Socket.IO socket so any allow-listed server event is routed to the
25
- * registered _backendOn handlers (via onAny — robust to registration order).
26
- */
27
- Usion._bindBackendSocket = function (socket) {
28
- if (!socket || typeof socket.onAny !== 'function') return;
29
- if (this._boundSockets) {
30
- if (this._boundSockets.has(socket)) return;
31
- this._boundSockets.add(socket);
32
- }
33
- const self = this;
34
- socket.onAny(function (event, payload) {
35
- const h = self._backendHandlers[event];
36
- if (h) h(payload);
37
- });
38
- };
39
-
40
- /** Subscribe to a backend server-push event (works in all modes). */
41
- Usion._backendOn = function (event, handler) {
42
- this._backendHandlers[event] = handler;
43
- const s = this.game && this.game.socket;
44
- if (s) {
45
- if (typeof s.onAny === 'function') this._bindBackendSocket(s);
46
- else if (typeof s.on === 'function') s.on(event, handler); // fallback (tests / minimal sockets)
47
- }
48
- };
49
-
50
- /**
51
- * Emit a backend request and await its ack. Routes to the SDK socket when
52
- * standalone, or through the parent host when embedded.
53
- * Standalone apps that never call Usion.game.connect() (e.g. a non-game
54
- * app using cloud/leaderboard/notify) get an automatic one-time connect —
55
- * the backend channel is not coupled to starting a game.
56
- * Rejections are always UsionError (stable `code`, plus `retryAfter` on
57
- * RATE_LIMITED), regardless of transport.
58
- * @returns {Promise<any>}
59
- */
60
- Usion._backendEmit = function (event, data, timeout) {
61
- const self = this;
62
- timeout = timeout || 8000;
63
-
64
- function emitOn(sock) {
65
- return new Promise(function (resolve, reject) {
66
- let done = false;
67
- const timer = setTimeout(function () {
68
- if (done) return; done = true;
69
- reject(new UsionError(ERROR_CODES.REQUEST_TIMEOUT, 'Backend request timeout'));
70
- }, timeout);
71
- try {
72
- sock.emit(event, data || {}, function (resp) {
73
- if (done) return; done = true; clearTimeout(timer);
74
- if (resp && resp.error) reject(toUsionError(resp));
75
- else resolve(resp);
76
- });
77
- } catch (e) { clearTimeout(timer); reject(toUsionError(e)); }
78
- });
79
- }
80
-
81
- const s = self.game && self.game.socket;
82
- if (s && s.connected) return emitOn(s);
83
- if (self._isEmbedded) {
84
- // Host relays this onto its authenticated socket and replies with the ack.
85
- return self._request('BACKEND_EMIT', { event: event, data: data || {} }, timeout)
86
- .catch(function (e) { throw toUsionError(e); });
87
- }
88
-
89
- // Standalone without a live socket. If no socket exists yet, auto-connect
90
- // once (connect() dedupes concurrent callers via _connectPromise). A
91
- // socket that exists but is mid-reconnect is left to Socket.IO's own
92
- // reconnection — the caller gets a coded error and can retry.
93
- const connectionMode = (self.config && self.config.connectionMode) || 'platform';
94
- if (!s && connectionMode !== 'direct'
95
- && self.game && typeof self.game.connect === 'function') {
96
- return self.game.connect().then(function () {
97
- const sock = self.game.socket;
98
- if (sock && sock.connected) return emitOn(sock);
99
- throw new UsionError(ERROR_CODES.NOT_CONNECTED, 'No backend connection');
100
- }).catch(function (e) {
101
- if (e instanceof UsionError) throw e;
102
- throw new UsionError(ERROR_CODES.NOT_CONNECTED,
103
- 'Backend connect failed: ' + (e && e.message ? e.message : String(e)));
104
- });
105
- }
106
- return Promise.reject(new UsionError(ERROR_CODES.NOT_CONNECTED,
107
- 'No backend connection — socket is offline or reconnecting'));
108
- };
109
- }
@@ -1,52 +0,0 @@
1
- /**
2
- * Usion SDK Bot Bridge — for inline bot iframes
3
- */
4
-
5
- export function createBotModule(Usion) {
6
- return {
7
- _messageHandler: null,
8
-
9
- /**
10
- * Call a bot action. Delivered as an "iframe_action" webhook to the bot's server.
11
- * @param {string} action - Action name (e.g., "submit_form", "select_item")
12
- * @param {object} [data] - Action payload
13
- * @returns {Promise} Resolves when the host app confirms delivery
14
- */
15
- callAction: function(action, data) {
16
- return Usion._request('CALL_BOT', { action: action, data: data || {} }, 30000);
17
- },
18
-
19
- /**
20
- * Send a message as if the user typed it. Triggers the normal bot webhook flow.
21
- * @param {string} text - Message text
22
- */
23
- sendMessage: function(text) {
24
- Usion._post({ type: 'SEND_USER_MESSAGE', text: text });
25
- },
26
-
27
- /**
28
- * Update context metadata visible to the bot on the next webhook delivery.
29
- * @param {object} ctx - Context key/value pairs
30
- */
31
- updateContext: function(ctx) {
32
- Usion._post({ type: 'UPDATE_CONTEXT', context: ctx });
33
- },
34
-
35
- /**
36
- * Close the iframe, optionally returning a result to the bot.
37
- * @param {object} [result] - Optional result data
38
- */
39
- close: function(result) {
40
- Usion._post({ type: 'CLOSE', result: result });
41
- },
42
-
43
- /**
44
- * Listen for new bot messages in the conversation.
45
- * Called whenever the bot sends a message (text, components, etc.).
46
- * @param {function} callback - Called with { id, content, content_type, components, sender_id }
47
- */
48
- onMessage: function(callback) {
49
- this._messageHandler = callback;
50
- }
51
- };
52
- }
@@ -1,35 +0,0 @@
1
- /**
2
- * Usion SDK Chat Module — messaging between users
3
- */
4
-
5
- /**
6
- * @param {object} Usion - Reference to the main Usion object
7
- */
8
- export function createChatModule(Usion) {
9
- return {
10
- /**
11
- * Request to send a message to another user.
12
- * The parent app will show a confirmation prompt to the user.
13
- * @param {string} recipientId - Usion user ID of the recipient
14
- * @param {string} message - Message content to send
15
- * @returns {Promise<{success: boolean, reason?: string}>}
16
- */
17
- sendMessage: function(recipientId, message) {
18
- return Usion._request('SEND_MESSAGE_REQUEST', {
19
- recipientId: recipientId,
20
- message: message
21
- });
22
- },
23
-
24
- /**
25
- * Create a personal chat with another user (no message sent).
26
- * @param {string} peerUserId - Usion user ID of the other user
27
- * @returns {Promise<{chatId: string, peerName: string, peerUsername: string, peerAvatar: string}>}
28
- */
29
- createPersonalChat: function(peerUserId) {
30
- return Usion._request('CREATE_PERSONAL_CHAT', {
31
- peerUserId: peerUserId
32
- });
33
- }
34
- };
35
- }
@@ -1,67 +0,0 @@
1
- /**
2
- * Usion SDK Cloud — server-persisted KV storage for mini-apps.
3
- *
4
- * Unlike `Usion.storage` (device-local), cloud values live on the backend and
5
- * survive reinstall / device switch. Enabled for every service, bounded by
6
- * quotas (64 KB per value, 200 keys / 1 MB per bucket, 60 ops/min).
7
- * Rides the unified backend channel, so it works standalone AND embedded.
8
- *
9
- * Per-user scope (each user sees only their own data):
10
- * await Usion.cloud.set('save', { level: 3 });
11
- * const save = await Usion.cloud.get('save'); // null if absent
12
- * await Usion.cloud.remove('save');
13
- * const keys = await Usion.cloud.keys();
14
- *
15
- * Shared scope (one bucket per app — ALL users read and write):
16
- * await Usion.cloud.shared.set('motd', 'hello');
17
- * const plays = await Usion.cloud.shared.incr('plays'); // atomic counter
18
- *
19
- * Keys: 1-128 chars of A-Za-z0-9_.-:/
20
- */
21
- export function createCloudModule(Usion) {
22
- function serviceId(opts) {
23
- return (opts && opts.serviceId) || (Usion.config && Usion.config.serviceId);
24
- }
25
-
26
- function emit(event, scope, extra, opts) {
27
- var payload = Object.assign({ service_id: serviceId(opts), scope: scope }, extra);
28
- return Usion._backendEmit(event, payload);
29
- }
30
-
31
- function scopeApi(scope) {
32
- return {
33
- /** Get a value; resolves to null when the key doesn't exist. */
34
- get: function (key, opts) {
35
- return emit('kv:get', scope, { key: key }, opts)
36
- .then(function (r) { return r && r.exists ? r.value : null; });
37
- },
38
-
39
- /** Set a JSON-serializable value (max 64 KB). */
40
- set: function (key, value, opts) {
41
- return emit('kv:set', scope, { key: key, value: value }, opts);
42
- },
43
-
44
- /** Remove a key. Resolves to { success, removed }. */
45
- remove: function (key, opts) {
46
- return emit('kv:remove', scope, { key: key }, opts);
47
- },
48
-
49
- /** List keys in this scope's bucket. */
50
- keys: function (opts) {
51
- return emit('kv:keys', scope, {}, opts)
52
- .then(function (r) { return (r && r.keys) || []; });
53
- },
54
- };
55
- }
56
-
57
- var cloud = scopeApi('user');
58
- cloud.shared = scopeApi('shared');
59
-
60
- /** Atomically increment a shared numeric value (default delta 1). Resolves to the new value. */
61
- cloud.shared.incr = function (key, delta, opts) {
62
- return emit('kv:incr', 'shared', { key: key, delta: delta == null ? 1 : delta }, opts)
63
- .then(function (r) { return r && r.value; });
64
- };
65
-
66
- return cloud;
67
- }