This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(demarches-simplifies): check if url exists via HEAD request (#252)
* fix(demarches-simplifies): check url with url-exist * fix: up * chore: up * fix: up * fix: up
- Loading branch information
1 parent
f68e363
commit 14fbc8f
Showing
5 changed files
with
78 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Source: https://stackoverflow.com/questions/35756479/does-jest-support-es6-import-export | ||
import http from "http"; | ||
import { URL } from "url"; | ||
|
||
// https://github.com/sindresorhus/is-url-superb/blob/main/index.js | ||
function isUrl(str: string): boolean { | ||
if (typeof str !== "string") { | ||
return false; | ||
} | ||
|
||
const trimmedStr = str.trim(); | ||
if (trimmedStr.includes(" ")) { | ||
return false; | ||
} | ||
|
||
try { | ||
new URL(str); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
|
||
// https://github.com/Richienb/url-exist/blob/master/index.js | ||
export function urlExists(url: string): Promise<boolean> { | ||
return new Promise((resolve) => { | ||
if (!isUrl(url)) { | ||
return resolve(false); | ||
} | ||
|
||
const { host, pathname } = new URL(url.trim()); | ||
|
||
const options = { | ||
method: "HEAD", | ||
host, | ||
path: pathname, | ||
timeout: 2000, | ||
}; | ||
|
||
const req = http.request(options, (res) => { | ||
resolve(res.statusCode < 400); | ||
}); | ||
req.on("error", () => resolve(false)); | ||
req.on("timeout", () => { | ||
req.destroy(); | ||
resolve(false); | ||
}); | ||
|
||
req.end(); | ||
}); | ||
} |