@xata.io/client
Version:
Xata.io SDK for TypeScript and JavaScript
1,474 lines (1,472 loc) • 191 kB
JavaScript
const defaultTrace = async (name, fn, _options) => {
return await fn({
name,
setAttributes: () => {
return;
}
});
};
const TraceAttributes = {
KIND: "xata.trace.kind",
VERSION: "xata.sdk.version",
TABLE: "xata.table",
HTTP_REQUEST_ID: "http.request_id",
HTTP_STATUS_CODE: "http.status_code",
HTTP_HOST: "http.host",
HTTP_SCHEME: "http.scheme",
HTTP_USER_AGENT: "http.user_agent",
HTTP_METHOD: "http.method",
HTTP_URL: "http.url",
HTTP_ROUTE: "http.route",
HTTP_TARGET: "http.target",
CLOUDFLARE_RAY_ID: "cf.ray"
};
const lookup = [];
const revLookup = [];
const code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (let i = 0, len = code.length; i < len; ++i) {
lookup[i] = code[i];
revLookup[code.charCodeAt(i)] = i;
}
revLookup["-".charCodeAt(0)] = 62;
revLookup["_".charCodeAt(0)] = 63;
function getLens(b64) {
const len = b64.length;
if (len % 4 > 0) {
throw new Error("Invalid string. Length must be a multiple of 4");
}
let validLen = b64.indexOf("=");
if (validLen === -1) validLen = len;
const placeHoldersLen = validLen === len ? 0 : 4 - validLen % 4;
return [validLen, placeHoldersLen];
}
function _byteLength(_b64, validLen, placeHoldersLen) {
return (validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen;
}
function toByteArray(b64) {
let tmp;
const lens = getLens(b64);
const validLen = lens[0];
const placeHoldersLen = lens[1];
const arr = new Uint8Array(_byteLength(b64, validLen, placeHoldersLen));
let curByte = 0;
const len = placeHoldersLen > 0 ? validLen - 4 : validLen;
let i;
for (i = 0; i < len; i += 4) {
tmp = revLookup[b64.charCodeAt(i)] << 18 | revLookup[b64.charCodeAt(i + 1)] << 12 | revLookup[b64.charCodeAt(i + 2)] << 6 | revLookup[b64.charCodeAt(i + 3)];
arr[curByte++] = tmp >> 16 & 255;
arr[curByte++] = tmp >> 8 & 255;
arr[curByte++] = tmp & 255;
}
if (placeHoldersLen === 2) {
tmp = revLookup[b64.charCodeAt(i)] << 2 | revLookup[b64.charCodeAt(i + 1)] >> 4;
arr[curByte++] = tmp & 255;
}
if (placeHoldersLen === 1) {
tmp = revLookup[b64.charCodeAt(i)] << 10 | revLookup[b64.charCodeAt(i + 1)] << 4 | revLookup[b64.charCodeAt(i + 2)] >> 2;
arr[curByte++] = tmp >> 8 & 255;
arr[curByte++] = tmp & 255;
}
return arr;
}
function tripletToBase64(num) {
return lookup[num >> 18 & 63] + lookup[num >> 12 & 63] + lookup[num >> 6 & 63] + lookup[num & 63];
}
function encodeChunk(uint8, start, end) {
let tmp;
const output = [];
for (let i = start; i < end; i += 3) {
tmp = (uint8[i] << 16 & 16711680) + (uint8[i + 1] << 8 & 65280) + (uint8[i + 2] & 255);
output.push(tripletToBase64(tmp));
}
return output.join("");
}
function fromByteArray(uint8) {
let tmp;
const len = uint8.length;
const extraBytes = len % 3;
const parts = [];
const maxChunkLength = 16383;
for (let i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
parts.push(encodeChunk(uint8, i, i + maxChunkLength > len2 ? len2 : i + maxChunkLength));
}
if (extraBytes === 1) {
tmp = uint8[len - 1];
parts.push(lookup[tmp >> 2] + lookup[tmp << 4 & 63] + "==");
} else if (extraBytes === 2) {
tmp = (uint8[len - 2] << 8) + uint8[len - 1];
parts.push(lookup[tmp >> 10] + lookup[tmp >> 4 & 63] + lookup[tmp << 2 & 63] + "=");
}
return parts.join("");
}
const K_MAX_LENGTH = 2147483647;
const MAX_ARGUMENTS_LENGTH = 4096;
class Buffer extends Uint8Array {
/**
* Constructs a new `Buffer` instance.
*
* @param value
* @param encodingOrOffset
* @param length
*/
constructor(value, encodingOrOffset, length) {
if (typeof value === "number") {
if (typeof encodingOrOffset === "string") {
throw new TypeError("The first argument must be of type string, received type number");
}
if (value < 0) {
throw new RangeError("The buffer size cannot be negative");
}
super(value < 0 ? 0 : Buffer._checked(value) | 0);
} else if (typeof value === "string") {
if (typeof encodingOrOffset !== "string") {
encodingOrOffset = "utf8";
}
if (!Buffer.isEncoding(encodingOrOffset)) {
throw new TypeError("Unknown encoding: " + encodingOrOffset);
}
const length2 = Buffer.byteLength(value, encodingOrOffset) | 0;
super(length2);
const written = this.write(value, 0, this.length, encodingOrOffset);
if (written !== length2) {
throw new TypeError(
"Number of bytes written did not match expected length (wrote " + written + ", expected " + length2 + ")"
);
}
} else if (ArrayBuffer.isView(value)) {
if (Buffer._isInstance(value, Uint8Array)) {
const copy = new Uint8Array(value);
const array = copy.buffer;
const byteOffset = copy.byteOffset;
const length2 = copy.byteLength;
if (byteOffset < 0 || array.byteLength < byteOffset) {
throw new RangeError("offset is outside of buffer bounds");
}
if (array.byteLength < byteOffset + (length2 || 0)) {
throw new RangeError("length is outside of buffer bounds");
}
super(new Uint8Array(array, byteOffset, length2));
} else {
const array = value;
const length2 = array.length < 0 ? 0 : Buffer._checked(array.length) | 0;
super(new Uint8Array(length2));
for (let i = 0; i < length2; i++) {
this[i] = array[i] & 255;
}
}
} else if (value == null) {
throw new TypeError(
"The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type " + typeof value
);
} else if (Buffer._isInstance(value, ArrayBuffer) || value && Buffer._isInstance(value.buffer, ArrayBuffer)) {
const array = value;
const byteOffset = encodingOrOffset;
if (byteOffset < 0 || array.byteLength < byteOffset) {
throw new RangeError("offset is outside of buffer bounds");
}
if (array.byteLength < byteOffset + (length || 0)) {
throw new RangeError("length is outside of buffer bounds");
}
super(new Uint8Array(array, byteOffset, length));
} else if (Array.isArray(value)) {
const array = value;
const length2 = array.length < 0 ? 0 : Buffer._checked(array.length) | 0;
super(new Uint8Array(length2));
for (let i = 0; i < length2; i++) {
this[i] = array[i] & 255;
}
} else {
throw new TypeError("Unable to determine the correct way to allocate buffer for type " + typeof value);
}
}
/**
* Return JSON representation of the buffer.
*/
toJSON() {
return {
type: "Buffer",
data: Array.prototype.slice.call(this)
};
}
/**
* Writes `string` to the buffer at `offset` according to the character encoding in `encoding`. The `length`
* parameter is the number of bytes to write. If the buffer does not contain enough space to fit the entire string,
* only part of `string` will be written. However, partially encoded characters will not be written.
*
* @param string String to write to `buf`.
* @param offset Number of bytes to skip before starting to write `string`. Default: `0`.
* @param length Maximum number of bytes to write: Default: `buf.length - offset`.
* @param encoding The character encoding of `string`. Default: `utf8`.
*/
write(string, offset, length, encoding) {
if (typeof offset === "undefined") {
encoding = "utf8";
length = this.length;
offset = 0;
} else if (typeof length === "undefined" && typeof offset === "string") {
encoding = offset;
length = this.length;
offset = 0;
} else if (typeof offset === "number" && isFinite(offset)) {
offset = offset >>> 0;
if (typeof length === "number" && isFinite(length)) {
length = length >>> 0;
encoding ?? (encoding = "utf8");
} else if (typeof length === "string") {
encoding = length;
length = void 0;
}
} else {
throw new Error("Buffer.write(string, encoding, offset[, length]) is no longer supported");
}
const remaining = this.length - offset;
if (typeof length === "undefined" || length > remaining) {
length = remaining;
}
if (string.length > 0 && (length < 0 || offset < 0) || offset > this.length) {
throw new RangeError("Attempt to write outside buffer bounds");
}
encoding || (encoding = "utf8");
switch (Buffer._getEncoding(encoding)) {
case "hex":
return Buffer._hexWrite(this, string, offset, length);
case "utf8":
return Buffer._utf8Write(this, string, offset, length);
case "ascii":
case "latin1":
case "binary":
return Buffer._asciiWrite(this, string, offset, length);
case "ucs2":
case "utf16le":
return Buffer._ucs2Write(this, string, offset, length);
case "base64":
return Buffer._base64Write(this, string, offset, length);
}
}
/**
* Decodes the buffer to a string according to the specified character encoding.
* Passing `start` and `end` will decode only a subset of the buffer.
*
* Note that if the encoding is `utf8` and a byte sequence in the input is not valid UTF-8, then each invalid byte
* will be replaced with `U+FFFD`.
*
* @param encoding
* @param start
* @param end
*/
toString(encoding, start, end) {
const length = this.length;
if (length === 0) {
return "";
}
if (arguments.length === 0) {
return Buffer._utf8Slice(this, 0, length);
}
if (typeof start === "undefined" || start < 0) {
start = 0;
}
if (start > this.length) {
return "";
}
if (typeof end === "undefined" || end > this.length) {
end = this.length;
}
if (end <= 0) {
return "";
}
end >>>= 0;
start >>>= 0;
if (end <= start) {
return "";
}
if (!encoding) {
encoding = "utf8";
}
switch (Buffer._getEncoding(encoding)) {
case "hex":
return Buffer._hexSlice(this, start, end);
case "utf8":
return Buffer._utf8Slice(this, start, end);
case "ascii":
return Buffer._asciiSlice(this, start, end);
case "latin1":
case "binary":
return Buffer._latin1Slice(this, start, end);
case "ucs2":
case "utf16le":
return Buffer._utf16leSlice(this, start, end);
case "base64":
return Buffer._base64Slice(this, start, end);
}
}
/**
* Returns true if this buffer's is equal to the provided buffer, meaning they share the same exact data.
*
* @param otherBuffer
*/
equals(otherBuffer) {
if (!Buffer.isBuffer(otherBuffer)) {
throw new TypeError("Argument must be a Buffer");
}
if (this === otherBuffer) {
return true;
}
return Buffer.compare(this, otherBuffer) === 0;
}
/**
* Compares the buffer with `otherBuffer` and returns a number indicating whether the buffer comes before, after,
* or is the same as `otherBuffer` in sort order. Comparison is based on the actual sequence of bytes in each
* buffer.
*
* - `0` is returned if `otherBuffer` is the same as this buffer.
* - `1` is returned if `otherBuffer` should come before this buffer when sorted.
* - `-1` is returned if `otherBuffer` should come after this buffer when sorted.
*
* @param otherBuffer The buffer to compare to.
* @param targetStart The offset within `otherBuffer` at which to begin comparison.
* @param targetEnd The offset within `otherBuffer` at which to end comparison (exclusive).
* @param sourceStart The offset within this buffer at which to begin comparison.
* @param sourceEnd The offset within this buffer at which to end the comparison (exclusive).
*/
compare(otherBuffer, targetStart, targetEnd, sourceStart, sourceEnd) {
if (Buffer._isInstance(otherBuffer, Uint8Array)) {
otherBuffer = Buffer.from(otherBuffer, otherBuffer.byteOffset, otherBuffer.byteLength);
}
if (!Buffer.isBuffer(otherBuffer)) {
throw new TypeError("Argument must be a Buffer or Uint8Array");
}
targetStart ?? (targetStart = 0);
targetEnd ?? (targetEnd = otherBuffer ? otherBuffer.length : 0);
sourceStart ?? (sourceStart = 0);
sourceEnd ?? (sourceEnd = this.length);
if (targetStart < 0 || targetEnd > otherBuffer.length || sourceStart < 0 || sourceEnd > this.length) {
throw new RangeError("Out of range index");
}
if (sourceStart >= sourceEnd && targetStart >= targetEnd) {
return 0;
}
if (sourceStart >= sourceEnd) {
return -1;
}
if (targetStart >= targetEnd) {
return 1;
}
targetStart >>>= 0;
targetEnd >>>= 0;
sourceStart >>>= 0;
sourceEnd >>>= 0;
if (this === otherBuffer) {
return 0;
}
let x = sourceEnd - sourceStart;
let y = targetEnd - targetStart;
const len = Math.min(x, y);
const thisCopy = this.slice(sourceStart, sourceEnd);
const targetCopy = otherBuffer.slice(targetStart, targetEnd);
for (let i = 0; i < len; ++i) {
if (thisCopy[i] !== targetCopy[i]) {
x = thisCopy[i];
y = targetCopy[i];
break;
}
}
if (x < y) return -1;
if (y < x) return 1;
return 0;
}
/**
* Copies data from a region of this buffer to a region in `targetBuffer`, even if the `targetBuffer` memory
* region overlaps with this buffer.
*
* @param targetBuffer The target buffer to copy into.
* @param targetStart The offset within `targetBuffer` at which to begin writing.
* @param sourceStart The offset within this buffer at which to begin copying.
* @param sourceEnd The offset within this buffer at which to end copying (exclusive).
*/
copy(targetBuffer, targetStart, sourceStart, sourceEnd) {
if (!Buffer.isBuffer(targetBuffer)) throw new TypeError("argument should be a Buffer");
if (!sourceStart) sourceStart = 0;
if (!targetStart) targetStart = 0;
if (!sourceEnd && sourceEnd !== 0) sourceEnd = this.length;
if (targetStart >= targetBuffer.length) targetStart = targetBuffer.length;
if (!targetStart) targetStart = 0;
if (sourceEnd > 0 && sourceEnd < sourceStart) sourceEnd = sourceStart;
if (sourceEnd === sourceStart) return 0;
if (targetBuffer.length === 0 || this.length === 0) return 0;
if (targetStart < 0) {
throw new RangeError("targetStart out of bounds");
}
if (sourceStart < 0 || sourceStart >= this.length) throw new RangeError("Index out of range");
if (sourceEnd < 0) throw new RangeError("sourceEnd out of bounds");
if (sourceEnd > this.length) sourceEnd = this.length;
if (targetBuffer.length - targetStart < sourceEnd - sourceStart) {
sourceEnd = targetBuffer.length - targetStart + sourceStart;
}
const len = sourceEnd - sourceStart;
if (this === targetBuffer && typeof Uint8Array.prototype.copyWithin === "function") {
this.copyWithin(targetStart, sourceStart, sourceEnd);
} else {
Uint8Array.prototype.set.call(targetBuffer, this.subarray(sourceStart, sourceEnd), targetStart);
}
return len;
}
/**
* Returns a new `Buffer` that references the same memory as the original, but offset and cropped by the `start`
* and `end` indices. This is the same behavior as `buf.subarray()`.
*
* This method is not compatible with the `Uint8Array.prototype.slice()`, which is a superclass of Buffer. To copy
* the slice, use `Uint8Array.prototype.slice()`.
*
* @param start
* @param end
*/
slice(start, end) {
if (!start) {
start = 0;
}
const len = this.length;
start = ~~start;
end = end === void 0 ? len : ~~end;
if (start < 0) {
start += len;
if (start < 0) {
start = 0;
}
} else if (start > len) {
start = len;
}
if (end < 0) {
end += len;
if (end < 0) {
end = 0;
}
} else if (end > len) {
end = len;
}
if (end < start) {
end = start;
}
const newBuf = this.subarray(start, end);
Object.setPrototypeOf(newBuf, Buffer.prototype);
return newBuf;
}
/**
* Writes `byteLength` bytes of `value` to `buf` at the specified `offset` as little-endian. Supports up to 48 bits
* of accuracy. Behavior is undefined when value is anything other than an unsigned integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param byteLength Number of bytes to write, between 0 and 6.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeUIntLE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
Buffer._checkInt(this, value, offset, byteLength, maxBytes, 0);
}
let mul = 1;
let i = 0;
this[offset] = value & 255;
while (++i < byteLength && (mul *= 256)) {
this[offset + i] = value / mul & 255;
}
return offset + byteLength;
}
/**
* Writes `byteLength` bytes of `value` to `buf` at the specified `offset` as big-endian. Supports up to 48 bits of
* accuracy. Behavior is undefined when `value` is anything other than an unsigned integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param byteLength Number of bytes to write, between 0 and 6.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeUIntBE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
Buffer._checkInt(this, value, offset, byteLength, maxBytes, 0);
}
let i = byteLength - 1;
let mul = 1;
this[offset + i] = value & 255;
while (--i >= 0 && (mul *= 256)) {
this[offset + i] = value / mul & 255;
}
return offset + byteLength;
}
/**
* Writes `byteLength` bytes of `value` to `buf` at the specified `offset` as little-endian. Supports up to 48 bits
* of accuracy. Behavior is undefined when `value` is anything other than a signed integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param byteLength Number of bytes to write, between 0 and 6.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeIntLE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
const limit = Math.pow(2, 8 * byteLength - 1);
Buffer._checkInt(this, value, offset, byteLength, limit - 1, -limit);
}
let i = 0;
let mul = 1;
let sub = 0;
this[offset] = value & 255;
while (++i < byteLength && (mul *= 256)) {
if (value < 0 && sub === 0 && this[offset + i - 1] !== 0) {
sub = 1;
}
this[offset + i] = (value / mul >> 0) - sub & 255;
}
return offset + byteLength;
}
/**
* Writes `byteLength` bytes of `value` to `buf` at the specified `offset` as big-endian. Supports up to 48 bits
* of accuracy. Behavior is undefined when `value` is anything other than a signed integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param byteLength Number of bytes to write, between 0 and 6.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeIntBE(value, offset, byteLength, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
const limit = Math.pow(2, 8 * byteLength - 1);
Buffer._checkInt(this, value, offset, byteLength, limit - 1, -limit);
}
let i = byteLength - 1;
let mul = 1;
let sub = 0;
this[offset + i] = value & 255;
while (--i >= 0 && (mul *= 256)) {
if (value < 0 && sub === 0 && this[offset + i + 1] !== 0) {
sub = 1;
}
this[offset + i] = (value / mul >> 0) - sub & 255;
}
return offset + byteLength;
}
/**
* Reads `byteLength` number of bytes from `buf` at the specified `offset` and interprets the result as an
* unsigned, little-endian integer supporting up to 48 bits of accuracy.
*
* @param offset Number of bytes to skip before starting to read.
* @param byteLength Number of bytes to read, between 0 and 6.
* @param noAssert
*/
readUIntLE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, byteLength, this.length);
}
let val = this[offset];
let mul = 1;
let i = 0;
while (++i < byteLength && (mul *= 256)) {
val += this[offset + i] * mul;
}
return val;
}
/**
* Reads `byteLength` number of bytes from `buf` at the specified `offset` and interprets the result as an
* unsigned, big-endian integer supporting up to 48 bits of accuracy.
*
* @param offset Number of bytes to skip before starting to read.
* @param byteLength Number of bytes to read, between 0 and 6.
* @param noAssert
*/
readUIntBE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, byteLength, this.length);
}
let val = this[offset + --byteLength];
let mul = 1;
while (byteLength > 0 && (mul *= 256)) {
val += this[offset + --byteLength] * mul;
}
return val;
}
/**
* Reads `byteLength` number of bytes from `buf` at the specified `offset` and interprets the result as a
* little-endian, two's complement signed value supporting up to 48 bits of accuracy.
*
* @param offset Number of bytes to skip before starting to read.
* @param byteLength Number of bytes to read, between 0 and 6.
* @param noAssert
*/
readIntLE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, byteLength, this.length);
}
let val = this[offset];
let mul = 1;
let i = 0;
while (++i < byteLength && (mul *= 256)) {
val += this[offset + i] * mul;
}
mul *= 128;
if (val >= mul) {
val -= Math.pow(2, 8 * byteLength);
}
return val;
}
/**
* Reads `byteLength` number of bytes from `buf` at the specified `offset` and interprets the result as a
* big-endian, two's complement signed value supporting up to 48 bits of accuracy.
*
* @param offset Number of bytes to skip before starting to read.
* @param byteLength Number of bytes to read, between 0 and 6.
* @param noAssert
*/
readIntBE(offset, byteLength, noAssert) {
offset = offset >>> 0;
byteLength = byteLength >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, byteLength, this.length);
}
let i = byteLength;
let mul = 1;
let val = this[offset + --i];
while (i > 0 && (mul *= 256)) {
val += this[offset + --i] * mul;
}
mul *= 128;
if (val >= mul) {
val -= Math.pow(2, 8 * byteLength);
}
return val;
}
/**
* Reads an unsigned 8-bit integer from `buf` at the specified `offset`.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readUInt8(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 1, this.length);
}
return this[offset];
}
/**
* Reads an unsigned, little-endian 16-bit integer from `buf` at the specified `offset`.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readUInt16LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 2, this.length);
}
return this[offset] | this[offset + 1] << 8;
}
/**
* Reads an unsigned, big-endian 16-bit integer from `buf` at the specified `offset`.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readUInt16BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 2, this.length);
}
return this[offset] << 8 | this[offset + 1];
}
/**
* Reads an unsigned, little-endian 32-bit integer from `buf` at the specified `offset`.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readUInt32LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 4, this.length);
}
return (this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16) + this[offset + 3] * 16777216;
}
/**
* Reads an unsigned, big-endian 32-bit integer from `buf` at the specified `offset`.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readUInt32BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 4, this.length);
}
return this[offset] * 16777216 + (this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3]);
}
/**
* Reads a signed 8-bit integer from `buf` at the specified `offset`. Integers read from a `Buffer` are interpreted
* as two's complement signed values.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readInt8(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 1, this.length);
}
if (!(this[offset] & 128)) {
return this[offset];
}
return (255 - this[offset] + 1) * -1;
}
/**
* Reads a signed, little-endian 16-bit integer from `buf` at the specified `offset`. Integers read from a `Buffer`
* are interpreted as two's complement signed values.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readInt16LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 2, this.length);
}
const val = this[offset] | this[offset + 1] << 8;
return val & 32768 ? val | 4294901760 : val;
}
/**
* Reads a signed, big-endian 16-bit integer from `buf` at the specified `offset`. Integers read from a `Buffer`
* are interpreted as two's complement signed values.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readInt16BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 2, this.length);
}
const val = this[offset + 1] | this[offset] << 8;
return val & 32768 ? val | 4294901760 : val;
}
/**
* Reads a signed, little-endian 32-bit integer from `buf` at the specified `offset`. Integers read from a `Buffer`
* are interpreted as two's complement signed values.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readInt32LE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 4, this.length);
}
return this[offset] | this[offset + 1] << 8 | this[offset + 2] << 16 | this[offset + 3] << 24;
}
/**
* Reads a signed, big-endian 32-bit integer from `buf` at the specified `offset`. Integers read from a `Buffer`
* are interpreted as two's complement signed values.
*
* @param offset Number of bytes to skip before starting to read.
* @param noAssert
*/
readInt32BE(offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkOffset(offset, 4, this.length);
}
return this[offset] << 24 | this[offset + 1] << 16 | this[offset + 2] << 8 | this[offset + 3];
}
/**
* Interprets `buf` as an array of unsigned 16-bit integers and swaps the byte order in-place.
* Throws a `RangeError` if `buf.length` is not a multiple of 2.
*/
swap16() {
const len = this.length;
if (len % 2 !== 0) {
throw new RangeError("Buffer size must be a multiple of 16-bits");
}
for (let i = 0; i < len; i += 2) {
this._swap(this, i, i + 1);
}
return this;
}
/**
* Interprets `buf` as an array of unsigned 32-bit integers and swaps the byte order in-place.
* Throws a `RangeError` if `buf.length` is not a multiple of 4.
*/
swap32() {
const len = this.length;
if (len % 4 !== 0) {
throw new RangeError("Buffer size must be a multiple of 32-bits");
}
for (let i = 0; i < len; i += 4) {
this._swap(this, i, i + 3);
this._swap(this, i + 1, i + 2);
}
return this;
}
/**
* Interprets `buf` as an array of unsigned 64-bit integers and swaps the byte order in-place.
* Throws a `RangeError` if `buf.length` is not a multiple of 8.
*/
swap64() {
const len = this.length;
if (len % 8 !== 0) {
throw new RangeError("Buffer size must be a multiple of 64-bits");
}
for (let i = 0; i < len; i += 8) {
this._swap(this, i, i + 7);
this._swap(this, i + 1, i + 6);
this._swap(this, i + 2, i + 5);
this._swap(this, i + 3, i + 4);
}
return this;
}
/**
* Swaps two octets.
*
* @param b
* @param n
* @param m
*/
_swap(b, n, m) {
const i = b[n];
b[n] = b[m];
b[m] = i;
}
/**
* Writes `value` to `buf` at the specified `offset`. The `value` must be a valid unsigned 8-bit integer.
* Behavior is undefined when `value` is anything other than an unsigned 8-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeUInt8(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 1, 255, 0);
}
this[offset] = value & 255;
return offset + 1;
}
/**
* Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid unsigned 16-bit
* integer. Behavior is undefined when `value` is anything other than an unsigned 16-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeUInt16LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 2, 65535, 0);
}
this[offset] = value & 255;
this[offset + 1] = value >>> 8;
return offset + 2;
}
/**
* Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid unsigned 16-bit
* integer. Behavior is undefined when `value` is anything other than an unsigned 16-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeUInt16BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 2, 65535, 0);
}
this[offset] = value >>> 8;
this[offset + 1] = value & 255;
return offset + 2;
}
/**
* Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid unsigned 32-bit
* integer. Behavior is undefined when `value` is anything other than an unsigned 32-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeUInt32LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 4, 4294967295, 0);
}
this[offset + 3] = value >>> 24;
this[offset + 2] = value >>> 16;
this[offset + 1] = value >>> 8;
this[offset] = value & 255;
return offset + 4;
}
/**
* Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid unsigned 32-bit
* integer. Behavior is undefined when `value` is anything other than an unsigned 32-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeUInt32BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 4, 4294967295, 0);
}
this[offset] = value >>> 24;
this[offset + 1] = value >>> 16;
this[offset + 2] = value >>> 8;
this[offset + 3] = value & 255;
return offset + 4;
}
/**
* Writes `value` to `buf` at the specified `offset`. The `value` must be a valid signed 8-bit integer.
* Behavior is undefined when `value` is anything other than a signed 8-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeInt8(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 1, 127, -128);
}
if (value < 0) {
value = 255 + value + 1;
}
this[offset] = value & 255;
return offset + 1;
}
/**
* Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid signed 16-bit
* integer. Behavior is undefined when `value` is anything other than a signed 16-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeInt16LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 2, 32767, -32768);
}
this[offset] = value & 255;
this[offset + 1] = value >>> 8;
return offset + 2;
}
/**
* Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid signed 16-bit
* integer. Behavior is undefined when `value` is anything other than a signed 16-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeInt16BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 2, 32767, -32768);
}
this[offset] = value >>> 8;
this[offset + 1] = value & 255;
return offset + 2;
}
/**
* Writes `value` to `buf` at the specified `offset` as little-endian. The `value` must be a valid signed 32-bit
* integer. Behavior is undefined when `value` is anything other than a signed 32-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeInt32LE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 4, 2147483647, -2147483648);
}
this[offset] = value & 255;
this[offset + 1] = value >>> 8;
this[offset + 2] = value >>> 16;
this[offset + 3] = value >>> 24;
return offset + 4;
}
/**
* Writes `value` to `buf` at the specified `offset` as big-endian. The `value` must be a valid signed 32-bit
* integer. Behavior is undefined when `value` is anything other than a signed 32-bit integer.
*
* @param value Number to write.
* @param offset Number of bytes to skip before starting to write.
* @param noAssert
* @returns `offset` plus the number of bytes written.
*/
writeInt32BE(value, offset, noAssert) {
value = +value;
offset = offset >>> 0;
if (!noAssert) {
Buffer._checkInt(this, value, offset, 4, 2147483647, -2147483648);
}
if (value < 0) {
value = 4294967295 + value + 1;
}
this[offset] = value >>> 24;
this[offset + 1] = value >>> 16;
this[offset + 2] = value >>> 8;
this[offset + 3] = value & 255;
return offset + 4;
}
/**
* Fills `buf` with the specified `value`. If the `offset` and `end` are not given, the entire `buf` will be
* filled. The `value` is coerced to a `uint32` value if it is not a string, `Buffer`, or integer. If the resulting
* integer is greater than `255` (decimal), then `buf` will be filled with `value & 255`.
*
* If the final write of a `fill()` operation falls on a multi-byte character, then only the bytes of that
* character that fit into `buf` are written.
*
* If `value` contains invalid characters, it is truncated; if no valid fill data remains, an exception is thrown.
*
* @param value
* @param encoding
*/
fill(value, offset, end, encoding) {
if (typeof value === "string") {
if (typeof offset === "string") {
encoding = offset;
offset = 0;
end = this.length;
} else if (typeof end === "string") {
encoding = end;
end = this.length;
}
if (encoding !== void 0 && typeof encoding !== "string") {
throw new TypeError("encoding must be a string");
}
if (typeof encoding === "string" && !Buffer.isEncoding(encoding)) {
throw new TypeError("Unknown encoding: " + encoding);
}
if (value.length === 1) {
const code = value.charCodeAt(0);
if (encoding === "utf8" && code < 128) {
value = code;
}
}
} else if (typeof value === "number") {
value = value & 255;
} else if (typeof value === "boolean") {
value = Number(value);
}
offset ?? (offset = 0);
end ?? (end = this.length);
if (offset < 0 || this.length < offset || this.length < end) {
throw new RangeError("Out of range index");
}
if (end <= offset) {
return this;
}
offset = offset >>> 0;
end = end === void 0 ? this.length : end >>> 0;
value || (value = 0);
let i;
if (typeof value === "number") {
for (i = offset; i < end; ++i) {
this[i] = value;
}
} else {
const bytes = Buffer.isBuffer(value) ? value : Buffer.from(value, encoding);
const len = bytes.length;
if (len === 0) {
throw new TypeError('The value "' + value + '" is invalid for argument "value"');
}
for (i = 0; i < end - offset; ++i) {
this[i + offset] = bytes[i % len];
}
}
return this;
}
/**
* Returns the index of the specified value.
*
* If `value` is:
* - a string, `value` is interpreted according to the character encoding in `encoding`.
* - a `Buffer` or `Uint8Array`, `value` will be used in its entirety. To compare a partial Buffer, use `slice()`.
* - a number, `value` will be interpreted as an unsigned 8-bit integer value between `0` and `255`.
*
* Any other types will throw a `TypeError`.
*
* @param value What to search for.
* @param byteOffset Where to begin searching in `buf`. If negative, then calculated from the end.
* @param encoding If `value` is a string, this is the encoding used to search.
* @returns The index of the first occurrence of `value` in `buf`, or `-1` if not found.
*/
indexOf(value, byteOffset, encoding) {
return this._bidirectionalIndexOf(this, value, byteOffset, encoding, true);
}
/**
* Gets the last index of the specified value.
*
* @see indexOf()
* @param value
* @param byteOffset
* @param encoding
*/
lastIndexOf(value, byteOffset, encoding) {
return this._bidirectionalIndexOf(this, value, byteOffset, encoding, false);
}
_bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) {
if (buffer.length === 0) {
return -1;
}
if (typeof byteOffset === "string") {
encoding = byteOffset;
byteOffset = 0;
} else if (typeof byteOffset === "undefined") {
byteOffset = 0;
} else if (byteOffset > 2147483647) {
byteOffset = 2147483647;
} else if (byteOffset < -2147483648) {
byteOffset = -2147483648;
}
byteOffset = +byteOffset;
if (byteOffset !== byteOffset) {
byteOffset = dir ? 0 : buffer.length - 1;
}
if (byteOffset < 0) {
byteOffset = buffer.length + byteOffset;
}
if (byteOffset >= buffer.length) {
if (dir) {
return -1;
} else {
byteOffset = buffer.length - 1;
}
} else if (byteOffset < 0) {
if (dir) {
byteOffset = 0;
} else {
return -1;
}
}
if (typeof val === "string") {
val = Buffer.from(val, encoding);
}
if (Buffer.isBuffer(val)) {
if (val.length === 0) {
return -1;
}
return Buffer._arrayIndexOf(buffer, val, byteOffset, encoding, dir);
} else if (typeof val === "number") {
val = val & 255;
if (typeof Uint8Array.prototype.indexOf === "function") {
if (dir) {
return Uint8Array.prototype.indexOf.call(buffer, val, byteOffset);
} else {
return Uint8Array.prototype.lastIndexOf.call(buffer, val, byteOffset);
}
}
return Buffer._arrayIndexOf(buffer, Buffer.from([val]), byteOffset, encoding, dir);
}
throw new TypeError("val must be string, number or Buffer");
}
/**
* Equivalent to `buf.indexOf() !== -1`.
*
* @param value
* @param byteOffset
* @param encoding
*/
includes(value, byteOffset, encoding) {
return this.indexOf(value, byteOffset, encoding) !== -1;
}
/**
* Creates a new buffer from the given parameters.
*
* @param data
* @param encoding
*/
static from(a, b, c) {
return new Buffer(a, b, c);
}
/**
* Returns true if `obj` is a Buffer.
*
* @param obj
*/
static isBuffer(obj) {
return obj != null && obj !== Buffer.prototype && Buffer._isInstance(obj, Buffer);
}
/**
* Returns true if `encoding` is a supported encoding.
*
* @param encoding
*/
static isEncoding(encoding) {
switch (encoding.toLowerCase()) {
case "hex":
case "utf8":
case "ascii":
case "binary":
case "latin1":
case "ucs2":
case "utf16le":
case "base64":
return true;
default:
return false;
}
}
/**
* Gives the actual byte length of a string for an encoding. This is not the same as `string.length` since that
* returns the number of characters in the string.
*
* @param string The string to test.
* @param encoding The encoding to use for calculation. Defaults is `utf8`.
*/
static byteLength(string, encoding) {
if (Buffer.isBuffer(string)) {
return string.length;
}
if (typeof string !== "string" && (ArrayBuffer.isView(string) || Buffer._isInstance(string, ArrayBuffer))) {
return string.byteLength;
}
if (typeof string !== "string") {
throw new TypeError(
'The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type ' + typeof string
);
}
const len = string.length;
const mustMatch = arguments.length > 2 && arguments[2] === true;
if (!mustMatch && len === 0) {
return 0;
}
switch (encoding?.toLowerCase()) {
case "ascii":
case "latin1":
case "binary":
return len;
case "utf8":
return Buffer._utf8ToBytes(string).length;
case "hex":
return len >>> 1;
case "ucs2":
case "utf16le":
return len * 2;
case "base64":
return Buffer._base64ToBytes(string).length;
default:
return mustMatch ? -1 : Buffer._utf8ToBytes(string).length;
}
}
/**
* Returns a Buffer which is the result of concatenating all the buffers in the list together.
*
* - If the list has no items, or if the `totalLength` is 0, then it returns a zero-length buffer.
* - If the list has exactly one item, then the first item is returned.
* - If the list has more than one item, then a new buffer is created.
*
* It is faster to provide the `totalLength` if it is known. However, it will be calculated if not provided at
* a small computational expense.
*
* @param list An array of Buffer objects to concatenate.
* @param totalLength Total length of the buffers when concatenated.
*/
static concat(list, totalLength) {
if (!Array.isArray(list)) {
throw new TypeError('"list" argument must be an Array of Buffers');
}
if (list.length === 0) {
return Buffer.alloc(0);
}
let i;
if (totalLength === void 0) {
totalLength = 0;
for (i = 0; i < list.length; ++i) {
totalLength += list[i].length;
}
}
const buffer = Buffer.allocUnsafe(totalLength);
let pos = 0;
for (i = 0; i < list.length; ++i) {
let buf = list[i];
if (Buffer._isInstance(buf, Uint8Array)) {
if (pos + buf.length > buffer.length) {
if (!Buffer.isBuffer(buf)) {
buf = Buffer.from(buf);
}
buf.copy(buffer, pos);
} else {
Uint8Array.prototype.set.call(buffer, buf, pos);
}
} else if (!Buffer.isBuffer(buf)) {
throw new TypeError('"list" argument must be an Array of Buffers');
} else {
buf.copy(buffer, pos);
}
pos += buf.length;
}
return buffer;
}
/**
* The same as `buf1.compare(buf2)`.
*/
static compare(buf1, buf2) {
if (Buffer._isInstance(buf1, Uint8Array)) {
buf1 = Buffer.from(buf1, buf1.byteOffset, buf1.byteLength);
}
if (Buffer._isInstance(buf2, Uint8Array)) {
buf2 = Buffer.from(buf2, buf2.byteOffset, buf2.byteLength);
}
if (!Buffer.isBuffer(buf1) || !Buffer.isBuffer(buf2)) {
throw new TypeError('The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array');
}
if (buf1 === buf2) {
return 0;
}
let x = buf1.length;
let y = buf2.length;
for (let i = 0, len = Math.min(x, y); i < len; ++i) {
if (buf1[i] !== buf2[i]) {
x = buf1[i];
y = buf2[i];
break;
}
}
if (x < y) {
return -1;
}
if (y < x) {
return 1;
}
return 0;
}
/**
* Allocates a new buffer of `size` octets.
*
* @param size The number of octets to allocate.
* @param fill If specified, the buffer will be initialized by calling `buf.fill(fill)`, or with zeroes otherwise.
* @param encoding The encoding used for the call to `buf.fill()` while initializing.
*/
static alloc(size, fill, encoding) {
if (typeof size !== "number") {
throw new TypeError('"size" argument must be of type number');
} else if (size < 0) {
throw new RangeError('The value "' + size + '" is invalid for option "size"');
}
if (size <= 0) {
return new Buffer(size);
}
if (fill !== void 0) {
return typeof encoding === "string" ? new Buffer(size).fill(fill, 0, size, encoding) : new Buffer(size).fill(fill);
}
return new Buffer(size);
}
/**
* Allocates a new buffer of `size` octets without initializing memory. The contents of the buffer are unknown.
*
* @param size
*/
static allocUnsafe(size) {
if (typeof size !== "number") {
throw new TypeError('"size" argument must be of type number');
} else if (size < 0) {
throw new RangeError('The value "' + size + '" is invalid for option "size"');
}
return new Buffer(size < 0 ? 0 : Buffer._checked(size) | 0);
}
/**
* Returns true if the given `obj` is an instance of `type`.
*
* @param obj
* @param type
*/
static _isInstance(obj, type) {
return obj instanceof type || obj != null && obj.constructor != null && obj.constructor.name != null && obj.constructor.name === type.name;
}
static _checked(length) {
if (length >= K_MAX_LENGTH) {
throw new RangeError(
"Attempt to allocate Buffer larger than maximum size: 0x" + K_MAX_LENGTH.toString(16) + " bytes"
);
}
return length | 0;
}
static _blitBuffer(src, dst, offset, length) {
let i;
for (i = 0; i < length; ++i) {
if (i + offset >= dst.length || i >= src.length) {
break;
}
dst[i + offset] = src[i];
}
return i;
}
static _utf8Write(buf, string, offset, length) {
return Buffer._blitBuffer(Buffer._utf8ToBytes(string, buf.length - offset), buf, offset, length);
}
static _asciiWrite(buf, string, offset, length) {
return Buffer._blitBuffer(Buffer._asciiToBytes(string), buf, offset, length);
}
static _base64Write(buf, string, offset, length) {
return Buffer._blitBuffer(Buffer._base64ToBytes(string), buf, offset, length);
}
static _ucs2Write(buf, string, offset, length) {
return Buffer._blitBuffer(Buffer._utf16leToBytes(string, buf.length - offset), buf, offset, length);
}
static _hexWrite(buf, string, offset, length) {
offset = Number(offset) || 0;
const remaining = buf.length - offset;
if (!length) {
length = remaining;
} else {
length = Number(length);
if (length > remaining) {
length = remaining;
}
}
const strLen = string.length;
if (length > strLen / 2) {
length = strLen / 2;
}
let i;
for (i = 0; i < length; ++i) {
const parsed = parseInt(string.substr(i * 2, 2), 16);
if (parsed !== pa