it-length-prefixed
Advanced tools
Comparing version 9.1.1 to 10.0.0
(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 T=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var j=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var z=(r,t)=>{for(var e in t)T(r,e,{get:t[e],enumerable:!0})},J=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of j(t))!Y.call(r,i)&&i!==e&&T(r,i,{get:()=>t[i],enumerable:!(n=P(t,i))||n.enumerable});return r};var K=r=>J(T({},"__esModule",{value:!0}),r);var it={};z(it,{decode:()=>N,encode:()=>G});function w(r=0){return new Uint8Array(r)}function b(r=0){return new Uint8Array(r)}var Q=Math.pow(2,7),W=Math.pow(2,14),Z=Math.pow(2,21),_=Math.pow(2,28),R=Math.pow(2,35),V=Math.pow(2,42),O=Math.pow(2,49),o=128,c=127;function y(r){if(r<Q)return 1;if(r<W)return 2;if(r<Z)return 3;if(r<_)return 4;if(r<R)return 5;if(r<V)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 $(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 v(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 tt(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)*_,e<o)||(e=r[t+5],n+=(e&c)*R,e<o)||(e=r[t+6],n+=(e&c)*V,e<o)||(e=r[t+7],n+=(e&c)*O,e<o))return n;throw new RangeError("Could not decode varint")}function et(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)*_,e<o)||(e=r.get(t+5),n+=(e&c)*R,e<o)||(e=r.get(t+6),n+=(e&c)*V,e<o)||(e=r.get(t+7),n+=(e&c)*O,e<o))return n;throw new RangeError("Could not decode varint")}function S(r,t,e=0){return t==null&&(t=b(y(r))),t instanceof Uint8Array?$(r,t,e):v(r,t,e)}function H(r,t=0){return r instanceof Uint8Array?tt(r,t):et(r,t)}function M(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 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 q=Symbol.for("@achingbrain/uint8arraylist");function X(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 A(r){return!!r?.[q]}var d=class r{bufs;length;[q]=!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(A(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(A(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=X(this.bufs,t);return e.buf[e.index]}set(t,e){let n=X(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(A(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 M(n,i)}subarray(t,e){let{bufs:n,length:i}=this._subList(t,e);return n.length===1?n[0]:M(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(!A(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 p=h;p>=0;p--){let B=this.get(f+p);if(n[p]!==B){l=Math.max(1,p-u[B]);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(!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 U(r){return r[Symbol.asyncIterator]!=null}var x=r=>{let t=y(r),e=b(t);return S(r,e),x.bytes=t,e};x.bytes=0;function G(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 U(r)?async function*(){for await(let i of r)yield*n(i)}():function*(){for(let i of r)yield*n(i)}()}G.single=(r,t)=>{t=t??{};let e=t.lengthEncoder??x;return new d(e(r.byteLength),r)};var F=class extends Error{name="InvalidMessageLengthError";code="ERR_INVALID_MSG_LENGTH"},E=class extends Error{name="InvalidDataLengthError";code="ERR_MSG_DATA_TOO_LONG"},D=class extends Error{name="InvalidDataLengthLengthError";code="ERR_MSG_LENGTH_TOO_LONG"},m=class extends Error{name="UnexpectedEOFError";code="ERR_UNEXPECTED_EOF"};var nt=8,rt=1024*1024*4,L;(function(r){r[r.LENGTH=0]="LENGTH",r[r.DATA=1]="DATA"})(L||(L={}));var I=r=>{let t=H(r);return I.bytes=y(t),t};I.bytes=0;function N(r,t){let e=new d,n=L.LENGTH,i=-1,s=t?.lengthDecoder??I,a=t?.maxLengthLength??nt,u=t?.maxDataLength??rt;function*g(){for(;e.byteLength>0;){if(n===L.LENGTH)try{if(i=s(e),i<0)throw new F("Invalid message length");if(i>u)throw new E("Message length 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 new D("Message length 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 U(r)?async function*(){for await(let h of r)e.append(h),yield*g();if(e.byteLength>0)throw new m("Unexpected end of input")}():function*(){for(let h of r)e.append(h),yield*g();if(e.byteLength>0)throw new m("Unexpected end of input")}()}N.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 N(n,{...t??{},onLength:s=>{e=s}})};return K(it);})(); | ||
"use strict";var ItLengthPrefixed=(()=>{var _=Object.defineProperty;var Y=Object.getOwnPropertyDescriptor;var z=Object.getOwnPropertyNames;var J=Object.prototype.hasOwnProperty;var K=(r,t)=>{for(var e in t)_(r,e,{get:t[e],enumerable:!0})},Q=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of z(t))!J.call(r,i)&&i!==e&&_(r,i,{get:()=>t[i],enumerable:!(n=Y(t,i))||n.enumerable});return r};var W=r=>Q(_({},"__esModule",{value:!0}),r);var st={};K(st,{decode:()=>T,encode:()=>I});function w(r=0){return new Uint8Array(r)}function l(r=0){return new Uint8Array(r)}var Z=Math.pow(2,7),$=Math.pow(2,14),v=Math.pow(2,21),M=Math.pow(2,28),G=Math.pow(2,35),R=Math.pow(2,42),V=Math.pow(2,49),o=128,c=127;function y(r){if(r<Z)return 1;if(r<$)return 2;if(r<v)return 3;if(r<M)return 4;if(r<G)return 5;if(r<R)return 6;if(r<V)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function tt(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 et(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 nt(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)*M,e<o)||(e=r[t+5],n+=(e&c)*G,e<o)||(e=r[t+6],n+=(e&c)*R,e<o)||(e=r[t+7],n+=(e&c)*V,e<o))return n;throw new RangeError("Could not decode varint")}function rt(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)*M,e<o)||(e=r.get(t+5),n+=(e&c)*G,e<o)||(e=r.get(t+6),n+=(e&c)*R,e<o)||(e=r.get(t+7),n+=(e&c)*V,e<o))return n;throw new RangeError("Could not decode varint")}function S(r,t,e=0){return t==null&&(t=l(y(r))),t instanceof Uint8Array?tt(r,t,e):et(r,t,e)}function X(r,t=0){return r instanceof Uint8Array?nt(r,t):rt(r,t)}function O(r,t){t==null&&(t=r.reduce((i,s)=>i+s.length,0));let e=l(t),n=0;for(let i of r)e.set(i,n),n+=i.length;return 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 q(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 x(r){return!!r?.[P]}var d=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(x(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(x(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=q(this.bufs,t);return e.buf[e.index]}set(t,e){let n=q(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(x(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,b=e>u&&e<=g;if(h&&b){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(b){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(!x(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,b;for(let f=e;f<=g;f+=b){b=0;for(let m=h;m>=0;m--){let H=this.get(f+m);if(n[m]!==H){b=Math.max(1,m-u[H]);break}}if(b===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=l(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=l(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(!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}};var U=class extends Error{name="InvalidMessageLengthError";code="ERR_INVALID_MSG_LENGTH"},p=class extends Error{name="InvalidDataLengthError";code="ERR_MSG_DATA_TOO_LONG"},E=class extends Error{name="InvalidDataLengthLengthError";code="ERR_MSG_LENGTH_TOO_LONG"},A=class extends Error{name="UnexpectedEOFError";code="ERR_UNEXPECTED_EOF"};function D(r){return r[Symbol.asyncIterator]!=null}function j(r,t){if(r.byteLength>t)throw new p("Message length too long")}var N=r=>{let t=y(r),e=l(t);return S(r,e),N.bytes=t,e};N.bytes=0;function I(r,t){t=t??{};let e=t.lengthEncoder??N,n=t?.maxDataLength??4194304;function*i(s){j(s,n);let a=e(s.byteLength);a instanceof Uint8Array?yield a:yield*a,s instanceof Uint8Array?yield s:yield*s}return D(r)?async function*(){for await(let s of r)yield*i(s)}():function*(){for(let s of r)yield*i(s)}()}I.single=(r,t)=>{t=t??{};let e=t.lengthEncoder??N,n=t?.maxDataLength??4194304;return j(r,n),new d(e(r.byteLength),r)};var L;(function(r){r[r.LENGTH=0]="LENGTH",r[r.DATA=1]="DATA"})(L||(L={}));var B=r=>{let t=X(r);return B.bytes=y(t),t};B.bytes=0;function T(r,t){let e=new d,n=L.LENGTH,i=-1,s=t?.lengthDecoder??B,a=t?.maxLengthLength??8,u=t?.maxDataLength??4194304;function*g(){for(;e.byteLength>0;){if(n===L.LENGTH)try{if(i=s(e),i<0)throw new U("Invalid message length");if(i>u)throw new p("Message length 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 new E("Message length 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 D(r)?async function*(){for await(let h of r)e.append(h),yield*g();if(e.byteLength>0)throw new A("Unexpected end of input")}():function*(){for(let h of r)e.append(h),yield*g();if(e.byteLength>0)throw new A("Unexpected end of input")}()}T.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 T(n,{...t??{},onLength:s=>{e=s}})};return W(st);})(); | ||
return ItLengthPrefixed})); |
@@ -22,4 +22,2 @@ import { Uint8ArrayList } from 'uint8arraylist'; | ||
} | ||
export declare const MAX_LENGTH_LENGTH = 8; | ||
export declare const MAX_DATA_LENGTH: number; | ||
export declare function decode(source: Iterable<Uint8ArrayList | Uint8Array>, options?: DecoderOptions): Generator<Uint8ArrayList, void, unknown>; | ||
@@ -26,0 +24,0 @@ export declare function decode(source: Source<Uint8ArrayList | Uint8Array>, options?: DecoderOptions): AsyncGenerator<Uint8ArrayList, void, unknown>; |
/* eslint max-depth: ["error", 6] */ | ||
import * as varint from 'uint8-varint'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import { MAX_DATA_LENGTH, MAX_LENGTH_LENGTH } from './constants.js'; | ||
import { InvalidDataLengthError, InvalidDataLengthLengthError, InvalidMessageLengthError, UnexpectedEOFError } from './errors.js'; | ||
import { isAsyncIterable } from './utils.js'; | ||
// Maximum length of the length section of the message | ||
export const MAX_LENGTH_LENGTH = 8; // Varint.encode(Number.MAX_SAFE_INTEGER).length | ||
// Maximum length of the data section of the message | ||
export const MAX_DATA_LENGTH = 1024 * 1024 * 4; | ||
var ReadMode; | ||
@@ -11,0 +8,0 @@ (function (ReadMode) { |
@@ -6,2 +6,3 @@ import { Uint8ArrayList } from 'uint8arraylist'; | ||
lengthEncoder?: LengthEncoderFunction; | ||
maxDataLength?: number; | ||
} | ||
@@ -8,0 +9,0 @@ export declare function encode(source: Iterable<Uint8ArrayList | Uint8Array>, options?: EncoderOptions): Generator<Uint8Array, void, undefined>; |
import * as varint from 'uint8-varint'; | ||
import { Uint8ArrayList } from 'uint8arraylist'; | ||
import { allocUnsafe } from 'uint8arrays/alloc'; | ||
import { MAX_DATA_LENGTH } from './constants.js'; | ||
import { InvalidDataLengthError } from './errors.js'; | ||
import { isAsyncIterable } from './utils.js'; | ||
// Helper function to validate the chunk size against maxDataLength | ||
function validateMaxDataLength(chunk, maxDataLength) { | ||
if (chunk.byteLength > maxDataLength) { | ||
throw new InvalidDataLengthError('Message length too long'); | ||
} | ||
} | ||
const defaultEncoder = (length) => { | ||
@@ -16,3 +24,5 @@ const lengthLength = varint.encodingLength(length); | ||
const encodeLength = options.lengthEncoder ?? defaultEncoder; | ||
const maxDataLength = options?.maxDataLength ?? MAX_DATA_LENGTH; | ||
function* maybeYield(chunk) { | ||
validateMaxDataLength(chunk, maxDataLength); | ||
// length + data | ||
@@ -51,4 +61,6 @@ const length = encodeLength(chunk.byteLength); | ||
const encodeLength = options.lengthEncoder ?? defaultEncoder; | ||
const maxDataLength = options?.maxDataLength ?? MAX_DATA_LENGTH; | ||
validateMaxDataLength(chunk, maxDataLength); | ||
return new Uint8ArrayList(encodeLength(chunk.byteLength), chunk); | ||
}; | ||
//# sourceMappingURL=encode.js.map |
@@ -8,6 +8,2 @@ { | ||
"./decode:ReadState": "https://alanshaw.github.io/it-length-prefixed/interfaces/decode.ReadState.html", | ||
"MAX_DATA_LENGTH": "https://alanshaw.github.io/it-length-prefixed/variables/decode.MAX_DATA_LENGTH.html", | ||
"./decode:MAX_DATA_LENGTH": "https://alanshaw.github.io/it-length-prefixed/variables/decode.MAX_DATA_LENGTH.html", | ||
"MAX_LENGTH_LENGTH": "https://alanshaw.github.io/it-length-prefixed/variables/decode.MAX_LENGTH_LENGTH.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.html", | ||
@@ -14,0 +10,0 @@ "./decode:decode": "https://alanshaw.github.io/it-length-prefixed/functions/decode.decode.html", |
{ | ||
"name": "it-length-prefixed", | ||
"version": "9.1.1", | ||
"version": "10.0.0", | ||
"description": "Streaming length prefixed buffers with async iterables", | ||
@@ -5,0 +5,0 @@ "author": "Alan Shaw", |
@@ -5,2 +5,3 @@ /* eslint max-depth: ["error", 6] */ | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { MAX_DATA_LENGTH, MAX_LENGTH_LENGTH } from './constants.js' | ||
import { InvalidDataLengthError, InvalidDataLengthLengthError, InvalidMessageLengthError, UnexpectedEOFError } from './errors.js' | ||
@@ -32,7 +33,2 @@ import { isAsyncIterable } from './utils.js' | ||
// Maximum length of the length section of the message | ||
export const MAX_LENGTH_LENGTH = 8 // Varint.encode(Number.MAX_SAFE_INTEGER).length | ||
// Maximum length of the data section of the message | ||
export const MAX_DATA_LENGTH = 1024 * 1024 * 4 | ||
enum ReadMode { | ||
@@ -39,0 +35,0 @@ LENGTH, |
import * as varint from 'uint8-varint' | ||
import { Uint8ArrayList } from 'uint8arraylist' | ||
import { allocUnsafe } from 'uint8arrays/alloc' | ||
import { MAX_DATA_LENGTH } from './constants.js' | ||
import { InvalidDataLengthError } from './errors.js' | ||
import { isAsyncIterable } from './utils.js' | ||
@@ -10,4 +12,12 @@ import type { LengthEncoderFunction } from './index.js' | ||
lengthEncoder?: LengthEncoderFunction | ||
maxDataLength?: number | ||
} | ||
// Helper function to validate the chunk size against maxDataLength | ||
function validateMaxDataLength (chunk: Uint8Array | Uint8ArrayList, maxDataLength: number): void { | ||
if (chunk.byteLength > maxDataLength) { | ||
throw new InvalidDataLengthError('Message length too long') | ||
} | ||
} | ||
const defaultEncoder: LengthEncoderFunction = (length) => { | ||
@@ -31,4 +41,7 @@ const lengthLength = varint.encodingLength(length) | ||
const encodeLength = options.lengthEncoder ?? defaultEncoder | ||
const maxDataLength = options?.maxDataLength ?? MAX_DATA_LENGTH | ||
function * maybeYield (chunk: Uint8Array | Uint8ArrayList): Generator<Uint8Array, void, undefined> { | ||
validateMaxDataLength(chunk, maxDataLength) | ||
// length + data | ||
@@ -70,3 +83,6 @@ const length = encodeLength(chunk.byteLength) | ||
const encodeLength = options.lengthEncoder ?? defaultEncoder | ||
const maxDataLength = options?.maxDataLength ?? MAX_DATA_LENGTH | ||
validateMaxDataLength(chunk, maxDataLength) | ||
return new Uint8ArrayList( | ||
@@ -73,0 +89,0 @@ encodeLength(chunk.byteLength), |
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
Sorry, the diff of this file is not supported yet
53153
35
759