Remove all direct usages of Buffer so that it can be decided based on platform.

This commit is contained in:
Holly Stubbs 2023-10-24 12:44:11 +01:00
parent a157685fcc
commit dd46ec825b
2 changed files with 13 additions and 12 deletions

View file

@ -23,20 +23,20 @@ export class WriterBase {
} }
public writeBuffer(buffer:Buffer) { public writeBuffer(buffer:Buffer) {
this.buffer = Buffer.concat([this.buffer, buffer], this.buffer.length + buffer.length); this.buffer = getBufferClass().concat([this.buffer, buffer], this.buffer.length + buffer.length);
return this; return this;
} }
public writeUint8Array(array:Uint8Array) { public writeUint8Array(array:Uint8Array) {
this.writeBuffer(Buffer.from(array)); this.writeBuffer(getBufferClass().from(array));
return this; return this;
} }
public writeByte(value:number) { public writeByte(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(1); const buffer = getBufferClass().alloc(1);
buffer.writeInt8(value); buffer.writeInt8(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -49,7 +49,7 @@ export class WriterBase {
public writeUByte(value:number) { public writeUByte(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(1); const buffer = getBufferClass().alloc(1);
buffer.writeUInt8(value); buffer.writeUInt8(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {

View file

@ -3,11 +3,12 @@
import { IWriter } from "./IWriter"; import { IWriter } from "./IWriter";
import { WriterBase } from "../base/WriterBase"; import { WriterBase } from "../base/WriterBase";
import { getBufferClass } from "../base/BufferShim";
export class WriterLE extends WriterBase implements IWriter { export class WriterLE extends WriterBase implements IWriter {
public writeShort(value:number) { public writeShort(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(2); const buffer = getBufferClass().alloc(2);
buffer.writeInt16LE(value); buffer.writeInt16LE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -20,7 +21,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeUShort(value:number) { public writeUShort(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(2); const buffer = getBufferClass().alloc(2);
buffer.writeUInt16LE(value); buffer.writeUInt16LE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -33,7 +34,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeInt(value:number) { public writeInt(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(4); const buffer = getBufferClass().alloc(4);
buffer.writeInt32LE(value); buffer.writeInt32LE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -45,7 +46,7 @@ export class WriterLE extends WriterBase implements IWriter {
} }
public writeUInt(value:number) { public writeUInt(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(4); const buffer = getBufferClass().alloc(4);
buffer.writeUInt32LE(value); buffer.writeUInt32LE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -62,7 +63,7 @@ export class WriterLE extends WriterBase implements IWriter {
} }
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(8); const buffer = getBufferClass().alloc(8);
buffer.writeBigInt64LE(value); buffer.writeBigInt64LE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -79,7 +80,7 @@ export class WriterLE extends WriterBase implements IWriter {
} }
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(8); const buffer = getBufferClass().alloc(8);
buffer.writeBigUint64LE(value); buffer.writeBigUint64LE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -92,7 +93,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeFloat(value:number) { public writeFloat(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(4); const buffer = getBufferClass().alloc(4);
buffer.writeFloatLE(value); buffer.writeFloatLE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {
@ -105,7 +106,7 @@ export class WriterLE extends WriterBase implements IWriter {
public writeDouble(value:number) { public writeDouble(value:number) {
if (this.resizable) { if (this.resizable) {
const buffer = Buffer.alloc(8); const buffer = getBufferClass().alloc(8);
buffer.writeDoubleLE(value); buffer.writeDoubleLE(value);
this.writeBuffer(buffer); this.writeBuffer(buffer);
} else { } else {