it-length-prefixed
Advanced tools
Comparing version 9.0.4 to 9.0.5
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.ItLengthPrefixed = factory()}(typeof self !== 'undefined' ? self : this, function () { | ||
"use strict";var ItLengthPrefixed=(()=>{var Y=Object.create;var U=Object.defineProperty;var J=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var Q=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty;var Z=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports),$=(r,t)=>{for(var e in t)U(r,e,{get:t[e],enumerable:!0})},M=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of K(t))!W.call(r,i)&&i!==e&&U(r,i,{get:()=>t[i],enumerable:!(n=J(t,i))||n.enumerable});return r};var z=(r,t,e)=>(e=r!=null?Y(Q(r)):{},M(t||!r||!r.__esModule?U(e,"default",{value:r,enumerable:!0}):e,r)),v=r=>M(U({},"__esModule",{value:!0}),r);var q=Z((_t,j)=>{"use strict";function X(r,t){for(let e in t)Object.defineProperty(r,e,{value:t[e],enumerable:!0,configurable:!0});return r}function at(r,t,e){if(!r||typeof r=="string")throw new TypeError("Please pass an Error to err-code");e||(e={}),typeof t=="object"&&(e=t,t=""),t&&(e.code=t);try{return X(r,e)}catch{e.message=r.message,e.stack=r.stack;let i=function(){};return i.prototype=Object.create(Object.getPrototypeOf(r)),X(new i,e)}}j.exports=at});var ut={};$(ut,{decode:()=>D,encode:()=>B});function d(r){return globalThis.Buffer!=null?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):r}function l(r=0){return globalThis.Buffer?.alloc!=null?d(globalThis.Buffer.alloc(r)):new Uint8Array(r)}function w(r=0){return globalThis.Buffer?.allocUnsafe!=null?d(globalThis.Buffer.allocUnsafe(r)):new Uint8Array(r)}var tt=Math.pow(2,7),et=Math.pow(2,14),nt=Math.pow(2,21),T=Math.pow(2,28),N=Math.pow(2,35),_=Math.pow(2,42),O=Math.pow(2,49),o=128,u=127;function y(r){if(r<tt)return 1;if(r<et)return 2;if(r<nt)return 3;if(r<T)return 4;if(r<N)return 5;if(r<_)return 6;if(r<O)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function rt(r,t,e=0){switch(y(r)){case 8:t[e++]=r&255|o,r/=128;case 7:t[e++]=r&255|o,r/=128;case 6:t[e++]=r&255|o,r/=128;case 5:t[e++]=r&255|o,r/=128;case 4:t[e++]=r&255|o,r>>>=7;case 3:t[e++]=r&255|o,r>>>=7;case 2:t[e++]=r&255|o,r>>>=7;case 1:{t[e++]=r&255,r>>>=7;break}default:throw new Error("unreachable")}return t}function it(r,t,e=0){switch(y(r)){case 8:t.set(e++,r&255|o),r/=128;case 7:t.set(e++,r&255|o),r/=128;case 6:t.set(e++,r&255|o),r/=128;case 5:t.set(e++,r&255|o),r/=128;case 4:t.set(e++,r&255|o),r>>>=7;case 3:t.set(e++,r&255|o),r>>>=7;case 2:t.set(e++,r&255|o),r>>>=7;case 1:{t.set(e++,r&255),r>>>=7;break}default:throw new Error("unreachable")}return t}function st(r,t){let e=r[t],n=0;if(n+=e&u,e<o||(e=r[t+1],n+=(e&u)<<7,e<o)||(e=r[t+2],n+=(e&u)<<14,e<o)||(e=r[t+3],n+=(e&u)<<21,e<o)||(e=r[t+4],n+=(e&u)*T,e<o)||(e=r[t+5],n+=(e&u)*N,e<o)||(e=r[t+6],n+=(e&u)*_,e<o)||(e=r[t+7],n+=(e&u)*O,e<o))return n;throw new RangeError("Could not decode varint")}function ot(r,t){let e=r.get(t),n=0;if(n+=e&u,e<o||(e=r.get(t+1),n+=(e&u)<<7,e<o)||(e=r.get(t+2),n+=(e&u)<<14,e<o)||(e=r.get(t+3),n+=(e&u)<<21,e<o)||(e=r.get(t+4),n+=(e&u)*T,e<o)||(e=r.get(t+5),n+=(e&u)*N,e<o)||(e=r.get(t+6),n+=(e&u)*_,e<o)||(e=r.get(t+7),n+=(e&u)*O,e<o))return n;throw new RangeError("Could not decode varint")}function I(r,t,e=0){return t==null&&(t=w(y(r))),t instanceof Uint8Array?rt(r,t,e):it(r,t,e)}function S(r,t=0){return r instanceof Uint8Array?st(r,t):ot(r,t)}function R(r,t){if(globalThis.Buffer!=null)return d(globalThis.Buffer.concat(r,t));t==null&&(t=r.reduce((i,s)=>i+s.length,0));let e=w(t),n=0;for(let i of r)e.set(i,n),n+=i.length;return d(e)}function C(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}var P=Symbol.for("@achingbrain/uint8arraylist");function H(r,t){if(t==null||t<0)throw new RangeError("index is out of bounds");let e=0;for(let n of r){let i=e+n.byteLength;if(t<i)return{buf:n,index:t-e};e=i}throw new RangeError("index is out of bounds")}function E(r){return!!r?.[P]}var p=class r{bufs;length;[P]=!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 e=0;for(let n of t)if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.push(n);else if(E(n))e+=n.byteLength,this.bufs.push(...n.bufs);else throw new Error("Could not append value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}prepend(...t){this.prependAll(t)}prependAll(t){let e=0;for(let n of t.reverse())if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.unshift(n);else if(E(n))e+=n.byteLength,this.bufs.unshift(...n.bufs);else throw new Error("Could not prepend value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}get(t){let e=H(this.bufs,t);return e.buf[e.index]}set(t,e){let n=H(this.bufs,t);n.buf[n.index]=e}write(t,e=0){if(t instanceof Uint8Array)for(let n=0;n<t.length;n++)this.set(e+n,t[n]);else if(E(t))for(let n=0;n<t.length;n++)this.set(e+n,t.get(n));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,e){let{bufs:n,length:i}=this._subList(t,e);return R(n,i)}subarray(t,e){let{bufs:n,length:i}=this._subList(t,e);return n.length===1?n[0]:R(n,i)}sublist(t,e){let{bufs:n,length:i}=this._subList(t,e),s=new r;return s.length=i,s.bufs=[...n],s}_subList(t,e){if(t=t??0,e=e??this.length,t<0&&(t=this.length+t),e<0&&(e=this.length+e),t<0||e>this.length)throw new RangeError("index is out of bounds");if(t===e)return{bufs:[],length:0};if(t===0&&e===this.length)return{bufs:this.bufs,length:this.length};let n=[],i=0;for(let s=0;s<this.bufs.length;s++){let a=this.bufs[s],c=i,g=c+a.byteLength;if(i=g,t>=g)continue;let f=t>=c&&t<g,b=e>c&&e<=g;if(f&&b){if(t===c&&e===g){n.push(a);break}let h=t-c;n.push(a.subarray(h,h+(e-t)));break}if(f){if(t===0){n.push(a);continue}n.push(a.subarray(t-c));continue}if(b){if(e===g){n.push(a);break}n.push(a.subarray(0,e-c));break}n.push(a)}return{bufs:n,length:e-t}}indexOf(t,e=0){if(!E(t)&&!(t instanceof Uint8Array))throw new TypeError('The "value" argument must be a Uint8ArrayList or Uint8Array');let n=t instanceof Uint8Array?t:t.subarray();if(e=Number(e??0),isNaN(e)&&(e=0),e<0&&(e=this.length+e),e<0&&(e=0),t.length===0)return e>this.length?this.length:e;let i=n.byteLength;if(i===0)throw new TypeError("search must be at least 1 byte long");let s=256,a=new Int32Array(s);for(let h=0;h<s;h++)a[h]=-1;for(let h=0;h<i;h++)a[n[h]]=h;let c=a,g=this.byteLength-n.byteLength,f=n.byteLength-1,b;for(let h=e;h<=g;h+=b){b=0;for(let A=f;A>=0;A--){let G=this.get(h+A);if(n[A]!==G){b=Math.max(1,A-c[G]);break}}if(b===0)return h}return-1}getInt8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getInt8(0)}setInt8(t,e){let n=w(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setInt8(0,e),this.write(n,t)}getInt16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt16(0,e)}setInt16(t,e,n){let i=l(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt16(0,e,n),this.write(i,t)}getInt32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt32(0,e)}setInt32(t,e,n){let i=l(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt32(0,e,n),this.write(i,t)}getBigInt64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigInt64(0,e)}setBigInt64(t,e,n){let i=l(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigInt64(0,e,n),this.write(i,t)}getUint8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint8(0)}setUint8(t,e){let n=w(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setUint8(0,e),this.write(n,t)}getUint16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint16(0,e)}setUint16(t,e,n){let i=l(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint16(0,e,n),this.write(i,t)}getUint32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint32(0,e)}setUint32(t,e,n){let i=l(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint32(0,e,n),this.write(i,t)}getBigUint64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigUint64(0,e)}setBigUint64(t,e,n){let i=l(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigUint64(0,e,n),this.write(i,t)}getFloat32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat32(0,e)}setFloat32(t,e,n){let i=l(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat32(0,e,n),this.write(i,t)}getFloat64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat64(0,e)}setFloat64(t,e,n){let i=l(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat64(0,e,n),this.write(i,t)}equals(t){if(t==null||!(t instanceof r)||t.bufs.length!==this.bufs.length)return!1;for(let e=0;e<this.bufs.length;e++)if(!C(this.bufs[e],t.bufs[e]))return!1;return!0}static fromUint8Arrays(t,e){let n=new r;return n.bufs=t,e==null&&(e=t.reduce((i,s)=>i+s.byteLength,0)),n.length=e,n}};function F(r){return r[Symbol.asyncIterator]!=null}var x=r=>{let t=y(r),e=w(t);return I(r,e),x.bytes=t,e};x.bytes=0;function B(r,t){t=t??{};let e=t.lengthEncoder??x;function*n(i){let s=e(i.byteLength);s instanceof Uint8Array?yield s:yield*s,i instanceof Uint8Array?yield i:yield*i}return F(r)?async function*(){for await(let i of r)yield*n(i)}():function*(){for(let i of r)yield*n(i)}()}B.single=(r,t)=>{t=t??{};let e=t.lengthEncoder??x;return new p(e(r.byteLength),r)};var m=z(q(),1);var ft=8,ht=1024*1024*4,L;(function(r){r[r.LENGTH=0]="LENGTH",r[r.DATA=1]="DATA"})(L||(L={}));var V=r=>{let t=S(r);return V.bytes=y(t),t};V.bytes=0;function D(r,t){let e=new p,n=L.LENGTH,i=-1,s=t?.lengthDecoder??V,a=t?.maxLengthLength??ft,c=t?.maxDataLength??ht;function*g(){for(;e.byteLength>0;){if(n===L.LENGTH)try{if(i=s(e),i<0)throw(0,m.default)(new Error("invalid message length"),"ERR_INVALID_MSG_LENGTH");if(i>c)throw(0,m.default)(new Error("message length too long"),"ERR_MSG_DATA_TOO_LONG");let f=s.bytes;e.consume(f),t?.onLength!=null&&t.onLength(i),n=L.DATA}catch(f){if(f instanceof RangeError){if(e.byteLength>a)throw(0,m.default)(new Error("message length length too long"),"ERR_MSG_LENGTH_TOO_LONG");break}throw f}if(n===L.DATA){if(e.byteLength<i)break;let f=e.sublist(0,i);e.consume(i),t?.onData!=null&&t.onData(f),yield f,n=L.LENGTH}}}return F(r)?async function*(){for await(let f of r)e.append(f),yield*g();if(e.byteLength>0)throw(0,m.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}():function*(){for(let f of r)e.append(f),yield*g();if(e.byteLength>0)throw(0,m.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}()}D.fromReader=(r,t)=>{let e=1,n=async function*(){for(;;)try{let{done:s,value:a}=await r.next(e);if(s===!0)return;a!=null&&(yield a)}catch(s){if(s.code==="ERR_UNDER_READ")return{done:!0,value:null};throw s}finally{e=1}}();return D(n,{...t??{},onLength:s=>{e=s}})};return v(ut);})(); | ||
"use strict";var ItLengthPrefixed=(()=>{var q=Object.create;var A=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,K=Object.prototype.hasOwnProperty;var Q=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports),W=(r,t)=>{for(var e in t)A(r,e,{get:t[e],enumerable:!0})},M=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of z(t))!K.call(r,i)&&i!==e&&A(r,i,{get:()=>t[i],enumerable:!(n=Y(t,i))||n.enumerable});return r};var Z=(r,t,e)=>(e=r!=null?q(J(r)):{},M(t||!r||!r.__esModule?A(e,"default",{value:r,enumerable:!0}):e,r)),$=r=>M(A({},"__esModule",{value:!0}),r);var j=Q((Tt,X)=>{"use strict";function P(r,t){for(let e in t)Object.defineProperty(r,e,{value:t[e],enumerable:!0,configurable:!0});return r}function ot(r,t,e){if(!r||typeof r=="string")throw new TypeError("Please pass an Error to err-code");e||(e={}),typeof t=="object"&&(e=t,t=""),t&&(e.code=t);try{return P(r,e)}catch{e.message=r.message,e.stack=r.stack;let i=function(){};return i.prototype=Object.create(Object.getPrototypeOf(r)),P(new i,e)}}X.exports=ot});var ft={};W(ft,{decode:()=>x,encode:()=>R});function w(r=0){return new Uint8Array(r)}function b(r=0){return new Uint8Array(r)}var v=Math.pow(2,7),tt=Math.pow(2,14),et=Math.pow(2,21),D=Math.pow(2,28),N=Math.pow(2,35),T=Math.pow(2,42),_=Math.pow(2,49),o=128,c=127;function y(r){if(r<v)return 1;if(r<tt)return 2;if(r<et)return 3;if(r<D)return 4;if(r<N)return 5;if(r<T)return 6;if(r<_)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function nt(r,t,e=0){switch(y(r)){case 8:t[e++]=r&255|o,r/=128;case 7:t[e++]=r&255|o,r/=128;case 6:t[e++]=r&255|o,r/=128;case 5:t[e++]=r&255|o,r/=128;case 4:t[e++]=r&255|o,r>>>=7;case 3:t[e++]=r&255|o,r>>>=7;case 2:t[e++]=r&255|o,r>>>=7;case 1:{t[e++]=r&255,r>>>=7;break}default:throw new Error("unreachable")}return t}function rt(r,t,e=0){switch(y(r)){case 8:t.set(e++,r&255|o),r/=128;case 7:t.set(e++,r&255|o),r/=128;case 6:t.set(e++,r&255|o),r/=128;case 5:t.set(e++,r&255|o),r/=128;case 4:t.set(e++,r&255|o),r>>>=7;case 3:t.set(e++,r&255|o),r>>>=7;case 2:t.set(e++,r&255|o),r>>>=7;case 1:{t.set(e++,r&255),r>>>=7;break}default:throw new Error("unreachable")}return t}function it(r,t){let e=r[t],n=0;if(n+=e&c,e<o||(e=r[t+1],n+=(e&c)<<7,e<o)||(e=r[t+2],n+=(e&c)<<14,e<o)||(e=r[t+3],n+=(e&c)<<21,e<o)||(e=r[t+4],n+=(e&c)*D,e<o)||(e=r[t+5],n+=(e&c)*N,e<o)||(e=r[t+6],n+=(e&c)*T,e<o)||(e=r[t+7],n+=(e&c)*_,e<o))return n;throw new RangeError("Could not decode varint")}function st(r,t){let e=r.get(t),n=0;if(n+=e&c,e<o||(e=r.get(t+1),n+=(e&c)<<7,e<o)||(e=r.get(t+2),n+=(e&c)<<14,e<o)||(e=r.get(t+3),n+=(e&c)<<21,e<o)||(e=r.get(t+4),n+=(e&c)*D,e<o)||(e=r.get(t+5),n+=(e&c)*N,e<o)||(e=r.get(t+6),n+=(e&c)*T,e<o)||(e=r.get(t+7),n+=(e&c)*_,e<o))return n;throw new RangeError("Could not decode varint")}function B(r,t,e=0){return t==null&&(t=b(y(r))),t instanceof Uint8Array?nt(r,t,e):rt(r,t,e)}function I(r,t=0){return r instanceof Uint8Array?it(r,t):st(r,t)}function O(r,t){t==null&&(t=r.reduce((i,s)=>i+s.length,0));let e=b(t),n=0;for(let i of r)e.set(i,n),n+=i.length;return e}function k(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}var H=Symbol.for("@achingbrain/uint8arraylist");function C(r,t){if(t==null||t<0)throw new RangeError("index is out of bounds");let e=0;for(let n of r){let i=e+n.byteLength;if(t<i)return{buf:n,index:t-e};e=i}throw new RangeError("index is out of bounds")}function U(r){return!!r?.[H]}var d=class r{bufs;length;[H]=!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 e=0;for(let n of t)if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.push(n);else if(U(n))e+=n.byteLength,this.bufs.push(...n.bufs);else throw new Error("Could not append value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}prepend(...t){this.prependAll(t)}prependAll(t){let e=0;for(let n of t.reverse())if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.unshift(n);else if(U(n))e+=n.byteLength,this.bufs.unshift(...n.bufs);else throw new Error("Could not prepend value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}get(t){let e=C(this.bufs,t);return e.buf[e.index]}set(t,e){let n=C(this.bufs,t);n.buf[n.index]=e}write(t,e=0){if(t instanceof Uint8Array)for(let n=0;n<t.length;n++)this.set(e+n,t[n]);else if(U(t))for(let n=0;n<t.length;n++)this.set(e+n,t.get(n));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,e){let{bufs:n,length:i}=this._subList(t,e);return O(n,i)}subarray(t,e){let{bufs:n,length:i}=this._subList(t,e);return n.length===1?n[0]:O(n,i)}sublist(t,e){let{bufs:n,length:i}=this._subList(t,e),s=new r;return s.length=i,s.bufs=[...n],s}_subList(t,e){if(t=t??0,e=e??this.length,t<0&&(t=this.length+t),e<0&&(e=this.length+e),t<0||e>this.length)throw new RangeError("index is out of bounds");if(t===e)return{bufs:[],length:0};if(t===0&&e===this.length)return{bufs:this.bufs,length:this.length};let n=[],i=0;for(let s=0;s<this.bufs.length;s++){let a=this.bufs[s],u=i,g=u+a.byteLength;if(i=g,t>=g)continue;let h=t>=u&&t<g,l=e>u&&e<=g;if(h&&l){if(t===u&&e===g){n.push(a);break}let f=t-u;n.push(a.subarray(f,f+(e-t)));break}if(h){if(t===0){n.push(a);continue}n.push(a.subarray(t-u));continue}if(l){if(e===g){n.push(a);break}n.push(a.subarray(0,e-u));break}n.push(a)}return{bufs:n,length:e-t}}indexOf(t,e=0){if(!U(t)&&!(t instanceof Uint8Array))throw new TypeError('The "value" argument must be a Uint8ArrayList or Uint8Array');let n=t instanceof Uint8Array?t:t.subarray();if(e=Number(e??0),isNaN(e)&&(e=0),e<0&&(e=this.length+e),e<0&&(e=0),t.length===0)return e>this.length?this.length:e;let i=n.byteLength;if(i===0)throw new TypeError("search must be at least 1 byte long");let s=256,a=new Int32Array(s);for(let f=0;f<s;f++)a[f]=-1;for(let f=0;f<i;f++)a[n[f]]=f;let u=a,g=this.byteLength-n.byteLength,h=n.byteLength-1,l;for(let f=e;f<=g;f+=l){l=0;for(let m=h;m>=0;m--){let G=this.get(f+m);if(n[m]!==G){l=Math.max(1,m-u[G]);break}}if(l===0)return f}return-1}getInt8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getInt8(0)}setInt8(t,e){let n=b(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setInt8(0,e),this.write(n,t)}getInt16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt16(0,e)}setInt16(t,e,n){let i=w(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt16(0,e,n),this.write(i,t)}getInt32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt32(0,e)}setInt32(t,e,n){let i=w(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt32(0,e,n),this.write(i,t)}getBigInt64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigInt64(0,e)}setBigInt64(t,e,n){let i=w(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigInt64(0,e,n),this.write(i,t)}getUint8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint8(0)}setUint8(t,e){let n=b(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setUint8(0,e),this.write(n,t)}getUint16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint16(0,e)}setUint16(t,e,n){let i=w(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint16(0,e,n),this.write(i,t)}getUint32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint32(0,e)}setUint32(t,e,n){let i=w(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint32(0,e,n),this.write(i,t)}getBigUint64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigUint64(0,e)}setBigUint64(t,e,n){let i=w(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigUint64(0,e,n),this.write(i,t)}getFloat32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat32(0,e)}setFloat32(t,e,n){let i=w(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat32(0,e,n),this.write(i,t)}getFloat64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat64(0,e)}setFloat64(t,e,n){let i=w(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat64(0,e,n),this.write(i,t)}equals(t){if(t==null||!(t instanceof r)||t.bufs.length!==this.bufs.length)return!1;for(let e=0;e<this.bufs.length;e++)if(!k(this.bufs[e],t.bufs[e]))return!1;return!0}static fromUint8Arrays(t,e){let n=new r;return n.bufs=t,e==null&&(e=t.reduce((i,s)=>i+s.byteLength,0)),n.length=e,n}};function E(r){return r[Symbol.asyncIterator]!=null}var F=r=>{let t=y(r),e=b(t);return B(r,e),F.bytes=t,e};F.bytes=0;function R(r,t){t=t??{};let e=t.lengthEncoder??F;function*n(i){let s=e(i.byteLength);s instanceof Uint8Array?yield s:yield*s,i instanceof Uint8Array?yield i:yield*i}return E(r)?async function*(){for await(let i of r)yield*n(i)}():function*(){for(let i of r)yield*n(i)}()}R.single=(r,t)=>{t=t??{};let e=t.lengthEncoder??F;return new d(e(r.byteLength),r)};var p=Z(j(),1);var at=8,ht=1024*1024*4,L;(function(r){r[r.LENGTH=0]="LENGTH",r[r.DATA=1]="DATA"})(L||(L={}));var V=r=>{let t=I(r);return V.bytes=y(t),t};V.bytes=0;function x(r,t){let e=new d,n=L.LENGTH,i=-1,s=t?.lengthDecoder??V,a=t?.maxLengthLength??at,u=t?.maxDataLength??ht;function*g(){for(;e.byteLength>0;){if(n===L.LENGTH)try{if(i=s(e),i<0)throw(0,p.default)(new Error("invalid message length"),"ERR_INVALID_MSG_LENGTH");if(i>u)throw(0,p.default)(new Error("message length too long"),"ERR_MSG_DATA_TOO_LONG");let h=s.bytes;e.consume(h),t?.onLength!=null&&t.onLength(i),n=L.DATA}catch(h){if(h instanceof RangeError){if(e.byteLength>a)throw(0,p.default)(new Error("message length length too long"),"ERR_MSG_LENGTH_TOO_LONG");break}throw h}if(n===L.DATA){if(e.byteLength<i)break;let h=e.sublist(0,i);e.consume(i),t?.onData!=null&&t.onData(h),yield h,n=L.LENGTH}}}return E(r)?async function*(){for await(let h of r)e.append(h),yield*g();if(e.byteLength>0)throw(0,p.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}():function*(){for(let h of r)e.append(h),yield*g();if(e.byteLength>0)throw(0,p.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}()}x.fromReader=(r,t)=>{let e=1,n=async function*(){for(;;)try{let{done:s,value:a}=await r.next(e);if(s===!0)return;a!=null&&(yield a)}catch(s){if(s.code==="ERR_UNDER_READ")return{done:!0,value:null};throw s}finally{e=1}}();return x(n,{...t??{},onLength:s=>{e=s}})};return $(ft);})(); | ||
return ItLengthPrefixed})); |
@@ -10,4 +10,4 @@ import { Uint8ArrayList } from 'uint8arraylist'; | ||
lengthDecoder?: LengthDecoderFunction; | ||
onData?: (data: Uint8ArrayList) => void; | ||
onLength?: (length: number) => void; | ||
onData?(data: Uint8ArrayList): void; | ||
onLength?(length: number): void; | ||
maxLengthLength?: number; | ||
@@ -14,0 +14,0 @@ maxDataLength?: number; |
@@ -0,1 +1,45 @@ | ||
/** | ||
* @packageDocumentation | ||
* | ||
* Encode/decode streams of bytes with length-prefixes. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { pipe } from 'it-pipe' | ||
* import * as lp from 'it-length-prefixed' | ||
* | ||
* const encoded = [] | ||
* | ||
* // encode | ||
* await pipe( | ||
* [uint8ArrayFromString('hello world')], | ||
* (source) => lp.encode(source), | ||
* async source => { | ||
* for await (const chunk of source) { | ||
* encoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
* } | ||
* } | ||
* ) | ||
* | ||
* console.log(encoded) | ||
* // => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>] | ||
* | ||
* const decoded = [] | ||
* | ||
* // decode | ||
* await pipe( | ||
* encoded, // e.g. from above | ||
* (source) => lp.decode(source), | ||
* async source => { | ||
* for await (const chunk of source) { | ||
* decoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
* } | ||
* } | ||
* ) | ||
* | ||
* console.log(decoded) | ||
* // => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>] | ||
* ``` | ||
*/ | ||
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
@@ -2,0 +46,0 @@ export { encode } from './encode.js'; |
@@ -0,3 +1,47 @@ | ||
/** | ||
* @packageDocumentation | ||
* | ||
* Encode/decode streams of bytes with length-prefixes. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { pipe } from 'it-pipe' | ||
* import * as lp from 'it-length-prefixed' | ||
* | ||
* const encoded = [] | ||
* | ||
* // encode | ||
* await pipe( | ||
* [uint8ArrayFromString('hello world')], | ||
* (source) => lp.encode(source), | ||
* async source => { | ||
* for await (const chunk of source) { | ||
* encoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
* } | ||
* } | ||
* ) | ||
* | ||
* console.log(encoded) | ||
* // => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>] | ||
* | ||
* const decoded = [] | ||
* | ||
* // decode | ||
* await pipe( | ||
* encoded, // e.g. from above | ||
* (source) => lp.decode(source), | ||
* async source => { | ||
* for await (const chunk of source) { | ||
* decoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
* } | ||
* } | ||
* ) | ||
* | ||
* console.log(decoded) | ||
* // => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>] | ||
* ``` | ||
*/ | ||
export { encode } from './encode.js'; | ||
export { decode } from './decode.js'; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"fromReader": "https://alanshaw.github.io/it-length-prefixed/functions/decode.decode.fromReader.html", | ||
"./decode:fromReader": "https://alanshaw.github.io/it-length-prefixed/functions/decode.decode.fromReader.html", | ||
"DecoderOptions": "https://alanshaw.github.io/it-length-prefixed/interfaces/decode.DecoderOptions.html", | ||
@@ -14,8 +12,6 @@ "./decode:DecoderOptions": "https://alanshaw.github.io/it-length-prefixed/interfaces/decode.DecoderOptions.html", | ||
"./decode:MAX_LENGTH_LENGTH": "https://alanshaw.github.io/it-length-prefixed/variables/decode.MAX_LENGTH_LENGTH.html", | ||
"decode": "https://alanshaw.github.io/it-length-prefixed/functions/decode.decode-1.html", | ||
"./decode:decode": "https://alanshaw.github.io/it-length-prefixed/functions/decode.decode-1.html", | ||
"single": "https://alanshaw.github.io/it-length-prefixed/functions/encode.encode.single.html", | ||
"./encode:single": "https://alanshaw.github.io/it-length-prefixed/functions/encode.encode.single.html", | ||
"encode": "https://alanshaw.github.io/it-length-prefixed/functions/encode.encode-1.html", | ||
"./encode:encode": "https://alanshaw.github.io/it-length-prefixed/functions/encode.encode-1.html", | ||
"decode": "https://alanshaw.github.io/it-length-prefixed/functions/decode.decode.html", | ||
"./decode:decode": "https://alanshaw.github.io/it-length-prefixed/functions/decode.decode.html", | ||
"encode": "https://alanshaw.github.io/it-length-prefixed/functions/encode.encode.html", | ||
"./encode:encode": "https://alanshaw.github.io/it-length-prefixed/functions/encode.encode.html", | ||
"LengthDecoderFunction": "https://alanshaw.github.io/it-length-prefixed/interfaces/index.LengthDecoderFunction.html", | ||
@@ -22,0 +18,0 @@ ".:LengthDecoderFunction": "https://alanshaw.github.io/it-length-prefixed/interfaces/index.LengthDecoderFunction.html", |
{ | ||
"name": "it-length-prefixed", | ||
"version": "9.0.4", | ||
"version": "9.0.5", | ||
"description": "Streaming length prefixed buffers with async iterables", | ||
@@ -15,2 +15,6 @@ "author": "Alan Shaw", | ||
}, | ||
"publishConfig": { | ||
"access": "public", | ||
"provenance": true | ||
}, | ||
"keywords": [ | ||
@@ -24,6 +28,2 @@ "async", | ||
], | ||
"engines": { | ||
"node": ">=16.0.0", | ||
"npm": ">=7.0.0" | ||
}, | ||
"type": "module", | ||
@@ -70,2 +70,3 @@ "types": "./dist/src/index.d.ts", | ||
"parserOptions": { | ||
"project": true, | ||
"sourceType": "module" | ||
@@ -186,3 +187,3 @@ }, | ||
"devDependencies": { | ||
"aegir": "^40.0.0", | ||
"aegir": "^44.1.0", | ||
"iso-random-stream": "^2.0.0", | ||
@@ -198,3 +199,7 @@ "it-all": "^3.0.0", | ||
"random-int": "^3.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0", | ||
"npm": ">=7.0.0" | ||
} | ||
} |
111
README.md
@@ -1,2 +0,2 @@ | ||
# it-length-prefixed <!-- omit in toc --> | ||
# it-length-prefixed | ||
@@ -8,33 +8,23 @@ [![codecov](https://img.shields.io/codecov/c/github/alanshaw/it-length-prefixed.svg?style=flat-square)](https://codecov.io/gh/alanshaw/it-length-prefixed) | ||
## Table of contents <!-- omit in toc --> | ||
# About | ||
- [Install](#install) | ||
- [Browser `<script>` tag](#browser-script-tag) | ||
- [Usage](#usage) | ||
- [API](#api) | ||
- [`encode(source, [opts])`](#encodesource-opts) | ||
- [`encode.single(chunk, [opts])`](#encodesinglechunk-opts) | ||
- [`decode(source, [opts])`](#decodesource-opts) | ||
- [`decode.fromReader(reader, [opts])`](#decodefromreaderreader-opts) | ||
- [Contribute](#contribute) | ||
- [API Docs](#api-docs) | ||
- [License](#license) | ||
- [Contribution](#contribution) | ||
<!-- | ||
## Install | ||
!IMPORTANT! | ||
```console | ||
$ npm i it-length-prefixed | ||
``` | ||
Everything in this README between "# About" and "# Install" is automatically | ||
generated and will be overwritten the next time the doc generator is run. | ||
### Browser `<script>` tag | ||
To make changes to this section, please update the @packageDocumentation section | ||
of src/index.js or src/index.ts | ||
Loading this module through a script tag will make it's exports available as `ItLengthPrefixed` in the global namespace. | ||
To experiment with formatting, please run "npm run docs" from the root of this | ||
repo and examine the changes made. | ||
```html | ||
<script src="https://unpkg.com/it-length-prefixed/dist/index.min.js"></script> | ||
``` | ||
--> | ||
## Usage | ||
Encode/decode streams of bytes with length-prefixes. | ||
## Example | ||
```js | ||
@@ -77,78 +67,15 @@ import { pipe } from 'it-pipe' | ||
## API | ||
# API Docs | ||
```js | ||
import { | ||
encode, decode | ||
} from 'it-length-prefixed' | ||
import { | ||
encode | ||
} from 'it-length-prefixed/encode' | ||
import { | ||
decode, | ||
MAX_LENGTH_LENGTH, | ||
MAX_DATA_LENGTH | ||
} from 'it-length-prefixed/decode' | ||
``` | ||
### `encode(source, [opts])` | ||
- `source: Iterable<Uint8ArrayList | Uint8Array> | AsyncIterable<Uint8ArrayList | Uint8Array>` source to read bytes from | ||
- `opts: Object`, optional | ||
- `lengthEncoder: Function`: A function that encodes the length that will prefix each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) encoder. It is passed a `value` to encode, an (optional) `target` buffer to write to and an (optional) `offset` to start writing from. The function should encode the `value` into the `target` (or alloc a new Buffer if not specified), set the `lengthEncoder.bytes` value (the number of bytes written) and return the `target`. | ||
Returns `Generator` or `AsyncGenerator` that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects. All messages will be prefixed with a length, determined by the `lengthEncoder` function. | ||
### `encode.single(chunk, [opts])` | ||
- `chunk: Buffer|Uint8ArrayList` chunk to encode | ||
- `opts: Object`, optional | ||
- `lengthEncoder: Function`: See description above. Note that this encoder will *not* be passed a `target` or `offset` and so will need to allocate a buffer to write to. | ||
Returns a `Uint8ArrayList` containing the encoded chunk. | ||
### `decode(source, [opts])` | ||
- `source: Iterable<Uint8ArrayList | Uint8Array> | AsyncIterable<Uint8ArrayList | Uint8Array>` source to read bytes from | ||
- `opts: Object`, optional | ||
- `maxLengthLength`: If provided, will not decode messages whose length section exceeds the size specified, if omitted will use the default of 147 bytes. | ||
- `maxDataLength`: If provided, will not decode messages whose data section exceeds the size specified, if omitted will use the default of 4MB. | ||
- `onLength(len: Number)`: Called for every length prefix that is decoded from the stream | ||
- `onData(data: Uint8ArrayList)`: Called for every chunk of data that is decoded from the stream | ||
- `lengthDecoder: Function`: A function that decodes the length that prefixes each message. By default this is a [`varint`](https://www.npmjs.com/package/varint) decoder. It is passed some `data` to decode which is a [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist). The function should decode the length, set the `lengthDecoder.bytes` value (the number of bytes read) and return the length. If the length cannot be decoded, the function should throw a `RangeError`. | ||
Returns `Generator` or `AsyncGenerator` that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects. | ||
### `decode.fromReader(reader, [opts])` | ||
Behaves like `decode` except it only reads the exact number of bytes needed for each message in `reader`. | ||
- `reader: Reader`: An [it-reader](https://github.com/alanshaw/it-reader) | ||
- `opts: Object`, optional | ||
- `maxLengthLength`: If provided, will not decode messages whose length section exceeds the size specified, if omitted will use the default of 147 bytes. | ||
- `maxDataLength`: If provided, will not decode messages whose data section exceeds the size specified, if omitted will use the default of 4MB. | ||
- `onData(data: Uint8ArrayList)`: Called for every chunk of data that is decoded from the stream | ||
- `lengthEncoder: Function`: See description above. | ||
Returns `Generator` or `AsyncGenerator` that yields [`Uint8ArrayList`](https://www.npmjs.com/package/uint8arraylist) objects. | ||
## Contribute | ||
PRs and issues gladly accepted! Check out the [issues](https://github.com/alanshaw/it-length-prefixed/issues). | ||
## API Docs | ||
- <https://alanshaw.github.io/it-length-prefixed> | ||
## License | ||
# License | ||
Licensed under either of | ||
- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>) | ||
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>) | ||
- Apache 2.0, ([LICENSE-APACHE](https://github.com/alanshaw/it-length-prefixed/LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>) | ||
- MIT ([LICENSE-MIT](https://github.com/alanshaw/it-length-prefixed/LICENSE-MIT) / <http://opensource.org/licenses/MIT>) | ||
## Contribution | ||
# Contribution | ||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions. |
@@ -17,4 +17,4 @@ /* eslint max-depth: ["error", 6] */ | ||
lengthDecoder?: LengthDecoderFunction | ||
onData?: (data: Uint8ArrayList) => void | ||
onLength?: (length: number) => void | ||
onData?(data: Uint8ArrayList): void | ||
onLength?(length: number): void | ||
maxLengthLength?: number | ||
@@ -21,0 +21,0 @@ maxDataLength?: number |
@@ -0,1 +1,46 @@ | ||
/** | ||
* @packageDocumentation | ||
* | ||
* Encode/decode streams of bytes with length-prefixes. | ||
* | ||
* @example | ||
* | ||
* ```js | ||
* import { pipe } from 'it-pipe' | ||
* import * as lp from 'it-length-prefixed' | ||
* | ||
* const encoded = [] | ||
* | ||
* // encode | ||
* await pipe( | ||
* [uint8ArrayFromString('hello world')], | ||
* (source) => lp.encode(source), | ||
* async source => { | ||
* for await (const chunk of source) { | ||
* encoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
* } | ||
* } | ||
* ) | ||
* | ||
* console.log(encoded) | ||
* // => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>] | ||
* | ||
* const decoded = [] | ||
* | ||
* // decode | ||
* await pipe( | ||
* encoded, // e.g. from above | ||
* (source) => lp.decode(source), | ||
* async source => { | ||
* for await (const chunk of source) { | ||
* decoded.push(chunk.slice()) // (.slice converts Uint8ArrayList to Uint8Array) | ||
* } | ||
* } | ||
* ) | ||
* | ||
* console.log(decoded) | ||
* // => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>] | ||
* ``` | ||
*/ | ||
import type { Uint8ArrayList } from 'uint8arraylist' | ||
@@ -2,0 +47,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
646
48213
80