Skip to content

Commit bf60e27

Browse files
authored
v1.8.7 (#21)
1 parent 3e1888d commit bf60e27

File tree

9 files changed

+173
-297
lines changed

9 files changed

+173
-297
lines changed

dist/index.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/index.cjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/index.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 104 additions & 271 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tinybuf",
3-
"version": "1.8.6",
3+
"version": "1.8.7",
44
"author": "Reece Como <reece.como@gmail.com>",
55
"authors": [
66
"Reece Como <reece.como@gmail.com>",

src/__tests__/BufferWriter.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,30 @@ describe("BufferWriter", () => {
2121
const text = new TextDecoder("utf-8").decode(writer.$copyBytes());
2222
expect(text).toBe(input);
2323
});
24+
25+
it("should resize until the limit", () => {
26+
const writer = new BufferWriter(256);
27+
28+
// cheeky check of the underlying implementation
29+
expect((writer as any)._$dataView.byteOffset).toBe(0);
30+
expect((writer as any)._$dataView.byteLength).toBe(256);
31+
32+
const textA = "a".repeat(1200);
33+
const textBufferA = $utf8encode(textA);
34+
writer.$writeBytes(textBufferA);
35+
36+
expect((writer as any)._$dataView.byteOffset).toBe(0);
37+
expect((writer as any)._$dataView.byteLength).toBe(1280);
38+
39+
const textB = "b".repeat(100);
40+
const textBufferB = $utf8encode(textB);
41+
writer.$writeBytes(textBufferB);
42+
43+
// caps resize to encodingBufferMaxSize
44+
expect((writer as any)._$dataView.byteOffset).toBe(0);
45+
expect((writer as any)._$dataView.byteLength).toBe(1500);
46+
47+
const text = new TextDecoder("utf-8").decode(writer.$copyBytes());
48+
expect(text).toBe(textA + textB);
49+
});
2450
});

src/core/BufferFormat.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ export class BufferFormat<EncoderType extends EncoderDefinition, HeaderType exte
186186
private static _$initWriter(): BufferWriter {
187187
if (cfg.useGlobalEncodingBuffer) {
188188
if (!BufferFormat._$globalWriter) {
189-
// lazy init: global encoding buffer created at max size
190-
this._$globalWriter = new BufferWriter(cfg.encodingBufferInitialSize);
189+
this._$globalWriter = new BufferWriter(cfg.encodingBufferMaxSize);
191190
}
192191

193192
return this._$globalWriter;

src/core/lib/BufferWriter.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,40 @@ export class BufferWriter {
3333
// ----- Writers: -----
3434

3535
public $writeInt8(value: number): void {
36-
this._$alloc(1).setInt8(this._$writeHead, value);
36+
this._$pre(1).setInt8(this._$writeHead, value);
3737
}
3838

3939
public $writeInt16(value: number): void {
40-
this._$alloc(2).setInt16(this._$writeHead, value, true);
40+
this._$pre(2).setInt16(this._$writeHead, value, true);
4141
}
4242

4343
public $writeInt32(value: number): void {
44-
this._$alloc(4).setInt32(this._$writeHead, value, true);
44+
this._$pre(4).setInt32(this._$writeHead, value, true);
4545
}
4646

4747
public $writeUint8(value: number): void {
48-
this._$alloc(1).setUint8(this._$writeHead, value);
48+
this._$pre(1).setUint8(this._$writeHead, value);
4949
}
5050

5151
public $writeUint16(value: number): void {
52-
this._$alloc(2).setUint16(this._$writeHead, value, false); // big-endian for varint
52+
this._$pre(2).setUint16(this._$writeHead, value, false); // big-endian for varint
5353
}
5454

5555
public $writeUint32(value: number): void {
56-
this._$alloc(4).setUint32(this._$writeHead, value, false); // big-endian for varint
56+
this._$pre(4).setUint32(this._$writeHead, value, false); // big-endian for varint
5757
}
5858

5959
public $writeFloat32(value: number): void {
60-
this._$alloc(4).setFloat32(this._$writeHead, value, true);
60+
this._$pre(4).setFloat32(this._$writeHead, value, true);
6161
}
6262

6363
public $writeFloat64(value: number): void {
64-
this._$alloc(8).setFloat64(this._$writeHead, value, true);
64+
this._$pre(8).setFloat64(this._$writeHead, value, true);
6565
}
6666

6767
public $writeBytes(b: Uint8Array | ArrayBuffer | ArrayBufferView): void {
6868
// allocate bytes first
69-
this._$alloc(b.byteLength);
69+
this._$pre(b.byteLength);
7070

7171
let bBytes: Uint8Array = ArrayBuffer.isView(b)
7272
? b instanceof Uint8Array
@@ -84,12 +84,15 @@ export class BufferWriter {
8484

8585
// ----- Private methods: -----
8686

87-
private _$alloc(bytes: number): DataView {
87+
/**
88+
* Pre-allocate some bytes on the dataview, moving the write head into
89+
* position.
90+
*
91+
* @throws TinybufError
92+
*/
93+
private _$pre(bytes: number): DataView {
8894
if (this.$byteLength + bytes > this._$dataView.byteLength) {
89-
const minBytesNeeded = this.$byteLength + bytes - this._$dataView.byteLength;
90-
const requestedNewBytes = Math.ceil(minBytesNeeded / cfg.encodingBufferIncrement) * cfg.encodingBufferIncrement;
91-
if (!this._$resizable) throw new TinybufError("exceeded buffer length: " + this._$dataView.byteLength);
92-
this._$resizeBuffer(this._$dataView.byteLength + requestedNewBytes);
95+
this._$malloc(bytes);
9396
}
9497

9598
this._$writeHead = this.$byteLength;
@@ -98,14 +101,29 @@ export class BufferWriter {
98101
return this._$dataView;
99102
}
100103

101-
private _$resizeBuffer(newSize: number): void {
102-
if (newSize > cfg.encodingBufferMaxSize) {
103-
// safety check
104-
throw new TinybufError(`exceeded encodingBufferMaxSize: ${cfg.encodingBufferMaxSize}`);
104+
/**
105+
* @throws TinybufError
106+
*/
107+
private _$malloc(bytes: number): void {
108+
if (!this._$resizable) {
109+
throw new TinybufError("exceeded buffer length: " + this._$dataView.byteLength);
105110
}
106111

112+
const currentBytes = this._$dataView.byteLength;
113+
const minNewBytes = this.$byteLength + bytes - currentBytes;
114+
const availableBytes = cfg.encodingBufferMaxSize - currentBytes;
115+
116+
if (minNewBytes > availableBytes) {
117+
throw new TinybufError("exceeded encodingBufferMaxSize: " + cfg.encodingBufferMaxSize);
118+
}
119+
120+
const increment = cfg.encodingBufferIncrement;
121+
const newBytes = Math.ceil(minNewBytes / increment) * increment;
122+
const newSize = currentBytes + Math.min(newBytes, availableBytes);
107123
const buf = new Uint8Array(newSize);
108-
buf.set(this._$bytes); // copy bytes
124+
125+
// copy bytes
126+
buf.set(this._$bytes);
109127

110128
// update refs
111129
this._$dataView = new DataView(buf.buffer);

0 commit comments

Comments
 (0)