/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/http-connection-pool@1.0.0/dist/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var node_events=require("node:events"),http=require("node:http"),https=require("node:https"),stream=require("node:stream");function __classPrivateFieldGet(e,t,o,r){if("a"===o&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof t?e!==t||!r:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===o?r:"a"===o?r.call(e):r?r.value:t.get(e)}function __classPrivateFieldSet(e,t,o,r,n){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof t?e!==t||!n:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===r?n.call(e,o):n?n.value=o:t.set(e,o),o}function sleep(e){return new Promise((t=>setTimeout(t,e)))}function isPositiveInteger(e){return Number.isInteger(e)&&e>0}function isValidRetry(e){if(void 0===e);else if("number"==typeof e){if(!isPositiveInteger(e))throw new HcpError(`The value of "retry" expected positive number, not ${e}`,HcpErrorCode.INVALID_ARGS)}else{if("function"!=typeof e.hooks?.afterRetryHook&&void 0!==e.hooks?.afterRetryHook)throw new HcpError('The value of "afterRetryHook" expected function, not '+typeof e.hooks.afterRetryHook,HcpErrorCode.TYPE_ERROR);if("function"!=typeof e.hooks?.beforeRetryHook&&void 0!==e.hooks?.beforeRetryHook)throw new HcpError('The value of "beforeRetryHook" expected function, not '+typeof e.hooks.beforeRetryHook,HcpErrorCode.TYPE_ERROR);if("function"!=typeof e.hooks?.retryErrorHandler&&void 0!==e.hooks?.retryErrorHandler)throw new HcpError('The value of "retryErrorHandler" expected function, not '+typeof e.hooks.retryErrorHandler,HcpErrorCode.TYPE_ERROR);if(!isPositiveInteger(e.retry))throw new HcpError(`The value of "retry" expected positive number, not ${e.retry}`,HcpErrorCode.INVALID_ARGS);if(!isPositiveInteger(e.retryDelay??1))throw new HcpError(`The value of "retryDelay" expected positive number, not ${e.retryDelay}`,HcpErrorCode.INVALID_ARGS)}return!0}function createRetry(e){isValidRetry(e);const t={retry:0,retryDelay:0};return void 0===e?t:"number"==typeof e?{...t,retry:e}:{...t,...e}}function createUrl(e){if("string"==typeof e)return new URL(e);{let t=e.protocol+"://";if(t+=e.host,t+=e.port?`:${e.port}`:"",t+=e.path?"/"===e.path[0]?e.path:`/${e.path}`:"",e.urlQuery){t+="?";for(const[o,r]of Object.entries(e.urlQuery))t+=`${o}=${r}&`;t=t.substring(0,t.length-1)}return new URL(t)}}"function"==typeof SuppressedError&&SuppressedError;const HcpErrorCode={INVALID_ARGS:"ERR_INVALID_ARGS",TIMEOUT:"ERR_TIMEOUT",BAD_REQUEST:"ERR_BAD_REQUEST",BAD_RESPONSE:"ERR_BAD_RESPONSE",VALUE_ERROR:"ERR_VALUE",TYPE_ERROR:"ERR_TYPE",UNKNOWN_ERROR:"ERR_UNKNOWN"};class HcpError extends Error{constructor(e,t,o){super(e),this.code=t,this.name="HCP_ERROR",o?.config&&(this.config=o.config),o?.req&&(this.req=o.req),o?.res&&(this.res=o.res),!o?.origin||o.origin instanceof HcpError||(this.origin=o.origin),o?.retryCount&&(this.retryCount=o.retryCount)}}function isValidTimeout(e){if(void 0===e);else if("number"==typeof e){if(!isPositiveInteger(e))throw new HcpError(`The value of "timeout" expected positive number, not ${e}`,HcpErrorCode.INVALID_ARGS)}else if(!isPositiveInteger(e.timeout))throw new HcpError(`The value of "timeout" expected positive number, not ${e.timeout}`,HcpErrorCode.INVALID_ARGS);return!0}function createTimeout(e){isValidTimeout(e);const t={timeout:0};return void 0===e?t:"number"==typeof e?{...t,timeout:e}:{...t,...e}}const HTTPMethod={get:"get",GET:"GET",post:"post",POST:"POST",patch:"patch",PATCH:"PATCH",put:"put",PUT:"PUT",delete:"delete",DELETE:"DELETE",options:"options",OPTIONS:"OPTIONS",head:"head",HEAD:"HEAD"};class HttpClient{}var _HcpHttpClient_instances,_HcpHttpClient_validateStatusCode,_ConnectionPool_requestQueue,_ConnectionPool_events,_ConnectionPool_retry,_ConnectionPool_timeout,_ConnectionPool_httpAgent,_ConnectionPool_httpsAgent,_ConnectionPool_ignoreStatusCodes,_ConnectionPool_status;class HcpHttpClient extends HttpClient{constructor(e){super(),_HcpHttpClient_instances.add(this),this.config=e,this.retryCount=0,this.maxRetryCount=e.retry?.retry??0,this.retryDelay=e.retry?.retryDelay??0,this.timeout=e.timeout?.timeout??0,this.beforeRetryHook=e.retry?.hooks?.beforeRetryHook,this.afterRetryHook=e.retry?.hooks?.afterRetryHook,this.retryErrorHandler=e.retry?.hooks?.retryErrorHandler,this.url=e.url,this.method=e.method??HTTPMethod.GET,this.headers=e.requestHeaders,this.isHttps="https:"===this.url.protocol,this.body=e.requestBody,this.transport=this.isHttps?https:http,this.agent=this.isHttps?e.httpsAgent??new https.Agent({keepAlive:!0}):e.httpAgent??new http.Agent({keepAlive:!0}),this.ignoreStatusCodes=e.ignoreStatusCodes}dispatch(){return new Promise(((e,t)=>{const o=this.transport.request(this.url,{method:this.method,agent:this.agent,headers:this.headers},(r=>{if(__classPrivateFieldGet(this,_HcpHttpClient_instances,"m",_HcpHttpClient_validateStatusCode).call(this,r.statusCode)){let n="";r.on("data",(e=>{n+=e})),r.on("end",(()=>{e({statusCode:r.statusCode,statusMessage:r.statusMessage,headers:r.headers,body:n,config:this.config})})),r.on("error",(e=>{t(new HcpError(e?.message??"Response Error",e?.code??HcpErrorCode.BAD_RESPONSE,{config:this.config,req:o,origin:e,retryCount:this.retryCount}))}))}else t(new HcpError(`${r.statusMessage} with status code ${r.statusCode}`,HcpErrorCode.BAD_RESPONSE,{config:this.config,req:o,res:r,retryCount:this.retryCount}))}));this.body&&(this.body instanceof stream?(void 0===o.getHeader("content-type")&&console.warn('[HCP_WARNNING]: Request header "Content-Type" is empty.'),this.body.pipe(o)):"string"==typeof this.body?(void 0===o.getHeader("content-type")&&o.setHeader("Content-Type","text/plain"),void 0===o.getHeader("Content-Length")&&o.setHeader("Content-Length",Buffer.byteLength(this.body)),o.write(this.body)):(void 0===o.getHeader("Content-Type")&&o.setHeader("Content-Type","application/json"),void 0===o.getHeader("Content-Length")&&o.setHeader("Content-Length",Buffer.byteLength(JSON.stringify(this.body))),o.write(JSON.stringify(this.body)))),o.on("error",(e=>{t(new HcpError(e?.message??"Unknwon Error",e?.code??HcpErrorCode.UNKNOWN_ERROR,{config:this.config,req:o,origin:e,retryCount:this.retryCount}))})),this.timeout>0&&o.setTimeout(this.timeout,(()=>{this.afterTimeoutHook?.(o),o.destroy(new HcpError(`Request Timeout ${this.timeout}ms`,HcpErrorCode.TIMEOUT))})),this.body instanceof stream?this.body.on("end",(()=>{o.end()})):o.end()}))}call(){return new Promise((async(e,t)=>{let o;for(;this.retryCount<=this.maxRetryCount;)try{this.retryCount>=1&&(this.beforeRetryHook?.(this.retryCount),await sleep(this.retryDelay));e(await this.dispatch());break}catch(e){this.retryErrorHandler?.(e),o=e}finally{this.retryCount>=1&&this.afterRetryHook?.(this.retryCount),this.retryCount++}t(o)}))}}_HcpHttpClient_instances=new WeakSet,_HcpHttpClient_validateStatusCode=function(e){return void 0===e||(!!this.ignoreStatusCodes?.includes(e)||e<400)};class ExternalHttpClient extends HttpClient{constructor(e,...t){super(),this.fn=e,this.args=t}call(){return this.fn(...this.args)}}const HcpStatus={IDLE:"IDLE",BUSY:"BUSY"};class ConnectionPool{constructor(e){if(_ConnectionPool_requestQueue.set(this,void 0),_ConnectionPool_events.set(this,void 0),_ConnectionPool_retry.set(this,void 0),_ConnectionPool_timeout.set(this,void 0),_ConnectionPool_httpAgent.set(this,void 0),_ConnectionPool_httpsAgent.set(this,void 0),_ConnectionPool_ignoreStatusCodes.set(this,void 0),_ConnectionPool_status.set(this,void 0),__classPrivateFieldSet(this,_ConnectionPool_httpAgent,new http.Agent({keepAlive:!0}),"f"),__classPrivateFieldSet(this,_ConnectionPool_httpsAgent,new https.Agent({keepAlive:!0}),"f"),void 0===e)this.size=10;else if("number"==typeof e){if(!isPositiveInteger(e))throw new HcpError(`The value of "size" expected positive number, not ${e}`,HcpErrorCode.INVALID_ARGS);this.size=e}else{if(!isPositiveInteger(e.size))throw new HcpError(`The value of "size" expected positive number, not ${e.size}`,HcpErrorCode.INVALID_ARGS);this.size=e.size,isValidRetry(e.retry)&&__classPrivateFieldSet(this,_ConnectionPool_retry,e.retry,"f"),isValidTimeout(e.timeout)&&__classPrivateFieldSet(this,_ConnectionPool_timeout,e.timeout,"f"),e.httpAgent&&__classPrivateFieldSet(this,_ConnectionPool_httpAgent,e.httpAgent,"f"),e.httpsAgent&&__classPrivateFieldSet(this,_ConnectionPool_httpsAgent,e.httpsAgent,"f"),__classPrivateFieldSet(this,_ConnectionPool_ignoreStatusCodes,e.ignoreStatuCodes,"f")}__classPrivateFieldSet(this,_ConnectionPool_requestQueue,[],"f"),__classPrivateFieldSet(this,_ConnectionPool_events,new node_events.EventEmitter,"f"),this.currentSize=0,__classPrivateFieldSet(this,_ConnectionPool_status,HcpStatus.IDLE,"f"),__classPrivateFieldGet(this,_ConnectionPool_events,"f").on("next",(()=>{if(this.currentSize<this.size&&__classPrivateFieldGet(this,_ConnectionPool_requestQueue,"f").length>0){const e=__classPrivateFieldGet(this,_ConnectionPool_requestQueue,"f").shift();if(void 0!==e){const{request:t,resolve:o,reject:r}=e;this.currentSize++,__classPrivateFieldSet(this,_ConnectionPool_status,HcpStatus.BUSY,"f"),t.call().then(o).catch(r).finally((()=>{this.currentSize--,__classPrivateFieldGet(this,_ConnectionPool_events,"f").emit("next"),0===this.currentSize&&(__classPrivateFieldSet(this,_ConnectionPool_status,HcpStatus.IDLE,"f"),__classPrivateFieldGet(this,_ConnectionPool_events,"f").emit("done"))})),__classPrivateFieldGet(this,_ConnectionPool_events,"f").emit("next")}}}))}add(e){return new Promise(((t,o)=>{try{const r=new HcpHttpClient({url:createUrl(e.url),requestHeaders:e.headers,requestBody:e.body,httpAgent:__classPrivateFieldGet(this,_ConnectionPool_httpAgent,"f"),httpsAgent:__classPrivateFieldGet(this,_ConnectionPool_httpsAgent,"f"),method:e.method,retry:void 0===e.retry?createRetry(__classPrivateFieldGet(this,_ConnectionPool_retry,"f")):createRetry(e.retry),timeout:void 0===e.timeout?createTimeout(__classPrivateFieldGet(this,_ConnectionPool_timeout,"f")):createTimeout(e.timeout),ignoreStatusCodes:__classPrivateFieldGet(this,_ConnectionPool_ignoreStatusCodes,"f")});__classPrivateFieldGet(this,_ConnectionPool_requestQueue,"f").push({request:r,resolve:t,reject:o}),__classPrivateFieldGet(this,_ConnectionPool_events,"f").emit("next")}catch(e){o(new HcpError(e?.message??"Add Request Error",e?.code??HcpErrorCode.BAD_REQUEST,{origin:e}))}}))}addExternalHttpClient(e,...t){return new Promise(((o,r)=>{__classPrivateFieldGet(this,_ConnectionPool_requestQueue,"f").push({request:new ExternalHttpClient(e,...t),resolve:o,reject:r}),__classPrivateFieldGet(this,_ConnectionPool_events,"f").emit("next")}))}getPendingRequestSize(){return __classPrivateFieldGet(this,_ConnectionPool_requestQueue,"f").length}done(){return 0===__classPrivateFieldGet(this,_ConnectionPool_requestQueue,"f").length&&__classPrivateFieldGet(this,_ConnectionPool_status,"f")===HcpStatus.IDLE?new Promise((e=>{e()})):new Promise((e=>{__classPrivateFieldGet(this,_ConnectionPool_events,"f").once("done",(()=>{e()}))}))}}_ConnectionPool_requestQueue=new WeakMap,_ConnectionPool_events=new WeakMap,_ConnectionPool_retry=new WeakMap,_ConnectionPool_timeout=new WeakMap,_ConnectionPool_httpAgent=new WeakMap,_ConnectionPool_httpsAgent=new WeakMap,_ConnectionPool_ignoreStatusCodes=new WeakMap,_ConnectionPool_status=new WeakMap,exports.ConnectionPool=ConnectionPool,exports.HTTPMethod=HTTPMethod,exports.HcpError=HcpError,exports.HcpErrorCode=HcpErrorCode,exports.default=ConnectionPool;
//# sourceMappingURL=/sm/2aceff4b2a47ae5cc873ac970fd424b9b67cc5361b72f31bb9d050e7ab4e9843.map