grpc-libp2p-client 0.0.38 → 0.0.39
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/dist/dc-http2/stream.cjs.js +14 -4
- package/dist/dc-http2/stream.cjs.js.map +1 -1
- package/dist/dc-http2/stream.esm.js +14 -4
- package/dist/dc-http2/stream.esm.js.map +1 -1
- package/dist/grpc.js +14 -4
- package/dist/grpc.js.map +1 -1
- package/dist/grpc.min.js +1 -1
- package/dist/grpc.min.js.map +1 -1
- package/dist/index.cjs.js +14 -4
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +14 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/stats.html +1 -1
- package/package.json +1 -1
- package/src/dc-http2/stream.ts +15 -5
package/dist/grpc.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).GrpcLibp2pClient={})}(this,function(e){"use strict";"undefined"!=typeof window&&window;var t=function(){return t=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},t.apply(this,arguments)};function r(e,t,r,n){return new(r||(r=Promise))(function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function s(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(o,s)}c((n=n.apply(e,t||[])).next())})}function n(e,t){var r,n,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=s(0),o.throw=s(1),o.return=s(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(s){return function(c){return function(s){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,s[0]&&(a=0)),a;)try{if(r=1,n&&(i=2&s[0]?n.return:s[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,s[1])).done)return i;switch(n=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,n=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){a.label=s[1];break}if(6===s[0]&&a.label<i[1]){a.label=i[1],i=s;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(s);break}i[2]&&a.ops.pop(),a.trys.pop();continue}s=t.call(e,a)}catch(e){s=[6,e],n=0}finally{r=i=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}function i(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function a(e){return this instanceof a?(this.v=e,this):new a(e)}function o(e,t,r){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n,i=r.apply(e,t||[]),o=[];return n=Object.create(("function"==typeof AsyncIterator?AsyncIterator:Object).prototype),s("next"),s("throw"),s("return",function(e){return function(t){return Promise.resolve(t).then(e,l)}}),n[Symbol.asyncIterator]=function(){return this},n;function s(e,t){i[e]&&(n[e]=function(t){return new Promise(function(r,n){o.push([e,t,r,n])>1||c(e,t)})},t&&(n[e]=t(n[e])))}function c(e,t){try{(r=i[e](t)).value instanceof a?Promise.resolve(r.value.v).then(u,l):h(o[0][2],r)}catch(e){h(o[0][3],e)}var r}function u(e){c("next",e)}function l(e){c("throw",e)}function h(e,t){e(t),o.shift(),o.length&&c(o[0][0],o[0][1])}}function s(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,r=e[Symbol.asyncIterator];return r?r.call(e):(e=i(e),t={},n("next"),n("throw"),n("return"),t[Symbol.asyncIterator]=function(){return this},t);function n(r){t[r]=e[r]&&function(t){return new Promise(function(n,i){(function(e,t,r,n){Promise.resolve(n).then(function(t){e({value:t,done:r})},t)})(n,i,(t=e[r](t)).done,t.value)})}}}"function"==typeof SuppressedError&&SuppressedError;var c,u=0,l=1,h=3,d=4,f=6,p=7,m=8,g=1,v=1,w=function(){function e(e){void 0===e&&(e=4096),this.dynamicTable=[],this.dynamicTableSize=0,this.maxDynamicTableSize=e,this.staticTable=[["",""],[":authority",""],[":method","GET"],[":method","POST"],[":path","/"],[":path","/index.html"],[":scheme","http"],[":scheme","https"],[":status","200"],[":status","204"],[":status","206"],[":status","304"],[":status","400"],[":status","404"],[":status","500"],["accept-charset",""],["accept-encoding","gzip, deflate"],["accept-language",""],["accept-ranges",""],["accept",""],["access-control-allow-origin",""],["age",""],["allow",""],["authorization",""],["cache-control",""],["content-disposition",""],["content-encoding",""],["content-language",""],["content-length",""],["content-location",""],["content-range",""],["content-type",""],["cookie",""],["date",""],["etag",""],["expect",""],["expires",""],["from",""],["host",""],["if-match",""],["if-modified-since",""],["if-none-match",""],["if-range",""],["if-unmodified-since",""],["last-modified",""],["link",""],["location",""],["max-forwards",""],["proxy-authenticate",""],["proxy-authorization",""],["range",""],["referer",""],["refresh",""],["retry-after",""],["server",""],["set-cookie",""],["strict-transport-security",""],["transfer-encoding",""],["user-agent",""],["vary",""],["via",""],["www-authenticate",""]],this.huffmanTable=this.buildHuffmanTable()}return e.prototype.encode=function(e){for(var t=[],r=0,n=Object.entries(e);r<n.length;r++){var i=n[r],a=i[0],o=i[1],s=this.findInStaticTable(a,o);if(-1===s){var c=this.findInDynamicTable(a,o);-1===c?this.encodeLiteral(t,a,o):t.push.apply(t,this.encodeInteger(c+this.staticTable.length,7,128))}else t.push.apply(t,this.encodeInteger(s,7,128))}return new Uint8Array(t)},e.prototype.encodeInteger=function(e,t,r){void 0===r&&(r=0);var n=[],i=(1<<t)-1;if(e<i)return n.push(r|e),n;for(n.push(r|i),e-=i;e>=128;)n.push(127&e|128),e>>=7;return n.push(e),n},e.prototype.encodeString=function(e){var t=[],r=(new TextEncoder).encode(e),n=this.huffmanEncode(r);return n.length<r.length?(t.push.apply(t,this.encodeInteger(n.length,7,128)),Array.from(n).forEach(function(e){return t.push(e)})):(t.push.apply(t,this.encodeInteger(r.length,7,0)),Array.from(r).forEach(function(e){return t.push(e)})),t},e.prototype.findInStaticTable=function(e,t){for(var r=1;r<this.staticTable.length;r++)if(this.staticTable[r][0]===e&&this.staticTable[r][1]===t)return r;return-1},e.prototype.findInDynamicTable=function(e,t){for(var r=0;r<this.dynamicTable.length;r++)if(this.dynamicTable[r][0]===e&&this.dynamicTable[r][1]===t)return r;return-1},e.prototype.encodeLiteral=function(e,t,r){var n=this.findInStaticTable(t,"");-1!==n?e.push.apply(e,this.encodeInteger(n,6,64)):(e.push(64),e.push.apply(e,this.encodeString(t))),e.push.apply(e,this.encodeString(r)),this.addToDynamicTable(t,r)},e.prototype.addToDynamicTable=function(e,t){for(var r=e.length+t.length+32;this.dynamicTableSize+r>this.maxDynamicTableSize&&this.dynamicTable.length>0;){var n=this.dynamicTable.pop();n&&(this.dynamicTableSize-=n[0].length+n[1].length+32)}r<=this.maxDynamicTableSize&&(this.dynamicTable.unshift([e,t]),this.dynamicTableSize+=r),this.dynamicTable.push([e,t])},e.prototype.getIndexedHeader=function(e){return e<=this.staticTable.length-1?this.staticTable[e]:this.dynamicTable[e-this.staticTable.length]},e.prototype.buildHuffmanTable=function(){return{codes:new Uint32Array([8184,8388568,268435426,268435427,268435428,268435429,268435430,268435431,268435432,16777194,1073741820,268435433,268435434,1073741821,268435435,268435436,268435437,268435438,268435439,268435440,268435441,268435442,1073741822,268435443,268435444,268435445,268435446,268435447,268435448,268435449,268435450,268435451,20,1016,1017,4090,8185,21,248,2042,1018,1019,249,2043,250,22,23,24,0,1,2,25,26,27,28,29,30,31,92,251,32764,32,4091,1020,8186,33,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,252,115,253,8187,524272,8188,16380,34,32765,3,35,4,36,5,37,38,39,6,116,117,40,41,42,7,43,118,44,8,9,45,119,120,121,122,123,32766,2044,16381,8189,268435452,1048550,4194258,1048551,1048552,4194259,4194260,4194261,8388569,4194262,8388570,8388571,8388572,8388573,8388574,16777195,8388575,16777196,16777197,4194263,8388576,16777198,8388577,8388578,8388579,8388580,2097116,4194264,8388581,4194265,8388582,8388583,16777199,4194266,2097117,1048553,4194267,4194268,8388584,8388585,2097118,8388586,4194269,4194270,16777200,2097119,4194271,8388587,8388588,2097120,2097121,4194272,2097122,8388589,4194273,8388590,8388591,1048554,4194274,4194275,4194276,8388592,4194277,4194278,8388593,67108832,67108833,1048555,524273,4194279,8388594,4194280,33554412,67108834,67108835,67108836,134217694,134217695,67108837,16777201,33554413,524274,2097123,67108838,134217696,134217697,67108839,134217698,16777202,2097124,2097125,67108840,67108841,268435453,134217699,134217700,134217701,1048556,16777203,1048557,2097126,4194281,2097127,2097128,8388595,4194282,4194283,33554414,33554415,16777204,16777205,67108842,8388596,67108843,134217702,67108844,67108845,134217703,134217704,134217705,134217706,134217707,268435454,134217708,134217709,134217710,134217711,134217712,67108846]),lengths:new Uint8Array([13,23,28,28,28,28,28,28,28,24,30,28,28,30,28,28,28,28,28,28,28,28,30,28,28,28,28,28,28,28,28,28,6,10,10,12,13,6,8,11,10,10,8,11,8,6,6,6,5,5,5,6,6,6,6,6,6,6,7,8,15,6,12,10,13,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,7,8,13,19,13,14,6,15,5,6,5,6,5,6,6,6,5,7,7,6,6,6,5,6,7,6,5,5,6,7,7,7,7,7,15,11,14,13,28,20,22,20,20,22,22,22,23,22,23,23,23,23,23,24,23,24,24,22,23,24,23,23,23,23,21,22,23,22,23,23,24,22,21,20,22,22,23,23,21,23,22,22,24,21,22,23,23,21,21,22,21,23,22,23,23,20,22,22,22,23,22,22,23,26,26,20,19,22,23,22,25,26,26,26,27,27,26,24,25,19,21,26,27,27,26,27,24,21,21,26,26,28,27,27,27,20,24,20,21,22,21,21,23,22,22,25,25,24,24,26,23,26,27,26,26,27,27,27,27,27,28,27,27,27,27,27,26])}},e.prototype.decode=function(e){for(var t="",r=0,n=0,i=0;i<e.length;i++){for(r=r<<8|e[i],n+=8;n>=5;){var a=this.findSymbol(r,n);if(!a)break;var o=a[0],s=a[1];t+=String.fromCharCode(o),r&=(1<<n-s)-1,n-=s}}if(n>0){var c=(1<<n)-1;if((r&c)!==c)throw new Error("Invalid Huffman padding")}return t},e.prototype.findSymbol=function(e,t){for(var r=0;r<this.huffmanTable.codes.length;r++){var n=this.huffmanTable.lengths[r];if(!(t<n))if((e>>t-n&(1<<n)-1)===this.huffmanTable.codes[r])return[r,n]}return null},e.prototype.huffmanEncode=function(e){for(var t=[],r=0,n=0,i=0;i<e.length;i++){var a=e[i],o=this.huffmanTable.codes[a],s=this.huffmanTable.lengths[a];for(n+=s,r=r<<s|o;n>=8;)n-=8,t.push(r>>n&255)}return n>0&&(r=r<<8-n|(1<<8-n)-1,t.push(255&r)),new Uint8Array(t)},e.prototype.decodeHeaderFields=function(e){for(var t=new Map,r=0;r<e.length;){var n=e[r];if(128&n){var i=this.decodeIndexedHeader(e,r),a=i[0],o=i[1],s=i[2];a&&o&&t.set(a,o),r=s}else if(64&n){var c=this.decodeLiteralHeaderWithIndexing(e,r),u=c[0];o=c[1],s=c[2];u&&o&&t.set(u,o),r=s}else if(32&n)r++;else if(16&n){var l=this.decodeLiteralHeaderWithoutIndexing(e,r),h=l[0];o=l[1],s=l[2];h&&o&&t.set(h,o),r=s}else{var d=this.decodeLiteralHeaderWithoutIndexing(e,r),f=d[0];o=d[1],s=d[2];f&&o&&t.set(f,o),r=s}}return t},e.prototype.decodeInteger=function(e,t,r){var n=(1<<r)-1,i=t,a=e[t]&n;if(a<n)return[a,i+1];i++;for(var o=0;i<e.length;){var s=e[i++];if(a+=(127&s)<<o,o+=7,!(128&s))break}return[a,i]},e.prototype.decodeLiteralString=function(e,t){if(t>=e.length)return["",t];var r=!!(128&e[t]),n=this.decodeInteger(e,t,7),i=n[0],a=n[1];if(a+i>e.length)return["",a];var o,s=e.slice(a,a+i);if(r)try{o=this.decode(s)}catch(e){o=""}else try{o=(new TextDecoder).decode(s)}catch(e){o=""}return[o,a+i]},e.prototype.decodeIndexedHeader=function(e,t){var r=this.decodeInteger(e,t,7),n=r[0],i=r[1];if(n<=0)return["","",i];var a=this.staticTable[n];return a?[a[0],a[1],i]:["","",i]},e.prototype.decodeLiteralHeaderWithIndexing=function(e,t){var r,n=this.decodeInteger(e,t,6),i=n[0];if(t=n[1],i>0){var a=this.staticTable[i];r=a?a[0]:""}else{var o=this.decodeLiteralString(e,t);r=o[0],t=o[1]}var s=this.decodeLiteralString(e,t);return[r,s[0],s[1]]},e.prototype.decodeLiteralHeaderWithoutIndexing=function(e,t){return this.decodeLiteralHeaderWithIndexing(e,t)},e.prototype.huffmanDecodeToString=function(e){return this.decode(e)},e}(),b=function(){function e(){}return e.encodeSettingsFrame=function(e){var t=e.payload,r=6*t.length,n=new Uint8Array(9+r);return y(n,{length:r,type:e.type,flags:e.flags,streamId:e.streamId}),function(e,t){var r=9;t.forEach(function(t){!function(e,t,r){e[r]=t>>8&255,e[r+1]=255&t}(e,t.identifier,r),r+=2,S(e,t.value,r),r+=4})}(n,t),n},e.encodeSettingsAckFrame=function(){var e=new Uint8Array(9);return y(e,{length:0,type:4,flags:1,streamId:0}),e},e}();function y(e,t){var r,n=null!==(r=t.length)&&void 0!==r?r:0;e[0]=n>>16&255,e[1]=n>>8&255,e[2]=255&n,e[3]=t.type,e[4]=t.flags,S(e,2147483647&t.streamId,5)}function S(e,t,r){e[r]=t>>24&255,e[r+1]=t>>16&255,e[r+2]=t>>8&255,e[r+3]=255&t}var E=1,T=2,A=3,I=4,P=5,k=6,W=((c={})[E]=4096,c[T]=1,c[A]=100,c[I]=16384,c[P]=16384,c[k]=8192,c),F=function(){function e(){}return e.createPreface=function(){return(new TextEncoder).encode("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")},e.createPongFrame=function(t){return e.createFrame(6,1,0,t)},e.createSettingsFrame=function(t){void 0===t&&(t={});var r=e.createOriginSettingsFrame(t);return b.encodeSettingsFrame(r)},e.createSettingsAckFrame=function(){return b.encodeSettingsAckFrame()},e.createWindowUpdateFrame=function(t,r){if(r<=0||r>2147483647)throw new Error("Window size increment must be between 1 and 2^31-1");var n=new Uint8Array(4);return n[0]=r>>24&127,n[1]=r>>16&255,n[2]=r>>8&255,n[3]=255&r,e.createFrame(8,0,t,n)},e.createDataFrames=function(t,r,n,i){void 0===n&&(n=!1),void 0===i&&(i=16384);var a=[],o=r.length,s=new Uint8Array(5+o);s[0]=0,s[1]=o>>24&255,s[2]=o>>16&255,s[3]=o>>8&255,s[4]=255&o,s.set(r,5);for(var c=i-9,u=0;u<s.length;u+=c){var l=s.length-u,h=Math.min(c,l),d=s.slice(u,u+h),f=u+h>=s.length&&n,p=e.createFrame(0,f?1:0,t,d);a.push(p)}return a},e.createDataFrame=function(t,r,n){void 0===n&&(n=!0);var i=r.length,a=new Uint8Array(5+i);a[0]=0,a[1]=i>>24&255,a[2]=i>>16&255,a[3]=i>>8&255,a[4]=255&i,a.set(r,5);var o=n?1:0;return e.createFrame(0,o,t,a)},e.createHeadersFrame=function(t,r,n,i){void 0===n&&(n=!0);var a={":path":r,":method":"POST",":scheme":"http",":authority":"localhost","content-type":"application/grpc+proto","user-agent":"grpc-web-client/0.1",accept:"application/grpc+proto","grpc-timeout":"3600S"};i&&(a.authorization="Bearer ".concat(i));var o=(new w).encode(a),s=n?4:0;return e.createFrame(1,s,t,o)},e.createResponseHeadersFrame=function(t,r,n){void 0===n&&(n=!0);var i=(new w).encode(r),a=n?4:0;return e.createFrame(1,a,t,i)},e.createTrailersFrame=function(t,r){var n=(new w).encode(r);return e.createFrame(1,5,t,n)},e.createOriginSettingsFrame=function(e){void 0===e&&(e={});var r=t(t({},W),e);return function(e){for(var t=0,r=Object.entries(e);t<r.length;t++){var n=r[t],i=n[0],a=n[1];switch(Number(i)){case E:if(a<0)throw new Error("HEADER_TABLE_SIZE must be non-negative");break;case T:if(0!==a&&1!==a)throw new Error("ENABLE_PUSH must be 0 or 1");break;case I:if(a<0||a>2147483647)throw new Error("INITIAL_WINDOW_SIZE must be between 0 and 2^31-1");break;case P:if(a<16384||a>16777215)throw new Error("MAX_FRAME_SIZE must be between 16,384 and 16,777,215");break;case A:case k:if(a<0)throw new Error("Parameter ".concat(i," must be non-negative"));break;default:console.warn("Unknown settings parameter: ".concat(i))}}}(r),{type:4,flags:0,streamId:0,payload:C(r)}},e.createOriginSettingsAckFrame=function(){return{type:4,flags:1,streamId:0,payload:[]}},e.createFrame=function(e,t,r,n){var i=n?n.length:0,a=new Uint8Array(9+i);return a[0]=i>>16&255,a[1]=i>>8&255,a[2]=255&i,a[3]=e,a[4]=t,a[5]=r>>24&255,a[6]=r>>16&255,a[7]=r>>8&255,a[8]=255&r,n&&i>0&&a.set(n,9),a},e}();function C(e){for(var t=[],r=0,n=Object.entries(e);r<n.length;r++){var i=n[r],a=i[0],o=i[1];t.push({identifier:Number(a),value:o})}return t}var x=(new TextEncoder).encode("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"),z=function(){function e(e,t){var r;this.buffer=new Uint8Array(0),this.settingsAckReceived=!1,this.peerSettingsReceived=!1,this.connectionWindowSize=4<<20,this.streams=new Map,this.defaultStreamWindowSize=4<<20,this.sendConnWindow=65535,this.sendStreamWindows=new Map,this.peerInitialStreamWindow=65535,this.sendWindowWaiters=[],this.endFlag=!1,this.writer=e,this.compatibilityMode=null!==(r=null==t?void 0:t.compatibilityMode)&&void 0!==r&&r}return e.prototype.processStream=function(e){return r(this,void 0,void 0,function(){var t,r,i,a,o,c,u,l,h,d,f;return n(this,function(n){switch(n.label){case 0:n.trys.push([0,13,,14]),n.label=1;case 1:n.trys.push([1,6,7,12]),a=!0,o=s(e),n.label=2;case 2:return[4,o.next()];case 3:if(c=n.sent(),u=c.done)return[3,5];d=c.value,a=!1,t=d,this._processChunk(t),n.label=4;case 4:return a=!0,[3,2];case 5:return[3,12];case 6:return r=n.sent(),l={error:r},[3,12];case 7:return n.trys.push([7,,10,11]),a||u||!(h=o.return)?[3,9]:[4,h.call(o)];case 8:n.sent(),n.label=9;case 9:return[3,11];case 10:if(l)throw l.error;return[7];case 11:return[7];case 12:if(!this.compatibilityMode&&!this.endFlag){this.endFlag=!0;try{null===(f=this.onEnd)||void 0===f||f.call(this)}catch(e){console.error("Error during onEnd callback:",e)}}return[3,14];case 13:throw i=n.sent(),console.error("Error processing stream:",i),i;case 14:return[2]}})})},e.prototype._processChunk=function(e){var t="subarray"in e&&"function"==typeof e.subarray?e.subarray():e,r=new Uint8Array(this.buffer.length+t.length);r.set(this.buffer),r.set(t,this.buffer.length),this.buffer=r;for(var n=0;this.buffer.length-n>=9;)if(this.buffer.length-n>=24&&this.isHttp2Preface(this.buffer.subarray(n))){n+=24;var i=F.createSettingsFrame();this.writer.write(i)}else{var a=this._parseFrameHeader(this.buffer.subarray(n)),o=9+a.length;if(this.buffer.length-n<o)break;var s=this.buffer.subarray(n,n+o);this._handleFrame(a,s).catch(function(e){console.error("Error handling frame:",e)}),n+=o}n>0&&(this.buffer=this.buffer.slice(n))},e.prototype.isHttp2Preface=function(e){if(e.length<x.length)return!1;for(var t=0;t<x.length;t++)if(e[t]!==x[t])return!1;return!0},e.prototype._oldProcessStream_removed=function(){},e.prototype.waitForSettingsAck=function(){var e=this;return new Promise(function(t,r){if(e.settingsAckReceived)t();else var n=setInterval(function(){e.settingsAckReceived&&(clearInterval(n),clearTimeout(i),t())},100),i=setTimeout(function(){clearInterval(n),r(new Error("Settings ACK timeout"))},3e4)})},e.prototype.waitForPeerSettings=function(e){var t=this;return void 0===e&&(e=3e4),new Promise(function(r,n){if(t.peerSettingsReceived)r();else var i=setInterval(function(){t.peerSettingsReceived&&(clearInterval(i),clearTimeout(a),r())},100),a=setTimeout(function(){clearInterval(i),n(new Error("Peer SETTINGS timeout"))},e)})},e.prototype.registerOutboundStream=function(e){this.sendStreamWindows.has(e)||this.sendStreamWindows.set(e,this.peerInitialStreamWindow)},e.prototype.getSendWindows=function(e){var t,r=null!==(t=this.sendStreamWindows.get(e))&&void 0!==t?t:0;return{conn:this.sendConnWindow,stream:r}},e.prototype.consumeSendWindow=function(e,t){var r;this.sendConnWindow=Math.max(0,this.sendConnWindow-t);var n=null!==(r=this.sendStreamWindows.get(e))&&void 0!==r?r:0;this.sendStreamWindows.set(e,Math.max(0,n-t))},e.prototype.unsafeForceExtendSendWindow=function(e,t){var r;if(!(this.compatibilityMode||t<=0)){this.sendConnWindow=Math.min(2147483647,this.sendConnWindow+t);var n=null!==(r=this.sendStreamWindows.get(e))&&void 0!==r?r:0;this.sendStreamWindows.set(e,Math.min(2147483647,n+t))}},e.prototype.waitForSendWindow=function(e){return r(this,arguments,void 0,function(e,t,r){var i,a=this;return void 0===t&&(t=1),void 0===r&&(r=3e4),n(this,function(n){return i=Date.now(),[2,new Promise(function(n,o){var s=null,c=!1,u=function(){var u=a.getSendWindows(e),l=u.conn,h=u.stream;return l>=t&&h>=t?(c||(c=!0,s&&(clearInterval(s),s=null),n()),!0):Date.now()-i>r&&(c||(c=!0,s&&(clearInterval(s),s=null),o(new Error("Send window wait timeout"))),!0)};if(!u()){a.sendWindowWaiters.push(function(){u()}),s=setInterval(function(){u()&&s&&(clearInterval(s),s=null)},50)}})]})})},e.prototype._handleFrame=function(e,t){return r(this,void 0,void 0,function(){var r,i,a,o,s,c,w,b,y,S,E,T,A,I,P,k,W,C,x,z,O,D,L=this;return n(this,function(n){switch(e.type){case d:if((e.flags&g)===g)this.settingsAckReceived=!0;else{for(r=t.slice(9),i={},a=0,o=void 0,s=0;s<r.length;s+=6)c=r[s]<<8|r[s+1],w=r[s+2]<<24|r[s+3]<<16|r[s+4]<<8|r[s+5],i[c]=w,4===c?(this.defaultStreamWindowSize=w,a=w-this.peerInitialStreamWindow,this.peerInitialStreamWindow=w):3===c&&(o=w);this.compatibilityMode||0===a||Array.from(this.sendStreamWindows.entries()).forEach(function(e){var t=e[0],r=e[1],n=Math.max(0,r+a);L.sendStreamWindows.set(t,n)});try{!this.onSettingsParsed||void 0===o&&0===a||(b={},void 0!==o&&(b.maxConcurrentStreams=o),0!==a&&(b.initialWindowSize=this.peerInitialStreamWindow),this.onSettingsParsed(b))}catch(e){console.error("Error handling parsed SETTINGS callback:",e)}this.onSettings&&this.onSettings(e),this.peerSettingsReceived=!0,this.sendWindowWaiters.splice(0).forEach(function(e){try{e()}catch(e){console.debug("waiter error",e)}})}break;case u:this.onData&&this.onData(t.slice(9),e);try{0!==e.streamId&&(y=F.createWindowUpdateFrame(e.streamId,null!==(C=e.length)&&void 0!==C?C:0),this.writer.write(y)),S=F.createWindowUpdateFrame(0,null!==(x=e.length)&&void 0!==x?x:0),this.writer.write(S)}catch(e){console.error("[HTTP2] Error sending window update:",e)}if((e.flags&v)===v)return this.endFlag=!0,this.onEnd&&this.onEnd(),[2];break;case l:if(this.onHeaders&&this.onHeaders(t.slice(9),e),(e.flags&v)===v)return this.endFlag=!0,this.onEnd&&this.onEnd(),[2];break;case m:this.handleWindowUpdateFrame(e,t);try{E=this.parseWindowUpdateFrame(t,e).windowSizeIncrement,0===e.streamId?this.sendConnWindow+=E:(T=null!==(z=this.sendStreamWindows.get(e.streamId))&&void 0!==z?z:this.peerInitialStreamWindow,this.sendStreamWindows.set(e.streamId,T+E)),this.sendWindowWaiters.splice(0).forEach(function(e){try{e()}catch(e){console.debug("waiter error",e)}})}catch(e){}break;case f:this._handlePingFrame(e,t);break;case p:A=void 0;try{(I=t.subarray(9)).length>=8?(P=new DataView(I.buffer,I.byteOffset,I.byteLength),k=2147483647&P.getUint32(0,!1),W=P.getUint32(4,!1),A={lastStreamId:k,errorCode:W},console.warn("[HTTP2] GOAWAY received",A)):(console.warn("[HTTP2] GOAWAY received"),A={})}catch(e){}try{null===(O=this.onGoaway)||void 0===O||O.call(this,null!=A?A:{})}catch(e){console.error("Error during GOAWAY callback:",e)}this.endFlag=!0;try{null===(D=this.onEnd)||void 0===D||D.call(this)}catch(e){console.error("Error during GOAWAY onEnd callback:",e)}break;case h:this.endFlag=!0,this.onEnd&&this.onEnd();break;default:console.debug("Unknown frame type:",e.type)}return[2]})})},e.prototype._parseFrameHeader=function(e){return{length:e[0]<<16|e[1]<<8|e[2],type:e[3],flags:e[4],streamId:e[5]<<24|e[6]<<16|e[7]<<8|e[8],payload:e.slice(0,9)}},e.prototype._handlePingFrame=function(e,t){if(8!==e.length)throw new Error("PING frame must have a length of 8 bytes");if(!(e.flags&g)){var r=F.createPongFrame(t.slice(9));try{this.writer.write(r)}catch(e){throw console.error("Error sending PONG frame:",e),e}}},e.prototype.waitForEndOfStream=function(e){var t=this;return new Promise(function(r,n){if(t.endFlag)r();else{var i=null;e>0&&(i=setTimeout(function(){clearInterval(a),n(new Error("End of stream timeout"))},e));var a=setInterval(function(){t.endFlag&&(null!==i&&clearTimeout(i),clearInterval(a),r())},100),o=t.onEnd;t.onEnd=function(){t.endFlag||(t.endFlag=!0),null!==i&&clearTimeout(i),clearInterval(a),r(),o&&o()}}})},e.prototype.parseWindowUpdateFrame=function(e,t){if(4!==t.length)throw new Error("WINDOW_UPDATE frame must have a length of 4 bytes");var r=new Uint8Array(e.slice(9)),n=(127&r[0])<<24|r[1]<<16|r[2]<<8|r[3];if(0===n)throw new Error("WINDOW_UPDATE increment must not be zero");return{windowSizeIncrement:n}},e.prototype.handleWindowUpdateFrame=function(e,t){try{var r=this.parseWindowUpdateFrame(t,e);return this.connectionWindowSize+=r.windowSizeIncrement,r}catch(e){throw console.error("Error handling WINDOW_UPDATE frame:",e),e}},e}();function O(){const e={};return e.promise=new Promise((t,r)=>{e.resolve=t,e.reject=r}),e}class D{buffer;mask;top;btm;next;constructor(e){if(!(e>0)||e-1&e)throw new Error("Max size for a FixedFIFO should be a power of two");this.buffer=new Array(e),this.mask=e-1,this.top=0,this.btm=0,this.next=null}push(e){return void 0===this.buffer[this.top]&&(this.buffer[this.top]=e,this.top=this.top+1&this.mask,!0)}shift(){const e=this.buffer[this.btm];if(void 0!==e)return this.buffer[this.btm]=void 0,this.btm=this.btm+1&this.mask,e}isEmpty(){return void 0===this.buffer[this.btm]}}class L{size;hwm;head;tail;constructor(e={}){this.hwm=e.splitLimit??16,this.head=new D(this.hwm),this.tail=this.head,this.size=0}calculateSize(e){return null!=e?.byteLength?e.byteLength:1}push(e){if(null!=e?.value&&(this.size+=this.calculateSize(e.value)),!this.head.push(e)){const t=this.head;this.head=t.next=new D(2*this.head.buffer.length),this.head.push(e)}}shift(){let e=this.tail.shift();if(void 0===e&&null!=this.tail.next){const t=this.tail.next;this.tail.next=null,this.tail=t,e=this.tail.shift()}return null!=e?.value&&(this.size-=this.calculateSize(e.value)),e}isEmpty(){return this.head.isEmpty()}}class M extends Error{type;code;constructor(e,t){super(e??"The operation was aborted"),this.type="aborted",this.code=t??"ABORT_ERR"}}function U(e={}){return function(e,t){t=t??{};let r,n,i,a=t.onEnd,o=new L,s=O();const c=async()=>{try{return o.isEmpty()?i?{done:!0}:await new Promise((t,i)=>{n=a=>{n=null,o.push(a);try{t(e(o))}catch(e){i(e)}return r}}):e(o)}finally{o.isEmpty()&&queueMicrotask(()=>{s.resolve(),s=O()})}},u=e=>null!=n?n(e):(o.push(e),r),l=e=>(o=new L,null!=n?n({error:e}):(o.push({error:e}),r)),h=e=>{if(i)return r;if(!0!==t?.objectMode&&null==e?.byteLength)throw new Error("objectMode was not true but tried to push non-Uint8Array value");return u({done:!1,value:e})},d=e=>i?r:(i=!0,null!=e?l(e):u({done:!0})),f=()=>(o=new L,d(),{done:!0}),p=e=>(d(e),{done:!0});if(r={[Symbol.asyncIterator](){return this},next:c,return:f,throw:p,push:h,end:d,get readableLength(){return o.size},onEmpty:async e=>{const t=e?.signal;if(t?.throwIfAborted(),o.isEmpty())return;let r,n;null!=t&&(r=new Promise((e,r)=>{n=()=>{r(new M)},t.addEventListener("abort",n)}));try{await Promise.race([s.promise,r])}finally{null!=n&&null!=t&&t?.removeEventListener("abort",n)}}},null==a)return r;const m=r;return r={[Symbol.asyncIterator](){return this},next:()=>m.next(),throw:e=>(m.throw(e),null!=a&&(a(e),a=void 0),{done:!0}),return:()=>(m.return(),null!=a&&(a(),a=void 0),{done:!0}),push:h,end:e=>(m.end(e),null!=a&&(a(e),a=void 0),r),get readableLength(){return m.readableLength},onEmpty:e=>m.onEmpty(e)},r}(e=>{const t=e.shift();if(null==t)return{done:!0};if(null!=t.error)throw t.error;return{done:!0===t.done,value:t.value}},e)}var H=4091,j=function(){function e(e,t){var r,n,i;if(void 0===t&&(t={}),this.stream=e,this.options=t,this.bytesWritten=0,this.abortController=new AbortController,this.backpressureHistory=[],this.isBackpressure=!1,this.writeQueue=[],this.isProcessingQueue=!1,this.lastBackpressureCheck=0,this.bytesDrained=0,this.lastDrainEventAt=0,this.stallStartAt=0,this.lastBytesDrainedSeen=0,this.lastBpWarnAt=0,this.isHandlingError=!1,this.listeners=new Map,!e)throw new Error("StreamWriter requires a valid stream object");this.log={trace:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return console.debug.apply(console,function(e,t,r){if(r||2===arguments.length)for(var n,i=0,a=t.length;i<a;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))}(["[StreamWriter]"],e,!1))}},this.options=t?{chunkSize:null!==(r=t.chunkSize)&&void 0!==r?r:H,bufferSize:null!==(n=t.bufferSize)&&void 0!==n?n:5242880,retries:null!==(i=t.retries)&&void 0!==i?i:3}:{chunkSize:H,bufferSize:5242880,retries:3},this.options.chunkSize&&this.options.chunkSize>H&&(this.options.chunkSize=H);var a=U({objectMode:!1});a._originalPush=a.push.bind(a),a._originalNext=a.next.bind(a),a._queueSize=0,Object.defineProperty(a,"push",{value:function(e){return a._queueSize+=e.byteLength,a._originalPush(e)},writable:!1,configurable:!1}),this.p=a,this.startPipeline(),this.startWatchdog()}return Object.defineProperty(e.prototype,"queueSize",{get:function(){return this.p._queueSize},enumerable:!1,configurable:!0}),e.prototype.getAverageQueueSize=function(){var e=Date.now(),t=this.queueSize;return e-this.lastBackpressureCheck>100&&(t>0&&(this.backpressureHistory.push(t),this.backpressureHistory.length>5&&this.backpressureHistory.shift()),this.lastBackpressureCheck=e),0===this.backpressureHistory.length?t:this.backpressureHistory.reduce(function(e,t){return e+t},0)/this.backpressureHistory.length},e.prototype.handleError=function(e){var t,r;if(!this.abortController.signal.aborted&&!this.isHandlingError){this.isHandlingError=!0,this.abort(e.message);try{this.dispatchEvent(new CustomEvent("error",{detail:e}))}catch(e){null===(r=null===(t=this.log)||void 0===t?void 0:t.trace)||void 0===r||r.call(t,"Error in error event handler:",e)}}},e.prototype.startPipeline=function(){var e=this;this.pipeToStream().catch(function(t){return e.handleError(t)})},e.prototype.pipeToStream=function(){return r(this,void 0,void 0,function(){var e,t,r,i,a,o,c,u,l,h,d,f,p,m,g;return n(this,function(n){switch(n.label){case 0:if(!this.stream)return null===(p=null===(f=this.log)||void 0===f?void 0:f.trace)||void 0===p||p.call(f,"Stream is null/undefined, cannot start pipeline"),[2];n.label=1;case 1:n.trys.push([1,10,11,16]),e=!0,t=s(this.createTransform()(this.p)),n.label=2;case 2:return[4,t.next()];case 3:if(r=n.sent(),u=r.done)return[3,9];if(d=r.value,e=!1,i=d,this.abortController.signal.aborted)return[3,9];n.label=4;case 4:return n.trys.push([4,7,,8]),this.stream.send(i)?[3,6]:[4,this.stream.onDrain()];case 5:n.sent(),n.label=6;case 6:return[3,8];case 7:if(a=n.sent(),o=(a instanceof Error?a.message:"").toLowerCase(),a instanceof Error&&"StreamStateError"===a.name||o.includes("closing")||o.includes("closed")||o.includes("write to a stream that is closed"))return null===(g=null===(m=this.log)||void 0===m?void 0:m.trace)||void 0===g||g.call(m,"Stream is closing/closed, stopping pipeline"),[3,9];throw a;case 8:return e=!0,[3,2];case 9:return[3,16];case 10:return c=n.sent(),l={error:c},[3,16];case 11:return n.trys.push([11,,14,15]),e||u||!(h=t.return)?[3,13]:[4,h.call(t)];case 12:n.sent(),n.label=13;case 13:return[3,15];case 14:if(l)throw l.error;return[7];case 15:return[7];case 16:return[2]}})})},e.prototype.createTransform=function(){var e=this;return function(t){return o(this,arguments,function(){var r,i,o,c,u,l,h,d,f,p;return n(this,function(n){switch(n.label){case 0:n.trys.push([0,7,8,13]),r=!0,i=s(t),n.label=1;case 1:return[4,a(i.next())];case 2:return o=n.sent(),(h=o.done)?[3,6]:(p=o.value,r=!1,[4,a(c=p)]);case 3:return[4,n.sent()];case 4:n.sent();try{null!=e.p._queueSize&&(e.p._queueSize=Math.max(0,e.p._queueSize-c.byteLength)),e.bytesDrained+=c.byteLength,(u=Date.now())-e.lastDrainEventAt>250&&(e.lastDrainEventAt=u,e.dispatchEvent(new CustomEvent("drain",{detail:{drained:e.bytesDrained,queueSize:e.queueSize}}))),e.lastBytesDrainedSeen=e.bytesDrained}catch(e){console.error("Error updating bytesDrained in StreamWriter transform",e)}n.label=5;case 5:return r=!0,[3,1];case 6:return[3,13];case 7:return l=n.sent(),d={error:l},[3,13];case 8:return n.trys.push([8,,11,12]),r||h||!(f=i.return)?[3,10]:[4,a(f.call(i))];case 9:n.sent(),n.label=10;case 10:return[3,12];case 11:if(d)throw d.error;return[7];case 12:return[7];case 13:return[2]}})})}},e.prototype.startWatchdog=function(e,t){var r=this;void 0===e&&(e=500),void 0===t&&(t=1500),this.watchdogTimer||(this.watchdogTimer=setInterval(function(){if(!r.abortController.signal.aborted){var e=.7*r.options.bufferSize,n=r.queueSize,i=Date.now();n>=e?r.lastBytesDrainedSeen===r.bytesDrained?(r.stallStartAt||(r.stallStartAt=i),i-r.stallStartAt>=t&&(r.dispatchEvent(new CustomEvent("stalled",{detail:{queueSize:n,drained:r.bytesDrained,sinceMs:i-r.stallStartAt}})),r.stallStartAt=i)):(r.stallStartAt=0,r.lastBytesDrainedSeen=r.bytesDrained):r.stallStartAt=0}},e))},e.prototype.write=function(e){return r(this,void 0,void 0,function(){var t=this;return n(this,function(i){return this.abortController.signal.aborted?[2,Promise.resolve()]:[2,new Promise(function(i,a){t.writeQueue.push(function(){return r(t,void 0,void 0,function(){var t,r;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,3,,4]),this.abortController.signal.aborted?(i(),[2]):[4,this.convertToBuffer(e)];case 1:return t=n.sent(),[4,this.writeChunks(t)];case 2:return n.sent(),i(),[3,4];case 3:return r=n.sent(),a(r),[3,4];case 4:return[2]}})})}),t.processQueue()})]})})},e.prototype.convertToBuffer=function(e){return r(this,void 0,void 0,function(){return n(this,function(t){return e instanceof Blob?[2,e.arrayBuffer()]:"string"==typeof e?[2,(new TextEncoder).encode(e).buffer]:e instanceof Uint8Array?[2,e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)]:[2,e]})})},e.prototype.writeChunks=function(e){return r(this,void 0,void 0,function(){var t,r,i;return n(this,function(n){switch(n.label){case 0:t=0,n.label=1;case 1:return t<e.byteLength?(r=Math.min(t+this.options.chunkSize,e.byteLength),(i=new Uint8Array(r-t)).set(new Uint8Array(e.slice(t,r))),[4,this.retryableWrite(i)]):[3,4];case 2:n.sent(),this.updateProgress(i.byteLength),n.label=3;case 3:return t+=this.options.chunkSize,[3,1];case 4:return[2]}})})},e.prototype.retryableWrite=function(e){return r(this,arguments,void 0,function(e,t){var r,i,a,o,s=this;return void 0===t&&(t=0),n(this,function(n){switch(n.label){case 0:if(this.abortController.signal.aborted)throw new Error("Stream is aborted, cannot write");n.label=1;case 1:return n.trys.push([1,5,,8]),r=this.queueSize,i=.7*this.options.bufferSize,r>i?[4,this.monitorBackpressure()]:[3,3];case 2:n.sent(),n.label=3;case 3:if(this.abortController.signal.aborted)throw new Error("Stream aborted during backpressure monitoring");return[4,new Promise(function(t,r){try{s.p.push(e)}catch(e){r(e)}t()})];case 4:return n.sent(),[3,8];case 5:return a=n.sent(),t<this.options.retries?(o=this.calculateRetryDelay(t),[4,new Promise(function(e){return setTimeout(e,o)})]):[3,7];case 6:return n.sent(),[2,this.retryableWrite(e,t+1)];case 7:throw a;case 8:return[2]}})})},e.prototype.monitorBackpressure=function(){return r(this,void 0,void 0,function(){var e,t,r,i,a,o,s,c;return n(this,function(n){switch(n.label){case 0:if(e=this.queueSize,t=.7*this.options.bufferSize,r=.9*this.options.bufferSize,e<t)return this.isBackpressure&&(this.isBackpressure=!1,this.dispatchBackpressureEvent({currentSize:e,averageSize:this.getAverageQueueSize(),threshold:t,waitingTime:0})),[2];this.isBackpressure||(this.isBackpressure=!0,this.dispatchBackpressureEvent({currentSize:e,averageSize:this.getAverageQueueSize(),threshold:t,waitingTime:0})),i=e/this.options.bufferSize,a=e>=r?50+Math.min(200,100*i):Math.min(20,30*i),o=0,s=3,n.label=1;case 1:return this.queueSize>=t&&o<s?this.abortController.signal.aborted?[3,3]:[4,new Promise(function(e){return setTimeout(e,a)})]:[3,3];case 2:return n.sent(),o++,a=Math.min(1.5*a,100),[3,1];case 3:return this.queueSize>=t&&(c=Date.now())-this.lastBpWarnAt>1e3&&(this.lastBpWarnAt=c,console.warn("Stream writer: High backpressure detected (".concat(this.queueSize," bytes), continuing anyway"))),[2]}})})},e.prototype.calculateRetryDelay=function(e){return Math.min(10*Math.pow(2,e)+100*Math.random(),100)},e.prototype.processQueue=function(){return r(this,void 0,void 0,function(){var e,t;return n(this,function(r){switch(r.label){case 0:if(this.isProcessingQueue||this.abortController.signal.aborted)return[2];this.isProcessingQueue=!0,r.label=1;case 1:return this.writeQueue.length>0?this.abortController.signal.aborted?[3,5]:(e=this.queueSize,t=.5*this.options.bufferSize,e>t?[4,this.monitorBackpressure()]:[3,3]):[3,5];case 2:r.sent(),r.label=3;case 3:return[4,this.writeQueue.shift()()];case 4:return r.sent(),[3,1];case 5:return this.isProcessingQueue=!1,[2]}})})},e.prototype.updateProgress=function(e){this.bytesWritten+=e,this.dispatchEvent(new CustomEvent("progress",{detail:{loaded:this.bytesWritten}}))},e.prototype.end=function(){return r(this,void 0,void 0,function(){var e,t,r,i;return n(this,function(n){switch(n.label){case 0:if(this.p.end(),!this.stream||"function"!=typeof this.stream.close)return[3,4];n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this.stream.close()];case 2:return n.sent(),[3,4];case 3:return e=n.sent(),(t=(e instanceof Error?e.message:"").toLowerCase()).includes("closed")||t.includes("closing")||null===(i=null===(r=this.log)||void 0===r?void 0:r.trace)||void 0===i||i.call(r,"Stream close error:",e),[3,4];case 4:return this.cleanup(),[2]}})})},e.prototype.abort=function(e){var t,r,n,i;if(void 0===e&&(e="User aborted"),!this.abortController.signal.aborted){this.abortController.abort(e);try{if(this.stream&&"function"==typeof this.stream.abort){var a=this.stream.status||this.stream.state;"closed"!==a&&"closing"!==a&&this.stream.abort(new Error(e))}}catch(e){var o=(e instanceof Error?e.message:"").toLowerCase();o.includes("closed")||o.includes("closing")||o.includes("write")||null===(r=null===(t=this.log)||void 0===t?void 0:t.trace)||void 0===r||r.call(t,"Stream abort error:",e)}this.cleanup();try{this.dispatchEvent(new CustomEvent("abort",{detail:e}))}catch(e){null===(i=null===(n=this.log)||void 0===n?void 0:n.trace)||void 0===i||i.call(n,"Error in abort event handler:",e)}}},e.prototype.cleanup=function(){this.abortController.signal.aborted||this.abortController.abort(),this.writeQueue.splice(0).forEach(function(){});try{this.p.end()}catch(e){}this.watchdogTimer&&(clearInterval(this.watchdogTimer),this.watchdogTimer=void 0)},e.prototype.flush=function(){return r(this,arguments,void 0,function(e){var t;return void 0===e&&(e=1e4),n(this,function(r){switch(r.label){case 0:if(t=Date.now(),this.queueSize<=0&&!this.isProcessingQueue&&0===this.writeQueue.length)return[2];r.label=1;case 1:return this.abortController.signal.aborted||this.queueSize<=0&&!this.isProcessingQueue&&0===this.writeQueue.length?[2]:Date.now()-t>e?(console.warn("Stream writer: flush timeout with ".concat(this.queueSize," bytes still queued")),[2]):[4,new Promise(function(e){return setTimeout(e,10)})];case 2:return r.sent(),[3,1];case 3:return[2]}})})},e.prototype.addEventListener=function(e,t){var r=this.listeners.get(e)||[];r.push(t),this.listeners.set(e,r)},e.prototype.dispatchEvent=function(e){(this.listeners.get(e.type)||[]).forEach(function(t){return t(e)})},e.prototype.dispatchBackpressureEvent=function(e){this.dispatchEvent(new CustomEvent("backpressure",{detail:e}))},e}(),q=15e3,B=function(){function e(){this.streamIdLock=Promise.resolve(),this.currentStreamId=1}return e.prototype.getNextAppLevelStreamId=function(){return r(this,void 0,void 0,function(){var e=this;return n(this,function(t){return[2,new Promise(function(t){e.streamIdLock=e.streamIdLock.then(function(){var r=e.currentStreamId;e.currentStreamId+=2,t(r)})})]})})},e}(),_=function(){function e(e,t,r,n){this.node=e,this.peerAddr=t,this.protocol=n||"/dc/thread/0.0.1",this.token=r,this.connectionStreamManagers=new WeakMap,this.connectionStates=new WeakMap,this.connectionPool=new Map}return e.prototype.getStreamManagerFor=function(e){var t=this.connectionStreamManagers.get(e);return t||(t=new B,this.connectionStreamManagers.set(e,t)),t},e.prototype.getConnectionState=function(e){var t=this.connectionStates.get(e);return t||(t={activeStreams:0,maxConcurrentStreams:Number.POSITIVE_INFINITY,waiters:[]},this.connectionStates.set(e,t)),t},e.prototype.notifyStreamSlotAvailable=function(e){if(0!==e.waiters.length)for(;e.waiters.length>0&&e.activeStreams<e.maxConcurrentStreams;){var t=e.waiters.shift();if(!t)break;try{t.resolve()}catch(e){console.error("Error resolving stream waiter:",e)}}},e.prototype.rejectStreamWaiters=function(e,t){if(0!==e.waiters.length)for(var r=0,n=e.waiters.splice(0);r<n.length;r++){var i=n[r];try{i.reject(t)}catch(e){console.error("Error rejecting stream waiter:",e)}}},e.prototype.waitForStreamSlot=function(e,t){return r(this,arguments,void 0,function(e,t,r){return void 0===r&&(r=5e3),n(this,function(n){switch(n.label){case 0:if(e.maxConcurrentStreams<=0)throw new Error("No available HTTP/2 streams: server advertised zero concurrent streams");return!Number.isFinite(e.maxConcurrentStreams)||e.activeStreams<e.maxConcurrentStreams?[2]:[4,new Promise(function(n,i){var a,o=!1,s=function(){var r=e.waiters.indexOf(c);r>=0&&e.waiters.splice(r,1),a&&clearTimeout(a),t&&t.removeEventListener("abort",u)},c={resolve:function(){o||(o=!0,s(),n())},reject:function(e){o||(o=!0,s(),i(e))}},u=function(){c.reject(new Error("Aborted while waiting for available HTTP/2 stream slot"))};if(t){if(t.aborted)return void u();t.addEventListener("abort",u,{once:!0})}a=r>0?setTimeout(function(){c.reject(new Error("Timed out waiting for available HTTP/2 stream slot"))},r):void 0,e.waiters.push(c)})];case 1:return n.sent(),[2]}})})},e.prototype.sendFrameWithFlowControl=function(e,t,i,a,o){return r(this,arguments,void 0,function(e,t,r,i,a,o){var s,c,u,l,h,d,f;return void 0===o&&(o=q),n(this,function(n){switch(n.label){case 0:if(null==a?void 0:a.aborted)throw new Error("Operation aborted");if(!((s=Math.max(0,r.length-9))>0))return[3,4];c=e.getSendWindows(t),u=c.conn,l=c.stream,(u<s||l<s)&&console.debug("[stream ".concat(t,"] waiting for send window: need=").concat(s," conn=").concat(u," stream=").concat(l)),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,e.waitForSendWindow(t,s,o)];case 2:return n.sent(),[3,4];case 3:return h=n.sent(),console.warn("[stream ".concat(t,"] send window wait failed (").concat(null!==(f=null==h?void 0:h.message)&&void 0!==f?f:h,"); continuing best-effort")),d=Math.max(s,262144),e.unsafeForceExtendSendWindow(t,d),[3,4];case 4:if(null==a?void 0:a.aborted)throw new Error("Operation aborted");return[4,i.write(r)];case 5:return n.sent(),s>0&&e.consumeSendWindow(t,s),[2]}})})},e.prototype.acquireConnection=function(e){return r(this,void 0,void 0,function(){var t,i,a,o,s,c=this;return n(this,function(u){if(t=this.peerAddr.toString(),!e&&(i=this.connectionPool.get(t))){if(!(a=i.connection))return[2,i.promise];if(!(o=a.status)||"open"===o)return[2,a];this.connectionPool.delete(t)}return s=this.node.dial(this.peerAddr,{signal:AbortSignal.timeout(5e3)}).then(function(i){return r(c,void 0,void 0,function(){var r,a,o=this;return n(this,function(n){if(!e&&(r=this.connectionPool.get(t))&&r.promise===s){r.connection=i,a=function(){var e=o.connectionPool.get(t);e&&e.promise===s&&o.connectionPool.delete(t)};try{i.addEventListener("close",a,{once:!0})}catch(e){}}return[2,i]})})}).catch(function(r){if(!e){var n=c.connectionPool.get(t);n&&n.promise===s&&c.connectionPool.delete(t)}throw r}),e||this.connectionPool.set(t,{promise:s}),[2,s]})})},e.prototype.getDefaultTransportProfile=function(e){return"server-streaming"===e?"compatibility":"flow-control"},e.prototype.setToken=function(e){this.token=e},e.prototype.unaryCall=function(e,t){return r(this,arguments,void 0,function(e,t,r){var i,a,o,s,c,u,l,h,d,f,p,m,g,v,b,y,S,E,T,A,I,P,k,W,C,x=this;return void 0===r&&(r=3e4),n(this,function(n){switch(n.label){case 0:i=null,a=null,o=[],s=-1,c=new w,u=!1,l="",h=!1,d=null,f=null,p=!1,n.label=1;case 1:return n.trys.push([1,23,24,27]),[4,this.acquireConnection(!1)];case 2:d=n.sent(),m=this.peerAddr.toString(),f=this.getConnectionState(d),n.label=3;case 3:return n.trys.push([3,5,,6]),[4,this.waitForStreamSlot(f,void 0,r)];case 4:return n.sent(),f.activeStreams+=1,p=!0,[3,6];case 5:throw g=n.sent(),console.warn("[unaryCall] waiting for stream slot failed:",g),g;case 6:return[4,d.newStream(this.protocol,{maxOutboundStreams:10,negotiateFully:!1})];case 7:return i=n.sent(),[4,this.getStreamManagerFor(d).getNextAppLevelStreamId()];case 8:v=n.sent(),b=new j(i,{bufferSize:16777216});try{b.addEventListener("backpressure",function(e){var t=e.detail||{};console.warn("[unary stream ".concat(v,"] backpressure current=").concat(t.currentSize," avg=").concat(t.averageSize," threshold=").concat(t.threshold))}),b.addEventListener("drain",function(){}),b.addEventListener("stalled",function(e){var t,r=e.detail||{};console.warn("[unary stream ".concat(v,"] stalled queue=").concat(r.queueSize," drained=").concat(r.drained," since=").concat(r.sinceMs,"ms — sending PING"));try{var n=new Uint8Array(8);null===(t=crypto.getRandomValues)||void 0===t||t.call(crypto,n);var i=F.createFrame(6,0,0,n);b.write(i)}catch(e){}})}catch(e){}return(y=new z(b)).onGoaway=function(e){console.warn("[unaryCall] GOAWAY received from server",e),x.connectionPool.delete(m),f&&x.rejectStreamWaiters(f,new Error("Connection received GOAWAY")),u=!0,l="GOAWAY received: code=".concat(e.errorCode);try{null==d||d.close()}catch(e){console.warn("Error closing connection after GOAWAY:",e)}},y.onSettingsParsed=function(e){f&&void 0!==e.maxConcurrentStreams&&e.maxConcurrentStreams>0&&(f.maxConcurrentStreams=e.maxConcurrentStreams,x.notifyStreamSlotAvailable(f))},y.registerOutboundStream(v),s=-1,o=[],y.onData=function(e,t){if(-1===s){if(e.length<5)return;var r=e.slice(1,5);if((s=new DataView(r.buffer,r.byteOffset).getUint32(0,!1))<0)throw new Error("Invalid gRPC message length");if(s+5>e.length){var n=e.subarray(5);return o.push(n),void(s-=n.length)}n=e.subarray(5);o.push(n),a=n,h=!0,s=-1}else if(s>0&&(o.push(e),(s-=e.length)<=0)){a=new Uint8Array(o.reduce(function(e,t){return e+t.length},0));for(var i=0,c=0,u=o;c<u.length;c++){var l=u[c];a.set(l,i),i+=l.length}s=-1,h=!0}if(t&&1&t.flags&&!h){var d=o.reduce(function(e,t){return e+t.length},0);a=new Uint8Array(d);i=0;for(var f=0,p=o;f<p.length;f++){l=p[f];a.set(l,i),i+=l.length}h=!0}},y.onEnd=function(){if(!h)if(h=!0,0===o.length)a=new Uint8Array;else{var e=o.reduce(function(e,t){return e+t.length},0);a=new Uint8Array(e);for(var t=0,r=0,n=o;r<n.length;r++){var i=n[r];a.set(i,t),t+=i.length}h=!0}},y.onSettings=function(){var e=F.createSettingsAckFrame();b.write(e)},y.onHeaders=function(e){var t=c.decodeHeaderFields(e);"0"===t.get("grpc-status")||void 0!==t.get("grpc-status")&&(u=!0,l=t.get("grpc-message")||"gRPC call failed")},y.processStream(i).catch(function(e){console.error("Error in processStream:",e),u=!0,l||(l=e instanceof Error?e.message:"Stream processing failed")}),S=F.createPreface(),[4,b.write(S)];case 9:return n.sent(),E=F.createSettingsFrame(),[4,b.write(E)];case 10:return n.sent(),[4,Promise.race([y.waitForPeerSettings(1e3),y.waitForSettingsAck(),new Promise(function(e){return setTimeout(e,300)})])];case 11:return n.sent(),T=F.createHeadersFrame(v,e,!0,this.token),[4,b.write(T)];case 12:n.sent(),A=F.createDataFrames(v,t,!0),I=r>0?r:q,P=0,k=A,n.label=13;case 13:return P<k.length?(W=k[P],[4,this.sendFrameWithFlowControl(y,v,W,b,void 0,I)]):[3,16];case 14:n.sent(),n.label=15;case 15:return P++,[3,13];case 16:return[4,new Promise(function(e,t){var n=setTimeout(function(){t(new Error("gRPC response timeout"))},r),i=function(){h||u?(clearTimeout(n),e(a)):setTimeout(i,50)};i()})];case 17:n.sent(),n.label=18;case 18:return n.trys.push([18,20,,21]),[4,b.flush(r)];case 19:case 20:return n.sent(),[3,21];case 21:return[4,b.end()];case 22:return n.sent(),[3,27];case 23:throw C=n.sent(),console.error("unaryCall error:",C),C;case 24:return i?[4,i.close()]:[3,26];case 25:n.sent(),n.label=26;case 26:return p&&f&&(f.activeStreams=Math.max(0,f.activeStreams-1),this.notifyStreamSlotAvailable(f)),[7];case 27:if(u)throw new Error(l);return a||(a=new Uint8Array),[2,a]}})})},e.prototype.Call=function(e,t){return r(this,arguments,void 0,function(e,t,i,a,o,c,u,l,h,d){var f,p,m,g,v,b,y,S,E,T,A=this;return void 0===i&&(i=3e4),n(this,function(I){switch(I.label){case 0:f=new AbortController,m=null,g=null!==(T=null==d?void 0:d.transportProfile)&&void 0!==T?T:this.getDefaultTransportProfile(a),v="flow-control"===g,b=function(){if(f.abort(),p&&clearTimeout(p),m)try{m.close()}catch(e){console.error("Error closing stream on cancel:",e)}},(null==h?void 0:h.signal)&&(h.signal.aborted&&(l&&l(new Error("Operation aborted by context")),b()),h.signal.addEventListener("abort",function(){b()})),y=new Promise(function(e,t){p=setTimeout(function(){t(new Error("Operation timed out")),b()},i)}),S=r(A,void 0,void 0,function(){var h,g,b,y,S,E,T,A,I,P,k,W,C,x,O,D,L,M,U,H,B,_,G,N,R,Q,Y,V,Z,K,X,J,$,ee,te,re,ne,ie,ae,oe,se,ce,ue,le,he,de,fe,pe,me,ge=this;return n(this,function(ve){switch(ve.label){case 0:h=new Uint8Array(0),g=-1,b=new w,y=null,S=null,E=null,T=!1,ve.label=1;case 1:if(ve.trys.push([1,49,50,64]),f.signal.aborted)throw new Error("Operation aborted");if(!(null==d?void 0:d.freshConnection))return[3,5];ve.label=2;case 2:return ve.trys.push([2,4,,5]),this.connectionPool.delete(this.peerAddr.toString()),[4,this.node.hangUp(this.peerAddr)];case 3:return ve.sent(),console.warn("[Call] hangUp existing connection before dialing due to freshConnection=true"),[3,5];case 4:return A=ve.sent(),console.warn("[Call] hangUp failed or not supported, proceeding to dial",A),[3,5];case 5:return[4,this.acquireConnection(Boolean(null==d?void 0:d.freshConnection))];case 6:if(y=ve.sent(),S=this.peerAddr.toString(),!(E=this.getConnectionState(y)))return[3,10];ve.label=7;case 7:return ve.trys.push([7,9,,10]),[4,this.waitForStreamSlot(E,f.signal,i)];case 8:return ve.sent(),E.activeStreams+=1,T=!0,[3,10];case 9:throw I=ve.sent(),console.warn("[Call] waiting for stream slot failed:",I),I;case 10:return[4,y.newStream(this.protocol,{maxOutboundStreams:50,signal:AbortSignal.timeout(1e4),negotiateFully:!1})];case 11:return m=ve.sent(),[4,this.getStreamManagerFor(y).getNextAppLevelStreamId()];case 12:P=ve.sent(),k=new j(m,{bufferSize:16777216});try{k.addEventListener("backpressure",function(e){var t=e.detail||{};console.warn("[stream ".concat(P,"] backpressure current=").concat(t.currentSize," avg=").concat(t.averageSize," threshold=").concat(t.threshold))}),k.addEventListener("drain",function(){}),k.addEventListener("stalled",function(e){var t,r=e.detail||{};console.warn("[stream ".concat(P,"] stalled queue=").concat(r.queueSize," drained=").concat(r.drained," since=").concat(r.sinceMs,"ms — sending PING"));try{var n=new Uint8Array(8);null===(t=crypto.getRandomValues)||void 0===t||t.call(crypto,n);var i=F.createFrame(6,0,0,n);k.write(i)}catch(e){}})}catch(e){}if((W=new z(k,{compatibilityMode:!v})).onGoaway=function(e){console.warn("[Call] GOAWAY received from server",e),S&&ge.connectionPool.delete(S),E&&ge.rejectStreamWaiters(E,new Error("Connection received GOAWAY")),l&&l(new Error("GOAWAY received: code=".concat(e.errorCode))),f.abort();try{null==y||y.close()}catch(e){console.warn("Error closing connection after GOAWAY:",e)}},W.onSettingsParsed=function(e){E&&void 0!==e.maxConcurrentStreams&&e.maxConcurrentStreams>0&&(E.maxConcurrentStreams=e.maxConcurrentStreams,ge.notifyStreamSlotAvailable(E))},v&&W.registerOutboundStream(P),C=i>0?i:q,x=function(e){return r(ge,void 0,void 0,function(){return n(this,function(t){switch(t.label){case 0:return v?[4,this.sendFrameWithFlowControl(W,P,e,k,f.signal,C)]:[3,2];case 1:return t.sent(),[3,4];case 2:if(f.signal.aborted)throw new Error("Operation aborted");return[4,k.write(e)];case 3:t.sent(),t.label=4;case 4:return[2]}})})},O=function(e){return r(ge,void 0,void 0,function(){var t,r,i;return n(this,function(n){switch(n.label){case 0:t=0,r=e,n.label=1;case 1:return t<r.length?(i=r[t],[4,x(i)]):[3,4];case 2:n.sent(),n.label=3;case 3:return t++,[3,1];case 4:return[2]}})})},W.onData=function(e){return r(ge,void 0,void 0,function(){var t,r,i;return n(this,function(n){if(f.signal.aborted)return[2];try{for((t=new Uint8Array(h.length+e.length)).set(h),t.set(e,h.length),h=t;h.length>0;){if(f.signal.aborted)return[2];if(-1===g&&h.length>=5&&(r=h.slice(1,5),g=new DataView(r.buffer,r.byteOffset).getUint32(0,!1)),!(-1!==g&&h.length>=g+5))break;i=h.slice(5,g+5),o(i),h=h.slice(g+5),g=-1}}catch(e){if(!l)throw e;l(e)}return[2]})})},W.onSettings=function(){if(!f.signal.aborted){var e=F.createSettingsAckFrame();k.write(e)}},W.onHeaders=function(e){if(!f.signal.aborted){var t=b.decodeHeaderFields(e);if("0"===t.get("grpc-status"));else if(void 0!==t.get("grpc-status")){var r=t.get("grpc-message")||"gRPC call failed",n=new Error(r);if(!l)throw n;l(n)}}},W.processStream(m).catch(function(e){console.error("Error in processStream:",e),l&&l(e)}),f.signal.aborted)throw new Error("Operation aborted");return D=F.createPreface(),[4,k.write(D)];case 13:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return L=F.createSettingsFrame(),[4,k.write(L)];case 14:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return[4,Promise.race([W.waitForPeerSettings(1e3),W.waitForSettingsAck(),new Promise(function(e){return setTimeout(e,300)})])];case 15:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");if(f.signal.aborted)throw new Error("Operation aborted");return M=F.createHeadersFrame(P,e,!0,this.token),"unary"!==a&&"server-streaming"!==a?[3,18]:[4,k.write(M)];case 16:return ve.sent(),U=F.createDataFrames(P,t,!0),[4,O(U)];case 17:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return[3,47];case 18:return"client-streaming"!==a&&"bidirectional"!==a||!c?[3,47]:[4,k.write(M)];case 19:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return t.length>0?(H=F.createDataFrames(P,t,!1),[4,O(H)]):[3,21];case 20:ve.sent(),ve.label=21;case 21:B=(null==d?void 0:d.batchSize)||10,_=[],G=!1,N=function(){return r(ge,void 0,void 0,function(){var e,t,r,i,a,o,s;return n(this,function(n){switch(n.label){case 0:if(G||0===_.length)return[2];G=!0,e=[],n.label=1;case 1:if(n.trys.push([1,9,10,11]),!((e=_.splice(0,Math.min(B,_.length))).length>1))return[3,6];t=0,r=e,n.label=2;case 2:if(!(t<r.length))return[3,5];if(a=r[t],f.signal.aborted)throw new Error("Operation aborted");return i=F.createDataFrames(P,a.chunk,!1),[4,O(i)];case 3:n.sent(),n.label=4;case 4:return t++,[3,2];case 5:return e.forEach(function(e){return e.resolve()}),[3,8];case 6:return 1!==e.length?[3,8]:(a=e[0],o=F.createDataFrames(P,a.chunk,!1),[4,O(o)]);case 7:n.sent(),a.resolve(),n.label=8;case 8:return[3,11];case 9:return s=n.sent(),e.forEach(function(e){try{e.reject(s)}catch(e){console.warn("Error rejecting promise:",e)}}),[3,11];case 10:return G=!1,_.length>0&&!f.signal.aborted&&setTimeout(function(){return N()},0),[7];case 11:return[2]}})})},R=function(e){return new Promise(function(t,r){f.signal.aborted?r(new Error("Operation aborted")):(_.push({chunk:e,resolve:t,reject:r}),(_.length>=B||!G)&&N().catch(function(e){console.error("Error in processNextBatch:",e)}))})},ve.label=22;case 22:ve.trys.push([22,36,,37]),ve.label=23;case 23:ve.trys.push([23,29,30,35]),Q=!0,Y=s(c()),ve.label=24;case 24:return[4,Y.next()];case 25:if(V=ve.sent(),ue=V.done)return[3,28];if(de=V.value,Q=!1,Z=de,f.signal.aborted)throw new Error("Operation aborted");return K=Array.isArray(Z)?Z:[Z],X=K.map(function(e){return R(e)}),[4,Promise.all(X)];case 26:ve.sent(),ve.label=27;case 27:return Q=!0,[3,24];case 28:return[3,35];case 29:return J=ve.sent(),le={error:J},[3,35];case 30:return ve.trys.push([30,,33,34]),Q||ue||!(he=Y.return)?[3,32]:[4,he.call(Y)];case 31:ve.sent(),ve.label=32;case 32:return[3,34];case 33:if(le)throw le.error;return[7];case 34:return[7];case 35:return[3,37];case 36:throw $=ve.sent(),_.splice(0).forEach(function(e){try{e.reject($)}catch(e){console.warn("Error rejecting remaining promise:",e)}}),$;case 37:ee=Date.now(),te=i,ve.label=38;case 38:if(!(_.length>0||G))return[3,40];if(f.signal.aborted)throw new Error("Operation aborted");if(Date.now()-ee>te)throw _.splice(0).forEach(function(e){try{e.reject(new Error("Queue wait timeout"))}catch(e){console.warn("Error rejecting timeout promise:",e)}}),new Error("Queue processing timeout");return[4,new Promise(function(e){return setTimeout(e,10)})];case 39:return ve.sent(),[3,38];case 40:if(f.signal.aborted)throw new Error("Operation aborted");return re=F.createDataFrame(P,new Uint8Array,!0),[4,x(re)];case 41:ve.sent(),ve.label=42;case 42:return ve.trys.push([42,44,,45]),[4,k.flush(i)];case 43:case 44:return ve.sent(),[3,45];case 45:return[4,k.end()];case 46:ve.sent(),ve.label=47;case 47:if(f.signal.aborted)throw new Error("Operation aborted");return[4,W.waitForEndOfStream(0)];case 48:return ve.sent(),u&&u(),[3,64];case 49:return ne=ve.sent(),f.signal.aborted&&ne instanceof Error&&"Operation aborted"===ne.message?l&&l(new Error("Operation cancelled by user")):l?l(ne):ne instanceof Error?console.error("asyncCall error:",ne.message):console.error("asyncCall error:",ne),[3,64];case 50:if(clearTimeout(p),!m)return[3,54];ve.label=51;case 51:return ve.trys.push([51,53,,54]),[4,m.close()];case 52:return ve.sent(),[3,54];case 53:return ie=ve.sent(),console.error("Error closing stream:",ie),[3,54];case 54:if(!(null==d?void 0:d.freshConnection))return[3,63];ve.label=55;case 55:ve.trys.push([55,62,,63]),ae=(null===(pe=(fe=this.node).getConnections)||void 0===pe?void 0:pe.call(fe,this.peerAddr))||[],oe=0,se=ae,ve.label=56;case 56:if(!(oe<se.length))return[3,61];ce=se[oe],ve.label=57;case 57:return ve.trys.push([57,59,,60]),[4,null===(me=ce.close)||void 0===me?void 0:me.call(ce)];case 58:case 59:return ve.sent(),[3,60];case 60:return oe++,[3,56];case 61:return[3,63];case 62:return ve.sent(),[3,63];case 63:return T&&E&&(E.activeStreams=Math.max(0,E.activeStreams-1),this.notifyStreamSlotAvailable(E)),[7];case 64:return[2]}})}),I.label=1;case 1:return I.trys.push([1,3,,4]),[4,Promise.race([S,y])];case 2:return I.sent(),[2,b];case 3:throw E=I.sent(),b(),E;case 4:return[2]}})})},e}();e.Libp2pGrpcClient=_});//# sourceMappingURL=grpc.min.js.map
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).GrpcLibp2pClient={})}(this,function(e){"use strict";"undefined"!=typeof window&&window;var t=function(){return t=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var i in t=arguments[r])Object.prototype.hasOwnProperty.call(t,i)&&(e[i]=t[i]);return e},t.apply(this,arguments)};function r(e,t,r,n){return new(r||(r=Promise))(function(i,a){function o(e){try{c(n.next(e))}catch(e){a(e)}}function s(e){try{c(n.throw(e))}catch(e){a(e)}}function c(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(o,s)}c((n=n.apply(e,t||[])).next())})}function n(e,t){var r,n,i,a={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]},o=Object.create(("function"==typeof Iterator?Iterator:Object).prototype);return o.next=s(0),o.throw=s(1),o.return=s(2),"function"==typeof Symbol&&(o[Symbol.iterator]=function(){return this}),o;function s(s){return function(c){return function(s){if(r)throw new TypeError("Generator is already executing.");for(;o&&(o=0,s[0]&&(a=0)),a;)try{if(r=1,n&&(i=2&s[0]?n.return:s[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,s[1])).done)return i;switch(n=0,i&&(s=[2&s[0],i.value]),s[0]){case 0:case 1:i=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,n=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!(i=a.trys,(i=i.length>0&&i[i.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!i||s[1]>i[0]&&s[1]<i[3])){a.label=s[1];break}if(6===s[0]&&a.label<i[1]){a.label=i[1],i=s;break}if(i&&a.label<i[2]){a.label=i[2],a.ops.push(s);break}i[2]&&a.ops.pop(),a.trys.pop();continue}s=t.call(e,a)}catch(e){s=[6,e],n=0}finally{r=i=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,c])}}}function i(e){var t="function"==typeof Symbol&&Symbol.iterator,r=t&&e[t],n=0;if(r)return r.call(e);if(e&&"number"==typeof e.length)return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}};throw new TypeError(t?"Object is not iterable.":"Symbol.iterator is not defined.")}function a(e){return this instanceof a?(this.v=e,this):new a(e)}function o(e,t,r){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n,i=r.apply(e,t||[]),o=[];return n=Object.create(("function"==typeof AsyncIterator?AsyncIterator:Object).prototype),s("next"),s("throw"),s("return",function(e){return function(t){return Promise.resolve(t).then(e,l)}}),n[Symbol.asyncIterator]=function(){return this},n;function s(e,t){i[e]&&(n[e]=function(t){return new Promise(function(r,n){o.push([e,t,r,n])>1||c(e,t)})},t&&(n[e]=t(n[e])))}function c(e,t){try{(r=i[e](t)).value instanceof a?Promise.resolve(r.value.v).then(u,l):h(o[0][2],r)}catch(e){h(o[0][3],e)}var r}function u(e){c("next",e)}function l(e){c("throw",e)}function h(e,t){e(t),o.shift(),o.length&&c(o[0][0],o[0][1])}}function s(e){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var t,r=e[Symbol.asyncIterator];return r?r.call(e):(e=i(e),t={},n("next"),n("throw"),n("return"),t[Symbol.asyncIterator]=function(){return this},t);function n(r){t[r]=e[r]&&function(t){return new Promise(function(n,i){(function(e,t,r,n){Promise.resolve(n).then(function(t){e({value:t,done:r})},t)})(n,i,(t=e[r](t)).done,t.value)})}}}"function"==typeof SuppressedError&&SuppressedError;var c,u=0,l=1,h=3,d=4,f=6,p=7,m=8,g=1,v=1,w=function(){function e(e){void 0===e&&(e=4096),this.dynamicTable=[],this.dynamicTableSize=0,this.maxDynamicTableSize=e,this.staticTable=[["",""],[":authority",""],[":method","GET"],[":method","POST"],[":path","/"],[":path","/index.html"],[":scheme","http"],[":scheme","https"],[":status","200"],[":status","204"],[":status","206"],[":status","304"],[":status","400"],[":status","404"],[":status","500"],["accept-charset",""],["accept-encoding","gzip, deflate"],["accept-language",""],["accept-ranges",""],["accept",""],["access-control-allow-origin",""],["age",""],["allow",""],["authorization",""],["cache-control",""],["content-disposition",""],["content-encoding",""],["content-language",""],["content-length",""],["content-location",""],["content-range",""],["content-type",""],["cookie",""],["date",""],["etag",""],["expect",""],["expires",""],["from",""],["host",""],["if-match",""],["if-modified-since",""],["if-none-match",""],["if-range",""],["if-unmodified-since",""],["last-modified",""],["link",""],["location",""],["max-forwards",""],["proxy-authenticate",""],["proxy-authorization",""],["range",""],["referer",""],["refresh",""],["retry-after",""],["server",""],["set-cookie",""],["strict-transport-security",""],["transfer-encoding",""],["user-agent",""],["vary",""],["via",""],["www-authenticate",""]],this.huffmanTable=this.buildHuffmanTable()}return e.prototype.encode=function(e){for(var t=[],r=0,n=Object.entries(e);r<n.length;r++){var i=n[r],a=i[0],o=i[1],s=this.findInStaticTable(a,o);if(-1===s){var c=this.findInDynamicTable(a,o);-1===c?this.encodeLiteral(t,a,o):t.push.apply(t,this.encodeInteger(c+this.staticTable.length,7,128))}else t.push.apply(t,this.encodeInteger(s,7,128))}return new Uint8Array(t)},e.prototype.encodeInteger=function(e,t,r){void 0===r&&(r=0);var n=[],i=(1<<t)-1;if(e<i)return n.push(r|e),n;for(n.push(r|i),e-=i;e>=128;)n.push(127&e|128),e>>=7;return n.push(e),n},e.prototype.encodeString=function(e){var t=[],r=(new TextEncoder).encode(e),n=this.huffmanEncode(r);return n.length<r.length?(t.push.apply(t,this.encodeInteger(n.length,7,128)),Array.from(n).forEach(function(e){return t.push(e)})):(t.push.apply(t,this.encodeInteger(r.length,7,0)),Array.from(r).forEach(function(e){return t.push(e)})),t},e.prototype.findInStaticTable=function(e,t){for(var r=1;r<this.staticTable.length;r++)if(this.staticTable[r][0]===e&&this.staticTable[r][1]===t)return r;return-1},e.prototype.findInDynamicTable=function(e,t){for(var r=0;r<this.dynamicTable.length;r++)if(this.dynamicTable[r][0]===e&&this.dynamicTable[r][1]===t)return r;return-1},e.prototype.encodeLiteral=function(e,t,r){var n=this.findInStaticTable(t,"");-1!==n?e.push.apply(e,this.encodeInteger(n,6,64)):(e.push(64),e.push.apply(e,this.encodeString(t))),e.push.apply(e,this.encodeString(r)),this.addToDynamicTable(t,r)},e.prototype.addToDynamicTable=function(e,t){for(var r=e.length+t.length+32;this.dynamicTableSize+r>this.maxDynamicTableSize&&this.dynamicTable.length>0;){var n=this.dynamicTable.pop();n&&(this.dynamicTableSize-=n[0].length+n[1].length+32)}r<=this.maxDynamicTableSize&&(this.dynamicTable.unshift([e,t]),this.dynamicTableSize+=r),this.dynamicTable.push([e,t])},e.prototype.getIndexedHeader=function(e){return e<=this.staticTable.length-1?this.staticTable[e]:this.dynamicTable[e-this.staticTable.length]},e.prototype.buildHuffmanTable=function(){return{codes:new Uint32Array([8184,8388568,268435426,268435427,268435428,268435429,268435430,268435431,268435432,16777194,1073741820,268435433,268435434,1073741821,268435435,268435436,268435437,268435438,268435439,268435440,268435441,268435442,1073741822,268435443,268435444,268435445,268435446,268435447,268435448,268435449,268435450,268435451,20,1016,1017,4090,8185,21,248,2042,1018,1019,249,2043,250,22,23,24,0,1,2,25,26,27,28,29,30,31,92,251,32764,32,4091,1020,8186,33,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,252,115,253,8187,524272,8188,16380,34,32765,3,35,4,36,5,37,38,39,6,116,117,40,41,42,7,43,118,44,8,9,45,119,120,121,122,123,32766,2044,16381,8189,268435452,1048550,4194258,1048551,1048552,4194259,4194260,4194261,8388569,4194262,8388570,8388571,8388572,8388573,8388574,16777195,8388575,16777196,16777197,4194263,8388576,16777198,8388577,8388578,8388579,8388580,2097116,4194264,8388581,4194265,8388582,8388583,16777199,4194266,2097117,1048553,4194267,4194268,8388584,8388585,2097118,8388586,4194269,4194270,16777200,2097119,4194271,8388587,8388588,2097120,2097121,4194272,2097122,8388589,4194273,8388590,8388591,1048554,4194274,4194275,4194276,8388592,4194277,4194278,8388593,67108832,67108833,1048555,524273,4194279,8388594,4194280,33554412,67108834,67108835,67108836,134217694,134217695,67108837,16777201,33554413,524274,2097123,67108838,134217696,134217697,67108839,134217698,16777202,2097124,2097125,67108840,67108841,268435453,134217699,134217700,134217701,1048556,16777203,1048557,2097126,4194281,2097127,2097128,8388595,4194282,4194283,33554414,33554415,16777204,16777205,67108842,8388596,67108843,134217702,67108844,67108845,134217703,134217704,134217705,134217706,134217707,268435454,134217708,134217709,134217710,134217711,134217712,67108846]),lengths:new Uint8Array([13,23,28,28,28,28,28,28,28,24,30,28,28,30,28,28,28,28,28,28,28,28,30,28,28,28,28,28,28,28,28,28,6,10,10,12,13,6,8,11,10,10,8,11,8,6,6,6,5,5,5,6,6,6,6,6,6,6,7,8,15,6,12,10,13,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,7,8,13,19,13,14,6,15,5,6,5,6,5,6,6,6,5,7,7,6,6,6,5,6,7,6,5,5,6,7,7,7,7,7,15,11,14,13,28,20,22,20,20,22,22,22,23,22,23,23,23,23,23,24,23,24,24,22,23,24,23,23,23,23,21,22,23,22,23,23,24,22,21,20,22,22,23,23,21,23,22,22,24,21,22,23,23,21,21,22,21,23,22,23,23,20,22,22,22,23,22,22,23,26,26,20,19,22,23,22,25,26,26,26,27,27,26,24,25,19,21,26,27,27,26,27,24,21,21,26,26,28,27,27,27,20,24,20,21,22,21,21,23,22,22,25,25,24,24,26,23,26,27,26,26,27,27,27,27,27,28,27,27,27,27,27,26])}},e.prototype.decode=function(e){for(var t="",r=0,n=0,i=0;i<e.length;i++){for(r=r<<8|e[i],n+=8;n>=5;){var a=this.findSymbol(r,n);if(!a)break;var o=a[0],s=a[1];t+=String.fromCharCode(o),r&=(1<<n-s)-1,n-=s}}if(n>0){var c=(1<<n)-1;if((r&c)!==c)throw new Error("Invalid Huffman padding")}return t},e.prototype.findSymbol=function(e,t){for(var r=0;r<this.huffmanTable.codes.length;r++){var n=this.huffmanTable.lengths[r];if(!(t<n))if((e>>t-n&(1<<n)-1)===this.huffmanTable.codes[r])return[r,n]}return null},e.prototype.huffmanEncode=function(e){for(var t=[],r=0,n=0,i=0;i<e.length;i++){var a=e[i],o=this.huffmanTable.codes[a],s=this.huffmanTable.lengths[a];for(n+=s,r=r<<s|o;n>=8;)n-=8,t.push(r>>n&255)}return n>0&&(r=r<<8-n|(1<<8-n)-1,t.push(255&r)),new Uint8Array(t)},e.prototype.decodeHeaderFields=function(e){for(var t=new Map,r=0;r<e.length;){var n=e[r];if(128&n){var i=this.decodeIndexedHeader(e,r),a=i[0],o=i[1],s=i[2];a&&o&&t.set(a,o),r=s}else if(64&n){var c=this.decodeLiteralHeaderWithIndexing(e,r),u=c[0];o=c[1],s=c[2];u&&o&&t.set(u,o),r=s}else if(32&n)r++;else if(16&n){var l=this.decodeLiteralHeaderWithoutIndexing(e,r),h=l[0];o=l[1],s=l[2];h&&o&&t.set(h,o),r=s}else{var d=this.decodeLiteralHeaderWithoutIndexing(e,r),f=d[0];o=d[1],s=d[2];f&&o&&t.set(f,o),r=s}}return t},e.prototype.decodeInteger=function(e,t,r){var n=(1<<r)-1,i=t,a=e[t]&n;if(a<n)return[a,i+1];i++;for(var o=0;i<e.length;){var s=e[i++];if(a+=(127&s)<<o,o+=7,!(128&s))break}return[a,i]},e.prototype.decodeLiteralString=function(e,t){if(t>=e.length)return["",t];var r=!!(128&e[t]),n=this.decodeInteger(e,t,7),i=n[0],a=n[1];if(a+i>e.length)return["",a];var o,s=e.slice(a,a+i);if(r)try{o=this.decode(s)}catch(e){o=""}else try{o=(new TextDecoder).decode(s)}catch(e){o=""}return[o,a+i]},e.prototype.decodeIndexedHeader=function(e,t){var r=this.decodeInteger(e,t,7),n=r[0],i=r[1];if(n<=0)return["","",i];var a=this.staticTable[n];return a?[a[0],a[1],i]:["","",i]},e.prototype.decodeLiteralHeaderWithIndexing=function(e,t){var r,n=this.decodeInteger(e,t,6),i=n[0];if(t=n[1],i>0){var a=this.staticTable[i];r=a?a[0]:""}else{var o=this.decodeLiteralString(e,t);r=o[0],t=o[1]}var s=this.decodeLiteralString(e,t);return[r,s[0],s[1]]},e.prototype.decodeLiteralHeaderWithoutIndexing=function(e,t){return this.decodeLiteralHeaderWithIndexing(e,t)},e.prototype.huffmanDecodeToString=function(e){return this.decode(e)},e}(),b=function(){function e(){}return e.encodeSettingsFrame=function(e){var t=e.payload,r=6*t.length,n=new Uint8Array(9+r);return y(n,{length:r,type:e.type,flags:e.flags,streamId:e.streamId}),function(e,t){var r=9;t.forEach(function(t){!function(e,t,r){e[r]=t>>8&255,e[r+1]=255&t}(e,t.identifier,r),r+=2,S(e,t.value,r),r+=4})}(n,t),n},e.encodeSettingsAckFrame=function(){var e=new Uint8Array(9);return y(e,{length:0,type:4,flags:1,streamId:0}),e},e}();function y(e,t){var r,n=null!==(r=t.length)&&void 0!==r?r:0;e[0]=n>>16&255,e[1]=n>>8&255,e[2]=255&n,e[3]=t.type,e[4]=t.flags,S(e,2147483647&t.streamId,5)}function S(e,t,r){e[r]=t>>24&255,e[r+1]=t>>16&255,e[r+2]=t>>8&255,e[r+3]=255&t}var E=1,T=2,A=3,I=4,P=5,k=6,W=((c={})[E]=4096,c[T]=1,c[A]=100,c[I]=16384,c[P]=16384,c[k]=8192,c),F=function(){function e(){}return e.createPreface=function(){return(new TextEncoder).encode("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n")},e.createPongFrame=function(t){return e.createFrame(6,1,0,t)},e.createSettingsFrame=function(t){void 0===t&&(t={});var r=e.createOriginSettingsFrame(t);return b.encodeSettingsFrame(r)},e.createSettingsAckFrame=function(){return b.encodeSettingsAckFrame()},e.createWindowUpdateFrame=function(t,r){if(r<=0||r>2147483647)throw new Error("Window size increment must be between 1 and 2^31-1");var n=new Uint8Array(4);return n[0]=r>>24&127,n[1]=r>>16&255,n[2]=r>>8&255,n[3]=255&r,e.createFrame(8,0,t,n)},e.createDataFrames=function(t,r,n,i){void 0===n&&(n=!1),void 0===i&&(i=16384);var a=[],o=r.length,s=new Uint8Array(5+o);s[0]=0,s[1]=o>>24&255,s[2]=o>>16&255,s[3]=o>>8&255,s[4]=255&o,s.set(r,5);for(var c=i-9,u=0;u<s.length;u+=c){var l=s.length-u,h=Math.min(c,l),d=s.slice(u,u+h),f=u+h>=s.length&&n,p=e.createFrame(0,f?1:0,t,d);a.push(p)}return a},e.createDataFrame=function(t,r,n){void 0===n&&(n=!0);var i=r.length,a=new Uint8Array(5+i);a[0]=0,a[1]=i>>24&255,a[2]=i>>16&255,a[3]=i>>8&255,a[4]=255&i,a.set(r,5);var o=n?1:0;return e.createFrame(0,o,t,a)},e.createHeadersFrame=function(t,r,n,i){void 0===n&&(n=!0);var a={":path":r,":method":"POST",":scheme":"http",":authority":"localhost","content-type":"application/grpc+proto","user-agent":"grpc-web-client/0.1",accept:"application/grpc+proto","grpc-timeout":"3600S"};i&&(a.authorization="Bearer ".concat(i));var o=(new w).encode(a),s=n?4:0;return e.createFrame(1,s,t,o)},e.createResponseHeadersFrame=function(t,r,n){void 0===n&&(n=!0);var i=(new w).encode(r),a=n?4:0;return e.createFrame(1,a,t,i)},e.createTrailersFrame=function(t,r){var n=(new w).encode(r);return e.createFrame(1,5,t,n)},e.createOriginSettingsFrame=function(e){void 0===e&&(e={});var r=t(t({},W),e);return function(e){for(var t=0,r=Object.entries(e);t<r.length;t++){var n=r[t],i=n[0],a=n[1];switch(Number(i)){case E:if(a<0)throw new Error("HEADER_TABLE_SIZE must be non-negative");break;case T:if(0!==a&&1!==a)throw new Error("ENABLE_PUSH must be 0 or 1");break;case I:if(a<0||a>2147483647)throw new Error("INITIAL_WINDOW_SIZE must be between 0 and 2^31-1");break;case P:if(a<16384||a>16777215)throw new Error("MAX_FRAME_SIZE must be between 16,384 and 16,777,215");break;case A:case k:if(a<0)throw new Error("Parameter ".concat(i," must be non-negative"));break;default:console.warn("Unknown settings parameter: ".concat(i))}}}(r),{type:4,flags:0,streamId:0,payload:C(r)}},e.createOriginSettingsAckFrame=function(){return{type:4,flags:1,streamId:0,payload:[]}},e.createFrame=function(e,t,r,n){var i=n?n.length:0,a=new Uint8Array(9+i);return a[0]=i>>16&255,a[1]=i>>8&255,a[2]=255&i,a[3]=e,a[4]=t,a[5]=r>>24&255,a[6]=r>>16&255,a[7]=r>>8&255,a[8]=255&r,n&&i>0&&a.set(n,9),a},e}();function C(e){for(var t=[],r=0,n=Object.entries(e);r<n.length;r++){var i=n[r],a=i[0],o=i[1];t.push({identifier:Number(a),value:o})}return t}var x=(new TextEncoder).encode("PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"),z=function(){function e(e,t){var r;this.buffer=new Uint8Array(0),this.settingsAckReceived=!1,this.peerSettingsReceived=!1,this.connectionWindowSize=4<<20,this.streams=new Map,this.defaultStreamWindowSize=4<<20,this.sendConnWindow=65535,this.sendStreamWindows=new Map,this.peerInitialStreamWindow=65535,this.sendWindowWaiters=[],this.endFlag=!1,this.writer=e,this.compatibilityMode=null!==(r=null==t?void 0:t.compatibilityMode)&&void 0!==r&&r}return e.prototype.processStream=function(e){return r(this,void 0,void 0,function(){var t,r,i,a,o,c,u,l,h,d,f;return n(this,function(n){switch(n.label){case 0:n.trys.push([0,13,,14]),n.label=1;case 1:n.trys.push([1,6,7,12]),a=!0,o=s(e),n.label=2;case 2:return[4,o.next()];case 3:if(c=n.sent(),u=c.done)return[3,5];d=c.value,a=!1,t=d,this._processChunk(t),n.label=4;case 4:return a=!0,[3,2];case 5:return[3,12];case 6:return r=n.sent(),l={error:r},[3,12];case 7:return n.trys.push([7,,10,11]),a||u||!(h=o.return)?[3,9]:[4,h.call(o)];case 8:n.sent(),n.label=9;case 9:return[3,11];case 10:if(l)throw l.error;return[7];case 11:return[7];case 12:if(!this.compatibilityMode&&!this.endFlag){this.endFlag=!0;try{null===(f=this.onEnd)||void 0===f||f.call(this)}catch(e){console.error("Error during onEnd callback:",e)}}return[3,14];case 13:throw i=n.sent(),console.error("Error processing stream:",i),i;case 14:return[2]}})})},e.prototype._processChunk=function(e){var t="subarray"in e&&"function"==typeof e.subarray?e.subarray():e,r=new Uint8Array(this.buffer.length+t.length);r.set(this.buffer),r.set(t,this.buffer.length),this.buffer=r;for(var n=0;this.buffer.length-n>=9;)if(this.buffer.length-n>=24&&this.isHttp2Preface(this.buffer.subarray(n))){n+=24;var i=F.createSettingsFrame();this.writer.write(i)}else{var a=this._parseFrameHeader(this.buffer.subarray(n)),o=9+a.length;if(this.buffer.length-n<o)break;var s=this.buffer.subarray(n,n+o);this._handleFrame(a,s).catch(function(e){console.error("Error handling frame:",e)}),n+=o}n>0&&(this.buffer=this.buffer.slice(n))},e.prototype.isHttp2Preface=function(e){if(e.length<x.length)return!1;for(var t=0;t<x.length;t++)if(e[t]!==x[t])return!1;return!0},e.prototype._oldProcessStream_removed=function(){},e.prototype.waitForSettingsAck=function(){var e=this;return new Promise(function(t,r){if(e.settingsAckReceived)t();else var n=setInterval(function(){e.settingsAckReceived&&(clearInterval(n),clearTimeout(i),t())},100),i=setTimeout(function(){clearInterval(n),r(new Error("Settings ACK timeout"))},3e4)})},e.prototype.waitForPeerSettings=function(e){var t=this;return void 0===e&&(e=3e4),new Promise(function(r,n){if(t.peerSettingsReceived)r();else var i=setInterval(function(){t.peerSettingsReceived&&(clearInterval(i),clearTimeout(a),r())},100),a=setTimeout(function(){clearInterval(i),n(new Error("Peer SETTINGS timeout"))},e)})},e.prototype.registerOutboundStream=function(e){this.sendStreamWindows.has(e)||this.sendStreamWindows.set(e,this.peerInitialStreamWindow)},e.prototype.getSendWindows=function(e){var t,r=null!==(t=this.sendStreamWindows.get(e))&&void 0!==t?t:0;return{conn:this.sendConnWindow,stream:r}},e.prototype.consumeSendWindow=function(e,t){var r;this.sendConnWindow=Math.max(0,this.sendConnWindow-t);var n=null!==(r=this.sendStreamWindows.get(e))&&void 0!==r?r:0;this.sendStreamWindows.set(e,Math.max(0,n-t))},e.prototype.unsafeForceExtendSendWindow=function(e,t){var r;if(!(this.compatibilityMode||t<=0)){this.sendConnWindow=Math.min(2147483647,this.sendConnWindow+t);var n=null!==(r=this.sendStreamWindows.get(e))&&void 0!==r?r:0;this.sendStreamWindows.set(e,Math.min(2147483647,n+t))}},e.prototype.waitForSendWindow=function(e){return r(this,arguments,void 0,function(e,t,r){var i,a=this;return void 0===t&&(t=1),void 0===r&&(r=3e4),n(this,function(n){return i=Date.now(),[2,new Promise(function(n,o){var s=null,c=!1,u=function(){var u=a.getSendWindows(e),l=u.conn,h=u.stream;return l>=t&&h>=t?(c||(c=!0,s&&(clearInterval(s),s=null),n()),!0):Date.now()-i>r&&(c||(c=!0,s&&(clearInterval(s),s=null),o(new Error("Send window wait timeout"))),!0)};if(!u()){a.sendWindowWaiters.push(function(){u()}),s=setInterval(function(){u()&&s&&(clearInterval(s),s=null)},50)}})]})})},e.prototype._handleFrame=function(e,t){return r(this,void 0,void 0,function(){var r,i,a,o,s,c,w,b,y,S,E,T,A,I,P,k,W,C,x,z,O,D,L=this;return n(this,function(n){switch(e.type){case d:if((e.flags&g)===g)this.settingsAckReceived=!0;else{for(r=t.slice(9),i={},a=0,o=void 0,s=0;s<r.length;s+=6)c=r[s]<<8|r[s+1],w=r[s+2]<<24|r[s+3]<<16|r[s+4]<<8|r[s+5],i[c]=w,4===c?(this.defaultStreamWindowSize=w,a=w-this.peerInitialStreamWindow,this.peerInitialStreamWindow=w):3===c&&(o=w);this.compatibilityMode||0===a||Array.from(this.sendStreamWindows.entries()).forEach(function(e){var t=e[0],r=e[1],n=Math.max(0,r+a);L.sendStreamWindows.set(t,n)});try{!this.onSettingsParsed||void 0===o&&0===a||(b={},void 0!==o&&(b.maxConcurrentStreams=o),0!==a&&(b.initialWindowSize=this.peerInitialStreamWindow),this.onSettingsParsed(b))}catch(e){console.error("Error handling parsed SETTINGS callback:",e)}this.onSettings&&this.onSettings(e),this.peerSettingsReceived=!0,this.sendWindowWaiters.splice(0).forEach(function(e){try{e()}catch(e){console.debug("waiter error",e)}})}break;case u:this.onData&&this.onData(t.slice(9),e);try{0!==e.streamId&&(y=F.createWindowUpdateFrame(e.streamId,null!==(C=e.length)&&void 0!==C?C:0),this.writer.write(y)),S=F.createWindowUpdateFrame(0,null!==(x=e.length)&&void 0!==x?x:0),this.writer.write(S)}catch(e){console.error("[HTTP2] Error sending window update:",e)}if((e.flags&v)===v)return this.endFlag=!0,this.onEnd&&this.onEnd(),[2];break;case l:if(this.onHeaders&&this.onHeaders(t.slice(9),e),(e.flags&v)===v)return this.endFlag=!0,this.onEnd&&this.onEnd(),[2];break;case m:this.handleWindowUpdateFrame(e,t);try{E=this.parseWindowUpdateFrame(t,e).windowSizeIncrement,0===e.streamId?this.sendConnWindow+=E:(T=null!==(z=this.sendStreamWindows.get(e.streamId))&&void 0!==z?z:this.peerInitialStreamWindow,this.sendStreamWindows.set(e.streamId,T+E)),this.sendWindowWaiters.splice(0).forEach(function(e){try{e()}catch(e){console.debug("waiter error",e)}})}catch(e){}break;case f:this._handlePingFrame(e,t);break;case p:A=void 0;try{(I=t.subarray(9)).length>=8?(P=new DataView(I.buffer,I.byteOffset,I.byteLength),k=2147483647&P.getUint32(0,!1),W=P.getUint32(4,!1),A={lastStreamId:k,errorCode:W},console.warn("[HTTP2] GOAWAY received",A)):(console.warn("[HTTP2] GOAWAY received"),A={})}catch(e){}try{null===(O=this.onGoaway)||void 0===O||O.call(this,null!=A?A:{})}catch(e){console.error("Error during GOAWAY callback:",e)}this.endFlag=!0;try{null===(D=this.onEnd)||void 0===D||D.call(this)}catch(e){console.error("Error during GOAWAY onEnd callback:",e)}break;case h:this.endFlag=!0,this.onEnd&&this.onEnd();break;default:console.debug("Unknown frame type:",e.type)}return[2]})})},e.prototype._parseFrameHeader=function(e){return{length:e[0]<<16|e[1]<<8|e[2],type:e[3],flags:e[4],streamId:e[5]<<24|e[6]<<16|e[7]<<8|e[8],payload:e.slice(0,9)}},e.prototype._handlePingFrame=function(e,t){if(8!==e.length)throw new Error("PING frame must have a length of 8 bytes");if(!(e.flags&g)){var r=F.createPongFrame(t.slice(9));try{this.writer.write(r)}catch(e){throw console.error("Error sending PONG frame:",e),e}}},e.prototype.waitForEndOfStream=function(e){var t=this;return new Promise(function(r,n){if(t.endFlag)r();else{var i=null;e>0&&(i=setTimeout(function(){clearInterval(a),n(new Error("End of stream timeout"))},e));var a=setInterval(function(){t.endFlag&&(null!==i&&clearTimeout(i),clearInterval(a),r())},100),o=t.onEnd;t.onEnd=function(){t.endFlag||(t.endFlag=!0),null!==i&&clearTimeout(i),clearInterval(a),r(),o&&o()}}})},e.prototype.parseWindowUpdateFrame=function(e,t){if(4!==t.length)throw new Error("WINDOW_UPDATE frame must have a length of 4 bytes");var r=new Uint8Array(e.slice(9)),n=(127&r[0])<<24|r[1]<<16|r[2]<<8|r[3];if(0===n)throw new Error("WINDOW_UPDATE increment must not be zero");return{windowSizeIncrement:n}},e.prototype.handleWindowUpdateFrame=function(e,t){try{var r=this.parseWindowUpdateFrame(t,e);return this.connectionWindowSize+=r.windowSizeIncrement,r}catch(e){throw console.error("Error handling WINDOW_UPDATE frame:",e),e}},e}();function O(){const e={};return e.promise=new Promise((t,r)=>{e.resolve=t,e.reject=r}),e}class D{buffer;mask;top;btm;next;constructor(e){if(!(e>0)||e-1&e)throw new Error("Max size for a FixedFIFO should be a power of two");this.buffer=new Array(e),this.mask=e-1,this.top=0,this.btm=0,this.next=null}push(e){return void 0===this.buffer[this.top]&&(this.buffer[this.top]=e,this.top=this.top+1&this.mask,!0)}shift(){const e=this.buffer[this.btm];if(void 0!==e)return this.buffer[this.btm]=void 0,this.btm=this.btm+1&this.mask,e}isEmpty(){return void 0===this.buffer[this.btm]}}class L{size;hwm;head;tail;constructor(e={}){this.hwm=e.splitLimit??16,this.head=new D(this.hwm),this.tail=this.head,this.size=0}calculateSize(e){return null!=e?.byteLength?e.byteLength:1}push(e){if(null!=e?.value&&(this.size+=this.calculateSize(e.value)),!this.head.push(e)){const t=this.head;this.head=t.next=new D(2*this.head.buffer.length),this.head.push(e)}}shift(){let e=this.tail.shift();if(void 0===e&&null!=this.tail.next){const t=this.tail.next;this.tail.next=null,this.tail=t,e=this.tail.shift()}return null!=e?.value&&(this.size-=this.calculateSize(e.value)),e}isEmpty(){return this.head.isEmpty()}}class M extends Error{type;code;constructor(e,t){super(e??"The operation was aborted"),this.type="aborted",this.code=t??"ABORT_ERR"}}function U(e={}){return function(e,t){t=t??{};let r,n,i,a=t.onEnd,o=new L,s=O();const c=async()=>{try{return o.isEmpty()?i?{done:!0}:await new Promise((t,i)=>{n=a=>{n=null,o.push(a);try{t(e(o))}catch(e){i(e)}return r}}):e(o)}finally{o.isEmpty()&&queueMicrotask(()=>{s.resolve(),s=O()})}},u=e=>null!=n?n(e):(o.push(e),r),l=e=>(o=new L,null!=n?n({error:e}):(o.push({error:e}),r)),h=e=>{if(i)return r;if(!0!==t?.objectMode&&null==e?.byteLength)throw new Error("objectMode was not true but tried to push non-Uint8Array value");return u({done:!1,value:e})},d=e=>i?r:(i=!0,null!=e?l(e):u({done:!0})),f=()=>(o=new L,d(),{done:!0}),p=e=>(d(e),{done:!0});if(r={[Symbol.asyncIterator](){return this},next:c,return:f,throw:p,push:h,end:d,get readableLength(){return o.size},onEmpty:async e=>{const t=e?.signal;if(t?.throwIfAborted(),o.isEmpty())return;let r,n;null!=t&&(r=new Promise((e,r)=>{n=()=>{r(new M)},t.addEventListener("abort",n)}));try{await Promise.race([s.promise,r])}finally{null!=n&&null!=t&&t?.removeEventListener("abort",n)}}},null==a)return r;const m=r;return r={[Symbol.asyncIterator](){return this},next:()=>m.next(),throw:e=>(m.throw(e),null!=a&&(a(e),a=void 0),{done:!0}),return:()=>(m.return(),null!=a&&(a(),a=void 0),{done:!0}),push:h,end:e=>(m.end(e),null!=a&&(a(e),a=void 0),r),get readableLength(){return m.readableLength},onEmpty:e=>m.onEmpty(e)},r}(e=>{const t=e.shift();if(null==t)return{done:!0};if(null!=t.error)throw t.error;return{done:!0===t.done,value:t.value}},e)}var H=4091,j=function(){function e(e,t){var r,n,i;if(void 0===t&&(t={}),this.stream=e,this.options=t,this.bytesWritten=0,this.abortController=new AbortController,this.backpressureHistory=[],this.isBackpressure=!1,this.writeQueue=[],this.isProcessingQueue=!1,this.lastBackpressureCheck=0,this.bytesDrained=0,this.lastDrainEventAt=0,this.stallStartAt=0,this.lastBytesDrainedSeen=0,this.lastBpWarnAt=0,this.isHandlingError=!1,this.listeners=new Map,!e)throw new Error("StreamWriter requires a valid stream object");this.log={trace:function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return console.debug.apply(console,function(e,t,r){if(r||2===arguments.length)for(var n,i=0,a=t.length;i<a;i++)!n&&i in t||(n||(n=Array.prototype.slice.call(t,0,i)),n[i]=t[i]);return e.concat(n||Array.prototype.slice.call(t))}(["[StreamWriter]"],e,!1))}},this.options=t?{chunkSize:null!==(r=t.chunkSize)&&void 0!==r?r:H,bufferSize:null!==(n=t.bufferSize)&&void 0!==n?n:5242880,retries:null!==(i=t.retries)&&void 0!==i?i:3}:{chunkSize:H,bufferSize:5242880,retries:3},this.options.chunkSize&&this.options.chunkSize>H&&(this.options.chunkSize=H);var a=U({objectMode:!1});a._originalPush=a.push.bind(a),a._originalNext=a.next.bind(a),a._queueSize=0,Object.defineProperty(a,"push",{value:function(e){return a._queueSize+=e.byteLength,a._originalPush(e)},writable:!1,configurable:!1}),this.p=a,this.startPipeline(),this.startWatchdog()}return Object.defineProperty(e.prototype,"queueSize",{get:function(){return this.p._queueSize},enumerable:!1,configurable:!0}),e.prototype.getAverageQueueSize=function(){var e=Date.now(),t=this.queueSize;return e-this.lastBackpressureCheck>100&&(t>0&&(this.backpressureHistory.push(t),this.backpressureHistory.length>5&&this.backpressureHistory.shift()),this.lastBackpressureCheck=e),0===this.backpressureHistory.length?t:this.backpressureHistory.reduce(function(e,t){return e+t},0)/this.backpressureHistory.length},e.prototype.handleError=function(e){var t,r;if(!this.abortController.signal.aborted&&!this.isHandlingError){this.isHandlingError=!0,this.abort(e.message);try{this.dispatchEvent(new CustomEvent("error",{detail:e}))}catch(e){null===(r=null===(t=this.log)||void 0===t?void 0:t.trace)||void 0===r||r.call(t,"Error in error event handler:",e)}}},e.prototype.startPipeline=function(){var e=this;this.pipeToStream().catch(function(t){return e.handleError(t)})},e.prototype.pipeToStream=function(){return r(this,void 0,void 0,function(){var e,t,r,i,a,o,c,u,l,h,d,f,p,m,g;return n(this,function(n){switch(n.label){case 0:if(!this.stream)return null===(p=null===(f=this.log)||void 0===f?void 0:f.trace)||void 0===p||p.call(f,"Stream is null/undefined, cannot start pipeline"),[2];n.label=1;case 1:n.trys.push([1,10,11,16]),e=!0,t=s(this.createTransform()(this.p)),n.label=2;case 2:return[4,t.next()];case 3:if(r=n.sent(),u=r.done)return[3,9];if(d=r.value,e=!1,i=d,this.abortController.signal.aborted)return[3,9];n.label=4;case 4:return n.trys.push([4,7,,8]),this.stream.send(i)?[3,6]:[4,this.stream.onDrain()];case 5:n.sent(),n.label=6;case 6:return[3,8];case 7:if(a=n.sent(),o=(a instanceof Error?a.message:"").toLowerCase(),a instanceof Error&&"StreamStateError"===a.name||o.includes("closing")||o.includes("closed")||o.includes("write to a stream that is closed"))return null===(g=null===(m=this.log)||void 0===m?void 0:m.trace)||void 0===g||g.call(m,"Stream is closing/closed, stopping pipeline"),[3,9];throw a;case 8:return e=!0,[3,2];case 9:return[3,16];case 10:return c=n.sent(),l={error:c},[3,16];case 11:return n.trys.push([11,,14,15]),e||u||!(h=t.return)?[3,13]:[4,h.call(t)];case 12:n.sent(),n.label=13;case 13:return[3,15];case 14:if(l)throw l.error;return[7];case 15:return[7];case 16:return[2]}})})},e.prototype.createTransform=function(){var e=this;return function(t){return o(this,arguments,function(){var r,i,o,c,u,l,h,d,f,p;return n(this,function(n){switch(n.label){case 0:n.trys.push([0,7,8,13]),r=!0,i=s(t),n.label=1;case 1:return[4,a(i.next())];case 2:return o=n.sent(),(h=o.done)?[3,6]:(p=o.value,r=!1,[4,a(c=p)]);case 3:return[4,n.sent()];case 4:n.sent();try{null!=e.p._queueSize&&(e.p._queueSize=Math.max(0,e.p._queueSize-c.byteLength)),e.bytesDrained+=c.byteLength,(u=Date.now())-e.lastDrainEventAt>250&&(e.lastDrainEventAt=u,e.dispatchEvent(new CustomEvent("drain",{detail:{drained:e.bytesDrained,queueSize:e.queueSize}}))),e.lastBytesDrainedSeen=e.bytesDrained}catch(e){console.error("Error updating bytesDrained in StreamWriter transform",e)}n.label=5;case 5:return r=!0,[3,1];case 6:return[3,13];case 7:return l=n.sent(),d={error:l},[3,13];case 8:return n.trys.push([8,,11,12]),r||h||!(f=i.return)?[3,10]:[4,a(f.call(i))];case 9:n.sent(),n.label=10;case 10:return[3,12];case 11:if(d)throw d.error;return[7];case 12:return[7];case 13:return[2]}})})}},e.prototype.startWatchdog=function(e,t){var r=this;if(void 0===e&&(e=500),void 0===t&&(t=1500),!this.watchdogTimer){var n=function(){if(!r.abortController.signal.aborted){var i=.7*r.options.bufferSize,a=r.queueSize,o=Date.now();if(a>=i)if(r.lastBytesDrainedSeen===r.bytesDrained){if(r.stallStartAt||(r.stallStartAt=o),o-r.stallStartAt>=t){var s={queueSize:a,drained:r.bytesDrained,sinceMs:o-r.stallStartAt};setTimeout(function(){r.abortController.signal.aborted||r.dispatchEvent(new CustomEvent("stalled",{detail:s}))},0),r.stallStartAt=o}}else r.stallStartAt=0,r.lastBytesDrainedSeen=r.bytesDrained;else r.stallStartAt=0;r.watchdogTimer=setTimeout(n,e)}};this.watchdogTimer=setTimeout(n,e)}},e.prototype.write=function(e){return r(this,void 0,void 0,function(){var t=this;return n(this,function(i){return this.abortController.signal.aborted?[2,Promise.resolve()]:[2,new Promise(function(i,a){t.writeQueue.push(function(){return r(t,void 0,void 0,function(){var t,r;return n(this,function(n){switch(n.label){case 0:return n.trys.push([0,3,,4]),this.abortController.signal.aborted?(i(),[2]):[4,this.convertToBuffer(e)];case 1:return t=n.sent(),[4,this.writeChunks(t)];case 2:return n.sent(),i(),[3,4];case 3:return r=n.sent(),a(r),[3,4];case 4:return[2]}})})}),t.processQueue()})]})})},e.prototype.convertToBuffer=function(e){return r(this,void 0,void 0,function(){return n(this,function(t){return e instanceof Blob?[2,e.arrayBuffer()]:"string"==typeof e?[2,(new TextEncoder).encode(e).buffer]:e instanceof Uint8Array?[2,e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)]:[2,e]})})},e.prototype.writeChunks=function(e){return r(this,void 0,void 0,function(){var t,r,i;return n(this,function(n){switch(n.label){case 0:t=0,n.label=1;case 1:return t<e.byteLength?(r=Math.min(t+this.options.chunkSize,e.byteLength),(i=new Uint8Array(r-t)).set(new Uint8Array(e.slice(t,r))),[4,this.retryableWrite(i)]):[3,4];case 2:n.sent(),this.updateProgress(i.byteLength),n.label=3;case 3:return t+=this.options.chunkSize,[3,1];case 4:return[2]}})})},e.prototype.retryableWrite=function(e){return r(this,arguments,void 0,function(e,t){var r,i,a,o,s=this;return void 0===t&&(t=0),n(this,function(n){switch(n.label){case 0:if(this.abortController.signal.aborted)throw new Error("Stream is aborted, cannot write");n.label=1;case 1:return n.trys.push([1,5,,8]),r=this.queueSize,i=.7*this.options.bufferSize,r>i?[4,this.monitorBackpressure()]:[3,3];case 2:n.sent(),n.label=3;case 3:if(this.abortController.signal.aborted)throw new Error("Stream aborted during backpressure monitoring");return[4,new Promise(function(t,r){try{s.p.push(e)}catch(e){r(e)}t()})];case 4:return n.sent(),[3,8];case 5:return a=n.sent(),t<this.options.retries?(o=this.calculateRetryDelay(t),[4,new Promise(function(e){return setTimeout(e,o)})]):[3,7];case 6:return n.sent(),[2,this.retryableWrite(e,t+1)];case 7:throw a;case 8:return[2]}})})},e.prototype.monitorBackpressure=function(){return r(this,void 0,void 0,function(){var e,t,r,i,a,o,s,c;return n(this,function(n){switch(n.label){case 0:if(e=this.queueSize,t=.7*this.options.bufferSize,r=.9*this.options.bufferSize,e<t)return this.isBackpressure&&(this.isBackpressure=!1,this.dispatchBackpressureEvent({currentSize:e,averageSize:this.getAverageQueueSize(),threshold:t,waitingTime:0})),[2];this.isBackpressure||(this.isBackpressure=!0,this.dispatchBackpressureEvent({currentSize:e,averageSize:this.getAverageQueueSize(),threshold:t,waitingTime:0})),i=e/this.options.bufferSize,a=e>=r?50+Math.min(200,100*i):Math.min(20,30*i),o=0,s=3,n.label=1;case 1:return this.queueSize>=t&&o<s?this.abortController.signal.aborted?[3,3]:[4,new Promise(function(e){return setTimeout(e,a)})]:[3,3];case 2:return n.sent(),o++,a=Math.min(1.5*a,100),[3,1];case 3:return this.queueSize>=t&&(c=Date.now())-this.lastBpWarnAt>1e3&&(this.lastBpWarnAt=c,console.warn("Stream writer: High backpressure detected (".concat(this.queueSize," bytes), continuing anyway"))),[2]}})})},e.prototype.calculateRetryDelay=function(e){return Math.min(10*Math.pow(2,e)+100*Math.random(),100)},e.prototype.processQueue=function(){return r(this,void 0,void 0,function(){var e,t;return n(this,function(r){switch(r.label){case 0:if(this.isProcessingQueue||this.abortController.signal.aborted)return[2];this.isProcessingQueue=!0,r.label=1;case 1:return this.writeQueue.length>0?this.abortController.signal.aborted?[3,5]:(e=this.queueSize,t=.5*this.options.bufferSize,e>t?[4,this.monitorBackpressure()]:[3,3]):[3,5];case 2:r.sent(),r.label=3;case 3:return[4,this.writeQueue.shift()()];case 4:return r.sent(),[3,1];case 5:return this.isProcessingQueue=!1,[2]}})})},e.prototype.updateProgress=function(e){this.bytesWritten+=e,this.dispatchEvent(new CustomEvent("progress",{detail:{loaded:this.bytesWritten}}))},e.prototype.end=function(){return r(this,void 0,void 0,function(){var e,t,r,i;return n(this,function(n){switch(n.label){case 0:if(this.p.end(),!this.stream||"function"!=typeof this.stream.close)return[3,4];n.label=1;case 1:return n.trys.push([1,3,,4]),[4,this.stream.close()];case 2:return n.sent(),[3,4];case 3:return e=n.sent(),(t=(e instanceof Error?e.message:"").toLowerCase()).includes("closed")||t.includes("closing")||null===(i=null===(r=this.log)||void 0===r?void 0:r.trace)||void 0===i||i.call(r,"Stream close error:",e),[3,4];case 4:return this.cleanup(),[2]}})})},e.prototype.abort=function(e){var t,r,n,i;if(void 0===e&&(e="User aborted"),!this.abortController.signal.aborted){this.abortController.abort(e);try{if(this.stream&&"function"==typeof this.stream.abort){var a=this.stream.status||this.stream.state;"closed"!==a&&"closing"!==a&&this.stream.abort(new Error(e))}}catch(e){var o=(e instanceof Error?e.message:"").toLowerCase();o.includes("closed")||o.includes("closing")||o.includes("write")||null===(r=null===(t=this.log)||void 0===t?void 0:t.trace)||void 0===r||r.call(t,"Stream abort error:",e)}this.cleanup();try{this.dispatchEvent(new CustomEvent("abort",{detail:e}))}catch(e){null===(i=null===(n=this.log)||void 0===n?void 0:n.trace)||void 0===i||i.call(n,"Error in abort event handler:",e)}}},e.prototype.cleanup=function(){this.abortController.signal.aborted||this.abortController.abort(),this.writeQueue.splice(0).forEach(function(){});try{this.p.end()}catch(e){}this.watchdogTimer&&(clearTimeout(this.watchdogTimer),this.watchdogTimer=void 0)},e.prototype.flush=function(){return r(this,arguments,void 0,function(e){var t;return void 0===e&&(e=1e4),n(this,function(r){switch(r.label){case 0:if(t=Date.now(),this.queueSize<=0&&!this.isProcessingQueue&&0===this.writeQueue.length)return[2];r.label=1;case 1:return this.abortController.signal.aborted||this.queueSize<=0&&!this.isProcessingQueue&&0===this.writeQueue.length?[2]:Date.now()-t>e?(console.warn("Stream writer: flush timeout with ".concat(this.queueSize," bytes still queued")),[2]):[4,new Promise(function(e){return setTimeout(e,10)})];case 2:return r.sent(),[3,1];case 3:return[2]}})})},e.prototype.addEventListener=function(e,t){var r=this.listeners.get(e)||[];r.push(t),this.listeners.set(e,r)},e.prototype.dispatchEvent=function(e){(this.listeners.get(e.type)||[]).forEach(function(t){return t(e)})},e.prototype.dispatchBackpressureEvent=function(e){this.dispatchEvent(new CustomEvent("backpressure",{detail:e}))},e}(),q=15e3,B=function(){function e(){this.streamIdLock=Promise.resolve(),this.currentStreamId=1}return e.prototype.getNextAppLevelStreamId=function(){return r(this,void 0,void 0,function(){var e=this;return n(this,function(t){return[2,new Promise(function(t){e.streamIdLock=e.streamIdLock.then(function(){var r=e.currentStreamId;e.currentStreamId+=2,t(r)})})]})})},e}(),_=function(){function e(e,t,r,n){this.node=e,this.peerAddr=t,this.protocol=n||"/dc/thread/0.0.1",this.token=r,this.connectionStreamManagers=new WeakMap,this.connectionStates=new WeakMap,this.connectionPool=new Map}return e.prototype.getStreamManagerFor=function(e){var t=this.connectionStreamManagers.get(e);return t||(t=new B,this.connectionStreamManagers.set(e,t)),t},e.prototype.getConnectionState=function(e){var t=this.connectionStates.get(e);return t||(t={activeStreams:0,maxConcurrentStreams:Number.POSITIVE_INFINITY,waiters:[]},this.connectionStates.set(e,t)),t},e.prototype.notifyStreamSlotAvailable=function(e){if(0!==e.waiters.length)for(;e.waiters.length>0&&e.activeStreams<e.maxConcurrentStreams;){var t=e.waiters.shift();if(!t)break;try{t.resolve()}catch(e){console.error("Error resolving stream waiter:",e)}}},e.prototype.rejectStreamWaiters=function(e,t){if(0!==e.waiters.length)for(var r=0,n=e.waiters.splice(0);r<n.length;r++){var i=n[r];try{i.reject(t)}catch(e){console.error("Error rejecting stream waiter:",e)}}},e.prototype.waitForStreamSlot=function(e,t){return r(this,arguments,void 0,function(e,t,r){return void 0===r&&(r=5e3),n(this,function(n){switch(n.label){case 0:if(e.maxConcurrentStreams<=0)throw new Error("No available HTTP/2 streams: server advertised zero concurrent streams");return!Number.isFinite(e.maxConcurrentStreams)||e.activeStreams<e.maxConcurrentStreams?[2]:[4,new Promise(function(n,i){var a,o=!1,s=function(){var r=e.waiters.indexOf(c);r>=0&&e.waiters.splice(r,1),a&&clearTimeout(a),t&&t.removeEventListener("abort",u)},c={resolve:function(){o||(o=!0,s(),n())},reject:function(e){o||(o=!0,s(),i(e))}},u=function(){c.reject(new Error("Aborted while waiting for available HTTP/2 stream slot"))};if(t){if(t.aborted)return void u();t.addEventListener("abort",u,{once:!0})}a=r>0?setTimeout(function(){c.reject(new Error("Timed out waiting for available HTTP/2 stream slot"))},r):void 0,e.waiters.push(c)})];case 1:return n.sent(),[2]}})})},e.prototype.sendFrameWithFlowControl=function(e,t,i,a,o){return r(this,arguments,void 0,function(e,t,r,i,a,o){var s,c,u,l,h,d,f;return void 0===o&&(o=q),n(this,function(n){switch(n.label){case 0:if(null==a?void 0:a.aborted)throw new Error("Operation aborted");if(!((s=Math.max(0,r.length-9))>0))return[3,4];c=e.getSendWindows(t),u=c.conn,l=c.stream,(u<s||l<s)&&console.debug("[stream ".concat(t,"] waiting for send window: need=").concat(s," conn=").concat(u," stream=").concat(l)),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,e.waitForSendWindow(t,s,o)];case 2:return n.sent(),[3,4];case 3:return h=n.sent(),console.warn("[stream ".concat(t,"] send window wait failed (").concat(null!==(f=null==h?void 0:h.message)&&void 0!==f?f:h,"); continuing best-effort")),d=Math.max(s,262144),e.unsafeForceExtendSendWindow(t,d),[3,4];case 4:if(null==a?void 0:a.aborted)throw new Error("Operation aborted");return[4,i.write(r)];case 5:return n.sent(),s>0&&e.consumeSendWindow(t,s),[2]}})})},e.prototype.acquireConnection=function(e){return r(this,void 0,void 0,function(){var t,i,a,o,s,c=this;return n(this,function(u){if(t=this.peerAddr.toString(),!e&&(i=this.connectionPool.get(t))){if(!(a=i.connection))return[2,i.promise];if(!(o=a.status)||"open"===o)return[2,a];this.connectionPool.delete(t)}return s=this.node.dial(this.peerAddr,{signal:AbortSignal.timeout(5e3)}).then(function(i){return r(c,void 0,void 0,function(){var r,a,o=this;return n(this,function(n){if(!e&&(r=this.connectionPool.get(t))&&r.promise===s){r.connection=i,a=function(){var e=o.connectionPool.get(t);e&&e.promise===s&&o.connectionPool.delete(t)};try{i.addEventListener("close",a,{once:!0})}catch(e){}}return[2,i]})})}).catch(function(r){if(!e){var n=c.connectionPool.get(t);n&&n.promise===s&&c.connectionPool.delete(t)}throw r}),e||this.connectionPool.set(t,{promise:s}),[2,s]})})},e.prototype.getDefaultTransportProfile=function(e){return"server-streaming"===e?"compatibility":"flow-control"},e.prototype.setToken=function(e){this.token=e},e.prototype.unaryCall=function(e,t){return r(this,arguments,void 0,function(e,t,r){var i,a,o,s,c,u,l,h,d,f,p,m,g,v,b,y,S,E,T,A,I,P,k,W,C,x=this;return void 0===r&&(r=3e4),n(this,function(n){switch(n.label){case 0:i=null,a=null,o=[],s=-1,c=new w,u=!1,l="",h=!1,d=null,f=null,p=!1,n.label=1;case 1:return n.trys.push([1,23,24,27]),[4,this.acquireConnection(!1)];case 2:d=n.sent(),m=this.peerAddr.toString(),f=this.getConnectionState(d),n.label=3;case 3:return n.trys.push([3,5,,6]),[4,this.waitForStreamSlot(f,void 0,r)];case 4:return n.sent(),f.activeStreams+=1,p=!0,[3,6];case 5:throw g=n.sent(),console.warn("[unaryCall] waiting for stream slot failed:",g),g;case 6:return[4,d.newStream(this.protocol,{maxOutboundStreams:10,negotiateFully:!1})];case 7:return i=n.sent(),[4,this.getStreamManagerFor(d).getNextAppLevelStreamId()];case 8:v=n.sent(),b=new j(i,{bufferSize:16777216});try{b.addEventListener("backpressure",function(e){var t=e.detail||{};console.warn("[unary stream ".concat(v,"] backpressure current=").concat(t.currentSize," avg=").concat(t.averageSize," threshold=").concat(t.threshold))}),b.addEventListener("drain",function(){}),b.addEventListener("stalled",function(e){var t,r=e.detail||{};console.warn("[unary stream ".concat(v,"] stalled queue=").concat(r.queueSize," drained=").concat(r.drained," since=").concat(r.sinceMs,"ms — sending PING"));try{var n=new Uint8Array(8);null===(t=crypto.getRandomValues)||void 0===t||t.call(crypto,n);var i=F.createFrame(6,0,0,n);b.write(i)}catch(e){}})}catch(e){}return(y=new z(b)).onGoaway=function(e){console.warn("[unaryCall] GOAWAY received from server",e),x.connectionPool.delete(m),f&&x.rejectStreamWaiters(f,new Error("Connection received GOAWAY")),u=!0,l="GOAWAY received: code=".concat(e.errorCode);try{null==d||d.close()}catch(e){console.warn("Error closing connection after GOAWAY:",e)}},y.onSettingsParsed=function(e){f&&void 0!==e.maxConcurrentStreams&&e.maxConcurrentStreams>0&&(f.maxConcurrentStreams=e.maxConcurrentStreams,x.notifyStreamSlotAvailable(f))},y.registerOutboundStream(v),s=-1,o=[],y.onData=function(e,t){if(-1===s){if(e.length<5)return;var r=e.slice(1,5);if((s=new DataView(r.buffer,r.byteOffset).getUint32(0,!1))<0)throw new Error("Invalid gRPC message length");if(s+5>e.length){var n=e.subarray(5);return o.push(n),void(s-=n.length)}n=e.subarray(5);o.push(n),a=n,h=!0,s=-1}else if(s>0&&(o.push(e),(s-=e.length)<=0)){a=new Uint8Array(o.reduce(function(e,t){return e+t.length},0));for(var i=0,c=0,u=o;c<u.length;c++){var l=u[c];a.set(l,i),i+=l.length}s=-1,h=!0}if(t&&1&t.flags&&!h){var d=o.reduce(function(e,t){return e+t.length},0);a=new Uint8Array(d);i=0;for(var f=0,p=o;f<p.length;f++){l=p[f];a.set(l,i),i+=l.length}h=!0}},y.onEnd=function(){if(!h)if(h=!0,0===o.length)a=new Uint8Array;else{var e=o.reduce(function(e,t){return e+t.length},0);a=new Uint8Array(e);for(var t=0,r=0,n=o;r<n.length;r++){var i=n[r];a.set(i,t),t+=i.length}h=!0}},y.onSettings=function(){var e=F.createSettingsAckFrame();b.write(e)},y.onHeaders=function(e){var t=c.decodeHeaderFields(e);"0"===t.get("grpc-status")||void 0!==t.get("grpc-status")&&(u=!0,l=t.get("grpc-message")||"gRPC call failed")},y.processStream(i).catch(function(e){console.error("Error in processStream:",e),u=!0,l||(l=e instanceof Error?e.message:"Stream processing failed")}),S=F.createPreface(),[4,b.write(S)];case 9:return n.sent(),E=F.createSettingsFrame(),[4,b.write(E)];case 10:return n.sent(),[4,Promise.race([y.waitForPeerSettings(1e3),y.waitForSettingsAck(),new Promise(function(e){return setTimeout(e,300)})])];case 11:return n.sent(),T=F.createHeadersFrame(v,e,!0,this.token),[4,b.write(T)];case 12:n.sent(),A=F.createDataFrames(v,t,!0),I=r>0?r:q,P=0,k=A,n.label=13;case 13:return P<k.length?(W=k[P],[4,this.sendFrameWithFlowControl(y,v,W,b,void 0,I)]):[3,16];case 14:n.sent(),n.label=15;case 15:return P++,[3,13];case 16:return[4,new Promise(function(e,t){var n=setTimeout(function(){t(new Error("gRPC response timeout"))},r),i=function(){h||u?(clearTimeout(n),e(a)):setTimeout(i,50)};i()})];case 17:n.sent(),n.label=18;case 18:return n.trys.push([18,20,,21]),[4,b.flush(r)];case 19:case 20:return n.sent(),[3,21];case 21:return[4,b.end()];case 22:return n.sent(),[3,27];case 23:throw C=n.sent(),console.error("unaryCall error:",C),C;case 24:return i?[4,i.close()]:[3,26];case 25:n.sent(),n.label=26;case 26:return p&&f&&(f.activeStreams=Math.max(0,f.activeStreams-1),this.notifyStreamSlotAvailable(f)),[7];case 27:if(u)throw new Error(l);return a||(a=new Uint8Array),[2,a]}})})},e.prototype.Call=function(e,t){return r(this,arguments,void 0,function(e,t,i,a,o,c,u,l,h,d){var f,p,m,g,v,b,y,S,E,T,A=this;return void 0===i&&(i=3e4),n(this,function(I){switch(I.label){case 0:f=new AbortController,m=null,g=null!==(T=null==d?void 0:d.transportProfile)&&void 0!==T?T:this.getDefaultTransportProfile(a),v="flow-control"===g,b=function(){if(f.abort(),p&&clearTimeout(p),m)try{m.close()}catch(e){console.error("Error closing stream on cancel:",e)}},(null==h?void 0:h.signal)&&(h.signal.aborted&&(l&&l(new Error("Operation aborted by context")),b()),h.signal.addEventListener("abort",function(){b()})),y=new Promise(function(e,t){p=setTimeout(function(){t(new Error("Operation timed out")),b()},i)}),S=r(A,void 0,void 0,function(){var h,g,b,y,S,E,T,A,I,P,k,W,C,x,O,D,L,M,U,H,B,_,G,N,R,Q,Y,V,Z,K,X,J,$,ee,te,re,ne,ie,ae,oe,se,ce,ue,le,he,de,fe,pe,me,ge=this;return n(this,function(ve){switch(ve.label){case 0:h=new Uint8Array(0),g=-1,b=new w,y=null,S=null,E=null,T=!1,ve.label=1;case 1:if(ve.trys.push([1,49,50,64]),f.signal.aborted)throw new Error("Operation aborted");if(!(null==d?void 0:d.freshConnection))return[3,5];ve.label=2;case 2:return ve.trys.push([2,4,,5]),this.connectionPool.delete(this.peerAddr.toString()),[4,this.node.hangUp(this.peerAddr)];case 3:return ve.sent(),console.warn("[Call] hangUp existing connection before dialing due to freshConnection=true"),[3,5];case 4:return A=ve.sent(),console.warn("[Call] hangUp failed or not supported, proceeding to dial",A),[3,5];case 5:return[4,this.acquireConnection(Boolean(null==d?void 0:d.freshConnection))];case 6:if(y=ve.sent(),S=this.peerAddr.toString(),!(E=this.getConnectionState(y)))return[3,10];ve.label=7;case 7:return ve.trys.push([7,9,,10]),[4,this.waitForStreamSlot(E,f.signal,i)];case 8:return ve.sent(),E.activeStreams+=1,T=!0,[3,10];case 9:throw I=ve.sent(),console.warn("[Call] waiting for stream slot failed:",I),I;case 10:return[4,y.newStream(this.protocol,{maxOutboundStreams:50,signal:AbortSignal.timeout(1e4),negotiateFully:!1})];case 11:return m=ve.sent(),[4,this.getStreamManagerFor(y).getNextAppLevelStreamId()];case 12:P=ve.sent(),k=new j(m,{bufferSize:16777216});try{k.addEventListener("backpressure",function(e){var t=e.detail||{};console.warn("[stream ".concat(P,"] backpressure current=").concat(t.currentSize," avg=").concat(t.averageSize," threshold=").concat(t.threshold))}),k.addEventListener("drain",function(){}),k.addEventListener("stalled",function(e){var t,r=e.detail||{};console.warn("[stream ".concat(P,"] stalled queue=").concat(r.queueSize," drained=").concat(r.drained," since=").concat(r.sinceMs,"ms — sending PING"));try{var n=new Uint8Array(8);null===(t=crypto.getRandomValues)||void 0===t||t.call(crypto,n);var i=F.createFrame(6,0,0,n);k.write(i)}catch(e){}})}catch(e){}if((W=new z(k,{compatibilityMode:!v})).onGoaway=function(e){console.warn("[Call] GOAWAY received from server",e),S&&ge.connectionPool.delete(S),E&&ge.rejectStreamWaiters(E,new Error("Connection received GOAWAY")),l&&l(new Error("GOAWAY received: code=".concat(e.errorCode))),f.abort();try{null==y||y.close()}catch(e){console.warn("Error closing connection after GOAWAY:",e)}},W.onSettingsParsed=function(e){E&&void 0!==e.maxConcurrentStreams&&e.maxConcurrentStreams>0&&(E.maxConcurrentStreams=e.maxConcurrentStreams,ge.notifyStreamSlotAvailable(E))},v&&W.registerOutboundStream(P),C=i>0?i:q,x=function(e){return r(ge,void 0,void 0,function(){return n(this,function(t){switch(t.label){case 0:return v?[4,this.sendFrameWithFlowControl(W,P,e,k,f.signal,C)]:[3,2];case 1:return t.sent(),[3,4];case 2:if(f.signal.aborted)throw new Error("Operation aborted");return[4,k.write(e)];case 3:t.sent(),t.label=4;case 4:return[2]}})})},O=function(e){return r(ge,void 0,void 0,function(){var t,r,i;return n(this,function(n){switch(n.label){case 0:t=0,r=e,n.label=1;case 1:return t<r.length?(i=r[t],[4,x(i)]):[3,4];case 2:n.sent(),n.label=3;case 3:return t++,[3,1];case 4:return[2]}})})},W.onData=function(e){return r(ge,void 0,void 0,function(){var t,r,i;return n(this,function(n){if(f.signal.aborted)return[2];try{for((t=new Uint8Array(h.length+e.length)).set(h),t.set(e,h.length),h=t;h.length>0;){if(f.signal.aborted)return[2];if(-1===g&&h.length>=5&&(r=h.slice(1,5),g=new DataView(r.buffer,r.byteOffset).getUint32(0,!1)),!(-1!==g&&h.length>=g+5))break;i=h.slice(5,g+5),o(i),h=h.slice(g+5),g=-1}}catch(e){if(!l)throw e;l(e)}return[2]})})},W.onSettings=function(){if(!f.signal.aborted){var e=F.createSettingsAckFrame();k.write(e)}},W.onHeaders=function(e){if(!f.signal.aborted){var t=b.decodeHeaderFields(e);if("0"===t.get("grpc-status"));else if(void 0!==t.get("grpc-status")){var r=t.get("grpc-message")||"gRPC call failed",n=new Error(r);if(!l)throw n;l(n)}}},W.processStream(m).catch(function(e){console.error("Error in processStream:",e),l&&l(e)}),f.signal.aborted)throw new Error("Operation aborted");return D=F.createPreface(),[4,k.write(D)];case 13:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return L=F.createSettingsFrame(),[4,k.write(L)];case 14:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return[4,Promise.race([W.waitForPeerSettings(1e3),W.waitForSettingsAck(),new Promise(function(e){return setTimeout(e,300)})])];case 15:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");if(f.signal.aborted)throw new Error("Operation aborted");return M=F.createHeadersFrame(P,e,!0,this.token),"unary"!==a&&"server-streaming"!==a?[3,18]:[4,k.write(M)];case 16:return ve.sent(),U=F.createDataFrames(P,t,!0),[4,O(U)];case 17:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return[3,47];case 18:return"client-streaming"!==a&&"bidirectional"!==a||!c?[3,47]:[4,k.write(M)];case 19:if(ve.sent(),f.signal.aborted)throw new Error("Operation aborted");return t.length>0?(H=F.createDataFrames(P,t,!1),[4,O(H)]):[3,21];case 20:ve.sent(),ve.label=21;case 21:B=(null==d?void 0:d.batchSize)||10,_=[],G=!1,N=function(){return r(ge,void 0,void 0,function(){var e,t,r,i,a,o,s;return n(this,function(n){switch(n.label){case 0:if(G||0===_.length)return[2];G=!0,e=[],n.label=1;case 1:if(n.trys.push([1,9,10,11]),!((e=_.splice(0,Math.min(B,_.length))).length>1))return[3,6];t=0,r=e,n.label=2;case 2:if(!(t<r.length))return[3,5];if(a=r[t],f.signal.aborted)throw new Error("Operation aborted");return i=F.createDataFrames(P,a.chunk,!1),[4,O(i)];case 3:n.sent(),n.label=4;case 4:return t++,[3,2];case 5:return e.forEach(function(e){return e.resolve()}),[3,8];case 6:return 1!==e.length?[3,8]:(a=e[0],o=F.createDataFrames(P,a.chunk,!1),[4,O(o)]);case 7:n.sent(),a.resolve(),n.label=8;case 8:return[3,11];case 9:return s=n.sent(),e.forEach(function(e){try{e.reject(s)}catch(e){console.warn("Error rejecting promise:",e)}}),[3,11];case 10:return G=!1,_.length>0&&!f.signal.aborted&&setTimeout(function(){return N()},0),[7];case 11:return[2]}})})},R=function(e){return new Promise(function(t,r){f.signal.aborted?r(new Error("Operation aborted")):(_.push({chunk:e,resolve:t,reject:r}),(_.length>=B||!G)&&N().catch(function(e){console.error("Error in processNextBatch:",e)}))})},ve.label=22;case 22:ve.trys.push([22,36,,37]),ve.label=23;case 23:ve.trys.push([23,29,30,35]),Q=!0,Y=s(c()),ve.label=24;case 24:return[4,Y.next()];case 25:if(V=ve.sent(),ue=V.done)return[3,28];if(de=V.value,Q=!1,Z=de,f.signal.aborted)throw new Error("Operation aborted");return K=Array.isArray(Z)?Z:[Z],X=K.map(function(e){return R(e)}),[4,Promise.all(X)];case 26:ve.sent(),ve.label=27;case 27:return Q=!0,[3,24];case 28:return[3,35];case 29:return J=ve.sent(),le={error:J},[3,35];case 30:return ve.trys.push([30,,33,34]),Q||ue||!(he=Y.return)?[3,32]:[4,he.call(Y)];case 31:ve.sent(),ve.label=32;case 32:return[3,34];case 33:if(le)throw le.error;return[7];case 34:return[7];case 35:return[3,37];case 36:throw $=ve.sent(),_.splice(0).forEach(function(e){try{e.reject($)}catch(e){console.warn("Error rejecting remaining promise:",e)}}),$;case 37:ee=Date.now(),te=i,ve.label=38;case 38:if(!(_.length>0||G))return[3,40];if(f.signal.aborted)throw new Error("Operation aborted");if(Date.now()-ee>te)throw _.splice(0).forEach(function(e){try{e.reject(new Error("Queue wait timeout"))}catch(e){console.warn("Error rejecting timeout promise:",e)}}),new Error("Queue processing timeout");return[4,new Promise(function(e){return setTimeout(e,10)})];case 39:return ve.sent(),[3,38];case 40:if(f.signal.aborted)throw new Error("Operation aborted");return re=F.createDataFrame(P,new Uint8Array,!0),[4,x(re)];case 41:ve.sent(),ve.label=42;case 42:return ve.trys.push([42,44,,45]),[4,k.flush(i)];case 43:case 44:return ve.sent(),[3,45];case 45:return[4,k.end()];case 46:ve.sent(),ve.label=47;case 47:if(f.signal.aborted)throw new Error("Operation aborted");return[4,W.waitForEndOfStream(0)];case 48:return ve.sent(),u&&u(),[3,64];case 49:return ne=ve.sent(),f.signal.aborted&&ne instanceof Error&&"Operation aborted"===ne.message?l&&l(new Error("Operation cancelled by user")):l?l(ne):ne instanceof Error?console.error("asyncCall error:",ne.message):console.error("asyncCall error:",ne),[3,64];case 50:if(clearTimeout(p),!m)return[3,54];ve.label=51;case 51:return ve.trys.push([51,53,,54]),[4,m.close()];case 52:return ve.sent(),[3,54];case 53:return ie=ve.sent(),console.error("Error closing stream:",ie),[3,54];case 54:if(!(null==d?void 0:d.freshConnection))return[3,63];ve.label=55;case 55:ve.trys.push([55,62,,63]),ae=(null===(pe=(fe=this.node).getConnections)||void 0===pe?void 0:pe.call(fe,this.peerAddr))||[],oe=0,se=ae,ve.label=56;case 56:if(!(oe<se.length))return[3,61];ce=se[oe],ve.label=57;case 57:return ve.trys.push([57,59,,60]),[4,null===(me=ce.close)||void 0===me?void 0:me.call(ce)];case 58:case 59:return ve.sent(),[3,60];case 60:return oe++,[3,56];case 61:return[3,63];case 62:return ve.sent(),[3,63];case 63:return T&&E&&(E.activeStreams=Math.max(0,E.activeStreams-1),this.notifyStreamSlotAvailable(E)),[7];case 64:return[2]}})}),I.label=1;case 1:return I.trys.push([1,3,,4]),[4,Promise.race([S,y])];case 2:return I.sent(),[2,b];case 3:throw E=I.sent(),b(),E;case 4:return[2]}})})},e}();e.Libp2pGrpcClient=_});//# sourceMappingURL=grpc.min.js.map
|