@sachitv/avro-typescript 0.4.1 → 0.5.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/avro_reader_sync.d.ts +48 -0
- package/esm/avro_reader_sync.js +79 -0
- package/esm/avro_writer_sync.d.ts +28 -0
- package/esm/avro_writer_sync.js +63 -0
- package/esm/internal/collections/circular_buffer.d.ts +4 -3
- package/esm/internal/collections/circular_buffer.js +8 -6
- package/esm/mod.d.ts +17 -0
- package/esm/mod.js +11 -0
- package/esm/schemas/base_type.d.ts +8 -0
- package/esm/schemas/base_type.js +21 -0
- package/esm/schemas/complex/array_type.d.ts +31 -10
- package/esm/schemas/complex/array_type.js +129 -39
- package/esm/schemas/complex/enum_type.d.ts +24 -7
- package/esm/schemas/complex/enum_type.js +61 -15
- package/esm/schemas/complex/fixed_type.d.ts +27 -12
- package/esm/schemas/complex/fixed_type.js +40 -27
- package/esm/schemas/complex/map_type.d.ts +31 -10
- package/esm/schemas/complex/map_type.js +88 -47
- package/esm/schemas/complex/named_type.d.ts +1 -1
- package/esm/schemas/complex/named_type.js +2 -2
- package/esm/schemas/complex/record_field.d.ts +62 -0
- package/esm/schemas/complex/record_field.js +112 -0
- package/esm/schemas/complex/record_resolver.d.ts +45 -0
- package/esm/schemas/complex/record_resolver.js +92 -0
- package/esm/schemas/complex/record_type.d.ts +45 -70
- package/esm/schemas/complex/record_type.js +158 -213
- package/esm/schemas/complex/record_writer_cache.d.ts +44 -0
- package/esm/schemas/complex/record_writer_cache.js +141 -0
- package/esm/schemas/complex/record_writer_strategy.d.ts +123 -0
- package/esm/schemas/complex/record_writer_strategy.js +309 -0
- package/esm/schemas/complex/union_type.d.ts +23 -11
- package/esm/schemas/complex/union_type.js +77 -29
- package/esm/schemas/logical/decimal_logical_type.d.ts +1 -1
- package/esm/schemas/logical/decimal_logical_type.js +2 -2
- package/esm/schemas/logical/duration_logical_type.d.ts +1 -1
- package/esm/schemas/logical/duration_logical_type.js +2 -2
- package/esm/schemas/logical/logical_type.d.ts +27 -9
- package/esm/schemas/logical/logical_type.js +50 -17
- package/esm/schemas/logical/temporal_logical_types.d.ts +9 -9
- package/esm/schemas/logical/temporal_logical_types.js +18 -18
- package/esm/schemas/logical/uuid_logical_type.d.ts +1 -1
- package/esm/schemas/logical/uuid_logical_type.js +2 -2
- package/esm/schemas/primitive/boolean_type.d.ts +15 -4
- package/esm/schemas/primitive/boolean_type.js +22 -7
- package/esm/schemas/primitive/bytes_type.d.ts +16 -5
- package/esm/schemas/primitive/bytes_type.js +31 -16
- package/esm/schemas/primitive/double_type.d.ts +13 -2
- package/esm/schemas/primitive/double_type.js +34 -5
- package/esm/schemas/primitive/fixed_size_base_type.d.ts +11 -7
- package/esm/schemas/primitive/fixed_size_base_type.js +14 -14
- package/esm/schemas/primitive/float_type.d.ts +11 -2
- package/esm/schemas/primitive/float_type.js +26 -5
- package/esm/schemas/primitive/int_type.d.ts +17 -6
- package/esm/schemas/primitive/int_type.js +26 -17
- package/esm/schemas/primitive/long_type.d.ts +16 -5
- package/esm/schemas/primitive/long_type.js +30 -16
- package/esm/schemas/primitive/null_type.d.ts +15 -4
- package/esm/schemas/primitive/null_type.js +20 -7
- package/esm/schemas/primitive/primitive_type.d.ts +2 -0
- package/esm/schemas/primitive/primitive_type.js +4 -0
- package/esm/schemas/primitive/string_type.d.ts +17 -6
- package/esm/schemas/primitive/string_type.js +33 -18
- package/esm/schemas/resolver.d.ts +6 -0
- package/esm/schemas/type.d.ts +64 -4
- package/esm/schemas/type.js +97 -0
- package/esm/serialization/avro_file_parser_sync.d.ts +34 -0
- package/esm/serialization/avro_file_parser_sync.js +160 -0
- package/esm/serialization/avro_file_writer_sync.d.ts +47 -0
- package/esm/serialization/avro_file_writer_sync.js +211 -0
- package/esm/serialization/buffers/blob_readable_buffer.d.ts +4 -3
- package/esm/serialization/buffers/blob_readable_buffer.js +20 -6
- package/esm/serialization/buffers/buffer.d.ts +6 -1
- package/esm/serialization/buffers/buffer.js +3 -0
- package/esm/serialization/buffers/buffer_error.d.ts +28 -0
- package/esm/serialization/buffers/buffer_error.js +70 -0
- package/esm/serialization/buffers/buffer_sync.d.ts +51 -0
- package/esm/serialization/buffers/buffer_sync.js +4 -0
- package/esm/serialization/buffers/in_memory_buffer.d.ts +1 -0
- package/esm/serialization/buffers/in_memory_buffer.js +7 -5
- package/esm/serialization/buffers/in_memory_buffer_sync.d.ts +133 -0
- package/esm/serialization/buffers/in_memory_buffer_sync.js +259 -0
- package/esm/serialization/counting_writable_tap.d.ts +45 -0
- package/esm/serialization/counting_writable_tap.js +90 -0
- package/esm/serialization/counting_writable_tap_sync.d.ts +46 -0
- package/esm/serialization/counting_writable_tap_sync.js +87 -0
- package/esm/serialization/decoders/decoder_null_sync.d.ts +12 -0
- package/esm/serialization/decoders/decoder_null_sync.js +13 -0
- package/esm/serialization/decoders/decoder_sync.d.ts +15 -0
- package/esm/serialization/decoders/decoder_sync.js +1 -0
- package/esm/serialization/encoders/encoder_null_sync.d.ts +12 -0
- package/esm/serialization/encoders/encoder_null_sync.js +13 -0
- package/esm/serialization/encoders/encoder_sync.d.ts +15 -0
- package/esm/serialization/encoders/encoder_sync.js +1 -0
- package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.d.ts +3 -2
- package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.js +19 -9
- package/esm/serialization/streams/fixed_size_stream_reader_sync.d.ts +25 -0
- package/esm/serialization/streams/fixed_size_stream_reader_sync.js +63 -0
- package/esm/serialization/streams/fixed_size_stream_writer_sync.d.ts +45 -0
- package/esm/serialization/streams/fixed_size_stream_writer_sync.js +98 -0
- package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.d.ts +5 -3
- package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.js +18 -8
- package/esm/serialization/streams/stream_readable_buffer_adapter.d.ts +3 -2
- package/esm/serialization/streams/stream_readable_buffer_adapter.js +19 -6
- package/esm/serialization/streams/stream_readable_buffer_adapter_sync.d.ts +36 -0
- package/esm/serialization/streams/stream_readable_buffer_adapter_sync.js +93 -0
- package/esm/serialization/streams/stream_writable_buffer_adapter_sync.d.ts +45 -0
- package/esm/serialization/streams/stream_writable_buffer_adapter_sync.js +78 -0
- package/esm/serialization/streams/streams_sync.d.ts +36 -0
- package/esm/serialization/streams/streams_sync.js +4 -0
- package/esm/serialization/tap.d.ts +15 -32
- package/esm/serialization/tap.js +45 -134
- package/esm/serialization/tap_sync.d.ts +240 -0
- package/esm/serialization/tap_sync.js +545 -0
- package/esm/serialization/text_encoding.d.ts +16 -0
- package/esm/serialization/text_encoding.js +48 -1
- package/esm/type/create_type.d.ts +20 -0
- package/esm/type/create_type.js +49 -28
- package/package.json +2 -2
- package/esm/serialization/manipulate_bytes.d.ts +0 -6
- package/esm/serialization/manipulate_bytes.js +0 -13
- package/esm/serialization/read_uint_le.d.ts +0 -4
- package/esm/serialization/read_uint_le.js +0 -14
package/esm/type/create_type.js
CHANGED
|
@@ -20,16 +20,26 @@ import { UuidLogicalType } from "../schemas/logical/uuid_logical_type.js";
|
|
|
20
20
|
import { DateLogicalType, LocalTimestampMicrosLogicalType, LocalTimestampMillisLogicalType, LocalTimestampNanosLogicalType, TimeMicrosLogicalType, TimeMillisLogicalType, TimestampMicrosLogicalType, TimestampMillisLogicalType, TimestampNanosLogicalType, } from "../schemas/logical/temporal_logical_types.js";
|
|
21
21
|
import { DurationLogicalType } from "../schemas/logical/duration_logical_type.js";
|
|
22
22
|
import { NamedType } from "../schemas/complex/named_type.js";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
23
|
+
function createPrimitiveType(name, validate) {
|
|
24
|
+
switch (name) {
|
|
25
|
+
case "null":
|
|
26
|
+
return new NullType(validate);
|
|
27
|
+
case "boolean":
|
|
28
|
+
return new BooleanType(validate);
|
|
29
|
+
case "int":
|
|
30
|
+
return new IntType(validate);
|
|
31
|
+
case "long":
|
|
32
|
+
return new LongType(validate);
|
|
33
|
+
case "float":
|
|
34
|
+
return new FloatType(validate);
|
|
35
|
+
case "double":
|
|
36
|
+
return new DoubleType(validate);
|
|
37
|
+
case "bytes":
|
|
38
|
+
return new BytesType(validate);
|
|
39
|
+
case "string":
|
|
40
|
+
return new StringType(validate);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
33
43
|
/**
|
|
34
44
|
* Constructs an Avro {@link Type} from a schema definition.
|
|
35
45
|
*/
|
|
@@ -38,6 +48,8 @@ export function createType(schema, options = {}) {
|
|
|
38
48
|
const context = {
|
|
39
49
|
namespace: options.namespace,
|
|
40
50
|
registry,
|
|
51
|
+
validate: options.validate ?? true,
|
|
52
|
+
writerStrategy: options.writerStrategy,
|
|
41
53
|
};
|
|
42
54
|
return constructType(schema, context);
|
|
43
55
|
}
|
|
@@ -76,11 +88,12 @@ function constructType(schema, context) {
|
|
|
76
88
|
return createMapType(schema, context);
|
|
77
89
|
}
|
|
78
90
|
if (isPrimitiveTypeName(type)) {
|
|
79
|
-
return
|
|
91
|
+
return createPrimitiveType(type, context.validate);
|
|
80
92
|
}
|
|
81
93
|
return createFromTypeName(type, {
|
|
82
94
|
namespace: extractNamespace(schema, context.namespace),
|
|
83
95
|
registry: context.registry,
|
|
96
|
+
validate: context.validate,
|
|
84
97
|
});
|
|
85
98
|
}
|
|
86
99
|
if (Array.isArray(type)) {
|
|
@@ -125,75 +138,75 @@ function createLogicalType(schema, logicalType, context) {
|
|
|
125
138
|
const logical = new DecimalLogicalType(underlying, {
|
|
126
139
|
precision,
|
|
127
140
|
scale: scaleValue,
|
|
128
|
-
});
|
|
141
|
+
}, context.validate);
|
|
129
142
|
return replaceIfNamed(logical);
|
|
130
143
|
}
|
|
131
144
|
return underlying;
|
|
132
145
|
}
|
|
133
146
|
case "uuid": {
|
|
134
147
|
if (underlying instanceof StringType || underlying instanceof FixedType) {
|
|
135
|
-
const logical = new UuidLogicalType(underlying);
|
|
148
|
+
const logical = new UuidLogicalType(underlying, context.validate);
|
|
136
149
|
return replaceIfNamed(logical);
|
|
137
150
|
}
|
|
138
151
|
return underlying;
|
|
139
152
|
}
|
|
140
153
|
case "date": {
|
|
141
154
|
if (underlying instanceof IntType) {
|
|
142
|
-
return new DateLogicalType(underlying);
|
|
155
|
+
return new DateLogicalType(underlying, context.validate);
|
|
143
156
|
}
|
|
144
157
|
return underlying;
|
|
145
158
|
}
|
|
146
159
|
case "time-millis": {
|
|
147
160
|
if (underlying instanceof IntType) {
|
|
148
|
-
return new TimeMillisLogicalType(underlying);
|
|
161
|
+
return new TimeMillisLogicalType(underlying, context.validate);
|
|
149
162
|
}
|
|
150
163
|
return underlying;
|
|
151
164
|
}
|
|
152
165
|
case "time-micros": {
|
|
153
166
|
if (underlying instanceof LongType) {
|
|
154
|
-
return new TimeMicrosLogicalType(underlying);
|
|
167
|
+
return new TimeMicrosLogicalType(underlying, context.validate);
|
|
155
168
|
}
|
|
156
169
|
return underlying;
|
|
157
170
|
}
|
|
158
171
|
case "timestamp-millis": {
|
|
159
172
|
if (underlying instanceof LongType) {
|
|
160
|
-
return new TimestampMillisLogicalType(underlying);
|
|
173
|
+
return new TimestampMillisLogicalType(underlying, context.validate);
|
|
161
174
|
}
|
|
162
175
|
return underlying;
|
|
163
176
|
}
|
|
164
177
|
case "timestamp-micros": {
|
|
165
178
|
if (underlying instanceof LongType) {
|
|
166
|
-
return new TimestampMicrosLogicalType(underlying);
|
|
179
|
+
return new TimestampMicrosLogicalType(underlying, context.validate);
|
|
167
180
|
}
|
|
168
181
|
return underlying;
|
|
169
182
|
}
|
|
170
183
|
case "timestamp-nanos": {
|
|
171
184
|
if (underlying instanceof LongType) {
|
|
172
|
-
return new TimestampNanosLogicalType(underlying);
|
|
185
|
+
return new TimestampNanosLogicalType(underlying, context.validate);
|
|
173
186
|
}
|
|
174
187
|
return underlying;
|
|
175
188
|
}
|
|
176
189
|
case "local-timestamp-millis": {
|
|
177
190
|
if (underlying instanceof LongType) {
|
|
178
|
-
return new LocalTimestampMillisLogicalType(underlying);
|
|
191
|
+
return new LocalTimestampMillisLogicalType(underlying, context.validate);
|
|
179
192
|
}
|
|
180
193
|
return underlying;
|
|
181
194
|
}
|
|
182
195
|
case "local-timestamp-micros": {
|
|
183
196
|
if (underlying instanceof LongType) {
|
|
184
|
-
return new LocalTimestampMicrosLogicalType(underlying);
|
|
197
|
+
return new LocalTimestampMicrosLogicalType(underlying, context.validate);
|
|
185
198
|
}
|
|
186
199
|
return underlying;
|
|
187
200
|
}
|
|
188
201
|
case "local-timestamp-nanos": {
|
|
189
202
|
if (underlying instanceof LongType) {
|
|
190
|
-
return new LocalTimestampNanosLogicalType(underlying);
|
|
203
|
+
return new LocalTimestampNanosLogicalType(underlying, context.validate);
|
|
191
204
|
}
|
|
192
205
|
return underlying;
|
|
193
206
|
}
|
|
194
207
|
case "duration": {
|
|
195
208
|
if (underlying instanceof FixedType) {
|
|
196
|
-
const logical = new DurationLogicalType(underlying);
|
|
209
|
+
const logical = new DurationLogicalType(underlying, context.validate);
|
|
197
210
|
return replaceIfNamed(logical);
|
|
198
211
|
}
|
|
199
212
|
return underlying;
|
|
@@ -208,7 +221,7 @@ function createLogicalType(schema, logicalType, context) {
|
|
|
208
221
|
}
|
|
209
222
|
function createFromTypeName(name, context) {
|
|
210
223
|
if (isPrimitiveTypeName(name)) {
|
|
211
|
-
return
|
|
224
|
+
return createPrimitiveType(name, context.validate);
|
|
212
225
|
}
|
|
213
226
|
const fullName = qualifyReference(name, context.namespace);
|
|
214
227
|
const found = context.registry.get(fullName);
|
|
@@ -236,6 +249,8 @@ function createRecordType(schema, typeName, context) {
|
|
|
236
249
|
const childContext = {
|
|
237
250
|
namespace: resolved.namespace || undefined,
|
|
238
251
|
registry: context.registry,
|
|
252
|
+
validate: context.validate,
|
|
253
|
+
writerStrategy: context.writerStrategy,
|
|
239
254
|
};
|
|
240
255
|
const fieldsValue = schema.fields;
|
|
241
256
|
if (!Array.isArray(fieldsValue)) {
|
|
@@ -287,6 +302,8 @@ function createRecordType(schema, typeName, context) {
|
|
|
287
302
|
const params = {
|
|
288
303
|
...resolved,
|
|
289
304
|
fields: buildFields,
|
|
305
|
+
validate: context.validate,
|
|
306
|
+
writerStrategy: context.writerStrategy,
|
|
290
307
|
};
|
|
291
308
|
const record = new RecordType(params);
|
|
292
309
|
if (shouldRegister) {
|
|
@@ -315,6 +332,7 @@ function createEnumType(schema, context) {
|
|
|
315
332
|
const params = {
|
|
316
333
|
...resolved,
|
|
317
334
|
symbols: symbols.slice(),
|
|
335
|
+
validate: context.validate,
|
|
318
336
|
};
|
|
319
337
|
if (schema.default !== undefined) {
|
|
320
338
|
params.default = schema.default;
|
|
@@ -340,6 +358,7 @@ function createFixedType(schema, context) {
|
|
|
340
358
|
const params = {
|
|
341
359
|
...resolved,
|
|
342
360
|
size: schema.size,
|
|
361
|
+
validate: context.validate,
|
|
343
362
|
};
|
|
344
363
|
const fixed = new FixedType(params);
|
|
345
364
|
context.registry.set(resolved.fullName, fixed);
|
|
@@ -350,24 +369,26 @@ function createArrayType(schema, context) {
|
|
|
350
369
|
throw new Error(`Array schema requires an "items" definition: ${safeStringify(schema)}`);
|
|
351
370
|
}
|
|
352
371
|
const itemsType = constructType(schema.items, context);
|
|
353
|
-
return new ArrayType({ items: itemsType });
|
|
372
|
+
return new ArrayType({ items: itemsType, validate: context.validate });
|
|
354
373
|
}
|
|
355
374
|
function createMapType(schema, context) {
|
|
356
375
|
if (!("values" in schema)) {
|
|
357
376
|
throw new Error(`Map schema requires a "values" definition: ${safeStringify(schema)}`);
|
|
358
377
|
}
|
|
359
378
|
const valuesType = constructType(schema.values, context);
|
|
360
|
-
return new MapType({ values: valuesType });
|
|
379
|
+
return new MapType({ values: valuesType, validate: context.validate });
|
|
361
380
|
}
|
|
362
381
|
function createUnionType(schemas, context) {
|
|
363
382
|
if (schemas.length === 0) {
|
|
364
383
|
throw new Error("Union schema requires at least one branch type.");
|
|
365
384
|
}
|
|
366
385
|
const types = schemas.map((branch) => constructType(branch, context));
|
|
367
|
-
return new UnionType({ types });
|
|
386
|
+
return new UnionType({ types, validate: context.validate });
|
|
368
387
|
}
|
|
369
388
|
function isPrimitiveTypeName(value) {
|
|
370
|
-
return
|
|
389
|
+
return value === "null" || value === "boolean" || value === "int" ||
|
|
390
|
+
value === "long" || value === "float" || value === "double" ||
|
|
391
|
+
value === "bytes" || value === "string";
|
|
371
392
|
}
|
|
372
393
|
function materializeLazyRecordFields(registry) {
|
|
373
394
|
for (const type of registry.values()) {
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sachitv/avro-typescript",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Avro schema parsing, serialization, and RPC helpers for TypeScript runtimes.",
|
|
5
5
|
"homepage": "https://github.com/sachitv/avro-typescript",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "https://github.com/sachitv/avro-typescript"
|
|
8
|
+
"url": "git+https://github.com/sachitv/avro-typescript"
|
|
9
9
|
},
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"bugs": {
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Inverts the bits of the bytes in a Uint8Array up to a specified length.
|
|
3
|
-
* @param arr The Uint8Array to invert.
|
|
4
|
-
* @param len The number of bytes to invert from the beginning of the array.
|
|
5
|
-
*/
|
|
6
|
-
export declare function invert(arr: Uint8Array, len: number): void;
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Inverts the bits of the bytes in a Uint8Array up to a specified length.
|
|
3
|
-
* @param arr The Uint8Array to invert.
|
|
4
|
-
* @param len The number of bytes to invert from the beginning of the array.
|
|
5
|
-
*/
|
|
6
|
-
export function invert(arr, len) {
|
|
7
|
-
const view = new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
|
8
|
-
let remaining = Math.min(Math.max(len, 0), arr.length);
|
|
9
|
-
while (remaining--) {
|
|
10
|
-
const current = view.getUint8(remaining);
|
|
11
|
-
view.setUint8(remaining, (~current) & 0xff);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reads an unsigned little-endian integer from a buffer at a specified offset and byte length.
|
|
3
|
-
*/
|
|
4
|
-
export function readUIntLE(view, offset, byteLength) {
|
|
5
|
-
let value = 0;
|
|
6
|
-
for (let i = 0; i < byteLength; i++) {
|
|
7
|
-
const index = offset + i;
|
|
8
|
-
if (index >= view.byteLength) {
|
|
9
|
-
break;
|
|
10
|
-
}
|
|
11
|
-
value |= view.getUint8(index) << (8 * i);
|
|
12
|
-
}
|
|
13
|
-
return value >>> 0;
|
|
14
|
-
}
|