@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.
- package/esm/_dnt.polyfills.d.ts +11 -0
- package/esm/_dnt.polyfills.js +15 -0
- package/esm/avro_reader.d.ts +156 -0
- package/esm/avro_reader.js +201 -0
- package/esm/avro_writer.d.ts +75 -0
- package/esm/avro_writer.js +105 -0
- package/esm/internal/collections/array_utils.d.ts +4 -0
- package/esm/internal/collections/array_utils.js +18 -0
- package/esm/internal/collections/circular_buffer.d.ts +48 -0
- package/esm/internal/collections/circular_buffer.js +129 -0
- package/esm/internal/crypto/md5.d.ts +12 -0
- package/esm/internal/crypto/md5.js +158 -0
- package/esm/internal/varint.d.ts +6 -0
- package/esm/internal/varint.js +30 -0
- package/esm/mod.d.ts +77 -0
- package/esm/mod.js +73 -0
- package/esm/package.json +3 -0
- package/esm/rpc/definitions/message_definition.d.ts +34 -0
- package/esm/rpc/definitions/message_definition.js +105 -0
- package/esm/rpc/definitions/protocol_definitions.d.ts +197 -0
- package/esm/rpc/definitions/protocol_definitions.js +1 -0
- package/esm/rpc/message_endpoint/base.d.ts +43 -0
- package/esm/rpc/message_endpoint/base.js +81 -0
- package/esm/rpc/message_endpoint/emitter.d.ts +96 -0
- package/esm/rpc/message_endpoint/emitter.js +245 -0
- package/esm/rpc/message_endpoint/helpers.d.ts +37 -0
- package/esm/rpc/message_endpoint/helpers.js +68 -0
- package/esm/rpc/message_endpoint/listener.d.ts +37 -0
- package/esm/rpc/message_endpoint/listener.js +212 -0
- package/esm/rpc/protocol/frame_assembler.d.ts +11 -0
- package/esm/rpc/protocol/frame_assembler.js +77 -0
- package/esm/rpc/protocol/protocol_helpers.d.ts +59 -0
- package/esm/rpc/protocol/protocol_helpers.js +123 -0
- package/esm/rpc/protocol/transports/fetch.d.ts +9 -0
- package/esm/rpc/protocol/transports/fetch.js +30 -0
- package/esm/rpc/protocol/transports/in_memory.d.ts +27 -0
- package/esm/rpc/protocol/transports/in_memory.js +125 -0
- package/esm/rpc/protocol/transports/transport_helpers.d.ts +118 -0
- package/esm/rpc/protocol/transports/transport_helpers.js +112 -0
- package/esm/rpc/protocol/transports/websocket.d.ts +9 -0
- package/esm/rpc/protocol/transports/websocket.js +102 -0
- package/esm/rpc/protocol/wire_format/framing.d.ts +44 -0
- package/esm/rpc/protocol/wire_format/framing.js +74 -0
- package/esm/rpc/protocol/wire_format/handshake.d.ts +110 -0
- package/esm/rpc/protocol/wire_format/handshake.js +239 -0
- package/esm/rpc/protocol/wire_format/messages.d.ts +192 -0
- package/esm/rpc/protocol/wire_format/messages.js +175 -0
- package/esm/rpc/protocol/wire_format/metadata.d.ts +44 -0
- package/esm/rpc/protocol/wire_format/metadata.js +74 -0
- package/esm/rpc/protocol_core.d.ts +121 -0
- package/esm/rpc/protocol_core.js +285 -0
- package/esm/schemas/base_type.d.ts +41 -0
- package/esm/schemas/base_type.js +49 -0
- package/esm/schemas/complex/array_type.d.ts +100 -0
- package/esm/schemas/complex/array_type.js +299 -0
- package/esm/schemas/complex/enum_type.d.ts +81 -0
- package/esm/schemas/complex/enum_type.js +217 -0
- package/esm/schemas/complex/fixed_type.d.ts +99 -0
- package/esm/schemas/complex/fixed_type.js +208 -0
- package/esm/schemas/complex/map_type.d.ts +97 -0
- package/esm/schemas/complex/map_type.js +290 -0
- package/esm/schemas/complex/named_type.d.ts +30 -0
- package/esm/schemas/complex/named_type.js +57 -0
- package/esm/schemas/complex/record_type.d.ts +165 -0
- package/esm/schemas/complex/record_type.js +547 -0
- package/esm/schemas/complex/resolve_names.d.ts +32 -0
- package/esm/schemas/complex/resolve_names.js +85 -0
- package/esm/schemas/complex/union_type.d.ts +116 -0
- package/esm/schemas/complex/union_type.js +392 -0
- package/esm/schemas/error.d.ts +31 -0
- package/esm/schemas/error.js +67 -0
- package/esm/schemas/json.d.ts +6 -0
- package/esm/schemas/json.js +35 -0
- package/esm/schemas/logical/decimal_logical_type.d.ts +49 -0
- package/esm/schemas/logical/decimal_logical_type.js +145 -0
- package/esm/schemas/logical/duration_logical_type.d.ts +66 -0
- package/esm/schemas/logical/duration_logical_type.js +112 -0
- package/esm/schemas/logical/logical_type.d.ts +138 -0
- package/esm/schemas/logical/logical_type.js +240 -0
- package/esm/schemas/logical/temporal_logical_types.d.ts +215 -0
- package/esm/schemas/logical/temporal_logical_types.js +364 -0
- package/esm/schemas/logical/uuid_logical_type.d.ts +63 -0
- package/esm/schemas/logical/uuid_logical_type.js +146 -0
- package/esm/schemas/primitive/boolean_type.d.ts +49 -0
- package/esm/schemas/primitive/boolean_type.js +75 -0
- package/esm/schemas/primitive/bytes_type.d.ts +49 -0
- package/esm/schemas/primitive/bytes_type.js +136 -0
- package/esm/schemas/primitive/double_type.d.ts +38 -0
- package/esm/schemas/primitive/double_type.js +98 -0
- package/esm/schemas/primitive/fixed_size_base_type.d.ts +28 -0
- package/esm/schemas/primitive/fixed_size_base_type.js +33 -0
- package/esm/schemas/primitive/float_type.d.ts +38 -0
- package/esm/schemas/primitive/float_type.js +88 -0
- package/esm/schemas/primitive/int_type.d.ts +31 -0
- package/esm/schemas/primitive/int_type.js +63 -0
- package/esm/schemas/primitive/long_type.d.ts +36 -0
- package/esm/schemas/primitive/long_type.js +95 -0
- package/esm/schemas/primitive/null_type.d.ts +45 -0
- package/esm/schemas/primitive/null_type.js +71 -0
- package/esm/schemas/primitive/primitive_type.d.ts +17 -0
- package/esm/schemas/primitive/primitive_type.js +27 -0
- package/esm/schemas/primitive/string_type.d.ts +34 -0
- package/esm/schemas/primitive/string_type.js +81 -0
- package/esm/schemas/resolver.d.ts +25 -0
- package/esm/schemas/resolver.js +25 -0
- package/esm/schemas/type.d.ts +101 -0
- package/esm/schemas/type.js +6 -0
- package/esm/serialization/avro_constants.d.ts +13 -0
- package/esm/serialization/avro_constants.js +29 -0
- package/esm/serialization/avro_file_parser.d.ts +68 -0
- package/esm/serialization/avro_file_parser.js +191 -0
- package/esm/serialization/avro_file_writer.d.ts +63 -0
- package/esm/serialization/avro_file_writer.js +235 -0
- package/esm/serialization/buffers/blob_readable_buffer.d.ts +53 -0
- package/esm/serialization/buffers/blob_readable_buffer.js +80 -0
- package/esm/serialization/buffers/buffer.d.ts +37 -0
- package/esm/serialization/buffers/buffer.js +1 -0
- package/esm/serialization/buffers/in_memory_buffer.d.ts +121 -0
- package/esm/serialization/buffers/in_memory_buffer.js +206 -0
- package/esm/serialization/clamp.d.ts +10 -0
- package/esm/serialization/clamp.js +19 -0
- package/esm/serialization/compare_bytes.d.ts +9 -0
- package/esm/serialization/compare_bytes.js +45 -0
- package/esm/serialization/conversion.d.ts +8 -0
- package/esm/serialization/conversion.js +15 -0
- package/esm/serialization/decoders/decoder.d.ts +16 -0
- package/esm/serialization/decoders/decoder.js +1 -0
- package/esm/serialization/decoders/deflate_decoder.d.ts +15 -0
- package/esm/serialization/decoders/deflate_decoder.js +26 -0
- package/esm/serialization/decoders/null_decoder.d.ts +12 -0
- package/esm/serialization/decoders/null_decoder.js +13 -0
- package/esm/serialization/encoders/deflate_encoder.d.ts +15 -0
- package/esm/serialization/encoders/deflate_encoder.js +26 -0
- package/esm/serialization/encoders/encoder.d.ts +16 -0
- package/esm/serialization/encoders/encoder.js +1 -0
- package/esm/serialization/encoders/null_encoder.d.ts +12 -0
- package/esm/serialization/encoders/null_encoder.js +13 -0
- package/esm/serialization/manipulate_bytes.d.ts +6 -0
- package/esm/serialization/manipulate_bytes.js +13 -0
- package/esm/serialization/read_uint_le.d.ts +4 -0
- package/esm/serialization/read_uint_le.js +14 -0
- package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.d.ts +52 -0
- package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.js +129 -0
- package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.d.ts +36 -0
- package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.js +114 -0
- package/esm/serialization/streams/stream_readable_buffer.d.ts +41 -0
- package/esm/serialization/streams/stream_readable_buffer.js +64 -0
- package/esm/serialization/streams/stream_readable_buffer_adapter.d.ts +38 -0
- package/esm/serialization/streams/stream_readable_buffer_adapter.js +105 -0
- package/esm/serialization/streams/stream_writable_buffer.d.ts +42 -0
- package/esm/serialization/streams/stream_writable_buffer.js +64 -0
- package/esm/serialization/streams/stream_writable_buffer_adapter.d.ts +36 -0
- package/esm/serialization/streams/stream_writable_buffer_adapter.js +65 -0
- package/esm/serialization/streams/streams.d.ts +29 -0
- package/esm/serialization/streams/streams.js +1 -0
- package/esm/serialization/tap.d.ts +427 -0
- package/esm/serialization/tap.js +605 -0
- package/esm/serialization/text_encoding.d.ts +12 -0
- package/esm/serialization/text_encoding.js +14 -0
- package/esm/type/create_type.d.ts +54 -0
- package/esm/type/create_type.js +401 -0
- package/package.json +33 -0
- package/sachitv-avro-typescript-0.4.0.tgz +0 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { ArrayType } from "../schemas/complex/array_type.js";
|
|
2
|
+
import { BooleanType } from "../schemas/primitive/boolean_type.js";
|
|
3
|
+
import { BytesType } from "../schemas/primitive/bytes_type.js";
|
|
4
|
+
import { DoubleType } from "../schemas/primitive/double_type.js";
|
|
5
|
+
import { EnumType } from "../schemas/complex/enum_type.js";
|
|
6
|
+
import { FixedType, } from "../schemas/complex/fixed_type.js";
|
|
7
|
+
import { FloatType } from "../schemas/primitive/float_type.js";
|
|
8
|
+
import { IntType } from "../schemas/primitive/int_type.js";
|
|
9
|
+
import { LongType } from "../schemas/primitive/long_type.js";
|
|
10
|
+
import { MapType } from "../schemas/complex/map_type.js";
|
|
11
|
+
import { RecordType, } from "../schemas/complex/record_type.js";
|
|
12
|
+
import { resolveNames } from "../schemas/complex/resolve_names.js";
|
|
13
|
+
import { StringType } from "../schemas/primitive/string_type.js";
|
|
14
|
+
import { UnionType } from "../schemas/complex/union_type.js";
|
|
15
|
+
import { NullType } from "../schemas/primitive/null_type.js";
|
|
16
|
+
import { Type } from "../schemas/type.js";
|
|
17
|
+
import { safeStringify } from "../schemas/json.js";
|
|
18
|
+
import { DecimalLogicalType } from "../schemas/logical/decimal_logical_type.js";
|
|
19
|
+
import { UuidLogicalType } from "../schemas/logical/uuid_logical_type.js";
|
|
20
|
+
import { DateLogicalType, LocalTimestampMicrosLogicalType, LocalTimestampMillisLogicalType, LocalTimestampNanosLogicalType, TimeMicrosLogicalType, TimeMillisLogicalType, TimestampMicrosLogicalType, TimestampMillisLogicalType, TimestampNanosLogicalType, } from "../schemas/logical/temporal_logical_types.js";
|
|
21
|
+
import { DurationLogicalType } from "../schemas/logical/duration_logical_type.js";
|
|
22
|
+
import { NamedType } from "../schemas/complex/named_type.js";
|
|
23
|
+
const PRIMITIVE_FACTORIES = {
|
|
24
|
+
"null": () => new NullType(),
|
|
25
|
+
"boolean": () => new BooleanType(),
|
|
26
|
+
"int": () => new IntType(),
|
|
27
|
+
"long": () => new LongType(),
|
|
28
|
+
"float": () => new FloatType(),
|
|
29
|
+
"double": () => new DoubleType(),
|
|
30
|
+
"bytes": () => new BytesType(),
|
|
31
|
+
"string": () => new StringType(),
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Constructs an Avro {@link Type} from a schema definition.
|
|
35
|
+
*/
|
|
36
|
+
export function createType(schema, options = {}) {
|
|
37
|
+
const registry = options.registry ?? new Map();
|
|
38
|
+
const context = {
|
|
39
|
+
namespace: options.namespace,
|
|
40
|
+
registry,
|
|
41
|
+
};
|
|
42
|
+
return constructType(schema, context);
|
|
43
|
+
}
|
|
44
|
+
function constructType(schema, context) {
|
|
45
|
+
if (schema instanceof Type) {
|
|
46
|
+
return schema;
|
|
47
|
+
}
|
|
48
|
+
if (typeof schema === "string") {
|
|
49
|
+
return createFromTypeName(schema, context);
|
|
50
|
+
}
|
|
51
|
+
if (Array.isArray(schema)) {
|
|
52
|
+
return createUnionType(schema, context);
|
|
53
|
+
}
|
|
54
|
+
if (schema === null || typeof schema !== "object") {
|
|
55
|
+
throw new Error(`Unsupported Avro schema: ${safeStringify(schema)}`);
|
|
56
|
+
}
|
|
57
|
+
const logicalType = schema.logicalType;
|
|
58
|
+
if (logicalType !== undefined) {
|
|
59
|
+
return createLogicalType(schema, logicalType, context);
|
|
60
|
+
}
|
|
61
|
+
const { type } = schema;
|
|
62
|
+
if (typeof type === "string") {
|
|
63
|
+
if (isRecordTypeName(type)) {
|
|
64
|
+
return createRecordType(schema, type, context);
|
|
65
|
+
}
|
|
66
|
+
if (type === "enum") {
|
|
67
|
+
return createEnumType(schema, context);
|
|
68
|
+
}
|
|
69
|
+
if (type === "fixed") {
|
|
70
|
+
return createFixedType(schema, context);
|
|
71
|
+
}
|
|
72
|
+
if (type === "array") {
|
|
73
|
+
return createArrayType(schema, context);
|
|
74
|
+
}
|
|
75
|
+
if (type === "map") {
|
|
76
|
+
return createMapType(schema, context);
|
|
77
|
+
}
|
|
78
|
+
if (isPrimitiveTypeName(type)) {
|
|
79
|
+
return PRIMITIVE_FACTORIES[type]();
|
|
80
|
+
}
|
|
81
|
+
return createFromTypeName(type, {
|
|
82
|
+
namespace: extractNamespace(schema, context.namespace),
|
|
83
|
+
registry: context.registry,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (Array.isArray(type)) {
|
|
87
|
+
// When we're dealing with an array as a schema, it is a Union.
|
|
88
|
+
// > https://avro.apache.org/docs/1.12.0/specification/#unions
|
|
89
|
+
// > `Unions, as mentioned above, are represented using JSON arrays`.
|
|
90
|
+
return createUnionType(type, context);
|
|
91
|
+
}
|
|
92
|
+
if (type && typeof type === "object") {
|
|
93
|
+
return constructType(type, context);
|
|
94
|
+
}
|
|
95
|
+
throw new Error(`Schema is missing a valid "type" property: ${safeStringify(schema)}`);
|
|
96
|
+
}
|
|
97
|
+
function createLogicalType(schema, logicalType, context) {
|
|
98
|
+
const buildUnderlying = () => {
|
|
99
|
+
const underlyingSchema = { ...schema };
|
|
100
|
+
delete underlyingSchema.logicalType;
|
|
101
|
+
return constructType(underlyingSchema, context);
|
|
102
|
+
};
|
|
103
|
+
if (typeof logicalType !== "string") {
|
|
104
|
+
return buildUnderlying();
|
|
105
|
+
}
|
|
106
|
+
const underlying = buildUnderlying();
|
|
107
|
+
const replaceIfNamed = (logical) => {
|
|
108
|
+
if (underlying instanceof NamedType) {
|
|
109
|
+
context.registry.set(underlying.getFullName(), logical);
|
|
110
|
+
}
|
|
111
|
+
return logical;
|
|
112
|
+
};
|
|
113
|
+
try {
|
|
114
|
+
switch (logicalType) {
|
|
115
|
+
case "decimal": {
|
|
116
|
+
if (underlying instanceof BytesType || underlying instanceof FixedType) {
|
|
117
|
+
const precision = schema.precision;
|
|
118
|
+
const scaleValue = schema.scale;
|
|
119
|
+
if (typeof precision !== "number") {
|
|
120
|
+
return underlying;
|
|
121
|
+
}
|
|
122
|
+
if (scaleValue !== undefined && typeof scaleValue !== "number") {
|
|
123
|
+
return underlying;
|
|
124
|
+
}
|
|
125
|
+
const logical = new DecimalLogicalType(underlying, {
|
|
126
|
+
precision,
|
|
127
|
+
scale: scaleValue,
|
|
128
|
+
});
|
|
129
|
+
return replaceIfNamed(logical);
|
|
130
|
+
}
|
|
131
|
+
return underlying;
|
|
132
|
+
}
|
|
133
|
+
case "uuid": {
|
|
134
|
+
if (underlying instanceof StringType || underlying instanceof FixedType) {
|
|
135
|
+
const logical = new UuidLogicalType(underlying);
|
|
136
|
+
return replaceIfNamed(logical);
|
|
137
|
+
}
|
|
138
|
+
return underlying;
|
|
139
|
+
}
|
|
140
|
+
case "date": {
|
|
141
|
+
if (underlying instanceof IntType) {
|
|
142
|
+
return new DateLogicalType(underlying);
|
|
143
|
+
}
|
|
144
|
+
return underlying;
|
|
145
|
+
}
|
|
146
|
+
case "time-millis": {
|
|
147
|
+
if (underlying instanceof IntType) {
|
|
148
|
+
return new TimeMillisLogicalType(underlying);
|
|
149
|
+
}
|
|
150
|
+
return underlying;
|
|
151
|
+
}
|
|
152
|
+
case "time-micros": {
|
|
153
|
+
if (underlying instanceof LongType) {
|
|
154
|
+
return new TimeMicrosLogicalType(underlying);
|
|
155
|
+
}
|
|
156
|
+
return underlying;
|
|
157
|
+
}
|
|
158
|
+
case "timestamp-millis": {
|
|
159
|
+
if (underlying instanceof LongType) {
|
|
160
|
+
return new TimestampMillisLogicalType(underlying);
|
|
161
|
+
}
|
|
162
|
+
return underlying;
|
|
163
|
+
}
|
|
164
|
+
case "timestamp-micros": {
|
|
165
|
+
if (underlying instanceof LongType) {
|
|
166
|
+
return new TimestampMicrosLogicalType(underlying);
|
|
167
|
+
}
|
|
168
|
+
return underlying;
|
|
169
|
+
}
|
|
170
|
+
case "timestamp-nanos": {
|
|
171
|
+
if (underlying instanceof LongType) {
|
|
172
|
+
return new TimestampNanosLogicalType(underlying);
|
|
173
|
+
}
|
|
174
|
+
return underlying;
|
|
175
|
+
}
|
|
176
|
+
case "local-timestamp-millis": {
|
|
177
|
+
if (underlying instanceof LongType) {
|
|
178
|
+
return new LocalTimestampMillisLogicalType(underlying);
|
|
179
|
+
}
|
|
180
|
+
return underlying;
|
|
181
|
+
}
|
|
182
|
+
case "local-timestamp-micros": {
|
|
183
|
+
if (underlying instanceof LongType) {
|
|
184
|
+
return new LocalTimestampMicrosLogicalType(underlying);
|
|
185
|
+
}
|
|
186
|
+
return underlying;
|
|
187
|
+
}
|
|
188
|
+
case "local-timestamp-nanos": {
|
|
189
|
+
if (underlying instanceof LongType) {
|
|
190
|
+
return new LocalTimestampNanosLogicalType(underlying);
|
|
191
|
+
}
|
|
192
|
+
return underlying;
|
|
193
|
+
}
|
|
194
|
+
case "duration": {
|
|
195
|
+
if (underlying instanceof FixedType) {
|
|
196
|
+
const logical = new DurationLogicalType(underlying);
|
|
197
|
+
return replaceIfNamed(logical);
|
|
198
|
+
}
|
|
199
|
+
return underlying;
|
|
200
|
+
}
|
|
201
|
+
default:
|
|
202
|
+
return underlying;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
catch {
|
|
206
|
+
return underlying;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
function createFromTypeName(name, context) {
|
|
210
|
+
if (isPrimitiveTypeName(name)) {
|
|
211
|
+
return PRIMITIVE_FACTORIES[name]();
|
|
212
|
+
}
|
|
213
|
+
const fullName = qualifyReference(name, context.namespace);
|
|
214
|
+
const found = context.registry.get(fullName);
|
|
215
|
+
if (found) {
|
|
216
|
+
return found;
|
|
217
|
+
}
|
|
218
|
+
materializeLazyRecordFields(context.registry);
|
|
219
|
+
const materialized = context.registry.get(fullName);
|
|
220
|
+
if (materialized) {
|
|
221
|
+
return materialized;
|
|
222
|
+
}
|
|
223
|
+
throw new Error(`Undefined Avro type reference: ${name}`);
|
|
224
|
+
}
|
|
225
|
+
function createRecordType(schema, typeName, context) {
|
|
226
|
+
const name = schema.name;
|
|
227
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
228
|
+
throw new Error(`Record schema requires a non-empty name: ${safeStringify(schema)}`);
|
|
229
|
+
}
|
|
230
|
+
const aliases = toStringArray(schema.aliases);
|
|
231
|
+
const resolved = resolveNames({
|
|
232
|
+
name,
|
|
233
|
+
namespace: extractNamespace(schema, context.namespace),
|
|
234
|
+
aliases,
|
|
235
|
+
});
|
|
236
|
+
const childContext = {
|
|
237
|
+
namespace: resolved.namespace || undefined,
|
|
238
|
+
registry: context.registry,
|
|
239
|
+
};
|
|
240
|
+
const fieldsValue = schema.fields;
|
|
241
|
+
if (!Array.isArray(fieldsValue)) {
|
|
242
|
+
throw new Error(`Record schema requires a fields array: ${safeStringify(schema)}`);
|
|
243
|
+
}
|
|
244
|
+
const buildFields = () => {
|
|
245
|
+
return fieldsValue.map((field) => {
|
|
246
|
+
if (field === null || typeof field !== "object") {
|
|
247
|
+
throw new Error(`Invalid record field definition: ${safeStringify(field)}`);
|
|
248
|
+
}
|
|
249
|
+
const fieldName = field.name;
|
|
250
|
+
if (typeof fieldName !== "string" || fieldName.length === 0) {
|
|
251
|
+
throw new Error(`Record field requires a non-empty name: ${safeStringify(field)}`);
|
|
252
|
+
}
|
|
253
|
+
if (!("type" in field)) {
|
|
254
|
+
throw new Error(`Record field "${fieldName}" is missing a type definition.`);
|
|
255
|
+
}
|
|
256
|
+
const fieldType = constructType(field.type, childContext);
|
|
257
|
+
const fieldAliases = toStringArray(field.aliases);
|
|
258
|
+
const order = field.order;
|
|
259
|
+
const fieldParams = {
|
|
260
|
+
name: fieldName,
|
|
261
|
+
type: fieldType,
|
|
262
|
+
};
|
|
263
|
+
if (fieldAliases.length > 0) {
|
|
264
|
+
fieldParams.aliases = fieldAliases;
|
|
265
|
+
}
|
|
266
|
+
if (order === "ascending" || order === "descending" || order === "ignore") {
|
|
267
|
+
fieldParams.order = order;
|
|
268
|
+
}
|
|
269
|
+
if (field.default !== undefined) {
|
|
270
|
+
fieldParams.default = field.default;
|
|
271
|
+
}
|
|
272
|
+
return fieldParams;
|
|
273
|
+
});
|
|
274
|
+
};
|
|
275
|
+
/*
|
|
276
|
+
The Avro spec (section 2.8 "Protocols") treats the implicit "request" type
|
|
277
|
+
declared on each RPC message as a record-like structure that is not a
|
|
278
|
+
standalone named type; it exists solely within that message's scope. Record
|
|
279
|
+
and error types are bona fide named types that can be reused elsewhere, so
|
|
280
|
+
only those should be registered in the shared registry. Skipping "request"
|
|
281
|
+
avoids polluting the registry with per-message-only definitions.
|
|
282
|
+
*/
|
|
283
|
+
const shouldRegister = typeName !== "request";
|
|
284
|
+
if (shouldRegister && context.registry.has(resolved.fullName)) {
|
|
285
|
+
throw new Error(`Duplicate Avro type name: ${resolved.fullName}`);
|
|
286
|
+
}
|
|
287
|
+
const params = {
|
|
288
|
+
...resolved,
|
|
289
|
+
fields: buildFields,
|
|
290
|
+
};
|
|
291
|
+
const record = new RecordType(params);
|
|
292
|
+
if (shouldRegister) {
|
|
293
|
+
context.registry.set(resolved.fullName, record);
|
|
294
|
+
}
|
|
295
|
+
return record;
|
|
296
|
+
}
|
|
297
|
+
function createEnumType(schema, context) {
|
|
298
|
+
const name = schema.name;
|
|
299
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
300
|
+
throw new Error(`Enum schema requires a non-empty name: ${safeStringify(schema)}`);
|
|
301
|
+
}
|
|
302
|
+
const aliases = toStringArray(schema.aliases);
|
|
303
|
+
const resolved = resolveNames({
|
|
304
|
+
name,
|
|
305
|
+
namespace: extractNamespace(schema, context.namespace),
|
|
306
|
+
aliases,
|
|
307
|
+
});
|
|
308
|
+
const symbols = schema.symbols;
|
|
309
|
+
if (!Array.isArray(symbols) || symbols.some((s) => typeof s !== "string")) {
|
|
310
|
+
throw new Error(`Enum schema requires an array of string symbols: ${safeStringify(schema)}`);
|
|
311
|
+
}
|
|
312
|
+
if (context.registry.has(resolved.fullName)) {
|
|
313
|
+
throw new Error(`Duplicate Avro type name: ${resolved.fullName}`);
|
|
314
|
+
}
|
|
315
|
+
const params = {
|
|
316
|
+
...resolved,
|
|
317
|
+
symbols: symbols.slice(),
|
|
318
|
+
};
|
|
319
|
+
if (schema.default !== undefined) {
|
|
320
|
+
params.default = schema.default;
|
|
321
|
+
}
|
|
322
|
+
const enumType = new EnumType(params);
|
|
323
|
+
context.registry.set(resolved.fullName, enumType);
|
|
324
|
+
return enumType;
|
|
325
|
+
}
|
|
326
|
+
function createFixedType(schema, context) {
|
|
327
|
+
const name = schema.name;
|
|
328
|
+
if (typeof name !== "string" || name.length === 0) {
|
|
329
|
+
throw new Error(`Fixed schema requires a non-empty name: ${safeStringify(schema)}`);
|
|
330
|
+
}
|
|
331
|
+
const aliases = toStringArray(schema.aliases);
|
|
332
|
+
const resolved = resolveNames({
|
|
333
|
+
name,
|
|
334
|
+
namespace: extractNamespace(schema, context.namespace),
|
|
335
|
+
aliases,
|
|
336
|
+
});
|
|
337
|
+
if (context.registry.has(resolved.fullName)) {
|
|
338
|
+
throw new Error(`Duplicate Avro type name: ${resolved.fullName}`);
|
|
339
|
+
}
|
|
340
|
+
const params = {
|
|
341
|
+
...resolved,
|
|
342
|
+
size: schema.size,
|
|
343
|
+
};
|
|
344
|
+
const fixed = new FixedType(params);
|
|
345
|
+
context.registry.set(resolved.fullName, fixed);
|
|
346
|
+
return fixed;
|
|
347
|
+
}
|
|
348
|
+
function createArrayType(schema, context) {
|
|
349
|
+
if (!("items" in schema)) {
|
|
350
|
+
throw new Error(`Array schema requires an "items" definition: ${safeStringify(schema)}`);
|
|
351
|
+
}
|
|
352
|
+
const itemsType = constructType(schema.items, context);
|
|
353
|
+
return new ArrayType({ items: itemsType });
|
|
354
|
+
}
|
|
355
|
+
function createMapType(schema, context) {
|
|
356
|
+
if (!("values" in schema)) {
|
|
357
|
+
throw new Error(`Map schema requires a "values" definition: ${safeStringify(schema)}`);
|
|
358
|
+
}
|
|
359
|
+
const valuesType = constructType(schema.values, context);
|
|
360
|
+
return new MapType({ values: valuesType });
|
|
361
|
+
}
|
|
362
|
+
function createUnionType(schemas, context) {
|
|
363
|
+
if (schemas.length === 0) {
|
|
364
|
+
throw new Error("Union schema requires at least one branch type.");
|
|
365
|
+
}
|
|
366
|
+
const types = schemas.map((branch) => constructType(branch, context));
|
|
367
|
+
return new UnionType({ types });
|
|
368
|
+
}
|
|
369
|
+
function isPrimitiveTypeName(value) {
|
|
370
|
+
return typeof value === "string" && value in PRIMITIVE_FACTORIES;
|
|
371
|
+
}
|
|
372
|
+
function materializeLazyRecordFields(registry) {
|
|
373
|
+
for (const type of registry.values()) {
|
|
374
|
+
if (type instanceof RecordType) {
|
|
375
|
+
// Ensures nested named types defined inside record fields are registered.
|
|
376
|
+
type.getFields();
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
function isRecordTypeName(value) {
|
|
381
|
+
return value === "record" || value === "error" || value === "request";
|
|
382
|
+
}
|
|
383
|
+
function toStringArray(value) {
|
|
384
|
+
if (!Array.isArray(value)) {
|
|
385
|
+
return [];
|
|
386
|
+
}
|
|
387
|
+
return value.filter((entry) => typeof entry === "string");
|
|
388
|
+
}
|
|
389
|
+
function extractNamespace(schema, fallback) {
|
|
390
|
+
const namespace = schema.namespace;
|
|
391
|
+
if (typeof namespace === "string") {
|
|
392
|
+
return namespace.length > 0 ? namespace : undefined;
|
|
393
|
+
}
|
|
394
|
+
return fallback;
|
|
395
|
+
}
|
|
396
|
+
function qualifyReference(name, namespace) {
|
|
397
|
+
if (!namespace || name.includes(".")) {
|
|
398
|
+
return name;
|
|
399
|
+
}
|
|
400
|
+
return `${namespace}.${name}`;
|
|
401
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sachitv/avro-typescript",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"description": "Avro schema parsing, serialization, and RPC helpers for TypeScript runtimes.",
|
|
5
|
+
"homepage": "https://github.com/sachitv/avro-typescript",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/sachitv/avro-typescript"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/sachitv/avro-typescript/issues"
|
|
13
|
+
},
|
|
14
|
+
"module": "./esm/mod.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"import": "./esm/mod.mjs",
|
|
18
|
+
"types": "./esm/mod.d.ts"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22"
|
|
23
|
+
},
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"type": "module",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@types/node": "^20.9.0"
|
|
31
|
+
},
|
|
32
|
+
"_generatedBy": "dnt@0.40.0"
|
|
33
|
+
}
|
|
Binary file
|