apache-iggy 0.6.0 → 0.7.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/client/client.connection.d.ts +49 -0
- package/dist/client/client.connection.js +79 -0
- package/dist/client/client.d.ts +46 -0
- package/dist/client/client.debug.d.ts +4 -0
- package/dist/client/client.debug.js +4 -0
- package/dist/client/client.js +65 -1
- package/dist/client/client.socket.d.ts +91 -0
- package/dist/client/client.socket.js +94 -0
- package/dist/client/client.type.d.ts +72 -0
- package/dist/client/client.type.js +3 -0
- package/dist/client/client.utils.d.ts +28 -0
- package/dist/client/client.utils.js +29 -0
- package/dist/e2e/tcp.cluster.e2e.js +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/tcp.sm.utils.d.ts +31 -42
- package/dist/tcp.sm.utils.js +38 -22
- package/dist/type.utils.d.ts +14 -0
- package/dist/type.utils.js +9 -0
- package/dist/wire/command-set.d.ts +2 -9
- package/dist/wire/command.utils.d.ts +18 -0
- package/dist/wire/command.utils.js +9 -0
- package/dist/wire/consumer-group/create-group.command.d.ts +13 -0
- package/dist/wire/consumer-group/create-group.command.js +7 -0
- package/dist/wire/consumer-group/delete-group.command.d.ts +13 -0
- package/dist/wire/consumer-group/delete-group.command.js +7 -0
- package/dist/wire/consumer-group/ensure-group.virtual.command.d.ts +15 -1
- package/dist/wire/consumer-group/ensure-group.virtual.command.js +14 -0
- package/dist/wire/consumer-group/get-group.command.d.ts +13 -0
- package/dist/wire/consumer-group/get-group.command.js +7 -0
- package/dist/wire/consumer-group/get-groups.command.d.ts +12 -0
- package/dist/wire/consumer-group/get-groups.command.js +7 -0
- package/dist/wire/consumer-group/group.utils.d.ts +34 -0
- package/dist/wire/consumer-group/group.utils.js +22 -0
- package/dist/wire/consumer-group/join-group.command.d.ts +13 -0
- package/dist/wire/consumer-group/join-group.command.js +7 -0
- package/dist/wire/consumer-group/leave-group.command.d.ts +13 -0
- package/dist/wire/consumer-group/leave-group.command.js +7 -0
- package/dist/wire/identifier.utils.d.ts +11 -0
- package/dist/wire/identifier.utils.js +23 -1
- package/dist/wire/message/flush-unsaved-buffers.command.d.ts +13 -0
- package/dist/wire/message/flush-unsaved-buffers.command.js +7 -0
- package/dist/wire/message/header.type.d.ts +57 -15
- package/dist/wire/message/header.type.js +31 -18
- package/dist/wire/message/header.utils.d.ts +104 -10
- package/dist/wire/message/header.utils.js +233 -86
- package/dist/wire/message/header.utils.test.js +39 -16
- package/dist/wire/message/iggy-header.utils.d.ts +39 -1
- package/dist/wire/message/iggy-header.utils.js +30 -3
- package/dist/wire/message/index.d.ts +5 -5
- package/dist/wire/message/index.js +5 -5
- package/dist/wire/message/message.utils.d.ts +60 -0
- package/dist/wire/message/message.utils.js +54 -1
- package/dist/wire/message/partitioning.utils.d.ts +39 -0
- package/dist/wire/message/partitioning.utils.js +43 -1
- package/dist/wire/message/poll-messages.command.d.ts +17 -0
- package/dist/wire/message/poll-messages.command.js +7 -0
- package/dist/wire/message/poll.utils.d.ts +93 -12
- package/dist/wire/message/poll.utils.js +92 -21
- package/dist/wire/message/send-messages.command.d.ts +14 -0
- package/dist/wire/message/send-messages.command.js +7 -0
- package/dist/wire/message/send-messages.command.test.js +103 -39
- package/dist/wire/number.utils.d.ts +79 -0
- package/dist/wire/number.utils.js +79 -2
- package/dist/wire/offset/delete-offset.command.d.ts +13 -3
- package/dist/wire/offset/delete-offset.command.js +7 -0
- package/dist/wire/offset/get-offset.command.d.ts +14 -0
- package/dist/wire/offset/get-offset.command.js +7 -0
- package/dist/wire/offset/offset.utils.d.ts +48 -0
- package/dist/wire/offset/offset.utils.js +33 -0
- package/dist/wire/offset/store-offset.command.d.ts +15 -0
- package/dist/wire/offset/store-offset.command.js +7 -0
- package/dist/wire/partition/create-partition.command.d.ts +13 -0
- package/dist/wire/partition/create-partition.command.js +7 -0
- package/dist/wire/partition/delete-partition.command.d.ts +13 -0
- package/dist/wire/partition/delete-partition.command.js +7 -0
- package/dist/wire/partition/partition.utils.d.ts +9 -0
- package/dist/wire/partition/partition.utils.js +9 -0
- package/dist/wire/serialize.utils.d.ts +20 -0
- package/dist/wire/serialize.utils.js +20 -0
- package/dist/wire/session/login-with-token.command.d.ts +11 -0
- package/dist/wire/session/login-with-token.command.js +7 -0
- package/dist/wire/session/login.command.d.ts +7 -0
- package/dist/wire/session/login.command.js +7 -0
- package/dist/wire/session/login.utils.d.ts +25 -0
- package/dist/wire/session/login.utils.js +14 -0
- package/dist/wire/session/logout.command.d.ts +7 -0
- package/dist/wire/session/logout.command.js +7 -0
- package/dist/wire/stream/create-stream.command.d.ts +12 -0
- package/dist/wire/stream/create-stream.command.js +7 -0
- package/dist/wire/stream/delete-stream.command.d.ts +11 -0
- package/dist/wire/stream/delete-stream.command.js +7 -0
- package/dist/wire/stream/ensure-stream.virtual.command.d.ts +8 -1
- package/dist/wire/stream/ensure-stream.virtual.command.js +7 -0
- package/dist/wire/stream/get-stream.command.d.ts +11 -0
- package/dist/wire/stream/get-stream.command.js +7 -0
- package/dist/wire/stream/get-streams.command.d.ts +7 -0
- package/dist/wire/stream/get-streams.command.js +7 -0
- package/dist/wire/stream/purge-stream.command.d.ts +11 -0
- package/dist/wire/stream/purge-stream.command.js +7 -0
- package/dist/wire/stream/stream.utils.d.ts +22 -0
- package/dist/wire/stream/stream.utils.js +8 -0
- package/dist/wire/stream/update-stream.command.d.ts +12 -0
- package/dist/wire/stream/update-stream.command.js +7 -0
- package/dist/wire/token/create-token.command.d.ts +12 -0
- package/dist/wire/token/create-token.command.js +7 -0
- package/dist/wire/token/delete-token.command.d.ts +11 -0
- package/dist/wire/token/delete-token.command.js +7 -0
- package/dist/wire/token/get-tokens.command.d.ts +7 -0
- package/dist/wire/token/get-tokens.command.js +7 -0
- package/dist/wire/token/token.utils.d.ts +40 -0
- package/dist/wire/token/token.utils.js +21 -0
- package/dist/wire/topic/create-topic.command.d.ts +17 -0
- package/dist/wire/topic/create-topic.command.js +7 -0
- package/dist/wire/topic/delete-topic.command.d.ts +14 -2
- package/dist/wire/topic/delete-topic.command.js +7 -0
- package/dist/wire/topic/ensure-topic.virtual.command.d.ts +9 -2
- package/dist/wire/topic/ensure-topic.virtual.command.js +7 -0
- package/dist/wire/topic/get-topic.command.d.ts +13 -2
- package/dist/wire/topic/get-topic.command.js +7 -0
- package/dist/wire/topic/get-topics.command.d.ts +11 -0
- package/dist/wire/topic/get-topics.command.js +7 -0
- package/dist/wire/topic/purge-topic.command.d.ts +12 -0
- package/dist/wire/topic/purge-topic.command.js +7 -0
- package/dist/wire/topic/topic.utils.d.ts +73 -0
- package/dist/wire/topic/topic.utils.js +40 -0
- package/dist/wire/topic/update-topic.command.d.ts +17 -0
- package/dist/wire/topic/update-topic.command.js +7 -0
- package/dist/wire/user/change-password.command.d.ts +13 -0
- package/dist/wire/user/change-password.command.js +7 -0
- package/dist/wire/user/create-user.command.d.ts +14 -0
- package/dist/wire/user/create-user.command.js +7 -0
- package/dist/wire/user/delete-user.command.d.ts +11 -0
- package/dist/wire/user/delete-user.command.js +7 -0
- package/dist/wire/user/get-user.command.d.ts +11 -0
- package/dist/wire/user/get-user.command.js +7 -0
- package/dist/wire/user/get-users.command.d.ts +7 -0
- package/dist/wire/user/get-users.command.js +7 -0
- package/dist/wire/user/permissions.utils.d.ts +108 -0
- package/dist/wire/user/permissions.utils.js +68 -0
- package/dist/wire/user/update-permissions.command.d.ts +12 -0
- package/dist/wire/user/update-permissions.command.js +7 -0
- package/dist/wire/user/update-user.command.d.ts +13 -0
- package/dist/wire/user/update-user.command.js +7 -0
- package/dist/wire/user/user.utils.d.ts +40 -0
- package/dist/wire/user/user.utils.js +33 -0
- package/dist/wire/uuid.utils.d.ts +13 -0
- package/dist/wire/uuid.utils.js +13 -1
- package/package.json +2 -2
|
@@ -19,22 +19,71 @@
|
|
|
19
19
|
import { EventEmitter } from 'node:events';
|
|
20
20
|
import type { Socket } from 'node:net';
|
|
21
21
|
import type { ClientConfig } from "./client.type.js";
|
|
22
|
+
/**
|
|
23
|
+
* Manages the low-level TCP/TLS connection to the Iggy server.
|
|
24
|
+
* Handles connection lifecycle, reconnection, and data buffering.
|
|
25
|
+
*/
|
|
22
26
|
export declare class IggyConnection extends EventEmitter {
|
|
27
|
+
/** Client configuration */
|
|
23
28
|
config: ClientConfig;
|
|
29
|
+
/** Underlying socket connection */
|
|
24
30
|
socket: Socket;
|
|
31
|
+
/** Whether the connection is established */
|
|
25
32
|
connected: boolean;
|
|
33
|
+
/** Whether a connection attempt is in progress */
|
|
26
34
|
connecting: boolean;
|
|
35
|
+
/** Whether the connection is being intentionally closed */
|
|
27
36
|
ending: boolean;
|
|
37
|
+
/** Whether waiting for more data to complete a response */
|
|
28
38
|
private waitingResponseEnd;
|
|
39
|
+
/** Reconnection configuration */
|
|
29
40
|
private reconnectOption;
|
|
41
|
+
/** Number of reconnection attempts made */
|
|
30
42
|
private reconnectCount;
|
|
43
|
+
/** Buffer for incomplete response data */
|
|
31
44
|
private readBuffers;
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new IggyConnection.
|
|
47
|
+
*
|
|
48
|
+
* @param config - Client configuration
|
|
49
|
+
*/
|
|
32
50
|
constructor(config: ClientConfig);
|
|
51
|
+
/**
|
|
52
|
+
* Establishes the connection to the server.
|
|
53
|
+
* Sets up event handlers for data, errors, and disconnection.
|
|
54
|
+
*
|
|
55
|
+
* @returns Promise that resolves when connected
|
|
56
|
+
*/
|
|
33
57
|
connect(): Promise<unknown>;
|
|
58
|
+
/**
|
|
59
|
+
* Attempts to reconnect to the server.
|
|
60
|
+
* Respects maxRetries limit and emits error when exceeded.
|
|
61
|
+
*
|
|
62
|
+
* @param err - Optional error that triggered the reconnection
|
|
63
|
+
*/
|
|
34
64
|
reconnect(err?: Error): Promise<boolean | undefined>;
|
|
65
|
+
/**
|
|
66
|
+
* Destroys the connection and marks it as ending.
|
|
67
|
+
*/
|
|
35
68
|
_destroy(): void;
|
|
69
|
+
/**
|
|
70
|
+
* Clears the response buffer and resets the waiting state.
|
|
71
|
+
*/
|
|
36
72
|
_endResponseWait(): void;
|
|
73
|
+
/**
|
|
74
|
+
* Handles incoming data from the socket.
|
|
75
|
+
* Buffers incomplete responses and emits complete ones.
|
|
76
|
+
*
|
|
77
|
+
* @param data - Incoming data buffer
|
|
78
|
+
*/
|
|
37
79
|
_onData(data: Buffer): void;
|
|
80
|
+
/**
|
|
81
|
+
* Writes a command to the socket.
|
|
82
|
+
*
|
|
83
|
+
* @param command - Command code
|
|
84
|
+
* @param payload - Command payload
|
|
85
|
+
* @returns True if the write was successful
|
|
86
|
+
*/
|
|
38
87
|
writeCommand(command: number, payload: Buffer): boolean;
|
|
39
88
|
}
|
|
40
89
|
//# sourceMappingURL=client.connection.d.ts.map
|
|
@@ -21,13 +21,31 @@ import { createConnection } from 'node:net';
|
|
|
21
21
|
import { connect as TLSConnect } from 'node:tls';
|
|
22
22
|
import { serializeCommand } from './client.utils.js';
|
|
23
23
|
import { debug } from './client.debug.js';
|
|
24
|
+
/**
|
|
25
|
+
* Creates a TCP socket connection.
|
|
26
|
+
*
|
|
27
|
+
* @param options - TCP connection options
|
|
28
|
+
* @returns TCP socket
|
|
29
|
+
*/
|
|
24
30
|
const createTcpSocket = (options) => {
|
|
25
31
|
return createConnection(options);
|
|
26
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* Creates a TLS socket connection.
|
|
35
|
+
*
|
|
36
|
+
* @param options - TLS connection options including port
|
|
37
|
+
* @returns TLS socket
|
|
38
|
+
*/
|
|
27
39
|
const createTlsSocket = ({ port, ...options }) => {
|
|
28
40
|
const socket = TLSConnect(port, options);
|
|
29
41
|
return socket;
|
|
30
42
|
};
|
|
43
|
+
/**
|
|
44
|
+
* Creates a socket based on the transport type in the configuration.
|
|
45
|
+
*
|
|
46
|
+
* @param config - Client configuration with transport type
|
|
47
|
+
* @returns Socket for the specified transport
|
|
48
|
+
*/
|
|
31
49
|
const getTransport = (config) => {
|
|
32
50
|
const { transport, options } = config;
|
|
33
51
|
switch (transport) {
|
|
@@ -37,11 +55,23 @@ const getTransport = (config) => {
|
|
|
37
55
|
return createTcpSocket(options);
|
|
38
56
|
}
|
|
39
57
|
};
|
|
58
|
+
/**
|
|
59
|
+
* Default reconnection settings.
|
|
60
|
+
* Attempts reconnection every 5 seconds, up to 12 times.
|
|
61
|
+
*/
|
|
40
62
|
const DefaultReconnectOption = {
|
|
41
63
|
enabled: true,
|
|
42
64
|
interval: 5 * 1000,
|
|
43
65
|
maxRetries: 12
|
|
44
66
|
};
|
|
67
|
+
/**
|
|
68
|
+
* Recreates a socket after a delay.
|
|
69
|
+
* Used for reconnection attempts.
|
|
70
|
+
*
|
|
71
|
+
* @param option - Client configuration
|
|
72
|
+
* @param timer - Delay in milliseconds before recreating
|
|
73
|
+
* @returns Promise resolving to a new socket
|
|
74
|
+
*/
|
|
45
75
|
function recreate(option, timer = 1000) {
|
|
46
76
|
return new Promise((resolve) => {
|
|
47
77
|
setTimeout(() => {
|
|
@@ -49,16 +79,34 @@ function recreate(option, timer = 1000) {
|
|
|
49
79
|
}, timer);
|
|
50
80
|
});
|
|
51
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* Manages the low-level TCP/TLS connection to the Iggy server.
|
|
84
|
+
* Handles connection lifecycle, reconnection, and data buffering.
|
|
85
|
+
*/
|
|
52
86
|
export class IggyConnection extends EventEmitter {
|
|
87
|
+
/** Client configuration */
|
|
53
88
|
config;
|
|
89
|
+
/** Underlying socket connection */
|
|
54
90
|
socket;
|
|
91
|
+
/** Whether the connection is established */
|
|
55
92
|
connected;
|
|
93
|
+
/** Whether a connection attempt is in progress */
|
|
56
94
|
connecting;
|
|
95
|
+
/** Whether the connection is being intentionally closed */
|
|
57
96
|
ending;
|
|
97
|
+
/** Whether waiting for more data to complete a response */
|
|
58
98
|
waitingResponseEnd;
|
|
99
|
+
/** Reconnection configuration */
|
|
59
100
|
reconnectOption;
|
|
101
|
+
/** Number of reconnection attempts made */
|
|
60
102
|
reconnectCount;
|
|
103
|
+
/** Buffer for incomplete response data */
|
|
61
104
|
readBuffers;
|
|
105
|
+
/**
|
|
106
|
+
* Creates a new IggyConnection.
|
|
107
|
+
*
|
|
108
|
+
* @param config - Client configuration
|
|
109
|
+
*/
|
|
62
110
|
constructor(config) {
|
|
63
111
|
super();
|
|
64
112
|
this.config = config;
|
|
@@ -71,6 +119,12 @@ export class IggyConnection extends EventEmitter {
|
|
|
71
119
|
this.reconnectCount = 0;
|
|
72
120
|
this.readBuffers = Buffer.allocUnsafe(0);
|
|
73
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Establishes the connection to the server.
|
|
124
|
+
* Sets up event handlers for data, errors, and disconnection.
|
|
125
|
+
*
|
|
126
|
+
* @returns Promise that resolves when connected
|
|
127
|
+
*/
|
|
74
128
|
connect() {
|
|
75
129
|
this.connecting = true;
|
|
76
130
|
this.socket.on('data', this._onData.bind(this));
|
|
@@ -98,6 +152,12 @@ export class IggyConnection extends EventEmitter {
|
|
|
98
152
|
});
|
|
99
153
|
});
|
|
100
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Attempts to reconnect to the server.
|
|
157
|
+
* Respects maxRetries limit and emits error when exceeded.
|
|
158
|
+
*
|
|
159
|
+
* @param err - Optional error that triggered the reconnection
|
|
160
|
+
*/
|
|
101
161
|
async reconnect(err) {
|
|
102
162
|
const { enabled, interval, maxRetries } = this.reconnectOption;
|
|
103
163
|
debug('reconnect# event/reconnect?', {
|
|
@@ -115,14 +175,26 @@ export class IggyConnection extends EventEmitter {
|
|
|
115
175
|
this.socket = await recreate(this.config, interval);
|
|
116
176
|
this.connect();
|
|
117
177
|
}
|
|
178
|
+
/**
|
|
179
|
+
* Destroys the connection and marks it as ending.
|
|
180
|
+
*/
|
|
118
181
|
_destroy() {
|
|
119
182
|
this.ending = true;
|
|
120
183
|
this.socket.destroy();
|
|
121
184
|
}
|
|
185
|
+
/**
|
|
186
|
+
* Clears the response buffer and resets the waiting state.
|
|
187
|
+
*/
|
|
122
188
|
_endResponseWait() {
|
|
123
189
|
this.readBuffers = Buffer.allocUnsafe(0);
|
|
124
190
|
this.waitingResponseEnd = false;
|
|
125
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Handles incoming data from the socket.
|
|
194
|
+
* Buffers incomplete responses and emits complete ones.
|
|
195
|
+
*
|
|
196
|
+
* @param data - Incoming data buffer
|
|
197
|
+
*/
|
|
126
198
|
_onData(data) {
|
|
127
199
|
debug('ONDATA', typeof data, Buffer.isBuffer(data), data?.length, this.waitingResponseEnd);
|
|
128
200
|
// Append new data to any buffered data
|
|
@@ -159,6 +231,13 @@ export class IggyConnection extends EventEmitter {
|
|
|
159
231
|
// All data processed, reset buffers
|
|
160
232
|
this._endResponseWait();
|
|
161
233
|
}
|
|
234
|
+
/**
|
|
235
|
+
* Writes a command to the socket.
|
|
236
|
+
*
|
|
237
|
+
* @param command - Command code
|
|
238
|
+
* @param payload - Command payload
|
|
239
|
+
* @returns True if the write was successful
|
|
240
|
+
*/
|
|
162
241
|
writeCommand(command, payload) {
|
|
163
242
|
const cmd = serializeCommand(command, payload);
|
|
164
243
|
return this.socket.write(cmd);
|
package/dist/client/client.d.ts
CHANGED
|
@@ -19,20 +19,66 @@
|
|
|
19
19
|
import { type Pool } from 'generic-pool';
|
|
20
20
|
import type { RawClient, ClientConfig } from "./client.type.js";
|
|
21
21
|
import { CommandAPI } from '../wire/command-set.js';
|
|
22
|
+
/**
|
|
23
|
+
* Iggy client with connection pooling support.
|
|
24
|
+
* Manages a pool of connections for efficient resource utilization.
|
|
25
|
+
*/
|
|
22
26
|
export declare class Client extends CommandAPI {
|
|
27
|
+
/** Client configuration */
|
|
23
28
|
_config: ClientConfig;
|
|
29
|
+
/** Connection pool instance */
|
|
24
30
|
_pool: Pool<RawClient>;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new pooled client.
|
|
33
|
+
*
|
|
34
|
+
* @param config - Client configuration
|
|
35
|
+
*/
|
|
25
36
|
constructor(config: ClientConfig);
|
|
37
|
+
/**
|
|
38
|
+
* Destroys the client and drains all connections from the pool.
|
|
39
|
+
*/
|
|
26
40
|
destroy(): Promise<void>;
|
|
27
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Iggy client that uses a single persistent connection.
|
|
44
|
+
* Suitable for applications that don't need connection pooling.
|
|
45
|
+
*/
|
|
28
46
|
export declare class SingleClient extends CommandAPI {
|
|
47
|
+
/** Client configuration */
|
|
29
48
|
_config: ClientConfig;
|
|
49
|
+
/**
|
|
50
|
+
* Creates a new single-connection client.
|
|
51
|
+
*
|
|
52
|
+
* @param config - Client configuration
|
|
53
|
+
*/
|
|
30
54
|
constructor(config: ClientConfig);
|
|
55
|
+
/**
|
|
56
|
+
* Destroys the client connection.
|
|
57
|
+
*/
|
|
31
58
|
destroy(): Promise<void>;
|
|
32
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Simple Iggy client wrapper around an existing RawClient.
|
|
62
|
+
* Useful when you already have a RawClient instance.
|
|
63
|
+
*/
|
|
33
64
|
export declare class SimpleClient extends CommandAPI {
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new simple client from an existing RawClient.
|
|
67
|
+
*
|
|
68
|
+
* @param client - Existing RawClient instance
|
|
69
|
+
*/
|
|
34
70
|
constructor(client: RawClient);
|
|
71
|
+
/**
|
|
72
|
+
* Destroys the underlying client connection.
|
|
73
|
+
*/
|
|
35
74
|
destroy(): Promise<void>;
|
|
36
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Creates a SimpleClient with the given configuration.
|
|
78
|
+
* Convenience function for quickly creating a client.
|
|
79
|
+
*
|
|
80
|
+
* @param config - Client configuration
|
|
81
|
+
* @returns SimpleClient instance
|
|
82
|
+
*/
|
|
37
83
|
export declare const getClient: (config: ClientConfig) => Promise<SimpleClient>;
|
|
38
84
|
//# sourceMappingURL=client.d.ts.map
|
|
@@ -17,5 +17,9 @@
|
|
|
17
17
|
* under the License.
|
|
18
18
|
*/
|
|
19
19
|
import Debug from 'debug';
|
|
20
|
+
/**
|
|
21
|
+
* Debug logger for the Iggy client.
|
|
22
|
+
* Enable with DEBUG=iggy:client environment variable.
|
|
23
|
+
*/
|
|
20
24
|
export declare const debug: Debug.Debugger;
|
|
21
25
|
//# sourceMappingURL=client.debug.d.ts.map
|
package/dist/client/client.js
CHANGED
|
@@ -20,7 +20,12 @@ import { createPool } from 'generic-pool';
|
|
|
20
20
|
import { getRawClient } from '../client/client.socket.js';
|
|
21
21
|
import { CommandAPI } from '../wire/command-set.js';
|
|
22
22
|
import { debug } from './client.debug.js';
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Creates a pool factory for managing RawClient instances.
|
|
25
|
+
*
|
|
26
|
+
* @param config - Client configuration
|
|
27
|
+
* @returns Pool factory with create and destroy methods
|
|
28
|
+
*/
|
|
24
29
|
const createPoolFactory = (config) => ({
|
|
25
30
|
create: async function () {
|
|
26
31
|
return getRawClient(config);
|
|
@@ -29,6 +34,13 @@ const createPoolFactory = (config) => ({
|
|
|
29
34
|
return client.destroy();
|
|
30
35
|
}
|
|
31
36
|
});
|
|
37
|
+
/**
|
|
38
|
+
* Creates a client provider that uses connection pooling.
|
|
39
|
+
* Automatically acquires and releases clients from the pool.
|
|
40
|
+
*
|
|
41
|
+
* @param config - Client configuration including pool size options
|
|
42
|
+
* @returns Client provider function with attached pool reference
|
|
43
|
+
*/
|
|
32
44
|
const poolClientProvider = (config) => {
|
|
33
45
|
const min = config.poolSize?.min || 1;
|
|
34
46
|
const max = config.poolSize?.max || 4;
|
|
@@ -45,9 +57,20 @@ const poolClientProvider = (config) => {
|
|
|
45
57
|
poolClientProvider._pool = pool;
|
|
46
58
|
return poolClientProvider;
|
|
47
59
|
};
|
|
60
|
+
/**
|
|
61
|
+
* Iggy client with connection pooling support.
|
|
62
|
+
* Manages a pool of connections for efficient resource utilization.
|
|
63
|
+
*/
|
|
48
64
|
export class Client extends CommandAPI {
|
|
65
|
+
/** Client configuration */
|
|
49
66
|
_config;
|
|
67
|
+
/** Connection pool instance */
|
|
50
68
|
_pool;
|
|
69
|
+
/**
|
|
70
|
+
* Creates a new pooled client.
|
|
71
|
+
*
|
|
72
|
+
* @param config - Client configuration
|
|
73
|
+
*/
|
|
51
74
|
constructor(config) {
|
|
52
75
|
const pcp = poolClientProvider(config);
|
|
53
76
|
super(pcp);
|
|
@@ -55,6 +78,9 @@ export class Client extends CommandAPI {
|
|
|
55
78
|
this._pool = pcp._pool;
|
|
56
79
|
}
|
|
57
80
|
;
|
|
81
|
+
/**
|
|
82
|
+
* Destroys the client and drains all connections from the pool.
|
|
83
|
+
*/
|
|
58
84
|
async destroy() {
|
|
59
85
|
debug('destroying client pool. pool size is', this._pool.size);
|
|
60
86
|
await this._pool.drain();
|
|
@@ -62,34 +88,72 @@ export class Client extends CommandAPI {
|
|
|
62
88
|
debug('destroyed client pool. pool size is', this._pool.size);
|
|
63
89
|
}
|
|
64
90
|
}
|
|
91
|
+
/**
|
|
92
|
+
* Creates a client provider that reuses a single connection.
|
|
93
|
+
*
|
|
94
|
+
* @param config - Client configuration
|
|
95
|
+
* @returns Client provider function that always returns the same client
|
|
96
|
+
*/
|
|
65
97
|
const singleClientProvider = (config) => {
|
|
66
98
|
const c = getRawClient(config);
|
|
67
99
|
return async function singleClientProvider() {
|
|
68
100
|
return c;
|
|
69
101
|
};
|
|
70
102
|
};
|
|
103
|
+
/**
|
|
104
|
+
* Iggy client that uses a single persistent connection.
|
|
105
|
+
* Suitable for applications that don't need connection pooling.
|
|
106
|
+
*/
|
|
71
107
|
export class SingleClient extends CommandAPI {
|
|
108
|
+
/** Client configuration */
|
|
72
109
|
_config;
|
|
110
|
+
/**
|
|
111
|
+
* Creates a new single-connection client.
|
|
112
|
+
*
|
|
113
|
+
* @param config - Client configuration
|
|
114
|
+
*/
|
|
73
115
|
constructor(config) {
|
|
74
116
|
super(singleClientProvider(config));
|
|
75
117
|
this._config = config;
|
|
76
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Destroys the client connection.
|
|
121
|
+
*/
|
|
77
122
|
async destroy() {
|
|
78
123
|
const s = await this.clientProvider();
|
|
79
124
|
s.destroy();
|
|
80
125
|
}
|
|
81
126
|
}
|
|
82
127
|
;
|
|
128
|
+
/**
|
|
129
|
+
* Simple Iggy client wrapper around an existing RawClient.
|
|
130
|
+
* Useful when you already have a RawClient instance.
|
|
131
|
+
*/
|
|
83
132
|
export class SimpleClient extends CommandAPI {
|
|
133
|
+
/**
|
|
134
|
+
* Creates a new simple client from an existing RawClient.
|
|
135
|
+
*
|
|
136
|
+
* @param client - Existing RawClient instance
|
|
137
|
+
*/
|
|
84
138
|
constructor(client) {
|
|
85
139
|
super(() => Promise.resolve(client));
|
|
86
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* Destroys the underlying client connection.
|
|
143
|
+
*/
|
|
87
144
|
async destroy() {
|
|
88
145
|
const s = await this.clientProvider();
|
|
89
146
|
s.destroy();
|
|
90
147
|
}
|
|
91
148
|
}
|
|
92
149
|
;
|
|
150
|
+
/**
|
|
151
|
+
* Creates a SimpleClient with the given configuration.
|
|
152
|
+
* Convenience function for quickly creating a client.
|
|
153
|
+
*
|
|
154
|
+
* @param config - Client configuration
|
|
155
|
+
* @returns SimpleClient instance
|
|
156
|
+
*/
|
|
93
157
|
export const getClient = async (config) => {
|
|
94
158
|
const cli = getRawClient(config);
|
|
95
159
|
return new SimpleClient(cli);
|
|
@@ -18,31 +18,122 @@
|
|
|
18
18
|
*/
|
|
19
19
|
import { EventEmitter } from 'node:events';
|
|
20
20
|
import type { ClientConfig, ClientCredentials, CommandResponse, PasswordCredentials, RawClient, TokenCredentials } from '../client/client.type.js';
|
|
21
|
+
/**
|
|
22
|
+
* Manages command execution and response handling for the Iggy server.
|
|
23
|
+
* Implements command queuing, authentication, and heartbeat functionality.
|
|
24
|
+
*/
|
|
21
25
|
export declare class CommandResponseStream extends EventEmitter {
|
|
26
|
+
/** Client configuration */
|
|
22
27
|
private options;
|
|
28
|
+
/** Underlying connection to the server */
|
|
23
29
|
private connection;
|
|
30
|
+
/** Queue of pending command jobs */
|
|
24
31
|
private _execQueue;
|
|
32
|
+
/** Whether the stream is currently processing a command */
|
|
25
33
|
busy: boolean;
|
|
34
|
+
/** Whether the client has been authenticated */
|
|
26
35
|
isAuthenticated: boolean;
|
|
36
|
+
/** Authenticated user ID */
|
|
27
37
|
userId?: number;
|
|
38
|
+
/** Heartbeat interval timer handle */
|
|
28
39
|
heartbeatIntervalHandler?: NodeJS.Timeout;
|
|
40
|
+
/**
|
|
41
|
+
* Creates a new CommandResponseStream.
|
|
42
|
+
*
|
|
43
|
+
* @param options - Client configuration
|
|
44
|
+
*/
|
|
29
45
|
constructor(options: ClientConfig);
|
|
46
|
+
/**
|
|
47
|
+
* Initializes the stream by setting up heartbeat and connection event handlers.
|
|
48
|
+
*/
|
|
30
49
|
_init(): void;
|
|
50
|
+
/**
|
|
51
|
+
* Sends a command to the server.
|
|
52
|
+
* Automatically handles connection and authentication if needed.
|
|
53
|
+
*
|
|
54
|
+
* @param command - Command code to send
|
|
55
|
+
* @param payload - Command payload buffer
|
|
56
|
+
* @param handleResponse - Whether to parse the response (default: true)
|
|
57
|
+
* @param last - Whether to add to end of queue (default: true)
|
|
58
|
+
* @returns Promise resolving to the command response
|
|
59
|
+
*/
|
|
31
60
|
sendCommand(command: number, payload: Buffer, handleResponse?: boolean, last?: boolean): Promise<CommandResponse>;
|
|
61
|
+
/**
|
|
62
|
+
* Processes queued commands sequentially.
|
|
63
|
+
* Emits 'finishQueue' when all commands are processed.
|
|
64
|
+
*
|
|
65
|
+
* @param handleResponse - Whether to parse responses
|
|
66
|
+
*/
|
|
32
67
|
_processQueue(handleResponse?: boolean): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Processes a single command by writing it to the connection and waiting for response.
|
|
70
|
+
*
|
|
71
|
+
* @param command - Command code
|
|
72
|
+
* @param payload - Command payload
|
|
73
|
+
* @param handleResp - Whether to parse the response
|
|
74
|
+
* @returns Promise resolving to the command response
|
|
75
|
+
*/
|
|
33
76
|
_processNext(command: number, payload: Buffer, handleResp?: boolean): Promise<CommandResponse>;
|
|
77
|
+
/**
|
|
78
|
+
* Fails all queued commands with the given error.
|
|
79
|
+
*
|
|
80
|
+
* @param err - Error to reject all queued commands with
|
|
81
|
+
*/
|
|
34
82
|
_failQueue(err: Error): void;
|
|
83
|
+
/**
|
|
84
|
+
* Authenticates the client with the server.
|
|
85
|
+
*
|
|
86
|
+
* @param creds - Authentication credentials (token or password)
|
|
87
|
+
* @returns True if authentication succeeded
|
|
88
|
+
*/
|
|
35
89
|
authenticate(creds: ClientCredentials): Promise<boolean>;
|
|
90
|
+
/**
|
|
91
|
+
* Authenticates using username and password.
|
|
92
|
+
*
|
|
93
|
+
* @param creds - Password credentials
|
|
94
|
+
* @returns Login response with user ID
|
|
95
|
+
*/
|
|
36
96
|
_authWithPassword(creds: PasswordCredentials): Promise<{
|
|
37
97
|
userId: number;
|
|
38
98
|
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Authenticates using a token.
|
|
101
|
+
*
|
|
102
|
+
* @param creds - Token credentials
|
|
103
|
+
* @returns Login response with user ID
|
|
104
|
+
*/
|
|
39
105
|
_authWithToken(creds: TokenCredentials): Promise<{
|
|
40
106
|
userId: number;
|
|
41
107
|
}>;
|
|
108
|
+
/**
|
|
109
|
+
* Sends a ping command to the server.
|
|
110
|
+
*
|
|
111
|
+
* @returns Ping response
|
|
112
|
+
*/
|
|
42
113
|
ping(): Promise<boolean>;
|
|
114
|
+
/**
|
|
115
|
+
* Starts sending periodic heartbeat pings to keep the connection alive.
|
|
116
|
+
*
|
|
117
|
+
* @param interval - Heartbeat interval in milliseconds
|
|
118
|
+
*/
|
|
43
119
|
heartbeat(interval?: number): void;
|
|
120
|
+
/**
|
|
121
|
+
* Returns the underlying socket as a readable stream.
|
|
122
|
+
*
|
|
123
|
+
* @returns The connection socket
|
|
124
|
+
*/
|
|
44
125
|
getReadStream(): import("net").Socket;
|
|
126
|
+
/**
|
|
127
|
+
* Destroys the stream and cleans up resources.
|
|
128
|
+
* Stops heartbeat and destroys the connection.
|
|
129
|
+
*/
|
|
45
130
|
destroy(): void;
|
|
46
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* Creates a new RawClient instance.
|
|
134
|
+
*
|
|
135
|
+
* @param options - Client configuration
|
|
136
|
+
* @returns RawClient instance
|
|
137
|
+
*/
|
|
47
138
|
export declare function getRawClient(options: ClientConfig): RawClient;
|
|
48
139
|
//# sourceMappingURL=client.socket.d.ts.map
|