/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/@zacharygriffee/protoplex@2.4.0/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
import{EventEmitter}from"eventemitter3";import Protomux from"protomux";import{Duplex}from"streamx";import c from"compact-encoding";import b4a from"b4a";import BufferMap from"tiny-buffer-map";import{Subject,ReplaySubject,firstValueFrom,defaultIfEmpty}from"rxjs";import{concatMap}from"rxjs/operators";const PROTOCOL="protoplex/zacharygriffee";globalThis.setImmediate||=function(e){return setTimeout(e)};export class ProtoplexStream extends Duplex{constructor(e,t={}){const{id:n,handshake:o,handshakeEncoding:s,onhandshake:i,onreject:h=()=>{},encoding:r,unique:a,userData:l,protocol:u,incomingBufferSize:d=100,incomingBufferTime:m=1e4,...p}=t;if(super({...p,eagerOpen:!0}),!e.mux?.isProtomux)throw new Error("mux not an instance of Protomux!");this.plex=e,this.mux=e.mux,this.protocol=u||PROTOCOL,this.id=n??b4a.from([]),this.handshake=o??b4a.from([]),this.handshakeEncoding=s??c.raw,this.encoding=r?c.raw.array(r):c.raw.array(c.raw),this.unique=a??!1,this.onhandshake=i??null,this.onreject=h??null,this.remoteHandshake=null,this.userData=l??null,this.opened=!1,this.incoming$=new ReplaySubject(d,m),this.outgoing$=new Subject,this.drain$=new Subject,this._onopen=null,this._openWith=null,this._incomingSub=null,this.channel=e.mux.createChannel({protocol:this.protocol,id:this.id,handshake:this.handshakeEncoding,unique:this.unique,messages:[{encoding:this.encoding,onmessage:this._onmessage.bind(this)}],onopen:e=>this._onchannelopen(e),onclose:()=>setImmediate((()=>this.push(null))),ondestroy:()=>setImmediate((()=>this.destroy())),ondrain:()=>this._onDrainEvent()}),this.outgoing$.pipe(concatMap((e=>this.channel.messages[0].send(e)?Promise.resolve(!0):firstValueFrom(this.drain$.pipe(defaultIfEmpty(!0)))))).subscribe({error:e=>this._handleError(e)}),this.channel.open(this.handshake)}_onDrainEvent(){this.drain$.next(!0)}_writev(e,t){this.destroyed?t(new Error("Stream destroyed")):(this.outgoing$.next(e),t(null))}_read(e){this._incomingSub||(this._incomingSub=this.incoming$.subscribe({next:e=>{this.push(e)},error:e=>{console.error("Incoming stream error:",e),this.emit("error",e)},complete:()=>{this.destroyed||this.push(null)}})),e(null)}_final(e){this.channel?.close(),e(null)}_destroy(e){this.channel.close(),this.incoming$.complete(),this.outgoing$.complete(),this.drain$.complete(),this._incomingSub&&(this._incomingSub.unsubscribe(),this._incomingSub=null),e(null)}_predestroy(){this.opened=!1,this.channel?.close(),this._maybeOpen(new Error("Stream was destroyed!"))}_onmessage(e){if(e.length)for(const t of e)this.incoming$.next(t);else this.incoming$.next(b4a.alloc(0))}async _onhandshake(e){return!this.onhandshake||this.onhandshake(e)}_maybeOpen(e){this._openWith=this._openWith??e;const t=this._onopen;this._onopen=null,t&&(this.opened=!0,t(this._openWith))}_handleError(e){this.channel.close(),this.destroy(e)}_open(e){this._onopen=e,this.channel.opened&&this._maybeOpen(null)}async _onchannelopen(e){try{if(!await this._onhandshake(e)){const e=new Error("Connection rejected by server");return this.onreject(e),this.emit("reject",e),this.channel.close(),void this.destroy()}this.remoteHandshake=e,this.emit("connect"),this._maybeOpen(null)}catch(e){this.onreject(e),this.emit("reject",e),this.channel.close(),this.destroy()}}}function toKey(e,t){return e?b4a.concat([b4a.from(t+"###"),e]):b4a.from(t+"###")}export default class Protoplex extends EventEmitter{constructor(e,t={}){const{id:n,handshake:o,handshakeEncoding:s,onhandshake:i,encoding:h,unique:r,protocol:a,...c}=t;super(),this.mux=e,this.id=n??null,this.handshake=o??null,this.handshakeEncoding=s??null,this.onhandshake=i??null,this.encoding=h??null,this.unique=r??null,this.streamOpts=c??{},this.protocol=a||PROTOCOL,this._streams=new Set,this._listeners=new BufferMap}static from(e,t={}){const n=e.isProtomux?e:Protomux.from(e);return new Protoplex(n,t)}get isProtoplex(){return!0}listen(e,t={}){e&&!b4a.isBuffer(e)&&(t=e,e=null);const n=t.protocol||this.protocol||PROTOCOL,o=toKey(e=e??this.id,n);return this._listeners.has(o)||(this._listeners.set(o,t),this.mux.pair({protocol:n,id:e},this._onpair.bind(this,n))),this}unlisten(e={}){const t=e.protocol||this.protocol||PROTOCOL,n=e.id??this.id,o=toKey(n,t);return this._listeners.has(o)?(this._listeners.delete(o),this.mux.unpair({protocol:t,id:n}),this):this}connect(e,t={}){e&&!b4a.isBuffer(e)&&(t=e,e=null);const{protocol:n,id:o,handshake:s,handshakeEncoding:i,onhandshake:h,encoding:r,unique:a,streamOpts:c}=this;e=e??o;const l={protocol:t.protocol||n,handshake:s,handshakeEncoding:i,onhandshake:h,encoding:r,unique:a,...c,...t,id:e};return new ProtoplexStream(this,l)}_onpair(e,t){const{id:n,handshake:o,handshakeEncoding:s,onhandshake:i,encoding:h,unique:r,streamOpts:a}=this,c=toKey(t=t??n,e),l={protocol:e,handshake:o,handshakeEncoding:s,onhandshake:i,encoding:h,unique:r,...a,...this._listeners.get(c)??{},id:t},u=new ProtoplexStream(this,l);this._streams.add(u),u.once("close",(()=>this._streams.delete(u))),this.emit("connection",u)}[Symbol.iterator](){return this._streams[Symbol.iterator]()}}
//# sourceMappingURL=/sm/896698b06c03628bc46212b99c99d22e3b197202d156e7a54ca808bc16153051.map