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.
Files changed (149) hide show
  1. package/dist/client/client.connection.d.ts +49 -0
  2. package/dist/client/client.connection.js +79 -0
  3. package/dist/client/client.d.ts +46 -0
  4. package/dist/client/client.debug.d.ts +4 -0
  5. package/dist/client/client.debug.js +4 -0
  6. package/dist/client/client.js +65 -1
  7. package/dist/client/client.socket.d.ts +91 -0
  8. package/dist/client/client.socket.js +94 -0
  9. package/dist/client/client.type.d.ts +72 -0
  10. package/dist/client/client.type.js +3 -0
  11. package/dist/client/client.utils.d.ts +28 -0
  12. package/dist/client/client.utils.js +29 -0
  13. package/dist/e2e/tcp.cluster.e2e.js +1 -1
  14. package/dist/index.d.ts +3 -3
  15. package/dist/index.js +3 -3
  16. package/dist/tcp.sm.utils.d.ts +31 -42
  17. package/dist/tcp.sm.utils.js +38 -22
  18. package/dist/type.utils.d.ts +14 -0
  19. package/dist/type.utils.js +9 -0
  20. package/dist/wire/command-set.d.ts +2 -9
  21. package/dist/wire/command.utils.d.ts +18 -0
  22. package/dist/wire/command.utils.js +9 -0
  23. package/dist/wire/consumer-group/create-group.command.d.ts +13 -0
  24. package/dist/wire/consumer-group/create-group.command.js +7 -0
  25. package/dist/wire/consumer-group/delete-group.command.d.ts +13 -0
  26. package/dist/wire/consumer-group/delete-group.command.js +7 -0
  27. package/dist/wire/consumer-group/ensure-group.virtual.command.d.ts +15 -1
  28. package/dist/wire/consumer-group/ensure-group.virtual.command.js +14 -0
  29. package/dist/wire/consumer-group/get-group.command.d.ts +13 -0
  30. package/dist/wire/consumer-group/get-group.command.js +7 -0
  31. package/dist/wire/consumer-group/get-groups.command.d.ts +12 -0
  32. package/dist/wire/consumer-group/get-groups.command.js +7 -0
  33. package/dist/wire/consumer-group/group.utils.d.ts +34 -0
  34. package/dist/wire/consumer-group/group.utils.js +22 -0
  35. package/dist/wire/consumer-group/join-group.command.d.ts +13 -0
  36. package/dist/wire/consumer-group/join-group.command.js +7 -0
  37. package/dist/wire/consumer-group/leave-group.command.d.ts +13 -0
  38. package/dist/wire/consumer-group/leave-group.command.js +7 -0
  39. package/dist/wire/identifier.utils.d.ts +11 -0
  40. package/dist/wire/identifier.utils.js +23 -1
  41. package/dist/wire/message/flush-unsaved-buffers.command.d.ts +13 -0
  42. package/dist/wire/message/flush-unsaved-buffers.command.js +7 -0
  43. package/dist/wire/message/header.type.d.ts +57 -15
  44. package/dist/wire/message/header.type.js +31 -18
  45. package/dist/wire/message/header.utils.d.ts +104 -10
  46. package/dist/wire/message/header.utils.js +233 -86
  47. package/dist/wire/message/header.utils.test.js +39 -16
  48. package/dist/wire/message/iggy-header.utils.d.ts +39 -1
  49. package/dist/wire/message/iggy-header.utils.js +30 -3
  50. package/dist/wire/message/index.d.ts +5 -5
  51. package/dist/wire/message/index.js +5 -5
  52. package/dist/wire/message/message.utils.d.ts +60 -0
  53. package/dist/wire/message/message.utils.js +54 -1
  54. package/dist/wire/message/partitioning.utils.d.ts +39 -0
  55. package/dist/wire/message/partitioning.utils.js +43 -1
  56. package/dist/wire/message/poll-messages.command.d.ts +17 -0
  57. package/dist/wire/message/poll-messages.command.js +7 -0
  58. package/dist/wire/message/poll.utils.d.ts +93 -12
  59. package/dist/wire/message/poll.utils.js +92 -21
  60. package/dist/wire/message/send-messages.command.d.ts +14 -0
  61. package/dist/wire/message/send-messages.command.js +7 -0
  62. package/dist/wire/message/send-messages.command.test.js +103 -39
  63. package/dist/wire/number.utils.d.ts +79 -0
  64. package/dist/wire/number.utils.js +79 -2
  65. package/dist/wire/offset/delete-offset.command.d.ts +13 -3
  66. package/dist/wire/offset/delete-offset.command.js +7 -0
  67. package/dist/wire/offset/get-offset.command.d.ts +14 -0
  68. package/dist/wire/offset/get-offset.command.js +7 -0
  69. package/dist/wire/offset/offset.utils.d.ts +48 -0
  70. package/dist/wire/offset/offset.utils.js +33 -0
  71. package/dist/wire/offset/store-offset.command.d.ts +15 -0
  72. package/dist/wire/offset/store-offset.command.js +7 -0
  73. package/dist/wire/partition/create-partition.command.d.ts +13 -0
  74. package/dist/wire/partition/create-partition.command.js +7 -0
  75. package/dist/wire/partition/delete-partition.command.d.ts +13 -0
  76. package/dist/wire/partition/delete-partition.command.js +7 -0
  77. package/dist/wire/partition/partition.utils.d.ts +9 -0
  78. package/dist/wire/partition/partition.utils.js +9 -0
  79. package/dist/wire/serialize.utils.d.ts +20 -0
  80. package/dist/wire/serialize.utils.js +20 -0
  81. package/dist/wire/session/login-with-token.command.d.ts +11 -0
  82. package/dist/wire/session/login-with-token.command.js +7 -0
  83. package/dist/wire/session/login.command.d.ts +7 -0
  84. package/dist/wire/session/login.command.js +7 -0
  85. package/dist/wire/session/login.utils.d.ts +25 -0
  86. package/dist/wire/session/login.utils.js +14 -0
  87. package/dist/wire/session/logout.command.d.ts +7 -0
  88. package/dist/wire/session/logout.command.js +7 -0
  89. package/dist/wire/stream/create-stream.command.d.ts +12 -0
  90. package/dist/wire/stream/create-stream.command.js +7 -0
  91. package/dist/wire/stream/delete-stream.command.d.ts +11 -0
  92. package/dist/wire/stream/delete-stream.command.js +7 -0
  93. package/dist/wire/stream/ensure-stream.virtual.command.d.ts +8 -1
  94. package/dist/wire/stream/ensure-stream.virtual.command.js +7 -0
  95. package/dist/wire/stream/get-stream.command.d.ts +11 -0
  96. package/dist/wire/stream/get-stream.command.js +7 -0
  97. package/dist/wire/stream/get-streams.command.d.ts +7 -0
  98. package/dist/wire/stream/get-streams.command.js +7 -0
  99. package/dist/wire/stream/purge-stream.command.d.ts +11 -0
  100. package/dist/wire/stream/purge-stream.command.js +7 -0
  101. package/dist/wire/stream/stream.utils.d.ts +22 -0
  102. package/dist/wire/stream/stream.utils.js +8 -0
  103. package/dist/wire/stream/update-stream.command.d.ts +12 -0
  104. package/dist/wire/stream/update-stream.command.js +7 -0
  105. package/dist/wire/token/create-token.command.d.ts +12 -0
  106. package/dist/wire/token/create-token.command.js +7 -0
  107. package/dist/wire/token/delete-token.command.d.ts +11 -0
  108. package/dist/wire/token/delete-token.command.js +7 -0
  109. package/dist/wire/token/get-tokens.command.d.ts +7 -0
  110. package/dist/wire/token/get-tokens.command.js +7 -0
  111. package/dist/wire/token/token.utils.d.ts +40 -0
  112. package/dist/wire/token/token.utils.js +21 -0
  113. package/dist/wire/topic/create-topic.command.d.ts +17 -0
  114. package/dist/wire/topic/create-topic.command.js +7 -0
  115. package/dist/wire/topic/delete-topic.command.d.ts +14 -2
  116. package/dist/wire/topic/delete-topic.command.js +7 -0
  117. package/dist/wire/topic/ensure-topic.virtual.command.d.ts +9 -2
  118. package/dist/wire/topic/ensure-topic.virtual.command.js +7 -0
  119. package/dist/wire/topic/get-topic.command.d.ts +13 -2
  120. package/dist/wire/topic/get-topic.command.js +7 -0
  121. package/dist/wire/topic/get-topics.command.d.ts +11 -0
  122. package/dist/wire/topic/get-topics.command.js +7 -0
  123. package/dist/wire/topic/purge-topic.command.d.ts +12 -0
  124. package/dist/wire/topic/purge-topic.command.js +7 -0
  125. package/dist/wire/topic/topic.utils.d.ts +73 -0
  126. package/dist/wire/topic/topic.utils.js +40 -0
  127. package/dist/wire/topic/update-topic.command.d.ts +17 -0
  128. package/dist/wire/topic/update-topic.command.js +7 -0
  129. package/dist/wire/user/change-password.command.d.ts +13 -0
  130. package/dist/wire/user/change-password.command.js +7 -0
  131. package/dist/wire/user/create-user.command.d.ts +14 -0
  132. package/dist/wire/user/create-user.command.js +7 -0
  133. package/dist/wire/user/delete-user.command.d.ts +11 -0
  134. package/dist/wire/user/delete-user.command.js +7 -0
  135. package/dist/wire/user/get-user.command.d.ts +11 -0
  136. package/dist/wire/user/get-user.command.js +7 -0
  137. package/dist/wire/user/get-users.command.d.ts +7 -0
  138. package/dist/wire/user/get-users.command.js +7 -0
  139. package/dist/wire/user/permissions.utils.d.ts +108 -0
  140. package/dist/wire/user/permissions.utils.js +68 -0
  141. package/dist/wire/user/update-permissions.command.d.ts +12 -0
  142. package/dist/wire/user/update-permissions.command.js +7 -0
  143. package/dist/wire/user/update-user.command.d.ts +13 -0
  144. package/dist/wire/user/update-user.command.js +7 -0
  145. package/dist/wire/user/user.utils.d.ts +40 -0
  146. package/dist/wire/user/user.utils.js +33 -0
  147. package/dist/wire/uuid.utils.d.ts +13 -0
  148. package/dist/wire/uuid.utils.js +13 -1
  149. package/package.json +2 -2
@@ -16,176 +16,303 @@
16
16
  * specific language governing permissions and limitations
17
17
  * under the License.
18
18
  */
19
- import { boolToBuf, int8ToBuf, int16ToBuf, int32ToBuf, int64ToBuf, uint8ToBuf, uint16ToBuf, uint32ToBuf, uint64ToBuf, floatToBuf, doubleToBuf } from '../number.utils.js';
20
- import { HeaderKind, ReverseHeaderKind } from './header.type.js';
19
+ import { boolToBuf, int8ToBuf, int16ToBuf, int32ToBuf, int64ToBuf, uint8ToBuf, uint16ToBuf, uint32ToBuf, uint64ToBuf, floatToBuf, doubleToBuf, } from "../number.utils.js";
20
+ import { HeaderKind, ReverseHeaderKind, expectedSize, } from "./header.type.js";
21
+ /**
22
+ * Serializes a header value to a Buffer based on its kind.
23
+ *
24
+ * @param header - Header value to serialize
25
+ * @returns Serialized value as Buffer
26
+ */
21
27
  export const serializeHeaderValue = (header) => {
22
28
  const { kind, value } = header;
23
29
  switch (kind) {
24
- case HeaderKind.Raw: return value;
25
- case HeaderKind.String: return Buffer.from(value);
26
- case HeaderKind.Bool: return boolToBuf(value);
27
- case HeaderKind.Int8: return int8ToBuf(value);
28
- case HeaderKind.Int16: return int16ToBuf(value);
29
- case HeaderKind.Int32: return int32ToBuf(value);
30
- case HeaderKind.Int64: return int64ToBuf(value);
31
- case HeaderKind.Int128: return value;
32
- case HeaderKind.Uint8: return uint8ToBuf(value);
33
- case HeaderKind.Uint16: return uint16ToBuf(value);
34
- case HeaderKind.Uint32: return uint32ToBuf(value);
35
- case HeaderKind.Uint64: return uint64ToBuf(value);
36
- case HeaderKind.Uint128: return value;
37
- case HeaderKind.Float: return floatToBuf(value);
38
- case HeaderKind.Double: return doubleToBuf(value);
30
+ case HeaderKind.Raw:
31
+ return value;
32
+ case HeaderKind.String:
33
+ return Buffer.from(value);
34
+ case HeaderKind.Bool:
35
+ return boolToBuf(value);
36
+ case HeaderKind.Int8:
37
+ return int8ToBuf(value);
38
+ case HeaderKind.Int16:
39
+ return int16ToBuf(value);
40
+ case HeaderKind.Int32:
41
+ return int32ToBuf(value);
42
+ case HeaderKind.Int64:
43
+ return int64ToBuf(value);
44
+ case HeaderKind.Int128:
45
+ return value;
46
+ case HeaderKind.Uint8:
47
+ return uint8ToBuf(value);
48
+ case HeaderKind.Uint16:
49
+ return uint16ToBuf(value);
50
+ case HeaderKind.Uint32:
51
+ return uint32ToBuf(value);
52
+ case HeaderKind.Uint64:
53
+ return uint64ToBuf(value);
54
+ case HeaderKind.Uint128:
55
+ return value;
56
+ case HeaderKind.Float:
57
+ return floatToBuf(value);
58
+ case HeaderKind.Double:
59
+ return doubleToBuf(value);
60
+ }
61
+ };
62
+ /**
63
+ * Serializes a header key to a Buffer based on its kind.
64
+ *
65
+ * @param key - Header key to serialize
66
+ * @returns Serialized key as Buffer
67
+ */
68
+ export const serializeHeaderKey = (key) => {
69
+ const { kind, value } = key;
70
+ switch (kind) {
71
+ case HeaderKind.Raw:
72
+ return value;
73
+ case HeaderKind.String:
74
+ return Buffer.from(value);
75
+ case HeaderKind.Int32:
76
+ return int32ToBuf(value);
39
77
  }
40
78
  };
79
+ /**
80
+ * Serializes a single header key-value pair to wire format.
81
+ * Format: [key_kind][key_length][key][value_kind][value_length][value]
82
+ *
83
+ * @param key - Header key
84
+ * @param v - Binary header value
85
+ * @returns Serialized header as Buffer
86
+ */
41
87
  export const serializeHeader = (key, v) => {
42
- const bKey = Buffer.from(key);
43
- const b1 = uint32ToBuf(bKey.length);
44
- const b2 = Buffer.alloc(5);
45
- b2.writeUInt8(v.kind);
46
- b2.writeUInt32LE(v.value.length, 1);
47
- // @TODO debug
48
- // console.log(
49
- // 'SERIALIZE\n',
50
- // 'KEY-LEN', b1.length, b1.toString('hex'), '=', b1.readUInt32LE(0), '\n',
51
- // 'KEY', bKey.length, bKey.toString('hex'), '\n',
52
- // 'KIND', b2.readUInt8(0), b2.subarray(0, 1).toString('hex'), '\n',
53
- // 'V-LEN', b2.readUInt32LE(1), b2.subarray(1, 5).toString('hex'), '\n',
54
- // 'V', v.value.length, v.value.toString('hex')
55
- // );
56
- return Buffer.concat([
57
- b1,
58
- bKey,
59
- b2,
60
- v.value
61
- ]);
88
+ const bKey = serializeHeaderKey(key);
89
+ const keyHeader = Buffer.alloc(5);
90
+ keyHeader.writeUInt8(key.kind);
91
+ keyHeader.writeUInt32LE(bKey.length, 1);
92
+ const valueHeader = Buffer.alloc(5);
93
+ valueHeader.writeUInt8(v.kind);
94
+ valueHeader.writeUInt32LE(v.value.length, 1);
95
+ return Buffer.concat([keyHeader, bKey, valueHeader, v.value]);
62
96
  };
97
+ /** Empty headers buffer constant */
63
98
  export const EMPTY_HEADERS = Buffer.alloc(0);
99
+ /**
100
+ * Creates a binary header value from a typed header value.
101
+ *
102
+ * @param header - Typed header value
103
+ * @returns Binary header value
104
+ */
64
105
  const createHeaderValue = (header) => ({
65
106
  kind: header.kind,
66
- value: serializeHeaderValue(header)
107
+ value: serializeHeaderValue(header),
67
108
  });
109
+ /**
110
+ * Serializes all headers to a single buffer.
111
+ *
112
+ * @param headers - Optional headers array
113
+ * @returns Serialized headers buffer (empty if no headers)
114
+ */
68
115
  export const serializeHeaders = (headers) => {
69
- if (!headers)
116
+ if (!headers || headers.length === 0)
70
117
  return EMPTY_HEADERS;
71
- return Buffer.concat(Object.keys(headers).map((c) => serializeHeader(c, createHeaderValue(headers[c]))));
118
+ return Buffer.concat(headers.map((entry) => serializeHeader(entry.key, createHeaderValue(entry.value))));
72
119
  };
120
+ /**
121
+ * Maps a numeric header kind to its string identifier.
122
+ *
123
+ * @param k - Numeric header kind value
124
+ * @returns Header kind identifier string
125
+ * @throws Error if the header kind is unknown
126
+ */
73
127
  export const mapHeaderKind = (k) => {
74
128
  if (!ReverseHeaderKind[k])
75
129
  throw new Error(`unknow header kind: ${k}`);
76
130
  return ReverseHeaderKind[k];
77
131
  };
132
+ /**
133
+ * Deserializes a header value buffer based on its kind.
134
+ *
135
+ * @param kind - Numeric header kind
136
+ * @param value - Raw value buffer
137
+ * @returns Deserialized value
138
+ * @throws Error if the header kind is invalid
139
+ */
78
140
  export const deserializeHeaderValue = (kind, value) => {
79
141
  switch (kind) {
80
142
  case HeaderKind.Int128:
81
143
  case HeaderKind.Uint128:
82
- case HeaderKind.Raw: return value;
83
- case HeaderKind.String: return value.toString();
84
- case HeaderKind.Int8: return value.readInt8();
85
- case HeaderKind.Int16: return value.readInt16LE();
86
- case HeaderKind.Int32: return value.readInt32LE();
87
- case HeaderKind.Int64: return value.readBigInt64LE();
88
- case HeaderKind.Uint8: return value.readUint8();
89
- case HeaderKind.Uint16: return value.readUint16LE();
90
- case HeaderKind.Uint32: return value.readUInt32LE();
91
- case HeaderKind.Uint64: return value.readBigUInt64LE();
92
- case HeaderKind.Bool: return value.readUInt8() === 1;
93
- case HeaderKind.Float: return value.readFloatLE();
94
- case HeaderKind.Double: return value.readDoubleLE();
95
- default: throw new Error(`deserializeHeaderValue: invalid HeaderKind ${kind}`);
144
+ case HeaderKind.Raw:
145
+ return value;
146
+ case HeaderKind.String:
147
+ return value.toString();
148
+ case HeaderKind.Int8:
149
+ return value.readInt8();
150
+ case HeaderKind.Int16:
151
+ return value.readInt16LE();
152
+ case HeaderKind.Int32:
153
+ return value.readInt32LE();
154
+ case HeaderKind.Int64:
155
+ return value.readBigInt64LE();
156
+ case HeaderKind.Uint8:
157
+ return value.readUint8();
158
+ case HeaderKind.Uint16:
159
+ return value.readUint16LE();
160
+ case HeaderKind.Uint32:
161
+ return value.readUInt32LE();
162
+ case HeaderKind.Uint64:
163
+ return value.readBigUInt64LE();
164
+ case HeaderKind.Bool:
165
+ return value.readUInt8() === 1;
166
+ case HeaderKind.Float:
167
+ return value.readFloatLE();
168
+ case HeaderKind.Double:
169
+ return value.readDoubleLE();
170
+ default:
171
+ throw new Error(`deserializeHeaderValue: invalid HeaderKind ${kind}`);
96
172
  }
97
173
  };
174
+ /**
175
+ * Deserializes a single header from a buffer.
176
+ * Format: [key_kind][key_length][key][value_kind][value_length][value]
177
+ *
178
+ * @param p - Buffer containing serialized headers
179
+ * @param pos - Starting position in the buffer
180
+ * @returns Object with bytes read and deserialized header data
181
+ * @throws Error if header key or value length is invalid (must be 1-255)
182
+ */
98
183
  export const deserializeHeader = (p, pos = 0) => {
99
- const keyLength = p.readUInt32LE(pos);
100
- const key = p.subarray(pos + 4, pos + 4 + keyLength).toString();
101
- pos += keyLength + 4;
102
- const rawKind = p.readUInt8(pos);
103
- // @TODO ?
104
- // const kind = mapHeaderKind(rawKind);
184
+ const keyKind = p.readUInt8(pos);
185
+ const keyLength = p.readUInt32LE(pos + 1);
186
+ if (keyLength < 1 || keyLength > 255) {
187
+ throw new Error(`Invalid header key length: ${keyLength}, must be between 1 and 255`);
188
+ }
189
+ const keyExpected = expectedSize(keyKind);
190
+ if (keyExpected !== -1 && keyLength !== keyExpected) {
191
+ throw new Error(`Invalid header key size for kind ${keyKind}: expected ${keyExpected}, got ${keyLength}`);
192
+ }
193
+ const keyValue = deserializeHeaderValue(keyKind, p.subarray(pos + 5, pos + 5 + keyLength));
194
+ pos += 5 + keyLength;
195
+ const valueKind = p.readUInt8(pos);
105
196
  const valueLength = p.readUInt32LE(pos + 1);
106
- const value = deserializeHeaderValue(rawKind, p.subarray(pos + 5, pos + 5 + valueLength));
197
+ if (valueLength < 1 || valueLength > 255) {
198
+ throw new Error(`Invalid header value length: ${valueLength}, must be between 1 and 255`);
199
+ }
200
+ const valueExpected = expectedSize(valueKind);
201
+ if (valueExpected !== -1 && valueLength !== valueExpected) {
202
+ throw new Error(`Invalid header value size for kind ${valueKind}: expected ${valueExpected}, got ${valueLength}`);
203
+ }
204
+ const value = deserializeHeaderValue(valueKind, p.subarray(pos + 5, pos + 5 + valueLength));
107
205
  return {
108
- bytesRead: 4 + 4 + 1 + keyLength + valueLength,
206
+ bytesRead: 5 + keyLength + 5 + valueLength,
109
207
  data: {
110
- key,
111
- kind: rawKind,
112
- value
113
- }
208
+ key: { kind: keyKind, value: keyValue },
209
+ value: { kind: valueKind, value },
210
+ },
114
211
  };
115
212
  };
213
+ /**
214
+ * Deserializes all headers from a buffer.
215
+ *
216
+ * @param p - Buffer containing serialized headers
217
+ * @param pos - Starting position in the buffer
218
+ * @returns Array of parsed header entries
219
+ */
116
220
  export const deserializeHeaders = (p, pos = 0) => {
117
- const headers = {};
221
+ const headers = [];
118
222
  const len = p.length;
119
223
  while (pos < len) {
120
- const { bytesRead, data: { kind, key, value } } = deserializeHeader(p, pos);
121
- headers[key] = { kind, value };
224
+ const { bytesRead, data } = deserializeHeader(p, pos);
225
+ headers.push(data);
122
226
  pos += bytesRead;
123
227
  }
124
228
  return headers;
125
229
  };
126
- /** HeaderValue Helper */
230
+ /**
231
+ * HeaderValue factory functions and utilities.
232
+ * Provides type-safe constructors for each header value type.
233
+ */
234
+ /** Creates a raw binary header value */
127
235
  const Raw = (value) => ({
128
236
  kind: HeaderKind.Raw,
129
- value
237
+ value,
130
238
  });
239
+ /** Creates a string header value */
131
240
  const String = (value) => ({
132
241
  kind: HeaderKind.String,
133
- value
242
+ value,
134
243
  });
244
+ /** Creates a boolean header value */
135
245
  const Bool = (value) => ({
136
246
  kind: HeaderKind.Bool,
137
- value
247
+ value,
138
248
  });
249
+ /** Creates an Int8 header value */
139
250
  const Int8 = (value) => ({
140
251
  kind: HeaderKind.Int8,
141
- value
252
+ value,
142
253
  });
254
+ /** Creates an Int16 header value */
143
255
  const Int16 = (value) => ({
144
256
  kind: HeaderKind.Int16,
145
- value
257
+ value,
146
258
  });
259
+ /** Creates an Int32 header value */
147
260
  const Int32 = (value) => ({
148
261
  kind: HeaderKind.Int32,
149
- value
262
+ value,
150
263
  });
264
+ /** Creates an Int64 header value */
151
265
  const Int64 = (value) => ({
152
266
  kind: HeaderKind.Int64,
153
- value
267
+ value,
154
268
  });
269
+ /** Creates an Int128 header value */
155
270
  const Int128 = (value) => ({
156
271
  kind: HeaderKind.Int128,
157
- value
272
+ value,
158
273
  });
274
+ /** Creates a Uint8 header value */
159
275
  const Uint8 = (value) => ({
160
276
  kind: HeaderKind.Uint8,
161
- value
277
+ value,
162
278
  });
279
+ /** Creates a Uint16 header value */
163
280
  const Uint16 = (value) => ({
164
281
  kind: HeaderKind.Uint16,
165
- value
282
+ value,
166
283
  });
284
+ /** Creates a Uint32 header value */
167
285
  const Uint32 = (value) => ({
168
286
  kind: HeaderKind.Uint32,
169
- value
287
+ value,
170
288
  });
289
+ /** Creates a Uint64 header value */
171
290
  const Uint64 = (value) => ({
172
291
  kind: HeaderKind.Uint64,
173
- value
292
+ value,
174
293
  });
294
+ /** Creates a Uint128 header value */
175
295
  const Uint128 = (value) => ({
176
296
  kind: HeaderKind.Uint128,
177
- value
297
+ value,
178
298
  });
299
+ /** Creates a Float header value */
179
300
  const Float = (value) => ({
180
301
  kind: HeaderKind.Float,
181
- value
302
+ value,
182
303
  });
304
+ /** Creates a Double header value */
183
305
  const Double = (value) => ({
184
306
  kind: HeaderKind.Double,
185
- value
307
+ value,
186
308
  });
309
+ /** Gets the kind identifier string of a header value */
187
310
  const getKind = (h) => mapHeaderKind(h.kind);
311
+ /** Gets the value from a header value */
188
312
  const getValue = (h) => h.value;
313
+ /**
314
+ * HeaderValue factory object with constructors for all header types.
315
+ */
189
316
  export const HeaderValue = {
190
317
  Raw,
191
318
  String,
@@ -203,7 +330,27 @@ export const HeaderValue = {
203
330
  Float,
204
331
  Double,
205
332
  getKind,
206
- getValue
333
+ getValue,
334
+ };
335
+ /** Creates a raw binary header key */
336
+ const keyRaw = (value) => ({
337
+ kind: HeaderKind.Raw,
338
+ value,
339
+ });
340
+ /** Creates a string header key */
341
+ const keyString = (value) => ({
342
+ kind: HeaderKind.String,
343
+ value,
344
+ });
345
+ /** Creates an Int32 header key */
346
+ const keyInt32 = (value) => ({
347
+ kind: HeaderKind.Int32,
348
+ value,
349
+ });
350
+ export const HeaderKeyFactory = {
351
+ Raw: keyRaw,
352
+ String: keyString,
353
+ Int32: keyInt32,
207
354
  };
208
355
  // export type InputHeaderValue = boolean | number | string | bigint | Buffer;
209
356
  // export type InputHeaders = Record<string, InputHeaderValue>;
@@ -16,24 +16,47 @@
16
16
  * specific language governing permissions and limitations
17
17
  * under the License.
18
18
  */
19
- import { describe, it } from 'node:test';
20
- import assert from 'node:assert/strict';
21
- import { uuidv7, uuidv4 } from 'uuidv7';
22
- import { serializeHeaders, deserializeHeaders, HeaderValue } from './header.utils.js';
23
- describe('Headers', () => {
24
- const headers = {
25
- 'p': HeaderValue.Bool(true),
26
- 'x': HeaderValue.Uint32(123),
27
- 'y': HeaderValue.Uint64(42n),
28
- 'z': HeaderValue.Float(42.20000076293945),
29
- 'a': HeaderValue.Double(1 / 3),
30
- 'ID': HeaderValue.String(uuidv7()),
31
- 'val': HeaderValue.Raw(Buffer.from(uuidv4()))
32
- };
33
- it('serialize/deserialize', () => {
19
+ import { describe, it } from "node:test";
20
+ import assert from "node:assert/strict";
21
+ import { uuidv7, uuidv4 } from "uuidv7";
22
+ import { serializeHeaders, deserializeHeaders, HeaderValue, HeaderKeyFactory, } from "./header.utils.js";
23
+ import { HeaderKind } from "./header.type.js";
24
+ describe("Headers", () => {
25
+ const headers = [
26
+ { key: HeaderKeyFactory.String("p"), value: HeaderValue.Bool(true) },
27
+ { key: HeaderKeyFactory.String("x"), value: HeaderValue.Uint32(123) },
28
+ { key: HeaderKeyFactory.String("y"), value: HeaderValue.Uint64(42n) },
29
+ {
30
+ key: HeaderKeyFactory.String("z"),
31
+ value: HeaderValue.Float(42.20000076293945),
32
+ },
33
+ { key: HeaderKeyFactory.String("a"), value: HeaderValue.Double(1 / 3) },
34
+ { key: HeaderKeyFactory.String("ID"), value: HeaderValue.String(uuidv7()) },
35
+ {
36
+ key: HeaderKeyFactory.String("val"),
37
+ value: HeaderValue.Raw(Buffer.from(uuidv4())),
38
+ },
39
+ ];
40
+ it("serialize/deserialize string keys", () => {
34
41
  const s = serializeHeaders(headers);
35
42
  const d = deserializeHeaders(s);
36
- assert.deepEqual(headers, d);
43
+ assert.equal(d.length, headers.length);
44
+ for (let i = 0; i < headers.length; i++) {
45
+ assert.equal(d[i].key.kind, headers[i].key.kind);
46
+ assert.equal(d[i].value.kind, headers[i].value.kind);
47
+ }
48
+ });
49
+ it("serialize/deserialize int32 key", () => {
50
+ const int32Headers = [
51
+ { key: HeaderKeyFactory.Int32(42), value: HeaderValue.String("test") },
52
+ ];
53
+ const s = serializeHeaders(int32Headers);
54
+ const d = deserializeHeaders(s);
55
+ assert.equal(d.length, 1);
56
+ assert.equal(d[0].key.kind, HeaderKind.Int32);
57
+ assert.equal(d[0].key.value, 42);
58
+ assert.equal(d[0].value.kind, HeaderKind.String);
59
+ assert.equal(d[0].value.value, "test");
37
60
  });
38
61
  });
39
62
  //# sourceMappingURL=header.utils.test.js.map
@@ -16,17 +16,55 @@
16
16
  * specific language governing permissions and limitations
17
17
  * under the License.
18
18
  */
19
+ /**
20
+ * Iggy message header containing metadata for each message.
21
+ */
19
22
  export type IggyMessageHeader = {
23
+ /** Message checksum for integrity verification */
20
24
  checksum: bigint;
21
- id: string | BigInt;
25
+ /** Unique message identifier (UUID or numeric) */
26
+ id: string | bigint;
27
+ /** Message offset within the partition */
22
28
  offset: bigint;
29
+ /** Server-assigned timestamp */
23
30
  timestamp: Date;
31
+ /** Client-provided origin timestamp */
24
32
  originTimestamp: Date;
33
+ /** Length of user-defined headers in bytes */
25
34
  userHeadersLength: number;
35
+ /** Length of message payload in bytes */
26
36
  payloadLength: number;
37
+ /** Reserved for future use */
38
+ reserved: bigint;
27
39
  };
40
+ /**
41
+ * Size of the Iggy message header in bytes.
42
+ * Layout: u64 (checksum) + u128 (id) + u64 (offset) + u64 (timestamp) + u64 (originTimestamp) + u32 (userHeadersLength) + u32 (payloadLength) + u64 (reserved)
43
+ */
28
44
  export declare const IGGY_MESSAGE_HEADER_SIZE: number;
45
+ /**
46
+ * Serializes an Iggy message header to wire format.
47
+ * Sets checksum, offset, and timestamp to zero (filled by server).
48
+ *
49
+ * @param id - Message ID as 16-byte buffer
50
+ * @param payload - Message payload
51
+ * @param userHeaders - Serialized user headers
52
+ * @returns Serialized header buffer
53
+ */
29
54
  export declare const serializeIggyMessageHeader: (id: Buffer, payload: Buffer, userHeaders: Buffer) => Buffer<ArrayBuffer>;
55
+ /**
56
+ * Deserializes a message ID from a 16-byte buffer to BigInt.
57
+ *
58
+ * @param b - 16-byte buffer containing the message ID
59
+ * @returns Message ID as BigInt
60
+ */
30
61
  export declare const deserialiseMessageId: (b: Buffer) => bigint;
62
+ /**
63
+ * Deserializes Iggy message headers from a buffer.
64
+ *
65
+ * @param b - Buffer containing the serialized header
66
+ * @returns Parsed IggyMessageHeader object
67
+ * @throws Error if buffer length doesn't match expected header size
68
+ */
31
69
  export declare const deserializeIggyMessageHeaders: (b: Buffer) => IggyMessageHeader;
32
70
  //# sourceMappingURL=iggy-header.utils.d.ts.map
@@ -18,8 +18,20 @@
18
18
  */
19
19
  import { toDate } from "../serialize.utils.js";
20
20
  import { u128LEBufToBigint } from "../number.utils.js";
21
- // u64 + u128 + u64 + u64 + u64 + u32 + u32
22
- export const IGGY_MESSAGE_HEADER_SIZE = 8 + 16 + 8 + 8 + 8 + 4 + 4;
21
+ /**
22
+ * Size of the Iggy message header in bytes.
23
+ * Layout: u64 (checksum) + u128 (id) + u64 (offset) + u64 (timestamp) + u64 (originTimestamp) + u32 (userHeadersLength) + u32 (payloadLength) + u64 (reserved)
24
+ */
25
+ export const IGGY_MESSAGE_HEADER_SIZE = 8 + 16 + 8 + 8 + 8 + 4 + 4 + 8;
26
+ /**
27
+ * Serializes an Iggy message header to wire format.
28
+ * Sets checksum, offset, and timestamp to zero (filled by server).
29
+ *
30
+ * @param id - Message ID as 16-byte buffer
31
+ * @param payload - Message payload
32
+ * @param userHeaders - Serialized user headers
33
+ * @returns Serialized header buffer
34
+ */
23
35
  export const serializeIggyMessageHeader = (id, payload, userHeaders) => {
24
36
  const b = Buffer.allocUnsafe(IGGY_MESSAGE_HEADER_SIZE);
25
37
  b.writeBigUInt64LE(0n, 0); // checksum u64
@@ -29,9 +41,23 @@ export const serializeIggyMessageHeader = (id, payload, userHeaders) => {
29
41
  b.writeBigUint64LE(BigInt(new Date().getTime()), 40); // originTimestamp u64
30
42
  b.writeUInt32LE(userHeaders.length, 48); // userHeaders len u32
31
43
  b.writeUInt32LE(payload.length, 52); // payload len u32
44
+ b.writeBigUInt64LE(0n, 56); // reserved u64
32
45
  return b;
33
46
  };
47
+ /**
48
+ * Deserializes a message ID from a 16-byte buffer to BigInt.
49
+ *
50
+ * @param b - 16-byte buffer containing the message ID
51
+ * @returns Message ID as BigInt
52
+ */
34
53
  export const deserialiseMessageId = (b) => u128LEBufToBigint(b);
54
+ /**
55
+ * Deserializes Iggy message headers from a buffer.
56
+ *
57
+ * @param b - Buffer containing the serialized header
58
+ * @returns Parsed IggyMessageHeader object
59
+ * @throws Error if buffer length doesn't match expected header size
60
+ */
35
61
  export const deserializeIggyMessageHeaders = (b) => {
36
62
  if (b.length !== IGGY_MESSAGE_HEADER_SIZE)
37
63
  throw new Error(`deserialize message headers error, length = ${b.length} ` +
@@ -43,7 +69,8 @@ export const deserializeIggyMessageHeaders = (b) => {
43
69
  timestamp: toDate(b.readBigUInt64LE(32)),
44
70
  originTimestamp: toDate(b.readBigUInt64LE(40)),
45
71
  userHeadersLength: b.readUInt32LE(48),
46
- payloadLength: b.readUInt32LE(52)
72
+ payloadLength: b.readUInt32LE(52),
73
+ reserved: b.readBigUInt64LE(56),
47
74
  };
48
75
  return headers;
49
76
  };
@@ -16,10 +16,10 @@
16
16
  * specific language governing permissions and limitations
17
17
  * under the License.
18
18
  */
19
- export * from './poll-messages.command.js';
20
- export * from './send-messages.command.js';
21
- export * from './message.utils.js';
19
+ export * from "./poll-messages.command.js";
20
+ export * from "./send-messages.command.js";
21
+ export * from "./message.utils.js";
22
22
  export * from "./poll.utils.js";
23
- export { Partitioning } from './partitioning.utils.js';
24
- export { HeaderValue } from './header.utils.js';
23
+ export { Partitioning } from "./partitioning.utils.js";
24
+ export { HeaderValue, HeaderKeyFactory } from "./header.utils.js";
25
25
  //# sourceMappingURL=index.d.ts.map
@@ -16,10 +16,10 @@
16
16
  * specific language governing permissions and limitations
17
17
  * under the License.
18
18
  */
19
- export * from './poll-messages.command.js';
20
- export * from './send-messages.command.js';
21
- export * from './message.utils.js';
19
+ export * from "./poll-messages.command.js";
20
+ export * from "./send-messages.command.js";
21
+ export * from "./message.utils.js";
22
22
  export * from "./poll.utils.js";
23
- export { Partitioning } from './partitioning.utils.js';
24
- export { HeaderValue } from './header.utils.js';
23
+ export { Partitioning } from "./partitioning.utils.js";
24
+ export { HeaderValue, HeaderKeyFactory } from "./header.utils.js";
25
25
  //# sourceMappingURL=index.js.map