/**
 * Minified by jsDelivr using Terser v5.37.0.
 * Original file: /npm/hyperlink@5.0.4/lib/index.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
const AssetGraph=require("assetgraph"),asyncLib=require("async"),request=require("request"),extendWithSitemap=require("assetgraph-plugin-sitemap"),version=require("../package.json").version,relationDebugDescription=require("./relationDebugDescription"),getModifiedHref=require("./getModifiedHref"),prettyBytes=require("pretty-bytes"),net=require("net"),tls=require("tls"),defaultSkipFilters=[require("./known-culprits/linkedin")],hyperlinkUserAgent=`Hyperlink v${version} (https://www.npmjs.com/package/hyperlink)`,userContentFragmentOrigins=["https://github.com","https://www.npmjs.com"];function checkCompatibility(e,t){return void 0===t?t=AssetGraph.Asset:"string"==typeof t&&(t=AssetGraph[t]),e instanceof t||!e._type||t.prototype instanceof AssetGraph[e._type]||!(!e.isImage||t!==AssetGraph.Image)||!(!e.isImage||t!==AssetGraph.Font)}function returnFalse(){return!1}async function hyperlink({root:e,canonicalRoot:t,inputUrls:r,skipFilter:n=returnFalse,todoFilter:o=returnFalse,recursive:i=!1,internalOnly:s=!1,pretty:c=!1,followSourceMaps:a=!1,verbose:p=!1,memdebug:l=!1,concurrency:u=25}={},d){const m=new AssetGraph({root:e,canonicalRoot:t});function g(e){let t;for(const r of defaultSkipFilters){const n=r(e);n&&(t=n)}if(!t)try{t=n(e)}catch(e){console.error(e.stack),process.exit()}return(!0===t||"string"==typeof t)&&(d.push(null,{...e,skip:t,ok:!0}),!0)}function f(e){if(e.ok)return void d.push(null,e);const t=o(e);!0!==t&&"string"!=typeof t||(e.todo=t),d.push(null,e)}function h(e,t){const r=e.hostname,n="https:"===e.protocol,o=e.port?parseInt(e.port,10):n?443:80;return e=>{if(g(t))return setTimeout(e);const i=(n?tls:net).connect(o,r,(()=>{f({...t,ok:!0}),i.destroy(),e()})).on("error",(n=>{let o=n.message||"Unknown error";"ENOTFOUND"===n.code&&(o=`DNS missing: ${r}`),f({...t,ok:!1,actual:o}),e()}))}}function y(e,t=1){const r=e.url,n=e._incoming,o={operator:"external-check",name:`external-check ${r}`,at:[...new Set(n.map((e=>e.debugDescription)))].join("\n        "),expected:`200 ${r}`};return i=>{if(g(o))return setTimeout(i);request({method:1===t?"head":"get",url:e.url,strictSSL:!0,gzip:!0,headers:{"User-Agent":hyperlinkUserAgent,Accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Encoding":"gzip, deflate, sdch, br"}},((s,c)=>{if(s){const r=s.code;let n=r||"Unknown error";switch(r){case"ENOTFOUND":n=`DNS missing: ${e.hostname}`;break;case"HPE_INVALID_CONSTANT":if(1===t)return y(e,t+1)(i)}return f({...o,ok:!1,actual:n}),i()}const a=c.statusCode;if(a>=200&&a<300){const t=c.headers["content-type"];if(t&&e.type){const r=t.match(/^\s*([\w\-+.]+\/[\w-+.]+)(?:\s|;|$)/i);r&&e.expectedTypes&&(e.contentType=r[1].toLowerCase(),e._inferredType=void 0,e._tryUpgrade())}else if(!t){const r={ok:!1,name:`content-type-missing ${e.urlOrDescription}`,operator:"content-type-missing",expected:e.contentType||`A Content-Type compatible with ${e.type}`,actual:t,at:[...new Set(n.map((e=>e.debugDescription)))].join("\n        ")};g(r)||f(r)}}if(1===t&&a>=400&&a<600)return y(e,t+1)(i);if(2===t&&502===a)return void setTimeout((()=>y(e,t+1)(i)),1e3);const p=c.request._redirect.redirects;if(p.length>0){const e=[{redirectUri:r},...p].map(((e,t,r)=>(r[t+1]?e.statusCode=r[t+1].statusCode:e.statusCode=200,e))),t={operator:"external-redirect",name:`external-redirect ${r}`,at:[...new Set(n.map((e=>e.debugDescription)))].join("\n        "),expected:`302 ${r} --\x3e 200 ${e[e.length-1].redirectUri}`},o=e.map((e=>`${e.statusCode} ${e.redirectUri}`)).join(" --\x3e ");g(t)||([302,307].includes(e[0].statusCode)?e.length<3?f({...t,expected:o,actual:o,ok:!0}):f({...t,expected:`${e[0].statusCode} ${r} --\x3e 200 ${e[e.length-1].redirectUri}`,actual:o,ok:!1}):f({...t,actual:o,ok:!1}))}return 200===a?(f({...o,ok:!0,actual:o.expected}),i()):(f({...o,actual:`${a} ${r}`,ok:!1}),i())}))}}function k(e){const t=e.message||e,r=e.asset||e.relation&&e.relation.to,n={ok:!1,name:`Failed loading ${e.relation?"relation":r&&r.urlOrDescription||"asset"}`,operator:"error",actual:(r&&r.urlOrDescription+": "||"")+t.split("\nIncluding assets:").shift()};e.asset?e.asset._incoming&&(n.at=e.asset._incoming[0].debugDescription):e.relation&&(n.at=relationDebugDescription(e.relation)),e.stack&&(n.actual.stack+=e.stack),f(n)}extendWithSitemap(m),m.teepee.headers["User-Agent"]=hyperlinkUserAgent,m.teepee.timeout=3e4,p&&(m.on("addRelation",(e=>{console.error("addRelation",e.toString())})),m.on("addAsset",(e=>{console.error("addAsset",e.toString())}))),m.on("warn",k),m.on("error",k),l&&setInterval((()=>{const e=process.memoryUsage();for(const t of Object.keys(e))console.error(t,prettyBytes(e[t]))}),5e3),d.begin(),d.push({name:"Crawling internal assets"});const $=Object.keys(AssetGraph).filter((e=>AssetGraph[e]&&AssetGraph[e].prototype&&AssetGraph[e].prototype.findOutgoingRelationsInParseTree===AssetGraph.Asset.prototype.findOutgoingRelationsInParseTree)),D=m.addAssets(r),x=[...D],A=new Set,b=new Set;async function w(e){if(!b.has(e.urlOrDescription)){A.add(e),b.add(e.urlOrDescription);const t={operator:"load",name:`load ${e.urlOrDescription}`,expected:`200 ${e.urlOrDescription}`};if(e._incoming&&e._incoming[0].debugDescription?t.at=e._incoming[0].debugDescription:t.at=`${e.urlOrDescription} (input URL)`,g(t))return;try{await e.load(),f({...t,ok:!0})}catch(r){const n={...t,ok:!1,actual:r.message};if(!c||x.includes(e)||"file:"!==e.protocol||".html"===e.extension)return void f(n);{const r=e.url,o=e.url.replace(/(\?|#|$)/,".html$1");let i=m.findAssets({url:o})[0];i||(i=m.addAsset({url:o}));try{await i.load(),e.isRedirect=!0,e.fileRedirectTargetUrl=o}catch(e){return void f(n)}f({...t,ok:!0}),e.url=r}}if(e.isRedirect&&(e._redirectTarget=e.outgoingRelations.find((e=>/Redirect$/.test(e.type))).to),"Html"===e.type&&(x.includes(e)||(e.stopProcessing=!e.isRedirect&&(e.crossedOrigins||!i)),e.ids=new Set,e.names=new Set,e.isLoaded&&e.parseTree)){for(const t of Array.from(e.parseTree.querySelectorAll("[id]")))e.ids.add(t.getAttribute("id"));for(const t of Array.from(e.parseTree.querySelectorAll("[name]")))e.names.add(t.getAttribute("name"))}if(e.stopProcessing)return void e.unload();for(const t of e.externalRelations){if(!["http:","https:","file:"].includes(t.to.protocol))continue;t.targetType&&(t.to.expectedTypes=t.to.expectedTypes||new Set,t.to.expectedTypes.add(t.targetType));const r=t.fragment;if(r)if("#"===r){const r={name:`fragment-check ${t.from.urlOrDescription} --\x3e ${t.href}`,operator:"fragment-check",expected:"Fragment identifiers in links to different documents should not be empty",at:relationDebugDescription(t)};g(r)||t.to!==e&&f({...r,ok:!1})}else(t.to.incomingFragments=t.to.incomingFragments||[]).push({fragment:r,relationDebugDescription:relationDebugDescription(t),href:t.href,fromUrlOrDescription:t.from.urlOrDescription,fromUrl:t.from.url});if(t.to._incoming||(t.to._incoming=t.to._incoming||[]).push({type:t.type,debugDescription:relationDebugDescription(t)}),"https:"===t.from.nonInlineAncestor.protocol&&"http:"===t.to.protocol&&!["HtmlAnchor","SvgAnchor"].includes(t.type)){const e=t.href||t.to.url,r={name:`mixed-content ${t.from.urlOrDescription} --\x3e ${e}`,operator:"mixed-content",at:relationDebugDescription(t),expected:`${t.from.urlOrDescription} --\x3e ${e.replace(/\bhttps?:/g,"https:")}`,actual:`${t.from.urlOrDescription} --\x3e ${e}`};g(r)||(r.actual!==r.expected?f({...r,ok:!1}):f({...r,ok:!0}))}let n;["HtmlPreconnectLink","HtmlDnsPrefetchLink"].includes(t.type)?(n=!1,t.to["check"+t.type]=!0):["HtmlAnchor","SvgAnchor","HtmlIFrame"].includes(t.type)?t.crossorigin||t.from.crossedOrigins||!i?t.from!==t.to&&(i||"file:"!==t.from.protocol||"file:"!==t.to.protocol?s||(t.fragment&&"#"!==t.fragment?(n=!0,t.to.stopProcessing=!0):t.to.check=!0):(n=!0,t.to.stopProcessing=!0)):n=!0:/^(?:JavaScript|Css)Source(?:Mapping)Url$/.test(t.type)?a?n=!0:t.to.check=!0:["SourceMapFile","SourceMapSource"].includes(t.type)?a&&(t.to.check=!0):n="JavaScriptFetch"!==t.type&&(!s||!t.crossorigin),n&&(t.to.crossedOrigins=t.crossorigin||t.from.crossedOrigins,$.includes(t.to.type)?"file:"===t.from.nonInlineAncestor.protocol&&"file:"===t.to.protocol?(t.to.stopProcessing=!i,D.push(t.to)):t.to.check=!0:D.push(t.to))}p&&f({ok:!0,name:`unloading ${e.urlOrDescription}`}),e.unload()}}await new Promise((e=>{let t=0;!function r(){for(;D.length>0&&t<u;)t+=1,w(D.shift()).then((()=>{t-=1,r()}));0===t&&e()}()}));for(const e of m.findAssets({isRedirect:!0}))if(e.incomingFragments){const t=new Set([e]);for(const r of t)if(r._redirectTarget)t.add(r._redirectTarget);else{if("number"==typeof e.statusCode||!/\/(?:\?|$)/.test(e.url))for(const{fragment:t,href:r,fromUrl:n,relationDebugDescription:o,fromUrlOrDescription:i}of e.incomingFragments){const s=e._redirectTarget,c=getModifiedHref(r,n,s.url,m.root).replace("/index.html","/")+t,a={operator:"fragment-redirect",name:`fragment-redirect ${i} --\x3e ${e.urlOrDescription}${t} --\x3e ${s.urlOrDescription}`,expected:c,actual:r,at:o};g(a)||f({...a,ok:!1})}(r.incomingFragments=r.incomingFragments||[]).push(...e.incomingFragments)}delete e.incomingFragments}for(const e of m.findAssets({type:"Html",incomingFragments:{$exists:!0},ids:{$exists:!0}}))for(const{fragment:t,relationDebugDescription:r,href:n,fromUrlOrDescription:o}of e.incomingFragments){const i=t.substr(1),s={operator:"fragment-check",name:`fragment-check ${o} --\x3e ${n}`,expected:`id="${i}"`,at:r};g(s)||(e.ids.has(i)?f({...s,ok:!0,actual:s.expected}):userContentFragmentOrigins.includes(e.origin)&&e.ids.has(`user-content-${i}`)?f({...s,ok:!0,actual:`id="user-content-${i}"`}):e.names.has(i)?f({...s,ok:!0,actual:`name="${i}"`}):f({...s,ok:!1,actual:null}))}if(!s){const e=m.findAssets({check:!0}).filter((e=>!A.has(e)));d.push({name:`Crawling ${e.length} outgoing urls`}),await new Promise(((t,r)=>asyncLib.parallelLimit(e.map((e=>y(e))),20,(e=>{e?r(e):t()}))))}for(const e of m.findAssets({expectedTypes:{$exists:!0}})){const t=[...e.expectedTypes].filter((t=>!checkCompatibility(e,t)));if(t.length>0){const r=e.contentType||`A Content-Type compatible with ${e.type}`,n={ok:!1,operator:"content-type-mismatch",name:`content-type-mismatch ${e.urlOrDescription}`,expected:r,actual:`Asset is used as both ${[...t,e.type].sort().join(" and ")}`,at:[...new Set(e._incoming.map((e=>e.debugDescription)))].join("\n        ")};g(n)||f(n)}}const O=m.findAssets({checkHtmlPreconnectLink:!0});d.push({name:`Connecting to ${O.length} hosts (checking <link rel="preconnect" href="...">`}),await new Promise(((e,t)=>asyncLib.parallelLimit(O.map((e=>h(e,{operator:"preconnect-check",name:`preconnect-check ${e.url}`,at:[...new Set(e._incoming.map((e=>e.debugDescription)))].join("\n        "),expected:`connection accepted ${e.url}`}))),20,(r=>{r?t(r):e()}))));const S=m.findAssets({checkHtmlDnsPrefetchLink:!0});return d.push({name:`Looking up ${S.length} host names (checking <link rel="dns-prefetch" href="...">`}),await new Promise(((e,t)=>asyncLib.parallelLimit(S.map((e=>h(e,{operator:"dns-prefetch-check",name:`dns-prefetch-check ${e.hostname}`,at:[...new Set(e._incoming.map((e=>e.debugDescription)))].join("\n        "),expected:`DNS exists ${e.hostname}`}))),20,(r=>{r?t(r):e()})))),m}module.exports=hyperlink;
//# sourceMappingURL=/sm/c5566c2032a21b9ec8cf4dafd52a47087143d2257a0e079ff3e7b8d99a3462f1.map