stelar-time-real 1.2.3 → 2.0.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/README.md CHANGED
@@ -558,6 +558,13 @@ client.emitBinary('upload', fileData);
558
558
 
559
559
  ## Changelog
560
560
 
561
+ ### v2.0.0
562
+ - 🎉 **TypeScript!** - Código fuente ahora es TypeScript
563
+ - Tipos completos para IDEs (VS Code, WebStorm, etc.)
564
+ - Autocompletado y verificación de tipos
565
+ - Compatibilidad total con JS (los usuarios reciben archivos .js)
566
+ - Los tipos .d.ts se generan automáticamente
567
+
561
568
  ### v1.2.3
562
569
  - Cleanup: código fantasma eliminado (_parent, _children, _externalServer)
563
570
 
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "stelar-time-real",
3
- "version": "1.2.3",
4
- "description": "WebSocket liviano y rápido sin dependecias. Alternativa a socket.io, namespaces, ACKs y archivos binarios.",
5
- "main": "src/index.js",
3
+ "version": "2.0.0",
4
+ "description": "WebSocket liviano y rápido sin dependendas en TypeScript. Alternativa a socket.io, namespaces, ACKs y archivos binarios.",
5
+ "main": "./src/index.js",
6
+ "types": "./src/index.d.ts",
6
7
  "type": "module",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "prepublishOnly": "npm run build"
11
+ },
7
12
  "keywords": [
8
13
  "websocket",
9
14
  "real-time",
@@ -33,5 +38,9 @@
33
38
  ],
34
39
  "dependencies": {
35
40
  "ws": "^8.14.0"
41
+ },
42
+ "devDependencies": {
43
+ "@types/ws": "^8.5.10",
44
+ "typescript": "^5.3.0"
36
45
  }
37
46
  }
@@ -0,0 +1,52 @@
1
+ export interface StelarClientOptions {
2
+ reconnection?: boolean;
3
+ reconnectionAttempts?: number;
4
+ reconnectionDelay?: number;
5
+ heartbeatInterval?: number;
6
+ ackTimeout?: number;
7
+ }
8
+ export interface StelarEmitOptions {
9
+ ack?: string;
10
+ }
11
+ export type StelarEventHandler = (data: unknown) => void;
12
+ export type StelarBinaryHandler = (buffer: ArrayBuffer) => void;
13
+ declare class StelarClient {
14
+ private url;
15
+ private options;
16
+ private ws;
17
+ private events;
18
+ private _wildcardHandler;
19
+ private connected;
20
+ private id;
21
+ private _reconnectAttempts;
22
+ private _hbTimer;
23
+ private _isManualClose;
24
+ private _acks;
25
+ constructor(urlOrPort?: string | number, options?: StelarClientOptions);
26
+ setUrl(url: string): this;
27
+ on(event: string, handler: StelarEventHandler): this;
28
+ off(event: string, handler: StelarEventHandler): this;
29
+ onAll(handler: (data: {
30
+ event: string;
31
+ data: unknown;
32
+ isBinary?: boolean;
33
+ buffer?: ArrayBuffer;
34
+ }) => void): this;
35
+ onAck(name: string, handler: StelarEventHandler): this;
36
+ emit(event: string, data?: unknown, opts?: StelarEmitOptions): this;
37
+ emitBinary(event: string, data: ArrayBuffer): this;
38
+ sendFile(file: ArrayBuffer): this;
39
+ sendImage(blob: ArrayBuffer): this;
40
+ request(event: string, data: unknown, ackName: string): Promise<unknown>;
41
+ joinRoom(room: string): this;
42
+ leaveRoom(): this;
43
+ private startHeartbeat;
44
+ private stopHeartbeat;
45
+ private _connect;
46
+ connect(callback?: () => void): this;
47
+ disconnect(): this;
48
+ isConnected(): boolean;
49
+ getUrl(): string;
50
+ }
51
+ export default StelarClient;
52
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["client.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,mBAAmB;IAClC,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,iBAAiB;IAChC,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,MAAM,MAAM,kBAAkB,GAAG,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AQ,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI;IAIjC,SAAS,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI;IAIlC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAiBxE,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAK5B,SAAS,IAAI,IAAI;IAKjB,OAAO,CAAC,cAAc;IAQtB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,QAAQ;IAgFhB,OAAO,CAAC,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,IAAI;IAapC,UAAU,IAAI,IAAI;IAOlB,WAAW,IAAI,OAAO;IAItB,MAAM,IAAI,MAAM;CAGjB;AAED,eAAe,YAAY,CAAC"}
package/src/client.js CHANGED
@@ -1,237 +1,215 @@
1
1
  class StelarClient {
2
- constructor(urlOrPort = 'localhost:3000', options = {}) {
3
- if (typeof urlOrPort === 'number') {
4
- this.url = `ws://localhost:${urlOrPort}`;
5
- } else if (urlOrPort.includes('://')) {
6
- this.url = urlOrPort.startsWith('http') ? 'ws' + urlOrPort.slice(4) : urlOrPort;
7
- } else {
8
- this.url = `ws://${urlOrPort}`;
9
- }
10
-
11
- this.options = {
12
- reconnection: options.reconnection !== false,
13
- reconnectionAttempts: options.reconnectionAttempts || 5,
14
- reconnectionDelay: options.reconnectionDelay || 1000,
15
- heartbeatInterval: options.heartbeatInterval || 30000,
16
- ackTimeout: options.ackTimeout || 5000
17
- };
18
-
19
- this.ws = null;
20
- this.events = {};
21
- this._wildcardHandler = null;
22
- this.connected = false;
23
- this.id = null;
24
- this._reconnectAttempts = 0;
25
- this._hbTimer = null;
26
- this._isManualClose = false;
27
- this._acks = {};
28
- }
29
-
30
- setUrl(url) {
31
- this.url = url;
32
- return this;
33
- }
34
-
35
- on(event, handler) {
36
- this.events[event] = handler;
37
- return this;
38
- }
39
-
40
- off(event, handler) {
41
- if (this.events[event] === handler) {
42
- delete this.events[event];
43
- }
44
- return this;
45
- }
46
-
47
- onAll(handler) {
48
- this._wildcardHandler = handler;
49
- return this;
50
- }
51
-
52
- onAck(name, handler) {
53
- this._acks[name] = handler;
54
- return this;
55
- }
56
-
57
- emit(event, data, opts = {}) {
58
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
59
- const payload = { event, data };
60
- if (opts.ack) {
61
- payload._ackName = opts.ack;
62
- }
63
- this.ws.send(JSON.stringify(payload));
64
- }
65
- return this;
66
- }
67
-
68
- emitBinary(event, data) {
69
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
70
- const header = JSON.stringify({ event });
71
- const headerBytes = new TextEncoder().encode(header);
72
- const combined = new Uint8Array(headerBytes.length + 1 + data.byteLength);
73
- combined.set(headerBytes, 0);
74
- combined[headerBytes.length] = 0;
75
- combined.set(new Uint8Array(data), headerBytes.length + 1);
76
- this.ws.send(combined);
77
- }
78
- return this;
79
- }
80
-
81
- sendFile(file) {
82
- return this.emitBinary('file', file);
83
- }
84
-
85
- sendImage(blob) {
86
- return this.emitBinary('image', blob);
87
- }
88
-
89
- request(event, data, ackName) {
90
- return new Promise((resolve, reject) => {
91
- const timeout = setTimeout(() => {
92
- reject(new Error(`ACK '${ackName}' timeout`));
93
- }, this.options.ackTimeout);
94
-
95
- const handler = (responseData) => {
96
- clearTimeout(timeout);
97
- this.off(ackName, handler);
98
- resolve(responseData);
99
- };
100
-
101
- this.on(ackName, handler);
102
- this.emit(event, data, { ack: ackName });
103
- });
104
- }
105
-
106
- joinRoom(room) {
107
- this.emit('join-room', room);
108
- return this;
109
- }
110
-
111
- leaveRoom() {
112
- this.emit('leave-room', {});
113
- return this;
114
- }
115
-
116
- _startHeartbeat() {
117
- this._hbTimer = setInterval(() => {
118
- if (this.ws && this.ws.readyState === WebSocket.OPEN) {
119
- this.emit('pong', Date.now());
120
- }
121
- }, this.options.heartbeatInterval);
122
- }
123
-
124
- _stopHeartbeat() {
125
- if (this._hbTimer) {
126
- clearInterval(this._hbTimer);
127
- this._hbTimer = null;
128
- }
129
- }
130
-
131
- _connect() {
132
- this._isManualClose = false;
133
- this.ws = new WebSocket(this.url);
134
-
135
- this.ws.onopen = () => {
136
- this.connected = true;
137
- this._reconnectAttempts = 0;
138
- if (this.events['connect']) this.events['connect']();
139
- this._startHeartbeat();
140
- };
141
-
142
- this.ws.binaryType = 'arraybuffer';
143
-
144
- this.ws.onmessage = (e) => {
145
- try {
146
- if (e.data instanceof ArrayBuffer) {
147
- const view = new Uint8Array(e.data);
148
- let headerEnd = -1;
149
- for (let i = 0; i < view.length; i++) {
150
- if (view[i] === 0) {
151
- headerEnd = i;
152
- break;
2
+ constructor(urlOrPort = 'localhost:3000', options = {}) {
3
+ this.ws = null;
4
+ this.events = new Map();
5
+ this._wildcardHandler = null;
6
+ this.connected = false;
7
+ this.id = null;
8
+ this._reconnectAttempts = 0;
9
+ this._hbTimer = null;
10
+ this._isManualClose = false;
11
+ this._acks = new Map();
12
+ if (typeof urlOrPort === 'number') {
13
+ this.url = `ws://localhost:${urlOrPort}`;
14
+ }
15
+ else if (urlOrPort.includes('://')) {
16
+ this.url = urlOrPort.startsWith('http') ? 'ws' + urlOrPort.slice(4) : urlOrPort;
17
+ }
18
+ else {
19
+ this.url = `ws://${urlOrPort}`;
20
+ }
21
+ this.options = {
22
+ reconnection: options.reconnection !== false,
23
+ reconnectionAttempts: options.reconnectionAttempts || 5,
24
+ reconnectionDelay: options.reconnectionDelay || 1000,
25
+ heartbeatInterval: options.heartbeatInterval || 30000,
26
+ ackTimeout: options.ackTimeout || 5000
27
+ };
28
+ }
29
+ setUrl(url) {
30
+ this.url = url;
31
+ return this;
32
+ }
33
+ on(event, handler) {
34
+ this.events.set(event, handler);
35
+ return this;
36
+ }
37
+ off(event, handler) {
38
+ if (this.events.get(event) === handler) {
39
+ this.events.delete(event);
40
+ }
41
+ return this;
42
+ }
43
+ onAll(handler) {
44
+ this._wildcardHandler = handler;
45
+ return this;
46
+ }
47
+ onAck(name, handler) {
48
+ this._acks.set(name, handler);
49
+ return this;
50
+ }
51
+ emit(event, data, opts = {}) {
52
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
53
+ const payload = { event, data };
54
+ if (opts.ack) {
55
+ payload._ackName = opts.ack;
153
56
  }
154
- }
155
- if (headerEnd === -1) return;
156
-
157
- const headerStr = new TextDecoder().decode(view.slice(0, headerEnd));
158
- const header = JSON.parse(headerStr);
159
- const buffer = view.slice(headerEnd + 1);
160
-
161
- if (this.events[header.event]) {
162
- this.events[header.event](buffer);
163
- }
164
- if (this._wildcardHandler) {
165
- this._wildcardHandler({ event: header.event, data: buffer, buffer, isBinary: true });
166
- }
167
- return;
57
+ this.ws.send(JSON.stringify(payload));
168
58
  }
169
-
170
- const msg = JSON.parse(e.data);
171
- const { event, data, _isAck } = msg;
172
-
173
- if (event === 'ping') return;
174
-
175
- if (_isAck && this._acks[event]) {
176
- this._acks[event](data);
177
- return;
59
+ return this;
60
+ }
61
+ emitBinary(event, data) {
62
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
63
+ const header = JSON.stringify({ event });
64
+ const headerBytes = new TextEncoder().encode(header);
65
+ const combined = new Uint8Array(headerBytes.length + 1 + data.byteLength);
66
+ combined.set(headerBytes, 0);
67
+ combined[headerBytes.length] = 0;
68
+ combined.set(new Uint8Array(data), headerBytes.length + 1);
69
+ this.ws.send(combined);
178
70
  }
179
-
180
- if (this.events[event]) this.events[event](data);
181
-
182
- if (this._wildcardHandler) {
183
- this._wildcardHandler({ event, data });
71
+ return this;
72
+ }
73
+ sendFile(file) {
74
+ return this.emitBinary('file', file);
75
+ }
76
+ sendImage(blob) {
77
+ return this.emitBinary('image', blob);
78
+ }
79
+ request(event, data, ackName) {
80
+ return new Promise((resolve, reject) => {
81
+ const timeout = setTimeout(() => {
82
+ reject(new Error(`ACK '${ackName}' timeout`));
83
+ }, this.options.ackTimeout);
84
+ const handler = (responseData) => {
85
+ clearTimeout(timeout);
86
+ this._acks.delete(ackName);
87
+ resolve(responseData);
88
+ };
89
+ this._acks.set(ackName, handler);
90
+ this.emit(event, data, { ack: ackName });
91
+ });
92
+ }
93
+ joinRoom(room) {
94
+ this.emit('join-room', room);
95
+ return this;
96
+ }
97
+ leaveRoom() {
98
+ this.emit('leave-room', {});
99
+ return this;
100
+ }
101
+ startHeartbeat() {
102
+ this._hbTimer = setInterval(() => {
103
+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {
104
+ this.emit('pong', Date.now());
105
+ }
106
+ }, this.options.heartbeatInterval);
107
+ }
108
+ stopHeartbeat() {
109
+ if (this._hbTimer) {
110
+ clearInterval(this._hbTimer);
111
+ this._hbTimer = null;
184
112
  }
185
- } catch (err) {}
186
- };
187
-
188
- this.ws.onclose = () => {
189
- this.connected = false;
190
- this._stopHeartbeat();
191
- if (this.events['disconnect']) this.events['disconnect']();
192
-
193
- if (!this._isManualClose && this.options.reconnection && this._reconnectAttempts < this.options.reconnectionAttempts) {
194
- this._reconnectAttempts++;
195
- if (this.events['reconnecting']) this.events['reconnecting'](this._reconnectAttempts);
196
- setTimeout(() => this._connect(), this.options.reconnectionDelay * this._reconnectAttempts);
197
- }
198
- };
199
-
200
- this.ws.onerror = (err) => {
201
- if (this.events['error']) this.events['error'](err);
202
- };
203
-
204
- return this;
205
- }
206
-
207
- connect(callback) {
208
- this._connect();
209
- if (callback) {
210
- const checkConnection = setInterval(() => {
211
- if (this.connected) {
212
- clearInterval(checkConnection);
213
- callback();
113
+ }
114
+ _connect() {
115
+ this._isManualClose = false;
116
+ this.ws = new WebSocket(this.url);
117
+ this.ws.onopen = () => {
118
+ this.connected = true;
119
+ this._reconnectAttempts = 0;
120
+ const handler = this.events.get('connect');
121
+ if (handler)
122
+ handler(undefined);
123
+ this.startHeartbeat();
124
+ };
125
+ this.ws.binaryType = 'arraybuffer';
126
+ this.ws.onmessage = (e) => {
127
+ try {
128
+ if (e.data instanceof ArrayBuffer) {
129
+ const view = new Uint8Array(e.data);
130
+ let headerEnd = -1;
131
+ for (let i = 0; i < view.length; i++) {
132
+ if (view[i] === 0) {
133
+ headerEnd = i;
134
+ break;
135
+ }
136
+ }
137
+ if (headerEnd === -1)
138
+ return;
139
+ const headerStr = new TextDecoder().decode(view.slice(0, headerEnd));
140
+ const header = JSON.parse(headerStr);
141
+ const buffer = view.slice(headerEnd + 1);
142
+ const handler = this.events.get(header.event);
143
+ if (handler) {
144
+ handler(buffer.buffer);
145
+ }
146
+ else if (this._wildcardHandler) {
147
+ this._wildcardHandler({ event: header.event, data: buffer.buffer, isBinary: true, buffer: buffer.buffer });
148
+ }
149
+ return;
150
+ }
151
+ const msg = JSON.parse(e.data);
152
+ const { event, data, _isAck } = msg;
153
+ if (event === 'ping')
154
+ return;
155
+ if (_isAck && this._acks.has(event)) {
156
+ const handler = this._acks.get(event);
157
+ handler(data);
158
+ return;
159
+ }
160
+ const handler = this.events.get(event);
161
+ if (handler)
162
+ handler(data);
163
+ if (this._wildcardHandler) {
164
+ this._wildcardHandler({ event, data });
165
+ }
166
+ }
167
+ catch { }
168
+ };
169
+ this.ws.onclose = () => {
170
+ this.connected = false;
171
+ this.stopHeartbeat();
172
+ const handler = this.events.get('disconnect');
173
+ if (handler)
174
+ handler(undefined);
175
+ if (!this._isManualClose && this.options.reconnection && this._reconnectAttempts < this.options.reconnectionAttempts) {
176
+ this._reconnectAttempts++;
177
+ const reconHandler = this.events.get('reconnecting');
178
+ if (reconHandler)
179
+ reconHandler(this._reconnectAttempts);
180
+ setTimeout(() => this.connect(), this.options.reconnectionDelay * this._reconnectAttempts);
181
+ }
182
+ };
183
+ this.ws.onerror = (err) => {
184
+ const handler = this.events.get('error');
185
+ if (handler)
186
+ handler(err);
187
+ };
188
+ }
189
+ connect(callback) {
190
+ this._connect();
191
+ if (callback) {
192
+ const checkConnection = setInterval(() => {
193
+ if (this.connected) {
194
+ clearInterval(checkConnection);
195
+ callback();
196
+ }
197
+ }, 100);
214
198
  }
215
- }, 100);
216
- }
217
- return this;
218
- }
219
-
220
- disconnect() {
221
- this._isManualClose = true;
222
- this._stopHeartbeat();
223
- if (this.ws) this.ws.close();
224
- return this;
225
- }
226
-
227
- isConnected() {
228
- return this.connected;
229
- }
230
-
231
- getUrl() {
232
- return this.url;
233
- }
199
+ return this;
200
+ }
201
+ disconnect() {
202
+ this._isManualClose = true;
203
+ this.stopHeartbeat();
204
+ if (this.ws)
205
+ this.ws.close();
206
+ return this;
207
+ }
208
+ isConnected() {
209
+ return this.connected;
210
+ }
211
+ getUrl() {
212
+ return this.url;
213
+ }
234
214
  }
235
-
236
- if (typeof window !== 'undefined') window.StelarClient = StelarClient;
237
- export default StelarClient;
215
+ export default StelarClient;