@mescius/js-collaboration-client 18.0.7 → 18.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/dist/index.d.ts +94 -76
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,126 +1,144 @@
|
|
|
1
|
+
export type MessageData = unknown;
|
|
2
|
+
|
|
3
|
+
export type MessageType = string | null;
|
|
4
|
+
|
|
5
|
+
export interface IClientOptions {
|
|
6
|
+
path?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface IConnectOptions {
|
|
10
|
+
/**
|
|
11
|
+
* The query parameters for the connection.
|
|
12
|
+
*/
|
|
13
|
+
query?: IQuery;
|
|
14
|
+
/**
|
|
15
|
+
* The authentication data for the connection.
|
|
16
|
+
*/
|
|
17
|
+
auth?: IAuth;
|
|
18
|
+
}
|
|
19
|
+
|
|
1
20
|
/**
|
|
2
|
-
*
|
|
21
|
+
* Represents a collaboration client for bidirectional communication with the server.
|
|
3
22
|
*/
|
|
4
23
|
export declare class Client {
|
|
5
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new collaboration client instance.
|
|
26
|
+
* @param {string} [url] - The URL of the server (optional, defaults to '/').
|
|
27
|
+
* @param {IClientOptions} [options] - The configuration options for the client (optional).
|
|
28
|
+
*/
|
|
29
|
+
constructor(url?: string, options?: IClientOptions);
|
|
6
30
|
|
|
7
31
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @param url - The server url.
|
|
32
|
+
* Retrieves the server URL of the client.
|
|
10
33
|
*/
|
|
11
|
-
|
|
34
|
+
get url(): string;
|
|
12
35
|
|
|
13
36
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @param roomId - The room
|
|
16
|
-
* @param options - The
|
|
37
|
+
* Establishes a connection to a specified room.
|
|
38
|
+
* @param {string} roomId - The ID of the room to connect to.
|
|
39
|
+
* @param {IConnectOptions} [options] - The options for establishing the connection (optional).
|
|
40
|
+
* @returns {Connection} - A new connection instance.
|
|
17
41
|
*/
|
|
18
42
|
connect(roomId: string, options?: IConnectOptions): Connection;
|
|
19
43
|
}
|
|
20
44
|
|
|
21
|
-
export interface
|
|
45
|
+
export interface IConnectionEvents {
|
|
46
|
+
connect: () => void;
|
|
47
|
+
message: (data: MessageData, type: MessageType) => void;
|
|
48
|
+
disconnect: (reason: DisconnectReason) => void;
|
|
49
|
+
error: (e: Error) => void;
|
|
50
|
+
reconnectAttempts: (attempts: number) => void;
|
|
51
|
+
reconnect: (attempts: number) => void;
|
|
52
|
+
reconnectFailed: () => void;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Defines the possible reasons for a disconnection.
|
|
57
|
+
*/
|
|
58
|
+
export enum DisconnectReason {
|
|
22
59
|
/**
|
|
23
|
-
*
|
|
60
|
+
* Indicates the client intentionally disconnected.
|
|
24
61
|
*/
|
|
25
|
-
|
|
62
|
+
CLIENT_DISCONNECT,
|
|
26
63
|
/**
|
|
27
|
-
*
|
|
64
|
+
* Indicates the server intentionally disconnected.
|
|
28
65
|
*/
|
|
29
|
-
|
|
66
|
+
SERVER_DISCONNECT,
|
|
67
|
+
/**
|
|
68
|
+
* Indicates the client failed to respond with a PONG packet within the ping timeout period.
|
|
69
|
+
*/
|
|
70
|
+
PING_TIMEOUT,
|
|
71
|
+
/**
|
|
72
|
+
* Indicates the transport connection was closed.
|
|
73
|
+
*/
|
|
74
|
+
TRANSPORT_CLOSE,
|
|
75
|
+
/**
|
|
76
|
+
* Indicates an error occurred in the transport connection.
|
|
77
|
+
*/
|
|
78
|
+
TRANSPORT_ERROR,
|
|
30
79
|
}
|
|
31
80
|
|
|
32
81
|
export interface IQuery {
|
|
33
82
|
[key: string]: unknown;
|
|
34
83
|
}
|
|
84
|
+
|
|
35
85
|
export interface IAuth {
|
|
36
86
|
[key: string]: unknown;
|
|
37
87
|
}
|
|
38
88
|
|
|
39
89
|
/**
|
|
40
|
-
*
|
|
90
|
+
* Represents a client-side collaboration connection.
|
|
41
91
|
*/
|
|
42
92
|
export declare class Connection {
|
|
43
93
|
/**
|
|
44
|
-
*
|
|
45
|
-
* @returns {string}
|
|
94
|
+
* Retrieves the unique identifier of the connection.
|
|
95
|
+
* @returns {string} The connection's ID.
|
|
46
96
|
*/
|
|
47
97
|
get id(): string;
|
|
48
98
|
/**
|
|
49
|
-
*
|
|
50
|
-
* @returns {
|
|
99
|
+
* Retrieves the room identifier of the connection.
|
|
100
|
+
* @returns {string} The room ID associated with the connection.
|
|
51
101
|
*/
|
|
52
|
-
get
|
|
102
|
+
get roomId(): string;
|
|
53
103
|
/**
|
|
54
|
-
*
|
|
55
|
-
* @
|
|
56
|
-
* @param [type] - The message type.
|
|
104
|
+
* Indicates whether the connection is active and able to send messages.
|
|
105
|
+
* @returns {boolean} True if the connection is active, false otherwise.
|
|
57
106
|
*/
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Close the connection.
|
|
61
|
-
*/
|
|
62
|
-
close(): void;
|
|
107
|
+
get connected(): boolean;
|
|
63
108
|
/**
|
|
64
|
-
* Register a listener for
|
|
65
|
-
* @
|
|
66
|
-
* @param
|
|
109
|
+
* Register a listener for a specific event.
|
|
110
|
+
* @template NAME - The type of the event name, extending keyof IConnectionEvents.
|
|
111
|
+
* @param {NAME} name - The name of the event to listen for.
|
|
112
|
+
* @param {IConnectionEvents[NAME]} f - The event handler function.
|
|
67
113
|
*/
|
|
68
|
-
on<NAME extends keyof
|
|
114
|
+
on<NAME extends keyof IConnectionEvents>(name: NAME, f: IConnectionEvents[NAME]): IConnectionEvents[NAME];
|
|
69
115
|
/**
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
72
|
-
* @param
|
|
116
|
+
* Registers a one-time listener for a specific event.
|
|
117
|
+
* @template NAME - The type of the event name, extending keyof IConnectionEvents.
|
|
118
|
+
* @param {NAME} name - The name of the event to listen for.
|
|
119
|
+
* @param {IConnectionEvents[NAME]} f - The event handler function to be called once.
|
|
73
120
|
*/
|
|
74
|
-
once<
|
|
121
|
+
once<NAME extends keyof IConnectionEvents>(name: NAME, f: IConnectionEvents[NAME]): void;
|
|
75
122
|
/**
|
|
76
|
-
*
|
|
77
|
-
* @
|
|
78
|
-
* @param
|
|
123
|
+
* Removes a listener for a specific event.
|
|
124
|
+
* @template NAME - The type of the event name, extending keyof IConnectionEvents.
|
|
125
|
+
* @param {NAME} name - The name of the event to remove the listener from.
|
|
126
|
+
* @param {IConnectionEvents[NAME]} f - The event handler function to remove.
|
|
79
127
|
*/
|
|
80
|
-
off<
|
|
128
|
+
off<NAME extends keyof IConnectionEvents>(name: NAME, f: IConnectionEvents[NAME]): void;
|
|
81
129
|
/**
|
|
82
|
-
*
|
|
130
|
+
* Destroys the observable and cleans up resources.
|
|
83
131
|
*/
|
|
84
132
|
destroy(): void;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface IEvents {
|
|
88
|
-
open: () => void;
|
|
89
|
-
message: (data: MessageData, type: MessageType) => void;
|
|
90
|
-
close: (reason: CloseReason) => void;
|
|
91
|
-
error: (e: unknown) => void;
|
|
92
|
-
reconnect: (attempts: number) => void;
|
|
93
|
-
reconnectFailed: (err?: Error) => void;
|
|
94
|
-
reconnectAttempts: (attempts: number) => void;
|
|
95
|
-
connectionStateChanged: () => void;
|
|
96
|
-
}
|
|
97
133
|
|
|
98
|
-
export type MessageData = unknown;
|
|
99
|
-
export type MessageType = string | null;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* The reason of the connection closed.
|
|
103
|
-
*/
|
|
104
|
-
export enum CloseReason {
|
|
105
|
-
/**
|
|
106
|
-
* The client has manually disconnected.
|
|
107
|
-
*/
|
|
108
|
-
CLIENT_DISCONNECT,
|
|
109
|
-
/**
|
|
110
|
-
* The server has manually disconnected.
|
|
111
|
-
*/
|
|
112
|
-
SERVER_DISCONNECT,
|
|
113
|
-
/**
|
|
114
|
-
* The client did not send a PONG packet in the pingTimeout delay.
|
|
115
|
-
*/
|
|
116
|
-
PING_TIMEOUT,
|
|
117
134
|
/**
|
|
118
|
-
*
|
|
135
|
+
* Sends a message to the server.
|
|
136
|
+
* @param {MessageData} data - The data to send.
|
|
137
|
+
* @param {MessageType} [type] - The type of the message (optional).
|
|
119
138
|
*/
|
|
120
|
-
|
|
139
|
+
send(data: MessageData, type?: MessageType): void;
|
|
121
140
|
/**
|
|
122
|
-
*
|
|
141
|
+
* Closes the connection to the server.
|
|
123
142
|
*/
|
|
124
|
-
|
|
143
|
+
close(): void;
|
|
125
144
|
}
|
|
126
|
-
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var
|
|
1
|
+
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}(this,(()=>(()=>{"use strict";var e={234:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Client=void 0;const r=n(884),s=n(42);t.Client=class{get url(){return this._url}constructor(e,t){var n;this._url=e||"/",this._path=null!==(n=null==t?void 0:t.path)&&void 0!==n?n:r.DEFAULT_REQUEST_PATH}connect(e,t){return new s.Connection(this.url,e,Object.assign({path:this._path},t))}}},42:(e,t,n)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Connection=t.DisconnectReason=void 0;const r=n(87),s=n(884);var o;!function(e){e[e.CLIENT_DISCONNECT=0]="CLIENT_DISCONNECT",e[e.SERVER_DISCONNECT=1]="SERVER_DISCONNECT",e[e.PING_TIMEOUT=2]="PING_TIMEOUT",e[e.TRANSPORT_CLOSE=3]="TRANSPORT_CLOSE",e[e.TRANSPORT_ERROR=4]="TRANSPORT_ERROR"}(o||(t.DisconnectReason=o={}));class i extends s.Observable{constructor(e,t,n){super(),this._roomId=t;const o=Object.assign(Object.assign({},n),{query:Object.assign({roomId:t},n.query),perMessageDeflate:{threshold:1024},transports:["websocket"]});this._socket=(0,r.io)(e,o),this._socket.on("connect",this._handleConnect.bind(this)),this._socket.on("disconnect",this._handleDisconnect.bind(this)),this._socket.on("connect_error",(e=>this.emit("error",[e]))),this._socket.on("message_error",(e=>this.emit("error",[a(e)]))),this._socket.on(s.SOCKET_IO_MESSAGE_EVENT,this._handleMessage.bind(this)),this._socket.io.on("reconnect_attempt",(e=>this.emit("reconnectAttempts",[e]))),this._socket.io.on("reconnect",(e=>this.emit("reconnect",[e]))),this._socket.io.on("reconnect_failed",(()=>this.emit("reconnectFailed",[])))}get id(){return this._socket.id}get roomId(){return this._roomId}get connected(){return this._socket.connected}send(e,t){this._socket.emit(s.SOCKET_IO_MESSAGE_EVENT,(0,s.encodeMessage)(e,t))}close(){this._socket.disconnect()}_handleConnect(){this.emit("connect",[])}_handleMessage(e){this.emit("message",(0,s.decodeMessage)(e))}_handleDisconnect(e){this.emit("disconnect",[c(e)])}}function c(e){switch(e){case"io server disconnect":return o.SERVER_DISCONNECT;case"io client disconnect":return o.CLIENT_DISCONNECT;case"transport close":return o.TRANSPORT_CLOSE;case"transport error":default:return o.TRANSPORT_ERROR;case"ping timeout":return o.PING_TIMEOUT}}function a(e){return"string"==typeof e?new Error(e):e}t.Connection=i},871:e=>{e.exports=require("buffer")},331:e=>{e.exports=require("pako")},87:e=>{e.exports=require("socket.io-client")},884:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var s=Object.getOwnPropertyDescriptor(t,n);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,s)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),s=this&&this.__exportStar||function(e,t){for(var n in e)"default"===n||Object.prototype.hasOwnProperty.call(t,n)||r(t,e,n)};Object.defineProperty(t,"__esModule",{value:!0}),t.DEFAULT_REQUEST_PATH=void 0,s(n(589),t),s(n(735),t),s(n(31),t),s(n(76),t),s(n(959),t),t.DEFAULT_REQUEST_PATH="/collaboration/"},31:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.LoggerManager=void 0;class n{debug(...e){}info(e){}warn(...e){}error(...e){}}class r{constructor(e){this.namespace=e}debug(...e){this.isEnabled&&this.logger.debug(`[${this.namespace}]`,...e)}info(...e){this.isEnabled&&this.logger.info(`[${this.namespace}]`,...e)}warn(...e){this.isEnabled&&this.logger.warn(`[${this.namespace}]`,...e)}error(...e){this.isEnabled&&this.logger.error(`[${this.namespace}]`,...e)}get isEnabled(){return s._isEnabledNamespace(this.namespace)}get logger(){return s.getInstance()}}class s{static getInstance(){return s._instance||(s._instance=new n),s._instance}static createLogger(e){return new r(e)}static setLogger(e){s._instance=e}static enableNamespace(e){s._enabledNamespace.push(...e)}static disableNamespace(e){s._enabledNamespace=s._enabledNamespace.filter((t=>!e.includes(t)))}static _isEnabledNamespace(e){return s._enabledNamespace.includes(e)||s._enabledNamespace.includes("*")}}t.LoggerManager=s,s._enabledNamespace=[]},735:function(e,t,n){var r=this&&this.__createBinding||(Object.create?function(e,t,n,r){void 0===r&&(r=n);var s=Object.getOwnPropertyDescriptor(t,n);s&&!("get"in s?!t.__esModule:s.writable||s.configurable)||(s={enumerable:!0,get:function(){return t[n]}}),Object.defineProperty(e,r,s)}:function(e,t,n,r){void 0===r&&(r=n),e[r]=t[n]}),s=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),o=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var n in e)"default"!==n&&Object.prototype.hasOwnProperty.call(e,n)&&r(t,e,n);return s(t,e),t};Object.defineProperty(t,"__esModule",{value:!0}),t.SOCKET_IO_MESSAGE_EVENT=void 0,t.encodeMessage=function(e,t=null){return[t,"string"==typeof e?a(e):e]},t.decodeMessage=function(e){return["string"==typeof e[1]?u(e[1]):e[1],e[0]]};const i=o(n(331)),c=n(871);function a(e){const t=(new TextEncoder).encode(e),n=i.deflate(t);return c.Buffer.from(n).toString("base64")}function u(e){const t=c.Buffer.from(e,"base64"),n=i.inflate(new Uint8Array(t));return(new TextDecoder).decode(n)}t.SOCKET_IO_MESSAGE_EVENT="m"},589:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Middleware=void 0,t.Middleware=class{constructor(){this.middlewares=new Map,this.hooks=new Map}use(e,t){this.middlewares.has(e)||this.middlewares.set(e,[]),this.middlewares.get(e).push(t)}on(e,t){this.hooks.has(e)||this.hooks.set(e,[]),this.hooks.get(e).push(t)}async trigger(e,t,n){let r=this.middlewares.get(e);if(!r||0===r.length)return n();r=r.slice();const s=async e=>{if(e)return await n(e);const o=r.shift();if(!o)return await n();await o(t,s)};await s()}async emit(e,t){const n=this.hooks.get(e);if(n)for(const e of n)await e(t)}}},959:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.Observable=void 0,t.Observable=class{constructor(){this._observers=new Map}on(e,t){return n(this._observers,e,(()=>new Set)).add(t),t}once(e,t){const n=(...r)=>{this.off(e,n),t(...r)};this.on(e,n)}off(e,t){const n=this._observers.get(e);void 0!==n&&(n.delete(t),0===n.size&&this._observers.delete(e))}emit(e,t){return Array.from((this._observers.get(e)||new Map).values()).forEach((e=>e(...t)))}destroy(){this._observers=new Map}};const n=(e,t,n)=>{let r=e.get(t);return void 0===r&&(r=n(),e.set(t,r)),r}},76:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.isNullOrUndefined=function(e){return null==e}}},t={};function n(r){var s=t[r];if(void 0!==s)return s.exports;var o=t[r]={exports:{}};return e[r].call(o.exports,o,o.exports,n),o.exports}var r={};return(()=>{var e=r;Object.defineProperty(e,"__esModule",{value:!0}),e.LoggerManager=e.Connection=e.DisconnectReason=e.Client=void 0;const t=n(234);Object.defineProperty(e,"Client",{enumerable:!0,get:function(){return t.Client}});const s=n(42);Object.defineProperty(e,"DisconnectReason",{enumerable:!0,get:function(){return s.DisconnectReason}}),Object.defineProperty(e,"Connection",{enumerable:!0,get:function(){return s.Connection}});const o=n(884);Object.defineProperty(e,"LoggerManager",{enumerable:!0,get:function(){return o.LoggerManager}})})(),r})()));
|