node-noise
Version:
573 lines (570 loc) • 22.7 kB
JavaScript
"use strict";
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Uint8ArrayList = exports.isUint8ArrayList = void 0;
var concat_1 = require("../uint8arrays/concat");
var equals_1 = require("../uint8arrays/equals");
var alloc_1 = require("../uint8arrays/alloc");
var symbol = Symbol.for('@achingbrain/uint8arraylist');
function findBufAndOffset(bufs, index) {
if (index == null || index < 0) {
throw new RangeError('index is out of bounds');
}
var offset = 0;
for (var _i = 0, bufs_1 = bufs; _i < bufs_1.length; _i++) {
var buf = bufs_1[_i];
var bufEnd = offset + buf.byteLength;
if (index < bufEnd) {
return {
buf: buf,
index: index - offset
};
}
offset = bufEnd;
}
throw new RangeError('index is out of bounds');
}
/**
* Check if object is a CID instance
*
* @example
*
* ```js
* import { isUint8ArrayList, Uint8ArrayList } from 'uint8arraylist'
*
* isUint8ArrayList(true) // false
* isUint8ArrayList([]) // false
* isUint8ArrayList(new Uint8ArrayList()) // true
* ```
*/
function isUint8ArrayList(value) {
return Boolean(value === null || value === void 0 ? void 0 : value[symbol]);
}
exports.isUint8ArrayList = isUint8ArrayList;
var Uint8ArrayList = /** @class */ (function () {
function Uint8ArrayList() {
var data = [];
for (var _i = 0; _i < arguments.length; _i++) {
data[_i] = arguments[_i];
}
// Define symbol
Object.defineProperty(this, symbol, { value: true });
this.bufs = [];
this.length = 0;
if (data.length > 0) {
this.appendAll(data);
}
}
Uint8ArrayList.prototype[Symbol.iterator] = function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [5 /*yield**/, __values(this.bufs)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
};
Object.defineProperty(Uint8ArrayList.prototype, "byteLength", {
get: function () {
return this.length;
},
enumerable: false,
configurable: true
});
/**
* Add one or more `bufs` to the end of this Uint8ArrayList
*/
Uint8ArrayList.prototype.append = function () {
var bufs = [];
for (var _i = 0; _i < arguments.length; _i++) {
bufs[_i] = arguments[_i];
}
this.appendAll(bufs);
};
/**
* Add all `bufs` to the end of this Uint8ArrayList
*/
Uint8ArrayList.prototype.appendAll = function (bufs) {
var _a;
var length = 0;
for (var _i = 0, bufs_2 = bufs; _i < bufs_2.length; _i++) {
var buf = bufs_2[_i];
if (buf instanceof Uint8Array) {
length += buf.byteLength;
this.bufs.push(buf);
}
else if (isUint8ArrayList(buf)) {
length += buf.byteLength;
(_a = this.bufs).push.apply(_a, buf.bufs);
}
else {
throw new Error('Could not append value, must be an Uint8Array or a Uint8ArrayList');
}
}
this.length += length;
};
/**
* Add one or more `bufs` to the start of this Uint8ArrayList
*/
Uint8ArrayList.prototype.prepend = function () {
var bufs = [];
for (var _i = 0; _i < arguments.length; _i++) {
bufs[_i] = arguments[_i];
}
this.prependAll(bufs);
};
/**
* Add all `bufs` to the start of this Uint8ArrayList
*/
Uint8ArrayList.prototype.prependAll = function (bufs) {
var _a;
var length = 0;
for (var _i = 0, _b = bufs.reverse(); _i < _b.length; _i++) {
var buf = _b[_i];
if (buf instanceof Uint8Array) {
length += buf.byteLength;
this.bufs.unshift(buf);
}
else if (isUint8ArrayList(buf)) {
length += buf.byteLength;
(_a = this.bufs).unshift.apply(_a, buf.bufs);
}
else {
throw new Error('Could not prepend value, must be an Uint8Array or a Uint8ArrayList');
}
}
this.length += length;
};
/**
* Read the value at `index`
*/
Uint8ArrayList.prototype.get = function (index) {
var res = findBufAndOffset(this.bufs, index);
return res.buf[res.index];
};
/**
* Set the value at `index` to `value`
*/
Uint8ArrayList.prototype.set = function (index, value) {
var res = findBufAndOffset(this.bufs, index);
res.buf[res.index] = value;
};
/**
* Copy bytes from `buf` to the index specified by `offset`
*/
Uint8ArrayList.prototype.write = function (buf, offset) {
if (offset === void 0) { offset = 0; }
if (buf instanceof Uint8Array) {
for (var i = 0; i < buf.length; i++) {
this.set(offset + i, buf[i]);
}
}
else if (isUint8ArrayList(buf)) {
for (var i = 0; i < buf.length; i++) {
this.set(offset + i, buf.get(i));
}
}
else {
throw new Error('Could not write value, must be an Uint8Array or a Uint8ArrayList');
}
};
/**
* Remove bytes from the front of the pool
*/
Uint8ArrayList.prototype.consume = function (bytes) {
// first, normalize the argument, in accordance with how Buffer does it
bytes = Math.trunc(bytes);
// do nothing if not a positive number
if (Number.isNaN(bytes) || bytes <= 0) {
return;
}
// if consuming all bytes, skip iterating
if (bytes === this.byteLength) {
this.bufs = [];
this.length = 0;
return;
}
while (this.bufs.length > 0) {
if (bytes >= this.bufs[0].byteLength) {
bytes -= this.bufs[0].byteLength;
this.length -= this.bufs[0].byteLength;
this.bufs.shift();
}
else {
this.bufs[0] = this.bufs[0].subarray(bytes);
this.length -= bytes;
break;
}
}
};
/**
* Extracts a section of an array and returns a new array.
*
* This is a copy operation as it is with Uint8Arrays and Arrays
* - note this is different to the behaviour of Node Buffers.
*/
Uint8ArrayList.prototype.slice = function (beginInclusive, endExclusive) {
var _a = this._subList(beginInclusive, endExclusive), bufs = _a.bufs, length = _a.length;
return (0, concat_1.concat)(bufs, length);
};
/**
* Returns a alloc from the given start and end element index.
*
* In the best case where the data extracted comes from a single Uint8Array
* internally this is a no-copy operation otherwise it is a copy operation.
*/
Uint8ArrayList.prototype.subarray = function (beginInclusive, endExclusive) {
var _a = this._subList(beginInclusive, endExclusive), bufs = _a.bufs, length = _a.length;
if (bufs.length === 1) {
return bufs[0];
}
return (0, concat_1.concat)(bufs, length);
};
/**
* Returns a allocList from the given start and end element index.
*
* This is a no-copy operation.
*/
Uint8ArrayList.prototype.sublist = function (beginInclusive, endExclusive) {
var _a = this._subList(beginInclusive, endExclusive), bufs = _a.bufs, length = _a.length;
var list = new Uint8ArrayList();
list.length = length;
// don't loop, just set the bufs
list.bufs = bufs;
return list;
};
Uint8ArrayList.prototype._subList = function (beginInclusive, endExclusive) {
beginInclusive = beginInclusive !== null && beginInclusive !== void 0 ? beginInclusive : 0;
endExclusive = endExclusive !== null && endExclusive !== void 0 ? endExclusive : this.length;
if (beginInclusive < 0) {
beginInclusive = this.length + beginInclusive;
}
if (endExclusive < 0) {
endExclusive = this.length + endExclusive;
}
if (beginInclusive < 0 || endExclusive > this.length) {
throw new RangeError('index is out of bounds');
}
if (beginInclusive === endExclusive) {
return { bufs: [], length: 0 };
}
if (beginInclusive === 0 && endExclusive === this.length) {
return { bufs: __spreadArray([], this.bufs, true), length: this.length };
}
var bufs = [];
var offset = 0;
for (var i = 0; i < this.bufs.length; i++) {
var buf = this.bufs[i];
var bufStart = offset;
var bufEnd = bufStart + buf.byteLength;
// for next loop
offset = bufEnd;
if (beginInclusive >= bufEnd) {
// start after this buf
continue;
}
var sliceStartInBuf = beginInclusive >= bufStart && beginInclusive < bufEnd;
var sliceEndsInBuf = endExclusive > bufStart && endExclusive <= bufEnd;
if (sliceStartInBuf && sliceEndsInBuf) {
// slice is wholly contained within this buffer
if (beginInclusive === bufStart && endExclusive === bufEnd) {
// requested whole buffer
bufs.push(buf);
break;
}
// requested part of buffer
var start = beginInclusive - bufStart;
bufs.push(buf.subarray(start, start + (endExclusive - beginInclusive)));
break;
}
if (sliceStartInBuf) {
// slice starts in this buffer
if (beginInclusive === 0) {
// requested whole buffer
bufs.push(buf);
continue;
}
// requested part of buffer
bufs.push(buf.subarray(beginInclusive - bufStart));
continue;
}
if (sliceEndsInBuf) {
if (endExclusive === bufEnd) {
// requested whole buffer
bufs.push(buf);
break;
}
// requested part of buffer
bufs.push(buf.subarray(0, endExclusive - bufStart));
break;
}
// slice started before this buffer and ends after it
bufs.push(buf);
}
return { bufs: bufs, length: endExclusive - beginInclusive };
};
Uint8ArrayList.prototype.indexOf = function (search, offset) {
if (offset === void 0) { offset = 0; }
if (!isUint8ArrayList(search) && !(search instanceof Uint8Array)) {
throw new TypeError('The "value" argument must be a Uint8ArrayList or Uint8Array');
}
var needle = search instanceof Uint8Array ? search : search.subarray();
offset = Number(offset !== null && offset !== void 0 ? offset : 0);
if (isNaN(offset)) {
offset = 0;
}
if (offset < 0) {
offset = this.length + offset;
}
if (offset < 0) {
offset = 0;
}
if (search.length === 0) {
return offset > this.length ? this.length : offset;
}
// https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm
var M = needle.byteLength;
if (M === 0) {
throw new TypeError('search must be at least 1 byte long');
}
// radix
var radix = 256;
var rightmostPositions = new Int32Array(radix);
// position of the rightmost occurrence of the byte c in the pattern
for (var c = 0; c < radix; c++) {
// -1 for bytes not in pattern
rightmostPositions[c] = -1;
}
for (var j = 0; j < M; j++) {
// rightmost position for bytes in pattern
rightmostPositions[needle[j]] = j;
}
// Return offset of first match, -1 if no match
var right = rightmostPositions;
var lastIndex = this.byteLength - needle.byteLength;
var lastPatIndex = needle.byteLength - 1;
var skip;
for (var i = offset; i <= lastIndex; i += skip) {
skip = 0;
for (var j = lastPatIndex; j >= 0; j--) {
var char = this.get(i + j);
if (needle[j] !== char) {
skip = Math.max(1, j - right[char]);
break;
}
}
if (skip === 0) {
return i;
}
}
return -1;
};
Uint8ArrayList.prototype.getInt8 = function (byteOffset) {
var buf = this.subarray(byteOffset, byteOffset + 1);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getInt8(0);
};
Uint8ArrayList.prototype.setInt8 = function (byteOffset, value) {
var buf = (0, alloc_1.allocUnsafe)(1);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setInt8(0, value);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getInt16 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 2);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getInt16(0, littleEndian);
};
Uint8ArrayList.prototype.setInt16 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(2);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setInt16(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getInt32 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 4);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getInt32(0, littleEndian);
};
Uint8ArrayList.prototype.setInt32 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(4);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setInt32(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getBigInt64 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 8);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getBigInt64(0, littleEndian);
};
Uint8ArrayList.prototype.setBigInt64 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(8);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setBigInt64(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getUint8 = function (byteOffset) {
var buf = this.subarray(byteOffset, byteOffset + 1);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getUint8(0);
};
Uint8ArrayList.prototype.setUint8 = function (byteOffset, value) {
var buf = (0, alloc_1.allocUnsafe)(1);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setUint8(0, value);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getUint16 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 2);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getUint16(0, littleEndian);
};
Uint8ArrayList.prototype.setUint16 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(2);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setUint16(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getUint32 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 4);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getUint32(0, littleEndian);
};
Uint8ArrayList.prototype.setUint32 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(4);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setUint32(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getBigUint64 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 8);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getBigUint64(0, littleEndian);
};
Uint8ArrayList.prototype.setBigUint64 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(8);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setBigUint64(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getFloat32 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 4);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getFloat32(0, littleEndian);
};
Uint8ArrayList.prototype.setFloat32 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(4);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setFloat32(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.getFloat64 = function (byteOffset, littleEndian) {
var buf = this.subarray(byteOffset, byteOffset + 8);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
return view.getFloat64(0, littleEndian);
};
Uint8ArrayList.prototype.setFloat64 = function (byteOffset, value, littleEndian) {
var buf = (0, alloc_1.alloc)(8);
var view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
view.setFloat64(0, value, littleEndian);
this.write(buf, byteOffset);
};
Uint8ArrayList.prototype.equals = function (other) {
if (other == null) {
return false;
}
if (!(other instanceof Uint8ArrayList)) {
return false;
}
if (other.bufs.length !== this.bufs.length) {
return false;
}
for (var i = 0; i < this.bufs.length; i++) {
if (!(0, equals_1.equals)(this.bufs[i], other.bufs[i])) {
return false;
}
}
return true;
};
/**
* Create a Uint8ArrayList from a pre-existing list of Uint8Arrays. Use this
* method if you know the total size of all the Uint8Arrays ahead of time.
*/
Uint8ArrayList.fromUint8Arrays = function (bufs, length) {
var list = new Uint8ArrayList();
list.bufs = bufs;
if (length == null) {
length = bufs.reduce(function (acc, curr) { return acc + curr.byteLength; }, 0);
}
list.length = length;
return list;
};
return Uint8ArrayList;
}());
exports.Uint8ArrayList = Uint8ArrayList;
/*
function indexOf (needle: Uint8Array, haystack: Uint8Array, offset = 0) {
for (let i = offset; i < haystack.byteLength; i++) {
for (let j = 0; j < needle.length; j++) {
if (haystack[i + j] !== needle[j]) {
break
}
if (j === needle.byteLength -1) {
return i
}
}
if (haystack.byteLength - i < needle.byteLength) {
break
}
}
return -1
}
*/
//# sourceMappingURL=index.js.map