@usions/sdk 2.24.2 → 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 +25 -46
- package/src/browser.js +0 -6725
- 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-ack.js +0 -32
- package/src/modules/game-core.js +0 -278
- package/src/modules/game-direct.js +0 -267
- package/src/modules/game-methods.js +0 -782
- package/src/modules/game-netcode.js +0 -349
- package/src/modules/game-proxy.js +0 -184
- package/src/modules/game-reliability.js +0 -278
- package/src/modules/game-resilience.js +0 -245
- package/src/modules/game-socket.js +0 -214
- 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 -1308
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Netcode — WebTransport (HTTP/3 / QUIC) client transport.
|
|
3
|
-
*
|
|
4
|
-
* The lowest-latency *client-server* path for browser games (Baseline across
|
|
5
|
-
* major browsers since 2026). Unlike WebSocket (TCP → head-of-line blocking)
|
|
6
|
-
* it offers UDP-like unreliable **datagrams** — the newest snapshot always
|
|
7
|
-
* gets through — plus reliable **streams** for must-arrive events, over a
|
|
8
|
-
* single QUIC connection, without WebRTC's ICE/STUN/TURN/SDP complexity.
|
|
9
|
-
*
|
|
10
|
-
* Uses the native `WebTransport` API (zero dependency). On the server, pair
|
|
11
|
-
* with an HTTP/3 server such as the open-source `@fails-components/webtransport`
|
|
12
|
-
* (Node). Same interface shape as MeshConnection so it drops into the same
|
|
13
|
-
* snapshot sender/receiver + interpolation pipeline.
|
|
14
|
-
*
|
|
15
|
-
* Framing:
|
|
16
|
-
* - datagram = [seq:uint32 BE][type:uint8][payload] (one datagram = one msg)
|
|
17
|
-
* - stream = [len:uint32 BE][type:uint8][payload] (len = 1 + payload bytes)
|
|
18
|
-
* - type 0 = binary (Uint8Array), 1 = JSON (utf8). Sequenced datagrams drop
|
|
19
|
-
* stale/out-of-order frames (receiver only advances).
|
|
20
|
-
*/
|
|
21
|
-
const _enc = typeof TextEncoder !== 'undefined' ? new TextEncoder() : null;
|
|
22
|
-
const _dec = typeof TextDecoder !== 'undefined' ? new TextDecoder() : null;
|
|
23
|
-
|
|
24
|
-
function toBytes(s) { return _enc ? _enc.encode(s) : Uint8Array.from(Buffer.from(s, 'utf8')); }
|
|
25
|
-
function fromBytes(b) { return _dec ? _dec.decode(b) : Buffer.from(b).toString('utf8'); }
|
|
26
|
-
function isBinary(d) { return d instanceof ArrayBuffer || ArrayBuffer.isView(d); }
|
|
27
|
-
|
|
28
|
-
/** Encode a value to { type, bytes }: binary passthrough or JSON utf8. */
|
|
29
|
-
function encodePayload(data) {
|
|
30
|
-
if (isBinary(data)) return { type: 0, bytes: data instanceof Uint8Array ? data : new Uint8Array(data.buffer || data) };
|
|
31
|
-
return { type: 1, bytes: toBytes(JSON.stringify(data)) };
|
|
32
|
-
}
|
|
33
|
-
function decodePayload(type, bytes) {
|
|
34
|
-
if (type === 1) { try { return JSON.parse(fromBytes(bytes)); } catch (_) { return null; } }
|
|
35
|
-
return bytes;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/** datagram frame: [seq:4][type:1][payload] */
|
|
39
|
-
export function encodeDatagram(seq, data) {
|
|
40
|
-
const { type, bytes } = encodePayload(data);
|
|
41
|
-
const out = new Uint8Array(5 + bytes.length);
|
|
42
|
-
new DataView(out.buffer).setUint32(0, seq >>> 0, false);
|
|
43
|
-
out[4] = type;
|
|
44
|
-
out.set(bytes, 5);
|
|
45
|
-
return out;
|
|
46
|
-
}
|
|
47
|
-
export function decodeDatagram(frame) {
|
|
48
|
-
const b = frame instanceof Uint8Array ? frame : new Uint8Array(frame);
|
|
49
|
-
if (b.length < 5) return null;
|
|
50
|
-
const seq = new DataView(b.buffer, b.byteOffset, b.byteLength).getUint32(0, false);
|
|
51
|
-
return { seq: seq, value: decodePayload(b[4], b.subarray(5)) };
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** stream frame: [len:4][type:1][payload], len = 1 + payload length */
|
|
55
|
-
export function encodeStreamFrame(data) {
|
|
56
|
-
const { type, bytes } = encodePayload(data);
|
|
57
|
-
const out = new Uint8Array(4 + 1 + bytes.length);
|
|
58
|
-
new DataView(out.buffer).setUint32(0, 1 + bytes.length, false);
|
|
59
|
-
out[4] = type;
|
|
60
|
-
out.set(bytes, 5);
|
|
61
|
-
return out;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Stateful deframer for the reliable byte stream. push(bytes) → [values]. */
|
|
65
|
-
export class StreamDeframer {
|
|
66
|
-
constructor() { this._buf = new Uint8Array(0); }
|
|
67
|
-
push(chunk) {
|
|
68
|
-
const c = chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
|
69
|
-
const merged = new Uint8Array(this._buf.length + c.length);
|
|
70
|
-
merged.set(this._buf, 0); merged.set(c, this._buf.length);
|
|
71
|
-
this._buf = merged;
|
|
72
|
-
const out = [];
|
|
73
|
-
while (this._buf.length >= 4) {
|
|
74
|
-
const len = new DataView(this._buf.buffer, this._buf.byteOffset, 4).getUint32(0, false);
|
|
75
|
-
if (this._buf.length < 4 + len) break;
|
|
76
|
-
const type = this._buf[4];
|
|
77
|
-
const payload = this._buf.subarray(5, 4 + len);
|
|
78
|
-
out.push(decodePayload(type, payload.slice()));
|
|
79
|
-
this._buf = this._buf.subarray(4 + len);
|
|
80
|
-
}
|
|
81
|
-
return out;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export class WebTransportConnection {
|
|
86
|
-
/**
|
|
87
|
-
* @param {object} opts
|
|
88
|
-
* @param {string} opts.url https:// HTTP/3 endpoint
|
|
89
|
-
* @param {Array} [opts.serverCertificateHashes] for self-signed dev certs
|
|
90
|
-
* @param {boolean}[opts.sequenced=true] drop stale datagrams
|
|
91
|
-
* @param {Function}[opts.WebTransport] injectable for tests
|
|
92
|
-
*/
|
|
93
|
-
constructor(opts = {}) {
|
|
94
|
-
if (!opts.url) throw new Error('WebTransportConnection requires a url');
|
|
95
|
-
this._url = opts.url;
|
|
96
|
-
this._opts = opts;
|
|
97
|
-
this._sequenced = opts.sequenced !== false;
|
|
98
|
-
this._WT = opts.WebTransport || (typeof WebTransport !== 'undefined' ? WebTransport : null);
|
|
99
|
-
|
|
100
|
-
this.connected = false;
|
|
101
|
-
this._t = null;
|
|
102
|
-
this._dgWriter = null;
|
|
103
|
-
this._streamWriter = null;
|
|
104
|
-
this._deframer = new StreamDeframer();
|
|
105
|
-
this._sendSeq = 0;
|
|
106
|
-
this._recvSeq = 0;
|
|
107
|
-
|
|
108
|
-
this.onOpen = null; // () => void
|
|
109
|
-
this.onMessage = null; // (data, channel:'datagram'|'reliable') => void
|
|
110
|
-
this.onClose = null; // () => void
|
|
111
|
-
this.onError = null; // (err) => void
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async connect() {
|
|
115
|
-
if (!this._WT) throw new Error('WebTransport unavailable in this environment');
|
|
116
|
-
const init = {};
|
|
117
|
-
if (this._opts.serverCertificateHashes) init.serverCertificateHashes = this._opts.serverCertificateHashes;
|
|
118
|
-
const t = new this._WT(this._url, init);
|
|
119
|
-
this._t = t;
|
|
120
|
-
if (t.ready && typeof t.ready.then === 'function') await t.ready;
|
|
121
|
-
|
|
122
|
-
if (t.datagrams && t.datagrams.writable) this._dgWriter = t.datagrams.writable.getWriter();
|
|
123
|
-
if (typeof t.createBidirectionalStream === 'function') {
|
|
124
|
-
try {
|
|
125
|
-
const s = await t.createBidirectionalStream();
|
|
126
|
-
this._streamWriter = s.writable.getWriter();
|
|
127
|
-
this._pumpReadable(s.readable, 'reliable');
|
|
128
|
-
} catch (e) { /* reliable stream optional */ }
|
|
129
|
-
}
|
|
130
|
-
if (t.datagrams && t.datagrams.readable) this._pumpDatagrams(t.datagrams.readable);
|
|
131
|
-
if (t.closed && typeof t.closed.then === 'function') t.closed.then(() => this._onClosed(), () => this._onClosed());
|
|
132
|
-
|
|
133
|
-
this.connected = true;
|
|
134
|
-
if (this.onOpen) this.onOpen();
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
async _pumpDatagrams(readable) {
|
|
138
|
-
try {
|
|
139
|
-
const reader = readable.getReader();
|
|
140
|
-
for (;;) {
|
|
141
|
-
const { value, done } = await reader.read();
|
|
142
|
-
if (done) break;
|
|
143
|
-
if (value) this._onDatagramBytes(value);
|
|
144
|
-
}
|
|
145
|
-
} catch (e) { if (this.onError) this.onError(e); }
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
async _pumpReadable(readable, channel) {
|
|
149
|
-
try {
|
|
150
|
-
const reader = readable.getReader();
|
|
151
|
-
for (;;) {
|
|
152
|
-
const { value, done } = await reader.read();
|
|
153
|
-
if (done) break;
|
|
154
|
-
if (value) { const msgs = this._deframer.push(value); for (let i = 0; i < msgs.length; i++) if (this.onMessage) this.onMessage(msgs[i], channel); }
|
|
155
|
-
}
|
|
156
|
-
} catch (e) { if (this.onError) this.onError(e); }
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Handle one inbound datagram frame (also the test entry point). */
|
|
160
|
-
_onDatagramBytes(frame) {
|
|
161
|
-
const parsed = decodeDatagram(frame);
|
|
162
|
-
if (!parsed) return;
|
|
163
|
-
if (this._sequenced) {
|
|
164
|
-
if (parsed.seq <= this._recvSeq) return; // stale / out-of-order
|
|
165
|
-
this._recvSeq = parsed.seq;
|
|
166
|
-
}
|
|
167
|
-
if (this.onMessage) this.onMessage(parsed.value, 'datagram');
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/** Send over the unreliable datagram channel (sequenced). */
|
|
171
|
-
send(data) {
|
|
172
|
-
if (!this._dgWriter) return false;
|
|
173
|
-
this._sendSeq += 1;
|
|
174
|
-
try { this._dgWriter.write(encodeDatagram(this._sendSeq, data)); return true; }
|
|
175
|
-
catch (e) { return false; }
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** Send over the reliable ordered stream. */
|
|
179
|
-
sendReliable(data) {
|
|
180
|
-
if (!this._streamWriter) return false;
|
|
181
|
-
try { this._streamWriter.write(encodeStreamFrame(data)); return true; }
|
|
182
|
-
catch (e) { return false; }
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
_onClosed() {
|
|
186
|
-
if (this.connected) { this.connected = false; if (this.onClose) this.onClose(); }
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
close() {
|
|
190
|
-
try { if (this._dgWriter) this._dgWriter.releaseLock && this._dgWriter.releaseLock(); } catch (_) {}
|
|
191
|
-
try { if (this._t) this._t.close(); } catch (_) {}
|
|
192
|
-
this._t = this._dgWriter = this._streamWriter = null;
|
|
193
|
-
this.connected = false;
|
|
194
|
-
}
|
|
195
|
-
}
|
package/src/modules/notify.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Notify — let a mini-app notify its own user.
|
|
3
|
-
*
|
|
4
|
-
* A notification reaches the user even when they aren't looking at the app:
|
|
5
|
-
* - online elsewhere in Usion -> in-app banner
|
|
6
|
-
* - offline / app backgrounded -> OS push notification
|
|
7
|
-
* Tapping it re-opens THIS mini-app, optionally at a specific internal screen
|
|
8
|
-
* via `path` (read back on launch with `Usion.getLaunchParams().path`).
|
|
9
|
-
*
|
|
10
|
-
* Rides the unified backend channel, so it works standalone AND embedded.
|
|
11
|
-
* Scope & safety: a notification can only target the CURRENT user (you cannot
|
|
12
|
-
* notify other people from here), and the platform rate-limits per user per
|
|
13
|
-
* service. Users can silence a service with `setMuted(true)`.
|
|
14
|
-
*
|
|
15
|
-
* await Usion.notify.send({
|
|
16
|
-
* title: 'Render complete',
|
|
17
|
-
* body: 'Your video is ready to view.',
|
|
18
|
-
* path: '/render/abc123', // optional — deep-links inside the app
|
|
19
|
-
* });
|
|
20
|
-
*
|
|
21
|
-
* await Usion.notify.setMuted(true); // user opts out for this app
|
|
22
|
-
* const muted = await Usion.notify.isMuted();
|
|
23
|
-
*
|
|
24
|
-
* For server-triggered notifications (e.g. a long-running job finishing while
|
|
25
|
-
* the app is closed), a mini-app's own backend calls the signed REST endpoint
|
|
26
|
-
* `POST /services/{id}/notify` instead — see the publishing reference.
|
|
27
|
-
*/
|
|
28
|
-
export function createNotifyModule(Usion) {
|
|
29
|
-
function serviceId(opts) {
|
|
30
|
-
return (opts && opts.serviceId) || (Usion.config && Usion.config.serviceId);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
/**
|
|
35
|
-
* Send a notification to the current user.
|
|
36
|
-
* @param {{title: string, body: string, path?: string, serviceId?: string}} opts
|
|
37
|
-
* @returns {Promise<{success: boolean, delivered?: string}>}
|
|
38
|
-
*/
|
|
39
|
-
send: function (opts) {
|
|
40
|
-
opts = opts || {};
|
|
41
|
-
return Usion._backendEmit('notify:send', {
|
|
42
|
-
service_id: serviceId(opts),
|
|
43
|
-
title: opts.title,
|
|
44
|
-
body: opts.body,
|
|
45
|
-
path: opts.path,
|
|
46
|
-
});
|
|
47
|
-
},
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Mute (or unmute) notifications from this app for the current user.
|
|
51
|
-
* @param {boolean} muted
|
|
52
|
-
* @returns {Promise<{success: boolean, muted: boolean}>}
|
|
53
|
-
*/
|
|
54
|
-
setMuted: function (muted, opts) {
|
|
55
|
-
return Usion._backendEmit('notify:set_pref', {
|
|
56
|
-
service_id: serviceId(opts),
|
|
57
|
-
muted: !!muted,
|
|
58
|
-
});
|
|
59
|
-
},
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Whether the current user has muted notifications from this app.
|
|
63
|
-
* @returns {Promise<boolean>}
|
|
64
|
-
*/
|
|
65
|
-
isMuted: function (opts) {
|
|
66
|
-
return Usion._backendEmit('notify:get_pref', { service_id: serviceId(opts) })
|
|
67
|
-
.then(function (r) { return !!(r && r.muted); });
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
}
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Permissions — a mini-app asks the user to allow capabilities.
|
|
3
|
-
*
|
|
4
|
-
* Works exactly like asking for money: the app requests, the HOST shows a modal,
|
|
5
|
-
* the user **allows or cancels**, and the result flows back. The user can later
|
|
6
|
-
* change any grant in the Usion app's settings for this app.
|
|
7
|
-
*
|
|
8
|
-
* const res = await Usion.permissions.request(['notifications']);
|
|
9
|
-
* if (res.permissions.notifications) { ... } // granted
|
|
10
|
-
*
|
|
11
|
-
* const state = await Usion.permissions.query(['notifications']);
|
|
12
|
-
* const ok = await Usion.permissions.has('notifications');
|
|
13
|
-
*
|
|
14
|
-
* Permissions are enforced by the PLATFORM, not by this return value — e.g.
|
|
15
|
-
* `Usion.notify.send` is dropped (`delivered: 'blocked'`) until the user grants
|
|
16
|
-
* `notifications`. So the pattern is: request first, then notify.
|
|
17
|
-
*
|
|
18
|
-
* This is an EMBEDDED feature. Running standalone (outside the Usion host) there
|
|
19
|
-
* is no modal — request()/query() resolve to a benign "not granted" default and
|
|
20
|
-
* the user manages the grant inside the Usion app.
|
|
21
|
-
*/
|
|
22
|
-
import { getNextRequestId, isTrustedMessageSource } from './core.js';
|
|
23
|
-
|
|
24
|
-
export function createPermissionsModule(Usion) {
|
|
25
|
-
function normalizeList(perms) {
|
|
26
|
-
if (typeof perms === 'string') return perms ? [perms] : [];
|
|
27
|
-
if (Array.isArray(perms)) {
|
|
28
|
-
return perms.filter(function (p) { return typeof p === 'string' && p; });
|
|
29
|
-
}
|
|
30
|
-
return [];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function isEmbedded() {
|
|
34
|
-
return typeof window !== 'undefined' &&
|
|
35
|
-
(!!window.ReactNativeWebView || (!!window.parent && window.parent !== window));
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function denyAll(list) {
|
|
39
|
-
const out = {};
|
|
40
|
-
list.forEach(function (p) { out[p] = false; });
|
|
41
|
-
return out;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function summarize(list, map) {
|
|
45
|
-
const perms = map || {};
|
|
46
|
-
const granted = list.length > 0 && list.every(function (p) { return !!perms[p]; });
|
|
47
|
-
return { granted: granted, permissions: perms };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const api = {
|
|
51
|
-
// How long to wait for the host's PERMISSION_RESULT before falling back to a
|
|
52
|
-
// state query — the user may sit on the modal. We NEVER reject: a dropped
|
|
53
|
-
// result must not strand the caller (mirrors wallet.requestPayment recovery).
|
|
54
|
-
// Overridable, mainly for tests.
|
|
55
|
-
_requestTimeoutMs: 120000,
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Ask the user to allow one or more permissions. Shows the host modal
|
|
59
|
-
* (unless every requested permission is already granted, in which case the
|
|
60
|
-
* host resolves immediately).
|
|
61
|
-
*
|
|
62
|
-
* @param {string|string[]} permissions e.g. 'notifications' or ['notifications']
|
|
63
|
-
* @param {{reason?: string}} [opts] optional one-line context shown in the modal
|
|
64
|
-
* @returns {Promise<{granted: boolean, permissions: Object}>}
|
|
65
|
-
* `granted` is true only if EVERY requested permission ended granted;
|
|
66
|
-
* `permissions` maps each requested key to its resulting boolean state.
|
|
67
|
-
*/
|
|
68
|
-
request: function (permissions, opts) {
|
|
69
|
-
const list = normalizeList(permissions);
|
|
70
|
-
const reason = (opts && typeof opts.reason === 'string') ? opts.reason : undefined;
|
|
71
|
-
|
|
72
|
-
if (!isEmbedded()) {
|
|
73
|
-
try { console.warn('[Usion] permissions.request needs the Usion app; resolving as not granted.'); } catch (e) { /* noop */ }
|
|
74
|
-
return Promise.resolve(summarize(list, denyAll(list)));
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return new Promise(function (resolve) {
|
|
78
|
-
const requestId = getNextRequestId();
|
|
79
|
-
let settled = false;
|
|
80
|
-
let timer = null;
|
|
81
|
-
|
|
82
|
-
function cleanup() {
|
|
83
|
-
if (timer) clearTimeout(timer);
|
|
84
|
-
window.removeEventListener('message', handler);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function finish(result) {
|
|
88
|
-
if (settled) return;
|
|
89
|
-
settled = true;
|
|
90
|
-
cleanup();
|
|
91
|
-
resolve(result);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function handler(event) {
|
|
95
|
-
// Only honor results from the trusted host shell. (A forged result can
|
|
96
|
-
// only mislead this app's UI — the backend gate is the real guard.)
|
|
97
|
-
if (!isTrustedMessageSource(event)) return;
|
|
98
|
-
let response;
|
|
99
|
-
try {
|
|
100
|
-
response = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
|
|
101
|
-
} catch (e) { return; }
|
|
102
|
-
if (!response || response._requestId !== requestId) return;
|
|
103
|
-
if (response.type === 'PERMISSION_RESULT') {
|
|
104
|
-
finish(summarize(list, response.permissions));
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
window.addEventListener('message', handler);
|
|
109
|
-
|
|
110
|
-
// Host never answered (lost message / older host shell): fall back to the
|
|
111
|
-
// current persisted state so we still resolve with the truth.
|
|
112
|
-
timer = setTimeout(function () {
|
|
113
|
-
if (settled) return;
|
|
114
|
-
api.query(list).then(function (map) {
|
|
115
|
-
finish(summarize(list, map));
|
|
116
|
-
}).catch(function () {
|
|
117
|
-
finish(summarize(list, denyAll(list)));
|
|
118
|
-
});
|
|
119
|
-
}, api._requestTimeoutMs || 120000);
|
|
120
|
-
|
|
121
|
-
Usion._post({
|
|
122
|
-
type: 'PERMISSION_REQUEST',
|
|
123
|
-
_requestId: requestId,
|
|
124
|
-
permissions: list,
|
|
125
|
-
reason: reason,
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Read current permission state WITHOUT prompting the user.
|
|
132
|
-
* @param {string|string[]} [permissions]
|
|
133
|
-
* @returns {Promise<Object>} map of permission key -> boolean (granted)
|
|
134
|
-
*/
|
|
135
|
-
query: function (permissions) {
|
|
136
|
-
const list = normalizeList(permissions);
|
|
137
|
-
if (!isEmbedded()) {
|
|
138
|
-
return Promise.resolve(denyAll(list));
|
|
139
|
-
}
|
|
140
|
-
return Usion._request('PERMISSION_QUERY', { permissions: list }, 8000)
|
|
141
|
-
.then(function (res) { return (res && res.permissions) || {}; });
|
|
142
|
-
},
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Convenience: is a single permission currently granted?
|
|
146
|
-
* @param {string} permission
|
|
147
|
-
* @returns {Promise<boolean>}
|
|
148
|
-
*/
|
|
149
|
-
has: function (permission) {
|
|
150
|
-
return api.query([permission]).then(function (map) {
|
|
151
|
-
return !!(map && map[permission]);
|
|
152
|
-
});
|
|
153
|
-
},
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
return api;
|
|
157
|
-
}
|
package/src/modules/results.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Results — server-side result persistence across devices
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export function createResultsMethods(Usion) {
|
|
6
|
-
return {
|
|
7
|
-
/**
|
|
8
|
-
* Save a result to server-side storage (persists across devices).
|
|
9
|
-
* @param {string} data - Result string (URL, JSON, etc.)
|
|
10
|
-
* @param {object} [metadata] - Optional metadata (thumbnail_url, title, type)
|
|
11
|
-
* @returns {Promise<object>} The saved result document
|
|
12
|
-
*/
|
|
13
|
-
saveResult: function(data, metadata) {
|
|
14
|
-
return Usion._request('SAVE_RESULT', { data: data, metadata: metadata || {} }, 15000);
|
|
15
|
-
},
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Delete a saved result by ID.
|
|
19
|
-
* @param {string} resultId - The result ID to delete
|
|
20
|
-
* @returns {Promise<void>}
|
|
21
|
-
*/
|
|
22
|
-
deleteResult: function(resultId) {
|
|
23
|
-
return Usion._request('DELETE_RESULT', { resultId: resultId });
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Get all saved results for this service (populated from INIT config).
|
|
28
|
-
* @returns {Array} Array of result objects
|
|
29
|
-
*/
|
|
30
|
-
getResults: function() {
|
|
31
|
-
return Usion._results || [];
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
package/src/modules/session.js
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Session Module — ephemeral session data management
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param {object} Usion - Reference to the main Usion object
|
|
7
|
-
*/
|
|
8
|
-
export function createSessionModule(Usion) {
|
|
9
|
-
return {
|
|
10
|
-
_id: null,
|
|
11
|
-
_data: {},
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Get the current session ID
|
|
15
|
-
* @returns {string|null}
|
|
16
|
-
*/
|
|
17
|
-
getId: function() {
|
|
18
|
-
return this._id || Usion.config.sessionId || null;
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Get session data
|
|
23
|
-
* @param {string} key - Optional key to get specific value
|
|
24
|
-
* @returns {any} Session data or specific value
|
|
25
|
-
*/
|
|
26
|
-
getData: function(key) {
|
|
27
|
-
if (key) {
|
|
28
|
-
return this._data[key];
|
|
29
|
-
}
|
|
30
|
-
return this._data;
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Set session data (ephemeral, cleared on session end)
|
|
35
|
-
* @param {string|object} keyOrData - Key or object of data to set
|
|
36
|
-
* @param {any} value - Value if key is string
|
|
37
|
-
*/
|
|
38
|
-
setData: function(keyOrData, value) {
|
|
39
|
-
if (typeof keyOrData === 'object') {
|
|
40
|
-
Object.assign(this._data, keyOrData);
|
|
41
|
-
} else {
|
|
42
|
-
this._data[keyOrData] = value;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Notify parent of session data change
|
|
46
|
-
Usion._post({
|
|
47
|
-
type: 'SESSION_DATA_UPDATE',
|
|
48
|
-
data: this._data
|
|
49
|
-
});
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Clear session data
|
|
54
|
-
*/
|
|
55
|
-
clear: function() {
|
|
56
|
-
this._data = {};
|
|
57
|
-
Usion._post({
|
|
58
|
-
type: 'SESSION_DATA_CLEAR'
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
}
|
package/src/modules/storage.js
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK Storage Module — persistent storage (per-user, per-service)
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @param {object} Usion - Reference to the main Usion object
|
|
7
|
-
*/
|
|
8
|
-
export function createStorageModule(Usion) {
|
|
9
|
-
return {
|
|
10
|
-
/**
|
|
11
|
-
* Get a stored value
|
|
12
|
-
* @param {string} key - Storage key
|
|
13
|
-
* @returns {Promise<any>} Stored value or null
|
|
14
|
-
*/
|
|
15
|
-
get: function(key) {
|
|
16
|
-
return Usion._request('STORAGE_GET', { key: key }).then(function(response) {
|
|
17
|
-
return response.value;
|
|
18
|
-
});
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Set a stored value
|
|
23
|
-
* @param {string} key - Storage key
|
|
24
|
-
* @param {any} value - Value to store (will be JSON serialized)
|
|
25
|
-
* @returns {Promise<void>}
|
|
26
|
-
*/
|
|
27
|
-
set: function(key, value) {
|
|
28
|
-
return Usion._request('STORAGE_SET', { key: key, value: value }).then(function() {
|
|
29
|
-
return;
|
|
30
|
-
});
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Remove a stored value
|
|
35
|
-
* @param {string} key - Storage key
|
|
36
|
-
* @returns {Promise<void>}
|
|
37
|
-
*/
|
|
38
|
-
remove: function(key) {
|
|
39
|
-
return Usion._request('STORAGE_REMOVE', { key: key }).then(function() {
|
|
40
|
-
return;
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Clear all stored values for this service
|
|
46
|
-
* @returns {Promise<void>}
|
|
47
|
-
*/
|
|
48
|
-
clear: function() {
|
|
49
|
-
return Usion._request('STORAGE_CLEAR', {}).then(function() {
|
|
50
|
-
return;
|
|
51
|
-
});
|
|
52
|
-
},
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Get all keys
|
|
56
|
-
* @returns {Promise<string[]>}
|
|
57
|
-
*/
|
|
58
|
-
keys: function() {
|
|
59
|
-
return Usion._request('STORAGE_KEYS', {}).then(function(response) {
|
|
60
|
-
return response.keys || [];
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
}
|
package/src/modules/ui.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Usion SDK UI Utilities — DOM helpers for mini apps
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
export const uiMethods = {
|
|
6
|
-
/**
|
|
7
|
-
* Set button to loading state
|
|
8
|
-
* @param {HTMLElement|string} btn - Button element or selector
|
|
9
|
-
* @param {boolean} loading - Whether to show loading state
|
|
10
|
-
*/
|
|
11
|
-
setLoading: function(btn, loading) {
|
|
12
|
-
const el = typeof btn === 'string' ? document.querySelector(btn) : btn;
|
|
13
|
-
if (!el) return;
|
|
14
|
-
|
|
15
|
-
if (loading) {
|
|
16
|
-
el.classList.add('usion-btn-loading');
|
|
17
|
-
el.disabled = true;
|
|
18
|
-
el.dataset.originalText = el.textContent;
|
|
19
|
-
} else {
|
|
20
|
-
el.classList.remove('usion-btn-loading');
|
|
21
|
-
el.disabled = false;
|
|
22
|
-
if (el.dataset.originalText) {
|
|
23
|
-
el.textContent = el.dataset.originalText;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Show/hide an element
|
|
30
|
-
* @param {HTMLElement|string} el - Element or selector
|
|
31
|
-
* @param {boolean} show - Whether to show or hide
|
|
32
|
-
*/
|
|
33
|
-
toggle: function(el, show) {
|
|
34
|
-
const element = typeof el === 'string' ? document.querySelector(el) : el;
|
|
35
|
-
if (!element) return;
|
|
36
|
-
|
|
37
|
-
if (show) {
|
|
38
|
-
element.classList.remove('usion-hidden', 'hidden');
|
|
39
|
-
element.classList.add('usion-visible');
|
|
40
|
-
} else {
|
|
41
|
-
element.classList.add('usion-hidden');
|
|
42
|
-
element.classList.remove('usion-visible');
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Update character count display
|
|
48
|
-
* @param {HTMLElement|string} input - Input element or selector
|
|
49
|
-
* @param {HTMLElement|string} counter - Counter element or selector
|
|
50
|
-
* @param {number} max - Maximum characters
|
|
51
|
-
*/
|
|
52
|
-
charCount: function(input, counter, max) {
|
|
53
|
-
const inputEl = typeof input === 'string' ? document.querySelector(input) : input;
|
|
54
|
-
const counterEl = typeof counter === 'string' ? document.querySelector(counter) : counter;
|
|
55
|
-
|
|
56
|
-
if (!inputEl || !counterEl) return;
|
|
57
|
-
|
|
58
|
-
function update() {
|
|
59
|
-
const count = inputEl.value.length;
|
|
60
|
-
counterEl.textContent = count + ' / ' + max;
|
|
61
|
-
|
|
62
|
-
counterEl.classList.remove('warning', 'error');
|
|
63
|
-
if (count > max * 0.9) {
|
|
64
|
-
counterEl.classList.add('error');
|
|
65
|
-
} else if (count > max * 0.7) {
|
|
66
|
-
counterEl.classList.add('warning');
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
inputEl.addEventListener('input', update);
|
|
71
|
-
update();
|
|
72
|
-
},
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Create a selection handler for grid items
|
|
76
|
-
* @param {string} containerSelector - Container selector
|
|
77
|
-
* @param {string} itemSelector - Item selector
|
|
78
|
-
* @param {function} onChange - Callback when selection changes
|
|
79
|
-
*/
|
|
80
|
-
selectionGrid: function(containerSelector, itemSelector, onChange) {
|
|
81
|
-
const container = document.querySelector(containerSelector);
|
|
82
|
-
if (!container) return;
|
|
83
|
-
|
|
84
|
-
let selected = null;
|
|
85
|
-
|
|
86
|
-
container.querySelectorAll(itemSelector).forEach(function(item) {
|
|
87
|
-
item.addEventListener('click', function() {
|
|
88
|
-
// Remove selection from all
|
|
89
|
-
container.querySelectorAll(itemSelector).forEach(function(i) {
|
|
90
|
-
i.classList.remove('selected');
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
// Select this one
|
|
94
|
-
item.classList.add('selected');
|
|
95
|
-
selected = item.dataset.value || item.dataset.id;
|
|
96
|
-
|
|
97
|
-
if (onChange) onChange(selected, item);
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
return {
|
|
102
|
-
getSelected: function() { return selected; },
|
|
103
|
-
clear: function() {
|
|
104
|
-
container.querySelectorAll(itemSelector).forEach(function(i) {
|
|
105
|
-
i.classList.remove('selected');
|
|
106
|
-
});
|
|
107
|
-
selected = null;
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
};
|