@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.
Files changed (122) hide show
  1. package/esm/avro_reader_sync.d.ts +48 -0
  2. package/esm/avro_reader_sync.js +79 -0
  3. package/esm/avro_writer_sync.d.ts +28 -0
  4. package/esm/avro_writer_sync.js +63 -0
  5. package/esm/internal/collections/circular_buffer.d.ts +4 -3
  6. package/esm/internal/collections/circular_buffer.js +8 -6
  7. package/esm/mod.d.ts +17 -0
  8. package/esm/mod.js +11 -0
  9. package/esm/schemas/base_type.d.ts +8 -0
  10. package/esm/schemas/base_type.js +21 -0
  11. package/esm/schemas/complex/array_type.d.ts +31 -10
  12. package/esm/schemas/complex/array_type.js +129 -39
  13. package/esm/schemas/complex/enum_type.d.ts +24 -7
  14. package/esm/schemas/complex/enum_type.js +61 -15
  15. package/esm/schemas/complex/fixed_type.d.ts +27 -12
  16. package/esm/schemas/complex/fixed_type.js +40 -27
  17. package/esm/schemas/complex/map_type.d.ts +31 -10
  18. package/esm/schemas/complex/map_type.js +88 -47
  19. package/esm/schemas/complex/named_type.d.ts +1 -1
  20. package/esm/schemas/complex/named_type.js +2 -2
  21. package/esm/schemas/complex/record_field.d.ts +62 -0
  22. package/esm/schemas/complex/record_field.js +112 -0
  23. package/esm/schemas/complex/record_resolver.d.ts +45 -0
  24. package/esm/schemas/complex/record_resolver.js +92 -0
  25. package/esm/schemas/complex/record_type.d.ts +45 -70
  26. package/esm/schemas/complex/record_type.js +158 -213
  27. package/esm/schemas/complex/record_writer_cache.d.ts +44 -0
  28. package/esm/schemas/complex/record_writer_cache.js +141 -0
  29. package/esm/schemas/complex/record_writer_strategy.d.ts +123 -0
  30. package/esm/schemas/complex/record_writer_strategy.js +309 -0
  31. package/esm/schemas/complex/union_type.d.ts +23 -11
  32. package/esm/schemas/complex/union_type.js +77 -29
  33. package/esm/schemas/logical/decimal_logical_type.d.ts +1 -1
  34. package/esm/schemas/logical/decimal_logical_type.js +2 -2
  35. package/esm/schemas/logical/duration_logical_type.d.ts +1 -1
  36. package/esm/schemas/logical/duration_logical_type.js +2 -2
  37. package/esm/schemas/logical/logical_type.d.ts +27 -9
  38. package/esm/schemas/logical/logical_type.js +50 -17
  39. package/esm/schemas/logical/temporal_logical_types.d.ts +9 -9
  40. package/esm/schemas/logical/temporal_logical_types.js +18 -18
  41. package/esm/schemas/logical/uuid_logical_type.d.ts +1 -1
  42. package/esm/schemas/logical/uuid_logical_type.js +2 -2
  43. package/esm/schemas/primitive/boolean_type.d.ts +15 -4
  44. package/esm/schemas/primitive/boolean_type.js +22 -7
  45. package/esm/schemas/primitive/bytes_type.d.ts +16 -5
  46. package/esm/schemas/primitive/bytes_type.js +31 -16
  47. package/esm/schemas/primitive/double_type.d.ts +13 -2
  48. package/esm/schemas/primitive/double_type.js +34 -5
  49. package/esm/schemas/primitive/fixed_size_base_type.d.ts +11 -7
  50. package/esm/schemas/primitive/fixed_size_base_type.js +14 -14
  51. package/esm/schemas/primitive/float_type.d.ts +11 -2
  52. package/esm/schemas/primitive/float_type.js +26 -5
  53. package/esm/schemas/primitive/int_type.d.ts +17 -6
  54. package/esm/schemas/primitive/int_type.js +26 -17
  55. package/esm/schemas/primitive/long_type.d.ts +16 -5
  56. package/esm/schemas/primitive/long_type.js +30 -16
  57. package/esm/schemas/primitive/null_type.d.ts +15 -4
  58. package/esm/schemas/primitive/null_type.js +20 -7
  59. package/esm/schemas/primitive/primitive_type.d.ts +2 -0
  60. package/esm/schemas/primitive/primitive_type.js +4 -0
  61. package/esm/schemas/primitive/string_type.d.ts +17 -6
  62. package/esm/schemas/primitive/string_type.js +33 -18
  63. package/esm/schemas/resolver.d.ts +6 -0
  64. package/esm/schemas/type.d.ts +64 -4
  65. package/esm/schemas/type.js +97 -0
  66. package/esm/serialization/avro_file_parser_sync.d.ts +34 -0
  67. package/esm/serialization/avro_file_parser_sync.js +160 -0
  68. package/esm/serialization/avro_file_writer_sync.d.ts +47 -0
  69. package/esm/serialization/avro_file_writer_sync.js +211 -0
  70. package/esm/serialization/buffers/blob_readable_buffer.d.ts +4 -3
  71. package/esm/serialization/buffers/blob_readable_buffer.js +20 -6
  72. package/esm/serialization/buffers/buffer.d.ts +6 -1
  73. package/esm/serialization/buffers/buffer.js +3 -0
  74. package/esm/serialization/buffers/buffer_error.d.ts +28 -0
  75. package/esm/serialization/buffers/buffer_error.js +70 -0
  76. package/esm/serialization/buffers/buffer_sync.d.ts +51 -0
  77. package/esm/serialization/buffers/buffer_sync.js +4 -0
  78. package/esm/serialization/buffers/in_memory_buffer.d.ts +1 -0
  79. package/esm/serialization/buffers/in_memory_buffer.js +7 -5
  80. package/esm/serialization/buffers/in_memory_buffer_sync.d.ts +133 -0
  81. package/esm/serialization/buffers/in_memory_buffer_sync.js +259 -0
  82. package/esm/serialization/counting_writable_tap.d.ts +45 -0
  83. package/esm/serialization/counting_writable_tap.js +90 -0
  84. package/esm/serialization/counting_writable_tap_sync.d.ts +46 -0
  85. package/esm/serialization/counting_writable_tap_sync.js +87 -0
  86. package/esm/serialization/decoders/decoder_null_sync.d.ts +12 -0
  87. package/esm/serialization/decoders/decoder_null_sync.js +13 -0
  88. package/esm/serialization/decoders/decoder_sync.d.ts +15 -0
  89. package/esm/serialization/decoders/decoder_sync.js +1 -0
  90. package/esm/serialization/encoders/encoder_null_sync.d.ts +12 -0
  91. package/esm/serialization/encoders/encoder_null_sync.js +13 -0
  92. package/esm/serialization/encoders/encoder_sync.d.ts +15 -0
  93. package/esm/serialization/encoders/encoder_sync.js +1 -0
  94. package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.d.ts +3 -2
  95. package/esm/serialization/streams/fixed_size_stream_readable_buffer_adapter.js +19 -9
  96. package/esm/serialization/streams/fixed_size_stream_reader_sync.d.ts +25 -0
  97. package/esm/serialization/streams/fixed_size_stream_reader_sync.js +63 -0
  98. package/esm/serialization/streams/fixed_size_stream_writer_sync.d.ts +45 -0
  99. package/esm/serialization/streams/fixed_size_stream_writer_sync.js +98 -0
  100. package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.d.ts +5 -3
  101. package/esm/serialization/streams/forward_only_stream_readable_buffer_adapter.js +18 -8
  102. package/esm/serialization/streams/stream_readable_buffer_adapter.d.ts +3 -2
  103. package/esm/serialization/streams/stream_readable_buffer_adapter.js +19 -6
  104. package/esm/serialization/streams/stream_readable_buffer_adapter_sync.d.ts +36 -0
  105. package/esm/serialization/streams/stream_readable_buffer_adapter_sync.js +93 -0
  106. package/esm/serialization/streams/stream_writable_buffer_adapter_sync.d.ts +45 -0
  107. package/esm/serialization/streams/stream_writable_buffer_adapter_sync.js +78 -0
  108. package/esm/serialization/streams/streams_sync.d.ts +36 -0
  109. package/esm/serialization/streams/streams_sync.js +4 -0
  110. package/esm/serialization/tap.d.ts +15 -32
  111. package/esm/serialization/tap.js +45 -134
  112. package/esm/serialization/tap_sync.d.ts +240 -0
  113. package/esm/serialization/tap_sync.js +545 -0
  114. package/esm/serialization/text_encoding.d.ts +16 -0
  115. package/esm/serialization/text_encoding.js +48 -1
  116. package/esm/type/create_type.d.ts +20 -0
  117. package/esm/type/create_type.js +49 -28
  118. package/package.json +2 -2
  119. package/esm/serialization/manipulate_bytes.d.ts +0 -6
  120. package/esm/serialization/manipulate_bytes.js +0 -13
  121. package/esm/serialization/read_uint_le.d.ts +0 -4
  122. package/esm/serialization/read_uint_le.js +0 -14
@@ -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
- 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
- };
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 PRIMITIVE_FACTORIES[type]();
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 PRIMITIVE_FACTORIES[name]();
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 typeof value === "string" && value in PRIMITIVE_FACTORIES;
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.4.1",
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,4 +0,0 @@
1
- /**
2
- * Reads an unsigned little-endian integer from a buffer at a specified offset and byte length.
3
- */
4
- export declare function readUIntLE(view: DataView, offset: number, byteLength: number): number;
@@ -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
- }