Skip to content

Commit 471f7a7

Browse files
committed
feat: initiaize with Buffer or Uint8Array.
1 parent 9de4e50 commit 471f7a7

File tree

3 files changed

+25
-20
lines changed

3 files changed

+25
-20
lines changed

src/dynamicBuffer.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class DynamicBuffer {
129129
*
130130
* @param data Initial data in the buffer.
131131
*/
132-
constructor(data: string);
132+
constructor(data: string | Buffer | Uint8Array);
133133

134134
/**
135135
* Create a DynamicBuffer with initial data and the specific settings.
@@ -147,16 +147,16 @@ export class DynamicBuffer {
147147
* @param data Initial data in the buffer.
148148
* @param options Buffer settings.
149149
*/
150-
constructor(data: string, options: DynamicBufferOptions);
150+
constructor(data: string | Buffer | Uint8Array, options: DynamicBufferOptions);
151151

152152
constructor(
153-
data?: string | DynamicBufferOptions,
153+
data?: string | Buffer | Uint8Array | DynamicBufferOptions,
154154
options?: DynamicBufferOptions,
155155
) {
156-
let initData: string | undefined;
156+
let initData: string | Buffer | Uint8Array | undefined;
157157
let initOptions: DynamicBufferOptions | undefined;
158158

159-
if (typeof data === 'string') {
159+
if (typeof data === 'string' || (typeof data === 'object' && (data instanceof Buffer || data instanceof Uint8Array))) {
160160
initData = data;
161161
initOptions = options;
162162
} else {
@@ -191,7 +191,12 @@ export class DynamicBuffer {
191191
}
192192

193193
if (initData) {
194-
this.append(initData);
194+
if (typeof initData === 'string') {
195+
this.append(initData);
196+
} else {
197+
this.buffer?.set(initData);
198+
this.used = initData.length;
199+
}
195200
}
196201

197202
// eslint-disable-next-line no-constructor-return

test/dynamicBuffer.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ describe('Initialization tests', () => {
4747
assert.equal(buffer.toString(), data);
4848
});
4949

50+
it('Test initializing with a buffer', () => {
51+
const buffer = new DynamicBuffer(Buffer.from([72, 101, 108, 108, 111]));
52+
53+
assert.equal(buffer.toString(), 'Hello');
54+
});
55+
56+
it('Test initializing with an Uint8Array', () => {
57+
const buffer = new DynamicBuffer(new Uint8Array([72, 101, 108, 108, 111]));
58+
59+
assert.equal(buffer.toString(), 'Hello');
60+
});
61+
5062
it('Test initializing with initial data and initial size', () => {
5163
const data = 'Hello world';
5264
const buffer = new DynamicBuffer(data, { size: 4 });

test/read.spec.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,7 @@ describe('At tests', () => {
6666

6767
describe('More read test', () => {
6868
it('Test read big int from buffer', () => {
69-
const buf = new DynamicBuffer();
70-
71-
buf[4] = 0xff;
72-
buf[5] = 0xff;
73-
buf[6] = 0xff;
74-
buf[7] = 0xff;
75-
// 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
69+
const buf = new DynamicBuffer(new Uint8Array([0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff]));
7670

7771
assert.equal(buf.readBigInt64BE(), 4294967295n);
7872
assert.equal(buf.readBigInt64LE(), -4294967296n);
@@ -94,13 +88,7 @@ describe('More read test', () => {
9488
});
9589

9690
it('Test read int from buffer', () => {
97-
const buf = new DynamicBuffer();
98-
buf[0] = 0x12;
99-
buf[1] = 0x34;
100-
buf[2] = 0x56;
101-
buf[3] = 0x78;
102-
buf[4] = 0x90;
103-
buf[5] = 0xab;
91+
const buf = new DynamicBuffer(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x90, 0xab]));
10492

10593
const be = [0x12, 0x1234, 0x123456, 0x12345678, 0x1234567890, 0x1234567890ab];
10694
const le = [0x12, 0x3412, 0x563412, 0x78563412, 0x9078563412, 0xab9078563412];

0 commit comments

Comments
 (0)