@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,49 @@
1
+ import { BytesType } from "../primitive/bytes_type.js";
2
+ import { FixedType } from "../complex/fixed_type.js";
3
+ import { LogicalType } from "./logical_type.js";
4
+ import type { JSONType } from "../type.js";
5
+ interface DecimalParams {
6
+ precision: number;
7
+ scale?: number;
8
+ }
9
+ /**
10
+ * Decimal logical type.
11
+ */
12
+ export declare class DecimalLogicalType extends LogicalType<bigint, Uint8Array> {
13
+ #private;
14
+ /**
15
+ * Creates a new DecimalLogicalType.
16
+ * @param underlying The underlying bytes or fixed type.
17
+ * @param params The precision and scale parameters.
18
+ */
19
+ constructor(underlying: BytesType | FixedType, params: DecimalParams);
20
+ /**
21
+ * Gets the precision of the decimal.
22
+ */
23
+ getPrecision(): number;
24
+ /**
25
+ * Gets the scale of the decimal.
26
+ */
27
+ getScale(): number;
28
+ /**
29
+ * Checks if a writer schema can be read by this logical type.
30
+ */
31
+ protected canReadFromLogical(writer: LogicalType<unknown, unknown>): boolean;
32
+ /**
33
+ * Checks if the given value is a bigint representing a decimal.
34
+ */
35
+ protected isInstance(value: unknown): value is bigint;
36
+ /**
37
+ * Converts a bigint decimal value to its underlying byte representation.
38
+ */
39
+ protected toUnderlying(value: bigint): Uint8Array;
40
+ /**
41
+ * Converts an underlying byte array to a bigint decimal value.
42
+ */
43
+ protected fromUnderlying(value: Uint8Array): bigint;
44
+ /**
45
+ * Returns the JSON representation of this decimal logical type.
46
+ */
47
+ toJSON(): JSONType;
48
+ }
49
+ export {};
@@ -0,0 +1,145 @@
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 _DecimalLogicalType_precision, _DecimalLogicalType_scale, _DecimalLogicalType_fixedSize;
13
+ import { BytesType } from "../primitive/bytes_type.js";
14
+ import { FixedType } from "../complex/fixed_type.js";
15
+ import { withLogicalTypeJSON } from "./logical_type.js";
16
+ import { LogicalType } from "./logical_type.js";
17
+ /**
18
+ * Decimal logical type.
19
+ */
20
+ export class DecimalLogicalType extends LogicalType {
21
+ /**
22
+ * Creates a new DecimalLogicalType.
23
+ * @param underlying The underlying bytes or fixed type.
24
+ * @param params The precision and scale parameters.
25
+ */
26
+ constructor(underlying, params) {
27
+ super(underlying);
28
+ _DecimalLogicalType_precision.set(this, void 0);
29
+ _DecimalLogicalType_scale.set(this, void 0);
30
+ _DecimalLogicalType_fixedSize.set(this, void 0);
31
+ if (!(underlying instanceof BytesType || underlying instanceof FixedType)) {
32
+ throw new Error("Decimal logical type requires bytes or fixed underlying type.");
33
+ }
34
+ const precision = params.precision;
35
+ if (!Number.isInteger(precision) || precision <= 0) {
36
+ throw new Error("Decimal logical type requires a positive precision.");
37
+ }
38
+ const scale = params.scale ?? 0;
39
+ if (!Number.isInteger(scale) || scale < 0 || scale > precision) {
40
+ throw new Error("Decimal logical type requires a valid scale (0 <= scale <= precision).");
41
+ }
42
+ if (underlying instanceof FixedType) {
43
+ const size = underlying.getSize();
44
+ const maxPrecision = Math.floor(Math.log10(2) * (8 * size - 1));
45
+ if (precision > maxPrecision) {
46
+ throw new Error("Decimal precision exceeds maximum for fixed size.");
47
+ }
48
+ __classPrivateFieldSet(this, _DecimalLogicalType_fixedSize, size, "f");
49
+ }
50
+ __classPrivateFieldSet(this, _DecimalLogicalType_precision, precision, "f");
51
+ __classPrivateFieldSet(this, _DecimalLogicalType_scale, scale, "f");
52
+ }
53
+ /**
54
+ * Gets the precision of the decimal.
55
+ */
56
+ getPrecision() {
57
+ return __classPrivateFieldGet(this, _DecimalLogicalType_precision, "f");
58
+ }
59
+ /**
60
+ * Gets the scale of the decimal.
61
+ */
62
+ getScale() {
63
+ return __classPrivateFieldGet(this, _DecimalLogicalType_scale, "f");
64
+ }
65
+ /**
66
+ * Checks if a writer schema can be read by this logical type.
67
+ */
68
+ canReadFromLogical(writer) {
69
+ return writer instanceof DecimalLogicalType &&
70
+ __classPrivateFieldGet(writer, _DecimalLogicalType_precision, "f") === __classPrivateFieldGet(this, _DecimalLogicalType_precision, "f") &&
71
+ __classPrivateFieldGet(writer, _DecimalLogicalType_scale, "f") === __classPrivateFieldGet(this, _DecimalLogicalType_scale, "f");
72
+ }
73
+ /**
74
+ * Checks if the given value is a bigint representing a decimal.
75
+ */
76
+ isInstance(value) {
77
+ return typeof value === "bigint";
78
+ }
79
+ /**
80
+ * Converts a bigint decimal value to its underlying byte representation.
81
+ */
82
+ toUnderlying(value) {
83
+ const digits = value < 0n
84
+ ? (-value).toString().length
85
+ : value.toString().length;
86
+ if (digits > __classPrivateFieldGet(this, _DecimalLogicalType_precision, "f")) {
87
+ throw new Error(`Decimal value: ${value} exceeds declared precision: ${__classPrivateFieldGet(this, _DecimalLogicalType_precision, "f")}`);
88
+ }
89
+ const size = __classPrivateFieldGet(this, _DecimalLogicalType_fixedSize, "f");
90
+ // Constructor already enforced precision <= max for the fixed size, so every
91
+ // value that passes the precision check fits within `size` bytes.
92
+ const byteLength = size ?? minimalBytesForValue(value);
93
+ return encodeBigInt(value, byteLength);
94
+ }
95
+ /**
96
+ * Converts an underlying byte array to a bigint decimal value.
97
+ */
98
+ fromUnderlying(value) {
99
+ return decodeBigInt(value);
100
+ }
101
+ /**
102
+ * Returns the JSON representation of this decimal logical type.
103
+ */
104
+ toJSON() {
105
+ const extras = {
106
+ precision: __classPrivateFieldGet(this, _DecimalLogicalType_precision, "f"),
107
+ };
108
+ if (__classPrivateFieldGet(this, _DecimalLogicalType_scale, "f") !== 0) {
109
+ extras.scale = __classPrivateFieldGet(this, _DecimalLogicalType_scale, "f");
110
+ }
111
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "decimal", extras);
112
+ }
113
+ }
114
+ _DecimalLogicalType_precision = new WeakMap(), _DecimalLogicalType_scale = new WeakMap(), _DecimalLogicalType_fixedSize = new WeakMap();
115
+ function minimalBytesForValue(value) {
116
+ let bytes = 1;
117
+ while (value < -(1n << BigInt(bytes * 8 - 1)) ||
118
+ value >= (1n << BigInt(bytes * 8 - 1))) {
119
+ bytes++;
120
+ }
121
+ return bytes;
122
+ }
123
+ function encodeBigInt(value, size) {
124
+ let twos = BigInt.asUintN(size * 8, value);
125
+ const result = new Uint8Array(size);
126
+ for (let i = size - 1; i >= 0; i--) {
127
+ result[i] = Number(twos & 0xffn);
128
+ twos >>= 8n;
129
+ }
130
+ return result;
131
+ }
132
+ function decodeBigInt(bytes) {
133
+ if (bytes.length === 0) {
134
+ return 0n;
135
+ }
136
+ let value = 0n;
137
+ for (const byte of bytes) {
138
+ value = (value << 8n) | BigInt(byte);
139
+ }
140
+ if (bytes[0] & 0x80) {
141
+ const max = 1n << BigInt(bytes.length * 8);
142
+ value -= max;
143
+ }
144
+ return value;
145
+ }
@@ -0,0 +1,66 @@
1
+ import type { FixedType } from "../complex/fixed_type.js";
2
+ import { type LogicalType, NamedLogicalType } from "./logical_type.js";
3
+ import type { JSONType } from "../type.js";
4
+ /**
5
+ * Represents a duration value with months, days, and milliseconds.
6
+ */
7
+ export interface DurationValue {
8
+ /** Number of months. */
9
+ months: number;
10
+ /** Number of days. */
11
+ days: number;
12
+ /** Number of milliseconds. */
13
+ millis: number;
14
+ }
15
+ /**
16
+ * Logical type for `duration`.
17
+ * Represents a duration of time.
18
+ */
19
+ export declare class DurationLogicalType extends NamedLogicalType<DurationValue, Uint8Array> {
20
+ /**
21
+ * Creates a new DurationLogicalType.
22
+ * @param underlying The underlying fixed type (must be 12 bytes).
23
+ */
24
+ constructor(underlying: FixedType);
25
+ /**
26
+ * Checks if this logical type can read from the given writer logical type.
27
+ * @param writer The writer logical type.
28
+ * @returns True if compatible.
29
+ */
30
+ protected canReadFromLogical(writer: LogicalType<unknown, unknown>): boolean;
31
+ /**
32
+ * Checks if the value is an instance of DurationValue.
33
+ * @param value The value to check.
34
+ * @returns True if the value is a DurationValue.
35
+ */
36
+ protected isInstance(value: unknown): value is DurationValue;
37
+ /**
38
+ * Converts the logical DurationValue to its underlying Uint8Array representation.
39
+ * @param value The DurationValue to convert.
40
+ * @returns The Uint8Array representation.
41
+ */
42
+ protected toUnderlying(value: DurationValue): Uint8Array;
43
+ /**
44
+ * Converts the underlying Uint8Array to a DurationValue.
45
+ * @param value The underlying bytes (must be 12 bytes).
46
+ * @returns The duration value.
47
+ */
48
+ protected fromUnderlying(value: Uint8Array): DurationValue;
49
+ /**
50
+ * Compares two DurationValue instances lexicographically.
51
+ * @param a The first duration value.
52
+ * @param b The second duration value.
53
+ * @returns -1 if a < b, 0 if equal, 1 if a > b.
54
+ */
55
+ compare(a: DurationValue, b: DurationValue): number;
56
+ /**
57
+ * Generates a random DurationValue.
58
+ * @returns A random duration value.
59
+ */
60
+ random(): DurationValue;
61
+ /**
62
+ * Returns the JSON representation of this type.
63
+ * @returns The JSON type.
64
+ */
65
+ toJSON(): JSONType;
66
+ }
@@ -0,0 +1,112 @@
1
+ import { NamedLogicalType, withLogicalTypeJSON, } from "./logical_type.js";
2
+ const MAX_UINT32 = 0xffffffff;
3
+ /**
4
+ * Logical type for `duration`.
5
+ * Represents a duration of time.
6
+ */
7
+ export class DurationLogicalType extends NamedLogicalType {
8
+ /**
9
+ * Creates a new DurationLogicalType.
10
+ * @param underlying The underlying fixed type (must be 12 bytes).
11
+ */
12
+ constructor(underlying) {
13
+ super(underlying);
14
+ if (underlying.getSize() !== 12) {
15
+ throw new Error("Duration logical type requires fixed size of 12 bytes.");
16
+ }
17
+ }
18
+ /**
19
+ * Checks if this logical type can read from the given writer logical type.
20
+ * @param writer The writer logical type.
21
+ * @returns True if compatible.
22
+ */
23
+ canReadFromLogical(writer) {
24
+ return writer instanceof DurationLogicalType;
25
+ }
26
+ /**
27
+ * Checks if the value is an instance of DurationValue.
28
+ * @param value The value to check.
29
+ * @returns True if the value is a DurationValue.
30
+ */
31
+ isInstance(value) {
32
+ return isDurationValue(value);
33
+ }
34
+ /**
35
+ * Converts the logical DurationValue to its underlying Uint8Array representation.
36
+ * @param value The DurationValue to convert.
37
+ * @returns The Uint8Array representation.
38
+ */
39
+ toUnderlying(value) {
40
+ // The value is always valid here since isInstance ensures it.
41
+ const buffer = new ArrayBuffer(12);
42
+ const view = new DataView(buffer);
43
+ view.setUint32(0, value.months, true);
44
+ view.setUint32(4, value.days, true);
45
+ view.setUint32(8, value.millis, true);
46
+ return new Uint8Array(buffer);
47
+ }
48
+ /**
49
+ * Converts the underlying Uint8Array to a DurationValue.
50
+ * @param value The underlying bytes (must be 12 bytes).
51
+ * @returns The duration value.
52
+ */
53
+ fromUnderlying(value) {
54
+ if (value.length !== 12) {
55
+ throw new Error("Duration bytes must be 12 bytes long.");
56
+ }
57
+ const view = new DataView(value.buffer, value.byteOffset, value.byteLength);
58
+ return {
59
+ months: view.getUint32(0, true),
60
+ days: view.getUint32(4, true),
61
+ millis: view.getUint32(8, true),
62
+ };
63
+ }
64
+ /**
65
+ * Compares two DurationValue instances lexicographically.
66
+ * @param a The first duration value.
67
+ * @param b The second duration value.
68
+ * @returns -1 if a < b, 0 if equal, 1 if a > b.
69
+ */
70
+ compare(a, b) {
71
+ if (a.months !== b.months) {
72
+ return a.months < b.months ? -1 : 1;
73
+ }
74
+ if (a.days !== b.days) {
75
+ return a.days < b.days ? -1 : 1;
76
+ }
77
+ if (a.millis !== b.millis) {
78
+ return a.millis < b.millis ? -1 : 1;
79
+ }
80
+ return 0;
81
+ }
82
+ /**
83
+ * Generates a random DurationValue.
84
+ * @returns A random duration value.
85
+ */
86
+ random() {
87
+ return {
88
+ months: Math.floor(Math.random() * 1200),
89
+ days: Math.floor(Math.random() * 365),
90
+ millis: Math.floor(Math.random() * 86400000),
91
+ };
92
+ }
93
+ /**
94
+ * Returns the JSON representation of this type.
95
+ * @returns The JSON type.
96
+ */
97
+ toJSON() {
98
+ return withLogicalTypeJSON(this.getUnderlyingType().toJSON(), "duration");
99
+ }
100
+ }
101
+ function isDurationValue(value) {
102
+ if (typeof value !== "object" || value === null) {
103
+ return false;
104
+ }
105
+ const maybe = value;
106
+ return isUint32(maybe.months) && isUint32(maybe.days) &&
107
+ isUint32(maybe.millis);
108
+ }
109
+ function isUint32(value) {
110
+ return typeof value === "number" && Number.isInteger(value) &&
111
+ value >= 0 && value <= MAX_UINT32;
112
+ }
@@ -0,0 +1,138 @@
1
+ import type { ReadableTapLike, WritableTapLike } from "../../serialization/tap.js";
2
+ import { Resolver } from "../resolver.js";
3
+ import type { NamedType } from "../complex/named_type.js";
4
+ import { Type } from "../type.js";
5
+ import type { JSONType } from "../type.js";
6
+ import { type ErrorHook } from "../error.js";
7
+ /**
8
+ * Base class for Avro logical types that wrap an underlying physical type
9
+ * while exposing a richer TypeScript representation.
10
+ */
11
+ export declare abstract class LogicalType<TValue, TUnderlying> extends Type<TValue> {
12
+ /** The underlying type that this logical type wraps. */
13
+ protected readonly underlyingType: Type<TUnderlying>;
14
+ /**
15
+ * Constructs a new LogicalType.
16
+ * @param underlyingType - The underlying type to wrap.
17
+ */
18
+ protected constructor(underlyingType: Type<TUnderlying>);
19
+ /**
20
+ * Gets the underlying type that this logical type wraps.
21
+ */
22
+ getUnderlyingType(): Type<TUnderlying>;
23
+ /**
24
+ * Checks if a value is an instance of the logical type's value.
25
+ */
26
+ protected abstract isInstance(value: unknown): value is TValue;
27
+ /**
28
+ * Converts the logical value to its underlying representation.
29
+ */
30
+ protected abstract toUnderlying(value: TValue): TUnderlying;
31
+ /**
32
+ * Converts the underlying representation to the logical value.
33
+ */
34
+ protected abstract fromUnderlying(value: TUnderlying): TValue;
35
+ /**
36
+ * Converts a value from the underlying type to the logical type value.
37
+ * @param value The underlying value.
38
+ */
39
+ convertFromUnderlying(value: TUnderlying): TValue;
40
+ /**
41
+ * Determines if this logical type is compatible with the writer logical type for reading.
42
+ */
43
+ protected canReadFromLogical(_writer: LogicalType<unknown, unknown>): boolean;
44
+ /**
45
+ * Serializes the logical value to an ArrayBuffer.
46
+ * @param value The logical value to serialize.
47
+ */
48
+ toBuffer(value: TValue): Promise<ArrayBuffer>;
49
+ /**
50
+ * Deserializes a logical value from an ArrayBuffer.
51
+ * @param buffer The buffer to deserialize from.
52
+ */
53
+ fromBuffer(buffer: ArrayBuffer): Promise<TValue>;
54
+ /**
55
+ * Checks if the given value is valid for this type.
56
+ * @param value The value to validate.
57
+ * @param opts Optional validation options.
58
+ */
59
+ isValid(value: unknown, opts?: {
60
+ errorHook?: ErrorHook;
61
+ }): boolean;
62
+ /**
63
+ * Validates the value against this type, reporting errors via the hook if provided.
64
+ * @param value The value to check.
65
+ * @param errorHook Optional hook for reporting validation errors.
66
+ * @param path The path to the value in the schema.
67
+ */
68
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
69
+ /**
70
+ * Clones a value, ensuring it is valid for this type.
71
+ */
72
+ cloneFromValue(value: unknown): TValue;
73
+ /**
74
+ * Compares two values by comparing their underlying representations.
75
+ */
76
+ compare(val1: TValue, val2: TValue): number;
77
+ /**
78
+ * Generates a random value by generating a random underlying value and converting it.
79
+ */
80
+ random(): TValue;
81
+ /**
82
+ * Writes the value to the tap by writing the underlying value.
83
+ */
84
+ write(tap: WritableTapLike, value: TValue): Promise<void>;
85
+ /** Reads the value from the tap. */
86
+ read(tap: ReadableTapLike): Promise<TValue>;
87
+ /** Skips the value in the tap. */
88
+ skip(tap: ReadableTapLike): Promise<void>;
89
+ /** Matches the value in the taps. */
90
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
91
+ /**
92
+ * Creates a resolver for reading from the writer type, handling logical types specially.
93
+ */
94
+ createResolver(writerType: Type): Resolver;
95
+ /**
96
+ * Ensures the value is valid for this logical type.
97
+ * @param value The value to validate.
98
+ * @param path The path for error reporting.
99
+ */
100
+ protected ensureValid(value: TValue, path: string[]): void;
101
+ /**
102
+ * Returns the JSON representation of this logical type.
103
+ */
104
+ abstract toJSON(): JSONType;
105
+ }
106
+ /**
107
+ * Logical type variant for named Avro schemas (record, enum, fixed) that keeps
108
+ * track of the fully qualified name and aliases of the underlying type.
109
+ */
110
+ /**
111
+ * Logical type variant for named Avro schemas (record, enum, fixed) that keeps
112
+ * track of the fully qualified name and aliases of the underlying type.
113
+ */
114
+ export declare abstract class NamedLogicalType<TValue, TUnderlying> extends LogicalType<TValue, TUnderlying> {
115
+ /** The underlying named type. */
116
+ protected readonly namedType: NamedType<TUnderlying>;
117
+ /**
118
+ * Creates a new NamedLogicalType.
119
+ * @param namedType The underlying named type.
120
+ */
121
+ protected constructor(namedType: NamedType<TUnderlying>);
122
+ /**
123
+ * Gets the full name of the underlying type.
124
+ */
125
+ getFullName(): string;
126
+ /**
127
+ * Gets the namespace of the underlying type.
128
+ */
129
+ getNamespace(): string;
130
+ /**
131
+ * Gets the aliases of the underlying type.
132
+ */
133
+ getAliases(): string[];
134
+ }
135
+ /**
136
+ * Helper to attach a logicalType annotation to an underlying Avro schema JSON.
137
+ */
138
+ export declare function withLogicalTypeJSON(underlying: JSONType, logicalType: string, extras?: Record<string, unknown>): JSONType;