@websimai/socket-types 0.0.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Roman A
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,3 @@
1
+ # @websimai/socket-types
2
+
3
+ WebsimSocket type declarations
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@websimai/socket-types",
3
+ "version": "0.0.1",
4
+ "description": "WebsimSocket type declarations",
5
+ "author": "GameRoMan",
6
+ "license": "MIT",
7
+ "types": "./src/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/index.d.ts"
11
+ },
12
+ "./socket": {
13
+ "types": "./src/socket.d.ts"
14
+ }
15
+ },
16
+ "scripts": {
17
+ "publish": "bun publish --access public"
18
+ },
19
+ "keywords": [
20
+ "websim",
21
+ "types",
22
+ "typescript"
23
+ ],
24
+ "peerDependencies": {
25
+ "typescript": "^5.9.2"
26
+ }
27
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { WebsimSocket as WebsimSocketClass } from "./socket";
2
+
3
+ type WebsimSocketConstructor = typeof WebsimSocketClass;
4
+
5
+ declare global {
6
+ interface Window {
7
+ readonly WebsimSocket: WebsimSocketConstructor;
8
+ }
9
+
10
+ const WebsimSocket: WebsimSocketConstructor;
11
+ }
12
+
13
+ type WebsimSocket = InstanceType<WebsimSocketConstructor>;
14
+
15
+ export { WebsimSocket };
@@ -0,0 +1,271 @@
1
+ type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
2
+
3
+ type KeyValue = Record<string, any>;
4
+
5
+ interface WebsimSocketParty {
6
+ /**
7
+ * Object containing the client ID, avatar URL, and username of this connected client.
8
+ */
9
+ client: {
10
+ id: string;
11
+ avatarUrl: `https://${string}`;
12
+ username: string;
13
+ };
14
+
15
+ /**
16
+ * Object containing all connected peers, including this client.
17
+ * This is always up-to-date.
18
+ */
19
+ peers: {
20
+ [id: string]: {
21
+ avatarUrl: `https://${string}`;
22
+ username: string;
23
+ id: string;
24
+ is_anonymous: boolean;
25
+ };
26
+ };
27
+
28
+ subscribe: (
29
+ callback: (peers: {
30
+ [clientId: string]: { avatarUrl: string; username: string };
31
+ }) => void
32
+ ) => () => void;
33
+
34
+ /**
35
+ * Object containing the current presence state of all connected peers, including this client.
36
+ * This is always up-to-date after initialization.
37
+ */
38
+ presence: KeyValue;
39
+
40
+ /**
41
+ * Updates the current client's presence state.
42
+ * @param presence The new presence state to set.
43
+ */
44
+ updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
45
+
46
+ /**
47
+ * Subscribe to presence updates from all peers.
48
+ * @param callback Function to call when presence changes.
49
+ * @returns Function to unsubscribe.
50
+ */
51
+ subscribePresence<TPresence extends KeyValue>(
52
+ callback: (presence: { [clientId: string]: TPresence }) => void
53
+ ): () => void;
54
+
55
+ /**
56
+ * Object containing the current room-wide state.
57
+ * This is always up-to-date.
58
+ */
59
+ roomState: KeyValue;
60
+ }
61
+
62
+ interface CollectionAPI<T extends string> {
63
+ getList: <TData extends KeyValue>() => Promise<
64
+ Expand<
65
+ TData & {
66
+ id: string;
67
+ $type: T;
68
+ created_at: string;
69
+ updated_at: string;
70
+ user_id: string;
71
+ username: string;
72
+ }
73
+ >[]
74
+ >;
75
+ create: <TData extends KeyValue>(
76
+ data: TData
77
+ ) => Promise<
78
+ Expand<
79
+ TData & {
80
+ id: string;
81
+ $type: T;
82
+ created_at: string;
83
+ username: string;
84
+ }
85
+ >
86
+ >;
87
+ update: <T_Id extends string, TData extends KeyValue>(
88
+ id: T_Id,
89
+ data: TData
90
+ ) => Promise<
91
+ Expand<
92
+ TData & {
93
+ id: T_Id;
94
+ $type: T;
95
+ created_at: string;
96
+ username: string;
97
+ }
98
+ >
99
+ >;
100
+ upsert: <
101
+ TData extends KeyValue & { id?: T_Id },
102
+ T_Id extends string = string
103
+ >(
104
+ data: TData
105
+ ) => Promise<
106
+ Expand<
107
+ TData extends { id: T_Id }
108
+ ? TData &
109
+ KeyValue & {
110
+ id: T_Id;
111
+ $type: T;
112
+ created_at: string;
113
+ username: string;
114
+ }
115
+ : TData & {
116
+ id: T_Id;
117
+ $type: T;
118
+ created_at: string;
119
+ username: string;
120
+ }
121
+ >
122
+ >;
123
+ delete: (id: string) => Promise<void>;
124
+ subscribe: (
125
+ callback: (
126
+ records: Expand<
127
+ KeyValue & {
128
+ id: string;
129
+ $type: T;
130
+ created_at: string;
131
+ updated_at: string;
132
+ user_id: string;
133
+ username: string;
134
+ }
135
+ >[]
136
+ ) => void
137
+ ) => () => void;
138
+ filter: (filters: KeyValue) => CollectionAPI<T>;
139
+ }
140
+
141
+ interface QueryAPI<T extends readonly KeyValue[] = KeyValue[]>
142
+ extends PromiseLike<T> {
143
+ getList: () => Promise<T>;
144
+ subscribe: (callback: (records: T) => void) => () => void;
145
+ }
146
+
147
+ export class WebsimSocket {
148
+ constructor();
149
+
150
+ readonly CONNECTING: 0;
151
+ readonly OPEN: 1;
152
+ readonly CLOSING: 2;
153
+ readonly CLOSED: 3;
154
+
155
+ readonly binaryType: "arraybuffer" | (string & {});
156
+ readonly bufferedAmount: number;
157
+ readonly extensions: string;
158
+ readonly protocol: "ws" | "wss";
159
+ readonly readyState: 0 | 1 | 2 | 3;
160
+ readonly url: string;
161
+
162
+ accept(): void;
163
+ serializeAttachment(): void;
164
+ deserializeAttachment(): void;
165
+
166
+ onclose: ((this: WebSocket, ev: CloseEvent) => any) | null;
167
+ onerror: ((this: WebSocket, ev: Event) => any) | null;
168
+ onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null;
169
+ onopen: ((this: WebSocket, ev: Event) => any) | null;
170
+
171
+ dispatchEvent(event: Event | MessageEvent | CloseEvent | ErrorEvent): boolean;
172
+
173
+ close(_code?: number, _reason?: string): void;
174
+ send<TData extends string | object>(data: TData): void;
175
+
176
+ query(queryString: string, params?: any[]): QueryAPI;
177
+
178
+ collection<T extends string>($type: T): CollectionAPI<T>;
179
+
180
+ clientId: string;
181
+
182
+ /**
183
+ * Object containing information about the connected client and their peers.
184
+ */
185
+ party: WebsimSocketParty;
186
+
187
+ /**
188
+ * Legacy event handler for changes in connected peers.
189
+ * @param peers An object with client IDs as keys, each containing the client's avatar URL and username.
190
+ */
191
+ onPeersChanged:
192
+ | ((peers: {
193
+ [clientId: string]: { avatarUrl: string; username: string };
194
+ }) => any)
195
+ | null;
196
+
197
+ /**
198
+ * Initialize the WebSocket connection.
199
+ * @returns A promise that resolves when initialization is complete.
200
+ */
201
+ initialize(): Promise<void>;
202
+
203
+ /**
204
+ * Request a presence update from a specific client.
205
+ * @param clientId The ID of the client to request an update from.
206
+ * @param update The update to request.
207
+ */
208
+ requestPresenceUpdate(clientId: string, update: KeyValue): void;
209
+
210
+ /**
211
+ * Subscribe to presence update requests from other clients.
212
+ * @param callback Function to call when a presence update is requested.
213
+ * @returns Function to unsubscribe.
214
+ */
215
+ subscribePresenceUpdateRequests<TUpdateRequest extends KeyValue>(
216
+ callback: (updateRequest: TUpdateRequest, fromClientId: string) => void
217
+ ): () => void;
218
+
219
+ /**
220
+ * Updates the room-wide state. This merges with existing state.
221
+ * @param delta The new state to merge with current room state.
222
+ */
223
+ updateRoomState<TDelta extends KeyValue>(delta: TDelta): void;
224
+
225
+ /**
226
+ * Subscribe to room state updates.
227
+ * @param callback Function to call when room state changes.
228
+ * @returns Function to unsubscribe.
229
+ */
230
+ subscribeRoomState<TRoomState extends KeyValue>(
231
+ callback: (state: TRoomState) => void
232
+ ): () => void;
233
+
234
+ /**
235
+ * Object containing the current presence state of all connected peers, including this client.
236
+ * This is always up-to-date after initialization.
237
+ */
238
+ presence: { [clientId: string]: KeyValue };
239
+
240
+ /**
241
+ * Object containing the current room-wide state.
242
+ * This is always up-to-date.
243
+ */
244
+ roomState: KeyValue;
245
+
246
+ /**
247
+ * Object containing all connected peers, including this client.
248
+ * This is always up-to-date.
249
+ */
250
+ peers: {
251
+ [clientId: string]: {
252
+ avatarUrl: string;
253
+ username: string;
254
+ };
255
+ };
256
+
257
+ /**
258
+ * Updates the current client's presence state.
259
+ * @param presence The new presence state to set.
260
+ */
261
+ updatePresence<TPresence extends KeyValue>(presence: TPresence): void;
262
+
263
+ /**
264
+ * Subscribe to presence updates from all peers.
265
+ * @param callback Function to call when presence changes.
266
+ * @returns Function to unsubscribe.
267
+ */
268
+ subscribePresence<TPresence extends KeyValue>(
269
+ callback: (presence: { [clientId: string]: TPresence }) => void
270
+ ): () => void;
271
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": ["ESNext", "DOM"],
5
+ "target": "esnext",
6
+ "module": "preserve",
7
+ "moduleDetection": "force",
8
+
9
+ // Bundler mode
10
+ "moduleResolution": "bundler",
11
+ "allowImportingTsExtensions": true,
12
+ "noEmit": true,
13
+
14
+ // Max strictness
15
+ "strict": true,
16
+ "skipLibCheck": true,
17
+ "noFallthroughCasesInSwitch": true,
18
+ "noUncheckedIndexedAccess": true,
19
+ "noImplicitOverride": true,
20
+ "noUnusedLocals": true,
21
+ "noUnusedParameters": true,
22
+ "noPropertyAccessFromIndexSignature": true,
23
+ "noImplicitAny": true,
24
+ "strictNullChecks": true,
25
+ "forceConsistentCasingInFileNames": true,
26
+ "verbatimModuleSyntax": true
27
+ }
28
+ }