verify-image-url 1.5.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -10
- package/dist/index.js +29 -36
- package/package.json +5 -5
- package/dist/index.d.ts +0 -11
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# verify-image-url
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Verify if a URL is an image or extract the image URL from Open Graph meta tags.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -16,21 +16,22 @@ const { verifyImageURL } = require('verify-image-url');
|
|
|
16
16
|
await verifyImageURL('https://example.com/example.png');
|
|
17
17
|
// -> { isImage: true, imageURL: 'https://example.com/example.png' }
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
// HTML page with og:image
|
|
20
|
+
await verifyImageURL('https://giveaway.boats');
|
|
21
|
+
// -> { isImage: true, imageURL: 'https://giveaway.boats/assets/logo.png' }
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
// This link is from og:image meta tag since prnt.sc is a site where you can upload screenshots and get the web page url from it which isn't your image link but it's in the meta tag
|
|
23
|
+
// Supports custom timeouts (default: 5s)
|
|
24
|
+
await verifyImageURL('https://example.com/example.png', { timeout: 10000 });
|
|
24
25
|
|
|
25
|
-
//
|
|
26
|
+
// Works with SVGs
|
|
26
27
|
await verifyImageURL('https://example.com/example.svg', { allowSVG: true });
|
|
27
28
|
// -> { isImage: true, imageURL: 'https://example.com/example.svg' }
|
|
28
29
|
|
|
29
|
-
//
|
|
30
|
+
// Route through a proxy
|
|
30
31
|
await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com?url=' } });
|
|
31
32
|
// This sends a GET request to https://proxy.example.com?url=https://example.com/example.png
|
|
32
33
|
|
|
33
|
-
//
|
|
34
|
-
await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com', auth: '
|
|
35
|
-
// This sends a POST request to
|
|
34
|
+
// Proxy with authentication
|
|
35
|
+
await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com', auth: 'secret' } });
|
|
36
|
+
// This sends a POST request to https://proxy.example.com with the JSON body `{ method: 'GET', url: 'https://example.com/example.png' }` along with Authorization header set to 'secret'
|
|
36
37
|
```
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const image_type_1 = __importDefault(require("image-type"));
|
|
9
|
-
const is_svg_1 = __importDefault(require("is-svg"));
|
|
10
|
-
const is_url_1 = __importDefault(require("is-url"));
|
|
11
|
-
const jsdom_1 = require("jsdom");
|
|
12
|
-
const url_1 = require("url");
|
|
13
|
-
const verifyImageURL = async (url, options) => {
|
|
14
|
-
var _a, _b, _c;
|
|
1
|
+
import * as cheerio from 'cheerio';
|
|
2
|
+
import got from 'got';
|
|
3
|
+
import getImageType from 'image-type';
|
|
4
|
+
import isSvg from 'is-svg';
|
|
5
|
+
import isURL from 'is-url';
|
|
6
|
+
import { URL } from 'url';
|
|
7
|
+
export const verifyImageURL = async (url, options) => {
|
|
15
8
|
const getReturnValue = (isImage = false, imageURL = url) => ({ isImage, imageURL });
|
|
16
|
-
if (!(
|
|
9
|
+
if (!isURL(url))
|
|
17
10
|
return getReturnValue();
|
|
18
11
|
try {
|
|
19
12
|
let requestURL;
|
|
@@ -21,44 +14,45 @@ const verifyImageURL = async (url, options) => {
|
|
|
21
14
|
headers: {
|
|
22
15
|
'User-Agent': 'got',
|
|
23
16
|
},
|
|
24
|
-
timeout:
|
|
17
|
+
timeout: options?.timeout ?? 5000,
|
|
25
18
|
};
|
|
26
19
|
// If proxy auth is provided, send a POST request to the proxy url with the provided auth
|
|
27
|
-
if (
|
|
20
|
+
if (options?.proxy?.auth) {
|
|
28
21
|
requestURL = options.proxy.url;
|
|
29
22
|
requestOptions.method = 'POST';
|
|
30
23
|
requestOptions.json = { method: 'GET', url };
|
|
31
24
|
requestOptions.headers.Authorization = options.proxy.auth;
|
|
32
25
|
}
|
|
33
26
|
// Otherwise, if a proxy url is provided, use it
|
|
34
|
-
else if (
|
|
27
|
+
else if (options?.proxy?.url)
|
|
35
28
|
requestURL = `${options.proxy.url}${encodeURIComponent(url)}`;
|
|
36
29
|
// Otherwise, send the request directly
|
|
37
30
|
else
|
|
38
31
|
requestURL = url;
|
|
39
|
-
const responseBuffer = (await
|
|
40
|
-
const imageType = (
|
|
41
|
-
if (!
|
|
32
|
+
const responseBuffer = (await got.default(requestURL, requestOptions)).rawBody;
|
|
33
|
+
const imageType = getImageType(responseBuffer);
|
|
34
|
+
if (!imageType?.mime.startsWith('image')) {
|
|
42
35
|
if (responseBuffer.includes('og:image') || responseBuffer.includes('itemprop="image"')) {
|
|
43
|
-
const
|
|
44
|
-
const meta =
|
|
45
|
-
|
|
36
|
+
const $ = cheerio.load(responseBuffer);
|
|
37
|
+
const meta = $('meta[property="og:image"]') || $('meta[itemprop="image"]');
|
|
38
|
+
let metaContent = meta?.attr('content');
|
|
39
|
+
if (!metaContent)
|
|
46
40
|
return getReturnValue();
|
|
47
|
-
if (
|
|
48
|
-
|
|
49
|
-
if (!(
|
|
41
|
+
if (metaContent[0] === '/' && metaContent[1] !== '/')
|
|
42
|
+
metaContent = `${new URL(url).origin}${metaContent}`;
|
|
43
|
+
if (!isURL(metaContent))
|
|
50
44
|
return getReturnValue();
|
|
51
|
-
if (!/^https?:/.test(
|
|
52
|
-
if (/^\/\//.test(
|
|
53
|
-
|
|
54
|
-
else if (/^\//.test(
|
|
55
|
-
|
|
45
|
+
if (!/^https?:/.test(metaContent)) {
|
|
46
|
+
if (/^\/\//.test(metaContent))
|
|
47
|
+
metaContent = `http:${metaContent}`;
|
|
48
|
+
else if (/^\//.test(metaContent))
|
|
49
|
+
metaContent = `http:/${metaContent}`;
|
|
56
50
|
else
|
|
57
|
-
|
|
51
|
+
metaContent = `http://${metaContent}`;
|
|
58
52
|
}
|
|
59
|
-
return getReturnValue(true,
|
|
53
|
+
return getReturnValue(true, metaContent);
|
|
60
54
|
}
|
|
61
|
-
else if (
|
|
55
|
+
else if (options?.allowSVG && isSvg(responseBuffer.toString()))
|
|
62
56
|
return getReturnValue(true);
|
|
63
57
|
}
|
|
64
58
|
else
|
|
@@ -70,4 +64,3 @@ const verifyImageURL = async (url, options) => {
|
|
|
70
64
|
}
|
|
71
65
|
return getReturnValue();
|
|
72
66
|
};
|
|
73
|
-
exports.verifyImageURL = verifyImageURL;
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "verify-image-url",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "A package to check if a URL is an image URL or not and also get the valid image link from it",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"scripts": {
|
|
8
9
|
"build": "tsc",
|
|
9
10
|
"prepare": "npm run build"
|
|
@@ -26,14 +27,13 @@
|
|
|
26
27
|
},
|
|
27
28
|
"homepage": "https://github.com/AwesomeStickz/verify-image-url#readme",
|
|
28
29
|
"dependencies": {
|
|
30
|
+
"cheerio": "^1.2.0",
|
|
29
31
|
"got": "^11.8.2",
|
|
30
32
|
"image-type": "^4.1.0",
|
|
31
33
|
"is-svg": "^6.1.0",
|
|
32
|
-
"is-url": "^1.2.4"
|
|
33
|
-
"jsdom": "^16.4.0"
|
|
34
|
+
"is-url": "^1.2.4"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
|
-
"@types/is-url": "^1.2.28"
|
|
37
|
-
"@types/jsdom": "^16.2.6"
|
|
37
|
+
"@types/is-url": "^1.2.28"
|
|
38
38
|
}
|
|
39
39
|
}
|