@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
@@ -0,0 +1,44 @@
1
+ import type { Type } from "../type.js";
2
+ import type { CompiledSyncWriter, CompiledWriter, RecordWriterContext, RecordWriterStrategy } from "./record_writer_strategy.js";
3
+ /**
4
+ * Cache for compiled record writers with support for recursive types.
5
+ *
6
+ * This class manages the four cache slots (strict/unchecked × sync/async)
7
+ * and implements the placeholder pattern that allows recursive record types
8
+ * to compile without infinite recursion.
9
+ */
10
+ export declare class RecordWriterCache {
11
+ #private;
12
+ /**
13
+ * Creates a new RecordWriterCache.
14
+ * @param strategy The writer strategy to use for compilation.
15
+ */
16
+ constructor(strategy?: RecordWriterStrategy);
17
+ /**
18
+ * Gets the writer strategy.
19
+ */
20
+ getStrategy(): RecordWriterStrategy;
21
+ /**
22
+ * Clears all cached writers.
23
+ * Call this when the record's fields change.
24
+ */
25
+ clear(): void;
26
+ /**
27
+ * Gets or creates a compiled async writer.
28
+ *
29
+ * @param validate Whether to validate values during writes.
30
+ * @param context The record writer context.
31
+ * @param getNestedWriter Callback to get writers for nested RecordTypes.
32
+ * @returns The compiled writer function.
33
+ */
34
+ getOrCreateWriter(validate: boolean, context: RecordWriterContext, getNestedWriter: (type: Type, validate: boolean) => CompiledWriter): CompiledWriter;
35
+ /**
36
+ * Gets or creates a compiled sync writer.
37
+ *
38
+ * @param validate Whether to validate values during writes.
39
+ * @param context The record writer context.
40
+ * @param getNestedWriter Callback to get writers for nested RecordTypes.
41
+ * @returns The compiled sync writer function.
42
+ */
43
+ getOrCreateSyncWriter(validate: boolean, context: RecordWriterContext, getNestedWriter: (type: Type, validate: boolean) => CompiledSyncWriter): CompiledSyncWriter;
44
+ }
@@ -0,0 +1,141 @@
1
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _RecordWriterCache_compiledSyncWriterStrict, _RecordWriterCache_compiledSyncWriterUnchecked, _RecordWriterCache_compiledWriterStrict, _RecordWriterCache_compiledWriterUnchecked, _RecordWriterCache_strategy;
13
+ import { defaultWriterStrategy } from "./record_writer_strategy.js";
14
+ /**
15
+ * Cache for compiled record writers with support for recursive types.
16
+ *
17
+ * This class manages the four cache slots (strict/unchecked × sync/async)
18
+ * and implements the placeholder pattern that allows recursive record types
19
+ * to compile without infinite recursion.
20
+ */
21
+ export class RecordWriterCache {
22
+ /**
23
+ * Creates a new RecordWriterCache.
24
+ * @param strategy The writer strategy to use for compilation.
25
+ */
26
+ constructor(strategy = defaultWriterStrategy) {
27
+ _RecordWriterCache_compiledSyncWriterStrict.set(this, void 0);
28
+ _RecordWriterCache_compiledSyncWriterUnchecked.set(this, void 0);
29
+ _RecordWriterCache_compiledWriterStrict.set(this, void 0);
30
+ _RecordWriterCache_compiledWriterUnchecked.set(this, void 0);
31
+ _RecordWriterCache_strategy.set(this, void 0);
32
+ __classPrivateFieldSet(this, _RecordWriterCache_strategy, strategy, "f");
33
+ }
34
+ /**
35
+ * Gets the writer strategy.
36
+ */
37
+ getStrategy() {
38
+ return __classPrivateFieldGet(this, _RecordWriterCache_strategy, "f");
39
+ }
40
+ /**
41
+ * Clears all cached writers.
42
+ * Call this when the record's fields change.
43
+ */
44
+ clear() {
45
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledSyncWriterStrict, undefined, "f");
46
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledSyncWriterUnchecked, undefined, "f");
47
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledWriterStrict, undefined, "f");
48
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledWriterUnchecked, undefined, "f");
49
+ }
50
+ /**
51
+ * Gets or creates a compiled async writer.
52
+ *
53
+ * @param validate Whether to validate values during writes.
54
+ * @param context The record writer context.
55
+ * @param getNestedWriter Callback to get writers for nested RecordTypes.
56
+ * @returns The compiled writer function.
57
+ */
58
+ getOrCreateWriter(validate, context, getNestedWriter) {
59
+ const cached = validate
60
+ ? __classPrivateFieldGet(this, _RecordWriterCache_compiledWriterStrict, "f")
61
+ : __classPrivateFieldGet(this, _RecordWriterCache_compiledWriterUnchecked, "f");
62
+ if (cached) {
63
+ return cached;
64
+ }
65
+ // Support recursive record types by installing a placeholder before walking
66
+ // field types; recursive references will see this function and avoid
67
+ // infinite recursion during compilation. The impl array allows the
68
+ // placeholder to delegate to the actual writer once it's created.
69
+ const impl = [null];
70
+ const placeholder = (tap, value) => impl[0](tap, value);
71
+ if (validate) {
72
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledWriterStrict, placeholder, "f");
73
+ }
74
+ else {
75
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledWriterUnchecked, placeholder, "f");
76
+ }
77
+ // Compile field writers using the strategy
78
+ const fieldCount = context.fieldTypes.length;
79
+ const fieldWriters = new Array(fieldCount);
80
+ for (let i = 0; i < fieldCount; i++) {
81
+ const fieldType = context.fieldTypes[i];
82
+ fieldWriters[i] = __classPrivateFieldGet(this, _RecordWriterCache_strategy, "f").compileFieldWriter(fieldType, validate, getNestedWriter);
83
+ }
84
+ // Assemble the complete record writer
85
+ const actualWriter = __classPrivateFieldGet(this, _RecordWriterCache_strategy, "f").assembleRecordWriter(context, fieldWriters);
86
+ if (validate) {
87
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledWriterStrict, actualWriter, "f");
88
+ }
89
+ else {
90
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledWriterUnchecked, actualWriter, "f");
91
+ }
92
+ impl[0] = actualWriter;
93
+ return placeholder;
94
+ }
95
+ /**
96
+ * Gets or creates a compiled sync writer.
97
+ *
98
+ * @param validate Whether to validate values during writes.
99
+ * @param context The record writer context.
100
+ * @param getNestedWriter Callback to get writers for nested RecordTypes.
101
+ * @returns The compiled sync writer function.
102
+ */
103
+ getOrCreateSyncWriter(validate, context, getNestedWriter) {
104
+ const cached = validate
105
+ ? __classPrivateFieldGet(this, _RecordWriterCache_compiledSyncWriterStrict, "f")
106
+ : __classPrivateFieldGet(this, _RecordWriterCache_compiledSyncWriterUnchecked, "f");
107
+ if (cached) {
108
+ return cached;
109
+ }
110
+ // Support recursive record types by installing a placeholder before walking
111
+ // field types; recursive references will see this function and avoid
112
+ // infinite recursion during compilation. The impl array allows the
113
+ // placeholder to delegate to the actual writer once it's created.
114
+ const impl = [null];
115
+ const placeholder = (tap, value) => impl[0](tap, value);
116
+ if (validate) {
117
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledSyncWriterStrict, placeholder, "f");
118
+ }
119
+ else {
120
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledSyncWriterUnchecked, placeholder, "f");
121
+ }
122
+ // Compile field writers using the strategy
123
+ const fieldCount = context.fieldTypes.length;
124
+ const fieldWriters = new Array(fieldCount);
125
+ for (let i = 0; i < fieldCount; i++) {
126
+ const fieldType = context.fieldTypes[i];
127
+ fieldWriters[i] = __classPrivateFieldGet(this, _RecordWriterCache_strategy, "f").compileSyncFieldWriter(fieldType, validate, getNestedWriter);
128
+ }
129
+ // Assemble the complete record writer
130
+ const actualWriter = __classPrivateFieldGet(this, _RecordWriterCache_strategy, "f").assembleSyncRecordWriter(context, fieldWriters);
131
+ impl[0] = actualWriter;
132
+ if (validate) {
133
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledSyncWriterStrict, actualWriter, "f");
134
+ }
135
+ else {
136
+ __classPrivateFieldSet(this, _RecordWriterCache_compiledSyncWriterUnchecked, actualWriter, "f");
137
+ }
138
+ return placeholder;
139
+ }
140
+ }
141
+ _RecordWriterCache_compiledSyncWriterStrict = new WeakMap(), _RecordWriterCache_compiledSyncWriterUnchecked = new WeakMap(), _RecordWriterCache_compiledWriterStrict = new WeakMap(), _RecordWriterCache_compiledWriterUnchecked = new WeakMap(), _RecordWriterCache_strategy = new WeakMap();
@@ -0,0 +1,123 @@
1
+ import type { WritableTapLike } from "../../serialization/tap.js";
2
+ import type { SyncWritableTapLike } from "../../serialization/tap_sync.js";
3
+ import type { Type } from "../type.js";
4
+ /**
5
+ * Compiled async writer function signature.
6
+ */
7
+ export type CompiledWriter = (tap: WritableTapLike, value: unknown) => Promise<void>;
8
+ /**
9
+ * Compiled sync writer function signature.
10
+ */
11
+ export type CompiledSyncWriter = (tap: SyncWritableTapLike, value: unknown) => void;
12
+ /**
13
+ * Context passed to strategy methods for assembling record writers.
14
+ */
15
+ export interface RecordWriterContext {
16
+ /** Field names in order. */
17
+ fieldNames: string[];
18
+ /** Field types in order. */
19
+ fieldTypes: Type[];
20
+ /** Default value getters for each field (undefined if no default). */
21
+ fieldDefaultGetters: Array<(() => unknown) | undefined>;
22
+ /** Whether each field has a default value. */
23
+ fieldHasDefault: boolean[];
24
+ /** Whether to validate values during writes. */
25
+ validate: boolean;
26
+ /** Reference to the parent RecordType for error reporting. */
27
+ recordType: Type;
28
+ /** Callback to check if a value is a valid record object. */
29
+ isRecord: (value: unknown) => value is Record<string, unknown>;
30
+ }
31
+ /**
32
+ * Strategy interface for compiling record field writers.
33
+ *
34
+ * Implementations can choose different approaches:
35
+ * - CompiledWriterStrategy: Inlines primitive tap methods for performance
36
+ * - InterpretedWriterStrategy: Delegates to type.write() for simplicity
37
+ */
38
+ export interface RecordWriterStrategy {
39
+ /**
40
+ * Compiles an async writer for a single field type.
41
+ * @param fieldType The type of the field.
42
+ * @param validate Whether to validate values.
43
+ * @param getRecordWriter Callback to get a writer for nested RecordTypes (handles recursion).
44
+ */
45
+ compileFieldWriter(fieldType: Type, validate: boolean, getRecordWriter: (type: Type, validate: boolean) => CompiledWriter): CompiledWriter;
46
+ /**
47
+ * Compiles a sync writer for a single field type.
48
+ * @param fieldType The type of the field.
49
+ * @param validate Whether to validate values.
50
+ * @param getRecordWriter Callback to get a writer for nested RecordTypes (handles recursion).
51
+ */
52
+ compileSyncFieldWriter(fieldType: Type, validate: boolean, getRecordWriter: (type: Type, validate: boolean) => CompiledSyncWriter): CompiledSyncWriter;
53
+ /**
54
+ * Assembles field writers into a complete async record writer.
55
+ * @param context The record writer context.
56
+ * @param fieldWriters Pre-compiled writers for each field.
57
+ */
58
+ assembleRecordWriter(context: RecordWriterContext, fieldWriters: CompiledWriter[]): CompiledWriter;
59
+ /**
60
+ * Assembles field writers into a complete sync record writer.
61
+ * @param context The record writer context.
62
+ * @param fieldWriters Pre-compiled writers for each field.
63
+ */
64
+ assembleSyncRecordWriter(context: RecordWriterContext, fieldWriters: CompiledSyncWriter[]): CompiledSyncWriter;
65
+ }
66
+ /**
67
+ * Compiled writer strategy that inlines primitive tap methods for performance.
68
+ *
69
+ * This is the default strategy that provides optimized serialization by:
70
+ * - Inlining primitive type writes directly to tap methods
71
+ * - Using unchecked writes when validation is disabled
72
+ * - Supporting recursive record types via the getRecordWriter callback
73
+ */
74
+ export declare class CompiledWriterStrategy implements RecordWriterStrategy {
75
+ #private;
76
+ /**
77
+ * Compiles an async writer for a single field type with primitive inlining.
78
+ */
79
+ compileFieldWriter(fieldType: Type, validate: boolean, getRecordWriter: (type: Type, validate: boolean) => CompiledWriter): CompiledWriter;
80
+ /**
81
+ * Compiles a sync writer for a single field type with primitive inlining.
82
+ */
83
+ compileSyncFieldWriter(fieldType: Type, validate: boolean, getRecordWriter: (type: Type, validate: boolean) => CompiledSyncWriter): CompiledSyncWriter;
84
+ /**
85
+ * Assembles an async record writer from field writers.
86
+ */
87
+ assembleRecordWriter(context: RecordWriterContext, fieldWriters: CompiledWriter[]): CompiledWriter;
88
+ /**
89
+ * Assembles a sync record writer from field writers.
90
+ */
91
+ assembleSyncRecordWriter(context: RecordWriterContext, fieldWriters: CompiledSyncWriter[]): CompiledSyncWriter;
92
+ }
93
+ /**
94
+ * Interpreted writer strategy that delegates to type.write() methods.
95
+ *
96
+ * This strategy provides simpler, more straightforward serialization by
97
+ * delegating all writes to the type's own write methods. It's useful for:
98
+ * - Debugging and testing
99
+ * - Scenarios where compilation overhead isn't worth the performance gain
100
+ * - Future extensibility without recompilation
101
+ */
102
+ export declare class InterpretedWriterStrategy implements RecordWriterStrategy {
103
+ /**
104
+ * Compiles an async writer that delegates to the type's write method.
105
+ */
106
+ compileFieldWriter(fieldType: Type, validate: boolean, _getRecordWriter: (type: Type, validate: boolean) => CompiledWriter): CompiledWriter;
107
+ /**
108
+ * Compiles a sync writer that delegates to the type's writeSync method.
109
+ */
110
+ compileSyncFieldWriter(fieldType: Type, validate: boolean, _getRecordWriter: (type: Type, validate: boolean) => CompiledSyncWriter): CompiledSyncWriter;
111
+ /**
112
+ * Assembles an async record writer from field writers.
113
+ */
114
+ assembleRecordWriter(context: RecordWriterContext, fieldWriters: CompiledWriter[]): CompiledWriter;
115
+ /**
116
+ * Assembles a sync record writer from field writers.
117
+ */
118
+ assembleSyncRecordWriter(context: RecordWriterContext, fieldWriters: CompiledSyncWriter[]): CompiledSyncWriter;
119
+ }
120
+ /**
121
+ * Default strategy instance used when no strategy is specified.
122
+ */
123
+ export declare const defaultWriterStrategy: RecordWriterStrategy;
@@ -0,0 +1,309 @@
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, _CompiledWriterStrategy_compileUncheckedWriter, _CompiledWriterStrategy_compileUncheckedSyncWriter;
7
+ import { throwInvalidError } from "../error.js";
8
+ import { BooleanType } from "../primitive/boolean_type.js";
9
+ import { BytesType } from "../primitive/bytes_type.js";
10
+ import { DoubleType } from "../primitive/double_type.js";
11
+ import { FloatType } from "../primitive/float_type.js";
12
+ import { IntType } from "../primitive/int_type.js";
13
+ import { LongType } from "../primitive/long_type.js";
14
+ import { NullType } from "../primitive/null_type.js";
15
+ import { StringType } from "../primitive/string_type.js";
16
+ /**
17
+ * Checks if a type is a RecordType without importing it (avoids circular dependency).
18
+ */
19
+ function isRecordType(type) {
20
+ return type.constructor.name === "RecordType";
21
+ }
22
+ /**
23
+ * Compiled writer strategy that inlines primitive tap methods for performance.
24
+ *
25
+ * This is the default strategy that provides optimized serialization by:
26
+ * - Inlining primitive type writes directly to tap methods
27
+ * - Using unchecked writes when validation is disabled
28
+ * - Supporting recursive record types via the getRecordWriter callback
29
+ */
30
+ export class CompiledWriterStrategy {
31
+ /**
32
+ * Compiles an async writer for a single field type with primitive inlining.
33
+ */
34
+ compileFieldWriter(fieldType, validate, getRecordWriter) {
35
+ if (isRecordType(fieldType)) {
36
+ return getRecordWriter(fieldType, validate);
37
+ }
38
+ if (!validate) {
39
+ return __classPrivateFieldGet(_a, _a, "m", _CompiledWriterStrategy_compileUncheckedWriter).call(_a, fieldType);
40
+ }
41
+ return (tap, value) => fieldType.write(tap, value);
42
+ }
43
+ /**
44
+ * Compiles a sync writer for a single field type with primitive inlining.
45
+ */
46
+ compileSyncFieldWriter(fieldType, validate, getRecordWriter) {
47
+ if (isRecordType(fieldType)) {
48
+ return getRecordWriter(fieldType, validate);
49
+ }
50
+ if (!validate) {
51
+ return __classPrivateFieldGet(_a, _a, "m", _CompiledWriterStrategy_compileUncheckedSyncWriter).call(_a, fieldType);
52
+ }
53
+ return (tap, value) => fieldType.writeSync(tap, value);
54
+ }
55
+ /**
56
+ * Assembles an async record writer from field writers.
57
+ */
58
+ assembleRecordWriter(context, fieldWriters) {
59
+ const { fieldNames, fieldDefaultGetters, fieldHasDefault, validate, recordType, isRecord, } = context;
60
+ const fieldCount = fieldNames.length;
61
+ const hasAnyDefaults = fieldHasDefault.some((hasDefault) => hasDefault);
62
+ return async (tap, value) => {
63
+ if (validate && !isRecord(value)) {
64
+ throwInvalidError([], value, recordType);
65
+ }
66
+ const record = value;
67
+ if (!validate && !hasAnyDefaults) {
68
+ for (let i = 0; i < fieldCount; i++) {
69
+ const name = fieldNames[i];
70
+ await fieldWriters[i](tap, record[name]);
71
+ }
72
+ return;
73
+ }
74
+ for (let i = 0; i < fieldCount; i++) {
75
+ const name = fieldNames[i];
76
+ const hasValue = Object.hasOwn(record, name);
77
+ let toWrite;
78
+ if (hasValue) {
79
+ toWrite = record[name];
80
+ }
81
+ else {
82
+ const getter = fieldDefaultGetters[i];
83
+ if (getter) {
84
+ toWrite = getter();
85
+ }
86
+ else {
87
+ if (validate) {
88
+ throwInvalidError([name], undefined, recordType);
89
+ }
90
+ toWrite = undefined;
91
+ }
92
+ }
93
+ await fieldWriters[i](tap, toWrite);
94
+ }
95
+ };
96
+ }
97
+ /**
98
+ * Assembles a sync record writer from field writers.
99
+ */
100
+ assembleSyncRecordWriter(context, fieldWriters) {
101
+ const { fieldNames, fieldDefaultGetters, fieldHasDefault, validate, recordType, isRecord, } = context;
102
+ const fieldCount = fieldNames.length;
103
+ const hasAnyDefaults = fieldHasDefault.some((hasDefault) => hasDefault);
104
+ return (tap, value) => {
105
+ if (validate && !isRecord(value)) {
106
+ throwInvalidError([], value, recordType);
107
+ }
108
+ const record = value;
109
+ if (!validate && !hasAnyDefaults) {
110
+ for (let i = 0; i < fieldCount; i++) {
111
+ const name = fieldNames[i];
112
+ fieldWriters[i](tap, record[name]);
113
+ }
114
+ return;
115
+ }
116
+ for (let i = 0; i < fieldCount; i++) {
117
+ const name = fieldNames[i];
118
+ const hasValue = Object.hasOwn(record, name);
119
+ let toWrite;
120
+ if (hasValue) {
121
+ toWrite = record[name];
122
+ }
123
+ else {
124
+ const getter = fieldDefaultGetters[i];
125
+ if (getter) {
126
+ toWrite = getter();
127
+ }
128
+ else {
129
+ if (validate) {
130
+ throwInvalidError([name], undefined, recordType);
131
+ }
132
+ toWrite = undefined;
133
+ }
134
+ }
135
+ fieldWriters[i](tap, toWrite);
136
+ }
137
+ };
138
+ }
139
+ }
140
+ _a = CompiledWriterStrategy, _CompiledWriterStrategy_compileUncheckedWriter = function _CompiledWriterStrategy_compileUncheckedWriter(type) {
141
+ if (type instanceof NullType) {
142
+ return async () => { };
143
+ }
144
+ if (type instanceof BooleanType) {
145
+ return (tap, value) => tap.writeBoolean(value);
146
+ }
147
+ if (type instanceof IntType) {
148
+ return (tap, value) => tap.writeInt(value);
149
+ }
150
+ if (type instanceof LongType) {
151
+ return (tap, value) => tap.writeLong(value);
152
+ }
153
+ if (type instanceof FloatType) {
154
+ return (tap, value) => tap.writeFloat(value);
155
+ }
156
+ if (type instanceof DoubleType) {
157
+ return (tap, value) => tap.writeDouble(value);
158
+ }
159
+ if (type instanceof BytesType) {
160
+ return (tap, value) => tap.writeBytes(value);
161
+ }
162
+ if (type instanceof StringType) {
163
+ return (tap, value) => tap.writeString(value);
164
+ }
165
+ return (tap, value) => type.writeUnchecked(tap, value);
166
+ }, _CompiledWriterStrategy_compileUncheckedSyncWriter = function _CompiledWriterStrategy_compileUncheckedSyncWriter(type) {
167
+ if (type instanceof NullType) {
168
+ return () => { };
169
+ }
170
+ if (type instanceof BooleanType) {
171
+ return (tap, value) => tap.writeBoolean(value);
172
+ }
173
+ if (type instanceof IntType) {
174
+ return (tap, value) => tap.writeInt(value);
175
+ }
176
+ if (type instanceof LongType) {
177
+ return (tap, value) => tap.writeLong(value);
178
+ }
179
+ if (type instanceof FloatType) {
180
+ return (tap, value) => tap.writeFloat(value);
181
+ }
182
+ if (type instanceof DoubleType) {
183
+ return (tap, value) => tap.writeDouble(value);
184
+ }
185
+ if (type instanceof BytesType) {
186
+ return (tap, value) => tap.writeBytes(value);
187
+ }
188
+ if (type instanceof StringType) {
189
+ return (tap, value) => tap.writeString(value);
190
+ }
191
+ return (tap, value) => type.writeSyncUnchecked(tap, value);
192
+ };
193
+ /**
194
+ * Interpreted writer strategy that delegates to type.write() methods.
195
+ *
196
+ * This strategy provides simpler, more straightforward serialization by
197
+ * delegating all writes to the type's own write methods. It's useful for:
198
+ * - Debugging and testing
199
+ * - Scenarios where compilation overhead isn't worth the performance gain
200
+ * - Future extensibility without recompilation
201
+ */
202
+ export class InterpretedWriterStrategy {
203
+ /**
204
+ * Compiles an async writer that delegates to the type's write method.
205
+ */
206
+ compileFieldWriter(fieldType, validate, _getRecordWriter) {
207
+ if (validate) {
208
+ return (tap, value) => fieldType.write(tap, value);
209
+ }
210
+ return (tap, value) => fieldType.writeUnchecked(tap, value);
211
+ }
212
+ /**
213
+ * Compiles a sync writer that delegates to the type's writeSync method.
214
+ */
215
+ compileSyncFieldWriter(fieldType, validate, _getRecordWriter) {
216
+ if (validate) {
217
+ return (tap, value) => fieldType.writeSync(tap, value);
218
+ }
219
+ return (tap, value) => fieldType.writeSyncUnchecked(tap, value);
220
+ }
221
+ /**
222
+ * Assembles an async record writer from field writers.
223
+ */
224
+ assembleRecordWriter(context, fieldWriters) {
225
+ const { fieldNames, fieldDefaultGetters, fieldHasDefault, validate, recordType, isRecord, } = context;
226
+ const fieldCount = fieldNames.length;
227
+ const hasAnyDefaults = fieldHasDefault.some((hasDefault) => hasDefault);
228
+ return async (tap, value) => {
229
+ if (validate && !isRecord(value)) {
230
+ throwInvalidError([], value, recordType);
231
+ }
232
+ const record = value;
233
+ if (!validate && !hasAnyDefaults) {
234
+ for (let i = 0; i < fieldCount; i++) {
235
+ const name = fieldNames[i];
236
+ await fieldWriters[i](tap, record[name]);
237
+ }
238
+ return;
239
+ }
240
+ for (let i = 0; i < fieldCount; i++) {
241
+ const name = fieldNames[i];
242
+ const hasValue = Object.hasOwn(record, name);
243
+ let toWrite;
244
+ if (hasValue) {
245
+ toWrite = record[name];
246
+ }
247
+ else {
248
+ const getter = fieldDefaultGetters[i];
249
+ if (getter) {
250
+ toWrite = getter();
251
+ }
252
+ else {
253
+ if (validate) {
254
+ throwInvalidError([name], undefined, recordType);
255
+ }
256
+ toWrite = undefined;
257
+ }
258
+ }
259
+ await fieldWriters[i](tap, toWrite);
260
+ }
261
+ };
262
+ }
263
+ /**
264
+ * Assembles a sync record writer from field writers.
265
+ */
266
+ assembleSyncRecordWriter(context, fieldWriters) {
267
+ const { fieldNames, fieldDefaultGetters, fieldHasDefault, validate, recordType, isRecord, } = context;
268
+ const fieldCount = fieldNames.length;
269
+ const hasAnyDefaults = fieldHasDefault.some((hasDefault) => hasDefault);
270
+ return (tap, value) => {
271
+ if (validate && !isRecord(value)) {
272
+ throwInvalidError([], value, recordType);
273
+ }
274
+ const record = value;
275
+ if (!validate && !hasAnyDefaults) {
276
+ for (let i = 0; i < fieldCount; i++) {
277
+ const name = fieldNames[i];
278
+ fieldWriters[i](tap, record[name]);
279
+ }
280
+ return;
281
+ }
282
+ for (let i = 0; i < fieldCount; i++) {
283
+ const name = fieldNames[i];
284
+ const hasValue = Object.hasOwn(record, name);
285
+ let toWrite;
286
+ if (hasValue) {
287
+ toWrite = record[name];
288
+ }
289
+ else {
290
+ const getter = fieldDefaultGetters[i];
291
+ if (getter) {
292
+ toWrite = getter();
293
+ }
294
+ else {
295
+ if (validate) {
296
+ throwInvalidError([name], undefined, recordType);
297
+ }
298
+ toWrite = undefined;
299
+ }
300
+ }
301
+ fieldWriters[i](tap, toWrite);
302
+ }
303
+ };
304
+ }
305
+ }
306
+ /**
307
+ * Default strategy instance used when no strategy is specified.
308
+ */
309
+ export const defaultWriterStrategy = new CompiledWriterStrategy();