@libp2p/multistream-select 4.0.6-6625a27fc → 4.0.6-68db79f6b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.min.js +14 -12
- package/dist/src/handle.d.ts.map +1 -1
- package/dist/src/handle.js +14 -7
- package/dist/src/handle.js.map +1 -1
- package/dist/src/index.d.ts +3 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/multistream.d.ts +2 -2
- package/dist/src/multistream.d.ts.map +1 -1
- package/dist/src/multistream.js.map +1 -1
- package/dist/src/select.d.ts +8 -10
- package/dist/src/select.d.ts.map +1 -1
- package/dist/src/select.js +167 -19
- package/dist/src/select.js.map +1 -1
- package/package.json +10 -4
- package/src/handle.ts +15 -7
- package/src/index.ts +3 -2
- package/src/multistream.ts +2 -2
- package/src/select.ts +208 -20
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[](http://libp2p.io/)
|
|
2
2
|
[](https://discuss.libp2p.io)
|
|
3
3
|
[](https://codecov.io/gh/libp2p/js-libp2p)
|
|
4
|
-
[](https://github.com/libp2p/js-libp2p/actions/workflows/main.yml?query=branch%3Amain)
|
|
5
5
|
|
|
6
6
|
> JavaScript implementation of multistream-select
|
|
7
7
|
|
package/dist/index.min.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Libp2PMultistreamSelect = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
|
2
|
-
"use strict";var Libp2PMultistreamSelect=(()=>{var It=Object.create;var oe=Object.defineProperty;var _t=Object.getOwnPropertyDescriptor;var Ft=Object.getOwnPropertyNames;var Mt=Object.getPrototypeOf,Bt=Object.prototype.hasOwnProperty;var Pt=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports),y=(r,e)=>{for(var t in e)oe(r,t,{get:e[t],enumerable:!0})},Je=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of Ft(e))!Bt.call(r,o)&&o!==t&&oe(r,o,{get:()=>e[o],enumerable:!(n=_t(e,o))||n.enumerable});return r};var Vt=(r,e,t)=>(t=r!=null?It(Mt(r)):{},Je(e||!r||!r.__esModule?oe(t,"default",{value:r,enumerable:!0}):t,r)),zt=r=>Je(oe({},"__esModule",{value:!0}),r);var ot=Pt((Vn,nt)=>{"use strict";function rt(r,e){for(let t in e)Object.defineProperty(r,t,{value:e[t],enumerable:!0,configurable:!0});return r}function Jt(r,e,t){if(!r||typeof r=="string")throw new TypeError("Please pass an Error to err-code");t||(t={}),typeof e=="object"&&(t=e,e=""),e&&(t.code=e);try{return rt(r,t)}catch{t.message=r.message,t.stack=r.stack;let o=function(){};return o.prototype=Object.create(Object.getPrototypeOf(r)),rt(new o,t)}}nt.exports=Jt});var fn={};y(fn,{PROTOCOL_ID:()=>x,handle:()=>Ct,lazySelect:()=>Nt,select:()=>Ot});var x="/multistream/1.0.0";var B=class extends Error{code;props;constructor(e,t,n){super(e),this.code=t,this.name=n?.name??"CodeError",this.props=n??{}}};function P(r){return globalThis.Buffer!=null?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):r}function N(r=0){return globalThis.Buffer?.alloc!=null?P(globalThis.Buffer.alloc(r)):new Uint8Array(r)}function S(r=0){return globalThis.Buffer?.allocUnsafe!=null?P(globalThis.Buffer.allocUnsafe(r)):new Uint8Array(r)}function me(r,e){e==null&&(e=r.reduce((o,s)=>o+s.length,0));let t=S(e),n=0;for(let o of r)t.set(o,n),n+=o.length;return P(t)}function We(r,e){if(r===e)return!0;if(r.byteLength!==e.byteLength)return!1;for(let t=0;t<r.byteLength;t++)if(r[t]!==e[t])return!1;return!0}var Ke=Symbol.for("@achingbrain/uint8arraylist");function Ye(r,e){if(e==null||e<0)throw new RangeError("index is out of bounds");let t=0;for(let n of r){let o=t+n.byteLength;if(e<o)return{buf:n,index:e-t};t=o}throw new RangeError("index is out of bounds")}function se(r){return!!r?.[Ke]}var b=class r{constructor(...e){Object.defineProperty(this,Ke,{value:!0}),this.bufs=[],this.length=0,e.length>0&&this.appendAll(e)}*[Symbol.iterator](){yield*this.bufs}get byteLength(){return this.length}append(...e){this.appendAll(e)}appendAll(e){let t=0;for(let n of e)if(n instanceof Uint8Array)t+=n.byteLength,this.bufs.push(n);else if(se(n))t+=n.byteLength,this.bufs.push(...n.bufs);else throw new Error("Could not append value, must be an Uint8Array or a Uint8ArrayList");this.length+=t}prepend(...e){this.prependAll(e)}prependAll(e){let t=0;for(let n of e.reverse())if(n instanceof Uint8Array)t+=n.byteLength,this.bufs.unshift(n);else if(se(n))t+=n.byteLength,this.bufs.unshift(...n.bufs);else throw new Error("Could not prepend value, must be an Uint8Array or a Uint8ArrayList");this.length+=t}get(e){let t=Ye(this.bufs,e);return t.buf[t.index]}set(e,t){let n=Ye(this.bufs,e);n.buf[n.index]=t}write(e,t=0){if(e instanceof Uint8Array)for(let n=0;n<e.length;n++)this.set(t+n,e[n]);else if(se(e))for(let n=0;n<e.length;n++)this.set(t+n,e.get(n));else throw new Error("Could not write value, must be an Uint8Array or a Uint8ArrayList")}consume(e){if(e=Math.trunc(e),!(Number.isNaN(e)||e<=0)){if(e===this.byteLength){this.bufs=[],this.length=0;return}for(;this.bufs.length>0;)if(e>=this.bufs[0].byteLength)e-=this.bufs[0].byteLength,this.length-=this.bufs[0].byteLength,this.bufs.shift();else{this.bufs[0]=this.bufs[0].subarray(e),this.length-=e;break}}}slice(e,t){let{bufs:n,length:o}=this._subList(e,t);return me(n,o)}subarray(e,t){let{bufs:n,length:o}=this._subList(e,t);return n.length===1?n[0]:me(n,o)}sublist(e,t){let{bufs:n,length:o}=this._subList(e,t),s=new r;return s.length=o,s.bufs=n,s}_subList(e,t){if(e=e??0,t=t??this.length,e<0&&(e=this.length+e),t<0&&(t=this.length+t),e<0||t>this.length)throw new RangeError("index is out of bounds");if(e===t)return{bufs:[],length:0};if(e===0&&t===this.length)return{bufs:[...this.bufs],length:this.length};let n=[],o=0;for(let s=0;s<this.bufs.length;s++){let i=this.bufs[s],a=o,c=a+i.byteLength;if(o=c,e>=c)continue;let h=e>=a&&e<c,l=t>a&&t<=c;if(h&&l){if(e===a&&t===c){n.push(i);break}let u=e-a;n.push(i.subarray(u,u+(t-e)));break}if(h){if(e===0){n.push(i);continue}n.push(i.subarray(e-a));continue}if(l){if(t===c){n.push(i);break}n.push(i.subarray(0,t-a));break}n.push(i)}return{bufs:n,length:t-e}}indexOf(e,t=0){if(!se(e)&&!(e instanceof Uint8Array))throw new TypeError('The "value" argument must be a Uint8ArrayList or Uint8Array');let n=e instanceof Uint8Array?e:e.subarray();if(t=Number(t??0),isNaN(t)&&(t=0),t<0&&(t=this.length+t),t<0&&(t=0),e.length===0)return t>this.length?this.length:t;let o=n.byteLength;if(o===0)throw new TypeError("search must be at least 1 byte long");let s=256,i=new Int32Array(s);for(let u=0;u<s;u++)i[u]=-1;for(let u=0;u<o;u++)i[n[u]]=u;let a=i,c=this.byteLength-n.byteLength,h=n.byteLength-1,l;for(let u=t;u<=c;u+=l){l=0;for(let v=h;v>=0;v--){let re=this.get(u+v);if(n[v]!==re){l=Math.max(1,v-a[re]);break}}if(l===0)return u}return-1}getInt8(e){let t=this.subarray(e,e+1);return new DataView(t.buffer,t.byteOffset,t.byteLength).getInt8(0)}setInt8(e,t){let n=S(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setInt8(0,t),this.write(n,e)}getInt16(e,t){let n=this.subarray(e,e+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt16(0,t)}setInt16(e,t,n){let o=N(2);new DataView(o.buffer,o.byteOffset,o.byteLength).setInt16(0,t,n),this.write(o,e)}getInt32(e,t){let n=this.subarray(e,e+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt32(0,t)}setInt32(e,t,n){let o=N(4);new DataView(o.buffer,o.byteOffset,o.byteLength).setInt32(0,t,n),this.write(o,e)}getBigInt64(e,t){let n=this.subarray(e,e+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigInt64(0,t)}setBigInt64(e,t,n){let o=N(8);new DataView(o.buffer,o.byteOffset,o.byteLength).setBigInt64(0,t,n),this.write(o,e)}getUint8(e){let t=this.subarray(e,e+1);return new DataView(t.buffer,t.byteOffset,t.byteLength).getUint8(0)}setUint8(e,t){let n=S(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setUint8(0,t),this.write(n,e)}getUint16(e,t){let n=this.subarray(e,e+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint16(0,t)}setUint16(e,t,n){let o=N(2);new DataView(o.buffer,o.byteOffset,o.byteLength).setUint16(0,t,n),this.write(o,e)}getUint32(e,t){let n=this.subarray(e,e+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint32(0,t)}setUint32(e,t,n){let o=N(4);new DataView(o.buffer,o.byteOffset,o.byteLength).setUint32(0,t,n),this.write(o,e)}getBigUint64(e,t){let n=this.subarray(e,e+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigUint64(0,t)}setBigUint64(e,t,n){let o=N(8);new DataView(o.buffer,o.byteOffset,o.byteLength).setBigUint64(0,t,n),this.write(o,e)}getFloat32(e,t){let n=this.subarray(e,e+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat32(0,t)}setFloat32(e,t,n){let o=N(4);new DataView(o.buffer,o.byteOffset,o.byteLength).setFloat32(0,t,n),this.write(o,e)}getFloat64(e,t){let n=this.subarray(e,e+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat64(0,t)}setFloat64(e,t,n){let o=N(8);new DataView(o.buffer,o.byteOffset,o.byteLength).setFloat64(0,t,n),this.write(o,e)}equals(e){if(e==null||!(e instanceof r)||e.bufs.length!==this.bufs.length)return!1;for(let t=0;t<this.bufs.length;t++)if(!We(this.bufs[t],e.bufs[t]))return!1;return!0}static fromUint8Arrays(e,t){let n=new r;return n.bufs=e,t==null&&(t=e.reduce((o,s)=>o+s.byteLength,0)),n.length=t,n}};function j(){let r={};return r.promise=new Promise((e,t)=>{r.resolve=e,r.reject=t}),r}var ie=class extends Error{type;code;constructor(e,t){super(e??"The operation was aborted"),this.type="aborted",this.name="AbortError",this.code=t??"ABORT_ERR"}};async function Qe(r,e,t){if(e==null)return r;if(e.aborted)return Promise.reject(new ie(t?.errorMessage,t?.errorCode));let n,o=new ie(t?.errorMessage,t?.errorCode);try{return await Promise.race([r,new Promise((s,i)=>{n=()=>{i(o)},e.addEventListener("abort",n)})])}finally{n!=null&&e.removeEventListener("abort",n)}}var be=class{needNext;haveNext;ended;nextResult;constructor(){this.ended=!1,this.needNext=j(),this.needNext.resolve(),this.haveNext=j()}[Symbol.asyncIterator](){return this}async next(){if(this.nextResult==null&&await this.haveNext.promise,this.nextResult==null)throw new Error("Have next but next was undefined");let e=this.nextResult;return this.nextResult=void 0,this.needNext.resolve(),this.needNext=j(),e}async throw(e){return this.ended=!0,e!=null&&this.haveNext.reject(e),{done:!0,value:void 0}}async return(){let e={done:!0,value:void 0};return await this._push(void 0),e}async push(e,t){await this._push(e,t)}async end(e,t){e!=null?await this.throw(e):await this._push(void 0,t)}async _push(e,t){if(e!=null&&this.ended)throw new Error("Cannot push value onto an ended pushable");this.nextResult!=null&&await this.needNext.promise,e!=null?this.nextResult={done:!1,value:e}:(this.ended=!0,this.nextResult={done:!0,value:void 0}),this.haveNext.resolve(),this.haveNext=j(),await Qe(this.needNext.promise,t?.signal,t)}};function Ze(){return new be}var ae=class extends Error{code;constructor(e,t){super(e),this.code=t}},ge=class extends ae{type;constructor(e){super(e,"ABORT_ERR"),this.type="aborted"}};function et(r){let e=Ze();r.sink(e).catch(async s=>{await e.end(s)}),r.sink=async s=>{for await(let i of s)await e.push(i);await e.end()};let t=r.source;r.source[Symbol.iterator]!=null?t=r.source[Symbol.iterator]():r.source[Symbol.asyncIterator]!=null&&(t=r.source[Symbol.asyncIterator]());let n=new b;return{read:async(s,i)=>{i?.signal?.throwIfAborted();let a,c=new Promise((h,l)=>{a=()=>{l(new ge("Read aborted"))},i?.signal?.addEventListener("abort",a)});try{if(s==null){let{done:l,value:u}=await Promise.race([t.next(),c]);return l===!0?new b:u}for(;n.byteLength<s;){let{value:l,done:u}=await Promise.race([t.next(),c]);if(u===!0)throw new ae("unexpected end of input","ERR_UNEXPECTED_EOF");n.append(l)}let h=n.sublist(0,s);return n.consume(s),h}finally{a!=null&&i?.signal?.removeEventListener("abort",a)}},write:async(s,i)=>{i?.signal?.throwIfAborted(),s instanceof Uint8Array?await e.push(s,i):await e.push(s.subarray(),i)},unwrap:()=>{let s=r.source;return r.source=async function*(){yield*n,yield*s}(),r}}}var kt=Math.pow(2,7),Gt=Math.pow(2,14),$t=Math.pow(2,21),ye=Math.pow(2,28),xe=Math.pow(2,35),Ee=Math.pow(2,42),Le=Math.pow(2,49),f=128,w=127;function V(r){if(r<kt)return 1;if(r<Gt)return 2;if(r<$t)return 3;if(r<ye)return 4;if(r<xe)return 5;if(r<Ee)return 6;if(r<Le)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function jt(r,e,t=0){switch(V(r)){case 8:e[t++]=r&255|f,r/=128;case 7:e[t++]=r&255|f,r/=128;case 6:e[t++]=r&255|f,r/=128;case 5:e[t++]=r&255|f,r/=128;case 4:e[t++]=r&255|f,r>>>=7;case 3:e[t++]=r&255|f,r>>>=7;case 2:e[t++]=r&255|f,r>>>=7;case 1:{e[t++]=r&255,r>>>=7;break}default:throw new Error("unreachable")}return e}function Ht(r,e,t=0){switch(V(r)){case 8:e.set(t++,r&255|f),r/=128;case 7:e.set(t++,r&255|f),r/=128;case 6:e.set(t++,r&255|f),r/=128;case 5:e.set(t++,r&255|f),r/=128;case 4:e.set(t++,r&255|f),r>>>=7;case 3:e.set(t++,r&255|f),r>>>=7;case 2:e.set(t++,r&255|f),r>>>=7;case 1:{e.set(t++,r&255),r>>>=7;break}default:throw new Error("unreachable")}return e}function Xt(r,e){let t=r[e],n=0;if(n+=t&w,t<f||(t=r[e+1],n+=(t&w)<<7,t<f)||(t=r[e+2],n+=(t&w)<<14,t<f)||(t=r[e+3],n+=(t&w)<<21,t<f)||(t=r[e+4],n+=(t&w)*ye,t<f)||(t=r[e+5],n+=(t&w)*xe,t<f)||(t=r[e+6],n+=(t&w)*Ee,t<f)||(t=r[e+7],n+=(t&w)*Le,t<f))return n;throw new RangeError("Could not decode varint")}function qt(r,e){let t=r.get(e),n=0;if(n+=t&w,t<f||(t=r.get(e+1),n+=(t&w)<<7,t<f)||(t=r.get(e+2),n+=(t&w)<<14,t<f)||(t=r.get(e+3),n+=(t&w)<<21,t<f)||(t=r.get(e+4),n+=(t&w)*ye,t<f)||(t=r.get(e+5),n+=(t&w)*xe,t<f)||(t=r.get(e+6),n+=(t&w)*Ee,t<f)||(t=r.get(e+7),n+=(t&w)*Le,t<f))return n;throw new RangeError("Could not decode varint")}function tt(r,e,t=0){return e==null&&(e=S(V(r))),e instanceof Uint8Array?jt(r,e,t):Ht(r,e,t)}function ce(r,e=0){return r instanceof Uint8Array?Xt(r,e):qt(r,e)}function fe(r){return r[Symbol.asyncIterator]!=null}var he=r=>{let e=V(r),t=S(e);return tt(r,t),he.bytes=e,t};he.bytes=0;function z(r,e){e=e??{};let t=e.lengthEncoder??he;function*n(o){let s=t(o.byteLength);s instanceof Uint8Array?yield s:yield*s,o instanceof Uint8Array?yield o:yield*o}return fe(r)?async function*(){for await(let o of r)yield*n(o)}():function*(){for(let o of r)yield*n(o)}()}z.single=(r,e)=>{e=e??{};let t=e.lengthEncoder??he;return new b(t(r.byteLength),r)};var H=Vt(ot(),1);var Wt=8,Yt=1024*1024*4,k;(function(r){r[r.LENGTH=0]="LENGTH",r[r.DATA=1]="DATA"})(k||(k={}));var ve=r=>{let e=ce(r);return ve.bytes=V(e),e};ve.bytes=0;function Se(r,e){let t=new b,n=k.LENGTH,o=-1,s=e?.lengthDecoder??ve,i=e?.maxLengthLength??Wt,a=e?.maxDataLength??Yt;function*c(){for(;t.byteLength>0;){if(n===k.LENGTH)try{if(o=s(t),o<0)throw(0,H.default)(new Error("invalid message length"),"ERR_INVALID_MSG_LENGTH");if(o>a)throw(0,H.default)(new Error("message length too long"),"ERR_MSG_DATA_TOO_LONG");let h=s.bytes;t.consume(h),e?.onLength!=null&&e.onLength(o),n=k.DATA}catch(h){if(h instanceof RangeError){if(t.byteLength>i)throw(0,H.default)(new Error("message length length too long"),"ERR_MSG_LENGTH_TOO_LONG");break}throw h}if(n===k.DATA){if(t.byteLength<o)break;let h=t.sublist(0,o);t.consume(o),e?.onData!=null&&e.onData(h),yield h,n=k.LENGTH}}}return fe(r)?async function*(){for await(let h of r)t.append(h),yield*c();if(t.byteLength>0)throw(0,H.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}():function*(){for(let h of r)t.append(h),yield*c();if(t.byteLength>0)throw(0,H.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}()}Se.fromReader=(r,e)=>{let t=1,n=async function*(){for(;;)try{let{done:s,value:i}=await r.next(t);if(s===!0)return;i!=null&&(yield i)}catch(s){if(s.code==="ERR_UNDER_READ")return{done:!0,value:null};throw s}finally{t=1}}();return Se(n,{...e??{},onLength:s=>{t=s}})};var ue=class extends Error{code;constructor(e,t){super(e),this.code=t}},st=r=>ce(r);st.bytes=0;function K(r,e){let t=et(r);return{read:async o=>{let s=-1,i=new b,a=e?.lengthDecoder??st;for(;;){i.append(await t.read(1,o));try{s=a(i)}catch(c){if(c instanceof RangeError)continue;throw c}if(s>-1)break;if(e?.maxLengthLength!=null&&i.byteLength>e.maxLengthLength)throw new ue("message length length too long","ERR_MSG_LENGTH_TOO_LONG")}if(e?.maxDataLength!=null&&s>e.maxDataLength)throw new ue("message length too long","ERR_MSG_DATA_TOO_LONG");return t.read(s,o)},write:async(o,s)=>{await t.write(z.single(o,e),s)},writeV:async(o,s)=>{let i=new b(...o.map(a=>z.single(a,e)));await t.write(i,s)},unwrap:()=>t.unwrap()}}var Oe={};y(Oe,{base10:()=>nr});function Qt(r,e){if(r.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),n=0;n<t.length;n++)t[n]=255;for(var o=0;o<r.length;o++){var s=r.charAt(o),i=s.charCodeAt(0);if(t[i]!==255)throw new TypeError(s+" is ambiguous");t[i]=o}var a=r.length,c=r.charAt(0),h=Math.log(a)/Math.log(256),l=Math.log(256)/Math.log(a);function u(d){if(d instanceof Uint8Array||(ArrayBuffer.isView(d)?d=new Uint8Array(d.buffer,d.byteOffset,d.byteLength):Array.isArray(d)&&(d=Uint8Array.from(d))),!(d instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(d.length===0)return"";for(var m=0,M=0,L=0,U=d.length;L!==U&&d[L]===0;)L++,m++;for(var R=(U-L)*l+1>>>0,E=new Uint8Array(R);L!==U;){for(var D=d[L],I=0,A=R-1;(D!==0||I<M)&&A!==-1;A--,I++)D+=256*E[A]>>>0,E[A]=D%a>>>0,D=D/a>>>0;if(D!==0)throw new Error("Non-zero carry");M=I,L++}for(var O=R-M;O!==R&&E[O]===0;)O++;for(var ne=c.repeat(m);O<R;++O)ne+=r.charAt(E[O]);return ne}function v(d){if(typeof d!="string")throw new TypeError("Expected String");if(d.length===0)return new Uint8Array;var m=0;if(d[m]!==" "){for(var M=0,L=0;d[m]===c;)M++,m++;for(var U=(d.length-m)*h+1>>>0,R=new Uint8Array(U);d[m];){var E=t[d.charCodeAt(m)];if(E===255)return;for(var D=0,I=U-1;(E!==0||D<L)&&I!==-1;I--,D++)E+=a*R[I]>>>0,R[I]=E%256>>>0,E=E/256>>>0;if(E!==0)throw new Error("Non-zero carry");L=D,m++}if(d[m]!==" "){for(var A=U-L;A!==U&&R[A]===0;)A++;for(var O=new Uint8Array(M+(U-A)),ne=M;A!==U;)O[ne++]=R[A++];return O}}}function re(d){var m=v(d);if(m)return m;throw new Error(`Non-${e} character`)}return{encode:u,decodeUnsafe:v,decode:re}}var Zt=Qt,er=Zt,it=er;var Wn=new Uint8Array(0);var at=(r,e)=>{if(r===e)return!0;if(r.byteLength!==e.byteLength)return!1;for(let t=0;t<r.byteLength;t++)if(r[t]!==e[t])return!1;return!0},C=r=>{if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")};var ct=r=>new TextEncoder().encode(r),ft=r=>new TextDecoder().decode(r);var Ue=class{constructor(e,t,n){this.name=e,this.prefix=t,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},Re=class{constructor(e,t,n){if(this.name=e,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.codePointAt(0),this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return ut(this,e)}},De=class{constructor(e){this.decoders=e}or(e){return ut(this,e)}decode(e){let t=e[0],n=this.decoders[t];if(n)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},ut=(r,e)=>new De({...r.decoders||{[r.prefix]:r},...e.decoders||{[e.prefix]:e}}),Te=class{constructor(e,t,n,o){this.name=e,this.prefix=t,this.baseEncode=n,this.baseDecode=o,this.encoder=new Ue(e,t,n),this.decoder=new Re(e,t,o)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}},X=({name:r,prefix:e,encode:t,decode:n})=>new Te(r,e,t,n),_=({prefix:r,name:e,alphabet:t})=>{let{encode:n,decode:o}=it(t,e);return X({prefix:r,name:e,encode:n,decode:s=>C(o(s))})},tr=(r,e,t,n)=>{let o={};for(let l=0;l<e.length;++l)o[e[l]]=l;let s=r.length;for(;r[s-1]==="=";)--s;let i=new Uint8Array(s*t/8|0),a=0,c=0,h=0;for(let l=0;l<s;++l){let u=o[r[l]];if(u===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<t|u,a+=t,a>=8&&(a-=8,i[h++]=255&c>>a)}if(a>=t||255&c<<8-a)throw new SyntaxError("Unexpected end of data");return i},rr=(r,e,t)=>{let n=e[e.length-1]==="=",o=(1<<t)-1,s="",i=0,a=0;for(let c=0;c<r.length;++c)for(a=a<<8|r[c],i+=8;i>t;)i-=t,s+=e[o&a>>i];if(i&&(s+=e[o&a<<t-i]),n)for(;s.length*t&7;)s+="=";return s},p=({name:r,prefix:e,bitsPerChar:t,alphabet:n})=>X({prefix:e,name:r,encode(o){return rr(o,n,t)},decode(o){return tr(o,n,t,r)}});var nr=_({prefix:"9",name:"base10",alphabet:"0123456789"});var Ne={};y(Ne,{base16:()=>or,base16upper:()=>sr});var or=p({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),sr=p({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Ce={};y(Ce,{base2:()=>ir});var ir=p({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var Ie={};y(Ie,{base256emoji:()=>ur});var lt=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),ar=lt.reduce((r,e,t)=>(r[t]=e,r),[]),cr=lt.reduce((r,e,t)=>(r[e.codePointAt(0)]=t,r),[]);function fr(r){return r.reduce((e,t)=>(e+=ar[t],e),"")}function hr(r){let e=[];for(let t of r){let n=cr[t.codePointAt(0)];if(n===void 0)throw new Error(`Non-base256emoji character: ${t}`);e.push(n)}return new Uint8Array(e)}var ur=X({prefix:"\u{1F680}",name:"base256emoji",encode:fr,decode:hr});var _e={};y(_e,{base32:()=>q,base32hex:()=>wr,base32hexpad:()=>br,base32hexpadupper:()=>gr,base32hexupper:()=>mr,base32pad:()=>dr,base32padupper:()=>pr,base32upper:()=>lr,base32z:()=>yr});var q=p({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),lr=p({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),dr=p({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),pr=p({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),wr=p({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),mr=p({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),br=p({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),gr=p({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),yr=p({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Fe={};y(Fe,{base36:()=>xr,base36upper:()=>Er});var xr=_({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Er=_({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var Me={};y(Me,{base58btc:()=>T,base58flickr:()=>Lr});var T=_({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Lr=_({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Be={};y(Be,{base64:()=>Ar,base64pad:()=>Sr,base64url:()=>vr,base64urlpad:()=>Ur});var Ar=p({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Sr=p({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),vr=p({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Ur=p({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Pe={};y(Pe,{base8:()=>Rr});var Rr=p({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var Ve={};y(Ve,{identity:()=>Dr});var Dr=X({prefix:"\0",name:"identity",encode:r=>ft(r),decode:r=>ct(r)});var ho=new TextEncoder,uo=new TextDecoder;var ke={};y(ke,{identity:()=>Kr});var Nr=wt,dt=128,Cr=127,Ir=~Cr,_r=Math.pow(2,31);function wt(r,e,t){e=e||[],t=t||0;for(var n=t;r>=_r;)e[t++]=r&255|dt,r/=128;for(;r&Ir;)e[t++]=r&255|dt,r>>>=7;return e[t]=r|0,wt.bytes=t-n+1,e}var Fr=ze,Mr=128,pt=127;function ze(r,n){var t=0,n=n||0,o=0,s=n,i,a=r.length;do{if(s>=a)throw ze.bytes=0,new RangeError("Could not decode varint");i=r[s++],t+=o<28?(i&pt)<<o:(i&pt)*Math.pow(2,o),o+=7}while(i>=Mr);return ze.bytes=s-n,t}var Br=Math.pow(2,7),Pr=Math.pow(2,14),Vr=Math.pow(2,21),zr=Math.pow(2,28),kr=Math.pow(2,35),Gr=Math.pow(2,42),$r=Math.pow(2,49),jr=Math.pow(2,56),Hr=Math.pow(2,63),Xr=function(r){return r<Br?1:r<Pr?2:r<Vr?3:r<zr?4:r<kr?5:r<Gr?6:r<$r?7:r<jr?8:r<Hr?9:10},qr={encode:Nr,decode:Fr,encodingLength:Xr},Jr=qr,Q=Jr;var Z=(r,e=0)=>[Q.decode(r,e),Q.decode.bytes],J=(r,e,t=0)=>(Q.encode(r,e,t),e),W=r=>Q.encodingLength(r);var G=(r,e)=>{let t=e.byteLength,n=W(r),o=n+W(t),s=new Uint8Array(o+t);return J(r,s,0),J(t,s,n),s.set(e,o),new Y(r,t,e,s)},mt=r=>{let e=C(r),[t,n]=Z(e),[o,s]=Z(e.subarray(n)),i=e.subarray(n+s);if(i.byteLength!==o)throw new Error("Incorrect length");return new Y(t,o,i,e)},bt=(r,e)=>{if(r===e)return!0;{let t=e;return r.code===t.code&&r.size===t.size&&t.bytes instanceof Uint8Array&&at(r.bytes,t.bytes)}},Y=class{constructor(e,t,n,o){this.code=e,this.size=t,this.digest=n,this.bytes=o}};var gt=0,Wr="identity",yt=C,Yr=r=>G(gt,yt(r)),Kr={code:gt,name:Wr,encode:yt,digest:Yr};var je={};y(je,{sha256:()=>Qr,sha512:()=>Zr});var $e=({name:r,code:e,encode:t})=>new Ge(r,e,t),Ge=class{constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){let t=this.encode(e);return t instanceof Uint8Array?G(this.code,t):t.then(n=>G(this.code,n))}else throw Error("Unknown type, must be binary type")}};var Et=r=>async e=>new Uint8Array(await crypto.subtle.digest(r,e)),Qr=$e({name:"sha2-256",code:18,encode:Et("SHA-256")}),Zr=$e({name:"sha2-512",code:19,encode:Et("SHA-512")});var Lt=(r,e)=>{let{bytes:t,version:n}=r;switch(n){case 0:return tn(t,He(r),e||T.encoder);default:return rn(t,He(r),e||q.encoder)}};var At=new WeakMap,He=r=>{let e=At.get(r);if(e==null){let t=new Map;return At.set(r,t),t}return e},de=class r{constructor(e,t,n,o){this.code=t,this.version=e,this.multihash=n,this.bytes=o,this["/"]=o}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:e,multihash:t}=this;if(e!==te)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==nn)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(t)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:e,digest:t}=this.multihash,n=G(e,t);return r.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(e){return r.equals(this,e)}static equals(e,t){let n=t;return n&&e.code===n.code&&e.version===n.version&&bt(e.multihash,n.multihash)}toString(e){return Lt(this,e)}toJSON(){return{"/":Lt(this)}}link(){return this}get[Symbol.toStringTag](){return"CID"}[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(e){if(e==null)return null;let t=e;if(t instanceof r)return t;if(t["/"]!=null&&t["/"]===t.bytes||t.asCID===t){let{version:n,code:o,multihash:s,bytes:i}=t;return new r(n,o,s,i||St(n,o,s.bytes))}else if(t[on]===!0){let{version:n,multihash:o,code:s}=t,i=mt(o);return r.create(n,s,i)}else return null}static create(e,t,n){if(typeof t!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(e){case 0:{if(t!==te)throw new Error(`Version 0 CID must use dag-pb (code: ${te}) block encoding`);return new r(e,t,n,n.bytes)}case 1:{let o=St(e,t,n.bytes);return new r(e,t,n,o)}default:throw new Error("Invalid version")}}static createV0(e){return r.create(0,te,e)}static createV1(e,t){return r.create(1,e,t)}static decode(e){let[t,n]=r.decodeFirst(e);if(n.length)throw new Error("Incorrect length");return t}static decodeFirst(e){let t=r.inspectBytes(e),n=t.size-t.multihashSize,o=C(e.subarray(n,n+t.multihashSize));if(o.byteLength!==t.multihashSize)throw new Error("Incorrect length");let s=o.subarray(t.multihashSize-t.digestSize),i=new Y(t.multihashCode,t.digestSize,s,o);return[t.version===0?r.createV0(i):r.createV1(t.codec,i),e.subarray(t.size)]}static inspectBytes(e){let t=0,n=()=>{let[u,v]=Z(e.subarray(t));return t+=v,u},o=n(),s=te;if(o===18?(o=0,t=0):s=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let i=t,a=n(),c=n(),h=t+c,l=h-i;return{version:o,codec:s,multihashCode:a,digestSize:c,multihashSize:l,size:h}}static parse(e,t){let[n,o]=en(e,t),s=r.decode(o);if(s.version===0&&e[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return He(s).set(n,e),s}},en=(r,e)=>{switch(r[0]){case"Q":{let t=e||T;return[T.prefix,t.decode(`${T.prefix}${r}`)]}case T.prefix:{let t=e||T;return[T.prefix,t.decode(r)]}case q.prefix:{let t=e||q;return[q.prefix,t.decode(r)]}default:{if(e==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[r[0],e.decode(r)]}}},tn=(r,e,t)=>{let{prefix:n}=t;if(n!==T.prefix)throw Error(`Cannot string encode V0 in ${t.name} encoding`);let o=e.get(n);if(o==null){let s=t.encode(r).slice(1);return e.set(n,s),s}else return o},rn=(r,e,t)=>{let{prefix:n}=t,o=e.get(n);if(o==null){let s=t.encode(r);return e.set(n,s),s}else return o},te=112,nn=18,St=(r,e,t)=>{let n=W(r),o=n+W(e),s=new Uint8Array(o+t.byteLength);return J(r,s,0),J(e,s,n),s.set(t,o),s},on=Symbol.for("@ipld/js-cid/CID");var Xe={...Ve,...Ce,...Pe,...Oe,...Ne,..._e,...Fe,...Me,...Be,...Ie},Do={...je,...ke};function Ut(r,e,t,n){return{name:r,prefix:e,encoder:{name:r,prefix:e,encode:t},decoder:{decode:n}}}var vt=Ut("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),qe=Ut("ascii","a",r=>{let e="a";for(let t=0;t<r.length;t++)e+=String.fromCharCode(r[t]);return e},r=>{r=r.substring(1);let e=S(r.length);for(let t=0;t<r.length;t++)e[t]=r.charCodeAt(t);return e}),sn={utf8:vt,"utf-8":vt,hex:Xe.base16,latin1:qe,ascii:qe,binary:qe,...Xe},pe=sn;function g(r,e="utf8"){let t=pe[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return(e==="utf8"||e==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?P(globalThis.Buffer.from(r,"utf-8")):t.decoder.decode(`${t.prefix}${r}`)}function Rt(r,e="utf8"){let t=pe[e];if(t==null)throw new Error(`Unsupported encoding "${e}"`);return(e==="utf8"||e==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?globalThis.Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("utf8"):t.encoder.encode(r).substring(1)}var an=g(`
|
|
3
|
-
`);async function
|
|
4
|
-
`),
|
|
5
|
-
`);await Dt(n,[s
|
|
6
|
-
`),
|
|
7
|
-
|
|
8
|
-
`)
|
|
9
|
-
`),
|
|
10
|
-
`),t),
|
|
11
|
-
`))),
|
|
12
|
-
`)
|
|
13
|
-
`)
|
|
2
|
+
"use strict";var Libp2PMultistreamSelect=(()=>{var Ct=Object.create;var oe=Object.defineProperty;var It=Object.getOwnPropertyDescriptor;var _t=Object.getOwnPropertyNames;var Ft=Object.getPrototypeOf,Pt=Object.prototype.hasOwnProperty;var Mt=(r,t)=>()=>(t||r((t={exports:{}}).exports,t),t.exports),U=(r,t)=>{for(var e in t)oe(r,e,{get:t[e],enumerable:!0})},We=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of _t(t))!Pt.call(r,o)&&o!==e&&oe(r,o,{get:()=>t[o],enumerable:!(n=It(t,o))||n.enumerable});return r};var Bt=(r,t,e)=>(e=r!=null?Ct(Ft(r)):{},We(t||!r||!r.__esModule?oe(e,"default",{value:r,enumerable:!0}):e,r)),Vt=r=>We(oe({},"__esModule",{value:!0}),r);var ot=Mt((Vn,nt)=>{"use strict";function rt(r,t){for(let e in t)Object.defineProperty(r,e,{value:t[e],enumerable:!0,configurable:!0});return r}function qt(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 rt(r,e)}catch{e.message=r.message,e.stack=r.stack;let o=function(){};return o.prototype=Object.create(Object.getPrototypeOf(r)),rt(new o,e)}}nt.exports=qt});var fn={};U(fn,{PROTOCOL_ID:()=>x,handle:()=>Nt,select:()=>Ot});var x="/multistream/1.0.0";var G=class extends Error{code;props;constructor(t,e,n){super(t),this.code=e,this.name=n?.name??"CodeError",this.props=n??{}}};function M(r){return globalThis.Buffer!=null?new Uint8Array(r.buffer,r.byteOffset,r.byteLength):r}function _(r=0){return globalThis.Buffer?.alloc!=null?M(globalThis.Buffer.alloc(r)):new Uint8Array(r)}function T(r=0){return globalThis.Buffer?.allocUnsafe!=null?M(globalThis.Buffer.allocUnsafe(r)):new Uint8Array(r)}function be(r,t){if(globalThis.Buffer!=null)return M(globalThis.Buffer.concat(r,t));t==null&&(t=r.reduce((o,i)=>o+i.length,0));let e=T(t),n=0;for(let o of r)e.set(o,n),n+=o.length;return M(e)}function Ye(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 Qe=Symbol.for("@achingbrain/uint8arraylist");function Ke(r,t){if(t==null||t<0)throw new RangeError("index is out of bounds");let e=0;for(let n of r){let o=e+n.byteLength;if(t<o)return{buf:n,index:t-e};e=o}throw new RangeError("index is out of bounds")}function ie(r){return!!r?.[Qe]}var L=class r{bufs;length;[Qe]=!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(ie(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(ie(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=Ke(this.bufs,t);return e.buf[e.index]}set(t,e){let n=Ke(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(ie(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:o}=this._subList(t,e);return be(n,o)}subarray(t,e){let{bufs:n,length:o}=this._subList(t,e);return n.length===1?n[0]:be(n,o)}sublist(t,e){let{bufs:n,length:o}=this._subList(t,e),i=new r;return i.length=o,i.bufs=[...n],i}_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=[],o=0;for(let i=0;i<this.bufs.length;i++){let s=this.bufs[i],a=o,c=a+s.byteLength;if(o=c,t>=c)continue;let f=t>=a&&t<c,d=e>a&&e<=c;if(f&&d){if(t===a&&e===c){n.push(s);break}let h=t-a;n.push(s.subarray(h,h+(e-t)));break}if(f){if(t===0){n.push(s);continue}n.push(s.subarray(t-a));continue}if(d){if(e===c){n.push(s);break}n.push(s.subarray(0,e-a));break}n.push(s)}return{bufs:n,length:e-t}}indexOf(t,e=0){if(!ie(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 o=n.byteLength;if(o===0)throw new TypeError("search must be at least 1 byte long");let i=256,s=new Int32Array(i);for(let h=0;h<i;h++)s[h]=-1;for(let h=0;h<o;h++)s[n[h]]=h;let a=s,c=this.byteLength-n.byteLength,f=n.byteLength-1,d;for(let h=e;h<=c;h+=d){d=0;for(let b=f;b>=0;b--){let k=this.get(h+b);if(n[b]!==k){d=Math.max(1,b-a[k]);break}}if(d===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=T(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 o=_(2);new DataView(o.buffer,o.byteOffset,o.byteLength).setInt16(0,e,n),this.write(o,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 o=_(4);new DataView(o.buffer,o.byteOffset,o.byteLength).setInt32(0,e,n),this.write(o,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 o=_(8);new DataView(o.buffer,o.byteOffset,o.byteLength).setBigInt64(0,e,n),this.write(o,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=T(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 o=_(2);new DataView(o.buffer,o.byteOffset,o.byteLength).setUint16(0,e,n),this.write(o,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 o=_(4);new DataView(o.buffer,o.byteOffset,o.byteLength).setUint32(0,e,n),this.write(o,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 o=_(8);new DataView(o.buffer,o.byteOffset,o.byteLength).setBigUint64(0,e,n),this.write(o,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 o=_(4);new DataView(o.buffer,o.byteOffset,o.byteLength).setFloat32(0,e,n),this.write(o,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 o=_(8);new DataView(o.buffer,o.byteOffset,o.byteLength).setFloat64(0,e,n),this.write(o,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(!Ye(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((o,i)=>o+i.byteLength,0)),n.length=e,n}};function O(){let r={};return r.promise=new Promise((t,e)=>{r.resolve=t,r.reject=e}),r}var se=class extends Error{type;code;constructor(t,e){super(t??"The operation was aborted"),this.type="aborted",this.name="AbortError",this.code=e??"ABORT_ERR"}};async function Ze(r,t,e){if(t==null)return r;if(t.aborted)return Promise.reject(new se(e?.errorMessage,e?.errorCode));let n,o=new se(e?.errorMessage,e?.errorCode);try{return await Promise.race([r,new Promise((i,s)=>{n=()=>{s(o)},t.addEventListener("abort",n)})])}finally{n!=null&&t.removeEventListener("abort",n)}}var ye=class{needNext;haveNext;ended;nextResult;constructor(){this.ended=!1,this.needNext=O(),this.needNext.resolve(),this.haveNext=O()}[Symbol.asyncIterator](){return this}async next(){if(this.nextResult==null&&await this.haveNext.promise,this.nextResult==null)throw new Error("Have next but next was undefined");let t=this.nextResult;return this.nextResult=void 0,this.needNext.resolve(),this.needNext=O(),t}async throw(t){return this.ended=!0,t!=null&&this.haveNext.reject(t),{done:!0,value:void 0}}async return(){let t={done:!0,value:void 0};return await this._push(void 0),t}async push(t,e){await this._push(t,e)}async end(t,e){t!=null?await this.throw(t):await this._push(void 0,e)}async _push(t,e){if(t!=null&&this.ended)throw new Error("Cannot push value onto an ended pushable");this.nextResult!=null&&await this.needNext.promise,t!=null?this.nextResult={done:!1,value:t}:(this.ended=!0,this.nextResult={done:!0,value:void 0}),this.haveNext.resolve(),this.haveNext=O(),await Ze(this.needNext.promise,e?.signal,e)}};function et(){return new ye}var ae=class extends Error{code;constructor(t,e){super(t),this.code=e}},xe=class extends ae{type;constructor(t){super(t,"ABORT_ERR"),this.type="aborted"}};function tt(r,t){let e=et();r.sink(e).catch(async s=>{await e.end(s)}),r.sink=async s=>{for await(let a of s)await e.push(a);await e.end()};let n=r.source;r.source[Symbol.iterator]!=null?n=r.source[Symbol.iterator]():r.source[Symbol.asyncIterator]!=null&&(n=r.source[Symbol.asyncIterator]());let o=new L;return{read:async(s,a)=>{a?.signal?.throwIfAborted();let c,f=new Promise((d,h)=>{c=()=>{h(new xe("Read aborted"))},a?.signal?.addEventListener("abort",c)});try{if(s==null){let{done:h,value:b}=await Promise.race([n.next(),f]);return h===!0?new L:b}for(;o.byteLength<s;){let{value:h,done:b}=await Promise.race([n.next(),f]);if(b===!0)throw new ae("unexpected end of input","ERR_UNEXPECTED_EOF");o.append(h)}let d=o.sublist(0,s);return o.consume(s),d}finally{c!=null&&a?.signal?.removeEventListener("abort",c)}},write:async(s,a)=>{a?.signal?.throwIfAborted(),s instanceof Uint8Array?await e.push(s,a):await e.push(s.subarray(),a)},unwrap:()=>{if(o.byteLength>0){let s=r.source;r.source=async function*(){t?.yieldBytes===!1?yield o:yield*o,yield*s}()}return r}}}var kt=Math.pow(2,7),Gt=Math.pow(2,14),zt=Math.pow(2,21),Le=Math.pow(2,28),Ee=Math.pow(2,35),Ae=Math.pow(2,42),ve=Math.pow(2,49),u=128,E=127;function F(r){if(r<kt)return 1;if(r<Gt)return 2;if(r<zt)return 3;if(r<Le)return 4;if(r<Ee)return 5;if(r<Ae)return 6;if(r<ve)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function $t(r,t,e=0){switch(F(r)){case 8:t[e++]=r&255|u,r/=128;case 7:t[e++]=r&255|u,r/=128;case 6:t[e++]=r&255|u,r/=128;case 5:t[e++]=r&255|u,r/=128;case 4:t[e++]=r&255|u,r>>>=7;case 3:t[e++]=r&255|u,r>>>=7;case 2:t[e++]=r&255|u,r>>>=7;case 1:{t[e++]=r&255,r>>>=7;break}default:throw new Error("unreachable")}return t}function jt(r,t,e=0){switch(F(r)){case 8:t.set(e++,r&255|u),r/=128;case 7:t.set(e++,r&255|u),r/=128;case 6:t.set(e++,r&255|u),r/=128;case 5:t.set(e++,r&255|u),r/=128;case 4:t.set(e++,r&255|u),r>>>=7;case 3:t.set(e++,r&255|u),r>>>=7;case 2:t.set(e++,r&255|u),r>>>=7;case 1:{t.set(e++,r&255),r>>>=7;break}default:throw new Error("unreachable")}return t}function Ht(r,t){let e=r[t],n=0;if(n+=e&E,e<u||(e=r[t+1],n+=(e&E)<<7,e<u)||(e=r[t+2],n+=(e&E)<<14,e<u)||(e=r[t+3],n+=(e&E)<<21,e<u)||(e=r[t+4],n+=(e&E)*Le,e<u)||(e=r[t+5],n+=(e&E)*Ee,e<u)||(e=r[t+6],n+=(e&E)*Ae,e<u)||(e=r[t+7],n+=(e&E)*ve,e<u))return n;throw new RangeError("Could not decode varint")}function Xt(r,t){let e=r.get(t),n=0;if(n+=e&E,e<u||(e=r.get(t+1),n+=(e&E)<<7,e<u)||(e=r.get(t+2),n+=(e&E)<<14,e<u)||(e=r.get(t+3),n+=(e&E)<<21,e<u)||(e=r.get(t+4),n+=(e&E)*Le,e<u)||(e=r.get(t+5),n+=(e&E)*Ee,e<u)||(e=r.get(t+6),n+=(e&E)*Ae,e<u)||(e=r.get(t+7),n+=(e&E)*ve,e<u))return n;throw new RangeError("Could not decode varint")}function ce(r,t,e=0){return t==null&&(t=T(F(r))),t instanceof Uint8Array?$t(r,t,e):jt(r,t,e)}function fe(r,t=0){return r instanceof Uint8Array?Ht(r,t):Xt(r,t)}function le(r){return r[Symbol.asyncIterator]!=null}var ue=r=>{let t=F(r),e=T(t);return ce(r,e),ue.bytes=t,e};ue.bytes=0;function z(r,t){t=t??{};let e=t.lengthEncoder??ue;function*n(o){let i=e(o.byteLength);i instanceof Uint8Array?yield i:yield*i,o instanceof Uint8Array?yield o:yield*o}return le(r)?async function*(){for await(let o of r)yield*n(o)}():function*(){for(let o of r)yield*n(o)}()}z.single=(r,t)=>{t=t??{};let e=t.lengthEncoder??ue;return new L(e(r.byteLength),r)};var X=Bt(ot(),1);var Jt=8,Wt=1024*1024*4,$;(function(r){r[r.LENGTH=0]="LENGTH",r[r.DATA=1]="DATA"})($||($={}));var Ue=r=>{let t=fe(r);return Ue.bytes=F(t),t};Ue.bytes=0;function Se(r,t){let e=new L,n=$.LENGTH,o=-1,i=t?.lengthDecoder??Ue,s=t?.maxLengthLength??Jt,a=t?.maxDataLength??Wt;function*c(){for(;e.byteLength>0;){if(n===$.LENGTH)try{if(o=i(e),o<0)throw(0,X.default)(new Error("invalid message length"),"ERR_INVALID_MSG_LENGTH");if(o>a)throw(0,X.default)(new Error("message length too long"),"ERR_MSG_DATA_TOO_LONG");let f=i.bytes;e.consume(f),t?.onLength!=null&&t.onLength(o),n=$.DATA}catch(f){if(f instanceof RangeError){if(e.byteLength>s)throw(0,X.default)(new Error("message length length too long"),"ERR_MSG_LENGTH_TOO_LONG");break}throw f}if(n===$.DATA){if(e.byteLength<o)break;let f=e.sublist(0,o);e.consume(o),t?.onData!=null&&t.onData(f),yield f,n=$.LENGTH}}}return le(r)?async function*(){for await(let f of r)e.append(f),yield*c();if(e.byteLength>0)throw(0,X.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}():function*(){for(let f of r)e.append(f),yield*c();if(e.byteLength>0)throw(0,X.default)(new Error("unexpected end of input"),"ERR_UNEXPECTED_EOF")}()}Se.fromReader=(r,t)=>{let e=1,n=async function*(){for(;;)try{let{done:i,value:s}=await r.next(e);if(i===!0)return;s!=null&&(yield s)}catch(i){if(i.code==="ERR_UNDER_READ")return{done:!0,value:null};throw i}finally{e=1}}();return Se(n,{...t??{},onLength:i=>{e=i}})};var de=class extends Error{code;constructor(t,e){super(t),this.code=e}},it=r=>fe(r);it.bytes=0;function Q(r,t={}){let e=tt(r,t);return t.maxDataLength!=null&&t.maxLengthLength==null&&(t.maxLengthLength=F(t.maxDataLength)),{read:async o=>{let i=-1,s=new L,a=t?.lengthDecoder??it;for(;;){s.append(await e.read(1,o));try{i=a(s)}catch(c){if(c instanceof RangeError)continue;throw c}if(t?.maxLengthLength!=null&&s.byteLength>t.maxLengthLength)throw new de("message length length too long","ERR_MSG_LENGTH_TOO_LONG");if(i>-1)break}if(t?.maxDataLength!=null&&i>t.maxDataLength)throw new de("message length too long","ERR_MSG_DATA_TOO_LONG");return e.read(i,o)},write:async(o,i)=>{await e.write(z.single(o,t),i)},writeV:async(o,i)=>{let s=new L(...o.map(a=>z.single(a,t)));await e.write(s,i)},unwrap:()=>e.unwrap()}}var Ne={};U(Ne,{base10:()=>rr});function Kt(r,t){if(r.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),n=0;n<e.length;n++)e[n]=255;for(var o=0;o<r.length;o++){var i=r.charAt(o),s=i.charCodeAt(0);if(e[s]!==255)throw new TypeError(i+" is ambiguous");e[s]=o}var a=r.length,c=r.charAt(0),f=Math.log(a)/Math.log(256),d=Math.log(256)/Math.log(a);function h(l){if(l instanceof Uint8Array||(ArrayBuffer.isView(l)?l=new Uint8Array(l.buffer,l.byteOffset,l.byteLength):Array.isArray(l)&&(l=Uint8Array.from(l))),!(l instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(l.length===0)return"";for(var m=0,C=0,S=0,g=l.length;S!==g&&l[S]===0;)S++,m++;for(var y=(g-S)*d+1>>>0,p=new Uint8Array(y);S!==g;){for(var v=l[S],R=0,D=y-1;(v!==0||R<C)&&D!==-1;D--,R++)v+=256*p[D]>>>0,p[D]=v%a>>>0,v=v/a>>>0;if(v!==0)throw new Error("Non-zero carry");C=R,S++}for(var I=y-C;I!==y&&p[I]===0;)I++;for(var ne=c.repeat(m);I<y;++I)ne+=r.charAt(p[I]);return ne}function b(l){if(typeof l!="string")throw new TypeError("Expected String");if(l.length===0)return new Uint8Array;var m=0;if(l[m]!==" "){for(var C=0,S=0;l[m]===c;)C++,m++;for(var g=(l.length-m)*f+1>>>0,y=new Uint8Array(g);l[m];){var p=e[l.charCodeAt(m)];if(p===255)return;for(var v=0,R=g-1;(p!==0||v<S)&&R!==-1;R--,v++)p+=a*y[R]>>>0,y[R]=p%256>>>0,p=p/256>>>0;if(p!==0)throw new Error("Non-zero carry");S=v,m++}if(l[m]!==" "){for(var D=g-S;D!==g&&y[D]===0;)D++;for(var I=new Uint8Array(C+(g-D)),ne=C;D!==g;)I[ne++]=y[D++];return I}}}function k(l){var m=b(l);if(m)return m;throw new Error(`Non-${t} character`)}return{encode:h,decodeUnsafe:b,decode:k}}var Qt=Kt,Zt=Qt,st=Zt;var Wn=new Uint8Array(0);var at=(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},P=r=>{if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")};var ct=r=>new TextEncoder().encode(r),ft=r=>new TextDecoder().decode(r);var Re=class{constructor(t,e,n){this.name=t,this.prefix=e,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}},De=class{constructor(t,e,n){if(this.name=t,this.prefix=e,e.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=e.codePointAt(0),this.baseDecode=n}decode(t){if(typeof t=="string"){if(t.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(t)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(t.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(t){return lt(this,t)}},Te=class{constructor(t){this.decoders=t}or(t){return lt(this,t)}decode(t){let e=t[0],n=this.decoders[e];if(n)return n.decode(t);throw RangeError(`Unable to decode multibase string ${JSON.stringify(t)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},lt=(r,t)=>new Te({...r.decoders||{[r.prefix]:r},...t.decoders||{[t.prefix]:t}}),Oe=class{constructor(t,e,n,o){this.name=t,this.prefix=e,this.baseEncode=n,this.baseDecode=o,this.encoder=new Re(t,e,n),this.decoder=new De(t,e,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}},q=({name:r,prefix:t,encode:e,decode:n})=>new Oe(r,t,e,n),B=({prefix:r,name:t,alphabet:e})=>{let{encode:n,decode:o}=st(e,t);return q({prefix:r,name:t,encode:n,decode:i=>P(o(i))})},er=(r,t,e,n)=>{let o={};for(let d=0;d<t.length;++d)o[t[d]]=d;let i=r.length;for(;r[i-1]==="=";)--i;let s=new Uint8Array(i*e/8|0),a=0,c=0,f=0;for(let d=0;d<i;++d){let h=o[r[d]];if(h===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<e|h,a+=e,a>=8&&(a-=8,s[f++]=255&c>>a)}if(a>=e||255&c<<8-a)throw new SyntaxError("Unexpected end of data");return s},tr=(r,t,e)=>{let n=t[t.length-1]==="=",o=(1<<e)-1,i="",s=0,a=0;for(let c=0;c<r.length;++c)for(a=a<<8|r[c],s+=8;s>e;)s-=e,i+=t[o&a>>s];if(s&&(i+=t[o&a<<e-s]),n)for(;i.length*e&7;)i+="=";return i},w=({name:r,prefix:t,bitsPerChar:e,alphabet:n})=>q({prefix:t,name:r,encode(o){return tr(o,n,e)},decode(o){return er(o,n,e,r)}});var rr=B({prefix:"9",name:"base10",alphabet:"0123456789"});var Ce={};U(Ce,{base16:()=>nr,base16upper:()=>or});var nr=w({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),or=w({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Ie={};U(Ie,{base2:()=>ir});var ir=w({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var _e={};U(_e,{base256emoji:()=>hr});var ut=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),sr=ut.reduce((r,t,e)=>(r[e]=t,r),[]),ar=ut.reduce((r,t,e)=>(r[t.codePointAt(0)]=e,r),[]);function cr(r){return r.reduce((t,e)=>(t+=sr[e],t),"")}function fr(r){let t=[];for(let e of r){let n=ar[e.codePointAt(0)];if(n===void 0)throw new Error(`Non-base256emoji character: ${e}`);t.push(n)}return new Uint8Array(t)}var hr=q({prefix:"\u{1F680}",name:"base256emoji",encode:cr,decode:fr});var Fe={};U(Fe,{base32:()=>J,base32hex:()=>gr,base32hexpad:()=>mr,base32hexpadupper:()=>pr,base32hexupper:()=>wr,base32pad:()=>ur,base32padupper:()=>dr,base32upper:()=>lr,base32z:()=>br});var J=w({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),lr=w({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),ur=w({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),dr=w({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),gr=w({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),wr=w({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),mr=w({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),pr=w({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),br=w({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Pe={};U(Pe,{base36:()=>yr,base36upper:()=>xr});var yr=B({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),xr=B({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var Me={};U(Me,{base58btc:()=>N,base58flickr:()=>Lr});var N=B({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Lr=B({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Be={};U(Be,{base64:()=>Er,base64pad:()=>Ar,base64url:()=>vr,base64urlpad:()=>Sr});var Er=w({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Ar=w({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),vr=w({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Sr=w({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Ve={};U(Ve,{base8:()=>Ur});var Ur=w({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var ke={};U(ke,{identity:()=>Rr});var Rr=q({prefix:"\0",name:"identity",encode:r=>ft(r),decode:r=>ct(r)});var ho=new TextEncoder,lo=new TextDecoder;var ze={};U(ze,{identity:()=>Yr});var Or=wt,dt=128,Nr=127,Cr=~Nr,Ir=Math.pow(2,31);function wt(r,t,e){t=t||[],e=e||0;for(var n=e;r>=Ir;)t[e++]=r&255|dt,r/=128;for(;r&Cr;)t[e++]=r&255|dt,r>>>=7;return t[e]=r|0,wt.bytes=e-n+1,t}var _r=Ge,Fr=128,gt=127;function Ge(r,n){var e=0,n=n||0,o=0,i=n,s,a=r.length;do{if(i>=a)throw Ge.bytes=0,new RangeError("Could not decode varint");s=r[i++],e+=o<28?(s>)<<o:(s>)*Math.pow(2,o),o+=7}while(s>=Fr);return Ge.bytes=i-n,e}var Pr=Math.pow(2,7),Mr=Math.pow(2,14),Br=Math.pow(2,21),Vr=Math.pow(2,28),kr=Math.pow(2,35),Gr=Math.pow(2,42),zr=Math.pow(2,49),$r=Math.pow(2,56),jr=Math.pow(2,63),Hr=function(r){return r<Pr?1:r<Mr?2:r<Br?3:r<Vr?4:r<kr?5:r<Gr?6:r<zr?7:r<$r?8:r<jr?9:10},Xr={encode:Or,decode:_r,encodingLength:Hr},qr=Xr,Z=qr;var ee=(r,t=0)=>[Z.decode(r,t),Z.decode.bytes],W=(r,t,e=0)=>(Z.encode(r,t,e),t),Y=r=>Z.encodingLength(r);var j=(r,t)=>{let e=t.byteLength,n=Y(r),o=n+Y(e),i=new Uint8Array(o+e);return W(r,i,0),W(e,i,n),i.set(t,o),new K(r,e,t,i)},mt=r=>{let t=P(r),[e,n]=ee(t),[o,i]=ee(t.subarray(n)),s=t.subarray(n+i);if(s.byteLength!==o)throw new Error("Incorrect length");return new K(e,o,s,t)},pt=(r,t)=>{if(r===t)return!0;{let e=t;return r.code===e.code&&r.size===e.size&&e.bytes instanceof Uint8Array&&at(r.bytes,e.bytes)}},K=class{constructor(t,e,n,o){this.code=t,this.size=e,this.digest=n,this.bytes=o}};var bt=0,Jr="identity",yt=P,Wr=r=>j(bt,yt(r)),Yr={code:bt,name:Jr,encode:yt,digest:Wr};var He={};U(He,{sha256:()=>Kr,sha512:()=>Qr});var je=({name:r,code:t,encode:e})=>new $e(r,t,e),$e=class{constructor(t,e,n){this.name=t,this.code=e,this.encode=n}digest(t){if(t instanceof Uint8Array){let e=this.encode(t);return e instanceof Uint8Array?j(this.code,e):e.then(n=>j(this.code,n))}else throw Error("Unknown type, must be binary type")}};var Lt=r=>async t=>new Uint8Array(await crypto.subtle.digest(r,t)),Kr=je({name:"sha2-256",code:18,encode:Lt("SHA-256")}),Qr=je({name:"sha2-512",code:19,encode:Lt("SHA-512")});var Et=(r,t)=>{let{bytes:e,version:n}=r;switch(n){case 0:return en(e,Xe(r),t||N.encoder);default:return tn(e,Xe(r),t||J.encoder)}};var At=new WeakMap,Xe=r=>{let t=At.get(r);if(t==null){let e=new Map;return At.set(r,e),e}return t},we=class r{constructor(t,e,n,o){this.code=e,this.version=t,this.multihash=n,this.bytes=o,this["/"]=o}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:t,multihash:e}=this;if(t!==re)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(e.code!==rn)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(e)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:t,digest:e}=this.multihash,n=j(t,e);return r.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(t){return r.equals(this,t)}static equals(t,e){let n=e;return n&&t.code===n.code&&t.version===n.version&&pt(t.multihash,n.multihash)}toString(t){return Et(this,t)}toJSON(){return{"/":Et(this)}}link(){return this}get[Symbol.toStringTag](){return"CID"}[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(t){if(t==null)return null;let e=t;if(e instanceof r)return e;if(e["/"]!=null&&e["/"]===e.bytes||e.asCID===e){let{version:n,code:o,multihash:i,bytes:s}=e;return new r(n,o,i,s||vt(n,o,i.bytes))}else if(e[nn]===!0){let{version:n,multihash:o,code:i}=e,s=mt(o);return r.create(n,i,s)}else return null}static create(t,e,n){if(typeof e!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(t){case 0:{if(e!==re)throw new Error(`Version 0 CID must use dag-pb (code: ${re}) block encoding`);return new r(t,e,n,n.bytes)}case 1:{let o=vt(t,e,n.bytes);return new r(t,e,n,o)}default:throw new Error("Invalid version")}}static createV0(t){return r.create(0,re,t)}static createV1(t,e){return r.create(1,t,e)}static decode(t){let[e,n]=r.decodeFirst(t);if(n.length)throw new Error("Incorrect length");return e}static decodeFirst(t){let e=r.inspectBytes(t),n=e.size-e.multihashSize,o=P(t.subarray(n,n+e.multihashSize));if(o.byteLength!==e.multihashSize)throw new Error("Incorrect length");let i=o.subarray(e.multihashSize-e.digestSize),s=new K(e.multihashCode,e.digestSize,i,o);return[e.version===0?r.createV0(s):r.createV1(e.codec,s),t.subarray(e.size)]}static inspectBytes(t){let e=0,n=()=>{let[h,b]=ee(t.subarray(e));return e+=b,h},o=n(),i=re;if(o===18?(o=0,e=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let s=e,a=n(),c=n(),f=e+c,d=f-s;return{version:o,codec:i,multihashCode:a,digestSize:c,multihashSize:d,size:f}}static parse(t,e){let[n,o]=Zr(t,e),i=r.decode(o);if(i.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return Xe(i).set(n,t),i}},Zr=(r,t)=>{switch(r[0]){case"Q":{let e=t||N;return[N.prefix,e.decode(`${N.prefix}${r}`)]}case N.prefix:{let e=t||N;return[N.prefix,e.decode(r)]}case J.prefix:{let e=t||J;return[J.prefix,e.decode(r)]}default:{if(t==null)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[r[0],t.decode(r)]}}},en=(r,t,e)=>{let{prefix:n}=e;if(n!==N.prefix)throw Error(`Cannot string encode V0 in ${e.name} encoding`);let o=t.get(n);if(o==null){let i=e.encode(r).slice(1);return t.set(n,i),i}else return o},tn=(r,t,e)=>{let{prefix:n}=e,o=t.get(n);if(o==null){let i=e.encode(r);return t.set(n,i),i}else return o},re=112,rn=18,vt=(r,t,e)=>{let n=Y(r),o=n+Y(t),i=new Uint8Array(o+e.byteLength);return W(r,i,0),W(t,i,n),i.set(e,o),i},nn=Symbol.for("@ipld/js-cid/CID");var qe={...ke,...Ie,...Ve,...Ne,...Ce,...Fe,...Pe,...Me,...Be,..._e},Do={...He,...ze};function Ut(r,t,e,n){return{name:r,prefix:t,encoder:{name:r,prefix:t,encode:e},decoder:{decode:n}}}var St=Ut("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),Je=Ut("ascii","a",r=>{let t="a";for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return t},r=>{r=r.substring(1);let t=T(r.length);for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}),on={utf8:St,"utf-8":St,hex:qe.base16,latin1:Je,ascii:Je,binary:Je,...qe},me=on;function A(r,t="utf8"){let e=me[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return(t==="utf8"||t==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?M(globalThis.Buffer.from(r,"utf-8")):e.decoder.decode(`${e.prefix}${r}`)}function Rt(r,t="utf8"){let e=me[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return(t==="utf8"||t==="utf-8")&&globalThis.Buffer!=null&&globalThis.Buffer.from!=null?globalThis.Buffer.from(r.buffer,r.byteOffset,r.byteLength).toString("utf8"):e.encoder.encode(r).substring(1)}var sn=A(`
|
|
3
|
+
`);async function H(r,t,e){await r.write(t,e)}async function Dt(r,t,e){await r.writeV(t,e)}async function an(r,t){let e=await r.read(t);if(e.byteLength===0||e.get(e.byteLength-1)!==sn[0])throw t?.log.error("Invalid mss message - missing newline",e),new G("missing newline","ERR_INVALID_MULTISTREAM_SELECT_MESSAGE");return e.sublist(0,-1)}async function V(r,t){let e=await an(r,t);return Rt(e.subarray())}async function Ot(r,t,e){if(t=Array.isArray(t)?[...t]:[t],t.length===1)return cn(r,t[0],e);let n=Q(r,{...e,maxDataLength:1024}),o=t.shift();if(o==null)throw new Error("At least one protocol must be specified");e?.log.trace('select: write ["%s", "%s"]',x,o);let i=A(`${x}
|
|
4
|
+
`),s=A(`${o}
|
|
5
|
+
`);await Dt(n,[i,s],e),e?.log.trace("select: reading multistream-select header");let a=await V(n,e);if(e?.log.trace('select: read "%s"',a),a===x&&(e?.log.trace("select: reading protocol response"),a=await V(n,e),e?.log.trace('select: read "%s"',a)),a===o)return{stream:n.unwrap(),protocol:o};for(let c of t){e?.log.trace('select: write "%s"',c),await H(n,A(`${c}
|
|
6
|
+
`),e),e?.log.trace("select: reading protocol response");let f=await V(n,e);if(e?.log.trace('select: read "%s" for "%s"',f,c),f===c)return{stream:n.unwrap(),protocol:c}}throw new G("protocol selection failed","ERR_UNSUPPORTED_PROTOCOL")}function cn(r,t,e){let n=r.sink.bind(r),o=r.source,i=!1,s=!1,a=O(),c=!1,f=!1,d=O(),h=!1,b=!1,k=O(),l=Q({sink:n,source:o},{...e,maxDataLength:1024});r.sink=async g=>{let{sink:y}=l.unwrap();await y(async function*(){let p=!1;for await(let v of g){if(f&&await d.promise,c)yield v;else{f=!0,e?.log.trace('optimistic: write ["%s", "%s", data(%d)] in sink',x,t,v.byteLength);let R=`${t}
|
|
7
|
+
`;yield new L(Uint8Array.from([19]),A(`${x}
|
|
8
|
+
`),ce(R.length),A(R),v).subarray(),e?.log.trace('optimistic: wrote ["%s", "%s", data(%d)] in sink',x,t,v.byteLength),c=!0,f=!1,d.resolve()}p=!0}p||await m()}())};async function m(){if(s){e?.log.trace("optimistic: already negotiating %s stream",t),await a.promise;return}s=!0;try{c||(e?.log.trace("optimistic: doing send protocol for %s stream",t),await C()),h||(e?.log.trace("optimistic: doing read protocol for %s stream",t),await S())}finally{s=!1,i=!0,a.resolve()}}async function C(){if(f){await d.promise;return}f=!0;try{e?.log.trace('optimistic: write ["%s", "%s", data] in source',x,t),await l.writeV([A(`${x}
|
|
9
|
+
`),A(`${t}
|
|
10
|
+
`)]),e?.log.trace('optimistic: wrote ["%s", "%s", data] in source',x,t)}finally{c=!0,f=!1,d.resolve()}}async function S(){if(b){await k.promise;return}b=!0;try{e?.log.trace("optimistic: reading multistream select header");let g=await V(l,e);if(e?.log.trace('optimistic: read multistream select header "%s"',g),g===x&&(g=await V(l,e)),e?.log.trace('optimistic: read protocol "%s", expecting "%s"',g,t),g!==t)throw new G("protocol selection failed","ERR_UNSUPPORTED_PROTOCOL")}finally{h=!0,b=!1,k.resolve()}}if(r.source=async function*(){await m(),e?.log.trace('optimistic: reading data from "%s" stream',t),yield*l.unwrap().source}(),r.closeRead!=null){let g=r.closeRead.bind(r);r.closeRead=async y=>{i||await m().catch(p=>{e?.log.error("could not negotiate protocol before close read",p)}),await g(y)}}if(r.closeWrite!=null){let g=r.closeWrite.bind(r);r.closeWrite=async y=>{i||await m().catch(p=>{e?.log.error("could not negotiate protocol before close write",p)}),await g(y)}}if(r.close!=null){let g=r.close.bind(r);r.close=async y=>{i||(i=!0,s=!1,a.resolve()),await g(y)}}return{stream:r,protocol:t}}async function Nt(r,t,e){t=Array.isArray(t)?t:[t],e.log.trace("handle: available protocols %s",t);let n=Q(r,{...e,maxDataLength:1024,maxLengthLength:2});for(;;){e?.log.trace("handle: reading incoming string");let o=await V(n,e);if(e.log.trace('handle: read "%s"',o),o===x){e.log.trace('handle: respond with "%s" for "%s"',x,o),await H(n,A(`${x}
|
|
11
|
+
`),e),e.log.trace('handle: responded with "%s" for "%s"',x,o);continue}if(t.includes(o))return e.log.trace('handle: respond with "%s" for "%s"',o,o),await H(n,A(`${o}
|
|
12
|
+
`),e),e.log.trace('handle: responded with "%s" for "%s"',o,o),{stream:n.unwrap(),protocol:o};if(o==="ls"){let i=new L(...t.map(s=>z.single(A(`${s}
|
|
13
|
+
`))),A(`
|
|
14
|
+
`));e.log.trace('handle: respond with "%s" for %s',t,o),await H(n,i,e),e.log.trace('handle: responded with "%s" for %s',t,o);continue}e.log('handle: respond with "na" for "%s"',o),await H(n,A(`na
|
|
15
|
+
`),e),e.log('handle: responded with "na" for "%s"',o)}}return Vt(fn);})();
|
|
14
16
|
return Libp2PMultistreamSelect}));
|
package/dist/src/handle.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../../src/handle.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAsB,MAAM,CAAE,MAAM,SAAS,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAG,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"handle.d.ts","sourceRoot":"","sources":["../../src/handle.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACvE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,wBAAsB,MAAM,CAAE,MAAM,SAAS,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,EAAG,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CA+ClL"}
|
package/dist/src/handle.js
CHANGED
|
@@ -52,32 +52,39 @@ import * as multistream from './multistream.js';
|
|
|
52
52
|
*/
|
|
53
53
|
export async function handle(stream, protocols, options) {
|
|
54
54
|
protocols = Array.isArray(protocols) ? protocols : [protocols];
|
|
55
|
+
options.log.trace('handle: available protocols %s', protocols);
|
|
55
56
|
const lp = lpStream(stream, {
|
|
56
|
-
|
|
57
|
+
...options,
|
|
58
|
+
maxDataLength: MAX_PROTOCOL_LENGTH,
|
|
59
|
+
maxLengthLength: 2 // 2 bytes is enough to length-prefix MAX_PROTOCOL_LENGTH
|
|
57
60
|
});
|
|
58
61
|
while (true) {
|
|
59
|
-
options
|
|
62
|
+
options?.log.trace('handle: reading incoming string');
|
|
60
63
|
const protocol = await multistream.readString(lp, options);
|
|
61
|
-
options.log.trace('read "%s"', protocol);
|
|
64
|
+
options.log.trace('handle: read "%s"', protocol);
|
|
62
65
|
if (protocol === PROTOCOL_ID) {
|
|
63
|
-
options.log.trace('respond with "%s" for "%s"', PROTOCOL_ID, protocol);
|
|
66
|
+
options.log.trace('handle: respond with "%s" for "%s"', PROTOCOL_ID, protocol);
|
|
64
67
|
await multistream.write(lp, uint8ArrayFromString(`${PROTOCOL_ID}\n`), options);
|
|
68
|
+
options.log.trace('handle: responded with "%s" for "%s"', PROTOCOL_ID, protocol);
|
|
65
69
|
continue;
|
|
66
70
|
}
|
|
67
71
|
if (protocols.includes(protocol)) {
|
|
72
|
+
options.log.trace('handle: respond with "%s" for "%s"', protocol, protocol);
|
|
68
73
|
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options);
|
|
69
|
-
options.log.trace('
|
|
74
|
+
options.log.trace('handle: responded with "%s" for "%s"', protocol, protocol);
|
|
70
75
|
return { stream: lp.unwrap(), protocol };
|
|
71
76
|
}
|
|
72
77
|
if (protocol === 'ls') {
|
|
73
78
|
// <varint-msg-len><varint-proto-name-len><proto-name>\n<varint-proto-name-len><proto-name>\n\n
|
|
74
79
|
const protos = new Uint8ArrayList(...protocols.map(p => encode.single(uint8ArrayFromString(`${p}\n`))), uint8ArrayFromString('\n'));
|
|
80
|
+
options.log.trace('handle: respond with "%s" for %s', protocols, protocol);
|
|
75
81
|
await multistream.write(lp, protos, options);
|
|
76
|
-
options.log.trace('
|
|
82
|
+
options.log.trace('handle: responded with "%s" for %s', protocols, protocol);
|
|
77
83
|
continue;
|
|
78
84
|
}
|
|
85
|
+
options.log('handle: respond with "na" for "%s"', protocol);
|
|
79
86
|
await multistream.write(lp, uint8ArrayFromString('na\n'), options);
|
|
80
|
-
options.log('
|
|
87
|
+
options.log('handle: responded with "na" for "%s"', protocol);
|
|
81
88
|
}
|
|
82
89
|
}
|
|
83
90
|
//# sourceMappingURL=handle.js.map
|
package/dist/src/handle.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle.js","sourceRoot":"","sources":["../../src/handle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAC3C,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AACpD,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AACjE,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAI/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAyC,MAAc,EAAE,SAA4B,EAAE,OAA8B;IAC/I,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;IAC9D,MAAM,EAAE,GAAG,QAAQ,CAAC,MAAM,EAAE;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CAAC,oCAAoC,EAAE,SAAS,EAAE,QAAQ,CAAC,CAAA;YAC5E,SAAQ;QACV,CAAC;QAED,OAAO,CAAC,GAAG,CAAC,oCAAoC,EAAE,QAAQ,CAAC,CAAA;QAC3D,MAAM,WAAW,CAAC,KAAK,CAAC,EAAE,EAAE,oBAAoB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAA;QAClE,OAAO,CAAC,GAAG,CAAC,sCAAsC,EAAE,QAAQ,CAAC,CAAA;IAC/D,CAAC;AACH,CAAC"}
|
package/dist/src/index.d.ts
CHANGED
|
@@ -21,13 +21,14 @@
|
|
|
21
21
|
*/
|
|
22
22
|
import { PROTOCOL_ID } from './constants.js';
|
|
23
23
|
import type { AbortOptions, LoggerOptions } from '@libp2p/interface';
|
|
24
|
+
import type { LengthPrefixedStreamOpts } from 'it-length-prefixed-stream';
|
|
24
25
|
export { PROTOCOL_ID };
|
|
25
26
|
export interface ProtocolStream<Stream> {
|
|
26
27
|
stream: Stream;
|
|
27
28
|
protocol: string;
|
|
28
29
|
}
|
|
29
|
-
export interface MultistreamSelectInit extends AbortOptions, LoggerOptions {
|
|
30
|
+
export interface MultistreamSelectInit extends AbortOptions, LoggerOptions, Partial<LengthPrefixedStreamOpts> {
|
|
30
31
|
}
|
|
31
|
-
export { select
|
|
32
|
+
export { select } from './select.js';
|
|
32
33
|
export { handle } from './handle.js';
|
|
33
34
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACpE,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAA;AAEzE,OAAO,EAAE,WAAW,EAAE,CAAA;AAEtB,MAAM,WAAW,cAAc,CAAC,MAAM;IACpC,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,qBAAsB,SAAQ,YAAY,EAAE,aAAa,EAAE,OAAO,CAAC,wBAAwB,CAAC;CAE5G;AAED,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
|
package/dist/src/index.js
CHANGED
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAI5C,OAAO,EAAE,WAAW,EAAE,CAAA;AAWtB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA"}
|
|
@@ -6,11 +6,11 @@ import type { Duplex, Source } from 'it-stream-types';
|
|
|
6
6
|
/**
|
|
7
7
|
* `write` encodes and writes a single buffer
|
|
8
8
|
*/
|
|
9
|
-
export declare function write(writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>>, buffer: Uint8Array | Uint8ArrayList, options?: MultistreamSelectInit): Promise<void>;
|
|
9
|
+
export declare function write(writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffer: Uint8Array | Uint8ArrayList, options?: MultistreamSelectInit): Promise<void>;
|
|
10
10
|
/**
|
|
11
11
|
* `writeAll` behaves like `write`, except it encodes an array of items as a single write
|
|
12
12
|
*/
|
|
13
|
-
export declare function writeAll(writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>>, buffers: Uint8Array[], options?: MultistreamSelectInit): Promise<void>;
|
|
13
|
+
export declare function writeAll(writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffers: Uint8Array[], options?: MultistreamSelectInit): Promise<void>;
|
|
14
14
|
/**
|
|
15
15
|
* Read a length-prefixed buffer from the passed stream, stripping the final newline character
|
|
16
16
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multistream.d.ts","sourceRoot":"","sources":["../../src/multistream.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAGpD,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,GAAG,CAAA;AAC9C,OAAO,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA;AACpE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AACrE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAIrD;;GAEG;AACH,wBAAsB,KAAK,CAAE,MAAM,EAAE,oBAAoB,CAAC,MAAM,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,UAAU,GAAG,cA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AAU,CAAE,MAAM,EAAE,oBAAoB,CAAC,MAAM,CAAC,cAAc,CAAC,UAAU,GAAG,cAAc,CAAC,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC,CAIhM"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"multistream.js","sourceRoot":"","sources":["../../src/multistream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAuB,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAMtE,MAAM,OAAO,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAA;AAE1C;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAE,
|
|
1
|
+
{"version":3,"file":"multistream.js","sourceRoot":"","sources":["../../src/multistream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAuB,MAAM,gBAAgB,CAAA;AACpD,OAAO,EAAE,UAAU,IAAI,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AAC5E,OAAO,EAAE,QAAQ,IAAI,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAMtE,MAAM,OAAO,GAAG,oBAAoB,CAAC,IAAI,CAAC,CAAA;AAE1C;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,KAAK,CAAE,MAAqG,EAAE,MAAmC,EAAE,OAA+B;IACtM,MAAM,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AACrC,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAE,MAAqG,EAAE,OAAqB,EAAE,OAA+B;IAC3L,MAAM,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;AACvC,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,IAAI,CAAE,MAAqG,EAAE,OAAsC;IACvK,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IAEtC,IAAI,GAAG,CAAC,UAAU,KAAK,CAAC,IAAI,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,UAAU,GAAG,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACvE,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,uCAAuC,EAAE,GAAG,CAAC,CAAA;QAChE,MAAM,IAAI,SAAS,CAAC,iBAAiB,EAAE,wCAAwC,CAAC,CAAA;IAClF,CAAC;IAED,OAAO,GAAG,CAAC,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA,CAAC,iBAAiB;AAC7C,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAE,MAAqG,EAAE,OAAsC;IAC7K,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAEvC,OAAO,kBAAkB,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAA;AAC3C,CAAC"}
|
package/dist/src/select.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { MultistreamSelectInit, ProtocolStream } from './index.js';
|
|
2
|
+
import type { AbortOptions } from '@libp2p/interface';
|
|
2
3
|
import type { Duplex } from 'it-stream-types';
|
|
4
|
+
export interface SelectStream extends Duplex<any, any, any> {
|
|
5
|
+
readStatus?: string;
|
|
6
|
+
closeWrite?(options?: AbortOptions): Promise<void>;
|
|
7
|
+
closeRead?(options?: AbortOptions): Promise<void>;
|
|
8
|
+
close?(options?: AbortOptions): Promise<void>;
|
|
9
|
+
}
|
|
3
10
|
/**
|
|
4
11
|
* Negotiate a protocol to use from a list of protocols.
|
|
5
12
|
*
|
|
@@ -43,14 +50,5 @@ import type { Duplex } from 'it-stream-types';
|
|
|
43
50
|
* // }
|
|
44
51
|
* ```
|
|
45
52
|
*/
|
|
46
|
-
export declare function select<Stream extends
|
|
47
|
-
/**
|
|
48
|
-
* Lazily negotiates a protocol.
|
|
49
|
-
*
|
|
50
|
-
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
51
|
-
* simply assumes the negotiation went successfully and starts writing data.
|
|
52
|
-
*
|
|
53
|
-
* Use when it is known that the receiver supports the desired protocol.
|
|
54
|
-
*/
|
|
55
|
-
export declare function lazySelect<Stream extends Duplex<any, any, any>>(stream: Stream, protocol: string, options: MultistreamSelectInit): ProtocolStream<Stream>;
|
|
53
|
+
export declare function select<Stream extends SelectStream>(stream: Stream, protocols: string | string[], options: MultistreamSelectInit): Promise<ProtocolStream<Stream>>;
|
|
56
54
|
//# sourceMappingURL=select.d.ts.map
|
package/dist/src/select.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"select.d.ts","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AACvE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AACrD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AAE7C,MAAM,WAAW,YAAa,SAAQ,MAAM,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC;IACzD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,CAAC,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAClD,SAAS,CAAC,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACjD,KAAK,CAAC,CAAC,OAAO,CAAC,EAAE,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CAC9C;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAsB,MAAM,CAAE,MAAM,SAAS,YAAY,EAAG,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,EAAE,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAoDzK"}
|
package/dist/src/select.js
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors';
|
|
2
2
|
import { lpStream } from 'it-length-prefixed-stream';
|
|
3
|
+
import pDefer from 'p-defer';
|
|
4
|
+
import * as varint from 'uint8-varint';
|
|
5
|
+
import { Uint8ArrayList } from 'uint8arraylist';
|
|
3
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string';
|
|
4
7
|
import { MAX_PROTOCOL_LENGTH } from './constants.js';
|
|
5
8
|
import * as multistream from './multistream.js';
|
|
@@ -49,7 +52,11 @@ import { PROTOCOL_ID } from './index.js';
|
|
|
49
52
|
*/
|
|
50
53
|
export async function select(stream, protocols, options) {
|
|
51
54
|
protocols = Array.isArray(protocols) ? [...protocols] : [protocols];
|
|
55
|
+
if (protocols.length === 1) {
|
|
56
|
+
return optimisticSelect(stream, protocols[0], options);
|
|
57
|
+
}
|
|
52
58
|
const lp = lpStream(stream, {
|
|
59
|
+
...options,
|
|
53
60
|
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
54
61
|
});
|
|
55
62
|
const protocol = protocols.shift();
|
|
@@ -60,10 +67,12 @@ export async function select(stream, protocols, options) {
|
|
|
60
67
|
const p1 = uint8ArrayFromString(`${PROTOCOL_ID}\n`);
|
|
61
68
|
const p2 = uint8ArrayFromString(`${protocol}\n`);
|
|
62
69
|
await multistream.writeAll(lp, [p1, p2], options);
|
|
70
|
+
options?.log.trace('select: reading multistream-select header');
|
|
63
71
|
let response = await multistream.readString(lp, options);
|
|
64
72
|
options?.log.trace('select: read "%s"', response);
|
|
65
73
|
// Read the protocol response if we got the protocolId in return
|
|
66
74
|
if (response === PROTOCOL_ID) {
|
|
75
|
+
options?.log.trace('select: reading protocol response');
|
|
67
76
|
response = await multistream.readString(lp, options);
|
|
68
77
|
options?.log.trace('select: read "%s"', response);
|
|
69
78
|
}
|
|
@@ -75,6 +84,7 @@ export async function select(stream, protocols, options) {
|
|
|
75
84
|
for (const protocol of protocols) {
|
|
76
85
|
options?.log.trace('select: write "%s"', protocol);
|
|
77
86
|
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options);
|
|
87
|
+
options?.log.trace('select: reading protocol response');
|
|
78
88
|
const response = await multistream.readString(lp, options);
|
|
79
89
|
options?.log.trace('select: read "%s" for "%s"', response, protocol);
|
|
80
90
|
if (response === protocol) {
|
|
@@ -84,45 +94,183 @@ export async function select(stream, protocols, options) {
|
|
|
84
94
|
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
85
95
|
}
|
|
86
96
|
/**
|
|
87
|
-
*
|
|
97
|
+
* Optimistically negotiates a protocol.
|
|
88
98
|
*
|
|
89
99
|
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
90
100
|
* simply assumes the negotiation went successfully and starts writing data.
|
|
91
101
|
*
|
|
92
102
|
* Use when it is known that the receiver supports the desired protocol.
|
|
93
103
|
*/
|
|
94
|
-
|
|
104
|
+
function optimisticSelect(stream, protocol, options) {
|
|
95
105
|
const originalSink = stream.sink.bind(stream);
|
|
96
106
|
const originalSource = stream.source;
|
|
107
|
+
let negotiated = false;
|
|
108
|
+
let negotiating = false;
|
|
109
|
+
const doneNegotiating = pDefer();
|
|
110
|
+
let sentProtocol = false;
|
|
111
|
+
let sendingProtocol = false;
|
|
112
|
+
const doneSendingProtocol = pDefer();
|
|
113
|
+
let readProtocol = false;
|
|
114
|
+
let readingProtocol = false;
|
|
115
|
+
const doneReadingProtocol = pDefer();
|
|
97
116
|
const lp = lpStream({
|
|
98
117
|
sink: originalSink,
|
|
99
118
|
source: originalSource
|
|
100
119
|
}, {
|
|
120
|
+
...options,
|
|
101
121
|
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
102
122
|
});
|
|
103
123
|
stream.sink = async (source) => {
|
|
104
|
-
|
|
105
|
-
await
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
124
|
+
const { sink } = lp.unwrap();
|
|
125
|
+
await sink(async function* () {
|
|
126
|
+
let sentData = false;
|
|
127
|
+
for await (const buf of source) {
|
|
128
|
+
// started reading before the source yielded, wait for protocol send
|
|
129
|
+
if (sendingProtocol) {
|
|
130
|
+
await doneSendingProtocol.promise;
|
|
131
|
+
}
|
|
132
|
+
// writing before reading, send the protocol and the first chunk of data
|
|
133
|
+
if (!sentProtocol) {
|
|
134
|
+
sendingProtocol = true;
|
|
135
|
+
options?.log.trace('optimistic: write ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength);
|
|
136
|
+
const protocolString = `${protocol}\n`;
|
|
137
|
+
// send protocols in first chunk of data written to transport
|
|
138
|
+
yield new Uint8ArrayList(Uint8Array.from([19]), // length of PROTOCOL_ID plus newline
|
|
139
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`), varint.encode(protocolString.length), uint8ArrayFromString(protocolString), buf).subarray();
|
|
140
|
+
options?.log.trace('optimistic: wrote ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength);
|
|
141
|
+
sentProtocol = true;
|
|
142
|
+
sendingProtocol = false;
|
|
143
|
+
doneSendingProtocol.resolve();
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
yield buf;
|
|
147
|
+
}
|
|
148
|
+
sentData = true;
|
|
149
|
+
}
|
|
150
|
+
// special case - the source passed to the sink has ended but we didn't
|
|
151
|
+
// negotiated the protocol yet so do it now
|
|
152
|
+
if (!sentData) {
|
|
153
|
+
await negotiate();
|
|
154
|
+
}
|
|
155
|
+
}());
|
|
111
156
|
};
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
157
|
+
async function negotiate() {
|
|
158
|
+
if (negotiating) {
|
|
159
|
+
options?.log.trace('optimistic: already negotiating %s stream', protocol);
|
|
160
|
+
await doneNegotiating.promise;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
negotiating = true;
|
|
164
|
+
try {
|
|
165
|
+
// we haven't sent the protocol yet, send it now
|
|
166
|
+
if (!sentProtocol) {
|
|
167
|
+
options?.log.trace('optimistic: doing send protocol for %s stream', protocol);
|
|
168
|
+
await doSendProtocol();
|
|
169
|
+
}
|
|
170
|
+
// if we haven't read the protocol response yet, do it now
|
|
171
|
+
if (!readProtocol) {
|
|
172
|
+
options?.log.trace('optimistic: doing read protocol for %s stream', protocol);
|
|
173
|
+
await doReadProtocol();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
finally {
|
|
177
|
+
negotiating = false;
|
|
178
|
+
negotiated = true;
|
|
179
|
+
doneNegotiating.resolve();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
async function doSendProtocol() {
|
|
183
|
+
if (sendingProtocol) {
|
|
184
|
+
await doneSendingProtocol.promise;
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
sendingProtocol = true;
|
|
188
|
+
try {
|
|
189
|
+
options?.log.trace('optimistic: write ["%s", "%s", data] in source', PROTOCOL_ID, protocol);
|
|
190
|
+
await lp.writeV([
|
|
191
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`),
|
|
192
|
+
uint8ArrayFromString(`${protocol}\n`)
|
|
193
|
+
]);
|
|
194
|
+
options?.log.trace('optimistic: wrote ["%s", "%s", data] in source', PROTOCOL_ID, protocol);
|
|
195
|
+
}
|
|
196
|
+
finally {
|
|
197
|
+
sentProtocol = true;
|
|
198
|
+
sendingProtocol = false;
|
|
199
|
+
doneSendingProtocol.resolve();
|
|
118
200
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
201
|
+
}
|
|
202
|
+
async function doReadProtocol() {
|
|
203
|
+
if (readingProtocol) {
|
|
204
|
+
await doneReadingProtocol.promise;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
readingProtocol = true;
|
|
208
|
+
try {
|
|
209
|
+
options?.log.trace('optimistic: reading multistream select header');
|
|
210
|
+
let response = await multistream.readString(lp, options);
|
|
211
|
+
options?.log.trace('optimistic: read multistream select header "%s"', response);
|
|
212
|
+
if (response === PROTOCOL_ID) {
|
|
213
|
+
response = await multistream.readString(lp, options);
|
|
214
|
+
}
|
|
215
|
+
options?.log.trace('optimistic: read protocol "%s", expecting "%s"', response, protocol);
|
|
216
|
+
if (response !== protocol) {
|
|
217
|
+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL');
|
|
218
|
+
}
|
|
122
219
|
}
|
|
123
|
-
|
|
220
|
+
finally {
|
|
221
|
+
readProtocol = true;
|
|
222
|
+
readingProtocol = false;
|
|
223
|
+
doneReadingProtocol.resolve();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
stream.source = (async function* () {
|
|
227
|
+
// make sure we've done protocol negotiation before we read stream data
|
|
228
|
+
await negotiate();
|
|
229
|
+
options?.log.trace('optimistic: reading data from "%s" stream', protocol);
|
|
124
230
|
yield* lp.unwrap().source;
|
|
125
231
|
})();
|
|
232
|
+
if (stream.closeRead != null) {
|
|
233
|
+
const originalCloseRead = stream.closeRead.bind(stream);
|
|
234
|
+
stream.closeRead = async (opts) => {
|
|
235
|
+
// we need to read & write to negotiate the protocol so ensure we've done
|
|
236
|
+
// this before closing the readable end of the stream
|
|
237
|
+
if (!negotiated) {
|
|
238
|
+
await negotiate().catch(err => {
|
|
239
|
+
options?.log.error('could not negotiate protocol before close read', err);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
// protocol has been negotiated, ok to close the readable end
|
|
243
|
+
await originalCloseRead(opts);
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
if (stream.closeWrite != null) {
|
|
247
|
+
const originalCloseWrite = stream.closeWrite.bind(stream);
|
|
248
|
+
stream.closeWrite = async (opts) => {
|
|
249
|
+
// we need to read & write to negotiate the protocol so ensure we've done
|
|
250
|
+
// this before closing the writable end of the stream
|
|
251
|
+
if (!negotiated) {
|
|
252
|
+
await negotiate().catch(err => {
|
|
253
|
+
options?.log.error('could not negotiate protocol before close write', err);
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
// protocol has been negotiated, ok to close the writable end
|
|
257
|
+
await originalCloseWrite(opts);
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
if (stream.close != null) {
|
|
261
|
+
const originalClose = stream.close.bind(stream);
|
|
262
|
+
stream.close = async (opts) => {
|
|
263
|
+
// the stream is being closed, don't try to negotiate a protocol if we
|
|
264
|
+
// haven't already
|
|
265
|
+
if (!negotiated) {
|
|
266
|
+
negotiated = true;
|
|
267
|
+
negotiating = false;
|
|
268
|
+
doneNegotiating.resolve();
|
|
269
|
+
}
|
|
270
|
+
// protocol has been negotiated, ok to close the writable end
|
|
271
|
+
await originalClose(opts);
|
|
272
|
+
};
|
|
273
|
+
}
|
|
126
274
|
return {
|
|
127
275
|
stream,
|
|
128
276
|
protocol
|
package/dist/src/select.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"select.js","sourceRoot":"","sources":["../../src/select.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAA;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAA;AACpD,OAAO,EAAE,UAAU,IAAI,oBAAoB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,EAAE,GAAG,CAAC,CAAA;gBAC5E,CAAC,CAAC,CAAA;YACJ,CAAC;YAED,6DAA6D;YAC7D,MAAM,kBAAkB,CAAC,IAAI,CAAC,CAAA;QAChC,CAAC,CAAA;IACH,CAAC;IAED,IAAI,MAAM,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;QACzB,MAAM,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAE/C,MAAM,CAAC,KAAK,GAAG,KAAK,EAAE,IAAI,EAAE,EAAE;YAC5B,sEAAsE;YACtE,kBAAkB;YAClB,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChB,UAAU,GAAG,IAAI,CAAA;gBACjB,WAAW,GAAG,KAAK,CAAA;gBACnB,eAAe,CAAC,OAAO,EAAE,CAAA;YAC3B,CAAC;YAED,6DAA6D;YAC7D,MAAM,aAAa,CAAC,IAAI,CAAC,CAAA;QAC3B,CAAC,CAAA;IACH,CAAC;IAED,OAAO;QACL,MAAM;QACN,QAAQ;KACT,CAAA;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/multistream-select",
|
|
3
|
-
"version": "4.0.6-
|
|
3
|
+
"version": "4.0.6-68db79f6b",
|
|
4
4
|
"description": "JavaScript implementation of multistream-select",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
|
-
"homepage": "https://github.com/libp2p/js-libp2p/tree/
|
|
6
|
+
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/multistream-select#readme",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/libp2p/js-libp2p.git"
|
|
@@ -11,6 +11,10 @@
|
|
|
11
11
|
"bugs": {
|
|
12
12
|
"url": "https://github.com/libp2p/js-libp2p/issues"
|
|
13
13
|
},
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public",
|
|
16
|
+
"provenance": true
|
|
17
|
+
},
|
|
14
18
|
"keywords": [
|
|
15
19
|
"ipfs",
|
|
16
20
|
"libp2p",
|
|
@@ -53,16 +57,18 @@
|
|
|
53
57
|
"test:electron-main": "aegir test -t electron-main"
|
|
54
58
|
},
|
|
55
59
|
"dependencies": {
|
|
56
|
-
"@libp2p/interface": "0.1.6-
|
|
60
|
+
"@libp2p/interface": "0.1.6-68db79f6b",
|
|
57
61
|
"it-length-prefixed": "^9.0.3",
|
|
58
62
|
"it-length-prefixed-stream": "^1.1.1",
|
|
59
63
|
"it-pipe": "^3.0.1",
|
|
60
64
|
"it-stream-types": "^2.0.1",
|
|
65
|
+
"p-defer": "^4.0.0",
|
|
66
|
+
"uint8-varint": "^2.0.2",
|
|
61
67
|
"uint8arraylist": "^2.4.3",
|
|
62
68
|
"uint8arrays": "^4.0.6"
|
|
63
69
|
},
|
|
64
70
|
"devDependencies": {
|
|
65
|
-
"@libp2p/logger": "3.1.0-
|
|
71
|
+
"@libp2p/logger": "3.1.0-68db79f6b",
|
|
66
72
|
"aegir": "^41.0.2",
|
|
67
73
|
"iso-random-stream": "^2.0.2",
|
|
68
74
|
"it-all": "^3.0.3",
|
package/src/handle.ts
CHANGED
|
@@ -55,24 +55,30 @@ import type { Duplex } from 'it-stream-types'
|
|
|
55
55
|
*/
|
|
56
56
|
export async function handle <Stream extends Duplex<any, any, any>> (stream: Stream, protocols: string | string[], options: MultistreamSelectInit): Promise<ProtocolStream<Stream>> {
|
|
57
57
|
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
58
|
+
options.log.trace('handle: available protocols %s', protocols)
|
|
59
|
+
|
|
58
60
|
const lp = lpStream(stream, {
|
|
59
|
-
|
|
61
|
+
...options,
|
|
62
|
+
maxDataLength: MAX_PROTOCOL_LENGTH,
|
|
63
|
+
maxLengthLength: 2 // 2 bytes is enough to length-prefix MAX_PROTOCOL_LENGTH
|
|
60
64
|
})
|
|
61
65
|
|
|
62
66
|
while (true) {
|
|
63
|
-
options
|
|
67
|
+
options?.log.trace('handle: reading incoming string')
|
|
64
68
|
const protocol = await multistream.readString(lp, options)
|
|
65
|
-
options.log.trace('read "%s"', protocol)
|
|
69
|
+
options.log.trace('handle: read "%s"', protocol)
|
|
66
70
|
|
|
67
71
|
if (protocol === PROTOCOL_ID) {
|
|
68
|
-
options.log.trace('respond with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
72
|
+
options.log.trace('handle: respond with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
69
73
|
await multistream.write(lp, uint8ArrayFromString(`${PROTOCOL_ID}\n`), options)
|
|
74
|
+
options.log.trace('handle: responded with "%s" for "%s"', PROTOCOL_ID, protocol)
|
|
70
75
|
continue
|
|
71
76
|
}
|
|
72
77
|
|
|
73
78
|
if (protocols.includes(protocol)) {
|
|
79
|
+
options.log.trace('handle: respond with "%s" for "%s"', protocol, protocol)
|
|
74
80
|
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
|
|
75
|
-
options.log.trace('
|
|
81
|
+
options.log.trace('handle: responded with "%s" for "%s"', protocol, protocol)
|
|
76
82
|
|
|
77
83
|
return { stream: lp.unwrap(), protocol }
|
|
78
84
|
}
|
|
@@ -84,12 +90,14 @@ export async function handle <Stream extends Duplex<any, any, any>> (stream: Str
|
|
|
84
90
|
uint8ArrayFromString('\n')
|
|
85
91
|
)
|
|
86
92
|
|
|
93
|
+
options.log.trace('handle: respond with "%s" for %s', protocols, protocol)
|
|
87
94
|
await multistream.write(lp, protos, options)
|
|
88
|
-
options.log.trace('
|
|
95
|
+
options.log.trace('handle: responded with "%s" for %s', protocols, protocol)
|
|
89
96
|
continue
|
|
90
97
|
}
|
|
91
98
|
|
|
99
|
+
options.log('handle: respond with "na" for "%s"', protocol)
|
|
92
100
|
await multistream.write(lp, uint8ArrayFromString('na\n'), options)
|
|
93
|
-
options.log('
|
|
101
|
+
options.log('handle: responded with "na" for "%s"', protocol)
|
|
94
102
|
}
|
|
95
103
|
}
|
package/src/index.ts
CHANGED
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
|
|
23
23
|
import { PROTOCOL_ID } from './constants.js'
|
|
24
24
|
import type { AbortOptions, LoggerOptions } from '@libp2p/interface'
|
|
25
|
+
import type { LengthPrefixedStreamOpts } from 'it-length-prefixed-stream'
|
|
25
26
|
|
|
26
27
|
export { PROTOCOL_ID }
|
|
27
28
|
|
|
@@ -30,9 +31,9 @@ export interface ProtocolStream<Stream> {
|
|
|
30
31
|
protocol: string
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
export interface MultistreamSelectInit extends AbortOptions, LoggerOptions {
|
|
34
|
+
export interface MultistreamSelectInit extends AbortOptions, LoggerOptions, Partial<LengthPrefixedStreamOpts> {
|
|
34
35
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
|
-
export { select
|
|
38
|
+
export { select } from './select.js'
|
|
38
39
|
export { handle } from './handle.js'
|
package/src/multistream.ts
CHANGED
|
@@ -12,14 +12,14 @@ const NewLine = uint8ArrayFromString('\n')
|
|
|
12
12
|
/**
|
|
13
13
|
* `write` encodes and writes a single buffer
|
|
14
14
|
*/
|
|
15
|
-
export async function write (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>>, buffer: Uint8Array | Uint8ArrayList, options?: MultistreamSelectInit): Promise<void> {
|
|
15
|
+
export async function write (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffer: Uint8Array | Uint8ArrayList, options?: MultistreamSelectInit): Promise<void> {
|
|
16
16
|
await writer.write(buffer, options)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* `writeAll` behaves like `write`, except it encodes an array of items as a single write
|
|
21
21
|
*/
|
|
22
|
-
export async function writeAll (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>>>, buffers: Uint8Array[], options?: MultistreamSelectInit): Promise<void> {
|
|
22
|
+
export async function writeAll (writer: LengthPrefixedStream<Duplex<AsyncGenerator<Uint8Array | Uint8ArrayList>, Source<Uint8Array>>>, buffers: Uint8Array[], options?: MultistreamSelectInit): Promise<void> {
|
|
23
23
|
await writer.writeV(buffers, options)
|
|
24
24
|
}
|
|
25
25
|
|
package/src/select.ts
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import { CodeError } from '@libp2p/interface/errors'
|
|
2
2
|
import { lpStream } from 'it-length-prefixed-stream'
|
|
3
|
+
import pDefer from 'p-defer'
|
|
4
|
+
import * as varint from 'uint8-varint'
|
|
5
|
+
import { Uint8ArrayList } from 'uint8arraylist'
|
|
3
6
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
4
7
|
import { MAX_PROTOCOL_LENGTH } from './constants.js'
|
|
5
8
|
import * as multistream from './multistream.js'
|
|
6
9
|
import { PROTOCOL_ID } from './index.js'
|
|
7
10
|
import type { MultistreamSelectInit, ProtocolStream } from './index.js'
|
|
11
|
+
import type { AbortOptions } from '@libp2p/interface'
|
|
8
12
|
import type { Duplex } from 'it-stream-types'
|
|
9
13
|
|
|
14
|
+
export interface SelectStream extends Duplex<any, any, any> {
|
|
15
|
+
readStatus?: string
|
|
16
|
+
closeWrite?(options?: AbortOptions): Promise<void>
|
|
17
|
+
closeRead?(options?: AbortOptions): Promise<void>
|
|
18
|
+
close?(options?: AbortOptions): Promise<void>
|
|
19
|
+
}
|
|
20
|
+
|
|
10
21
|
/**
|
|
11
22
|
* Negotiate a protocol to use from a list of protocols.
|
|
12
23
|
*
|
|
@@ -50,9 +61,15 @@ import type { Duplex } from 'it-stream-types'
|
|
|
50
61
|
* // }
|
|
51
62
|
* ```
|
|
52
63
|
*/
|
|
53
|
-
export async function select <Stream extends
|
|
64
|
+
export async function select <Stream extends SelectStream> (stream: Stream, protocols: string | string[], options: MultistreamSelectInit): Promise<ProtocolStream<Stream>> {
|
|
54
65
|
protocols = Array.isArray(protocols) ? [...protocols] : [protocols]
|
|
66
|
+
|
|
67
|
+
if (protocols.length === 1) {
|
|
68
|
+
return optimisticSelect(stream, protocols[0], options)
|
|
69
|
+
}
|
|
70
|
+
|
|
55
71
|
const lp = lpStream(stream, {
|
|
72
|
+
...options,
|
|
56
73
|
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
57
74
|
})
|
|
58
75
|
const protocol = protocols.shift()
|
|
@@ -66,11 +83,13 @@ export async function select <Stream extends Duplex<any, any, any>> (stream: Str
|
|
|
66
83
|
const p2 = uint8ArrayFromString(`${protocol}\n`)
|
|
67
84
|
await multistream.writeAll(lp, [p1, p2], options)
|
|
68
85
|
|
|
86
|
+
options?.log.trace('select: reading multistream-select header')
|
|
69
87
|
let response = await multistream.readString(lp, options)
|
|
70
88
|
options?.log.trace('select: read "%s"', response)
|
|
71
89
|
|
|
72
90
|
// Read the protocol response if we got the protocolId in return
|
|
73
91
|
if (response === PROTOCOL_ID) {
|
|
92
|
+
options?.log.trace('select: reading protocol response')
|
|
74
93
|
response = await multistream.readString(lp, options)
|
|
75
94
|
options?.log.trace('select: read "%s"', response)
|
|
76
95
|
}
|
|
@@ -84,6 +103,7 @@ export async function select <Stream extends Duplex<any, any, any>> (stream: Str
|
|
|
84
103
|
for (const protocol of protocols) {
|
|
85
104
|
options?.log.trace('select: write "%s"', protocol)
|
|
86
105
|
await multistream.write(lp, uint8ArrayFromString(`${protocol}\n`), options)
|
|
106
|
+
options?.log.trace('select: reading protocol response')
|
|
87
107
|
const response = await multistream.readString(lp, options)
|
|
88
108
|
options?.log.trace('select: read "%s" for "%s"', response, protocol)
|
|
89
109
|
|
|
@@ -96,56 +116,224 @@ export async function select <Stream extends Duplex<any, any, any>> (stream: Str
|
|
|
96
116
|
}
|
|
97
117
|
|
|
98
118
|
/**
|
|
99
|
-
*
|
|
119
|
+
* Optimistically negotiates a protocol.
|
|
100
120
|
*
|
|
101
121
|
* It *does not* block writes waiting for the other end to respond. Instead, it
|
|
102
122
|
* simply assumes the negotiation went successfully and starts writing data.
|
|
103
123
|
*
|
|
104
124
|
* Use when it is known that the receiver supports the desired protocol.
|
|
105
125
|
*/
|
|
106
|
-
|
|
126
|
+
function optimisticSelect <Stream extends SelectStream> (stream: Stream, protocol: string, options: MultistreamSelectInit): ProtocolStream<Stream> {
|
|
107
127
|
const originalSink = stream.sink.bind(stream)
|
|
108
128
|
const originalSource = stream.source
|
|
109
129
|
|
|
130
|
+
let negotiated = false
|
|
131
|
+
let negotiating = false
|
|
132
|
+
const doneNegotiating = pDefer()
|
|
133
|
+
|
|
134
|
+
let sentProtocol = false
|
|
135
|
+
let sendingProtocol = false
|
|
136
|
+
const doneSendingProtocol = pDefer()
|
|
137
|
+
|
|
138
|
+
let readProtocol = false
|
|
139
|
+
let readingProtocol = false
|
|
140
|
+
const doneReadingProtocol = pDefer()
|
|
141
|
+
|
|
110
142
|
const lp = lpStream({
|
|
111
143
|
sink: originalSink,
|
|
112
144
|
source: originalSource
|
|
113
145
|
}, {
|
|
146
|
+
...options,
|
|
114
147
|
maxDataLength: MAX_PROTOCOL_LENGTH
|
|
115
148
|
})
|
|
116
149
|
|
|
117
150
|
stream.sink = async source => {
|
|
118
|
-
|
|
151
|
+
const { sink } = lp.unwrap()
|
|
152
|
+
|
|
153
|
+
await sink(async function * () {
|
|
154
|
+
let sentData = false
|
|
155
|
+
|
|
156
|
+
for await (const buf of source) {
|
|
157
|
+
// started reading before the source yielded, wait for protocol send
|
|
158
|
+
if (sendingProtocol) {
|
|
159
|
+
await doneSendingProtocol.promise
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// writing before reading, send the protocol and the first chunk of data
|
|
163
|
+
if (!sentProtocol) {
|
|
164
|
+
sendingProtocol = true
|
|
165
|
+
|
|
166
|
+
options?.log.trace('optimistic: write ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength)
|
|
167
|
+
|
|
168
|
+
const protocolString = `${protocol}\n`
|
|
119
169
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
170
|
+
// send protocols in first chunk of data written to transport
|
|
171
|
+
yield new Uint8ArrayList(
|
|
172
|
+
Uint8Array.from([19]), // length of PROTOCOL_ID plus newline
|
|
173
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`),
|
|
174
|
+
varint.encode(protocolString.length),
|
|
175
|
+
uint8ArrayFromString(protocolString),
|
|
176
|
+
buf
|
|
177
|
+
).subarray()
|
|
124
178
|
|
|
125
|
-
|
|
126
|
-
|
|
179
|
+
options?.log.trace('optimistic: wrote ["%s", "%s", data(%d)] in sink', PROTOCOL_ID, protocol, buf.byteLength)
|
|
180
|
+
|
|
181
|
+
sentProtocol = true
|
|
182
|
+
sendingProtocol = false
|
|
183
|
+
doneSendingProtocol.resolve()
|
|
184
|
+
} else {
|
|
185
|
+
yield buf
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
sentData = true
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// special case - the source passed to the sink has ended but we didn't
|
|
192
|
+
// negotiated the protocol yet so do it now
|
|
193
|
+
if (!sentData) {
|
|
194
|
+
await negotiate()
|
|
195
|
+
}
|
|
196
|
+
}())
|
|
127
197
|
}
|
|
128
198
|
|
|
129
|
-
|
|
130
|
-
|
|
199
|
+
async function negotiate (): Promise<void> {
|
|
200
|
+
if (negotiating) {
|
|
201
|
+
options?.log.trace('optimistic: already negotiating %s stream', protocol)
|
|
202
|
+
await doneNegotiating.promise
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
negotiating = true
|
|
131
207
|
|
|
132
|
-
|
|
133
|
-
|
|
208
|
+
try {
|
|
209
|
+
// we haven't sent the protocol yet, send it now
|
|
210
|
+
if (!sentProtocol) {
|
|
211
|
+
options?.log.trace('optimistic: doing send protocol for %s stream', protocol)
|
|
212
|
+
await doSendProtocol()
|
|
213
|
+
}
|
|
134
214
|
|
|
135
|
-
|
|
136
|
-
|
|
215
|
+
// if we haven't read the protocol response yet, do it now
|
|
216
|
+
if (!readProtocol) {
|
|
217
|
+
options?.log.trace('optimistic: doing read protocol for %s stream', protocol)
|
|
218
|
+
await doReadProtocol()
|
|
219
|
+
}
|
|
220
|
+
} finally {
|
|
221
|
+
negotiating = false
|
|
222
|
+
negotiated = true
|
|
223
|
+
doneNegotiating.resolve()
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
async function doSendProtocol (): Promise<void> {
|
|
228
|
+
if (sendingProtocol) {
|
|
229
|
+
await doneSendingProtocol.promise
|
|
230
|
+
return
|
|
137
231
|
}
|
|
138
232
|
|
|
139
|
-
|
|
233
|
+
sendingProtocol = true
|
|
234
|
+
|
|
235
|
+
try {
|
|
236
|
+
options?.log.trace('optimistic: write ["%s", "%s", data] in source', PROTOCOL_ID, protocol)
|
|
237
|
+
await lp.writeV([
|
|
238
|
+
uint8ArrayFromString(`${PROTOCOL_ID}\n`),
|
|
239
|
+
uint8ArrayFromString(`${protocol}\n`)
|
|
240
|
+
])
|
|
241
|
+
options?.log.trace('optimistic: wrote ["%s", "%s", data] in source', PROTOCOL_ID, protocol)
|
|
242
|
+
} finally {
|
|
243
|
+
sentProtocol = true
|
|
244
|
+
sendingProtocol = false
|
|
245
|
+
doneSendingProtocol.resolve()
|
|
246
|
+
}
|
|
247
|
+
}
|
|
140
248
|
|
|
141
|
-
|
|
142
|
-
|
|
249
|
+
async function doReadProtocol (): Promise<void> {
|
|
250
|
+
if (readingProtocol) {
|
|
251
|
+
await doneReadingProtocol.promise
|
|
252
|
+
return
|
|
143
253
|
}
|
|
144
254
|
|
|
145
|
-
|
|
255
|
+
readingProtocol = true
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
options?.log.trace('optimistic: reading multistream select header')
|
|
259
|
+
let response = await multistream.readString(lp, options)
|
|
260
|
+
options?.log.trace('optimistic: read multistream select header "%s"', response)
|
|
261
|
+
|
|
262
|
+
if (response === PROTOCOL_ID) {
|
|
263
|
+
response = await multistream.readString(lp, options)
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
options?.log.trace('optimistic: read protocol "%s", expecting "%s"', response, protocol)
|
|
267
|
+
|
|
268
|
+
if (response !== protocol) {
|
|
269
|
+
throw new CodeError('protocol selection failed', 'ERR_UNSUPPORTED_PROTOCOL')
|
|
270
|
+
}
|
|
271
|
+
} finally {
|
|
272
|
+
readProtocol = true
|
|
273
|
+
readingProtocol = false
|
|
274
|
+
doneReadingProtocol.resolve()
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
stream.source = (async function * () {
|
|
279
|
+
// make sure we've done protocol negotiation before we read stream data
|
|
280
|
+
await negotiate()
|
|
281
|
+
|
|
282
|
+
options?.log.trace('optimistic: reading data from "%s" stream', protocol)
|
|
146
283
|
yield * lp.unwrap().source
|
|
147
284
|
})()
|
|
148
285
|
|
|
286
|
+
if (stream.closeRead != null) {
|
|
287
|
+
const originalCloseRead = stream.closeRead.bind(stream)
|
|
288
|
+
|
|
289
|
+
stream.closeRead = async (opts) => {
|
|
290
|
+
// we need to read & write to negotiate the protocol so ensure we've done
|
|
291
|
+
// this before closing the readable end of the stream
|
|
292
|
+
if (!negotiated) {
|
|
293
|
+
await negotiate().catch(err => {
|
|
294
|
+
options?.log.error('could not negotiate protocol before close read', err)
|
|
295
|
+
})
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// protocol has been negotiated, ok to close the readable end
|
|
299
|
+
await originalCloseRead(opts)
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (stream.closeWrite != null) {
|
|
304
|
+
const originalCloseWrite = stream.closeWrite.bind(stream)
|
|
305
|
+
|
|
306
|
+
stream.closeWrite = async (opts) => {
|
|
307
|
+
// we need to read & write to negotiate the protocol so ensure we've done
|
|
308
|
+
// this before closing the writable end of the stream
|
|
309
|
+
if (!negotiated) {
|
|
310
|
+
await negotiate().catch(err => {
|
|
311
|
+
options?.log.error('could not negotiate protocol before close write', err)
|
|
312
|
+
})
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// protocol has been negotiated, ok to close the writable end
|
|
316
|
+
await originalCloseWrite(opts)
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
if (stream.close != null) {
|
|
321
|
+
const originalClose = stream.close.bind(stream)
|
|
322
|
+
|
|
323
|
+
stream.close = async (opts) => {
|
|
324
|
+
// the stream is being closed, don't try to negotiate a protocol if we
|
|
325
|
+
// haven't already
|
|
326
|
+
if (!negotiated) {
|
|
327
|
+
negotiated = true
|
|
328
|
+
negotiating = false
|
|
329
|
+
doneNegotiating.resolve()
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// protocol has been negotiated, ok to close the writable end
|
|
333
|
+
await originalClose(opts)
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
149
337
|
return {
|
|
150
338
|
stream,
|
|
151
339
|
protocol
|