|
| 1 | +/* eslint-disable @typescript-eslint/unified-signatures */ |
1 | 2 | import { writers, readers } from "./lib/coders"; |
2 | 3 | import * as coders from "./lib/coders"; |
3 | 4 | import { $hashCode, $strToHashCode } from "./lib/hashCode"; |
@@ -104,10 +105,6 @@ export class BufferFormat<EncoderType extends EncoderDefinition, HeaderType exte |
104 | 105 | /** @internal */ |
105 | 106 | private _$writer?: BufferWriter; |
106 | 107 |
|
107 | | - public get encodingBuffer(): DataView | undefined { |
108 | | - return this._$writer?._$dataView; |
109 | | - } |
110 | | - |
111 | 108 | public constructor( |
112 | 109 | def: EncoderType, |
113 | 110 | header?: HeaderType | null, |
@@ -199,6 +196,27 @@ export class BufferFormat<EncoderType extends EncoderDefinition, HeaderType exte |
199 | 196 | return new BufferWriter(cfg.encodingBufferInitialSize); |
200 | 197 | } |
201 | 198 |
|
| 199 | + /** |
| 200 | + * Encode an object into an existing byte array. |
| 201 | + * |
| 202 | + * **Warning:** Returns an unsafe view into the encoding buffer. Pass this reference to preserve |
| 203 | + * performance, and to minimize memory allocation and fragmentation. |
| 204 | + */ |
| 205 | + public encodeInto<TDecodedType extends DecodedType<EncoderType>>( |
| 206 | + data: TDecodedType, |
| 207 | + bytes: Uint8Array, |
| 208 | + ): Uint8Array { |
| 209 | + const writer = new BufferWriter(bytes); |
| 210 | + |
| 211 | + if (this._$hasValidationOrTransforms) { |
| 212 | + data = this._$preprocess(data); |
| 213 | + } |
| 214 | + |
| 215 | + this._$write(data, writer); |
| 216 | + |
| 217 | + return writer.$viewBytes(); |
| 218 | + } |
| 219 | + |
202 | 220 | /** |
203 | 221 | * Encode an object to bytes. |
204 | 222 | * |
@@ -236,13 +254,37 @@ export class BufferFormat<EncoderType extends EncoderDefinition, HeaderType exte |
236 | 254 | : this._$writer.$viewBytes(); |
237 | 255 | } |
238 | 256 |
|
| 257 | + /** |
| 258 | + * Decode binary data into an existing object instance. |
| 259 | + * @throws if fails to decode bytes to schema. |
| 260 | + */ |
| 261 | + public decodeInto<TDecodedType = DecodedType<EncoderType>>( |
| 262 | + bytes: Uint8Array | ArrayBufferView | ArrayBuffer, |
| 263 | + obj: Partial<TDecodedType>, |
| 264 | + ): TDecodedType { |
| 265 | + return this._$read(new BufferReader(bytes, this.header === undefined ? 0 : 2), obj); |
| 266 | + } |
| 267 | + /** |
| 268 | + * Decode binary data to an object. |
| 269 | + * @throws if fails to decode bytes to schema. |
| 270 | + */ |
| 271 | + public decode<TDecodedType = DecodedType<EncoderType>>( |
| 272 | + bytes: Uint8Array | ArrayBufferView | ArrayBuffer |
| 273 | + ): TDecodedType; |
| 274 | + /** |
| 275 | + * @deprecated use decodeInto() instead |
| 276 | + */ |
| 277 | + public decode<TDecodedType = DecodedType<EncoderType>>( |
| 278 | + bytes: Uint8Array | ArrayBufferView | ArrayBuffer, |
| 279 | + decodeInto: Partial<TDecodedType>, |
| 280 | + ): TDecodedType; |
239 | 281 | /** |
240 | 282 | * Decode binary data to an object. |
241 | 283 | * @throws if fails to decode bytes to schema. |
242 | 284 | */ |
243 | 285 | public decode<TDecodedType = DecodedType<EncoderType>>( |
244 | 286 | bytes: Uint8Array | ArrayBufferView | ArrayBuffer, |
245 | | - decodeInto?: Partial<TDecodedType>, |
| 287 | + decodeInto?: Partial<TDecodedType> | undefined, |
246 | 288 | ): TDecodedType { |
247 | 289 | return this._$read(new BufferReader(bytes, this.header === undefined ? 0 : 2), decodeInto); |
248 | 290 | } |
|
0 commit comments