@sachitv/avro-typescript 0.4.2 → 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 +1 -1
  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
@@ -1 +1,4 @@
1
+ /**
2
+ * Interface describing a random-access readable buffer.
3
+ */
1
4
  export {};
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Shared error types for buffer implementations.
3
+ *
4
+ * These errors are shared between the async and sync buffer implementations so
5
+ * that callers can rely on consistent error shapes.
6
+ */
7
+ /** Error thrown when a readable buffer operation fails. */
8
+ export declare class ReadBufferError extends Error {
9
+ /** The offset where the read operation failed. */
10
+ readonly offset: number;
11
+ /** The size requested for the read operation. */
12
+ readonly size: number;
13
+ /** The total buffer length (or best-effort upper bound). */
14
+ readonly bufferLength: number;
15
+ /** Creates a new ReadBufferError. */
16
+ constructor(message: string, offset: number, size: number, bufferLength: number);
17
+ }
18
+ /** Error thrown when a writable buffer operation fails. */
19
+ export declare class WriteBufferError extends Error {
20
+ /** The offset where the write operation failed. */
21
+ readonly offset: number;
22
+ /** The size of data being written. */
23
+ readonly dataSize: number;
24
+ /** The total buffer length (or best-effort upper bound). */
25
+ readonly bufferLength: number;
26
+ /** Creates a new WriteBufferError. */
27
+ constructor(message: string, offset: number, dataSize: number, bufferLength: number);
28
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Shared error types for buffer implementations.
3
+ *
4
+ * These errors are shared between the async and sync buffer implementations so
5
+ * that callers can rely on consistent error shapes.
6
+ */
7
+ /** Error thrown when a readable buffer operation fails. */
8
+ export class ReadBufferError extends Error {
9
+ /** Creates a new ReadBufferError. */
10
+ constructor(message, offset, size, bufferLength) {
11
+ super(message);
12
+ /** The offset where the read operation failed. */
13
+ Object.defineProperty(this, "offset", {
14
+ enumerable: true,
15
+ configurable: true,
16
+ writable: true,
17
+ value: void 0
18
+ });
19
+ /** The size requested for the read operation. */
20
+ Object.defineProperty(this, "size", {
21
+ enumerable: true,
22
+ configurable: true,
23
+ writable: true,
24
+ value: void 0
25
+ });
26
+ /** The total buffer length (or best-effort upper bound). */
27
+ Object.defineProperty(this, "bufferLength", {
28
+ enumerable: true,
29
+ configurable: true,
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ this.name = "ReadBufferError";
34
+ this.offset = offset;
35
+ this.size = size;
36
+ this.bufferLength = bufferLength;
37
+ }
38
+ }
39
+ /** Error thrown when a writable buffer operation fails. */
40
+ export class WriteBufferError extends Error {
41
+ /** Creates a new WriteBufferError. */
42
+ constructor(message, offset, dataSize, bufferLength) {
43
+ super(message);
44
+ /** The offset where the write operation failed. */
45
+ Object.defineProperty(this, "offset", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ /** The size of data being written. */
52
+ Object.defineProperty(this, "dataSize", {
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true,
56
+ value: void 0
57
+ });
58
+ /** The total buffer length (or best-effort upper bound). */
59
+ Object.defineProperty(this, "bufferLength", {
60
+ enumerable: true,
61
+ configurable: true,
62
+ writable: true,
63
+ value: void 0
64
+ });
65
+ this.name = "WriteBufferError";
66
+ this.offset = offset;
67
+ this.dataSize = dataSize;
68
+ this.bufferLength = bufferLength;
69
+ }
70
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Synchronous interfaces for readable and writable buffers.
3
+ */
4
+ export { ReadBufferError, WriteBufferError } from "./buffer_error.js";
5
+ /**
6
+ * Interface describing a random-access readable buffer (synchronous).
7
+ */
8
+ export interface ISyncReadable {
9
+ /**
10
+ * Reads a portion of the buffer starting at offset with the given size.
11
+ *
12
+ * @throws ReadBufferError when the requested range is invalid or exceeds the
13
+ * available bounds.
14
+ */
15
+ read(offset: number, size: number): Uint8Array;
16
+ /**
17
+ * Checks if more data can be read starting at the given offset.
18
+ */
19
+ canReadMore(offset: number): boolean;
20
+ }
21
+ /**
22
+ * Interface describing an append-only writable buffer (synchronous).
23
+ */
24
+ export interface ISyncWritable {
25
+ /**
26
+ * Appends bytes to the buffer, advancing its internal write cursor when the
27
+ * operation succeeds.
28
+ *
29
+ * @throws WriteBufferError when the buffer cannot accept the requested bytes.
30
+ */
31
+ appendBytes(data: Uint8Array): void;
32
+ /**
33
+ * Appends a slice of bytes to the buffer without requiring callers to create
34
+ * a subarray view.
35
+ */
36
+ appendBytesFrom(data: Uint8Array, offset: number, length: number): void;
37
+ /**
38
+ * Returns whether the buffer can continue accepting writes. Implementations
39
+ * should flip this to `false` after a write would exceed capacity so callers
40
+ * can detect the overflow condition.
41
+ */
42
+ isValid(): boolean;
43
+ /**
44
+ * Checks if the buffer can accept appending the given number of bytes.
45
+ */
46
+ canAppendMore(size: number): boolean;
47
+ }
48
+ /**
49
+ * Convenience type for buffers capable of both read and write operations.
50
+ */
51
+ export type ISyncReadableAndWritable = ISyncReadable & ISyncWritable;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Synchronous interfaces for readable and writable buffers.
3
+ */
4
+ export { ReadBufferError, WriteBufferError } from "./buffer_error.js";
@@ -87,6 +87,7 @@ export declare class InMemoryWritableBuffer extends InMemoryBufferBase implement
87
87
  * Creates a new writable buffer with the specified ArrayBuffer and initial offset.
88
88
  * @param buf The underlying ArrayBuffer to write to.
89
89
  * @param offset The starting offset within the buffer (default: 0).
90
+ * @throws WriteBufferError If the initial offset is invalid.
90
91
  */
91
92
  constructor(buf: ArrayBuffer, offset?: number);
92
93
  /**
@@ -10,6 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
12
  var _InMemoryWritableBuffer_offset, _InMemoryWritableBuffer_initialOffset;
13
+ import { ReadBufferError, WriteBufferError } from "./buffer_error.js";
13
14
  /**
14
15
  * Shared strict in-memory buffer base with common functionality.
15
16
  */
@@ -67,10 +68,10 @@ export class InMemoryReadableBuffer extends InMemoryBufferBase {
67
68
  */
68
69
  checkBounds(offset, size) {
69
70
  if (offset < 0 || size < 0) {
70
- throw new RangeError(`Offset and size must be non-negative. Got offset=${offset}, size=${size}`);
71
+ throw new ReadBufferError(`Offset and size must be non-negative. Got offset=${offset}, size=${size}`, offset, size, this.view.length);
71
72
  }
72
73
  if (offset + size > this.view.length) {
73
- throw new RangeError(`Operation exceeds buffer bounds. offset=${offset}, size=${size}, bufferLength=${this.view.length}`);
74
+ throw new ReadBufferError(`Operation exceeds buffer bounds. offset=${offset}, size=${size}, bufferLength=${this.view.length}`, offset, size, this.view.length);
74
75
  }
75
76
  }
76
77
  /**
@@ -127,13 +128,14 @@ export class InMemoryWritableBuffer extends InMemoryBufferBase {
127
128
  * Creates a new writable buffer with the specified ArrayBuffer and initial offset.
128
129
  * @param buf The underlying ArrayBuffer to write to.
129
130
  * @param offset The starting offset within the buffer (default: 0).
131
+ * @throws WriteBufferError If the initial offset is invalid.
130
132
  */
131
133
  constructor(buf, offset = 0) {
132
134
  super(buf);
133
135
  _InMemoryWritableBuffer_offset.set(this, void 0);
134
136
  _InMemoryWritableBuffer_initialOffset.set(this, void 0);
135
137
  if (offset < 0 || offset > this.view.length) {
136
- throw new RangeError(`Initial offset must be within buffer bounds. Got offset=${offset}, bufferLength=${this.view.length}`);
138
+ throw new WriteBufferError(`Initial offset must be within buffer bounds. Got offset=${offset}, bufferLength=${this.view.length}`, offset, 0, this.view.length);
137
139
  }
138
140
  __classPrivateFieldSet(this, _InMemoryWritableBuffer_initialOffset, offset, "f");
139
141
  __classPrivateFieldSet(this, _InMemoryWritableBuffer_offset, offset, "f");
@@ -146,10 +148,10 @@ export class InMemoryWritableBuffer extends InMemoryBufferBase {
146
148
  */
147
149
  checkWriteBounds(offset, data) {
148
150
  if (offset < 0) {
149
- throw new RangeError(`Offset must be non-negative. Got offset=${offset}`);
151
+ throw new WriteBufferError(`Offset must be non-negative. Got offset=${offset}`, offset, data.length, this.view.length);
150
152
  }
151
153
  if (offset + data.length > this.view.length) {
152
- throw new RangeError(`Write operation exceeds buffer bounds. offset=${offset}, dataSize=${data.length}, bufferLength=${this.view.length}`);
154
+ throw new WriteBufferError(`Write operation exceeds buffer bounds. offset=${offset}, dataSize=${data.length}, bufferLength=${this.view.length}`, offset, data.length, this.view.length);
153
155
  }
154
156
  }
155
157
  /**
@@ -0,0 +1,133 @@
1
+ import type { ISyncReadable, ISyncWritable } from "./buffer_sync.js";
2
+ /**
3
+ * Shared strict in-memory buffer base with common functionality.
4
+ */
5
+ export declare abstract class SyncInMemoryBufferBase {
6
+ /** The underlying Uint8Array view of the buffer. */
7
+ protected readonly view: Uint8Array;
8
+ /**
9
+ * Constructs a SyncInMemoryBufferBase with the given ArrayBuffer.
10
+ * @param buf The ArrayBuffer to create the view from.
11
+ */
12
+ constructor(buf: ArrayBuffer);
13
+ /**
14
+ * Returns the length of the buffer in bytes.
15
+ * @returns The length of the buffer.
16
+ */
17
+ length(): number;
18
+ }
19
+ /**
20
+ * Strict read-only in-memory buffer for serialization reads (synchronous).
21
+ * Throws errors on all out-of-bounds operations.
22
+ *
23
+ * Key features:
24
+ * - Fixed size: The buffer size is determined at construction and cannot be resized.
25
+ * - Random access: Supports reading at arbitrary byte offsets.
26
+ * - Strict bounds checking: Operations that exceed buffer bounds throw ReadBufferError.
27
+ * - Efficient: Uses Uint8Array for fast byte-level operations.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const arrayBuffer = new ArrayBuffer(1024);
32
+ * const buffer = new SyncInMemoryReadableBuffer(arrayBuffer);
33
+ *
34
+ * // Read some data
35
+ * const data = buffer.read(0, 4); // Returns Uint8Array of 4 bytes
36
+ *
37
+ * // This will throw:
38
+ * buffer.read(1020, 10); // ReadBufferError: Operation exceeds buffer bounds
39
+ * ```
40
+ */
41
+ export declare class SyncInMemoryReadableBuffer extends SyncInMemoryBufferBase implements ISyncReadable {
42
+ /**
43
+ * Checks if the offset and size are within buffer bounds.
44
+ * @param offset The starting offset.
45
+ * @param size The number of bytes to check.
46
+ */
47
+ private checkBounds;
48
+ /**
49
+ * Reads a portion of the buffer.
50
+ * @param offset The starting offset.
51
+ * @param size The number of bytes to read.
52
+ * @returns A Uint8Array containing the read bytes.
53
+ */
54
+ read(offset: number, size: number): Uint8Array;
55
+ /**
56
+ * Checks if more data can be read starting at the given offset.
57
+ * @param offset The byte offset to check.
58
+ * @returns True if at least one byte can be read from the offset.
59
+ */
60
+ canReadMore(offset: number): boolean;
61
+ }
62
+ /**
63
+ * Strict write-only in-memory buffer for serialization writes (synchronous).
64
+ * Throws errors when attempting to write beyond buffer bounds.
65
+ *
66
+ * Key features:
67
+ * - Fixed size: The buffer size is determined at construction and cannot be resized.
68
+ * - Sequential writes: Maintains an internal offset that advances with each write.
69
+ * - Strict bounds checking: Throws WriteBufferError when write would exceed buffer capacity.
70
+ * - Efficient: Uses Uint8Array for fast byte-level operations.
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * const arrayBuffer = new ArrayBuffer(1024);
75
+ * const buffer = new SyncInMemoryWritableBuffer(arrayBuffer);
76
+ *
77
+ * // Write some data
78
+ * buffer.appendBytes(new Uint8Array([1, 2, 3, 4]));
79
+ *
80
+ * // This will throw if buffer is full:
81
+ * buffer.appendBytes(new Uint8Array(2000)); // WriteBufferError: Write operation exceeds buffer bounds
82
+ * ```
83
+ */
84
+ export declare class SyncInMemoryWritableBuffer extends SyncInMemoryBufferBase implements ISyncWritable {
85
+ #private;
86
+ /**
87
+ * Creates a new writable buffer with the specified ArrayBuffer and initial offset.
88
+ * @param buf The underlying ArrayBuffer to write to.
89
+ * @param offset The starting offset within the buffer (default: 0).
90
+ */
91
+ constructor(buf: ArrayBuffer, offset?: number);
92
+ /**
93
+ * Checks if writing the given data at the specified offset would exceed buffer bounds.
94
+ * Throws a WriteBufferError if the operation would exceed bounds.
95
+ * @param offset The offset to write at.
96
+ * @param data The data to write.
97
+ */
98
+ protected checkWriteBounds(offset: number, data: Uint8Array): void;
99
+ /**
100
+ * Checks if writing the given number of bytes at the specified offset would exceed buffer bounds.
101
+ * Throws a WriteBufferError if the operation would exceed bounds.
102
+ * @param offset The offset to write at.
103
+ * @param size The number of bytes to write.
104
+ */
105
+ protected checkWriteBoundsSize(offset: number, size: number): void;
106
+ /**
107
+ * Appends the given bytes to the buffer at the current offset and advances the offset.
108
+ * @param data The bytes to append.
109
+ */
110
+ appendBytes(data: Uint8Array): void;
111
+ /**
112
+ * Appends a slice of bytes to the buffer at the current offset and advances the offset.
113
+ * This avoids allocating subarray views in higher-level hot paths.
114
+ */
115
+ appendBytesFrom(data: Uint8Array, offset: number, length: number): void;
116
+ /**
117
+ * Checks if the buffer is in a valid state.
118
+ * Always returns true as this buffer throws on overflow instead of becoming invalid.
119
+ * @returns Always true.
120
+ */
121
+ isValid(): boolean;
122
+ /**
123
+ * Checks if the buffer can accept appending the given number of bytes.
124
+ * @param size The number of bytes to check.
125
+ * @returns True if the buffer can accept the append.
126
+ */
127
+ canAppendMore(_size: number): boolean;
128
+ /**
129
+ * Gets a copy of the buffer containing only the bytes written to the buffer.
130
+ * @returns An ArrayBuffer containing only the written data.
131
+ */
132
+ getBufferCopy(): ArrayBuffer;
133
+ }
@@ -0,0 +1,259 @@
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 _SyncInMemoryWritableBuffer_offset, _SyncInMemoryWritableBuffer_initialOffset;
13
+ import { ReadBufferError, WriteBufferError } from "./buffer_sync.js";
14
+ /**
15
+ * Shared strict in-memory buffer base with common functionality.
16
+ */
17
+ export class SyncInMemoryBufferBase {
18
+ /**
19
+ * Constructs a SyncInMemoryBufferBase with the given ArrayBuffer.
20
+ * @param buf The ArrayBuffer to create the view from.
21
+ */
22
+ constructor(buf) {
23
+ /** The underlying Uint8Array view of the buffer. */
24
+ Object.defineProperty(this, "view", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: void 0
29
+ });
30
+ this.view = new Uint8Array(buf);
31
+ }
32
+ /**
33
+ * Returns the length of the buffer in bytes.
34
+ * @returns The length of the buffer.
35
+ */
36
+ length() {
37
+ return this.view.length;
38
+ }
39
+ /**
40
+ * @internal Test-only function to get the underlying ArrayBuffer.
41
+ */
42
+ _testOnlyBuffer() {
43
+ return this.view.buffer;
44
+ }
45
+ }
46
+ /**
47
+ * Strict read-only in-memory buffer for serialization reads (synchronous).
48
+ * Throws errors on all out-of-bounds operations.
49
+ *
50
+ * Key features:
51
+ * - Fixed size: The buffer size is determined at construction and cannot be resized.
52
+ * - Random access: Supports reading at arbitrary byte offsets.
53
+ * - Strict bounds checking: Operations that exceed buffer bounds throw ReadBufferError.
54
+ * - Efficient: Uses Uint8Array for fast byte-level operations.
55
+ *
56
+ * @example
57
+ * ```typescript
58
+ * const arrayBuffer = new ArrayBuffer(1024);
59
+ * const buffer = new SyncInMemoryReadableBuffer(arrayBuffer);
60
+ *
61
+ * // Read some data
62
+ * const data = buffer.read(0, 4); // Returns Uint8Array of 4 bytes
63
+ *
64
+ * // This will throw:
65
+ * buffer.read(1020, 10); // ReadBufferError: Operation exceeds buffer bounds
66
+ * ```
67
+ */
68
+ export class SyncInMemoryReadableBuffer extends SyncInMemoryBufferBase {
69
+ /**
70
+ * Checks if the offset and size are within buffer bounds.
71
+ * @param offset The starting offset.
72
+ * @param size The number of bytes to check.
73
+ */
74
+ checkBounds(offset, size) {
75
+ if (offset < 0 || size < 0) {
76
+ throw new ReadBufferError(`Offset and size must be non-negative. Got offset=${offset}, size=${size}`, offset, size, this.view.length);
77
+ }
78
+ if (offset + size > this.view.length) {
79
+ throw new ReadBufferError(`Operation exceeds buffer bounds. offset=${offset}, size=${size}, bufferLength=${this.view.length}`, offset, size, this.view.length);
80
+ }
81
+ }
82
+ /**
83
+ * Reads a portion of the buffer.
84
+ * @param offset The starting offset.
85
+ * @param size The number of bytes to read.
86
+ * @returns A Uint8Array containing the read bytes.
87
+ */
88
+ read(offset, size) {
89
+ this.checkBounds(offset, size);
90
+ return this.view.slice(offset, offset + size);
91
+ }
92
+ /**
93
+ * Checks if more data can be read starting at the given offset.
94
+ * @param offset The byte offset to check.
95
+ * @returns True if at least one byte can be read from the offset.
96
+ */
97
+ canReadMore(offset) {
98
+ try {
99
+ this.checkBounds(offset, 1);
100
+ return true;
101
+ }
102
+ catch {
103
+ return false;
104
+ }
105
+ }
106
+ /**
107
+ * @internal Test-only function to get the underlying ArrayBuffer.
108
+ */
109
+ _testOnlyBuffer() {
110
+ return this.view.buffer;
111
+ }
112
+ }
113
+ /**
114
+ * Strict write-only in-memory buffer for serialization writes (synchronous).
115
+ * Throws errors when attempting to write beyond buffer bounds.
116
+ *
117
+ * Key features:
118
+ * - Fixed size: The buffer size is determined at construction and cannot be resized.
119
+ * - Sequential writes: Maintains an internal offset that advances with each write.
120
+ * - Strict bounds checking: Throws WriteBufferError when write would exceed buffer capacity.
121
+ * - Efficient: Uses Uint8Array for fast byte-level operations.
122
+ *
123
+ * @example
124
+ * ```typescript
125
+ * const arrayBuffer = new ArrayBuffer(1024);
126
+ * const buffer = new SyncInMemoryWritableBuffer(arrayBuffer);
127
+ *
128
+ * // Write some data
129
+ * buffer.appendBytes(new Uint8Array([1, 2, 3, 4]));
130
+ *
131
+ * // This will throw if buffer is full:
132
+ * buffer.appendBytes(new Uint8Array(2000)); // WriteBufferError: Write operation exceeds buffer bounds
133
+ * ```
134
+ */
135
+ export class SyncInMemoryWritableBuffer extends SyncInMemoryBufferBase {
136
+ /**
137
+ * Creates a new writable buffer with the specified ArrayBuffer and initial offset.
138
+ * @param buf The underlying ArrayBuffer to write to.
139
+ * @param offset The starting offset within the buffer (default: 0).
140
+ */
141
+ constructor(buf, offset = 0) {
142
+ super(buf);
143
+ _SyncInMemoryWritableBuffer_offset.set(this, void 0);
144
+ _SyncInMemoryWritableBuffer_initialOffset.set(this, void 0);
145
+ if (offset < 0 || offset > this.view.length) {
146
+ throw new WriteBufferError(`Initial offset must be within buffer bounds. Got offset=${offset}, bufferLength=${this.view.length}`, offset, 0, this.view.length);
147
+ }
148
+ __classPrivateFieldSet(this, _SyncInMemoryWritableBuffer_initialOffset, offset, "f");
149
+ __classPrivateFieldSet(this, _SyncInMemoryWritableBuffer_offset, offset, "f");
150
+ }
151
+ /**
152
+ * Checks if writing the given data at the specified offset would exceed buffer bounds.
153
+ * Throws a WriteBufferError if the operation would exceed bounds.
154
+ * @param offset The offset to write at.
155
+ * @param data The data to write.
156
+ */
157
+ checkWriteBounds(offset, data) {
158
+ this.checkWriteBoundsSize(offset, data.length);
159
+ }
160
+ /**
161
+ * Checks if writing the given number of bytes at the specified offset would exceed buffer bounds.
162
+ * Throws a WriteBufferError if the operation would exceed bounds.
163
+ * @param offset The offset to write at.
164
+ * @param size The number of bytes to write.
165
+ */
166
+ checkWriteBoundsSize(offset, size) {
167
+ if (offset < 0) {
168
+ throw new WriteBufferError(`Offset must be non-negative. Got offset=${offset}`, offset, size, this.view.length);
169
+ }
170
+ if (offset + size > this.view.length) {
171
+ throw new WriteBufferError(`Write operation exceeds buffer bounds. offset=${offset}, dataSize=${size}, bufferLength=${this.view.length}`, offset, size, this.view.length);
172
+ }
173
+ }
174
+ /**
175
+ * Appends the given bytes to the buffer at the current offset and advances the offset.
176
+ * @param data The bytes to append.
177
+ */
178
+ appendBytes(data) {
179
+ this.checkWriteBounds(__classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f"), data);
180
+ if (data.length === 0) {
181
+ return;
182
+ }
183
+ this.view.set(data, __classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f"));
184
+ __classPrivateFieldSet(this, _SyncInMemoryWritableBuffer_offset, __classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f") + data.length, "f");
185
+ }
186
+ /**
187
+ * Appends a slice of bytes to the buffer at the current offset and advances the offset.
188
+ * This avoids allocating subarray views in higher-level hot paths.
189
+ */
190
+ appendBytesFrom(data, offset, length) {
191
+ if (length === 0) {
192
+ return;
193
+ }
194
+ if (!Number.isInteger(offset) || !Number.isInteger(length) ||
195
+ offset < 0 || length < 0 || offset + length > data.length) {
196
+ throw new RangeError(`Invalid source range offset=${offset} length=${length} for dataSize=${data.length}`);
197
+ }
198
+ this.checkWriteBoundsSize(__classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f"), length);
199
+ for (let i = 0; i < length; i++) {
200
+ this.view[__classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f") + i] = data[offset + i];
201
+ }
202
+ __classPrivateFieldSet(this, _SyncInMemoryWritableBuffer_offset, __classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f") + length, "f");
203
+ }
204
+ /**
205
+ * Checks if the buffer is in a valid state.
206
+ * Always returns true as this buffer throws on overflow instead of becoming invalid.
207
+ * @returns Always true.
208
+ */
209
+ isValid() {
210
+ return true; // Always valid since we throw on overflow instead of marking as invalid
211
+ }
212
+ /**
213
+ * Checks if the buffer can accept appending the given number of bytes.
214
+ * @param size The number of bytes to check.
215
+ * @returns True if the buffer can accept the append.
216
+ */
217
+ canAppendMore(_size) {
218
+ return this.isValid();
219
+ }
220
+ /**
221
+ * @internal Test-only function to get the current write offset position.
222
+ */
223
+ _testOnlyOffset() {
224
+ return __classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f");
225
+ }
226
+ /**
227
+ * @internal Test-only function to get the underlying ArrayBuffer.
228
+ */
229
+ _testOnlyBuffer() {
230
+ return this.view.buffer;
231
+ }
232
+ /**
233
+ * @internal Test-only function to get the remaining space in the buffer.
234
+ */
235
+ _testOnlyRemaining() {
236
+ return this.view.length - __classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f");
237
+ }
238
+ /**
239
+ * Gets a copy of the buffer containing only the bytes written to the buffer.
240
+ * @returns An ArrayBuffer containing only the written data.
241
+ */
242
+ getBufferCopy() {
243
+ const sliced = this.view.slice(__classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_initialOffset, "f"), __classPrivateFieldGet(this, _SyncInMemoryWritableBuffer_offset, "f"));
244
+ return sliced.buffer.slice(sliced.byteOffset, sliced.byteOffset + sliced.length);
245
+ }
246
+ /**
247
+ * @internal Test-only method to check write bounds with arbitrary offset.
248
+ */
249
+ _testCheckWriteBounds(offset, data) {
250
+ this.checkWriteBounds(offset, data);
251
+ }
252
+ /**
253
+ * @internal Test-only method to check write bounds with arbitrary offset and size.
254
+ */
255
+ _testCheckWriteBoundsSize(offset, size) {
256
+ this.checkWriteBoundsSize(offset, size);
257
+ }
258
+ }
259
+ _SyncInMemoryWritableBuffer_offset = new WeakMap(), _SyncInMemoryWritableBuffer_initialOffset = new WeakMap();
@@ -0,0 +1,45 @@
1
+ import { TapBase, type WritableTapLike } from "./tap.js";
2
+ /**
3
+ * A writable tap that counts encoded bytes without allocating buffers.
4
+ *
5
+ * Used for two-pass serialization where the first pass calculates the
6
+ * exact buffer size needed, then the second pass writes to a pre-allocated
7
+ * buffer. This avoids buffer reallocations and copies.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * // Pass 1: Calculate size
12
+ * const countingTap = new CountingWritableTap();
13
+ * await type.write(countingTap, value);
14
+ * const size = countingTap.getPos();
15
+ *
16
+ * // Pass 2: Write to exact-size buffer
17
+ * const buffer = new ArrayBuffer(size);
18
+ * const tap = new WritableTap(buffer);
19
+ * await type.write(tap, value);
20
+ * ```
21
+ */
22
+ export declare class CountingWritableTap extends TapBase implements WritableTapLike {
23
+ /** Creates a new counting tap starting at position 0. */
24
+ constructor();
25
+ /** Returns whether the tap is valid (always true for counting tap). */
26
+ isValid(): Promise<boolean>;
27
+ /** Counts a boolean value (1 byte). */
28
+ writeBoolean(_value: boolean): Promise<void>;
29
+ /** Counts an integer value using varint encoding. */
30
+ writeInt(value: number): Promise<void>;
31
+ /** Counts a long value using zigzag + varint encoding. */
32
+ writeLong(value: bigint): Promise<void>;
33
+ /** Counts a float value (4 bytes). */
34
+ writeFloat(_value: number): Promise<void>;
35
+ /** Counts a double value (8 bytes). */
36
+ writeDouble(_value: number): Promise<void>;
37
+ /** Counts a fixed-length byte sequence. */
38
+ writeFixed(buf: Uint8Array): Promise<void>;
39
+ /** Counts a length-prefixed byte sequence. */
40
+ writeBytes(buf: Uint8Array): Promise<void>;
41
+ /** Counts a length-prefixed UTF-8 string. */
42
+ writeString(str: string): Promise<void>;
43
+ /** Counts raw binary bytes without length prefix. */
44
+ writeBinary(_str: string, len: number): Promise<void>;
45
+ }