2020-09-21 08:26:41 -04:00
|
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-06-21 09:47:03 -04:00
|
|
|
|
import * as hex from "../encoding/hex.ts";
|
|
|
|
|
import * as base64 from "../encoding/base64.ts";
|
2020-09-27 06:22:32 -04:00
|
|
|
|
import { normalizeEncoding, notImplemented } from "./_utils.ts";
|
2020-06-21 09:47:03 -04:00
|
|
|
|
|
|
|
|
|
const notImplementedEncodings = [
|
|
|
|
|
"ascii",
|
|
|
|
|
"binary",
|
2020-07-14 14:30:03 -04:00
|
|
|
|
"latin1",
|
2020-06-21 09:47:03 -04:00
|
|
|
|
"ucs2",
|
2020-07-14 14:30:03 -04:00
|
|
|
|
"utf16le",
|
2020-06-21 09:47:03 -04:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function checkEncoding(encoding = "utf8", strict = true): string {
|
|
|
|
|
if (typeof encoding !== "string" || (strict && encoding === "")) {
|
|
|
|
|
if (!strict) return "utf8";
|
|
|
|
|
throw new TypeError(`Unkown encoding: ${encoding}`);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 12:27:02 -04:00
|
|
|
|
const normalized = normalizeEncoding(encoding);
|
|
|
|
|
|
2020-07-14 15:24:17 -04:00
|
|
|
|
if (normalized === undefined) {
|
2020-06-28 12:27:02 -04:00
|
|
|
|
throw new TypeError(`Unkown encoding: ${encoding}`);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
}
|
2020-06-21 09:47:03 -04:00
|
|
|
|
|
|
|
|
|
if (notImplementedEncodings.includes(encoding)) {
|
|
|
|
|
notImplemented(`"${encoding}" encoding`);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 12:27:02 -04:00
|
|
|
|
return normalized;
|
2020-06-21 09:47:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 18:02:09 -04:00
|
|
|
|
interface EncodingOp {
|
|
|
|
|
byteLength(string: string): number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// https://github.com/nodejs/node/blob/56dbe466fdbc598baea3bfce289bf52b97b8b8f7/lib/buffer.js#L598
|
|
|
|
|
const encodingOps: { [key: string]: EncodingOp } = {
|
|
|
|
|
utf8: {
|
|
|
|
|
byteLength: (string: string): number =>
|
|
|
|
|
new TextEncoder().encode(string).byteLength,
|
|
|
|
|
},
|
|
|
|
|
ucs2: {
|
|
|
|
|
byteLength: (string: string): number => string.length * 2,
|
|
|
|
|
},
|
|
|
|
|
utf16le: {
|
|
|
|
|
byteLength: (string: string): number => string.length * 2,
|
|
|
|
|
},
|
|
|
|
|
latin1: {
|
|
|
|
|
byteLength: (string: string): number => string.length,
|
|
|
|
|
},
|
|
|
|
|
ascii: {
|
|
|
|
|
byteLength: (string: string): number => string.length,
|
|
|
|
|
},
|
|
|
|
|
base64: {
|
|
|
|
|
byteLength: (string: string): number =>
|
|
|
|
|
base64ByteLength(string, string.length),
|
|
|
|
|
},
|
|
|
|
|
hex: {
|
|
|
|
|
byteLength: (string: string): number => string.length >>> 1,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function base64ByteLength(str: string, bytes: number): number {
|
|
|
|
|
// Handle padding
|
|
|
|
|
if (str.charCodeAt(bytes - 1) === 0x3d) bytes--;
|
|
|
|
|
if (bytes > 1 && str.charCodeAt(bytes - 1) === 0x3d) bytes--;
|
|
|
|
|
|
|
|
|
|
// Base64 ratio: 3/4
|
|
|
|
|
return (bytes * 3) >>> 2;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 15:56:49 -04:00
|
|
|
|
/**
|
|
|
|
|
* See also https://nodejs.org/api/buffer.html
|
|
|
|
|
*/
|
|
|
|
|
export default class Buffer extends Uint8Array {
|
|
|
|
|
/**
|
|
|
|
|
* Allocates a new Buffer of size bytes.
|
|
|
|
|
*/
|
2020-06-27 15:56:39 -04:00
|
|
|
|
static alloc(
|
|
|
|
|
size: number,
|
|
|
|
|
fill?: number | string | Uint8Array | Buffer,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
encoding = "utf8",
|
2020-06-27 15:56:39 -04:00
|
|
|
|
): Buffer {
|
|
|
|
|
if (typeof size !== "number") {
|
|
|
|
|
throw new TypeError(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
`The "size" argument must be of type number. Received type ${typeof size}`,
|
2020-06-27 15:56:39 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buf = new Buffer(size);
|
|
|
|
|
if (size === 0) return buf;
|
|
|
|
|
|
|
|
|
|
let bufFill;
|
|
|
|
|
if (typeof fill === "string") {
|
|
|
|
|
encoding = checkEncoding(encoding);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
if (
|
2020-08-24 19:43:54 -04:00
|
|
|
|
typeof fill === "string" &&
|
|
|
|
|
fill.length === 1 &&
|
|
|
|
|
encoding === "utf8"
|
2020-07-14 15:24:17 -04:00
|
|
|
|
) {
|
2020-06-27 15:56:39 -04:00
|
|
|
|
buf.fill(fill.charCodeAt(0));
|
2020-07-14 15:24:17 -04:00
|
|
|
|
} else bufFill = Buffer.from(fill, encoding);
|
2020-06-28 10:16:54 -04:00
|
|
|
|
} else if (typeof fill === "number") {
|
|
|
|
|
buf.fill(fill);
|
|
|
|
|
} else if (fill instanceof Uint8Array) {
|
2020-06-27 15:56:39 -04:00
|
|
|
|
if (fill.length === 0) {
|
|
|
|
|
throw new TypeError(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
`The argument "value" is invalid. Received ${fill.constructor.name} []`,
|
2020-06-27 15:56:39 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bufFill = fill;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bufFill) {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
if (bufFill.length > buf.length) {
|
2020-06-27 15:56:39 -04:00
|
|
|
|
bufFill = bufFill.subarray(0, buf.length);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
}
|
2020-06-27 15:56:39 -04:00
|
|
|
|
|
|
|
|
|
let offset = 0;
|
|
|
|
|
while (offset < size) {
|
|
|
|
|
buf.set(bufFill, offset);
|
|
|
|
|
offset += bufFill.length;
|
|
|
|
|
if (offset + bufFill.length >= size) break;
|
|
|
|
|
}
|
|
|
|
|
if (offset !== size) {
|
|
|
|
|
buf.set(bufFill.subarray(0, size - offset), offset);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buf;
|
2020-06-06 15:56:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 10:16:54 -04:00
|
|
|
|
static allocUnsafe(size: number): Buffer {
|
|
|
|
|
return new Buffer(size);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 15:56:49 -04:00
|
|
|
|
/**
|
|
|
|
|
* Returns the byte length of a string when encoded. This is not the same as
|
|
|
|
|
* String.prototype.length, which does not account for the encoding that is
|
|
|
|
|
* used to convert the string into bytes.
|
|
|
|
|
*/
|
|
|
|
|
static byteLength(
|
2020-07-05 18:02:09 -04:00
|
|
|
|
string: string | Buffer | ArrayBufferView | ArrayBuffer | SharedArrayBuffer,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
encoding = "utf8",
|
2020-06-06 15:56:49 -04:00
|
|
|
|
): number {
|
|
|
|
|
if (typeof string != "string") return string.byteLength;
|
2020-07-05 18:02:09 -04:00
|
|
|
|
|
|
|
|
|
encoding = normalizeEncoding(encoding) || "utf8";
|
|
|
|
|
return encodingOps[encoding].byteLength(string);
|
2020-06-06 15:56:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a new Buffer which is the result of concatenating all the Buffer
|
|
|
|
|
* instances in the list together.
|
|
|
|
|
*/
|
|
|
|
|
static concat(list: Buffer[] | Uint8Array[], totalLength?: number): Buffer {
|
|
|
|
|
if (totalLength == undefined) {
|
|
|
|
|
totalLength = 0;
|
|
|
|
|
for (const buf of list) {
|
|
|
|
|
totalLength += buf.length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buffer = new Buffer(totalLength);
|
|
|
|
|
let pos = 0;
|
|
|
|
|
for (const buf of list) {
|
|
|
|
|
buffer.set(buf, pos);
|
|
|
|
|
pos += buf.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Allocates a new Buffer using an array of bytes in the range 0 – 255. Array
|
|
|
|
|
* entries outside that range will be truncated to fit into it.
|
|
|
|
|
*/
|
|
|
|
|
static from(array: number[]): Buffer;
|
|
|
|
|
/**
|
|
|
|
|
* This creates a view of the ArrayBuffer without copying the underlying
|
|
|
|
|
* memory. For example, when passed a reference to the .buffer property of a
|
|
|
|
|
* TypedArray instance, the newly created Buffer will share the same allocated
|
|
|
|
|
* memory as the TypedArray.
|
|
|
|
|
*/
|
|
|
|
|
static from(
|
|
|
|
|
arrayBuffer: ArrayBuffer | SharedArrayBuffer,
|
|
|
|
|
byteOffset?: number,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
length?: number,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
): Buffer;
|
|
|
|
|
/**
|
|
|
|
|
* Copies the passed buffer data onto a new Buffer instance.
|
|
|
|
|
*/
|
|
|
|
|
static from(buffer: Buffer | Uint8Array): Buffer;
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new Buffer containing string.
|
|
|
|
|
*/
|
2020-06-21 09:47:03 -04:00
|
|
|
|
static from(string: string, encoding?: string): Buffer;
|
|
|
|
|
static from(
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
value: any,
|
|
|
|
|
offsetOrEncoding?: number | string,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
length?: number,
|
2020-06-21 09:47:03 -04:00
|
|
|
|
): Buffer {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
const offset = typeof offsetOrEncoding === "string"
|
|
|
|
|
? undefined
|
|
|
|
|
: offsetOrEncoding;
|
|
|
|
|
let encoding = typeof offsetOrEncoding === "string"
|
|
|
|
|
? offsetOrEncoding
|
|
|
|
|
: undefined;
|
2020-06-21 09:47:03 -04:00
|
|
|
|
|
|
|
|
|
if (typeof value == "string") {
|
|
|
|
|
encoding = checkEncoding(encoding, false);
|
|
|
|
|
if (encoding === "hex") return new Buffer(hex.decodeString(value).buffer);
|
|
|
|
|
if (encoding === "base64") return new Buffer(base64.decode(value));
|
2020-06-06 15:56:49 -04:00
|
|
|
|
return new Buffer(new TextEncoder().encode(value).buffer);
|
2020-06-21 09:47:03 -04:00
|
|
|
|
}
|
2020-06-06 15:56:49 -04:00
|
|
|
|
|
|
|
|
|
// workaround for https://github.com/microsoft/TypeScript/issues/38446
|
|
|
|
|
return new Buffer(value, offset!, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if obj is a Buffer, false otherwise.
|
|
|
|
|
*/
|
2020-08-24 19:43:54 -04:00
|
|
|
|
static isBuffer(obj: unknown): obj is Buffer {
|
2020-06-06 15:56:49 -04:00
|
|
|
|
return obj instanceof Buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-28 12:27:02 -04:00
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
static isEncoding(encoding: any): boolean {
|
|
|
|
|
return (
|
|
|
|
|
typeof encoding === "string" &&
|
|
|
|
|
encoding.length !== 0 &&
|
|
|
|
|
normalizeEncoding(encoding) !== undefined
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 15:56:49 -04:00
|
|
|
|
/**
|
|
|
|
|
* Copies data from a region of buf to a region in target, even if the target
|
|
|
|
|
* memory region overlaps with buf.
|
|
|
|
|
*/
|
|
|
|
|
copy(
|
|
|
|
|
targetBuffer: Buffer | Uint8Array,
|
|
|
|
|
targetStart = 0,
|
|
|
|
|
sourceStart = 0,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
sourceEnd = this.length,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
): number {
|
|
|
|
|
const sourceBuffer = this.subarray(sourceStart, sourceEnd);
|
|
|
|
|
targetBuffer.set(sourceBuffer, targetStart);
|
|
|
|
|
return sourceBuffer.length;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 18:01:36 -04:00
|
|
|
|
/*
|
|
|
|
|
* Returns true if both buf and otherBuffer have exactly the same bytes, false otherwise.
|
|
|
|
|
*/
|
|
|
|
|
equals(otherBuffer: Uint8Array | Buffer): boolean {
|
|
|
|
|
if (!(otherBuffer instanceof Uint8Array)) {
|
|
|
|
|
throw new TypeError(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
`The "otherBuffer" argument must be an instance of Buffer or Uint8Array. Received type ${typeof otherBuffer}`,
|
2020-07-05 18:01:36 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this === otherBuffer) return true;
|
|
|
|
|
if (this.byteLength !== otherBuffer.byteLength) return false;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < this.length; i++) {
|
|
|
|
|
if (this[i] !== otherBuffer[i]) return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-06 15:56:49 -04:00
|
|
|
|
readBigInt64BE(offset = 0): bigint {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getBigInt64(offset);
|
|
|
|
|
}
|
|
|
|
|
readBigInt64LE(offset = 0): bigint {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getBigInt64(offset, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readBigUInt64BE(offset = 0): bigint {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getBigUint64(offset);
|
|
|
|
|
}
|
|
|
|
|
readBigUInt64LE(offset = 0): bigint {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getBigUint64(offset, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readDoubleBE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getFloat64(offset);
|
|
|
|
|
}
|
|
|
|
|
readDoubleLE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getFloat64(offset, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readFloatBE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getFloat32(offset);
|
|
|
|
|
}
|
|
|
|
|
readFloatLE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getFloat32(offset, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readInt8(offset = 0): number {
|
|
|
|
|
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt8(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
offset,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readInt16BE(offset = 0): number {
|
|
|
|
|
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
offset,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
readInt16LE(offset = 0): number {
|
|
|
|
|
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt16(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readInt32BE(offset = 0): number {
|
|
|
|
|
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
offset,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
readInt32LE(offset = 0): number {
|
|
|
|
|
return new DataView(this.buffer, this.byteOffset, this.byteLength).getInt32(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readUInt8(offset = 0): number {
|
|
|
|
|
return new DataView(this.buffer, this.byteOffset, this.byteLength).getUint8(
|
2020-07-14 15:24:17 -04:00
|
|
|
|
offset,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readUInt16BE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getUint16(offset);
|
|
|
|
|
}
|
|
|
|
|
readUInt16LE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getUint16(offset, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
readUInt32BE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getUint32(offset);
|
|
|
|
|
}
|
|
|
|
|
readUInt32LE(offset = 0): number {
|
|
|
|
|
return new DataView(
|
|
|
|
|
this.buffer,
|
|
|
|
|
this.byteOffset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.byteLength,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).getUint32(offset, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a new Buffer that references the same memory as the original, but
|
|
|
|
|
* offset and cropped by the start and end indices.
|
|
|
|
|
*/
|
|
|
|
|
slice(begin = 0, end = this.length): Buffer {
|
|
|
|
|
// workaround for https://github.com/microsoft/TypeScript/issues/38665
|
|
|
|
|
return this.subarray(begin, end) as Buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a JSON representation of buf. JSON.stringify() implicitly calls
|
|
|
|
|
* this function when stringifying a Buffer instance.
|
|
|
|
|
*/
|
2020-08-24 19:43:54 -04:00
|
|
|
|
toJSON(): Record<string, unknown> {
|
2020-06-06 15:56:49 -04:00
|
|
|
|
return { type: "Buffer", data: Array.from(this) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decodes buf to a string according to the specified character encoding in
|
|
|
|
|
* encoding. start and end may be passed to decode only a subset of buf.
|
|
|
|
|
*/
|
|
|
|
|
toString(encoding = "utf8", start = 0, end = this.length): string {
|
2020-06-21 09:47:03 -04:00
|
|
|
|
encoding = checkEncoding(encoding);
|
|
|
|
|
|
|
|
|
|
const b = this.subarray(start, end);
|
|
|
|
|
if (encoding === "hex") return hex.encodeToString(b);
|
|
|
|
|
if (encoding === "base64") return base64.encode(b.buffer);
|
|
|
|
|
|
|
|
|
|
return new TextDecoder(encoding).decode(b);
|
2020-06-06 15:56:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Writes string to buf at offset according to the character encoding in
|
|
|
|
|
* encoding. The length parameter is the number of bytes to write. If buf did
|
|
|
|
|
* not contain enough space to fit the entire string, only part of string will
|
|
|
|
|
* be written. However, partially encoded characters will not be written.
|
|
|
|
|
*/
|
|
|
|
|
write(string: string, offset = 0, length = this.length): number {
|
|
|
|
|
return new TextEncoder().encodeInto(
|
|
|
|
|
string,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
this.subarray(offset, offset + length),
|
2020-06-06 15:56:49 -04:00
|
|
|
|
).written;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeBigInt64BE(value: bigint, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
writeBigInt64LE(value: bigint, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigInt64(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeBigUInt64BE(value: bigint, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
writeBigUInt64LE(value: bigint, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setBigUint64(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeDoubleBE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 8;
|
|
|
|
|
}
|
|
|
|
|
writeDoubleLE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat64(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeFloatBE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
writeFloatLE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setFloat32(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeInt8(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt8(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeInt16BE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 2;
|
|
|
|
|
}
|
|
|
|
|
writeInt16LE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt16(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeInt32BE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
writeInt32LE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setInt32(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeUInt8(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint8(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeUInt16BE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 2;
|
|
|
|
|
}
|
|
|
|
|
writeUInt16LE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint16(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeUInt32BE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32(
|
|
|
|
|
offset,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
value,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
writeUInt32LE(value: number, offset = 0): number {
|
|
|
|
|
new DataView(this.buffer, this.byteOffset, this.byteLength).setUint32(
|
|
|
|
|
offset,
|
|
|
|
|
value,
|
2020-07-14 15:24:17 -04:00
|
|
|
|
true,
|
2020-06-06 15:56:49 -04:00
|
|
|
|
);
|
|
|
|
|
return offset + 4;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { Buffer };
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(globalThis, "Buffer", {
|
|
|
|
|
value: Buffer,
|
|
|
|
|
enumerable: false,
|
|
|
|
|
writable: true,
|
|
|
|
|
configurable: true,
|
|
|
|
|
});
|