UNPKG

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) 979 B
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}Object.defineProperty(exports,"__esModule",{value:!0});var e=t(require("http"));const o=/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g,r=/^https?:\/\//i;exports.linkExists=async function(t,n){if("string"!=typeof t)throw new TypeError("Expected a string, got "+typeof t);if(n&&n.ignoreProtocol&&"boolean"!=typeof n.ignoreProtocol)throw new TypeError("Expected a boolean, got "+typeof n.ignoreProtocol);const i=null!==t.match(o);if("boolean"==typeof i&&!1===i)return!1;const s=function(t,e){try{let o=t;return"boolean"==typeof e&&!0===e&&(o=r.test(o)?o:`http://${o}`),new URL(o.trim())}catch(t){return null}}(t,n?.ignoreProtocol);if(!s)return!1;const{host:c,pathname:a}=s,p={method:"HEAD",host:c,path:a};return new Promise((t=>{const o=e.default.request(p,(()=>t(!0)));o.on("error",(()=>t(!1))),o.end()}))}; //# sourceMappingURL=index.js.map