4 lines
7.0 KiB
JavaScript
4 lines
7.0 KiB
JavaScript
|
|
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Uint8Arraylist = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
||
|
|
"use strict";var Uint8Arraylist=(()=>{var L=Object.defineProperty;var B=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var O=(s,t)=>{for(var n in t)L(s,n,{get:t[n],enumerable:!0})},T=(s,t,n,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of d(t))!v.call(s,r)&&r!==n&&L(s,r,{get:()=>t[r],enumerable:!(e=B(t,r))||e.enumerable});return s};var F=s=>T(L({},"__esModule",{value:!0}),s);var k={};O(k,{Uint8ArrayList:()=>U,isUint8ArrayList:()=>y});function l(s){return globalThis.Buffer!=null?new Uint8Array(s.buffer,s.byteOffset,s.byteLength):s}function a(s=0){return globalThis.Buffer?.alloc!=null?l(globalThis.Buffer.alloc(s)):new Uint8Array(s)}function g(s=0){return globalThis.Buffer?.allocUnsafe!=null?l(globalThis.Buffer.allocUnsafe(s)):new Uint8Array(s)}function p(s,t){if(globalThis.Buffer!=null)return l(globalThis.Buffer.concat(s,t));t==null&&(t=s.reduce((r,i)=>r+i.length,0));let n=g(t),e=0;for(let r of s)n.set(r,e),e+=r.length;return l(n)}function A(s,t){if(s===t)return!0;if(s.byteLength!==t.byteLength)return!1;for(let n=0;n<s.byteLength;n++)if(s[n]!==t[n])return!1;return!0}var V=Symbol.for("@achingbrain/uint8arraylist");function D(s,t){if(t==null||t<0)throw new RangeError("index is out of bounds");let n=0;for(let e of s){let r=n+e.byteLength;if(t<r)return{buf:e,index:t-n};n=r}throw new RangeError("index is out of bounds")}function y(s){return!!s?.[V]}var U=class s{bufs;length;[V]=!0;constructor(...t){this.bufs=[],this.length=0,t.length>0&&this.appendAll(t)}*[Symbol.iterator](){yield*this.bufs}get byteLength(){return this.length}append(...t){this.appendAll(t)}appendAll(t){let n=0;for(let e of t)if(e instanceof Uint8Array)n+=e.byteLength,this.bufs.push(e);else if(y(e))n+=e.byteLength,this.bufs.push(...e.bufs);else throw new Error("Could not append value, must be an Uint8Array or a Uint8ArrayList");this.length+=n}prepend(...t){this.prependAll(t)}prependAll(t){let n=0;for(let e of t.reverse())if(e instanceof Uint8Array)n+=e.byteLength,this.bufs.unshift(e);else if(y(e))n+=e.byteLength,this.bufs.unshift(...e.bufs);else throw new Error("Could not prepend value, must be an Uint8Array or a Uint8ArrayList");this.length+=n}get(t){let n=D(this.bufs,t);return n.buf[n.index]}set(t,n){let e=D(this.bufs,t);e.buf[e.index]=n}write(t,n=0){if(t instanceof Uint8Array)for(let e=0;e<t.length;e++)this.set(n+e,t[e]);else if(y(t))for(let e=0;e<t.length;e++)this.set(n+e,t.get(e));else throw new Error("Could not write value, must be an Uint8Array or a Uint8ArrayList")}consume(t){if(t=Math.trunc(t),!(Number.isNaN(t)||t<=0)){if(t===this.byteLength){this.bufs=[],this.length=0;return}for(;this.bufs.length>0;)if(t>=this.bufs[0].byteLength)t-=this.bufs[0].byteLength,this.length-=this.bufs[0].byteLength,this.bufs.shift();else{this.bufs[0]=this.bufs[0].subarray(t),this.length-=t;break}}}slice(t,n){let{bufs:e,length:r}=this._subList(t,n);return p(e,r)}subarray(t,n){let{bufs:e,length:r}=this._subList(t,n);return e.length===1?e[0]:p(e,r)}sublist(t,n){let{bufs:e,length:r}=this._subList(t,n),i=new s;return i.length=r,i.bufs=[...e],i}_subList(t,n){if(t=t??0,n=n??this.length,t<0&&(t=this.length+t),n<0&&(n=this.length+n),t<0||n>this.length)throw new RangeError("index is out of bounds");if(t===n)return{bufs:[],length:0};if(t===0&&n===this.length)return{bufs:this.bufs,length:this.length};let e=[],r=0;for(let i=0;i<this.bufs.length;i++){let o=this.bufs[i],u=r,h=u+o.byteLength;if(r=h,t>=h)continue;let c=t>=u&&t<h,b=n>u&&n<=h;if(c&&b){if(t===u&&n===h){e.push(o);break}let f=t-u;e.push(o.subarray(f,f+(n-t)));break}if(c){if(t===0){e.push(o);continue}e.push(o.subarray(t-u));continue}if(b){if(n===h){e.push(o);break}e.push(o.subarray(0,n-u));break}e.push(o)}return{bufs:e,length:n-t}}indexOf(t,n=0){if(!y(t)&&!(t instanceof Uint8Array))throw new TypeError('The "value" argument must be a Uint8ArrayList or Uint8Array');let e=t instanceof Uint8Array?t:t.subarray();if(n=Number(n??0),isNaN(n)&&(n=0),n<0&&(n=this.length+n),n<0&&(n=0),t.length===0)return n>this.length?this.length:n;let r=e.byteLength;if(r===0)throw new T
|
||
|
|
return Uint8Arraylist}));
|