"use strict";var e=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(i,o){function s(e){try{a(n.next(e))}catch(e){o(e)}}function u(e){try{a(n.throw(e))}catch(e){o(e)}}function a(e){var t;e.done?i(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(s,u)}a((n=n.apply(e,t||[])).next())}))},t=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.pingCmd=void 0;const r=t(require("../log")),n=require("../core/registry"),i=t(require("node-fetch")),o=require("../common/Constants"),s=require("../core/install/common/OptionValid"),u=require("../core/registry/registry"),a=require("../core/registry/OhpmRequestInit"),c=require("../common/message"),f=require("../common/ohpm.config");function d(t,s){return e(this,void 0,void 0,(function*(){s=`${t.trim().replace(/\/?$/g,"")}/${s.trim().replace(/^\//,"")}`;const e=yield(0,u.getAuth)(t),c=(new a.OhpmRequestInit).withAgent((0,n.getProxyAgent)(t)).withTimeout((0,n.getFetchTimeout)()).withRedirect("error");e&&c.withHeaders({Authorization:e});try{const e=yield(0,i.default)(s,c);if(r.default.debug(`STATUS_${e.status}`,`- GET ${s}`),e.ok){const t=yield e.text();return t||!0}return!(404!==e.status||!f.PmConfig.registryWhiteList.includes(t))||(r.default.error("",`HttpCode ${e.status}, API ping in ${t} - ${e.statusText}`),!1)}catch(e){return e.errno=o.Constants.ERR_TYPE_MAP[e.type]?o.Constants.ERR_TYPE_MAP[e.type]:e.errno,o.Constants.ERRNO_EXIT_LIST.includes(e.errno)?(r.default.error(e.errno,e.message),!1):(r.default.error("",e.message),!1)}}))}exports.pingCmd=function(t){return e(this,void 0,void 0,(function*(){const i=Date.now();(0,s.validRequestOptions)("ping",t);const o=yield function(t){return e(this,void 0,void 0,(function*(){const e=[],i=(0,n.getRegistryList)("");if(0===i.length)throw new Error(c.Messages.Ping.RegistryIsEmpty);for(const n of i){r.default.info("PING",n);const i=Date.now(),o=yield d(n,t);if(o){!0!==o&&r.default.info("PONG",o);const e=Date.now()-i;r.default.info("PONG",`${e}ms`)}else e.push(n)}return e}))}("/-/ping"),u=Date.now()-i;if(r.default.info("PONG",`Total ${u}ms`),o.length>0)throw new Error((0,c.format)(c.Messages.Ping.PingRegistriesFailed,{registries:o.join(", ")}))}))};