@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 { type ReadableTapLike, type WritableTapLike } from "../../serialization/tap.js";
2
+ import { PrimitiveType } from "./primitive_type.js";
3
+ import type { JSONType, Type } from "../type.js";
4
+ import { Resolver } from "../resolver.js";
5
+ import { type ErrorHook } from "../error.js";
6
+ /**
7
+ * Bytes type.
8
+ */
9
+ export declare class BytesType extends PrimitiveType<Uint8Array> {
10
+ #private;
11
+ /** Checks if the value is a valid bytes array. */
12
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
13
+ /**
14
+ * Reads a byte array from the tap.
15
+ */
16
+ read(tap: ReadableTapLike): Promise<Uint8Array>;
17
+ /**
18
+ * Writes a byte array to the tap.
19
+ */
20
+ write(tap: WritableTapLike, value: Uint8Array): Promise<void>;
21
+ /**
22
+ * Skips a byte array in the tap.
23
+ */
24
+ skip(tap: ReadableTapLike): Promise<void>;
25
+ /**
26
+ * Converts a byte array to an ArrayBuffer.
27
+ */
28
+ toBuffer(value: Uint8Array): Promise<ArrayBuffer>;
29
+ /**
30
+ * Creates a resolver for the writer type.
31
+ */
32
+ createResolver(writerType: Type): Resolver;
33
+ /**
34
+ * Compares two byte arrays.
35
+ */
36
+ compare(val1: Uint8Array, val2: Uint8Array): number;
37
+ /**
38
+ * Clones a byte array value.
39
+ */
40
+ cloneFromValue(value: unknown): Uint8Array;
41
+ /**
42
+ * Generates a random byte array.
43
+ */
44
+ random(): Uint8Array;
45
+ /** Returns the JSON representation of the type. */
46
+ toJSON(): JSONType;
47
+ /** Matches bytes between two taps. */
48
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
49
+ }
@@ -0,0 +1,136 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ 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");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var _a, _BytesType_fromJsonString;
7
+ import { WritableTap, } from "../../serialization/tap.js";
8
+ import { PrimitiveType } from "./primitive_type.js";
9
+ import { Resolver } from "../resolver.js";
10
+ import { throwInvalidError } from "../error.js";
11
+ import { calculateVarintSize } from "../../internal/varint.js";
12
+ /**
13
+ * Bytes type.
14
+ */
15
+ export class BytesType extends PrimitiveType {
16
+ /** Checks if the value is a valid bytes array. */
17
+ check(value, errorHook, path = []) {
18
+ const isValid = value instanceof Uint8Array;
19
+ if (!isValid && errorHook) {
20
+ errorHook(path, value, this);
21
+ }
22
+ return isValid;
23
+ }
24
+ /**
25
+ * Reads a byte array from the tap.
26
+ */
27
+ async read(tap) {
28
+ return await tap.readBytes();
29
+ }
30
+ /**
31
+ * Writes a byte array to the tap.
32
+ */
33
+ async write(tap, value) {
34
+ if (!(value instanceof Uint8Array)) {
35
+ throwInvalidError([], value, this);
36
+ }
37
+ await tap.writeBytes(value);
38
+ }
39
+ /**
40
+ * Skips a byte array in the tap.
41
+ */
42
+ async skip(tap) {
43
+ await tap.skipBytes();
44
+ }
45
+ /**
46
+ * Converts a byte array to an ArrayBuffer.
47
+ */
48
+ async toBuffer(value) {
49
+ this.check(value, throwInvalidError, []);
50
+ // Pre-allocate buffer based on value length for efficiency
51
+ const lengthSize = calculateVarintSize(value.length);
52
+ const totalSize = lengthSize + value.length;
53
+ const buf = new ArrayBuffer(totalSize);
54
+ const tap = new WritableTap(buf);
55
+ await this.write(tap, value);
56
+ return buf;
57
+ }
58
+ /**
59
+ * Creates a resolver for the writer type.
60
+ */
61
+ createResolver(writerType) {
62
+ if (writerType.toJSON() === "string") {
63
+ // Bytes can promote from string. We use an anonymous class here to avoid a
64
+ // cyclic dependency between this file and the string type file.
65
+ return new class extends Resolver {
66
+ async read(tap) {
67
+ const str = await tap.readString();
68
+ // Convert string to bytes (assuming UTF-8)
69
+ const encoder = new TextEncoder();
70
+ return encoder.encode(str);
71
+ }
72
+ }(this);
73
+ }
74
+ else {
75
+ return super.createResolver(writerType);
76
+ }
77
+ }
78
+ /**
79
+ * Compares two byte arrays.
80
+ */
81
+ compare(val1, val2) {
82
+ const len1 = val1.length;
83
+ const len2 = val2.length;
84
+ const len = Math.min(len1, len2);
85
+ for (let i = 0; i < len; i++) {
86
+ if (val1[i] !== val2[i]) {
87
+ return val1[i] < val2[i] ? -1 : 1;
88
+ }
89
+ }
90
+ return len1 < len2 ? -1 : len1 > len2 ? 1 : 0;
91
+ }
92
+ /**
93
+ * Clones a byte array value.
94
+ */
95
+ cloneFromValue(value) {
96
+ let bytes;
97
+ if (value instanceof Uint8Array) {
98
+ bytes = value;
99
+ }
100
+ else if (typeof value === "string") {
101
+ bytes = __classPrivateFieldGet(_a, _a, "m", _BytesType_fromJsonString).call(_a, value);
102
+ }
103
+ else {
104
+ throwInvalidError([], value, this);
105
+ }
106
+ this.check(bytes, throwInvalidError, []);
107
+ return new Uint8Array(bytes);
108
+ }
109
+ /**
110
+ * Generates a random byte array.
111
+ */
112
+ random() {
113
+ // Generate at least one byte.
114
+ const len = Math.ceil(Math.random() * 31) + 1;
115
+ const buf = new Uint8Array(len);
116
+ for (let i = 0; i < len; i++) {
117
+ buf[i] = Math.floor(Math.random() * 256);
118
+ }
119
+ return buf;
120
+ }
121
+ /** Returns the JSON representation of the type. */
122
+ toJSON() {
123
+ return "bytes";
124
+ }
125
+ /** Matches bytes between two taps. */
126
+ async match(tap1, tap2) {
127
+ return await tap1.matchBytes(tap2);
128
+ }
129
+ }
130
+ _a = BytesType, _BytesType_fromJsonString = function _BytesType_fromJsonString(value) {
131
+ const bytes = new Uint8Array(value.length);
132
+ for (let i = 0; i < value.length; i++) {
133
+ bytes[i] = value.charCodeAt(i) & 0xff;
134
+ }
135
+ return bytes;
136
+ };
@@ -0,0 +1,38 @@
1
+ import type { ReadableTapLike, WritableTapLike } from "../../serialization/tap.js";
2
+ import { FixedSizeBaseType } from "./fixed_size_base_type.js";
3
+ import type { JSONType, Type } from "../type.js";
4
+ import { Resolver } from "../resolver.js";
5
+ import { type ErrorHook } from "../error.js";
6
+ /**
7
+ * Double type (64-bit).
8
+ */
9
+ export declare class DoubleType extends FixedSizeBaseType<number> {
10
+ /** Checks if the value is a valid double. */
11
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
12
+ /** Reads a double value from the tap. */
13
+ read(tap: ReadableTapLike): Promise<number>;
14
+ /** Writes a double value to the tap. */
15
+ write(tap: WritableTapLike, value: number): Promise<void>;
16
+ /** Skips a double value in the tap. */
17
+ skip(tap: ReadableTapLike): Promise<void>;
18
+ /**
19
+ * Gets the size in bytes.
20
+ */
21
+ sizeBytes(): number;
22
+ /** Clones the value to a number. */
23
+ cloneFromValue(value: unknown): number;
24
+ /**
25
+ * Compares two double values.
26
+ */
27
+ compare(val1: number, val2: number): number;
28
+ /**
29
+ * Generates a random double value.
30
+ */
31
+ random(): number;
32
+ /** Creates a resolver for the given writer type. */
33
+ createResolver(writerType: Type): Resolver;
34
+ /** Returns the JSON representation of the type. */
35
+ toJSON(): JSONType;
36
+ /** Matches two taps for equality. */
37
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
38
+ }
@@ -0,0 +1,98 @@
1
+ import { FixedSizeBaseType } from "./fixed_size_base_type.js";
2
+ import { Resolver } from "../resolver.js";
3
+ import { IntType } from "./int_type.js";
4
+ import { LongType } from "./long_type.js";
5
+ import { FloatType } from "./float_type.js";
6
+ import { throwInvalidError } from "../error.js";
7
+ /**
8
+ * Double type (64-bit).
9
+ */
10
+ export class DoubleType extends FixedSizeBaseType {
11
+ /** Checks if the value is a valid double. */
12
+ check(value, errorHook, path = []) {
13
+ const isValid = typeof value === "number";
14
+ if (!isValid && errorHook) {
15
+ errorHook(path, value, this);
16
+ }
17
+ return isValid;
18
+ }
19
+ /** Reads a double value from the tap. */
20
+ async read(tap) {
21
+ return await tap.readDouble();
22
+ }
23
+ /** Writes a double value to the tap. */
24
+ async write(tap, value) {
25
+ if (!this.check(value)) {
26
+ throwInvalidError([], value, this);
27
+ }
28
+ await tap.writeDouble(value);
29
+ }
30
+ /** Skips a double value in the tap. */
31
+ async skip(tap) {
32
+ await tap.skipDouble();
33
+ }
34
+ /**
35
+ * Gets the size in bytes.
36
+ */
37
+ sizeBytes() {
38
+ return 8; // 8 bytes
39
+ }
40
+ /** Clones the value to a number. */
41
+ cloneFromValue(value) {
42
+ this.check(value, throwInvalidError, []);
43
+ return value;
44
+ }
45
+ /**
46
+ * Compares two double values.
47
+ */
48
+ compare(val1, val2) {
49
+ return val1 < val2 ? -1 : val1 > val2 ? 1 : 0;
50
+ }
51
+ /**
52
+ * Generates a random double value.
53
+ */
54
+ random() {
55
+ return Math.random();
56
+ }
57
+ /** Creates a resolver for the given writer type. */
58
+ createResolver(writerType) {
59
+ if (writerType instanceof IntType) {
60
+ // Double can promote from int (32-bit to 64-bit double)
61
+ return new class extends Resolver {
62
+ async read(tap) {
63
+ const intValue = await tap.readInt();
64
+ return intValue;
65
+ }
66
+ }(this);
67
+ }
68
+ else if (writerType instanceof LongType) {
69
+ // Double can promote from long (64-bit to 64-bit double, lossy for large values)
70
+ return new class extends Resolver {
71
+ async read(tap) {
72
+ const longValue = await tap.readLong();
73
+ return Number(longValue);
74
+ }
75
+ }(this);
76
+ }
77
+ else if (writerType instanceof FloatType) {
78
+ // Double can promote from float (32-bit to 64-bit double)
79
+ return new class extends Resolver {
80
+ async read(tap) {
81
+ const floatValue = await tap.readFloat();
82
+ return floatValue;
83
+ }
84
+ }(this);
85
+ }
86
+ else {
87
+ return super.createResolver(writerType);
88
+ }
89
+ }
90
+ /** Returns the JSON representation of the type. */
91
+ toJSON() {
92
+ return "double";
93
+ }
94
+ /** Matches two taps for equality. */
95
+ async match(tap1, tap2) {
96
+ return await tap1.matchDouble(tap2);
97
+ }
98
+ }
@@ -0,0 +1,28 @@
1
+ import { type ReadableTapLike } from "../../serialization/tap.js";
2
+ import { BaseType } from "../base_type.js";
3
+ /**
4
+ * Base class for fixed-size Avro types.
5
+ * Provides optimized serialization for types with known fixed byte sizes.
6
+ */
7
+ /**
8
+ * Base class for fixed-size Avro types.
9
+ * Provides optimized serialization for types with known fixed byte sizes.
10
+ */
11
+ export declare abstract class FixedSizeBaseType<T = unknown> extends BaseType<T> {
12
+ /**
13
+ * Returns the fixed size in bytes for this type.
14
+ * @returns The exact size in bytes.
15
+ */
16
+ abstract sizeBytes(): number;
17
+ /**
18
+ * Serializes a value into an ArrayBuffer using the exact fixed size.
19
+ * @param value The value to serialize.
20
+ * @returns The serialized ArrayBuffer.
21
+ */
22
+ toBuffer(value: T): Promise<ArrayBuffer>;
23
+ /**
24
+ * Skips a fixed-size value by advancing the tap by the fixed size.
25
+ * @param tap The tap to skip from.
26
+ */
27
+ skip(tap: ReadableTapLike): Promise<void>;
28
+ }
@@ -0,0 +1,33 @@
1
+ import { WritableTap } from "../../serialization/tap.js";
2
+ import { BaseType } from "../base_type.js";
3
+ import { throwInvalidError } from "../error.js";
4
+ /**
5
+ * Base class for fixed-size Avro types.
6
+ * Provides optimized serialization for types with known fixed byte sizes.
7
+ */
8
+ /**
9
+ * Base class for fixed-size Avro types.
10
+ * Provides optimized serialization for types with known fixed byte sizes.
11
+ */
12
+ export class FixedSizeBaseType extends BaseType {
13
+ /**
14
+ * Serializes a value into an ArrayBuffer using the exact fixed size.
15
+ * @param value The value to serialize.
16
+ * @returns The serialized ArrayBuffer.
17
+ */
18
+ async toBuffer(value) {
19
+ this.check(value, throwInvalidError, []);
20
+ const size = this.sizeBytes();
21
+ const buf = new ArrayBuffer(size);
22
+ const tap = new WritableTap(buf);
23
+ await this.write(tap, value);
24
+ return buf;
25
+ }
26
+ /**
27
+ * Skips a fixed-size value by advancing the tap by the fixed size.
28
+ * @param tap The tap to skip from.
29
+ */
30
+ async skip(tap) {
31
+ await tap.skipFixed(this.sizeBytes());
32
+ }
33
+ }
@@ -0,0 +1,38 @@
1
+ import type { ReadableTapLike, WritableTapLike } from "../../serialization/tap.js";
2
+ import { FixedSizeBaseType } from "./fixed_size_base_type.js";
3
+ import type { JSONType, Type } from "../type.js";
4
+ import { Resolver } from "../resolver.js";
5
+ import { type ErrorHook } from "../error.js";
6
+ /**
7
+ * Float type (32-bit).
8
+ */
9
+ export declare class FloatType extends FixedSizeBaseType<number> {
10
+ /** Checks if the value is a valid float. */
11
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
12
+ /** Reads a float value from the tap. */
13
+ read(tap: ReadableTapLike): Promise<number>;
14
+ /** Writes a float value to the tap. */
15
+ write(tap: WritableTapLike, value: number): Promise<void>;
16
+ /** Skips a float value in the tap. */
17
+ skip(tap: ReadableTapLike): Promise<void>;
18
+ /**
19
+ * Gets the size in bytes.
20
+ */
21
+ sizeBytes(): number;
22
+ /** Clones a value to a float. */
23
+ cloneFromValue(value: unknown): number;
24
+ /**
25
+ * Compares two float values.
26
+ */
27
+ compare(val1: number, val2: number): number;
28
+ /**
29
+ * Generates a random float value.
30
+ */
31
+ random(): number;
32
+ /** Creates a resolver for schema evolution. */
33
+ createResolver(writerType: Type): Resolver;
34
+ /** Returns the JSON representation. */
35
+ toJSON(): JSONType;
36
+ /** Matches float values between taps. */
37
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
38
+ }
@@ -0,0 +1,88 @@
1
+ import { FixedSizeBaseType } from "./fixed_size_base_type.js";
2
+ import { Resolver } from "../resolver.js";
3
+ import { IntType } from "./int_type.js";
4
+ import { LongType } from "./long_type.js";
5
+ import { throwInvalidError } from "../error.js";
6
+ /**
7
+ * Float type (32-bit).
8
+ */
9
+ export class FloatType extends FixedSizeBaseType {
10
+ /** Checks if the value is a valid float. */
11
+ check(value, errorHook, path = []) {
12
+ const isValid = typeof value === "number";
13
+ if (!isValid && errorHook) {
14
+ errorHook(path, value, this);
15
+ }
16
+ return isValid;
17
+ }
18
+ /** Reads a float value from the tap. */
19
+ async read(tap) {
20
+ return await tap.readFloat();
21
+ }
22
+ /** Writes a float value to the tap. */
23
+ async write(tap, value) {
24
+ if (!this.check(value)) {
25
+ throwInvalidError([], value, this);
26
+ }
27
+ await tap.writeFloat(value);
28
+ }
29
+ /** Skips a float value in the tap. */
30
+ async skip(tap) {
31
+ await tap.skipFloat();
32
+ }
33
+ /**
34
+ * Gets the size in bytes.
35
+ */
36
+ sizeBytes() {
37
+ return 4; // 4 bytes
38
+ }
39
+ /** Clones a value to a float. */
40
+ cloneFromValue(value) {
41
+ this.check(value, throwInvalidError, []);
42
+ return value;
43
+ }
44
+ /**
45
+ * Compares two float values.
46
+ */
47
+ compare(val1, val2) {
48
+ return val1 < val2 ? -1 : val1 > val2 ? 1 : 0;
49
+ }
50
+ /**
51
+ * Generates a random float value.
52
+ */
53
+ random() {
54
+ return Math.random() * 1000;
55
+ }
56
+ /** Creates a resolver for schema evolution. */
57
+ createResolver(writerType) {
58
+ if (writerType instanceof IntType) {
59
+ // Float can promote from int (32-bit to 32-bit float)
60
+ return new class extends Resolver {
61
+ async read(tap) {
62
+ const intValue = await tap.readInt();
63
+ return intValue;
64
+ }
65
+ }(this);
66
+ }
67
+ else if (writerType instanceof LongType) {
68
+ // Float can promote from long (64-bit to 32-bit float, lossy)
69
+ return new class extends Resolver {
70
+ async read(tap) {
71
+ const longValue = await tap.readLong();
72
+ return Number(longValue);
73
+ }
74
+ }(this);
75
+ }
76
+ else {
77
+ return super.createResolver(writerType);
78
+ }
79
+ }
80
+ /** Returns the JSON representation. */
81
+ toJSON() {
82
+ return "float";
83
+ }
84
+ /** Matches float values between taps. */
85
+ async match(tap1, tap2) {
86
+ return await tap1.matchFloat(tap2);
87
+ }
88
+ }
@@ -0,0 +1,31 @@
1
+ import { type ReadableTapLike, type WritableTapLike } from "../../serialization/tap.js";
2
+ import { PrimitiveType } from "./primitive_type.js";
3
+ import type { JSONType } from "../type.js";
4
+ import { type ErrorHook } from "../error.js";
5
+ /**
6
+ * Int type (32-bit).
7
+ */
8
+ export declare class IntType extends PrimitiveType<number> {
9
+ /** Checks if the value is a valid 32-bit integer. */
10
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
11
+ /** Reads a 32-bit integer from the tap. */
12
+ read(tap: ReadableTapLike): Promise<number>;
13
+ /** Writes a 32-bit integer to the tap. */
14
+ write(tap: WritableTapLike, value: number): Promise<void>;
15
+ /** Skips a 32-bit integer in the tap. */
16
+ skip(tap: ReadableTapLike): Promise<void>;
17
+ /** Converts a 32-bit integer to its buffer representation. */
18
+ toBuffer(value: number): Promise<ArrayBuffer>;
19
+ /**
20
+ * Compares two int values.
21
+ */
22
+ compare(val1: number, val2: number): number;
23
+ /**
24
+ * Generates a random int value.
25
+ */
26
+ random(): number;
27
+ /** Returns the JSON schema representation for int type. */
28
+ toJSON(): JSONType;
29
+ /** Matches two 32-bit integers in the taps. */
30
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
31
+ }
@@ -0,0 +1,63 @@
1
+ import { WritableTap, } from "../../serialization/tap.js";
2
+ import { PrimitiveType } from "./primitive_type.js";
3
+ import { calculateVarintSize } from "../../internal/varint.js";
4
+ import { throwInvalidError } from "../error.js";
5
+ /**
6
+ * Int type (32-bit).
7
+ */
8
+ export class IntType extends PrimitiveType {
9
+ /** Checks if the value is a valid 32-bit integer. */
10
+ check(value, errorHook, path = []) {
11
+ const isValid = typeof value === "number" && Number.isInteger(value) &&
12
+ value >= -2147483648 && value <= 2147483647;
13
+ if (!isValid && errorHook) {
14
+ errorHook(path, value, this);
15
+ }
16
+ return isValid;
17
+ }
18
+ /** Reads a 32-bit integer from the tap. */
19
+ async read(tap) {
20
+ return await tap.readInt();
21
+ }
22
+ /** Writes a 32-bit integer to the tap. */
23
+ async write(tap, value) {
24
+ if (!this.check(value)) {
25
+ throwInvalidError([], value, this);
26
+ }
27
+ await tap.writeInt(value);
28
+ }
29
+ /** Skips a 32-bit integer in the tap. */
30
+ async skip(tap) {
31
+ await tap.skipInt();
32
+ }
33
+ /** Converts a 32-bit integer to its buffer representation. */
34
+ async toBuffer(value) {
35
+ this.check(value, throwInvalidError, []);
36
+ // For int, allocate exact size based on value
37
+ const size = calculateVarintSize(value);
38
+ const buf = new ArrayBuffer(size);
39
+ const tap = new WritableTap(buf);
40
+ await this.write(tap, value);
41
+ return buf;
42
+ }
43
+ /**
44
+ * Compares two int values.
45
+ */
46
+ compare(val1, val2) {
47
+ return val1 < val2 ? -1 : val1 > val2 ? 1 : 0;
48
+ }
49
+ /**
50
+ * Generates a random int value.
51
+ */
52
+ random() {
53
+ return Math.floor(Math.random() * 1000);
54
+ }
55
+ /** Returns the JSON schema representation for int type. */
56
+ toJSON() {
57
+ return "int";
58
+ }
59
+ /** Matches two 32-bit integers in the taps. */
60
+ async match(tap1, tap2) {
61
+ return await tap1.matchInt(tap2);
62
+ }
63
+ }
@@ -0,0 +1,36 @@
1
+ import { type ReadableTapLike, type WritableTapLike } from "../../serialization/tap.js";
2
+ import { PrimitiveType } from "./primitive_type.js";
3
+ import type { JSONType, Type } from "../type.js";
4
+ import { Resolver } from "../resolver.js";
5
+ import { type ErrorHook } from "../error.js";
6
+ /**
7
+ * Long type (64-bit).
8
+ */
9
+ export declare class LongType extends PrimitiveType<bigint> {
10
+ /** Checks if the value is a valid long. */
11
+ check(value: unknown, errorHook?: ErrorHook, path?: string[]): boolean;
12
+ /** Reads a long value from the tap. */
13
+ read(tap: ReadableTapLike): Promise<bigint>;
14
+ /** Writes a long value to the tap. */
15
+ write(tap: WritableTapLike, value: bigint): Promise<void>;
16
+ /** Skips a long value in the tap. */
17
+ skip(tap: ReadableTapLike): Promise<void>;
18
+ /** Converts a bigint value to its buffer representation. */
19
+ toBuffer(value: bigint): Promise<ArrayBuffer>;
20
+ /**
21
+ * Compares two long values.
22
+ */
23
+ compare(val1: bigint, val2: bigint): number;
24
+ /**
25
+ * Generates a random long value.
26
+ */
27
+ random(): bigint;
28
+ /** Clones and validates a value as a bigint. */
29
+ cloneFromValue(value: unknown): bigint;
30
+ /** Creates a resolver for reading from the writer type. */
31
+ createResolver(writerType: Type): Resolver;
32
+ /** Returns the JSON representation of the type. */
33
+ toJSON(): JSONType;
34
+ /** Compares two taps for long equality. */
35
+ match(tap1: ReadableTapLike, tap2: ReadableTapLike): Promise<number>;
36
+ }