diff options
Diffstat (limited to 'std/node/buffer.ts')
-rw-r--r-- | std/node/buffer.ts | 112 |
1 files changed, 59 insertions, 53 deletions
diff --git a/std/node/buffer.ts b/std/node/buffer.ts index 9c8d8784c..7656803d9 100644 --- a/std/node/buffer.ts +++ b/std/node/buffer.ts @@ -18,8 +18,9 @@ function checkEncoding(encoding = "utf8", strict = true): string { const normalized = normalizeEncoding(encoding); - if (normalized === undefined) + if (normalized === undefined) { throw new TypeError(`Unkown encoding: ${encoding}`); + } if (notImplementedEncodings.includes(encoding)) { notImplemented(`"${encoding}" encoding`); @@ -78,11 +79,11 @@ export default class Buffer extends Uint8Array { static alloc( size: number, fill?: number | string | Uint8Array | Buffer, - encoding = "utf8" + encoding = "utf8", ): Buffer { if (typeof size !== "number") { throw new TypeError( - `The "size" argument must be of type number. Received type ${typeof size}` + `The "size" argument must be of type number. Received type ${typeof size}`, ); } @@ -92,15 +93,17 @@ export default class Buffer extends Uint8Array { let bufFill; if (typeof fill === "string") { encoding = checkEncoding(encoding); - if (typeof fill === "string" && fill.length === 1 && encoding === "utf8") + if ( + typeof fill === "string" && fill.length === 1 && encoding === "utf8" + ) { buf.fill(fill.charCodeAt(0)); - else bufFill = Buffer.from(fill, encoding); + } else bufFill = Buffer.from(fill, encoding); } else if (typeof fill === "number") { buf.fill(fill); } else if (fill instanceof Uint8Array) { if (fill.length === 0) { throw new TypeError( - `The argument "value" is invalid. Received ${fill.constructor.name} []` + `The argument "value" is invalid. Received ${fill.constructor.name} []`, ); } @@ -108,8 +111,9 @@ export default class Buffer extends Uint8Array { } if (bufFill) { - if (bufFill.length > buf.length) + if (bufFill.length > buf.length) { bufFill = bufFill.subarray(0, buf.length); + } let offset = 0; while (offset < size) { @@ -136,7 +140,7 @@ export default class Buffer extends Uint8Array { */ static byteLength( string: string | Buffer | ArrayBufferView | ArrayBuffer | SharedArrayBuffer, - encoding = "utf8" + encoding = "utf8", ): number { if (typeof string != "string") return string.byteLength; @@ -180,7 +184,7 @@ export default class Buffer extends Uint8Array { static from( arrayBuffer: ArrayBuffer | SharedArrayBuffer, byteOffset?: number, - length?: number + length?: number, ): Buffer; /** * Copies the passed buffer data onto a new Buffer instance. @@ -194,12 +198,14 @@ export default class Buffer extends Uint8Array { // eslint-disable-next-line @typescript-eslint/no-explicit-any value: any, offsetOrEncoding?: number | string, - length?: number + length?: number, ): Buffer { - const offset = - typeof offsetOrEncoding === "string" ? undefined : offsetOrEncoding; - let encoding = - typeof offsetOrEncoding === "string" ? offsetOrEncoding : undefined; + const offset = typeof offsetOrEncoding === "string" + ? undefined + : offsetOrEncoding; + let encoding = typeof offsetOrEncoding === "string" + ? offsetOrEncoding + : undefined; if (typeof value == "string") { encoding = checkEncoding(encoding, false); @@ -236,7 +242,7 @@ export default class Buffer extends Uint8Array { targetBuffer: Buffer | Uint8Array, targetStart = 0, sourceStart = 0, - sourceEnd = this.length + sourceEnd = this.length, ): number { const sourceBuffer = this.subarray(sourceStart, sourceEnd); targetBuffer.set(sourceBuffer, targetStart); @@ -249,7 +255,7 @@ export default class Buffer extends Uint8Array { equals(otherBuffer: Uint8Array | Buffer): boolean { if (!(otherBuffer instanceof Uint8Array)) { throw new TypeError( - `The "otherBuffer" argument must be an instance of Buffer or Uint8Array. Received type ${typeof otherBuffer}` + `The "otherBuffer" argument must be an instance of Buffer or Uint8Array. Received type ${typeof otherBuffer}`, ); } @@ -267,14 +273,14 @@ export default class Buffer extends Uint8Array { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getBigInt64(offset); } readBigInt64LE(offset = 0): bigint { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getBigInt64(offset, true); } @@ -282,14 +288,14 @@ export default class Buffer extends Uint8Array { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getBigUint64(offset); } readBigUInt64LE(offset = 0): bigint { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getBigUint64(offset, true); } @@ -297,14 +303,14 @@ export default class Buffer extends Uint8Array { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getFloat64(offset); } readDoubleLE(offset = 0): number { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getFloat64(offset, true); } @@ -312,50 +318,50 @@ export default class Buffer extends Uint8Array { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getFloat32(offset); } readFloatLE(offset = 0): number { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getFloat32(offset, true); } readInt8(offset = 0): number { return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt8( - offset + offset, ); } readInt16BE(offset = 0): number { return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16( - offset + offset, ); } readInt16LE(offset = 0): number { return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16( offset, - true + true, ); } readInt32BE(offset = 0): number { return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32( - offset + offset, ); } readInt32LE(offset = 0): number { return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32( offset, - true + true, ); } readUInt8(offset = 0): number { return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint8( - offset + offset, ); } @@ -363,14 +369,14 @@ export default class Buffer extends Uint8Array { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getUint16(offset); } readUInt16LE(offset = 0): number { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getUint16(offset, true); } @@ -378,14 +384,14 @@ export default class Buffer extends Uint8Array { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getUint32(offset); } readUInt32LE(offset = 0): number { return new DataView( this.buffer, this.byteOffset, - this.byteLength + this.byteLength, ).getUint32(offset, true); } @@ -429,14 +435,14 @@ export default class Buffer extends Uint8Array { write(string: string, offset = 0, length = this.length): number { return new TextEncoder().encodeInto( string, - this.subarray(offset, offset + length) + this.subarray(offset, offset + length), ).written; } writeBigInt64BE(value: bigint, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64( offset, - value + value, ); return offset + 4; } @@ -444,7 +450,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64( offset, value, - true + true, ); return offset + 4; } @@ -452,7 +458,7 @@ export default class Buffer extends Uint8Array { writeBigUInt64BE(value: bigint, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64( offset, - value + value, ); return offset + 4; } @@ -460,7 +466,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64( offset, value, - true + true, ); return offset + 4; } @@ -468,7 +474,7 @@ export default class Buffer extends Uint8Array { writeDoubleBE(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64( offset, - value + value, ); return offset + 8; } @@ -476,7 +482,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64( offset, value, - true + true, ); return offset + 8; } @@ -484,7 +490,7 @@ export default class Buffer extends Uint8Array { writeFloatBE(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32( offset, - value + value, ); return offset + 4; } @@ -492,7 +498,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32( offset, value, - true + true, ); return offset + 4; } @@ -500,7 +506,7 @@ export default class Buffer extends Uint8Array { writeInt8(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setInt8( offset, - value + value, ); return offset + 1; } @@ -508,7 +514,7 @@ export default class Buffer extends Uint8Array { writeInt16BE(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16( offset, - value + value, ); return offset + 2; } @@ -516,7 +522,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16( offset, value, - true + true, ); return offset + 2; } @@ -524,7 +530,7 @@ export default class Buffer extends Uint8Array { writeInt32BE(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32( offset, - value + value, ); return offset + 4; } @@ -532,7 +538,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setInt32( offset, value, - true + true, ); return offset + 4; } @@ -540,7 +546,7 @@ export default class Buffer extends Uint8Array { writeUInt8(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setUint8( offset, - value + value, ); return offset + 1; } @@ -548,7 +554,7 @@ export default class Buffer extends Uint8Array { writeUInt16BE(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16( offset, - value + value, ); return offset + 2; } @@ -556,7 +562,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16( offset, value, - true + true, ); return offset + 2; } @@ -564,7 +570,7 @@ export default class Buffer extends Uint8Array { writeUInt32BE(value: number, offset = 0): number { new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32( offset, - value + value, ); return offset + 4; } @@ -572,7 +578,7 @@ export default class Buffer extends Uint8Array { new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32( offset, value, - true + true, ); return offset + 4; } |