@proj-airi/server-sdk 0.1.3 → 0.2.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.cjs CHANGED
@@ -7,31 +7,64 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
7
7
 
8
8
  const WebSocket__default = /*#__PURE__*/_interopDefaultCompat(WebSocket);
9
9
 
10
+ function sleep(ms) {
11
+ return new Promise((resolve) => setTimeout(resolve, ms));
12
+ }
13
+
10
14
  class Client {
15
+ opts;
11
16
  websocket;
12
17
  eventListeners = /* @__PURE__ */ new Map();
18
+ authenticateAttempts = 0;
13
19
  constructor(options) {
14
- const opts = defu.defu(options, { url: "ws://localhost:6121/ws", possibleEvents: [] });
15
- this.websocket = new WebSocket__default(opts.url);
20
+ this.opts = defu.defu(
21
+ options,
22
+ { url: "ws://localhost:6121/ws", possibleEvents: [] }
23
+ );
24
+ this.websocket = new WebSocket__default(this.opts.url);
25
+ this.onEvent("module:authenticated", async (event) => {
26
+ const auth = event.data.authenticated;
27
+ if (!auth) {
28
+ this.authenticateAttempts++;
29
+ await sleep(2 ** this.authenticateAttempts * 1e3);
30
+ this.tryAuthenticate();
31
+ } else {
32
+ this.tryAnnounce();
33
+ }
34
+ });
16
35
  this.websocket.onmessage = this.handleMessage.bind(this);
17
36
  this.websocket.onopen = () => {
18
- this.send({
19
- type: "module:announce",
20
- data: {
21
- name: opts.name,
22
- possibleEvents: opts.possibleEvents
23
- }
24
- });
37
+ if (this.opts.token) {
38
+ this.tryAuthenticate();
39
+ } else {
40
+ this.tryAnnounce();
41
+ }
25
42
  };
43
+ this.websocket.onclose = () => {
44
+ this.authenticateAttempts = 0;
45
+ };
46
+ }
47
+ tryAnnounce() {
48
+ this.send({
49
+ type: "module:announce",
50
+ data: {
51
+ name: this.opts.name,
52
+ possibleEvents: this.opts.possibleEvents
53
+ }
54
+ });
55
+ }
56
+ tryAuthenticate() {
57
+ if (this.opts.token) {
58
+ this.send({ type: "module:authenticate", data: { token: this.opts.token || "" } });
59
+ }
26
60
  }
27
61
  async handleMessage(event) {
28
62
  const data = JSON.parse(event.data);
29
63
  const listeners = this.eventListeners.get(data.type);
30
64
  if (!listeners)
31
65
  return;
32
- for (const listener of listeners) {
66
+ for (const listener of listeners)
33
67
  await listener(data);
34
- }
35
68
  }
36
69
  onEvent(event, callback) {
37
70
  if (!this.eventListeners.get(event)) {
package/dist/index.d.cts CHANGED
@@ -5,11 +5,16 @@ interface ClientOptions {
5
5
  url?: string;
6
6
  name: string;
7
7
  possibleEvents?: Array<(keyof WebSocketEvents)>;
8
+ token?: string;
8
9
  }
9
10
  declare class Client {
11
+ private opts;
10
12
  private websocket;
11
13
  private eventListeners;
14
+ private authenticateAttempts;
12
15
  constructor(options: ClientOptions);
16
+ private tryAnnounce;
17
+ private tryAuthenticate;
13
18
  private handleMessage;
14
19
  onEvent<E extends keyof WebSocketEvents>(event: E, callback: (data: WebSocketBaseEvent<E, WebSocketEvents[E]>) => void | Promise<void>): void;
15
20
  send(data: WebSocketEvent): void;
package/dist/index.d.mts CHANGED
@@ -5,11 +5,16 @@ interface ClientOptions {
5
5
  url?: string;
6
6
  name: string;
7
7
  possibleEvents?: Array<(keyof WebSocketEvents)>;
8
+ token?: string;
8
9
  }
9
10
  declare class Client {
11
+ private opts;
10
12
  private websocket;
11
13
  private eventListeners;
14
+ private authenticateAttempts;
12
15
  constructor(options: ClientOptions);
16
+ private tryAnnounce;
17
+ private tryAuthenticate;
13
18
  private handleMessage;
14
19
  onEvent<E extends keyof WebSocketEvents>(event: E, callback: (data: WebSocketBaseEvent<E, WebSocketEvents[E]>) => void | Promise<void>): void;
15
20
  send(data: WebSocketEvent): void;
package/dist/index.d.ts CHANGED
@@ -5,11 +5,16 @@ interface ClientOptions {
5
5
  url?: string;
6
6
  name: string;
7
7
  possibleEvents?: Array<(keyof WebSocketEvents)>;
8
+ token?: string;
8
9
  }
9
10
  declare class Client {
11
+ private opts;
10
12
  private websocket;
11
13
  private eventListeners;
14
+ private authenticateAttempts;
12
15
  constructor(options: ClientOptions);
16
+ private tryAnnounce;
17
+ private tryAuthenticate;
13
18
  private handleMessage;
14
19
  onEvent<E extends keyof WebSocketEvents>(event: E, callback: (data: WebSocketBaseEvent<E, WebSocketEvents[E]>) => void | Promise<void>): void;
15
20
  send(data: WebSocketEvent): void;
package/dist/index.mjs CHANGED
@@ -1,31 +1,64 @@
1
1
  import WebSocket from 'crossws/websocket';
2
2
  import { defu } from 'defu';
3
3
 
4
+ function sleep(ms) {
5
+ return new Promise((resolve) => setTimeout(resolve, ms));
6
+ }
7
+
4
8
  class Client {
9
+ opts;
5
10
  websocket;
6
11
  eventListeners = /* @__PURE__ */ new Map();
12
+ authenticateAttempts = 0;
7
13
  constructor(options) {
8
- const opts = defu(options, { url: "ws://localhost:6121/ws", possibleEvents: [] });
9
- this.websocket = new WebSocket(opts.url);
14
+ this.opts = defu(
15
+ options,
16
+ { url: "ws://localhost:6121/ws", possibleEvents: [] }
17
+ );
18
+ this.websocket = new WebSocket(this.opts.url);
19
+ this.onEvent("module:authenticated", async (event) => {
20
+ const auth = event.data.authenticated;
21
+ if (!auth) {
22
+ this.authenticateAttempts++;
23
+ await sleep(2 ** this.authenticateAttempts * 1e3);
24
+ this.tryAuthenticate();
25
+ } else {
26
+ this.tryAnnounce();
27
+ }
28
+ });
10
29
  this.websocket.onmessage = this.handleMessage.bind(this);
11
30
  this.websocket.onopen = () => {
12
- this.send({
13
- type: "module:announce",
14
- data: {
15
- name: opts.name,
16
- possibleEvents: opts.possibleEvents
17
- }
18
- });
31
+ if (this.opts.token) {
32
+ this.tryAuthenticate();
33
+ } else {
34
+ this.tryAnnounce();
35
+ }
19
36
  };
37
+ this.websocket.onclose = () => {
38
+ this.authenticateAttempts = 0;
39
+ };
40
+ }
41
+ tryAnnounce() {
42
+ this.send({
43
+ type: "module:announce",
44
+ data: {
45
+ name: this.opts.name,
46
+ possibleEvents: this.opts.possibleEvents
47
+ }
48
+ });
49
+ }
50
+ tryAuthenticate() {
51
+ if (this.opts.token) {
52
+ this.send({ type: "module:authenticate", data: { token: this.opts.token || "" } });
53
+ }
20
54
  }
21
55
  async handleMessage(event) {
22
56
  const data = JSON.parse(event.data);
23
57
  const listeners = this.eventListeners.get(data.type);
24
58
  if (!listeners)
25
59
  return;
26
- for (const listener of listeners) {
60
+ for (const listener of listeners)
27
61
  await listener(data);
28
- }
29
62
  }
30
63
  onEvent(event, callback) {
31
64
  if (!this.eventListeners.get(event)) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@proj-airi/server-sdk",
3
3
  "type": "module",
4
- "version": "0.1.3",
4
+ "version": "0.2.0",
5
5
  "private": false,
6
6
  "description": "Client-side SDK implementation for connecting to Airi server components and runtimes",
7
7
  "author": {
@@ -31,14 +31,15 @@
31
31
  "package.json"
32
32
  ],
33
33
  "dependencies": {
34
- "crossws": "^0.3.1",
34
+ "crossws": "^0.3.3",
35
35
  "defu": "^6.1.4",
36
- "@proj-airi/server-shared": "^0.1.3"
36
+ "@proj-airi/server-shared": "^0.2.0"
37
37
  },
38
38
  "scripts": {
39
39
  "dev": "pnpm run stub",
40
40
  "stub": "unbuild --stub",
41
41
  "build": "unbuild",
42
+ "typecheck": "tsc --noEmit",
42
43
  "package:publish": "pnpm build && pnpm publish --access public --no-git-checks"
43
44
  }
44
45
  }