Skip to content

Commit 1b2958e

Browse files
committed
updates
1 parent 501e478 commit 1b2958e

5 files changed

Lines changed: 35 additions & 42 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const stream = new BinaryStream();
6767
stream.writeUInt8(255);
6868
stream.writeInt16(-32768);
6969
stream.writeFloat32(3.14);
70-
stream.writeString("Hello, BinaryStream!");
70+
stream.write(new TextEncoder().encode("Hello, BinaryStream!"));
7171

7272
// Reset the position to the beginning of the stream for reading
7373
stream.flip();
@@ -76,12 +76,12 @@ stream.flip();
7676
const uint8 = stream.readUInt8();
7777
const int16 = stream.readInt16();
7878
const float32 = stream.readFloat32();
79-
const str = stream.readString();
79+
const str = stream.readRest();
8080

8181
console.log(uint8); // 255
8282
console.log(int16); // -32768
8383
console.log(float32); // 3.14
84-
console.log(str); // Hello, BinaryStream!
84+
console.log(new TextDecoder().decode(str)); // Hello, BinaryStream!
8585
console.log(stream.buffer); // Uint8Array (31) [255, 128, 0, 64, 72, 245, 195, 0, 0, 0, 20, 72, 101, 108, 108, 111, 44, 32, 66, 105, 110, 97, 114, 121, 83, 116, 114, 101, 97, 109, 33]
8686
```
8787

bun.lockb

1.71 KB
Binary file not shown.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
"url": "git+https://github.com/pocketnode/binarystream.git"
2727
},
2828
"devDependencies": {
29-
"typescript": "^5.0.0",
30-
"bun-plugin-dts": "^0.2.3",
31-
"bun-types": "latest",
29+
"typescript": "^5.9.3",
30+
"bun-plugin-dts": "^0.2.4",
31+
"@types/bun": "latest",
32+
"@types/node": "latest",
3233
"type-doc": "^0.1.41",
33-
"typedoc-material-theme": "^1.0.2",
34-
"typedoc-plugin-mdn-links": "^3.1.28"
34+
"typedoc-material-theme": "^1.4.1",
35+
"typedoc-plugin-mdn-links": "^3.3.8"
3536
}
3637
}

src/BinaryStream.ts

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class BinaryStream {
3737
if (buffer instanceof ArrayBuffer) {
3838
this.view = new DataView(buffer);
3939
} else if (buffer instanceof Uint8Array) {
40-
this.view = new DataView(buffer.buffer);
40+
this.view = new DataView(buffer.buffer as ArrayBuffer);
4141
} else {
4242
this.view = new DataView(new Uint8Array(buffer).buffer);
4343
}
@@ -50,7 +50,7 @@ class BinaryStream {
5050
* @returns The DataView object.
5151
*/
5252
getDataView(): DataView {
53-
return this.view;
53+
return this.view as DataView;
5454
}
5555

5656
/**
@@ -60,8 +60,8 @@ class BinaryStream {
6060
* @returns The updated BinaryStream instance.
6161
*/
6262
resize(size: number, canShrink: boolean = true): this {
63-
if (canShrink || (!canShrink && size > this.view.buffer.byteLength)) {
64-
this.view = new DataView(this.view.buffer.transfer(size));
63+
if (canShrink || (!canShrink && size > this.length)) {
64+
this.view = new DataView(this.arrayBuffer.transfer(size));
6565
}
6666
return this;
6767
}
@@ -111,7 +111,7 @@ class BinaryStream {
111111
throw new RangeError(`Buffer overrun, cannot read past the end of the buffer. Start: ${start}, End: ${end}, Length: ${this.length}`);
112112
}
113113

114-
return new Uint8Array(this.view.buffer.slice(start, end));
114+
return new Uint8Array(this.arrayBuffer.slice(start, end));
115115
}
116116

117117
/**
@@ -168,7 +168,7 @@ class BinaryStream {
168168
* @returns A copy of the ArrayBuffer.
169169
*/
170170
get buffer(): Uint8Array {
171-
return new Uint8Array(this.view.buffer);
171+
return new Uint8Array(this.arrayBuffer);
172172
}
173173

174174
/**
@@ -180,7 +180,7 @@ class BinaryStream {
180180
}
181181

182182
get arrayBuffer(): ArrayBuffer {
183-
return this.view.buffer;
183+
return this.view.buffer as ArrayBuffer;
184184
}
185185

186186
/**
@@ -189,7 +189,7 @@ class BinaryStream {
189189
* @returns The created Blob object.
190190
*/
191191
blob(type: string = "application/octet-stream"): Blob {
192-
return new Blob([this.view.buffer], { type });
192+
return new Blob([this.arrayBuffer], { type });
193193
}
194194

195195
/**
@@ -199,6 +199,8 @@ class BinaryStream {
199199
getRemainingBytes(): number {
200200
return this.length - this.offset;
201201
}
202+
/** @inheritdoc BinaryStream.getRemainingBytes */
203+
rest = this.getRemainingBytes;
202204

203205
/**
204206
* Reads the remaining amount of bytes.
@@ -207,6 +209,8 @@ class BinaryStream {
207209
readRemainingBytes(): Uint8Array {
208210
return this.read(this.getRemainingBytes());
209211
}
212+
/** @inheritdoc BinaryStream.readRemainingBytes */
213+
readRest = this.readRemainingBytes;
210214

211215
/**
212216
* Reads a boolean value.
@@ -818,15 +822,19 @@ class BinaryStream {
818822
* @param offset - The optional offset at which to write the value.
819823
* @returns The updated BinaryStream instance.
820824
*/
821-
writeUVarInt(v: number, offset: number = this.offset): this {
822-
for (let i = 0; i < 5; i++) {
823-
if ((v >> 7) !== 0) {
824-
this.writeByte(v | 0x80, offset++);
825-
} else {
826-
this.writeByte(v & 0x7f, offset++);
825+
writeUVarInt(v: number, offset: number = -1): this {
826+
if(offset >= 0){
827+
this.offset = offset;
828+
}
829+
830+
while (true) {
831+
if ((v & ~0x7f) == 0) {
832+
this.writeByte(v);
827833
break;
828834
}
829-
v >>= 7;
835+
836+
this.writeByte((v & 0x7f) | 0x80);
837+
v >>>= 7;
830838
}
831839

832840
return this;
@@ -997,7 +1005,7 @@ class BinaryStream {
9971005

9981006
default:
9991007
if (encoding === "binary") encoding = "latin1";
1000-
return new TextDecoder(encoding).decode(this.view.buffer);
1008+
return new TextDecoder(encoding).decode(this.arrayBuffer);
10011009
}
10021010

10031011
}
@@ -1010,7 +1018,7 @@ class BinaryStream {
10101018
split(bytes: number): Uint8Array[] {
10111019
let buffers: ArrayBuffer[] = [];
10121020
for (let i = 0; i < this.length; i += bytes) {
1013-
buffers.push(this.view.buffer.slice(i, bytes));
1021+
buffers.push(this.arrayBuffer.slice(i, i + bytes));
10141022
}
10151023
return buffers.map(buffer => new Uint8Array(buffer));
10161024
}
@@ -1050,22 +1058,6 @@ class BinaryStream {
10501058
throw new Error(`Unsupported encoding: ${encoding}`);
10511059
}
10521060
}
1053-
1054-
/**
1055-
* Writes a string to the buffer
1056-
*/
1057-
writeString(str: string, offset?: number): this {
1058-
let buffer = new TextEncoder().encode(str);
1059-
return this.writeUInt32BE(buffer.length, offset).write(buffer);
1060-
}
1061-
1062-
/**
1063-
* Reads a string from the buffer
1064-
*/
1065-
readString(offset?: number): string {
1066-
let length = this.readUInt32BE(offset);
1067-
return new TextDecoder().decode(this.read(length));
1068-
}
10691061
}
10701062

10711063
export {

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"compilerOptions": {
33
// Enable latest features
44
"lib": ["ESNext", "DOM"],
5-
"types": ["bun-types"],
5+
"types": ["@types/bun", "@types/node"],
66
"target": "ESNext",
77
"module": "ESNext",
88
"moduleDetection": "force",

0 commit comments

Comments
 (0)