link-exists
Version:
A super lightweight JavaScript / TypeScript library to check whether a given url is valid and exists or not.
3 lines (2 loc) • 836 B
JavaScript
import t from"http";const o=/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g,e=/^https?:\/\//i;async function r(r,n){if("string"!=typeof r)throw new TypeError("Expected a string, got "+typeof r);if(n&&n.ignoreProtocol&&"boolean"!=typeof n.ignoreProtocol)throw new TypeError("Expected a boolean, got "+typeof n.ignoreProtocol);const p=null!==r.match(o);if("boolean"==typeof p&&!1===p)return!1;const c=function(t,o){try{let r=t;return"boolean"==typeof o&&!0===o&&(r=e.test(r)?r:`http://${r}`),new URL(r.trim())}catch(t){return null}}(r,n?.ignoreProtocol);if(!c)return!1;const{host:i,pathname:a}=c,s={method:"HEAD",host:i,path:a};return new Promise((o=>{const e=t.request(s,(()=>o(!0)));e.on("error",(()=>o(!1))),e.end()}))}export{r as linkExists};
//# sourceMappingURL=index.esm.js.map