@lokiplay/sdk 0.1.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/LICENSE +21 -0
- package/README.md +11 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +100 -0
- package/dist/index.js.map +1 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Loki Play contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# @lokiplay/sdk
|
|
2
|
+
|
|
3
|
+
JavaScript client SDK for authenticating players, joining Loki multiplayer
|
|
4
|
+
rooms, sending actions and events, and subscribing to server messages.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
npm install @lokiplay/sdk
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Create a `LokiClient` with a project ID and a transport implementing the
|
|
11
|
+
exported `LokiTransport` interface.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type ClientEnvelope, type ServerEnvelope } from "@lokiplay/protocol";
|
|
2
|
+
export interface LokiTransport {
|
|
3
|
+
authenticate(token: string): Promise<{
|
|
4
|
+
playerId: string;
|
|
5
|
+
}>;
|
|
6
|
+
joinRoom(input: {
|
|
7
|
+
projectId: string;
|
|
8
|
+
roomKey: string;
|
|
9
|
+
}): Promise<{
|
|
10
|
+
roomId: string;
|
|
11
|
+
snapshot: ServerEnvelope;
|
|
12
|
+
}>;
|
|
13
|
+
send(message: ClientEnvelope): Promise<void>;
|
|
14
|
+
subscribe(listener: (message: unknown) => void): () => void;
|
|
15
|
+
close(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
export interface LokiClientOptions {
|
|
18
|
+
projectId: string;
|
|
19
|
+
transport: LokiTransport;
|
|
20
|
+
}
|
|
21
|
+
export declare class LokiClient {
|
|
22
|
+
#private;
|
|
23
|
+
constructor(options: LokiClientOptions);
|
|
24
|
+
initialize(): void;
|
|
25
|
+
authenticate(token: string): Promise<{
|
|
26
|
+
playerId: string;
|
|
27
|
+
}>;
|
|
28
|
+
joinRoom(roomKey: string): Promise<ServerEnvelope>;
|
|
29
|
+
sendAction(payload: unknown): Promise<void>;
|
|
30
|
+
sendEvent(payload: unknown, reliable?: boolean): Promise<void>;
|
|
31
|
+
sendHostState(expectedVersion: number, state: unknown): Promise<void>;
|
|
32
|
+
onMessage(listener: (message: ServerEnvelope) => void): () => void;
|
|
33
|
+
close(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=index.d.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AE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAa3E,SAAS,CAAC,QAAQ,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,GAAG,MAAM,IAAI;IAK5D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAM7B"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { ClientEnvelopeSchema, ServerEnvelopeSchema, } from "@lokiplay/protocol";
|
|
2
|
+
export class LokiClient {
|
|
3
|
+
#transport;
|
|
4
|
+
#projectId;
|
|
5
|
+
#listeners = new Set();
|
|
6
|
+
#unsubscribe;
|
|
7
|
+
#playerId;
|
|
8
|
+
#roomId;
|
|
9
|
+
#sequence = 0;
|
|
10
|
+
constructor(options) {
|
|
11
|
+
if (!/^[0-9a-f-]{36}$/i.test(options.projectId)) {
|
|
12
|
+
throw new Error("invalid project id");
|
|
13
|
+
}
|
|
14
|
+
this.#projectId = options.projectId;
|
|
15
|
+
this.#transport = options.transport;
|
|
16
|
+
}
|
|
17
|
+
initialize() {
|
|
18
|
+
if (this.#unsubscribe)
|
|
19
|
+
return;
|
|
20
|
+
this.#unsubscribe = this.#transport.subscribe((raw) => {
|
|
21
|
+
const message = ServerEnvelopeSchema.parse(raw);
|
|
22
|
+
if (message.roomId !== this.#roomId || message.sequence < this.#sequence)
|
|
23
|
+
return;
|
|
24
|
+
this.#sequence = message.sequence;
|
|
25
|
+
for (const listener of this.#listeners)
|
|
26
|
+
listener(message);
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async authenticate(token) {
|
|
30
|
+
if (!this.#unsubscribe)
|
|
31
|
+
this.initialize();
|
|
32
|
+
const session = await this.#transport.authenticate(token);
|
|
33
|
+
this.#playerId = session.playerId;
|
|
34
|
+
return session;
|
|
35
|
+
}
|
|
36
|
+
async joinRoom(roomKey) {
|
|
37
|
+
if (!this.#playerId)
|
|
38
|
+
throw new Error("authenticate before joining a room");
|
|
39
|
+
const joined = await this.#transport.joinRoom({
|
|
40
|
+
projectId: this.#projectId,
|
|
41
|
+
roomKey,
|
|
42
|
+
});
|
|
43
|
+
const snapshot = ServerEnvelopeSchema.parse(joined.snapshot);
|
|
44
|
+
if (snapshot.roomId !== joined.roomId || snapshot.type !== "snapshot") {
|
|
45
|
+
throw new Error("invalid join snapshot");
|
|
46
|
+
}
|
|
47
|
+
this.#roomId = joined.roomId;
|
|
48
|
+
this.#sequence = snapshot.sequence;
|
|
49
|
+
return snapshot;
|
|
50
|
+
}
|
|
51
|
+
async sendAction(payload) {
|
|
52
|
+
if (!this.#roomId)
|
|
53
|
+
throw new Error("join a room before sending actions");
|
|
54
|
+
const message = ClientEnvelopeSchema.parse({
|
|
55
|
+
protocolVersion: 1,
|
|
56
|
+
roomId: this.#roomId,
|
|
57
|
+
sequence: ++this.#sequence,
|
|
58
|
+
type: "action",
|
|
59
|
+
payload,
|
|
60
|
+
});
|
|
61
|
+
await this.#transport.send(message);
|
|
62
|
+
}
|
|
63
|
+
async sendEvent(payload, reliable = true) {
|
|
64
|
+
if (!this.#roomId)
|
|
65
|
+
throw new Error("join a room before sending events");
|
|
66
|
+
const message = ClientEnvelopeSchema.parse({
|
|
67
|
+
protocolVersion: 1,
|
|
68
|
+
roomId: this.#roomId,
|
|
69
|
+
sequence: ++this.#sequence,
|
|
70
|
+
type: "event",
|
|
71
|
+
reliable,
|
|
72
|
+
payload,
|
|
73
|
+
});
|
|
74
|
+
await this.#transport.send(message);
|
|
75
|
+
}
|
|
76
|
+
async sendHostState(expectedVersion, state) {
|
|
77
|
+
if (!this.#roomId)
|
|
78
|
+
throw new Error("join a room before sending state");
|
|
79
|
+
const message = ClientEnvelopeSchema.parse({
|
|
80
|
+
protocolVersion: 1,
|
|
81
|
+
roomId: this.#roomId,
|
|
82
|
+
sequence: ++this.#sequence,
|
|
83
|
+
type: "host_state",
|
|
84
|
+
expectedVersion,
|
|
85
|
+
state,
|
|
86
|
+
});
|
|
87
|
+
await this.#transport.send(message);
|
|
88
|
+
}
|
|
89
|
+
onMessage(listener) {
|
|
90
|
+
this.#listeners.add(listener);
|
|
91
|
+
return () => this.#listeners.delete(listener);
|
|
92
|
+
}
|
|
93
|
+
async close() {
|
|
94
|
+
this.#unsubscribe?.();
|
|
95
|
+
this.#unsubscribe = undefined;
|
|
96
|
+
this.#listeners.clear();
|
|
97
|
+
await this.#transport.close();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GAGrB,MAAM,6BAA6B,CAAC;AAkBrC,MAAM,OAAO,UAAU;IACZ,UAAU,CAAgB;IAC1B,UAAU,CAAS;IACnB,UAAU,GAAG,IAAI,GAAG,EAAqC,CAAC;IACnE,YAAY,CAAc;IAC1B,SAAS,CAAU;IACnB,OAAO,CAAU;IACjB,SAAS,GAAG,CAAC,CAAC;IAEd,YAAY,OAA0B;QACpC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;YAChD,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACxC,CAAC;QACD,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;QACpC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC;IACtC,CAAC;IAED,UAAU;QACR,IAAI,IAAI,CAAC,YAAY;YAAE,OAAO;QAC9B,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC,GAAG,EAAE,EAAE;YACpD,MAAM,OAAO,GAAG,oBAAoB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;YAChD,IAAI,OAAO,CAAC,MAAM,KAAK,IAAI,CAAC,OAAO,IAAI,OAAO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ED,SAAS,CAAC,QAA2C;QACnD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC9B,OAAO,GAAG,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAChD,CAAC;IAED,KAAK,CAAC,KAAK;QACT,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;QACtB,IAAI,CAAC,YAAY,GAAG,SAAS,CAAC;QAC9B,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QACxB,MAAM,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IAChC,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lokiplay/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "JavaScript client SDK for Loki multiplayer games.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -b --force && node ../../scripts/rewrite-package-imports.mjs dist",
|
|
22
|
+
"prepublishOnly": "npm run build"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@lokiplay/protocol": "0.1.0"
|
|
32
|
+
}
|
|
33
|
+
}
|