@playcademy/sdk 0.0.1-beta.31 → 0.0.1-beta.32
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/dist/index.js +105 -25
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -10,6 +10,87 @@ var __export = (target, all) => {
|
|
|
10
10
|
};
|
|
11
11
|
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
12
12
|
|
|
13
|
+
// ../logger/src/index.ts
|
|
14
|
+
var isBrowser = () => {
|
|
15
|
+
const g = globalThis;
|
|
16
|
+
return typeof g.window !== "undefined" && typeof g.document !== "undefined";
|
|
17
|
+
}, colors, getLevelColor = (level) => {
|
|
18
|
+
switch (level) {
|
|
19
|
+
case "debug":
|
|
20
|
+
return colors.blue;
|
|
21
|
+
case "info":
|
|
22
|
+
return colors.cyan;
|
|
23
|
+
case "warn":
|
|
24
|
+
return colors.yellow;
|
|
25
|
+
case "error":
|
|
26
|
+
return colors.red;
|
|
27
|
+
default:
|
|
28
|
+
return colors.reset;
|
|
29
|
+
}
|
|
30
|
+
}, logInBrowser = (level, message, context) => {
|
|
31
|
+
const timestamp = new Date().toISOString();
|
|
32
|
+
const levelUpper = level.toUpperCase();
|
|
33
|
+
const consoleMethod = getConsoleMethod(level);
|
|
34
|
+
if (context && Object.keys(context).length > 0) {
|
|
35
|
+
consoleMethod(`[${timestamp}] ${levelUpper}`, message, context);
|
|
36
|
+
} else {
|
|
37
|
+
consoleMethod(`[${timestamp}] ${levelUpper}`, message);
|
|
38
|
+
}
|
|
39
|
+
}, logOnServer = (level, message, context) => {
|
|
40
|
+
const consoleMethod = getConsoleMethod(level);
|
|
41
|
+
if (true) {
|
|
42
|
+
const timestamp = new Date().toISOString();
|
|
43
|
+
const levelColor = getLevelColor(level);
|
|
44
|
+
const levelUpper = level.toUpperCase().padEnd(5);
|
|
45
|
+
const coloredPrefix = `${colors.dim}[${timestamp}]${colors.reset} ${levelColor}${levelUpper}${colors.reset}`;
|
|
46
|
+
if (context && Object.keys(context).length > 0) {
|
|
47
|
+
consoleMethod(`${coloredPrefix} ${message}`, context);
|
|
48
|
+
} else {
|
|
49
|
+
consoleMethod(`${coloredPrefix} ${message}`);
|
|
50
|
+
}
|
|
51
|
+
} else {}
|
|
52
|
+
}, getConsoleMethod = (level) => {
|
|
53
|
+
switch (level) {
|
|
54
|
+
case "debug":
|
|
55
|
+
return console.debug;
|
|
56
|
+
case "info":
|
|
57
|
+
return console.info;
|
|
58
|
+
case "warn":
|
|
59
|
+
return console.warn;
|
|
60
|
+
case "error":
|
|
61
|
+
return console.error;
|
|
62
|
+
default:
|
|
63
|
+
return console.log;
|
|
64
|
+
}
|
|
65
|
+
}, performLog = (level, message, context) => {
|
|
66
|
+
if (level === "debug" && false) {}
|
|
67
|
+
if (isBrowser()) {
|
|
68
|
+
logInBrowser(level, message, context);
|
|
69
|
+
} else {
|
|
70
|
+
logOnServer(level, message, context);
|
|
71
|
+
}
|
|
72
|
+
}, createLogger = () => {
|
|
73
|
+
return {
|
|
74
|
+
debug: (message, context) => performLog("debug", message, context),
|
|
75
|
+
info: (message, context) => performLog("info", message, context),
|
|
76
|
+
warn: (message, context) => performLog("warn", message, context),
|
|
77
|
+
error: (message, context) => performLog("error", message, context),
|
|
78
|
+
log: performLog
|
|
79
|
+
};
|
|
80
|
+
}, log;
|
|
81
|
+
var init_src = __esm(() => {
|
|
82
|
+
colors = {
|
|
83
|
+
reset: "\x1B[0m",
|
|
84
|
+
dim: "\x1B[2m",
|
|
85
|
+
red: "\x1B[31m",
|
|
86
|
+
yellow: "\x1B[33m",
|
|
87
|
+
blue: "\x1B[34m",
|
|
88
|
+
cyan: "\x1B[36m",
|
|
89
|
+
gray: "\x1B[90m"
|
|
90
|
+
};
|
|
91
|
+
log = createLogger();
|
|
92
|
+
});
|
|
93
|
+
|
|
13
94
|
// src/core/errors.ts
|
|
14
95
|
var PlaycademyError, ApiError;
|
|
15
96
|
var init_errors = __esm(() => {
|
|
@@ -134,7 +215,6 @@ var init_messaging = __esm(() => {
|
|
|
134
215
|
});
|
|
135
216
|
|
|
136
217
|
// src/core/namespaces/runtime.ts
|
|
137
|
-
import { log } from "@playcademy/logger";
|
|
138
218
|
function createRuntimeNamespace(client) {
|
|
139
219
|
const eventListeners = new Map;
|
|
140
220
|
const trackListener = (eventType, handler) => {
|
|
@@ -245,11 +325,11 @@ function createRuntimeNamespace(client) {
|
|
|
245
325
|
};
|
|
246
326
|
}
|
|
247
327
|
var init_runtime = __esm(() => {
|
|
328
|
+
init_src();
|
|
248
329
|
init_messaging();
|
|
249
330
|
});
|
|
250
331
|
|
|
251
332
|
// src/core/request.ts
|
|
252
|
-
import { log as log2 } from "@playcademy/logger";
|
|
253
333
|
async function request({
|
|
254
334
|
path,
|
|
255
335
|
baseUrl,
|
|
@@ -295,7 +375,7 @@ async function fetchManifest(assetBundleBase) {
|
|
|
295
375
|
try {
|
|
296
376
|
const response = await fetch(manifestUrl);
|
|
297
377
|
if (!response.ok) {
|
|
298
|
-
|
|
378
|
+
log.error(`[fetchManifest] Failed to fetch manifest from ${manifestUrl}. Status: ${response.status}`);
|
|
299
379
|
throw new PlaycademyError(`Failed to fetch manifest: ${response.status} ${response.statusText}`);
|
|
300
380
|
}
|
|
301
381
|
return await response.json();
|
|
@@ -303,13 +383,14 @@ async function fetchManifest(assetBundleBase) {
|
|
|
303
383
|
if (error instanceof PlaycademyError) {
|
|
304
384
|
throw error;
|
|
305
385
|
}
|
|
306
|
-
|
|
386
|
+
log.error(`[Playcademy SDK] Error fetching or parsing manifest from ${manifestUrl}:`, {
|
|
307
387
|
error
|
|
308
388
|
});
|
|
309
389
|
throw new PlaycademyError("Failed to load or parse game manifest");
|
|
310
390
|
}
|
|
311
391
|
}
|
|
312
392
|
var init_request = __esm(() => {
|
|
393
|
+
init_src();
|
|
313
394
|
init_errors();
|
|
314
395
|
});
|
|
315
396
|
|
|
@@ -1400,8 +1481,6 @@ function createSpritesNamespace(client) {
|
|
|
1400
1481
|
}
|
|
1401
1482
|
|
|
1402
1483
|
// src/core/namespaces/realtime.client.ts
|
|
1403
|
-
import { log as log3 } from "@playcademy/logger";
|
|
1404
|
-
|
|
1405
1484
|
class RealtimeChannelClient {
|
|
1406
1485
|
gameId;
|
|
1407
1486
|
_channelName;
|
|
@@ -1443,13 +1522,13 @@ class RealtimeChannelClient {
|
|
|
1443
1522
|
this.ws.addEventListener("open", onOpen);
|
|
1444
1523
|
this.ws.addEventListener("error", onError);
|
|
1445
1524
|
});
|
|
1446
|
-
|
|
1525
|
+
log.debug("[RealtimeChannelClient] Connected to channel", {
|
|
1447
1526
|
gameId: this.gameId,
|
|
1448
1527
|
channel: this._channelName
|
|
1449
1528
|
});
|
|
1450
1529
|
return this;
|
|
1451
1530
|
} catch (error) {
|
|
1452
|
-
|
|
1531
|
+
log.error("[RealtimeChannelClient] Connection failed", {
|
|
1453
1532
|
gameId: this.gameId,
|
|
1454
1533
|
channel: this._channelName,
|
|
1455
1534
|
error
|
|
@@ -1467,14 +1546,14 @@ class RealtimeChannelClient {
|
|
|
1467
1546
|
try {
|
|
1468
1547
|
callback(data);
|
|
1469
1548
|
} catch (error) {
|
|
1470
|
-
|
|
1549
|
+
log.warn("[RealtimeChannelClient] Message listener error", {
|
|
1471
1550
|
channel: this._channelName,
|
|
1472
1551
|
error
|
|
1473
1552
|
});
|
|
1474
1553
|
}
|
|
1475
1554
|
});
|
|
1476
1555
|
} catch (error) {
|
|
1477
|
-
|
|
1556
|
+
log.warn("[RealtimeChannelClient] Failed to parse message", {
|
|
1478
1557
|
channel: this._channelName,
|
|
1479
1558
|
message: event.data,
|
|
1480
1559
|
error
|
|
@@ -1482,14 +1561,14 @@ class RealtimeChannelClient {
|
|
|
1482
1561
|
}
|
|
1483
1562
|
};
|
|
1484
1563
|
this.ws.onclose = (event) => {
|
|
1485
|
-
|
|
1564
|
+
log.debug("[RealtimeChannelClient] Connection closed", {
|
|
1486
1565
|
channel: this._channelName,
|
|
1487
1566
|
code: event.code,
|
|
1488
1567
|
reason: event.reason,
|
|
1489
1568
|
wasClean: event.wasClean
|
|
1490
1569
|
});
|
|
1491
1570
|
if (!this.isClosing && event.code !== CLOSE_CODES.TOKEN_REFRESH) {
|
|
1492
|
-
|
|
1571
|
+
log.warn("[RealtimeChannelClient] Unexpected disconnection", {
|
|
1493
1572
|
channel: this._channelName,
|
|
1494
1573
|
code: event.code,
|
|
1495
1574
|
reason: event.reason
|
|
@@ -1497,7 +1576,7 @@ class RealtimeChannelClient {
|
|
|
1497
1576
|
}
|
|
1498
1577
|
};
|
|
1499
1578
|
this.ws.onerror = (event) => {
|
|
1500
|
-
|
|
1579
|
+
log.error("[RealtimeChannelClient] WebSocket error", {
|
|
1501
1580
|
channel: this._channelName,
|
|
1502
1581
|
event
|
|
1503
1582
|
});
|
|
@@ -1505,13 +1584,13 @@ class RealtimeChannelClient {
|
|
|
1505
1584
|
}
|
|
1506
1585
|
setupTokenRefreshListener() {
|
|
1507
1586
|
const tokenRefreshHandler = async ({ token }) => {
|
|
1508
|
-
|
|
1587
|
+
log.debug("[RealtimeChannelClient] Token refresh received, reconnecting", {
|
|
1509
1588
|
channel: this._channelName
|
|
1510
1589
|
});
|
|
1511
1590
|
try {
|
|
1512
1591
|
await this.reconnectWithNewToken(token);
|
|
1513
1592
|
} catch (error) {
|
|
1514
|
-
|
|
1593
|
+
log.error("[RealtimeChannelClient] Token refresh reconnection failed", {
|
|
1515
1594
|
channel: this._channelName,
|
|
1516
1595
|
error
|
|
1517
1596
|
});
|
|
@@ -1544,14 +1623,14 @@ class RealtimeChannelClient {
|
|
|
1544
1623
|
const message = JSON.stringify(data);
|
|
1545
1624
|
this.ws.send(message);
|
|
1546
1625
|
} catch (error) {
|
|
1547
|
-
|
|
1626
|
+
log.error("[RealtimeChannelClient] Failed to send message", {
|
|
1548
1627
|
channel: this._channelName,
|
|
1549
1628
|
error,
|
|
1550
1629
|
data
|
|
1551
1630
|
});
|
|
1552
1631
|
}
|
|
1553
1632
|
} else {
|
|
1554
|
-
|
|
1633
|
+
log.warn("[RealtimeChannelClient] Cannot send message - connection not open", {
|
|
1555
1634
|
channel: this._channelName,
|
|
1556
1635
|
readyState: this.ws?.readyState
|
|
1557
1636
|
});
|
|
@@ -1572,7 +1651,7 @@ class RealtimeChannelClient {
|
|
|
1572
1651
|
this.ws = undefined;
|
|
1573
1652
|
}
|
|
1574
1653
|
this.listeners.clear();
|
|
1575
|
-
|
|
1654
|
+
log.debug("[RealtimeChannelClient] Channel closed", {
|
|
1576
1655
|
channel: this._channelName
|
|
1577
1656
|
});
|
|
1578
1657
|
}
|
|
@@ -1585,6 +1664,7 @@ class RealtimeChannelClient {
|
|
|
1585
1664
|
}
|
|
1586
1665
|
var CLOSE_CODES;
|
|
1587
1666
|
var init_realtime_client = __esm(() => {
|
|
1667
|
+
init_src();
|
|
1588
1668
|
init_messaging();
|
|
1589
1669
|
CLOSE_CODES = {
|
|
1590
1670
|
NORMAL_CLOSURE: 1000,
|
|
@@ -1687,7 +1767,6 @@ var init_init = __esm(() => {
|
|
|
1687
1767
|
});
|
|
1688
1768
|
|
|
1689
1769
|
// src/core/static/login.ts
|
|
1690
|
-
import { log as log4 } from "@playcademy/logger";
|
|
1691
1770
|
async function login(baseUrl, email, password) {
|
|
1692
1771
|
let url = baseUrl;
|
|
1693
1772
|
if (baseUrl.startsWith("/") && typeof window !== "undefined") {
|
|
@@ -1707,13 +1786,14 @@ async function login(baseUrl, email, password) {
|
|
|
1707
1786
|
const errorMessage = errorData && errorData.message ? String(errorData.message) : response.statusText;
|
|
1708
1787
|
throw new PlaycademyError(errorMessage);
|
|
1709
1788
|
} catch (error) {
|
|
1710
|
-
|
|
1789
|
+
log.error("[Playcademy SDK] Failed to parse error response JSON, using status text instead:", { error });
|
|
1711
1790
|
throw new PlaycademyError(response.statusText);
|
|
1712
1791
|
}
|
|
1713
1792
|
}
|
|
1714
1793
|
return response.json();
|
|
1715
1794
|
}
|
|
1716
1795
|
var init_login = __esm(() => {
|
|
1796
|
+
init_src();
|
|
1717
1797
|
init_errors();
|
|
1718
1798
|
});
|
|
1719
1799
|
|
|
@@ -1728,9 +1808,9 @@ var exports_client = {};
|
|
|
1728
1808
|
__export(exports_client, {
|
|
1729
1809
|
PlaycademyClient: () => PlaycademyClient
|
|
1730
1810
|
});
|
|
1731
|
-
import { log as log5 } from "@playcademy/logger";
|
|
1732
1811
|
var PlaycademyClient;
|
|
1733
1812
|
var init_client = __esm(() => {
|
|
1813
|
+
init_src();
|
|
1734
1814
|
init_errors();
|
|
1735
1815
|
init_namespaces();
|
|
1736
1816
|
init_request();
|
|
@@ -1753,7 +1833,7 @@ var init_client = __esm(() => {
|
|
|
1753
1833
|
}
|
|
1754
1834
|
if (this.gameId) {
|
|
1755
1835
|
this._initializeInternalSession().catch((error) => {
|
|
1756
|
-
|
|
1836
|
+
log.error("[Playcademy SDK] Background initialization of auto-session failed:", {
|
|
1757
1837
|
error
|
|
1758
1838
|
});
|
|
1759
1839
|
});
|
|
@@ -1761,8 +1841,8 @@ var init_client = __esm(() => {
|
|
|
1761
1841
|
}
|
|
1762
1842
|
getBaseUrl() {
|
|
1763
1843
|
const isRelative = this.baseUrl.startsWith("/");
|
|
1764
|
-
const
|
|
1765
|
-
return isRelative &&
|
|
1844
|
+
const isBrowser2 = typeof window !== "undefined";
|
|
1845
|
+
return isRelative && isBrowser2 ? `${window.location.origin}${this.baseUrl}` : this.baseUrl;
|
|
1766
1846
|
}
|
|
1767
1847
|
ping() {
|
|
1768
1848
|
return "pong";
|
|
@@ -1808,7 +1888,7 @@ var init_client = __esm(() => {
|
|
|
1808
1888
|
const response = await this.games.startSession(this.gameId);
|
|
1809
1889
|
this.internalClientSessionId = response.sessionId;
|
|
1810
1890
|
} catch (error) {
|
|
1811
|
-
|
|
1891
|
+
log.error("[Playcademy SDK] Auto-starting session failed for game", {
|
|
1812
1892
|
gameId: this.gameId,
|
|
1813
1893
|
error
|
|
1814
1894
|
});
|