"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.MessageStream=void 0,exports.collectMessages=collectMessages,exports.mapStream=mapStream,exports.filterStream=filterStream,exports.takeStream=takeStream,exports.mergeStreams=mergeStreams,exports.installAsyncIterators=installAsyncIterators;class MessageStream{agent;options;state;constructor(e,t={}){this.agent=e,this.options={from:t.from,scope:t.scope,bufferSize:t.bufferSize??100,signal:t.signal,allowCleartext:t.allowCleartext??!1},this.state={buffer:[],pending:[],done:!1},this.options.signal&&this.options.signal.addEventListener("abort",()=>{this.close(new Error("Stream aborted"))});const s=e=>{this.handleEnvelope(e)},r=this.agent.getTransports()[0];r&&(r.onReceive(s),this.state.cleanup=()=>{})}async handleEnvelope(e){if(this.state.done)return;const t=await this.agent.receive(e,{allowCleartext:this.options.allowCleartext});if(!t.ok)return;const s=t.value;if(!(this.options.from&&s.sender!==this.options.from||this.options.scope&&s.scope!==this.options.scope))if(this.state.pending.length>0){this.state.pending.shift()({value:s,done:!1})}else this.state.buffer.length<this.options.bufferSize&&this.state.buffer.push(s)}close(e){if(!this.state.done){this.state.done=!0,this.state.error=e;for(const e of this.state.pending)e({value:void 0,done:!0});this.state.pending=[],this.state.cleanup&&this.state.cleanup()}}async next(){if(this.state.done)return{value:void 0,done:!0};if(this.options.signal?.aborted)return this.close(new Error("Stream aborted")),{value:void 0,done:!0};if(this.state.buffer.length>0){return{value:this.state.buffer.shift(),done:!1}}return new Promise(e=>{this.state.pending.push(e)})}async return(e){return this.close(),{value:void 0,done:!0}}async throw(e){return this.close(e instanceof Error?e:new Error(String(e))),{value:void 0,done:!0}}[Symbol.asyncIterator](){return this}get bufferedCount(){return this.state.buffer.length}get closed(){return this.state.done}get error(){return this.state.error}}async function collectMessages(e,t={}){const s=[],r=new MessageStream(e,t);let a;t.timeout&&(a=setTimeout(()=>{r.return()},t.timeout));try{for await(const e of r)if(s.push(e),t.limit&&s.length>=t.limit)break}finally{a&&clearTimeout(a),r.closed||await r.return()}return s}async function*mapStream(e,t){for await(const s of e)yield await t(s)}async function*filterStream(e,t){for await(const s of e)await t(s)&&(yield s)}async function*takeStream(e,t){let s=0;for await(const r of e){if(s>=t)break;yield r,s++}}async function*mergeStreams(e){const t=e.map(e=>e[Symbol.asyncIterator]()),s=new Map;for(let e=0;e<t.length;e++)s.set(e,t[e].next());for(;s.size>0;){const e=Array.from(s.entries()),r=await Promise.race(e.map(async([e,t])=>({idx:e,result:await t})));s.delete(r.idx),r.result.done||(yield r.result.value,s.set(r.idx,t[r.idx].next()))}}function installAsyncIterators(e){"subscribe"in e.prototype||(e.prototype.subscribe=function(e){return new MessageStream(this,e)})}exports.MessageStream=MessageStream;