@evolu/common 8.6.1 → 8.7.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.
@@ -1,9 +1,16 @@
1
1
  /**
2
2
  * Binary data handling and byte array utilities.
3
3
  *
4
+ * Buffer-based decoding functions intentionally throw errors instead of
5
+ * returning {@link Result}. This is a deliberate micro-optimization for Evolu
6
+ * Protocol's hot paths: Result is inexpensive, but returning decoded values
7
+ * directly avoids its success-case allocation, and using `Error` objects
8
+ * preserves stack traces. In the future, we will try returning `Error` objects
9
+ * in `Result` values to measure the real-world performance impact.
10
+ *
4
11
  * @module
5
12
  */
6
- import { NonNegativeInt } from "./Type.ts";
13
+ import type { JsonValue, NonNegativeInt } from "./Type.ts";
7
14
  export { bytesToHex, bytesToUtf8, concatBytes, hexToBytes, utf8ToBytes, } from "@noble/ciphers/utils.js";
8
15
  /**
9
16
  * Custom error for {@link Buffer}-related failures like premature end of data.
@@ -26,12 +33,7 @@ export declare class BufferError extends Error {
26
33
  * reused within functions by leveraging `reset` to clear contents while
27
34
  * preserving capacity, or `truncate` to adjust the length to a specific size,
28
35
  * reducing the need for new allocations. Pass Buffers to `encode*` functions to
29
- * append serialized data and use `decode*` functions to extract data. Both
30
- * `shift` and `shiftN` throw an {@link BufferError} with message "Buffer parse
31
- * ended prematurely" on failure, as do higher-level `decode*` functions,
32
- * providing stack traces for debugging instead of using {@link Result}. This
33
- * avoids allocation overhead in success cases and leverages exceptions'
34
- * diagnostic benefits.
36
+ * append serialized data and use `decode*` functions to extract data.
35
37
  *
36
38
  * ### Example
37
39
  *
@@ -84,7 +86,7 @@ export interface Buffer {
84
86
  getLength: () => NonNegativeInt;
85
87
  /**
86
88
  * Appends binary data to the buffer, resizing if necessary. Throws if
87
- * `arg.length` is not a non-negative integer.
89
+ * `arg.length` is not a non-negative safe integer.
88
90
  */
89
91
  extend: (arg: Uint8Array | ArrayLike<number>) => void;
90
92
  /**
@@ -119,4 +121,61 @@ export interface Buffer {
119
121
  }
120
122
  /** Creates a {@link Buffer} for efficient byte operations. */
121
123
  export declare const createBuffer: (arrayLike?: Uint8Array | ArrayLike<number>) => Buffer;
124
+ /**
125
+ * Encodes a {@link JsonValue} using the MessagePack format.
126
+ *
127
+ * ### Example
128
+ *
129
+ * ```ts
130
+ * import {
131
+ * assertEqual,
132
+ * createBuffer,
133
+ * encodeJsonValue,
134
+ * JsonValue,
135
+ * } from "@evolu/common";
136
+ *
137
+ * const buffer = createBuffer();
138
+ * const value = JsonValue.orThrow({ name: "Ada" });
139
+ *
140
+ * encodeJsonValue(buffer, value);
141
+ *
142
+ * assertEqual(
143
+ * buffer.unwrap(),
144
+ * new Uint8Array([
145
+ * 0x81, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa3, 0x41, 0x64, 0x61,
146
+ * ]),
147
+ * );
148
+ * ```
149
+ *
150
+ * Encoding is artificially limited to 1,000 nested arrays or objects to keep
151
+ * recursive encoding and decoding safe and symmetric. JSON data should not
152
+ * require such depth; flatten or split deeply nested data, or use a
153
+ * purpose-built serialization format.
154
+ */
155
+ export declare const encodeJsonValue: (buffer: Buffer, value: JsonValue) => void;
156
+ /**
157
+ * Decodes a {@link JsonValue} using the MessagePack format.
158
+ *
159
+ * Throws a {@link BufferError} without modifying the Buffer if the encoded value
160
+ * is malformed, truncated, unsupported, outside the JsonValue domain, or
161
+ * exceeds 1,000 nested arrays or objects.
162
+ *
163
+ * ### Example
164
+ *
165
+ * ```ts
166
+ * import {
167
+ * assertEqual,
168
+ * createBuffer,
169
+ * decodeJsonValue,
170
+ * } from "@evolu/common";
171
+ *
172
+ * const buffer = createBuffer([
173
+ * 0x81, 0xa4, 0x6e, 0x61, 0x6d, 0x65, 0xa3, 0x41, 0x64, 0x61,
174
+ * ]);
175
+ *
176
+ * assertEqual(decodeJsonValue(buffer), { name: "Ada" });
177
+ * assertEqual(buffer.unwrap(), new Uint8Array());
178
+ * ```
179
+ */
180
+ export declare const decodeJsonValue: (buffer: Buffer) => JsonValue;
122
181
  //# sourceMappingURL=Buffer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../../src/Buffer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,cAAc,EAAsB,MAAM,WAAW,CAAC;AAC/D,OAAO,EACL,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,WAAW,GACZ,MAAM,yBAAyB,CAAC;AAEjC;;;GAGG;AACH,qBAAa,WAAY,SAAQ,KAAK;IACpC,YAAY,OAAO,EAAE,MAAM,EAK1B;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,MAAM,WAAW,MAAM;IACrB,kDAAkD;IAClD,WAAW,EAAE,MAAM,cAAc,CAAC;IAElC,gEAAgE;IAChE,SAAS,EAAE,MAAM,cAAc,CAAC;IAEhC;;;OAGG;IACH,MAAM,EAAE,CAAC,GAAG,EAAE,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KAAK,IAAI,CAAC;IAEtD;;;OAGG;IACH,KAAK,EAAE,MAAM,cAAc,CAAC;IAE5B;;;OAGG;IACH,MAAM,EAAE,CAAC,CAAC,EAAE,cAAc,KAAK,UAAU,CAAC;IAE1C;;;OAGG;IACH,QAAQ,EAAE,CAAC,MAAM,EAAE,cAAc,KAAK,IAAI,CAAC;IAE3C;;;;;;OAMG;IACH,KAAK,EAAE,MAAM,IAAI,CAAC;IAElB;;;;OAIG;IACH,MAAM,EAAE,MAAM,UAAU,CAAC;CAC1B;AAED,8DAA8D;AAC9D,eAAO,MAAM,YAAY,eACX,UAAU,GAAG,SAAS,CAAC,MAAM,CAAC,KACzC,MA4DF,CAAC"}
1
+ {"version":3,"file":"Buffer.d.ts","sourceRoot":"","sources":["../../src/Buffer.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AAGH,OAAO,KAAK,E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