@pluv/io 5.0.1 → 5.0.2

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/eslint.config.mjs DELETED
@@ -1,4 +0,0 @@
1
- import { config } from "eslint-config-pluv/base";
2
-
3
- /** @type {import("eslint").Linter.Config} */
4
- export default config;
@@ -1,29 +0,0 @@
1
- import type { JsonObject } from "@pluv/types";
2
-
3
- export abstract class AbstractPersistence {
4
- public abstract addUser(
5
- room: string,
6
- connectionId: string,
7
- user: JsonObject | null,
8
- ): Promise<void>;
9
-
10
- public abstract deleteStorageState(room: string): Promise<void>;
11
-
12
- public abstract deleteUser(room: string, connectionId: string): Promise<void>;
13
-
14
- public abstract deleteUsers(room: string): Promise<void>;
15
-
16
- public abstract getStorageState(room: string): Promise<string | null>;
17
-
18
- public abstract getUser(room: string, connectionId: string): Promise<JsonObject | null>;
19
-
20
- public abstract getUsers(
21
- room: string,
22
- ): Promise<Map<[connectionId: string][0], JsonObject | null>>;
23
-
24
- public abstract getUsersSize(room: string): Promise<number>;
25
-
26
- public abstract initialize(roomContext: any): typeof this;
27
-
28
- public abstract setStorageState(room: string, state: string): Promise<void>;
29
- }
@@ -1,105 +0,0 @@
1
- import type { Maybe } from "@pluv/types";
2
- import type { AbstractPersistence } from "./AbstractPersistence";
3
- import type { AbstractPubSub } from "./AbstractPubSub";
4
- import type { AbstractWebSocket, InferWebSocketSource } from "./AbstractWebSocket";
5
- import type { JWTEncodeParams } from "./authorize";
6
- import { Persistence } from "./Persistence";
7
- import { PubSub } from "./PubSub";
8
- import type { PlatformConfig, ResolvedPluvIOAuthorize, WebSocketSerializedState } from "./types";
9
-
10
- export type InferPlatformWebSocketType<TPlatform extends AbstractPlatform> =
11
- TPlatform extends AbstractPlatform<infer IAbstractWebSocket> ? IAbstractWebSocket : never;
12
-
13
- export type InferPlatformWebSocketSource<TPlatform> =
14
- TPlatform extends AbstractPlatform<infer IAbstractWebSocket>
15
- ? InferWebSocketSource<IAbstractWebSocket>
16
- : never;
17
-
18
- export type InferInitContextType<TPlatform extends AbstractPlatform> =
19
- TPlatform extends AbstractPlatform<any, infer IInitContext> ? IInitContext : never;
20
-
21
- export type InferRoomContextType<TPlatform extends AbstractPlatform> =
22
- TPlatform extends AbstractPlatform<any, any, infer IRoomContext> ? IRoomContext : never;
23
-
24
- export type AbstractPlatformConfig<TRoomContext extends Record<string, any> = {}> = {
25
- persistence?: AbstractPersistence;
26
- pubSub?: AbstractPubSub;
27
- roomContext?: TRoomContext;
28
- };
29
-
30
- export interface ConvertWebSocketConfig {
31
- room: string;
32
- }
33
-
34
- export abstract class AbstractPlatform<
35
- TWebSocket extends AbstractWebSocket<any, any> = AbstractWebSocket<any, any>,
36
- TInitContext extends Record<string, any> = {},
37
- TRoomContext extends Record<string, any> = {},
38
- TConfig extends PlatformConfig = PlatformConfig,
39
- > {
40
- protected readonly _initContext: TInitContext | undefined;
41
- protected readonly _roomContext: TRoomContext | undefined;
42
-
43
- private _initialized: boolean = false;
44
-
45
- public persistence: AbstractPersistence;
46
- public pubSub: AbstractPubSub;
47
-
48
- public abstract readonly id: string;
49
- public readonly _createToken?: (
50
- params: JWTEncodeParams<any, any> & {
51
- authorize: Maybe<ResolvedPluvIOAuthorize<any, any>>;
52
- },
53
- ) => Promise<string>;
54
- public _fetch?: (...args: any[]) => Promise<any>;
55
- public abstract readonly _config: TConfig;
56
- public abstract readonly _name: string;
57
-
58
- constructor(config: AbstractPlatformConfig<TRoomContext> = {}) {
59
- const { roomContext, persistence, pubSub } = config;
60
-
61
- (this as any)._meta = (config as any)._meta;
62
- if (!!roomContext) this._roomContext = roomContext;
63
-
64
- this.persistence = persistence ?? new Persistence();
65
- this.pubSub = pubSub ?? new PubSub();
66
- }
67
-
68
- public abstract acceptWebSocket(webSocket: AbstractWebSocket): Promise<void>;
69
-
70
- public abstract convertWebSocket(
71
- webSocket: InferWebSocketSource<TWebSocket>,
72
- config: ConvertWebSocketConfig,
73
- ): AbstractWebSocket<any, any>;
74
-
75
- public abstract getLastPing(webSocket: AbstractWebSocket<any, any>): number | null;
76
-
77
- public abstract getSerializedState(
78
- webSocket: InferWebSocketSource<TWebSocket>,
79
- ): WebSocketSerializedState | null;
80
-
81
- public abstract getSessionId(webSocket: InferWebSocketSource<TWebSocket>): string | null;
82
-
83
- public abstract getWebSockets(): readonly InferWebSocketSource<TWebSocket>[];
84
-
85
- public abstract initialize(config: AbstractPlatformConfig<TRoomContext>): this;
86
-
87
- public abstract parseData(data: string | ArrayBuffer): Record<string, any>;
88
-
89
- public abstract randomUUID(): string;
90
-
91
- public abstract setSerializedState(
92
- webSocket: AbstractWebSocket,
93
- state: WebSocketSerializedState,
94
- ): WebSocketSerializedState;
95
-
96
- public validateConfig(config: any): void {}
97
-
98
- protected _initialize(): typeof this {
99
- if (this._initialized) throw new Error("Platform is already initialized");
100
-
101
- this._initialized = true;
102
-
103
- return this;
104
- }
105
- }
@@ -1,20 +0,0 @@
1
- import type { IOEventMessage, IOLike, InferIOOutput } from "@pluv/types";
2
- import type { SendMessageOptions } from "./types";
3
-
4
- export type IOPubSubEventMessage<
5
- TIO extends IOLike,
6
- TEvent extends keyof InferIOOutput<TIO> = keyof InferIOOutput<TIO>,
7
- > = IOEventMessage<TIO, TEvent> & {
8
- options?: SendMessageOptions;
9
- };
10
-
11
- export abstract class AbstractPubSub {
12
- public abstract publish(roomName: string, payload: IOPubSubEventMessage<any>): Promise<void>;
13
-
14
- public abstract subscribe(
15
- roomName: string,
16
- onMessage: (message: IOPubSubEventMessage<any>) => void,
17
- ): Promise<number>;
18
-
19
- public abstract unsubscribe(subscriptionId: number): void;
20
- }
@@ -1,134 +0,0 @@
1
- import type {
2
- BaseUser,
3
- InferIOAuthorizeUser,
4
- IOAuthorize,
5
- IOEventMessage,
6
- JsonObject,
7
- MaybePromise,
8
- } from "@pluv/types";
9
- import type { AbstractPersistence } from "./AbstractPersistence";
10
- import type { AbstractPlatform } from "./AbstractPlatform";
11
- import type { WebSocketSerializedState, WebSocketSession } from "./types";
12
-
13
- export type AbstractEvent = {};
14
-
15
- export type AbstractCloseEvent = AbstractEvent & {
16
- readonly code: number;
17
- readonly reason: string;
18
- };
19
-
20
- export type AbstractMessageEvent<T = any> = AbstractEvent & {
21
- readonly data: T;
22
- };
23
-
24
- export type AbstractErrorEvent = AbstractEvent & {
25
- error: unknown;
26
- message?: string;
27
- };
28
-
29
- export interface AbstractEventMap {
30
- close: AbstractCloseEvent;
31
- message: AbstractMessageEvent;
32
- error: AbstractErrorEvent;
33
- }
34
-
35
- export type EventType = keyof AbstractEventMap;
36
-
37
- export interface AbstractWebSocketHandleErrorParams {
38
- error: unknown;
39
- message?: string;
40
- room: string;
41
- session?: WebSocketSession<any>;
42
- }
43
-
44
- export type AbstractListener<TType extends keyof AbstractEventMap> = (
45
- event: AbstractEventMap[TType],
46
- ) => MaybePromise<void>;
47
-
48
- export type InferWebSocketSource<TAbstractWebSocket extends AbstractWebSocket> =
49
- TAbstractWebSocket extends AbstractWebSocket<infer IWebSocket> ? IWebSocket : never;
50
-
51
- export interface AbstractWebSocketConfig {
52
- persistence: AbstractPersistence;
53
- platform: AbstractPlatform<any>;
54
- room: string;
55
- }
56
-
57
- export abstract class AbstractWebSocket<
58
- TWebSocket = any,
59
- TAuthorize extends IOAuthorize<any, any> | null = null,
60
- > {
61
- /** The connection is not yet open. */
62
- public readonly CONNECTING = 0;
63
- /** The connection is open and ready to communicate. */
64
- public readonly OPEN = 1;
65
- /** The connection is in the process of closing. */
66
- public readonly CLOSING = 2;
67
- /** The connection is closed. */
68
- public readonly CLOSED = 3;
69
-
70
- public readonly persistence: AbstractPersistence;
71
- public readonly room: string;
72
- public readonly webSocket: TWebSocket;
73
-
74
- protected readonly _platform: AbstractPlatform<this>;
75
-
76
- public abstract set presence(presence: JsonObject | null);
77
- public abstract get readyState(): 0 | 1 | 2 | 3;
78
- public abstract get session(): WebSocketSession<TAuthorize>;
79
- public abstract get sessionId(): string;
80
- public abstract get state(): WebSocketSerializedState;
81
- public abstract set state(state: WebSocketSerializedState);
82
- public abstract set user(user: InferIOAuthorizeUser<TAuthorize>);
83
-
84
- constructor(webSocket: TWebSocket, config: AbstractWebSocketConfig) {
85
- const { persistence, platform, room } = config;
86
-
87
- this.persistence = persistence;
88
- this._platform = platform;
89
- this.room = room;
90
- this.webSocket = webSocket;
91
- }
92
-
93
- /**
94
- * TODO
95
- * @description Return an unsubscribe method to remove the event-listener.
96
- * This is preferred over adding an explicit abstract removeEventListener
97
- * method, because the implementing class might have to transform the
98
- * listener, and removing the same listener might be complicated as a
99
- * result.
100
- * @date August 28, 2022
101
- */
102
- public abstract addEventListener<TType extends EventType>(
103
- type: TType,
104
- handler: AbstractListener<TType>,
105
- ): void;
106
-
107
- public abstract close(code?: number | undefined, reason?: string | undefined): void;
108
-
109
- public abstract send(message: ArrayBuffer | ArrayBufferView | string): void;
110
-
111
- public abstract terminate(): void;
112
-
113
- public handleError(params: AbstractWebSocketHandleErrorParams): void {
114
- const { error, message: _message, room, session } = params;
115
-
116
- const message = error instanceof Error ? error.message : "Unexpected error";
117
- const stack: string | null = error instanceof Error ? (error.stack ?? null) : null;
118
- const connectionId = (session?.id ?? null) as string;
119
-
120
- return this.sendMessage({
121
- connectionId,
122
- data: { message, stack },
123
- room,
124
- type: "$error" as const,
125
- user: session?.user ?? null,
126
- });
127
- }
128
-
129
- public sendMessage<TMessage extends IOEventMessage<any> = IOEventMessage<any>>(
130
- data: TMessage,
131
- ): void {
132
- return this.send(JSON.stringify(data));
133
- }
134
- }