@pluv/client 0.0.0 → 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.
@@ -1,5 +1,5 @@
1
1
 
2
- > @pluv/client@0.0.0 build /home/runner/work/pluv/pluv/packages/client
2
+ > @pluv/client@0.1.0 build /home/runner/work/pluv/pluv/packages/client
3
3
  > tsup src/index.ts --format esm,cjs --dts
4
4
 
5
5
  CLI Building entry: src/index.ts
@@ -8,10 +8,10 @@
8
8
  CLI Target: es6
9
9
  ESM Build start
10
10
  CJS Build start
11
- CJS dist/index.js 29.80 KB
12
- CJS ⚡️ Build success in 129ms
13
- ESM dist/index.mjs 28.72 KB
14
- ESM ⚡️ Build success in 136ms
11
+ ESM dist/index.mjs 29.33 KB
12
+ ESM ⚡️ Build success in 157ms
13
+ CJS dist/index.js 30.41 KB
14
+ CJS ⚡️ Build success in 165ms
15
15
  DTS Build start
16
- DTS ⚡️ Build success in 3389ms
17
- DTS dist/index.d.ts 8.01 KB
16
+ DTS ⚡️ Build success in 3438ms
17
+ DTS dist/index.d.ts 8.21 KB
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @pluv/client
2
+
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a22f525: Added documentation
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [a22f525]
12
+ - @pluv/crdt-yjs@0.1.0
13
+ - @pluv/types@0.1.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Shoubhit Dash
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,110 @@
1
+ <h1 align="center">
2
+ <br>
3
+ <img src="https://github.com/pluv-io/pluv/blob/master/assets/pluv-icon-192x192.png?raw=true" alt="Pluv.IO" width="180" style="border-radius:16px">
4
+ <br>
5
+ Pluv.IO (preview)
6
+ <br>
7
+ </h1>
8
+
9
+ <h3 align="center">Multi-platform, E2E type-safe realtime packages</h3>
10
+ <h4 align="center">💕 Inspired by <a href="https://trpc.io">trpc</a> 💕 Built with <a href="https://docs.yjs.dev/">yjs</a> 💕</h4>
11
+
12
+ <p align="center">
13
+ <a href="https://www.npmjs.com/package/@pluv/client">
14
+ <img src="https://img.shields.io/npm/v/@pluv/client" alt="npm @pluv/client" />
15
+ </a>
16
+ <a href="https://github.com/pluv-io/pluv/blob/master/LICENSE">
17
+ <img alt="GitHub" src="https://img.shields.io/github/license/pluv-io/pluv" alt="License MIT" />
18
+ </a>
19
+ <img src="https://badgen.net/badge/-/TypeScript?icon=typescript&label&labelColor=blue&color=555555" alt="TypeScript">
20
+ <a href="https://commitizen.github.io/cz-cli/">
21
+ <img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg" alt="Commitizen friendly" />
22
+ </a>
23
+ <img src="https://img.shields.io/badge/docs-coming%20soon!-blue" alt="Docs coming soon!" />
24
+ </p>
25
+
26
+ <img src="https://github.com/pluv-io/pluv/blob/master/assets/demo-events.gif?raw=true" alt="Demo" />
27
+
28
+ ## `@pluv/client`
29
+
30
+ > Framework agnostic frontend client for [@pluv/io](https://img.shields.io/npm/v/@pluv/io)
31
+
32
+ ## Installation
33
+
34
+ ```bash
35
+ # npm
36
+ npm install @pluv/client yjs
37
+
38
+ # yarn
39
+ yarn add @pluv/client yjs
40
+
41
+ # pnpm
42
+ pnpm add @pluv/client yjs
43
+ ```
44
+
45
+ If you plan on using presence and/or storage, make sure to also install `zod`.
46
+
47
+ ## Basic Example
48
+
49
+ ```ts
50
+ import { createClient, y } from "@pluv/client";
51
+ import { z } from "zod";
52
+ // Import the PluvIO instance as a type from your server file
53
+ import { type io } from "./server";
54
+
55
+ // Initialize the @pluv/client client
56
+ const client = createClient<typeof io>({
57
+ authEndpoint: (room) => {
58
+ return `https://localhost:3000/api/authorize?room=${room}`;
59
+ },
60
+ wsEndpoint: (room) => {
61
+ return `ws://localhost:3000/api/room/${room}`;
62
+ },
63
+ });
64
+
65
+ interface Presence {
66
+ cursor: { x: number; y: number } | null;
67
+ }
68
+
69
+ interface Storage {
70
+ messages: readonly { name: string; message: string }[]
71
+ }
72
+
73
+ // Create a room to join
74
+ const room = client.createRoom<Presence, Storage>("my-room", {
75
+ initialPresence: { cursor: null },
76
+ initialStorage: () => ({ messages: y.array([]) }),
77
+ onAuthorizationFail: (error) => {
78
+ console.log(error.message);
79
+ },
80
+ presence: z.object({
81
+ cursor: z.nullable(
82
+ z.object({
83
+ x: z.number(),
84
+ y: z.number(),
85
+ })
86
+ ),
87
+ }),
88
+ });
89
+
90
+ const main = async () => {
91
+ await client.enter(room);
92
+
93
+ const unsubscribe = client.event("RECEIVE_MESSAGE", ({ data }) => {
94
+ console.log(data.message);
95
+ });
96
+
97
+ client.broadcast({
98
+ type: "SEND_MESSAGE",
99
+ data: { message: "Hello world!" },
100
+ });
101
+
102
+ unsubscribe();
103
+
104
+ client.leave(room);
105
+ };
106
+ ```
107
+
108
+ ## Reference
109
+
110
+ Check us out on [GitHub](https://github.com/pluv-io/pluv) for more information on how to use `@pluv/io`.
package/dist/index.d.ts CHANGED
@@ -87,7 +87,7 @@ type PluvRoomOptions<TIO extends IOLike, TPresence extends JsonObject = {}, TSto
87
87
  type RoomConfig<TIO extends IOLike, TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}> = RoomEndpoints<TIO> & PluvRoomOptions<TIO, TPresence, TStorage>;
88
88
  declare class PluvRoom<TIO extends IOLike, TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}> {
89
89
  readonly _endpoints: RoomEndpoints<TIO>;
90
- readonly room: string;
90
+ readonly id: string;
91
91
  private _crdtManager;
92
92
  private _crdtNotifier;
93
93
  private _debug;
@@ -105,7 +105,7 @@ declare class PluvRoom<TIO extends IOLike, TPresence extends JsonObject = {}, TS
105
105
  private _usersManager;
106
106
  constructor(room: string, options: RoomConfig<TIO, TPresence, TStorage>);
107
107
  get webSocket(): WebSocket | null;
108
- broadcast(message: InferEventMessage<InferIOInput<TIO>>): void;
108
+ broadcast(message: Id<InferEventMessage<InferIOInput<TIO>>>): void;
109
109
  connect(): Promise<void>;
110
110
  disconnect(): void;
111
111
  event: <TEvent extends keyof InferIOOutput<TIO>>(name: TEvent, callback: EventNotifierSubscriptionCallback<TIO, TEvent>) => (() => void);
@@ -163,16 +163,21 @@ declare class PluvRoom<TIO extends IOLike, TPresence extends JsonObject = {}, TS
163
163
  private _updateState;
164
164
  }
165
165
 
166
- type PluvClientOptions<TIO extends IOLike> = RoomEndpoints<TIO>;
166
+ type PluvClientOptions<TIO extends IOLike> = RoomEndpoints<TIO> & {
167
+ debug?: boolean;
168
+ };
167
169
  declare class PluvClient<TIO extends IOLike = IOLike> {
168
170
  private _authEndpoint;
171
+ private _debug;
169
172
  private _wsEndpoint;
170
173
  private _rooms;
171
174
  constructor(options: PluvClientOptions<TIO>);
172
175
  createRoom: <TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}>(room: string, options: PluvRoomOptions<TIO, TPresence, TStorage>) => PluvRoom<TIO, TPresence, TStorage>;
173
- enter: (room: string) => Promise<void>;
176
+ enter: (room: string | PluvRoom<TIO, JsonObject, any>) => Promise<void>;
174
177
  getRoom: <TPresence extends JsonObject = {}, TStorage extends Record<string, AbstractType<any>> = {}>(room: string) => PluvRoom<TIO, TPresence, TStorage> | null;
175
- leave: (room: string) => void;
178
+ getRooms: () => readonly PluvRoom<TIO, JsonObject, any>[];
179
+ leave: (room: string | PluvRoom<TIO, JsonObject, any>) => void;
180
+ private _logDebug;
176
181
  }
177
182
 
178
183
  declare const createClient: <TIO extends IOLike<_pluv_types.IOAuthorize<any, any>, {}, {}> = IOLike<_pluv_types.IOAuthorize<any, any>, {}, {}>>(options: PluvClientOptions<TIO>) => PluvClient<TIO>;
package/dist/index.js CHANGED
@@ -336,7 +336,7 @@ var PluvRoom = class {
336
336
  presence,
337
337
  wsEndpoint
338
338
  } = options;
339
- this.room = room;
339
+ this.id = room;
340
340
  this._debug = debug;
341
341
  this._endpoints = {
342
342
  authEndpoint,
@@ -385,10 +385,10 @@ var PluvRoom = class {
385
385
  oldState.connection.state = "Connecting" /* Connecting */;
386
386
  return oldState;
387
387
  });
388
- const url = new URL(this._getWsEndpoint(this.room));
388
+ const url = new URL(this._getWsEndpoint(this.id));
389
389
  let authToken = null;
390
390
  try {
391
- authToken = yield this._getAuthorization(this.room);
391
+ authToken = yield this._getAuthorization(this.id);
392
392
  } catch (err) {
393
393
  this._onAuthorizationFail(err);
394
394
  return;
@@ -952,28 +952,43 @@ var PluvClient = class {
952
952
  wsEndpoint: this._wsEndpoint
953
953
  });
954
954
  this._rooms.set(room, newRoom);
955
+ this._logDebug(`New room was created: ${room}`);
955
956
  return newRoom;
956
957
  };
957
958
  this.enter = (room) => __async(this, null, function* () {
958
- const toEnter = this.getRoom(room);
959
+ const toEnter = typeof room === "string" ? this.getRoom(room) : room;
959
960
  if (!toEnter) {
960
961
  throw new Error(`Could not find room: ${room}.`);
961
962
  }
963
+ this._rooms.set(toEnter.id, toEnter);
962
964
  yield toEnter.connect();
965
+ this._logDebug(`Entered room: ${room}`);
963
966
  });
964
967
  this.getRoom = (room) => {
965
968
  const found = this._rooms.get(room);
966
969
  return found != null ? found : null;
967
970
  };
971
+ this.getRooms = () => {
972
+ return Array.from(this._rooms.values());
973
+ };
968
974
  this.leave = (room) => {
969
- const toLeave = this.getRoom(room);
975
+ const toLeave = typeof room === "string" ? this.getRoom(room) : room;
970
976
  if (!toLeave)
971
977
  return;
972
978
  toLeave.disconnect();
973
- this._rooms.delete(room);
979
+ this._rooms.delete(toLeave.id);
980
+ this._logDebug(`Left and deleted room: ${room}`);
974
981
  };
975
- this._authEndpoint = options.authEndpoint;
976
- this._wsEndpoint = options.wsEndpoint;
982
+ const { authEndpoint, debug = false, wsEndpoint } = options;
983
+ this._authEndpoint = authEndpoint;
984
+ this._debug = debug;
985
+ this._wsEndpoint = wsEndpoint;
986
+ }
987
+ _logDebug(...data) {
988
+ var _a;
989
+ if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
990
+ return;
991
+ this._debug && console.log(...data);
977
992
  }
978
993
  };
979
994
 
package/dist/index.mjs CHANGED
@@ -313,7 +313,7 @@ var PluvRoom = class {
313
313
  presence,
314
314
  wsEndpoint
315
315
  } = options;
316
- this.room = room;
316
+ this.id = room;
317
317
  this._debug = debug;
318
318
  this._endpoints = {
319
319
  authEndpoint,
@@ -362,10 +362,10 @@ var PluvRoom = class {
362
362
  oldState.connection.state = "Connecting" /* Connecting */;
363
363
  return oldState;
364
364
  });
365
- const url = new URL(this._getWsEndpoint(this.room));
365
+ const url = new URL(this._getWsEndpoint(this.id));
366
366
  let authToken = null;
367
367
  try {
368
- authToken = yield this._getAuthorization(this.room);
368
+ authToken = yield this._getAuthorization(this.id);
369
369
  } catch (err) {
370
370
  this._onAuthorizationFail(err);
371
371
  return;
@@ -929,28 +929,43 @@ var PluvClient = class {
929
929
  wsEndpoint: this._wsEndpoint
930
930
  });
931
931
  this._rooms.set(room, newRoom);
932
+ this._logDebug(`New room was created: ${room}`);
932
933
  return newRoom;
933
934
  };
934
935
  this.enter = (room) => __async(this, null, function* () {
935
- const toEnter = this.getRoom(room);
936
+ const toEnter = typeof room === "string" ? this.getRoom(room) : room;
936
937
  if (!toEnter) {
937
938
  throw new Error(`Could not find room: ${room}.`);
938
939
  }
940
+ this._rooms.set(toEnter.id, toEnter);
939
941
  yield toEnter.connect();
942
+ this._logDebug(`Entered room: ${room}`);
940
943
  });
941
944
  this.getRoom = (room) => {
942
945
  const found = this._rooms.get(room);
943
946
  return found != null ? found : null;
944
947
  };
948
+ this.getRooms = () => {
949
+ return Array.from(this._rooms.values());
950
+ };
945
951
  this.leave = (room) => {
946
- const toLeave = this.getRoom(room);
952
+ const toLeave = typeof room === "string" ? this.getRoom(room) : room;
947
953
  if (!toLeave)
948
954
  return;
949
955
  toLeave.disconnect();
950
- this._rooms.delete(room);
956
+ this._rooms.delete(toLeave.id);
957
+ this._logDebug(`Left and deleted room: ${room}`);
951
958
  };
952
- this._authEndpoint = options.authEndpoint;
953
- this._wsEndpoint = options.wsEndpoint;
959
+ const { authEndpoint, debug = false, wsEndpoint } = options;
960
+ this._authEndpoint = authEndpoint;
961
+ this._debug = debug;
962
+ this._wsEndpoint = wsEndpoint;
963
+ }
964
+ _logDebug(...data) {
965
+ var _a;
966
+ if (((_a = process == null ? void 0 : process.env) == null ? void 0 : _a.NODE_ENV) === "production")
967
+ return;
968
+ this._debug && console.log(...data);
954
969
  }
955
970
  };
956
971
 
package/package.json CHANGED
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "name": "@pluv/client",
3
- "version": "0.0.0",
3
+ "version": "0.1.0",
4
+ "description": "@pluv/io frontend client",
5
+ "author": "leedavidcs",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/pluv-io/pluv",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/pluv-io/pluv.git",
11
+ "directory": "packages/client"
12
+ },
4
13
  "main": "./dist/index.js",
5
14
  "module": "./dist/index.mjs",
6
15
  "types": "./dist/index.d.ts",
@@ -8,16 +17,16 @@
8
17
  "access": "public"
9
18
  },
10
19
  "dependencies": {
11
- "@pluv/crdt-yjs": "^0.0.0",
12
- "@pluv/types": "^0.0.0",
20
+ "@pluv/crdt-yjs": "^0.1.0",
21
+ "@pluv/types": "^0.1.0",
13
22
  "wonka": "^6.1.2"
14
23
  },
15
24
  "peerDependencies": {
16
25
  "yjs": "^13.0.0"
17
26
  },
18
27
  "devDependencies": {
19
- "@pluv/tsconfig": "^0.0.0",
20
- "eslint-config-pluv": "^0.0.0",
28
+ "@pluv/tsconfig": "^0.1.0",
29
+ "eslint-config-pluv": "^0.1.0",
21
30
  "tsup": "^6.5.0",
22
31
  "typescript": "^4.9.4",
23
32
  "yjs": "^13.5.44"
package/src/PluvClient.ts CHANGED
@@ -8,17 +8,23 @@ import type {
8
8
  } from "./PluvRoom";
9
9
  import { PluvRoom } from "./PluvRoom";
10
10
 
11
- export type PluvClientOptions<TIO extends IOLike> = RoomEndpoints<TIO>;
11
+ export type PluvClientOptions<TIO extends IOLike> = RoomEndpoints<TIO> & {
12
+ debug?: boolean;
13
+ };
12
14
 
13
15
  export class PluvClient<TIO extends IOLike = IOLike> {
14
16
  private _authEndpoint: AuthEndpoint;
17
+ private _debug: boolean;
15
18
  private _wsEndpoint: WsEndpoint;
16
19
 
17
20
  private _rooms = new Map<string, PluvRoom<TIO, JsonObject, any>>();
18
21
 
19
22
  constructor(options: PluvClientOptions<TIO>) {
20
- this._authEndpoint = options.authEndpoint as AuthEndpoint;
21
- this._wsEndpoint = options.wsEndpoint;
23
+ const { authEndpoint, debug = false, wsEndpoint } = options;
24
+
25
+ this._authEndpoint = authEndpoint as AuthEndpoint;
26
+ this._debug = debug;
27
+ this._wsEndpoint = wsEndpoint;
22
28
  }
23
29
 
24
30
  public createRoom = <
@@ -43,17 +49,25 @@ export class PluvClient<TIO extends IOLike = IOLike> {
43
49
 
44
50
  this._rooms.set(room, newRoom);
45
51
 
52
+ this._logDebug(`New room was created: ${room}`);
53
+
46
54
  return newRoom;
47
55
  };
48
56
 
49
- public enter = async (room: string): Promise<void> => {
50
- const toEnter = this.getRoom(room);
57
+ public enter = async (
58
+ room: string | PluvRoom<TIO, JsonObject, any>
59
+ ): Promise<void> => {
60
+ const toEnter = typeof room === "string" ? this.getRoom(room) : room;
51
61
 
52
62
  if (!toEnter) {
53
63
  throw new Error(`Could not find room: ${room}.`);
54
64
  }
55
65
 
66
+ this._rooms.set(toEnter.id, toEnter);
67
+
56
68
  await toEnter.connect();
69
+
70
+ this._logDebug(`Entered room: ${room}`);
57
71
  };
58
72
 
59
73
  public getRoom = <
@@ -69,13 +83,26 @@ export class PluvClient<TIO extends IOLike = IOLike> {
69
83
  return found ?? null;
70
84
  };
71
85
 
72
- public leave = (room: string): void => {
73
- const toLeave = this.getRoom(room);
86
+ public getRooms = (): readonly PluvRoom<TIO, JsonObject, any>[] => {
87
+ return Array.from(this._rooms.values());
88
+ };
89
+
90
+ public leave = (room: string | PluvRoom<TIO, JsonObject, any>): void => {
91
+ const toLeave = typeof room === "string" ? this.getRoom(room) : room;
74
92
 
75
93
  if (!toLeave) return;
76
94
 
77
95
  toLeave.disconnect();
78
96
 
79
- this._rooms.delete(room);
97
+ this._rooms.delete(toLeave.id);
98
+
99
+ this._logDebug(`Left and deleted room: ${room}`);
80
100
  };
101
+
102
+ private _logDebug(...data: any[]): void {
103
+ // eslint-disable-next-line turbo/no-undeclared-env-vars
104
+ if (process?.env?.NODE_ENV === "production") return;
105
+
106
+ this._debug && console.log(...data);
107
+ }
81
108
  }
package/src/PluvRoom.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { doc, InferYjsSharedTypeJson, YjsDoc } from "@pluv/crdt-yjs";
1
+ import { doc, InferYjsSharedTypeJson } from "@pluv/crdt-yjs";
2
2
  import type {
3
3
  BaseIOEventRecord,
4
4
  EventMessage,
@@ -121,7 +121,7 @@ export class PluvRoom<
121
121
  > {
122
122
  readonly _endpoints: RoomEndpoints<TIO>;
123
123
 
124
- public readonly room: string;
124
+ public readonly id: string;
125
125
 
126
126
  private _crdtManager: CrdtManager<TStorage> | null = null;
127
127
  private _crdtNotifier = new CrdtNotifier<TStorage>();
@@ -167,7 +167,7 @@ export class PluvRoom<
167
167
  wsEndpoint,
168
168
  } = options;
169
169
 
170
- this.room = room;
170
+ this.id = room;
171
171
  this._debug = debug;
172
172
 
173
173
  this._endpoints = {
@@ -201,7 +201,7 @@ export class PluvRoom<
201
201
  return this._state.webSocket;
202
202
  }
203
203
 
204
- public broadcast(message: InferEventMessage<InferIOInput<TIO>>): void {
204
+ public broadcast(message: Id<InferEventMessage<InferIOInput<TIO>>>): void {
205
205
  if (!this._state.webSocket) return;
206
206
  if (this._state.connection.state !== ConnectionState.Open) return;
207
207
 
@@ -228,12 +228,12 @@ export class PluvRoom<
228
228
  return oldState;
229
229
  });
230
230
 
231
- const url = new URL(this._getWsEndpoint(this.room));
231
+ const url = new URL(this._getWsEndpoint(this.id));
232
232
 
233
233
  let authToken: string | null = null;
234
234
 
235
235
  try {
236
- authToken = await this._getAuthorization(this.room);
236
+ authToken = await this._getAuthorization(this.id);
237
237
  } catch (err) {
238
238
  this._onAuthorizationFail(err);
239
239