@kalamba/sdk 0.35.0 → 0.37.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/dist/index.js CHANGED
@@ -2,76 +2,106 @@
2
2
  * BSD 3-Clause License
3
3
  * Copyright (c) 2026, Kalamba Games Limited
4
4
  */
5
- import { T as n, R as a } from "./errors-CyJnvnOm.js";
6
- import { a as g } from "./errors-CyJnvnOm.js";
7
- import { I as p } from "./encoders-BdN6uQHH.js";
8
- import { d as y, e as E } from "./encoders-BdN6uQHH.js";
9
- const d = () => {
5
+ import { i as TimeoutError, r as RgsErrorCode, t as RgsError } from "./errors-CakYyMwc.js";
6
+ import { t as I18n } from "./i18n-vzEFUHe0.js";
7
+
8
+ //#region src/sdk/KalambaSdk.ts
9
+ const logIn = () => {};
10
+ const logOut = () => {};
11
+ const passThroughCodec = {
12
+ encode: (value) => value,
13
+ decode: (value) => value
10
14
  };
11
- class w {
12
- #e;
13
- #r;
14
- #s;
15
- constructor({ messagePort: e }) {
16
- this.#s = e, this.on("configured", (r) => {
17
- this.#e = r, this.#r = new p({ config: r });
18
- });
19
- }
20
- get config() {
21
- if (!this.#e)
22
- throw new Error("config is not initialized, you must open game first");
23
- return this.#e;
24
- }
25
- get i18n() {
26
- if (!this.#r)
27
- throw new Error("i18n is not initialized, you must open game first");
28
- return this.#r;
29
- }
30
- request(e, ...[r]) {
31
- return new Promise((o) => {
32
- this.on(`${e}Response`, (s) => o({ type: "response", response: s }), {
33
- once: !0
34
- }), this.on(`${e}Error`, (s) => o({ type: "error", error: s }), { once: !0 }), this.send(e, r);
35
- });
36
- }
37
- on(e, r, o) {
38
- const s = function i(t) {
39
- t.data.message === `kalamba:wrapper:${e}` && (d("on", t.data.message.replace(/^kalamba:/, ""), t.data.payload), r(t.data.payload), o?.once && window.removeEventListener("message", i));
40
- };
41
- window.addEventListener("message", s);
42
- }
43
- // TODO error messages
44
- send(e, ...[r]) {
45
- this.#s.postMessage(
46
- {
47
- message: `kalamba:sdk:${e}`,
48
- payload: r
49
- },
50
- "*"
51
- );
52
- }
53
- configure(e) {
54
- this.send("configure", e);
55
- }
56
- async openGame() {
57
- const e = new Promise((o) => this.on("configured", o, { once: !0 })), r = await this.request("openGame");
58
- if (r.type === "response")
59
- return await e, r.response;
60
- throw r.error.type === "timeout" ? new n() : new a(r.error.data);
61
- }
62
- async play(e, r) {
63
- const o = { contract: e, extra: r }, s = await this.request("play", o);
64
- if (s.type === "response")
65
- return s.response;
66
- throw s.error.type === "timeout" ? new n() : new a(s.error.data);
67
- }
68
- }
69
- export {
70
- p as I18n,
71
- w as KalambaSdk,
72
- a as RgsError,
73
- g as RgsErrorCode,
74
- n as TimeoutError,
75
- y as decoders,
76
- E as encoders
15
+ const defaultCodecs = {
16
+ openGame: {
17
+ request: passThroughCodec,
18
+ response: passThroughCodec
19
+ },
20
+ play: {
21
+ request: passThroughCodec,
22
+ response: passThroughCodec
23
+ }
77
24
  };
25
+ var KalambaSdk = class {
26
+ #config;
27
+ #i18n;
28
+ #messagePort;
29
+ #codecs;
30
+ constructor({ messagePort, codecs }) {
31
+ this.#messagePort = messagePort;
32
+ this.#codecs = codecs ?? defaultCodecs;
33
+ this.on("configured", (config) => {
34
+ this.#config = config;
35
+ this.#i18n = new I18n({ config });
36
+ });
37
+ }
38
+ get config() {
39
+ if (!this.#config) throw new Error("config is not initialized, you must open game first");
40
+ return this.#config;
41
+ }
42
+ get i18n() {
43
+ if (!this.#i18n) throw new Error("i18n is not initialized, you must open game first");
44
+ return this.#i18n;
45
+ }
46
+ request(message, ...[payload]) {
47
+ return new Promise((resolve) => {
48
+ this.on(`${message}Response`, (response) => resolve({
49
+ type: "response",
50
+ response
51
+ }), { once: true });
52
+ this.on(`${message}Error`, (error) => resolve({
53
+ type: "error",
54
+ error
55
+ }), { once: true });
56
+ this.send(message, payload);
57
+ });
58
+ }
59
+ on(message, listener, options) {
60
+ window.addEventListener("message", function onMessage(event) {
61
+ if (event.data.message !== `kalamba:wrapper:${message}`) return;
62
+ logIn("on", event.data.message.replace(/^kalamba:/, ""), event.data.payload);
63
+ listener(event.data.payload);
64
+ if (options?.once) window.removeEventListener("message", onMessage);
65
+ });
66
+ }
67
+ send(message, ...[payload]) {
68
+ logOut("send", message, payload);
69
+ this.#messagePort.postMessage({
70
+ message: `kalamba:sdk:${message}`,
71
+ payload
72
+ }, "*");
73
+ }
74
+ configure(payload) {
75
+ this.send("configure", payload);
76
+ }
77
+ async openGame() {
78
+ const configuredPromise = new Promise((resolve) => this.on("configured", resolve, { once: true }));
79
+ const responseData = await this.request("openGame");
80
+ if (responseData.type === "response") {
81
+ await configuredPromise;
82
+ return {
83
+ contract: responseData.response.contract,
84
+ data: this.#codecs.openGame.response.decode(responseData.response.data)
85
+ };
86
+ } else if (responseData.error.type === "timeout") throw new TimeoutError();
87
+ else throw new RgsError(responseData.error.data);
88
+ }
89
+ async play(contract, extra) {
90
+ console.log("debug 1 payload", contract, extra);
91
+ const payload = {
92
+ contract,
93
+ extra: this.#codecs.play.request.encode(extra)
94
+ };
95
+ console.log("debug payload", payload);
96
+ const responseData = await this.request("play", payload);
97
+ if (responseData.type === "response") return {
98
+ contract: responseData.response.contract,
99
+ data: this.#codecs.play.response.decode(responseData.response.data)
100
+ };
101
+ else if (responseData.error.type === "timeout") throw new TimeoutError();
102
+ else throw new RgsError(responseData.error.data);
103
+ }
104
+ };
105
+
106
+ //#endregion
107
+ export { I18n, KalambaSdk, RgsError, RgsErrorCode, TimeoutError };
@@ -0,0 +1,28 @@
1
+ /**
2
+ * BSD 3-Clause License
3
+ * Copyright (c) 2026, Kalamba Games Limited
4
+ */
5
+
6
+ //#region src/common/logger.ts
7
+ function createLogger(prefix, style) {
8
+ return (message, ...args) => {
9
+ console.log(`[%c${prefix}\x1B[m] %s`, style, message, ...args);
10
+ };
11
+ }
12
+ var WithLogger = class {
13
+ static LOG_PREFIX;
14
+ static LOG_STYLES = "color:#000000;font-weight:bold;";
15
+ log;
16
+ constructor() {
17
+ const ctor = new.target;
18
+ this.log = createLogger(ctor.LOG_PREFIX ?? ctor.name, ctor.LOG_STYLES);
19
+ }
20
+ };
21
+
22
+ //#endregion
23
+ Object.defineProperty(exports, 'WithLogger', {
24
+ enumerable: true,
25
+ get: function () {
26
+ return WithLogger;
27
+ }
28
+ });
@@ -0,0 +1,22 @@
1
+ /**
2
+ * BSD 3-Clause License
3
+ * Copyright (c) 2026, Kalamba Games Limited
4
+ */
5
+ //#region src/common/logger.ts
6
+ function createLogger(prefix, style) {
7
+ return (message, ...args) => {
8
+ console.log(`[%c${prefix}\x1B[m] %s`, style, message, ...args);
9
+ };
10
+ }
11
+ var WithLogger = class {
12
+ static LOG_PREFIX;
13
+ static LOG_STYLES = "color:#000000;font-weight:bold;";
14
+ log;
15
+ constructor() {
16
+ const ctor = new.target;
17
+ this.log = createLogger(ctor.LOG_PREFIX ?? ctor.name, ctor.LOG_STYLES);
18
+ }
19
+ };
20
+
21
+ //#endregion
22
+ export { WithLogger as t };