@usions/sdk 2.10.0 → 2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usions/sdk",
3
- "version": "2.10.0",
3
+ "version": "2.11.0",
4
4
  "description": "Usion Mini App SDK for iframe games and services",
5
5
  "type": "module",
6
6
  "main": "src/modules/index.js",
package/src/browser.js CHANGED
@@ -4359,6 +4359,74 @@ var Usion = (function () {
4359
4359
  };
4360
4360
  }
4361
4361
 
4362
+ /**
4363
+ * Usion SDK Cloud — server-persisted KV storage for mini-apps.
4364
+ *
4365
+ * Unlike `Usion.storage` (device-local), cloud values live on the backend and
4366
+ * survive reinstall / device switch. Enabled for every service, bounded by
4367
+ * quotas (64 KB per value, 200 keys / 1 MB per bucket, 60 ops/min).
4368
+ * Rides the unified backend channel, so it works standalone AND embedded.
4369
+ *
4370
+ * Per-user scope (each user sees only their own data):
4371
+ * await Usion.cloud.set('save', { level: 3 });
4372
+ * const save = await Usion.cloud.get('save'); // null if absent
4373
+ * await Usion.cloud.remove('save');
4374
+ * const keys = await Usion.cloud.keys();
4375
+ *
4376
+ * Shared scope (one bucket per app — ALL users read and write):
4377
+ * await Usion.cloud.shared.set('motd', 'hello');
4378
+ * const plays = await Usion.cloud.shared.incr('plays'); // atomic counter
4379
+ *
4380
+ * Keys: 1-128 chars of A-Za-z0-9_.-:/
4381
+ */
4382
+ function createCloudModule(Usion) {
4383
+ function serviceId(opts) {
4384
+ return (opts && opts.serviceId) || (Usion.config && Usion.config.serviceId);
4385
+ }
4386
+
4387
+ function emit(event, scope, extra, opts) {
4388
+ var payload = Object.assign({ service_id: serviceId(opts), scope: scope }, extra);
4389
+ return Usion._backendEmit(event, payload);
4390
+ }
4391
+
4392
+ function scopeApi(scope) {
4393
+ return {
4394
+ /** Get a value; resolves to null when the key doesn't exist. */
4395
+ get: function (key, opts) {
4396
+ return emit('kv:get', scope, { key: key }, opts)
4397
+ .then(function (r) { return r && r.exists ? r.value : null; });
4398
+ },
4399
+
4400
+ /** Set a JSON-serializable value (max 64 KB). */
4401
+ set: function (key, value, opts) {
4402
+ return emit('kv:set', scope, { key: key, value: value }, opts);
4403
+ },
4404
+
4405
+ /** Remove a key. Resolves to { success, removed }. */
4406
+ remove: function (key, opts) {
4407
+ return emit('kv:remove', scope, { key: key }, opts);
4408
+ },
4409
+
4410
+ /** List keys in this scope's bucket. */
4411
+ keys: function (opts) {
4412
+ return emit('kv:keys', scope, {}, opts)
4413
+ .then(function (r) { return (r && r.keys) || []; });
4414
+ },
4415
+ };
4416
+ }
4417
+
4418
+ var cloud = scopeApi('user');
4419
+ cloud.shared = scopeApi('shared');
4420
+
4421
+ /** Atomically increment a shared numeric value (default delta 1). Resolves to the new value. */
4422
+ cloud.shared.incr = function (key, delta, opts) {
4423
+ return emit('kv:incr', 'shared', { key: key, delta: delta == null ? 1 : delta }, opts)
4424
+ .then(function (r) { return r && r.value; });
4425
+ };
4426
+
4427
+ return cloud;
4428
+ }
4429
+
4362
4430
  /**
4363
4431
  * Usion SDK Matchmaking — pair up with online strangers ("quick match").
4364
4432
  *
@@ -4552,6 +4620,7 @@ var Usion = (function () {
4552
4620
  applyBackendChannel(Usion);
4553
4621
  Usion.lobby = createLobbyModule(Usion);
4554
4622
  Usion.leaderboard = createLeaderboardModule(Usion);
4623
+ Usion.cloud = createCloudModule(Usion);
4555
4624
  Usion.matchmaking = createMatchmakingModule(Usion);
4556
4625
 
4557
4626
  // Netcode toolkit (transport-agnostic, zero-dependency).
@@ -0,0 +1,67 @@
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
+ }
@@ -27,6 +27,7 @@ import { createFileStorageModule } from './file-storage.js';
27
27
  import { createGameModule } from './game-core.js';
28
28
  import { createLobbyModule } from './lobby.js';
29
29
  import { createLeaderboardModule } from './leaderboard.js';
30
+ import { createCloudModule } from './cloud.js';
30
31
  import { createMatchmakingModule } from './matchmaking.js';
31
32
  import { applyBackendChannel } from './backend-channel.js';
32
33
  import { netcode } from './netcode/index.js';
@@ -47,6 +48,7 @@ Usion.game = createGameModule(Usion);
47
48
  applyBackendChannel(Usion);
48
49
  Usion.lobby = createLobbyModule(Usion);
49
50
  Usion.leaderboard = createLeaderboardModule(Usion);
51
+ Usion.cloud = createCloudModule(Usion);
50
52
  Usion.matchmaking = createMatchmakingModule(Usion);
51
53
 
52
54
  // Netcode toolkit (transport-agnostic, zero-dependency).