Skip to content

Commit 7237d56

Browse files
committed
Mirror new Rust API's in TypeScript
1 parent c87f356 commit 7237d56

2 files changed

Lines changed: 52 additions & 13 deletions

File tree

rust/bufferfish-core/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ use std::{
99
pub use decodable::Decodable;
1010
pub use encodable::Encodable;
1111

12-
/// Trait for types that can be created from bytes.
12+
/// Types that can be created from bytes.
1313
pub trait FromBytes: Sized {
14-
/// Create a new instance from bytes.
1514
fn from_bytes(bytes: &[u8]) -> Result<Self, BufferfishError>;
1615
}
1716

@@ -37,6 +36,7 @@ impl<T: Decodable> FromBytes for T {
3736

3837
let mut bf = Bufferfish::from(bytes);
3938
bf.start_reading();
39+
4040
T::decode(&mut bf)
4141
}
4242
}
@@ -145,7 +145,8 @@ impl Bufferfish {
145145
/// Decode a value from this `Bufferfish`.
146146
///
147147
/// This function will start reading from the beginning of the buffer.
148-
/// Performs validation to check if the buffer's content is within acceptable size limits.
148+
/// Performs validation to check if the buffer's content is within
149+
/// acceptable size limits.
149150
pub fn decode<T: Decodable>(&mut self) -> Result<T, BufferfishError> {
150151
let bytes = self.as_bytes();
151152

@@ -172,7 +173,8 @@ impl Bufferfish {
172173
}
173174

174175
/// Convert this Bufferfish into a value of type T.
175-
/// Performs validation to check if the buffer's content is within acceptable size limits.
176+
/// Performs validation to check if the buffer's content is within
177+
/// acceptable size limits.
176178
pub fn into_value<T: Decodable>(mut self) -> Result<T, BufferfishError> {
177179
let bytes = self.as_bytes();
178180

@@ -249,9 +251,11 @@ impl Bufferfish {
249251
/// Resizes the internal buffer to the given size (in bytes).
250252
/// This resets the buffer state and clears any existing data.
251253
pub fn truncate(&mut self, len: usize) {
254+
self.clear();
252255
self.inner.get_mut().truncate(len);
253256
self.inner.set_position(0);
254257
self.reading = false;
258+
self.max_capacity = len;
255259
}
256260

257261
/// Returns a `Vec<u8>` of the internal byte buffer.

typescript/src/bufferfish.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class Bufferfish {
99
private inner: Uint8Array
1010
private position: number
1111
private reading: boolean
12-
private capacity: number
12+
private max_capacity: number
1313

1414
private textDecoder: TextDecoder | undefined
1515
private textEncoder: TextEncoder | undefined
@@ -18,7 +18,7 @@ export class Bufferfish {
1818
this.inner = new Uint8Array(bf)
1919
this.position = 0
2020
this.reading = false
21-
this.capacity = 1024
21+
this.max_capacity = 1024
2222

2323
this.textDecoder = undefined
2424
this.textEncoder = undefined
@@ -32,12 +32,12 @@ export class Bufferfish {
3232
*/
3333
private write(bf: Uint8Array): number | Error {
3434
if (
35-
this.capacity > 0 &&
36-
(bf.length > this.capacity ||
37-
this.inner.length + bf.length > this.capacity)
35+
this.max_capacity > 0 &&
36+
(bf.length > this.max_capacity ||
37+
this.inner.length + bf.length > this.max_capacity)
3838
) {
3939
return new Error(
40-
`Bufferfish capacity exceeded (${this.capacity} bytes)`,
40+
`Bufferfish capacity exceeded (${this.max_capacity} bytes)`,
4141
)
4242
}
4343

@@ -80,7 +80,42 @@ export class Bufferfish {
8080
* A value of 0 will allow the buffer to grow indefinitely.
8181
*/
8282
public setMaxCapacity(capacity: number): void {
83-
this.capacity = capacity
83+
this.max_capacity = capacity
84+
}
85+
86+
/**
87+
* Returns true if the buffer is empty.
88+
*/
89+
public isEmpty = (): boolean => {
90+
return this.inner.length === 0
91+
}
92+
93+
/**
94+
* Returns the current length (in bytes) of the buffer.
95+
*/
96+
public length = (): number => {
97+
return this.inner.length
98+
}
99+
100+
/**
101+
* Clears the buffer and resets the cursor to the start position.
102+
*/
103+
public clear = (): void => {
104+
this.inner = new Uint8Array(0)
105+
this.position = 0
106+
this.reading = false
107+
}
108+
109+
/**
110+
* Resizes the internal buffer to the specified size (in bytes).
111+
* This resets the buffer state and clears any existing data.
112+
*/
113+
public truncate = (length: number): void => {
114+
this.clear()
115+
this.inner = this.inner.subarray(0, length)
116+
this.position = 0
117+
this.max_capacity = length
118+
this.reading = false
84119
}
85120

86121
/**
@@ -95,7 +130,7 @@ export class Bufferfish {
95130

96131
if (this.position >= this.inner.length || value === undefined) {
97132
return new Error(
98-
`peek of 1 byte exceeds the max capacity of ${this.capacity} bytes on this Bufferfish`,
133+
`peek of 1 byte exceeds the max capacity of ${this.max_capacity} bytes on this Bufferfish`,
99134
)
100135
}
101136

@@ -113,7 +148,7 @@ export class Bufferfish {
113148

114149
if (this.position + n > this.inner.length) {
115150
return new Error(
116-
`peek of ${n} bytes exceeds the max capacity of ${this.capacity} bytes on this Bufferfish`,
151+
`peek of ${n} bytes exceeds the max capacity of ${this.max_capacity} bytes on this Bufferfish`,
117152
)
118153
}
119154

0 commit comments

Comments
 (0)