diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index 11ee8c5..19a28b2 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -23,6 +23,12 @@ export class Buffer extends Uint8Array { return result; } + writeUInt8(value: u8, offset: i32 = 0): i32 { + if(offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE); + store(this.dataStart + offset, value); + return offset + 1; + } + writeInt8(value: i8, offset: i32 = 0): i32 { if(offset > this.dataLength) throw new RangeError(E_INDEXOUTOFRANGE); store(this.dataStart + offset, value); diff --git a/assembly/node.d.ts b/assembly/node.d.ts index 1e4f387..1fc97e4 100644 --- a/assembly/node.d.ts +++ b/assembly/node.d.ts @@ -3,6 +3,8 @@ declare class Buffer extends Uint8Array { static alloc(size: i32): Buffer; /** This method allocates a new Buffer of indicated size. This is unsafe because the data is not zeroed. */ static allocUnsafe(size: i32): Buffer; + /** Writes an inputted u8 value to the buffer, at the desired offset. */ + writeUInt8(value:u8, offset?:i32): i32; /** Writes an inputted value to the buffer, at the desired offset. */ writeInt8(value:i8, offset?:i32): i32; /** Reads a signed integer at the designated offset. */ diff --git a/tests/buffer.spec.ts b/tests/buffer.spec.ts index eb0d0b1..89dfbba 100644 --- a/tests/buffer.spec.ts +++ b/tests/buffer.spec.ts @@ -43,6 +43,14 @@ describe("buffer", () => { // TODO: expectFn(() => { Buffer.allocUnsafe(BLOCK_MAXSIZE + 1); }).toThrow(); }); + test("#writeUInt8", () => { + let buff = new Buffer(5); + expect(buff.writeUInt8(4)).toBe(1); + expect(buff.writeUInt8(252,4)).toBe(5); + expect(buff[0]).toBe(4); + expect(buff[4]).toBe(252); + }); + test("#writeInt8", () => { let buff = new Buffer(5); expect(buff.writeInt8(9)).toBe(1); @@ -65,5 +73,4 @@ describe("buffer", () => { // newBuff.readInt8(5); // }).toThrow(); }) - });