@platformatic/kafka 0.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.
Files changed (223) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +270 -0
  3. package/dist/apis/admin/alter-client-quotas.d.ts +33 -0
  4. package/dist/apis/admin/alter-client-quotas.js +64 -0
  5. package/dist/apis/admin/alter-configs.d.ts +26 -0
  6. package/dist/apis/admin/alter-configs.js +57 -0
  7. package/dist/apis/admin/alter-partition-reassignments.d.ts +30 -0
  8. package/dist/apis/admin/alter-partition-reassignments.js +68 -0
  9. package/dist/apis/admin/alter-partition.d.ts +39 -0
  10. package/dist/apis/admin/alter-partition.js +87 -0
  11. package/dist/apis/admin/alter-replica-log-dirs.d.ts +26 -0
  12. package/dist/apis/admin/alter-replica-log-dirs.js +55 -0
  13. package/dist/apis/admin/alter-user-scram-credentials.d.ts +27 -0
  14. package/dist/apis/admin/alter-user-scram-credentials.js +60 -0
  15. package/dist/apis/admin/consumer-group-describe.d.ts +41 -0
  16. package/dist/apis/admin/consumer-group-describe.js +103 -0
  17. package/dist/apis/admin/create-acls.d.ts +24 -0
  18. package/dist/apis/admin/create-acls.js +55 -0
  19. package/dist/apis/admin/create-delegation-token.d.ts +24 -0
  20. package/dist/apis/admin/create-delegation-token.js +54 -0
  21. package/dist/apis/admin/create-partitions.d.ts +24 -0
  22. package/dist/apis/admin/create-partitions.js +54 -0
  23. package/dist/apis/admin/create-topics.d.ts +42 -0
  24. package/dist/apis/admin/create-topics.js +86 -0
  25. package/dist/apis/admin/delete-acls.d.ts +36 -0
  26. package/dist/apis/admin/delete-acls.js +82 -0
  27. package/dist/apis/admin/delete-groups.d.ts +14 -0
  28. package/dist/apis/admin/delete-groups.js +40 -0
  29. package/dist/apis/admin/delete-records.d.ts +27 -0
  30. package/dist/apis/admin/delete-records.js +59 -0
  31. package/dist/apis/admin/delete-topics.d.ts +21 -0
  32. package/dist/apis/admin/delete-topics.js +50 -0
  33. package/dist/apis/admin/describe-acls.d.ts +25 -0
  34. package/dist/apis/admin/describe-acls.js +66 -0
  35. package/dist/apis/admin/describe-client-quotas.d.ts +30 -0
  36. package/dist/apis/admin/describe-client-quotas.js +57 -0
  37. package/dist/apis/admin/describe-cluster.d.ts +23 -0
  38. package/dist/apis/admin/describe-cluster.js +50 -0
  39. package/dist/apis/admin/describe-configs.d.ts +38 -0
  40. package/dist/apis/admin/describe-configs.js +85 -0
  41. package/dist/apis/admin/describe-delegation-token.d.ts +31 -0
  42. package/dist/apis/admin/describe-delegation-token.js +62 -0
  43. package/dist/apis/admin/describe-groups.d.ts +28 -0
  44. package/dist/apis/admin/describe-groups.js +67 -0
  45. package/dist/apis/admin/describe-log-dirs.d.ts +32 -0
  46. package/dist/apis/admin/describe-log-dirs.js +75 -0
  47. package/dist/apis/admin/describe-producers.d.ts +33 -0
  48. package/dist/apis/admin/describe-producers.js +70 -0
  49. package/dist/apis/admin/describe-quorum.d.ts +50 -0
  50. package/dist/apis/admin/describe-quorum.js +116 -0
  51. package/dist/apis/admin/describe-topic-partitions.d.ts +42 -0
  52. package/dist/apis/admin/describe-topic-partitions.js +94 -0
  53. package/dist/apis/admin/describe-transactions.d.ts +24 -0
  54. package/dist/apis/admin/describe-transactions.js +59 -0
  55. package/dist/apis/admin/describe-user-scram-credentials.d.ts +26 -0
  56. package/dist/apis/admin/describe-user-scram-credentials.js +62 -0
  57. package/dist/apis/admin/envelope.d.ts +10 -0
  58. package/dist/apis/admin/envelope.js +32 -0
  59. package/dist/apis/admin/expire-delegation-token.d.ts +11 -0
  60. package/dist/apis/admin/expire-delegation-token.js +29 -0
  61. package/dist/apis/admin/incremental-alter-configs.d.ts +27 -0
  62. package/dist/apis/admin/incremental-alter-configs.js +58 -0
  63. package/dist/apis/admin/index.d.ts +37 -0
  64. package/dist/apis/admin/index.js +37 -0
  65. package/dist/apis/admin/list-groups.d.ts +18 -0
  66. package/dist/apis/admin/list-groups.js +43 -0
  67. package/dist/apis/admin/list-partition-reassignments.d.ts +27 -0
  68. package/dist/apis/admin/list-partition-reassignments.js +56 -0
  69. package/dist/apis/admin/list-transactions.d.ts +18 -0
  70. package/dist/apis/admin/list-transactions.js +45 -0
  71. package/dist/apis/admin/offset-delete.d.ts +26 -0
  72. package/dist/apis/admin/offset-delete.js +59 -0
  73. package/dist/apis/admin/renew-delegation-token.d.ts +11 -0
  74. package/dist/apis/admin/renew-delegation-token.js +29 -0
  75. package/dist/apis/admin/unregister-broker.d.ts +12 -0
  76. package/dist/apis/admin/unregister-broker.js +28 -0
  77. package/dist/apis/admin/update-features.d.ts +23 -0
  78. package/dist/apis/admin/update-features.js +60 -0
  79. package/dist/apis/consumer/consumer-group-heartbeat.d.ts +27 -0
  80. package/dist/apis/consumer/consumer-group-heartbeat.js +70 -0
  81. package/dist/apis/consumer/fetch.d.ts +46 -0
  82. package/dist/apis/consumer/fetch.js +121 -0
  83. package/dist/apis/consumer/heartbeat.d.ts +11 -0
  84. package/dist/apis/consumer/heartbeat.js +34 -0
  85. package/dist/apis/consumer/index.d.ts +9 -0
  86. package/dist/apis/consumer/index.js +9 -0
  87. package/dist/apis/consumer/join-group.d.ts +27 -0
  88. package/dist/apis/consumer/join-group.js +71 -0
  89. package/dist/apis/consumer/leave-group.d.ts +22 -0
  90. package/dist/apis/consumer/leave-group.js +57 -0
  91. package/dist/apis/consumer/list-offsets.d.ts +30 -0
  92. package/dist/apis/consumer/list-offsets.js +68 -0
  93. package/dist/apis/consumer/offset-commit.d.ts +29 -0
  94. package/dist/apis/consumer/offset-commit.js +68 -0
  95. package/dist/apis/consumer/offset-fetch.d.ts +37 -0
  96. package/dist/apis/consumer/offset-fetch.js +81 -0
  97. package/dist/apis/consumer/sync-group.d.ts +18 -0
  98. package/dist/apis/consumer/sync-group.js +49 -0
  99. package/dist/apis/definitions.d.ts +16 -0
  100. package/dist/apis/definitions.js +12 -0
  101. package/dist/apis/enumerations.d.ts +114 -0
  102. package/dist/apis/enumerations.js +78 -0
  103. package/dist/apis/index.d.ts +8 -0
  104. package/dist/apis/index.js +10 -0
  105. package/dist/apis/metadata/api-versions.d.ts +17 -0
  106. package/dist/apis/metadata/api-versions.js +41 -0
  107. package/dist/apis/metadata/find-coordinator.d.ts +19 -0
  108. package/dist/apis/metadata/find-coordinator.js +50 -0
  109. package/dist/apis/metadata/index.d.ts +3 -0
  110. package/dist/apis/metadata/index.js +3 -0
  111. package/dist/apis/metadata/metadata.d.ts +37 -0
  112. package/dist/apis/metadata/metadata.js +92 -0
  113. package/dist/apis/producer/add-offsets-to-txn.d.ts +10 -0
  114. package/dist/apis/producer/add-offsets-to-txn.js +34 -0
  115. package/dist/apis/producer/add-partitions-to-txn.d.ts +34 -0
  116. package/dist/apis/producer/add-partitions-to-txn.js +79 -0
  117. package/dist/apis/producer/end-txn.d.ts +10 -0
  118. package/dist/apis/producer/end-txn.js +34 -0
  119. package/dist/apis/producer/index.d.ts +6 -0
  120. package/dist/apis/producer/index.js +6 -0
  121. package/dist/apis/producer/init-producer-id.d.ts +21 -0
  122. package/dist/apis/producer/init-producer-id.js +38 -0
  123. package/dist/apis/producer/produce.d.ts +29 -0
  124. package/dist/apis/producer/produce.js +104 -0
  125. package/dist/apis/producer/txn-offset-commit.d.ts +29 -0
  126. package/dist/apis/producer/txn-offset-commit.js +77 -0
  127. package/dist/apis/security/index.d.ts +2 -0
  128. package/dist/apis/security/index.js +2 -0
  129. package/dist/apis/security/sasl-authenticate.d.ts +15 -0
  130. package/dist/apis/security/sasl-authenticate.js +30 -0
  131. package/dist/apis/security/sasl-handshake.d.ts +10 -0
  132. package/dist/apis/security/sasl-handshake.js +28 -0
  133. package/dist/apis/telemetry/get-telemetry-subscriptions.d.ts +18 -0
  134. package/dist/apis/telemetry/get-telemetry-subscriptions.js +46 -0
  135. package/dist/apis/telemetry/index.d.ts +3 -0
  136. package/dist/apis/telemetry/index.js +3 -0
  137. package/dist/apis/telemetry/list-client-metrics-resources.d.ts +14 -0
  138. package/dist/apis/telemetry/list-client-metrics-resources.js +32 -0
  139. package/dist/apis/telemetry/push-telemetry.d.ts +10 -0
  140. package/dist/apis/telemetry/push-telemetry.js +36 -0
  141. package/dist/clients/admin/admin.d.ts +18 -0
  142. package/dist/clients/admin/admin.js +322 -0
  143. package/dist/clients/admin/index.d.ts +3 -0
  144. package/dist/clients/admin/index.js +3 -0
  145. package/dist/clients/admin/options.d.ts +135 -0
  146. package/dist/clients/admin/options.js +81 -0
  147. package/dist/clients/admin/types.d.ts +56 -0
  148. package/dist/clients/admin/types.js +1 -0
  149. package/dist/clients/base/base.d.ts +48 -0
  150. package/dist/clients/base/base.js +242 -0
  151. package/dist/clients/base/index.d.ts +3 -0
  152. package/dist/clients/base/index.js +3 -0
  153. package/dist/clients/base/options.d.ts +115 -0
  154. package/dist/clients/base/options.js +59 -0
  155. package/dist/clients/base/types.d.ts +38 -0
  156. package/dist/clients/base/types.js +1 -0
  157. package/dist/clients/callbacks.d.ts +8 -0
  158. package/dist/clients/callbacks.js +42 -0
  159. package/dist/clients/consumer/consumer.d.ts +33 -0
  160. package/dist/clients/consumer/consumer.js +767 -0
  161. package/dist/clients/consumer/index.d.ts +5 -0
  162. package/dist/clients/consumer/index.js +5 -0
  163. package/dist/clients/consumer/messages-stream.d.ts +56 -0
  164. package/dist/clients/consumer/messages-stream.js +404 -0
  165. package/dist/clients/consumer/options.d.ts +521 -0
  166. package/dist/clients/consumer/options.js +177 -0
  167. package/dist/clients/consumer/topics-map.d.ts +8 -0
  168. package/dist/clients/consumer/topics-map.js +48 -0
  169. package/dist/clients/consumer/types.d.ts +74 -0
  170. package/dist/clients/consumer/types.js +11 -0
  171. package/dist/clients/index.d.ts +6 -0
  172. package/dist/clients/index.js +6 -0
  173. package/dist/clients/producer/index.d.ts +3 -0
  174. package/dist/clients/producer/index.js +3 -0
  175. package/dist/clients/producer/options.d.ts +122 -0
  176. package/dist/clients/producer/options.js +47 -0
  177. package/dist/clients/producer/producer.d.ts +13 -0
  178. package/dist/clients/producer/producer.js +272 -0
  179. package/dist/clients/producer/types.d.ts +29 -0
  180. package/dist/clients/producer/types.js +1 -0
  181. package/dist/clients/serde.d.ts +40 -0
  182. package/dist/clients/serde.js +49 -0
  183. package/dist/errors.d.ts +67 -0
  184. package/dist/errors.js +160 -0
  185. package/dist/index.d.ts +6 -0
  186. package/dist/index.js +10 -0
  187. package/dist/network/connection-pool.d.ts +11 -0
  188. package/dist/network/connection-pool.js +101 -0
  189. package/dist/network/connection.d.ts +49 -0
  190. package/dist/network/connection.js +319 -0
  191. package/dist/network/index.d.ts +2 -0
  192. package/dist/network/index.js +2 -0
  193. package/dist/protocol/apis.d.ts +2 -0
  194. package/dist/protocol/apis.js +191 -0
  195. package/dist/protocol/compression.d.ts +70 -0
  196. package/dist/protocol/compression.js +114 -0
  197. package/dist/protocol/crc32c.d.ts +2 -0
  198. package/dist/protocol/crc32c.js +83 -0
  199. package/dist/protocol/definitions.d.ts +12 -0
  200. package/dist/protocol/definitions.js +11 -0
  201. package/dist/protocol/dynamic-buffer.d.ts +65 -0
  202. package/dist/protocol/dynamic-buffer.js +557 -0
  203. package/dist/protocol/errors.d.ts +8 -0
  204. package/dist/protocol/errors.js +908 -0
  205. package/dist/protocol/index.d.ts +14 -0
  206. package/dist/protocol/index.js +14 -0
  207. package/dist/protocol/murmur2.d.ts +1 -0
  208. package/dist/protocol/murmur2.js +55 -0
  209. package/dist/protocol/reader.d.ts +58 -0
  210. package/dist/protocol/reader.js +296 -0
  211. package/dist/protocol/records.d.ts +110 -0
  212. package/dist/protocol/records.js +149 -0
  213. package/dist/protocol/sasl/plain.d.ts +3 -0
  214. package/dist/protocol/sasl/plain.js +3 -0
  215. package/dist/protocol/sasl/scram-sha.d.ts +28 -0
  216. package/dist/protocol/sasl/scram-sha.js +104 -0
  217. package/dist/protocol/varint.d.ts +12 -0
  218. package/dist/protocol/varint.js +36 -0
  219. package/dist/protocol/writer.d.ts +48 -0
  220. package/dist/protocol/writer.js +223 -0
  221. package/dist/utils.d.ts +20 -0
  222. package/dist/utils.js +127 -0
  223. package/package.json +75 -0
@@ -0,0 +1,40 @@
1
+ export type Serializer<InputType = unknown> = (data?: InputType) => Buffer | undefined;
2
+ export type Deserializer<OutputType = unknown> = (data?: Buffer) => OutputType | undefined;
3
+ export interface Serializers<Key, Value, HeaderKey, HeaderValue> {
4
+ key: Serializer<Key>;
5
+ value: Serializer<Value>;
6
+ headerKey: Serializer<HeaderKey>;
7
+ headerValue: Serializer<HeaderValue>;
8
+ }
9
+ export interface Deserializers<Key, Value, HeaderKey, HeaderValue> {
10
+ key: Deserializer<Key>;
11
+ value: Deserializer<Value>;
12
+ headerKey: Deserializer<HeaderKey>;
13
+ headerValue: Deserializer<HeaderValue>;
14
+ }
15
+ export declare function stringSerializer(data?: string): Buffer | undefined;
16
+ export declare function stringDeserializer(data?: string | Buffer): string | undefined;
17
+ export declare function jsonSerializer<T = Record<string, any>>(data?: T): Buffer | undefined;
18
+ export declare function jsonDeserializer<T = Record<string, any>>(data?: string | Buffer): T | undefined;
19
+ export declare function serializersFrom<T>(serializer: Serializer<T>): Serializers<T, T, T, T>;
20
+ export declare function deserializersFrom<T>(deserializer: Deserializer<T>): Deserializers<T, T, T, T>;
21
+ export declare const serdeProperties: {
22
+ type: string;
23
+ properties: {
24
+ key: {
25
+ function: boolean;
26
+ };
27
+ value: {
28
+ function: boolean;
29
+ };
30
+ headerKey: {
31
+ function: boolean;
32
+ };
33
+ headerValue: {
34
+ function: boolean;
35
+ };
36
+ };
37
+ additionalProperties: boolean;
38
+ };
39
+ export declare const stringSerializers: Serializers<string, string, string, string>;
40
+ export declare const stringDeserializers: Deserializers<string, string, string, string>;
@@ -0,0 +1,49 @@
1
+ export function stringSerializer(data) {
2
+ if (typeof data !== 'string') {
3
+ return undefined;
4
+ }
5
+ return Buffer.from(data, 'utf-8');
6
+ }
7
+ export function stringDeserializer(data) {
8
+ if (!Buffer.isBuffer(data)) {
9
+ return undefined;
10
+ }
11
+ return data.toString('utf-8');
12
+ }
13
+ export function jsonSerializer(data) {
14
+ return Buffer.from(JSON.stringify(data), 'utf-8');
15
+ }
16
+ export function jsonDeserializer(data) {
17
+ if (!Buffer.isBuffer(data)) {
18
+ return undefined;
19
+ }
20
+ return JSON.parse(data.toString('utf-8'));
21
+ }
22
+ export function serializersFrom(serializer) {
23
+ return {
24
+ key: serializer,
25
+ value: serializer,
26
+ headerKey: serializer,
27
+ headerValue: serializer
28
+ };
29
+ }
30
+ export function deserializersFrom(deserializer) {
31
+ return {
32
+ key: deserializer,
33
+ value: deserializer,
34
+ headerKey: deserializer,
35
+ headerValue: deserializer
36
+ };
37
+ }
38
+ export const serdeProperties = {
39
+ type: 'object',
40
+ properties: {
41
+ key: { function: true },
42
+ value: { function: true },
43
+ headerKey: { function: true },
44
+ headerValue: { function: true }
45
+ },
46
+ additionalProperties: false
47
+ };
48
+ export const stringSerializers = serializersFrom(stringSerializer);
49
+ export const stringDeserializers = deserializersFrom(stringDeserializer);
@@ -0,0 +1,67 @@
1
+ declare const kGenericError: unique symbol;
2
+ declare const kMultipleErrors: unique symbol;
3
+ export declare const ERROR_PREFIX = "PLT_KFK_";
4
+ export declare const errorCodes: readonly ["PLT_KFK_AUTHENTICATION", "PLT_KFK_MULTIPLE", "PLT_KFK_NETWORK", "PLT_KFK_PROTOCOL", "PLT_KFK_RESPONSE", "PLT_KFK_TIMEOUT", "PLT_KFK_UNEXPECTED_CORRELATION_ID", "PLT_KFK_UNFINISHED_WRITE_BUFFER", "PLT_KFK_UNSUPPORTED_COMPRESSION", "PLT_KFK_UNSUPPORTED", "PLT_KFK_USER"];
5
+ export type ErrorCode = (typeof errorCodes)[number];
6
+ export type ErrorProperties = {
7
+ cause?: Error;
8
+ } & Record<string, any>;
9
+ export declare class GenericError extends Error {
10
+ code: string;
11
+ [index: string]: any;
12
+ [kGenericError]: true;
13
+ static isGenericError(error: Error): boolean;
14
+ constructor(code: ErrorCode, message: string, { cause, ...rest }?: ErrorProperties);
15
+ findBy<ErrorType extends GenericError = GenericError>(property: string, value: unknown): ErrorType | null;
16
+ }
17
+ export declare class MultipleErrors extends AggregateError {
18
+ code: string;
19
+ [index: string]: any;
20
+ [kGenericError]: true;
21
+ [kMultipleErrors]: true;
22
+ static code: ErrorCode;
23
+ static isGenericError(error: Error): boolean;
24
+ static isMultipleErrors(error: Error): boolean;
25
+ constructor(message: string, errors: Error[], { cause, ...rest }?: ErrorProperties);
26
+ findBy<ErrorType extends GenericError | MultipleErrors = MultipleErrors>(property: string, value: unknown): ErrorType | null;
27
+ }
28
+ export * from './protocol/errors.ts';
29
+ export declare class AuthenticationError extends GenericError {
30
+ static code: ErrorCode;
31
+ constructor(message: string, properties?: ErrorProperties);
32
+ }
33
+ export declare class NetworkError extends GenericError {
34
+ static code: ErrorCode;
35
+ constructor(message: string, properties?: ErrorProperties);
36
+ }
37
+ export declare class ProtocolError extends GenericError {
38
+ constructor(codeOrId: string | number, properties?: ErrorProperties, response?: unknown);
39
+ }
40
+ export declare class ResponseError extends MultipleErrors {
41
+ static code: ErrorCode;
42
+ constructor(apiName: number, apiVersion: number, errors: Record<string, number>, response: unknown, properties?: ErrorProperties);
43
+ }
44
+ export declare class TimeoutError extends GenericError {
45
+ static code: ErrorCode;
46
+ constructor(message: string, properties?: ErrorProperties);
47
+ }
48
+ export declare class UnexpectedCorrelationIdError extends GenericError {
49
+ static code: ErrorCode;
50
+ constructor(message: string, properties?: ErrorProperties);
51
+ }
52
+ export declare class UnfinishedWriteBufferError extends GenericError {
53
+ static code: ErrorCode;
54
+ constructor(message: string, properties?: ErrorProperties);
55
+ }
56
+ export declare class UnsupportedCompressionError extends GenericError {
57
+ static code: ErrorCode;
58
+ constructor(message: string, properties?: ErrorProperties);
59
+ }
60
+ export declare class UnsupportedError extends GenericError {
61
+ static code: ErrorCode;
62
+ constructor(message: string, properties?: ErrorProperties);
63
+ }
64
+ export declare class UserError extends GenericError {
65
+ static code: ErrorCode;
66
+ constructor(message: string, properties?: ErrorProperties);
67
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,160 @@
1
+ import { protocolAPIsById } from "./protocol/apis.js";
2
+ import { protocolErrors, protocolErrorsCodesById } from "./protocol/errors.js";
3
+ const kGenericError = Symbol('plt.kafka.genericError');
4
+ const kMultipleErrors = Symbol('plt.kafka.multipleErrors');
5
+ export const ERROR_PREFIX = 'PLT_KFK_';
6
+ export const errorCodes = [
7
+ 'PLT_KFK_AUTHENTICATION',
8
+ 'PLT_KFK_MULTIPLE',
9
+ 'PLT_KFK_NETWORK',
10
+ 'PLT_KFK_PROTOCOL',
11
+ 'PLT_KFK_RESPONSE',
12
+ 'PLT_KFK_TIMEOUT',
13
+ 'PLT_KFK_UNEXPECTED_CORRELATION_ID',
14
+ 'PLT_KFK_UNFINISHED_WRITE_BUFFER',
15
+ 'PLT_KFK_UNSUPPORTED_COMPRESSION',
16
+ 'PLT_KFK_UNSUPPORTED',
17
+ 'PLT_KFK_USER'
18
+ ];
19
+ export class GenericError extends Error {
20
+ code;
21
+ [kGenericError];
22
+ static isGenericError(error) {
23
+ return error[kGenericError] === true;
24
+ }
25
+ constructor(code, message, { cause, ...rest } = {}) {
26
+ super(message, cause ? { cause } : {});
27
+ this.code = code;
28
+ this[kGenericError] = true;
29
+ Reflect.defineProperty(this, 'message', { enumerable: true });
30
+ Reflect.defineProperty(this, 'code', { enumerable: true });
31
+ if ('stack' in this) {
32
+ Reflect.defineProperty(this, 'stack', { enumerable: true });
33
+ }
34
+ for (const [key, value] of Object.entries(rest)) {
35
+ Reflect.defineProperty(this, key, { value, enumerable: true });
36
+ }
37
+ Reflect.defineProperty(this, kGenericError, { value: true, enumerable: false });
38
+ }
39
+ findBy(property, value) {
40
+ if (this[property] === value) {
41
+ return this;
42
+ }
43
+ return null;
44
+ }
45
+ }
46
+ export class MultipleErrors extends AggregateError {
47
+ code;
48
+ [kGenericError];
49
+ [kMultipleErrors];
50
+ static code = 'PLT_KFK_MULTIPLE';
51
+ static isGenericError(error) {
52
+ return error[kGenericError] === true;
53
+ }
54
+ static isMultipleErrors(error) {
55
+ return error[kMultipleErrors] === true;
56
+ }
57
+ constructor(message, errors, { cause, ...rest } = {}) {
58
+ super(errors, message, cause ? { cause } : {});
59
+ this.code = MultipleErrors.code;
60
+ this[kGenericError] = true;
61
+ this[kMultipleErrors] = true;
62
+ Reflect.defineProperty(this, 'message', { enumerable: true });
63
+ Reflect.defineProperty(this, 'code', { enumerable: true });
64
+ if ('stack' in this) {
65
+ Reflect.defineProperty(this, 'stack', { enumerable: true });
66
+ }
67
+ for (const [key, value] of Object.entries(rest)) {
68
+ Reflect.defineProperty(this, key, { value, enumerable: true });
69
+ }
70
+ Reflect.defineProperty(this, kGenericError, { value: true, enumerable: false });
71
+ Reflect.defineProperty(this, kMultipleErrors, { value: true, enumerable: false });
72
+ }
73
+ findBy(property, value) {
74
+ if (this[property] === value) {
75
+ return this;
76
+ }
77
+ for (const error of this.errors) {
78
+ /* c8 ignore next */
79
+ if (error[kGenericError] ? error.findBy(property, value) : error[property] === value) {
80
+ return error;
81
+ }
82
+ }
83
+ return null;
84
+ }
85
+ }
86
+ export * from "./protocol/errors.js";
87
+ export class AuthenticationError extends GenericError {
88
+ static code = 'PLT_KFK_AUTHENTICATION';
89
+ constructor(message, properties = {}) {
90
+ super(AuthenticationError.code, message, properties);
91
+ }
92
+ }
93
+ export class NetworkError extends GenericError {
94
+ static code = 'PLT_KFK_NETWORK';
95
+ constructor(message, properties = {}) {
96
+ super(NetworkError.code, message, properties);
97
+ }
98
+ }
99
+ export class ProtocolError extends GenericError {
100
+ constructor(codeOrId, properties = {}, response = undefined) {
101
+ const { id, code, message, canRetry } = protocolErrors[typeof codeOrId === 'number' ? protocolErrorsCodesById[codeOrId] : codeOrId];
102
+ super('PLT_KFK_PROTOCOL', message, {
103
+ apiId: id,
104
+ apiCode: code,
105
+ canRetry,
106
+ hasStaleMetadata: ['UNKNOWN_TOPIC_OR_PARTITION', 'LEADER_NOT_AVAILABLE', 'NOT_LEADER_OR_FOLLOWER'].includes(id),
107
+ needsRejoin: ['MEMBER_ID_REQUIRED', 'UNKNOWN_MEMBER_ID', 'REBALANCE_IN_PROGRESS'].includes(id),
108
+ rebalanceInProgress: id === 'REBALANCE_IN_PROGRESS',
109
+ unknownMemberId: id === 'UNKNOWN_MEMBER_ID',
110
+ memberId: response?.memberId,
111
+ ...properties
112
+ });
113
+ }
114
+ }
115
+ export class ResponseError extends MultipleErrors {
116
+ static code = 'PLT_KFK_RESPONSE';
117
+ constructor(apiName, apiVersion, errors, response, properties = {}) {
118
+ super(`Received response with error while executing API ${protocolAPIsById[apiName]}(v${apiVersion})`, Object.entries(errors).map(([path, errorCode]) => new ProtocolError(errorCode, { path }, response)), {
119
+ ...properties,
120
+ response
121
+ });
122
+ this.code = ResponseError.code;
123
+ }
124
+ }
125
+ export class TimeoutError extends GenericError {
126
+ static code = 'PLT_KFK_TIMEOUT';
127
+ constructor(message, properties = {}) {
128
+ super(NetworkError.code, message, properties);
129
+ }
130
+ }
131
+ export class UnexpectedCorrelationIdError extends GenericError {
132
+ static code = 'PLT_KFK_UNEXPECTED_CORRELATION_ID';
133
+ constructor(message, properties = {}) {
134
+ super(UnexpectedCorrelationIdError.code, message, properties);
135
+ }
136
+ }
137
+ export class UnfinishedWriteBufferError extends GenericError {
138
+ static code = 'PLT_KFK_UNFINISHED_WRITE_BUFFER';
139
+ constructor(message, properties = {}) {
140
+ super(UnfinishedWriteBufferError.code, message, properties);
141
+ }
142
+ }
143
+ export class UnsupportedCompressionError extends GenericError {
144
+ static code = 'PLT_KFK_UNSUPPORTED_COMPRESSION';
145
+ constructor(message, properties = {}) {
146
+ super(UnsupportedCompressionError.code, message, properties);
147
+ }
148
+ }
149
+ export class UnsupportedError extends GenericError {
150
+ static code = 'PLT_KFK_UNSUPPORTED';
151
+ constructor(message, properties = {}) {
152
+ super(UnsupportedError.code, message, properties);
153
+ }
154
+ }
155
+ export class UserError extends GenericError {
156
+ static code = 'PLT_KFK_USER';
157
+ constructor(message, properties = {}) {
158
+ super(UserError.code, message, properties);
159
+ }
160
+ }
@@ -0,0 +1,6 @@
1
+ export * from './errors.ts';
2
+ export * from './utils.ts';
3
+ export * from './network/index.ts';
4
+ export * from './protocol/index.ts';
5
+ export * from './apis/index.ts';
6
+ export * from './clients/index.ts';
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ // General
2
+ export * from "./errors.js";
3
+ export * from "./utils.js";
4
+ // Networking
5
+ export * from "./network/index.js";
6
+ export * from "./protocol/index.js";
7
+ // APIs
8
+ export * from "./apis/index.js";
9
+ // Clients
10
+ export * from "./clients/index.js";
@@ -0,0 +1,11 @@
1
+ import EventEmitter from 'node:events';
2
+ import { type CallbackWithPromise } from '../clients/callbacks.ts';
3
+ import { Connection, type Broker, type ConnectionOptions } from './connection.ts';
4
+ export declare class ConnectionPool extends EventEmitter {
5
+ #private;
6
+ constructor(clientId: string, connectionOptions?: ConnectionOptions);
7
+ get(broker: Broker, callback: CallbackWithPromise<Connection>): void;
8
+ get(broker: Broker): Promise<Connection>;
9
+ getFirstAvailable(brokers: Broker[], callback?: CallbackWithPromise<Connection>, current?: number, errors?: Error[]): void | Promise<Connection>;
10
+ close(callback?: CallbackWithPromise<void>): void | Promise<void>;
11
+ }
@@ -0,0 +1,101 @@
1
+ import EventEmitter from 'node:events';
2
+ import { createPromisifiedCallback, kCallbackPromise, runConcurrentCallbacks } from "../clients/callbacks.js";
3
+ import { MultipleErrors } from "../errors.js";
4
+ import { Connection, ConnectionStatuses } from "./connection.js";
5
+ export class ConnectionPool extends EventEmitter {
6
+ #clientId;
7
+ // @ts-ignore This is used just for debugging
8
+ #ownerId;
9
+ #connections;
10
+ #connectionOptions;
11
+ constructor(clientId, connectionOptions = {}) {
12
+ super();
13
+ this.#clientId = clientId;
14
+ this.#ownerId = connectionOptions.ownerId;
15
+ this.#connections = new Map();
16
+ this.#connectionOptions = connectionOptions;
17
+ }
18
+ get(broker, callback) {
19
+ const key = `${broker.host}:${broker.port}`;
20
+ const existing = this.#connections.get(key);
21
+ if (!callback) {
22
+ callback = createPromisifiedCallback();
23
+ }
24
+ if (existing) {
25
+ if (existing.status !== ConnectionStatuses.CONNECTED) {
26
+ existing.ready(error => {
27
+ if (error) {
28
+ callback(error, undefined);
29
+ return;
30
+ }
31
+ callback(null, existing);
32
+ });
33
+ }
34
+ else {
35
+ callback(null, existing);
36
+ }
37
+ return callback[kCallbackPromise];
38
+ }
39
+ const connection = new Connection(this.#clientId, this.#connectionOptions);
40
+ this.#connections.set(key, connection);
41
+ const eventPayload = { broker, connection };
42
+ this.emit('connecting', eventPayload);
43
+ connection.connect(broker.host, broker.port, error => {
44
+ if (error) {
45
+ this.#connections.delete(key);
46
+ this.emit('failed', eventPayload);
47
+ callback(error, undefined);
48
+ return;
49
+ }
50
+ this.emit('connect', eventPayload);
51
+ callback(null, connection);
52
+ });
53
+ // Remove stale connections from the pool
54
+ connection.once('close', () => {
55
+ this.emit('disconnect', eventPayload);
56
+ this.#connections.delete(key);
57
+ });
58
+ connection.once('error', () => {
59
+ this.#connections.delete(key);
60
+ });
61
+ connection.on('drain', () => {
62
+ this.emit('drain', eventPayload);
63
+ });
64
+ return callback[kCallbackPromise];
65
+ }
66
+ getFirstAvailable(brokers, callback, current = 0, errors = []) {
67
+ if (!callback) {
68
+ callback = createPromisifiedCallback();
69
+ }
70
+ this.#getFirstAvailable(brokers, current, errors, callback);
71
+ return callback[kCallbackPromise];
72
+ }
73
+ close(callback) {
74
+ if (!callback) {
75
+ callback = createPromisifiedCallback();
76
+ }
77
+ if (this.#connections.size === 0) {
78
+ callback(null);
79
+ return callback[kCallbackPromise];
80
+ }
81
+ runConcurrentCallbacks('Closing connections failed.', this.#connections, ([key, connection], cb) => {
82
+ connection.close(cb);
83
+ this.#connections.delete(key);
84
+ }, error => callback(error));
85
+ return callback[kCallbackPromise];
86
+ }
87
+ #getFirstAvailable(brokers, current = 0, errors = [], callback) {
88
+ this.get(brokers[current], (error, connection) => {
89
+ if (error) {
90
+ errors.push(error);
91
+ if (current === brokers.length - 1) {
92
+ callback(new MultipleErrors('Cannot connect to any broker.', errors), undefined);
93
+ return;
94
+ }
95
+ this.#getFirstAvailable(brokers, current + 1, errors, callback);
96
+ return;
97
+ }
98
+ callback(null, connection);
99
+ });
100
+ }
101
+ }
@@ -0,0 +1,49 @@
1
+ import EventEmitter from 'node:events';
2
+ import { type Socket } from 'node:net';
3
+ import { type ConnectionOptions as TLSConnectionOptions } from 'node:tls';
4
+ import { type Callback, type ResponseParser } from '../apis/definitions.ts';
5
+ import { type CallbackWithPromise } from '../clients/callbacks.ts';
6
+ import { Writer } from '../protocol/writer.ts';
7
+ export interface Broker {
8
+ host: string;
9
+ port: number;
10
+ }
11
+ export interface ConnectionOptions {
12
+ connectTimeout?: number;
13
+ maxInflights?: number;
14
+ tls?: TLSConnectionOptions;
15
+ ownerId?: number;
16
+ }
17
+ export interface Request {
18
+ correlationId: number;
19
+ apiKey: number;
20
+ apiVersion: number;
21
+ hasRequestHeaderTaggedFields: boolean;
22
+ hasResponseHeaderTaggedFields: boolean;
23
+ payload: () => Writer;
24
+ parser: ResponseParser<unknown>;
25
+ callback: Callback<any>;
26
+ }
27
+ export declare const ConnectionStatuses: {
28
+ readonly NONE: "none";
29
+ readonly CONNECTING: "connecting";
30
+ readonly CONNECTED: "connected";
31
+ readonly CLOSED: "closed";
32
+ readonly CLOSING: "closing";
33
+ readonly ERROR: "error";
34
+ };
35
+ export type ConnectionStatus = keyof typeof ConnectionStatuses;
36
+ export type ConnectionStatusValue = (typeof ConnectionStatuses)[keyof typeof ConnectionStatuses];
37
+ export declare const defaultOptions: ConnectionOptions;
38
+ export declare function noResponseCallback(..._: any[]): void;
39
+ export declare namespace noResponseCallback { }
40
+ export declare class Connection extends EventEmitter {
41
+ #private;
42
+ constructor(clientId?: string, options?: ConnectionOptions);
43
+ get status(): ConnectionStatusValue;
44
+ get socket(): Socket;
45
+ connect(host: string, port: number, callback?: CallbackWithPromise<void>): void | Promise<void>;
46
+ ready(callback?: CallbackWithPromise<void>): void | Promise<void>;
47
+ close(callback?: CallbackWithPromise<void>): void | Promise<void>;
48
+ send<ReturnType>(apiKey: number, apiVersion: number, payload: () => Writer, responseParser: ResponseParser<ReturnType>, hasRequestHeaderTaggedFields: boolean, hasResponseHeaderTaggedFields: boolean, callback: Callback<ReturnType>): void;
49
+ }