@sachitv/avro-typescript 0.4.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 (163) hide show
  1. package/esm/_dnt.polyfills.d.ts +11 -0
  2. package/esm/_dnt.polyfills.js +15 -0
  3. package/esm/avro_reader.d.ts +156 -0
  4. package/esm/avro_reader.js +201 -0
  5. package/esm/avro_writer.d.ts +75 -0
  6. package/esm/avro_writer.js +105 -0
  7. package/esm/internal/collections/array_utils.d.ts +4 -0
  8. package/esm/internal/collections/array_utils.js +18 -0
  9. package/esm/internal/collections/circular_buffer.d.ts +48 -0
  10. package/esm/internal/collections/circular_buffer.js +129 -0
  11. package/esm/internal/crypto/md5.d.ts +12 -0
  12. package/esm/internal/crypto/md5.js +158 -0
  13. package/esm/internal/varint.d.ts +6 -0
  14. package/esm/internal/varint.js +30 -0
  15. package/esm/mod.d.ts +77 -0
  16. package/esm/mod.js +73 -0
  17. package/esm/package.json +3 -0
  18. package/esm/rpc/definitions/message_definition.d.ts +34 -0
  19. package/esm/rpc/definitions/message_definition.js +105 -0
  20. package/esm/rpc/definitions/protocol_definitions.d.ts +197 -0
  21. package/esm/rpc/definitions/protocol_definitions.js +1 -0
  22. package/esm/rpc/message_endpoint/base.d.ts +43 -0
  23. package/esm/rpc/message_endpoint/base.js +81 -0
  24. package/esm/rpc/message_endpoint/emitter.d.ts +96 -0
  25. package/esm/rpc/message_endpoint/emitter.js +245 -0
  26. package/esm/rpc/message_endpoint/helpers.d.ts +37 -0
  27. package/esm/rpc/message_endpoint/helpers.js +68 -0
  28. package/esm/rpc/message_endpoint/listener.d.ts +37 -0
  29. package/esm/rpc/message_endpoint/listener.js +212 -0
  30. package/esm/rpc/protocol/frame_assembler.d.ts +11 -0
  31. package/esm/rpc/protocol/frame_assembler.js +77 -0
  32. package/esm/rpc/protocol/protocol_helpers.d.ts +59 -0
  33. package/esm/rpc/protocol/protocol_helpers.js +123 -0
  34. package/esm/rpc/protocol/transports/fetch.d.ts +9 -0
  35. package/esm/rpc/protocol/transports/fetch.js +30 -0
  36. package/esm/rpc/protocol/transports/in_memory.d.ts +27 -0
  37. package/esm/rpc/protocol/transports/in_memory.js +125 -0
  38. package/esm/rpc/protocol/transports/transport_helpers.d.ts +118 -0
  39. package/esm/rpc/protocol/transports/transport_helpers.js +112 -0
  40. package/esm/rpc/protocol/transports/websocket.d.ts +9 -0
  41. package/esm/rpc/protocol/transports/websocket.js +102 -0
  42. package/esm/rpc/protocol/wire_format/framing.d.ts +44 -0
  43. package/esm/rpc/protocol/wire_format/framing.js +74 -0
  44. package/esm/rpc/protocol/wire_format/handshake.d.ts +110 -0
  45. package/esm/rpc/protocol/wire_format/handshake.js +239 -0
  46. package/esm/rpc/protocol/wire_format/messages.d.ts +192 -0
  47. package/esm/rpc/protocol/wire_format/messages.js +175 -0
  48. package/esm/rpc/protocol/wire_format/metadata.d.ts +44 -0
  49. package/esm/rpc/protocol/wire_format/metadata.js +74 -0
  50. package/esm/rpc/protocol_core.d.ts +121 -0
  51. package/esm/rpc/protocol_core.js +285 -0
  52. package/esm/schemas/base_type.d.ts +41 -0
  53. package/esm/schemas/base_type.js +49 -0
  54. package/esm/schemas/complex/array_type.d.ts +100 -0
  55. package/esm/schemas/complex/array_type.js +299 -0
  56. package/esm/schemas/complex/enum_type.d.ts +81 -0
  57. package/esm/schemas/complex/enum_type.js +217 -0
  58. package/esm/schemas/complex/fixed_type.d.ts +99 -0
  59. package/esm/schemas/complex/fixed_type.js +208 -0
  60. package/esm/schemas/complex/map_type.d.ts +97 -0
  61. package/esm/schemas/complex/map_type.js +290 -0
  62. package/esm/schemas/complex/named_type.d.ts +30 -0
  63. package/esm/schemas/complex/named_type.js +57 -0
  64. package/esm/schemas/complex/record_type.d.ts +165 -0
  65. package/esm/schemas/complex/record_type.js +547 -0
  66. package/esm/schemas/complex/resolve_names.d.ts +32 -0
  67. package/esm/schemas/complex/resolve_names.js +85 -0
  68. package/esm/schemas/complex/union_type.d.ts +116 -0
  69. package/esm/schemas/complex/union_type.js +392 -0
  70. package/esm/schemas/error.d.ts +31 -0
  71. package/esm/schemas/error.js +67 -0
  72. package/esm/schemas/json.d.ts +6 -0
  73. package/esm/schemas/json.js +35 -0
  74. package/esm/schemas/logical/decimal_logical_type.d.ts +49 -0
  75. package/esm/schemas/logical/decimal_logical_type.js +145 -0
  76. package/esm/schemas/logical/duration_logical_type.d.ts +66 -0
  77. package/esm/schemas/logical/duration_logical_type.js +112 -0
  78. package/esm/schemas/logical/logical_type.d.ts +138 -0
  79. package/esm/schemas/logical/logical_type.js +240 -0
  80. package/esm/schemas/logical/temporal_logical_types.d.ts +215 -0
  81. package/esm/schemas/logical/temporal_logical_types.js +364 -0
  82. package/esm/schemas/logical/uuid_logical_type.d.ts +63 -0
  83. package/esm/schemas/logical/uuid_logical_type.js +146 -0
  84. package/esm/schemas/primitive/boolean_type.d.ts +49 -0
  85. package/esm/schemas/primitive/boolean_type.js +75 -0
  86. package/esm/schemas/primitive/bytes_type.d.ts +49 -0
  87. package/esm/schemas/primitive/bytes_type.js +136 -0
  88. package/esm/schemas/primitive/double_type.d.ts +38 -0
  89. package/esm/schemas/primitive/double_type.js +98 -0
  90. package/esm/schemas/primitive/fixed_size_base_type.d.ts +28 -0
  91. package/esm/schemas/primitive/fixed_size_base_type.js +33 -0
  92. package/esm/schemas/primitive/float_type.d.ts +38 -0
  93. package/esm/schemas/primitive/float_type.js +88 -0
  94. package/esm/schemas/primitive/int_type.d.ts +31 -0
  95. package/esm/schemas/primitive/int_type.js +63 -0
  96. package/esm/schemas/primitive/long_type.d.ts +36 -0
  97. package/esm/schemas/primitive/long_type.js +95 -0
  98. package/esm/schemas/primitive/null_type.d.ts +45 -0
  99. package/esm/schemas/primitive/null_type.js +71 -0
  100. package/esm/schemas/primitive/primitive_type.d.ts +17 -0
  101. package/esm/schemas/primitive/primitive_type.js +27 -0
  102. package/esm/schemas/primitive/string_type.d.ts +34 -0
  103. package/esm/schemas/primitive/string_type.js +81 -0
  104. package/esm/schemas/resolver.d.ts +25 -0
  105. package/esm/schemas/resolver.js +25 -0
  106. package/esm/schemas/type.d.ts +101 -0
  107. package/esm/schemas/type.js +6 -0
  108. package/esm/serialization/avro_constants.d.ts +13 -0
  109. package/esm/serialization/avro_constants.js +29 -0
  110. package/esm/serialization/avro_file_parser.d.ts +68 -0
  111. package/esm/serialization/avro_file_parser.js +191 -0
  112. package/esm/serialization/avro_file_writer.d.ts +63 -0
  113. package/esm/serialization/avro_file_writer.js +235 -0
  114. package/esm/serialization/buffers/blob_readable_buffer.d.ts +53 -0
  115. package/esm/serialization/buffers/blob_readable_buffer.js +80 -0
  116. package/esm/serialization/buffers/buffer.d.ts +37 -0
  117. package/esm/serialization/buffers/buffer.js +1 -0
  118. package/esm/serialization/buffers/in_memory_buffer.d.ts +121 -0
  119. package/esm/serialization/buffers/in_memory_buffer.js +206 -0
  120. package/esm/serialization/clamp.d.ts +10 -0
  121. package/esm/serialization/clamp.js +19 -0
  122. package/esm/serialization/compare_bytes.d.ts +9 -0
  123. package/esm/serialization/compare_bytes.js +45 -0
  124. package/esm/serialization/conversion.d.ts +8 -0
  125. package/esm/serialization/conversion.js +15 -0
  126. package/esm/serialization/decoders/decoder.d.ts +16 -0
  127. package/esm/serialization/decoders/decoder.js +1 -0
  128. package/esm/serialization/decoders/deflate_decoder.d.ts +15 -0
  129. package/esm/serialization/decoders/deflate_decoder.js +26 -0
  130. package/esm/serialization/decoders/null_decoder.d.ts +12 -0
  131. package/esm/serialization/decoders/null_decoder.js +13 -0
  132. package/esm/serialization/encoders/deflate_encoder.d.ts +15 -0
  133. package/esm/serialization/encoders/deflate_encoder.js +26 -0
  134. package/esm/serialization/encoders/encoder.d.ts +16 -0
  135. package/esm/serialization/encoders/encoder.js +1 -0
  136. package/esm/serialization/encoders/null_encoder.d.ts +12 -0
  137. package/esm/serialization/encoders/null_encoder.js +13 -0
  138. package/esm/serialization/manipulate_bytes.d.ts +6 -0
  139. package/esm/serialization/manipulate_bytes.js +13 -0
  140. package/esm/serialization/read_uint_le.d.ts +4 -0
  141. package/esm/serialization/read_uint_le.js +14 -0
  142. package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.d.ts +52 -0
  143. package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.js +129 -0
  144. package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.d.ts +36 -0
  145. package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.js +114 -0
  146. package/esm/serialization/streams/stream_readable_buffer.d.ts +41 -0
  147. package/esm/serialization/streams/stream_readable_buffer.js +64 -0
  148. package/esm/serialization/streams/stream_readable_buffer_adapter.d.ts +38 -0
  149. package/esm/serialization/streams/stream_readable_buffer_adapter.js +105 -0
  150. package/esm/serialization/streams/stream_writable_buffer.d.ts +42 -0
  151. package/esm/serialization/streams/stream_writable_buffer.js +64 -0
  152. package/esm/serialization/streams/stream_writable_buffer_adapter.d.ts +36 -0
  153. package/esm/serialization/streams/stream_writable_buffer_adapter.js +65 -0
  154. package/esm/serialization/streams/streams.d.ts +29 -0
  155. package/esm/serialization/streams/streams.js +1 -0
  156. package/esm/serialization/tap.d.ts +427 -0
  157. package/esm/serialization/tap.js +605 -0
  158. package/esm/serialization/text_encoding.d.ts +12 -0
  159. package/esm/serialization/text_encoding.js +14 -0
  160. package/esm/type/create_type.d.ts +54 -0
  161. package/esm/type/create_type.js +401 -0
  162. package/package.json +33 -0
  163. package/sachitv-avro-typescript-0.4.0.tgz +0 -0
@@ -0,0 +1,364 @@
1
+ import { LogicalType, withLogicalTypeJSON } from "./logical_type.js";
2
+ import { bigIntToSafeNumber } from "../../serialization/conversion.js";
3
+ const MILLIS_PER_DAY = 86400000;
4
+ const MICROS_PER_DAY = 86400000000n;
5
+ /**
6
+ * Logical type for `date`.
7
+ * Represents the number of days since the epoch.
8
+ */
9
+ export class DateLogicalType extends LogicalType {
10
+ /**
11
+ * Creates a new DateLogicalType.
12
+ * @param underlying The underlying int type.
13
+ */
14
+ constructor(underlying) {
15
+ super(underlying);
16
+ }
17
+ /** Checks if the logical type can read from the underlying type. */
18
+ canReadFromLogical(writer) {
19
+ return writer instanceof DateLogicalType;
20
+ }
21
+ /** Checks if the value is a valid instance of this logical type. */
22
+ /** Checks if the value is a valid instance of this logical type. */
23
+ isInstance(value) {
24
+ return typeof value === "number" && Number.isInteger(value);
25
+ }
26
+ /** Converts the logical value to the underlying representation. */
27
+ toUnderlying(value) {
28
+ return value;
29
+ }
30
+ /** Converts the underlying value to the logical representation. */
31
+ fromUnderlying(value) {
32
+ return value;
33
+ }
34
+ /** Generates a random value for testing. */
35
+ random() {
36
+ const today = Math.floor(Date.now() / MILLIS_PER_DAY);
37
+ return today + Math.floor(Math.random() * 2000) - 1000;
38
+ }
39
+ /** Returns the JSON representation of this type. */
40
+ toJSON() {
41
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "date");
42
+ }
43
+ }
44
+ /**
45
+ * Logical type for `time-millis`.
46
+ * Represents the time of day in milliseconds since midnight.
47
+ */
48
+ export class TimeMillisLogicalType extends LogicalType {
49
+ /**
50
+ * Creates a new TimeMillisLogicalType.
51
+ * @param underlying The underlying int type.
52
+ */
53
+ constructor(underlying) {
54
+ super(underlying);
55
+ }
56
+ /** Checks if the logical type can read from the underlying type. */
57
+ canReadFromLogical(writer) {
58
+ return writer instanceof TimeMillisLogicalType;
59
+ }
60
+ /** Checks if the value is a valid instance of this logical type. */
61
+ isInstance(value) {
62
+ return typeof value === "number" && Number.isInteger(value) &&
63
+ value >= 0 && value < MILLIS_PER_DAY;
64
+ }
65
+ /** Converts the logical value to the underlying representation. */
66
+ toUnderlying(value) {
67
+ return value;
68
+ }
69
+ /** Converts the underlying value to the logical representation. */
70
+ fromUnderlying(value) {
71
+ return value;
72
+ }
73
+ /** Generates a random value for testing. */
74
+ random() {
75
+ return Math.floor(Math.random() * MILLIS_PER_DAY);
76
+ }
77
+ /** Returns the JSON representation of this type. */
78
+ toJSON() {
79
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "time-millis");
80
+ }
81
+ }
82
+ /**
83
+ * Logical type for `time-micros`.
84
+ * Represents the time of day in microseconds since midnight.
85
+ */
86
+ export class TimeMicrosLogicalType extends LogicalType {
87
+ /**
88
+ * Creates a new TimeMicrosLogicalType.
89
+ * @param underlying The underlying long type.
90
+ */
91
+ constructor(underlying) {
92
+ super(underlying);
93
+ }
94
+ /** Checks if the logical type can read from the underlying type. */
95
+ canReadFromLogical(writer) {
96
+ return writer instanceof TimeMicrosLogicalType;
97
+ }
98
+ /** Checks if the value is a valid instance of this logical type. */
99
+ /** Checks if the value is a valid instance of this logical type. */
100
+ isInstance(value) {
101
+ return typeof value === "bigint" && value >= 0n && value < MICROS_PER_DAY;
102
+ }
103
+ /** Converts the logical value to the underlying representation. */
104
+ toUnderlying(value) {
105
+ return value;
106
+ }
107
+ /** Converts the underlying value to the logical representation. */
108
+ fromUnderlying(value) {
109
+ return value;
110
+ }
111
+ /** Generates a random value for testing. */
112
+ random() {
113
+ return BigInt(Math.floor(Math.random() * Number(MICROS_PER_DAY)));
114
+ }
115
+ /** Returns the JSON representation of this type. */
116
+ toJSON() {
117
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "time-micros");
118
+ }
119
+ }
120
+ /**
121
+ * Logical type for `timestamp-millis`.
122
+ * Represents an instant in time as the number of milliseconds since the epoch.
123
+ */
124
+ export class TimestampMillisLogicalType extends LogicalType {
125
+ /**
126
+ * Creates a new TimestampMillisLogicalType.
127
+ * @param underlying The underlying long type.
128
+ */
129
+ constructor(underlying) {
130
+ super(underlying);
131
+ }
132
+ /** Checks if the logical type can read from the underlying type. */
133
+ canReadFromLogical(writer) {
134
+ return writer instanceof TimestampMillisLogicalType;
135
+ }
136
+ /** Checks if the value is a valid instance of this logical type. */
137
+ isInstance(value) {
138
+ return value instanceof Date && !Number.isNaN(value.getTime());
139
+ }
140
+ /** Converts the logical value to the underlying representation. */
141
+ toUnderlying(value) {
142
+ const millis = value.getTime();
143
+ if (!Number.isFinite(millis)) {
144
+ throw new Error("Invalid Date value for timestamp-millis logical type.");
145
+ }
146
+ return BigInt(Math.trunc(millis));
147
+ }
148
+ /** Converts the underlying value to the logical representation. */
149
+ fromUnderlying(value) {
150
+ const millis = bigIntToSafeNumber(value, "timestamp-millis");
151
+ return new Date(millis);
152
+ }
153
+ /** Generates a random value for testing. */
154
+ random() {
155
+ const now = Date.now();
156
+ const offset = Math.floor(Math.random() * 1000000000) - 500000000;
157
+ return new Date(now + offset);
158
+ }
159
+ /** Compares two Date values. */
160
+ compare(val1, val2) {
161
+ const diff = val1.getTime() - val2.getTime();
162
+ return diff < 0 ? -1 : diff > 0 ? 1 : 0;
163
+ }
164
+ /** Returns the JSON representation of this type. */
165
+ toJSON() {
166
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "timestamp-millis");
167
+ }
168
+ }
169
+ /**
170
+ * Logical type for `timestamp-micros`.
171
+ * Represents an instant in time as the number of microseconds since the epoch.
172
+ */
173
+ export class TimestampMicrosLogicalType extends LogicalType {
174
+ /**
175
+ * Creates a new TimestampMicrosLogicalType.
176
+ * @param underlying The underlying long type.
177
+ */
178
+ constructor(underlying) {
179
+ super(underlying);
180
+ }
181
+ /** Checks if the logical type can read from the underlying type. */
182
+ canReadFromLogical(writer) {
183
+ return writer instanceof TimestampMicrosLogicalType;
184
+ }
185
+ /** Checks if the value is a valid instance of this logical type. */
186
+ isInstance(value) {
187
+ return typeof value === "bigint";
188
+ }
189
+ /** Converts the logical value to the underlying representation. */
190
+ toUnderlying(value) {
191
+ return value;
192
+ }
193
+ /** Converts the underlying value to the logical representation. */
194
+ fromUnderlying(value) {
195
+ return value;
196
+ }
197
+ /** Generates a random value for testing. */
198
+ random() {
199
+ const nowMicros = BigInt(Math.trunc(Date.now())) * 1000n;
200
+ const offset = BigInt(Math.floor(Math.random() * 1000000)) - 500000n;
201
+ return nowMicros + offset;
202
+ }
203
+ /** Returns the JSON representation of this type. */
204
+ toJSON() {
205
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "timestamp-micros");
206
+ }
207
+ }
208
+ /**
209
+ * Logical type for `timestamp-nanos`.
210
+ * Represents an instant in time as the number of nanoseconds since the epoch.
211
+ */
212
+ export class TimestampNanosLogicalType extends LogicalType {
213
+ /**
214
+ * Creates a new TimestampNanosLogicalType.
215
+ * @param underlying The underlying long type.
216
+ */
217
+ constructor(underlying) {
218
+ super(underlying);
219
+ }
220
+ /** Checks if the logical type can read from the underlying type. */
221
+ canReadFromLogical(writer) {
222
+ return writer instanceof TimestampNanosLogicalType;
223
+ }
224
+ /** Checks if the value is a valid instance of this logical type. */
225
+ isInstance(value) {
226
+ return typeof value === "bigint";
227
+ }
228
+ /** Converts the logical value to the underlying representation. */
229
+ toUnderlying(value) {
230
+ return value;
231
+ }
232
+ /** Converts the underlying value to the logical representation. */
233
+ fromUnderlying(value) {
234
+ return value;
235
+ }
236
+ /** Generates a random value for testing. */
237
+ random() {
238
+ const nowNanos = BigInt(Math.trunc(Date.now())) * 1000000n;
239
+ const offset = BigInt(Math.floor(Math.random() * 1000000)) - 500000n;
240
+ return nowNanos + offset;
241
+ }
242
+ /** Returns the JSON representation of this type. */
243
+ toJSON() {
244
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "timestamp-nanos");
245
+ }
246
+ }
247
+ /**
248
+ * Logical type for `local-timestamp-millis`.
249
+ * Represents a timestamp in a local time zone, in milliseconds.
250
+ */
251
+ export class LocalTimestampMillisLogicalType extends LogicalType {
252
+ /**
253
+ * Creates a new LocalTimestampMillisLogicalType.
254
+ * @param underlying The underlying long type.
255
+ */
256
+ constructor(underlying) {
257
+ super(underlying);
258
+ }
259
+ /** Checks if the logical type can read from the underlying type. */
260
+ canReadFromLogical(writer) {
261
+ return writer instanceof LocalTimestampMillisLogicalType;
262
+ }
263
+ /** Checks if the value is a valid instance of this logical type. */
264
+ isInstance(value) {
265
+ return typeof value === "number" && Number.isFinite(value) &&
266
+ Number.isInteger(value);
267
+ }
268
+ /** Converts the logical value to the underlying representation. */
269
+ toUnderlying(value) {
270
+ return BigInt(value);
271
+ }
272
+ /** Converts the underlying value to the logical representation. */
273
+ fromUnderlying(value) {
274
+ return bigIntToSafeNumber(value, "local-timestamp-millis");
275
+ }
276
+ /** Generates a random value for testing. */
277
+ random() {
278
+ const now = Date.now();
279
+ const offset = Math.floor(Math.random() * 1000000000) - 500000000;
280
+ return Math.trunc(now + offset);
281
+ }
282
+ /** Returns the JSON representation of this type. */
283
+ toJSON() {
284
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "local-timestamp-millis");
285
+ }
286
+ }
287
+ /**
288
+ * Logical type for `local-timestamp-micros`.
289
+ * Represents a timestamp in a local time zone, in microseconds.
290
+ */
291
+ export class LocalTimestampMicrosLogicalType extends LogicalType {
292
+ /**
293
+ * Creates a new LocalTimestampMicrosLogicalType.
294
+ * @param underlying The underlying long type.
295
+ */
296
+ constructor(underlying) {
297
+ super(underlying);
298
+ }
299
+ /** Checks if the logical type can read from the underlying type. */
300
+ canReadFromLogical(writer) {
301
+ return writer instanceof LocalTimestampMicrosLogicalType;
302
+ }
303
+ /** Checks if the value is a valid instance of this logical type. */
304
+ isInstance(value) {
305
+ return typeof value === "bigint";
306
+ }
307
+ /** Converts the logical value to the underlying representation. */
308
+ toUnderlying(value) {
309
+ return value;
310
+ }
311
+ /** Converts the underlying value to the logical representation. */
312
+ fromUnderlying(value) {
313
+ return value;
314
+ }
315
+ /** Generates a random value for testing. */
316
+ random() {
317
+ const nowMicros = BigInt(Math.trunc(Date.now())) * 1000n;
318
+ const offset = BigInt(Math.floor(Math.random() * 1000000)) - 500000n;
319
+ return nowMicros + offset;
320
+ }
321
+ /** Returns the JSON representation of this type. */
322
+ toJSON() {
323
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "local-timestamp-micros");
324
+ }
325
+ }
326
+ /**
327
+ * Logical type for `local-timestamp-nanos`.
328
+ * Represents a timestamp in a local time zone, in nanoseconds.
329
+ */
330
+ export class LocalTimestampNanosLogicalType extends LogicalType {
331
+ /**
332
+ * Creates a new LocalTimestampNanosLogicalType.
333
+ * @param underlying The underlying long type.
334
+ */
335
+ constructor(underlying) {
336
+ super(underlying);
337
+ }
338
+ /** Checks if the logical type can read from the underlying type. */
339
+ canReadFromLogical(writer) {
340
+ return writer instanceof LocalTimestampNanosLogicalType;
341
+ }
342
+ /** Checks if the value is a valid instance of this logical type. */
343
+ isInstance(value) {
344
+ return typeof value === "bigint";
345
+ }
346
+ /** Converts the logical value to the underlying representation. */
347
+ toUnderlying(value) {
348
+ return value;
349
+ }
350
+ /** Converts the underlying value to the logical representation. */
351
+ fromUnderlying(value) {
352
+ return value;
353
+ }
354
+ /** Generates a random value for testing. */
355
+ random() {
356
+ const nowNanos = BigInt(Math.trunc(Date.now())) * 1000000n;
357
+ const offset = BigInt(Math.floor(Math.random() * 1000000)) - 500000n;
358
+ return nowNanos + offset;
359
+ }
360
+ /** Returns the JSON representation of this type. */
361
+ toJSON() {
362
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "local-timestamp-nanos");
363
+ }
364
+ }
@@ -0,0 +1,63 @@
1
+ import { FixedType } from "../complex/fixed_type.js";
2
+ import { StringType } from "../primitive/string_type.js";
3
+ import { LogicalType } from "./logical_type.js";
4
+ import type { JSONType } from "../type.js";
5
+ /**
6
+ * Logical type for UUID.
7
+ * Represents a universally unique identifier.
8
+ */
9
+ export declare class UuidLogicalType extends LogicalType<string, string | Uint8Array> {
10
+ #private;
11
+ /**
12
+ * Creates a UUID logical type backed by the given underlying type.
13
+ * @param underlying The underlying StringType or FixedType.
14
+ */
15
+ constructor(underlying: StringType | FixedType);
16
+ /**
17
+ * Checks if this UUID logical type can read from the given writer logical type.
18
+ * @param writer The writer logical type.
19
+ * @returns True if compatible, false otherwise.
20
+ */
21
+ protected canReadFromLogical(writer: LogicalType<unknown, unknown>): boolean;
22
+ /**
23
+ * Determines if the given value is a valid UUID string.
24
+ * @param value The value to check.
25
+ * @returns True if the value is a valid UUID string.
26
+ */
27
+ protected isInstance(value: unknown): value is string;
28
+ /**
29
+ * Converts a UUID string to the underlying type's representation.
30
+ * @param value The UUID string.
31
+ * @returns The underlying representation.
32
+ */
33
+ protected toUnderlying(value: string): string | Uint8Array;
34
+ /**
35
+ * Converts from the underlying type's representation to a UUID string.
36
+ * @param value The underlying value.
37
+ * @returns The UUID string.
38
+ */
39
+ protected fromUnderlying(value: string | Uint8Array): string;
40
+ /**
41
+ * Generates a random UUID string.
42
+ */
43
+ random(): string;
44
+ /**
45
+ * Returns the JSON schema representation of this logical type.
46
+ */
47
+ toJSON(): JSONType;
48
+ /**
49
+ * Returns the full name of the named type backing this UUID.
50
+ * Throws if backed by a primitive string type.
51
+ */
52
+ getFullName(): string;
53
+ /**
54
+ * Returns the namespace of the named type backing this UUID.
55
+ * Throws if backed by a primitive string type.
56
+ */
57
+ getNamespace(): string;
58
+ /**
59
+ * Returns the aliases of the named type backing this UUID.
60
+ * Throws if backed by a primitive string type.
61
+ */
62
+ getAliases(): string[];
63
+ }
@@ -0,0 +1,146 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _UuidLogicalType_underlying, _UuidLogicalType_named;
13
+ import { FixedType } from "../complex/fixed_type.js";
14
+ import { StringType } from "../primitive/string_type.js";
15
+ import { withLogicalTypeJSON } from "./logical_type.js";
16
+ import { LogicalType } from "./logical_type.js";
17
+ const UUID_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
18
+ /**
19
+ * Logical type for UUID.
20
+ * Represents a universally unique identifier.
21
+ */
22
+ export class UuidLogicalType extends LogicalType {
23
+ /**
24
+ * Creates a UUID logical type backed by the given underlying type.
25
+ * @param underlying The underlying StringType or FixedType.
26
+ */
27
+ constructor(underlying) {
28
+ super(underlying);
29
+ _UuidLogicalType_underlying.set(this, void 0);
30
+ _UuidLogicalType_named.set(this, void 0);
31
+ __classPrivateFieldSet(this, _UuidLogicalType_underlying, underlying, "f");
32
+ if (underlying instanceof FixedType) {
33
+ if (underlying.getSize() !== 16) {
34
+ throw new Error("UUID logical type requires fixed size of 16 bytes.");
35
+ }
36
+ __classPrivateFieldSet(this, _UuidLogicalType_named, underlying, "f");
37
+ }
38
+ }
39
+ /**
40
+ * Checks if this UUID logical type can read from the given writer logical type.
41
+ * @param writer The writer logical type.
42
+ * @returns True if compatible, false otherwise.
43
+ */
44
+ canReadFromLogical(writer) {
45
+ return writer instanceof UuidLogicalType;
46
+ }
47
+ /**
48
+ * Determines if the given value is a valid UUID string.
49
+ * @param value The value to check.
50
+ * @returns True if the value is a valid UUID string.
51
+ */
52
+ isInstance(value) {
53
+ return typeof value === "string" && UUID_REGEX.test(value);
54
+ }
55
+ /**
56
+ * Converts a UUID string to the underlying type's representation.
57
+ * @param value The UUID string.
58
+ * @returns The underlying representation.
59
+ */
60
+ toUnderlying(value) {
61
+ if (__classPrivateFieldGet(this, _UuidLogicalType_underlying, "f") instanceof StringType) {
62
+ return value;
63
+ }
64
+ return uuidToBytes(value);
65
+ }
66
+ /**
67
+ * Converts from the underlying type's representation to a UUID string.
68
+ * @param value The underlying value.
69
+ * @returns The UUID string.
70
+ */
71
+ fromUnderlying(value) {
72
+ if (typeof value === "string") {
73
+ return value;
74
+ }
75
+ return bytesToUuid(value);
76
+ }
77
+ /**
78
+ * Generates a random UUID string.
79
+ */
80
+ random() {
81
+ if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
82
+ return crypto.randomUUID();
83
+ }
84
+ const bytes = new Uint8Array(16);
85
+ for (let i = 0; i < bytes.length; i++) {
86
+ bytes[i] = Math.floor(Math.random() * 256);
87
+ }
88
+ // Set version (4) and variant bits for RFC-4122 compliance.
89
+ bytes[6] = (bytes[6] & 0x0f) | 0x40;
90
+ bytes[8] = (bytes[8] & 0x3f) | 0x80;
91
+ return bytesToUuid(bytes);
92
+ }
93
+ /**
94
+ * Returns the JSON schema representation of this logical type.
95
+ */
96
+ toJSON() {
97
+ return withLogicalTypeJSON(__classPrivateFieldGet(this, _UuidLogicalType_underlying, "f").toJSON(), "uuid");
98
+ }
99
+ /**
100
+ * Returns the full name of the named type backing this UUID.
101
+ * Throws if backed by a primitive string type.
102
+ */
103
+ getFullName() {
104
+ if (!__classPrivateFieldGet(this, _UuidLogicalType_named, "f")) {
105
+ throw new Error("UUID logical type backed by string has no name.");
106
+ }
107
+ return __classPrivateFieldGet(this, _UuidLogicalType_named, "f").getFullName();
108
+ }
109
+ /**
110
+ * Returns the namespace of the named type backing this UUID.
111
+ * Throws if backed by a primitive string type.
112
+ */
113
+ getNamespace() {
114
+ if (!__classPrivateFieldGet(this, _UuidLogicalType_named, "f")) {
115
+ throw new Error("UUID logical type backed by string has no namespace.");
116
+ }
117
+ return __classPrivateFieldGet(this, _UuidLogicalType_named, "f").getNamespace();
118
+ }
119
+ /**
120
+ * Returns the aliases of the named type backing this UUID.
121
+ * Throws if backed by a primitive string type.
122
+ */
123
+ getAliases() {
124
+ if (!__classPrivateFieldGet(this, _UuidLogicalType_named, "f")) {
125
+ throw new Error("UUID logical type backed by string has no aliases.");
126
+ }
127
+ return __classPrivateFieldGet(this, _UuidLogicalType_named, "f").getAliases();
128
+ }
129
+ }
130
+ _UuidLogicalType_underlying = new WeakMap(), _UuidLogicalType_named = new WeakMap();
131
+ function uuidToBytes(value) {
132
+ const hex = value.replace(/-/g, "");
133
+ const bytes = new Uint8Array(16);
134
+ for (let i = 0; i < 16; i++) {
135
+ bytes[i] = Number.parseInt(hex.slice(i * 2, i * 2 + 2), 16);
136
+ }
137
+ return bytes;
138
+ }
139
+ function bytesToUuid(bytes) {
140
+ if (bytes.length !== 16) {
141
+ throw new Error("UUID bytes must be 16 bytes long.");
142
+ }
143
+ const hex = Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0"))
144
+ .join("");
145
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
146
+ }
@@ -0,0 +1,49 @@
1
+ import type { ReadableTapLike, WritableTapLike } from "../../serialization/tap.js";
2
+ import { FixedSizeBaseType } from "./fixed_size_base_type.js";
3
+ import type { JSONType } from "../type.js";
4
+ import { type ErrorHook } from "../error.js";
5
+ /**
6
+ * Boolean type.
7
+ */
8
+ export declare class BooleanType extends FixedSizeBaseType<boolean> {
9
+ /**
10
+ * Validates if the value is a boolean.
11
+ */
12
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
13
+ /**
14
+ * Reads a boolean value from the tap.
15
+ */
16
+ read(tap: ReadableTapLike): Promise<boolean>;
17
+ /**
18
+ * Writes a boolean value to the tap.
19
+ */
20
+ write(tap: WritableTapLike, value: boolean): Promise<void>;
21
+ /**
22
+ * Skips a boolean value in the tap.
23
+ */
24
+ skip(tap: ReadableTapLike): Promise<void>;
25
+ /**
26
+ * Gets the size in bytes.
27
+ */
28
+ sizeBytes(): number;
29
+ /**
30
+ * Clones a boolean value from the given value.
31
+ */
32
+ cloneFromValue(value: unknown): boolean;
33
+ /**
34
+ * Compares two boolean values.
35
+ */
36
+ compare(val1: boolean, val2: boolean): number;
37
+ /**
38
+ * Generates a random boolean value.
39
+ */
40
+ random(): boolean;
41
+ /**
42
+ * Returns the JSON representation of the boolean type.
43
+ */
44
+ toJSON(): JSONType;
45
+ /**
46
+ * Matches two boolean values from the taps.
47
+ */
48
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
49
+ }
@@ -0,0 +1,75 @@
1
+ import { FixedSizeBaseType } from "./fixed_size_base_type.js";
2
+ import { throwInvalidError } from "../error.js";
3
+ /**
4
+ * Boolean type.
5
+ */
6
+ export class BooleanType extends FixedSizeBaseType {
7
+ /**
8
+ * Validates if the value is a boolean.
9
+ */
10
+ check(value, errorHook, path = []) {
11
+ const isValid = typeof value === "boolean";
12
+ if (!isValid && errorHook) {
13
+ errorHook(path, value, this);
14
+ }
15
+ return isValid;
16
+ }
17
+ /**
18
+ * Reads a boolean value from the tap.
19
+ */
20
+ async read(tap) {
21
+ return await tap.readBoolean();
22
+ }
23
+ /**
24
+ * Writes a boolean value to the tap.
25
+ */
26
+ async write(tap, value) {
27
+ if (typeof value !== "boolean") {
28
+ throwInvalidError([], value, this);
29
+ }
30
+ await tap.writeBoolean(value);
31
+ }
32
+ /**
33
+ * Skips a boolean value in the tap.
34
+ */
35
+ async skip(tap) {
36
+ await tap.skipBoolean();
37
+ }
38
+ /**
39
+ * Gets the size in bytes.
40
+ */
41
+ sizeBytes() {
42
+ return 1; // 1 byte
43
+ }
44
+ /**
45
+ * Clones a boolean value from the given value.
46
+ */
47
+ cloneFromValue(value) {
48
+ this.check(value, throwInvalidError, []);
49
+ return value;
50
+ }
51
+ /**
52
+ * Compares two boolean values.
53
+ */
54
+ compare(val1, val2) {
55
+ return val1 === val2 ? 0 : val1 ? 1 : -1;
56
+ }
57
+ /**
58
+ * Generates a random boolean value.
59
+ */
60
+ random() {
61
+ return Math.random() < 0.5;
62
+ }
63
+ /**
64
+ * Returns the JSON representation of the boolean type.
65
+ */
66
+ toJSON() {
67
+ return "boolean";
68
+ }
69
+ /**
70
+ * Matches two boolean values from the taps.
71
+ */
72
+ async match(tap1, tap2) {
73
+ return await tap1.matchBoolean(tap2);
74
+ }
75
+ }