verify-image-url 1.4.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 +14 -9
- package/dist/index.js +66 -53
- package/package.json +39 -39
- 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,17 +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
|
-
await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com'
|
|
31
|
-
// This sends a
|
|
30
|
+
// Route through a proxy
|
|
31
|
+
await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com?url=' } });
|
|
32
|
+
// This sends a GET request to https://proxy.example.com?url=https://example.com/example.png
|
|
33
|
+
|
|
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'
|
|
32
37
|
```
|
package/dist/index.js
CHANGED
|
@@ -1,53 +1,66 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
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) => {
|
|
8
|
+
const getReturnValue = (isImage = false, imageURL = url) => ({ isImage, imageURL });
|
|
9
|
+
if (!isURL(url))
|
|
10
|
+
return getReturnValue();
|
|
11
|
+
try {
|
|
12
|
+
let requestURL;
|
|
13
|
+
const requestOptions = {
|
|
14
|
+
headers: {
|
|
15
|
+
'User-Agent': 'got',
|
|
16
|
+
},
|
|
17
|
+
timeout: options?.timeout ?? 5000,
|
|
18
|
+
};
|
|
19
|
+
// If proxy auth is provided, send a POST request to the proxy url with the provided auth
|
|
20
|
+
if (options?.proxy?.auth) {
|
|
21
|
+
requestURL = options.proxy.url;
|
|
22
|
+
requestOptions.method = 'POST';
|
|
23
|
+
requestOptions.json = { method: 'GET', url };
|
|
24
|
+
requestOptions.headers.Authorization = options.proxy.auth;
|
|
25
|
+
}
|
|
26
|
+
// Otherwise, if a proxy url is provided, use it
|
|
27
|
+
else if (options?.proxy?.url)
|
|
28
|
+
requestURL = `${options.proxy.url}${encodeURIComponent(url)}`;
|
|
29
|
+
// Otherwise, send the request directly
|
|
30
|
+
else
|
|
31
|
+
requestURL = url;
|
|
32
|
+
const responseBuffer = (await got.default(requestURL, requestOptions)).rawBody;
|
|
33
|
+
const imageType = getImageType(responseBuffer);
|
|
34
|
+
if (!imageType?.mime.startsWith('image')) {
|
|
35
|
+
if (responseBuffer.includes('og:image') || responseBuffer.includes('itemprop="image"')) {
|
|
36
|
+
const $ = cheerio.load(responseBuffer);
|
|
37
|
+
const meta = $('meta[property="og:image"]') || $('meta[itemprop="image"]');
|
|
38
|
+
let metaContent = meta?.attr('content');
|
|
39
|
+
if (!metaContent)
|
|
40
|
+
return getReturnValue();
|
|
41
|
+
if (metaContent[0] === '/' && metaContent[1] !== '/')
|
|
42
|
+
metaContent = `${new URL(url).origin}${metaContent}`;
|
|
43
|
+
if (!isURL(metaContent))
|
|
44
|
+
return getReturnValue();
|
|
45
|
+
if (!/^https?:/.test(metaContent)) {
|
|
46
|
+
if (/^\/\//.test(metaContent))
|
|
47
|
+
metaContent = `http:${metaContent}`;
|
|
48
|
+
else if (/^\//.test(metaContent))
|
|
49
|
+
metaContent = `http:/${metaContent}`;
|
|
50
|
+
else
|
|
51
|
+
metaContent = `http://${metaContent}`;
|
|
52
|
+
}
|
|
53
|
+
return getReturnValue(true, metaContent);
|
|
54
|
+
}
|
|
55
|
+
else if (options?.allowSVG && isSvg(responseBuffer.toString()))
|
|
56
|
+
return getReturnValue(true);
|
|
57
|
+
}
|
|
58
|
+
else
|
|
59
|
+
return getReturnValue(true);
|
|
60
|
+
}
|
|
61
|
+
catch (err) {
|
|
62
|
+
if (err.code !== 'ETIMEDOUT' && err.code !== 'ENOTFOUND' && err.message !== 'Response code 404 (Not Found)')
|
|
63
|
+
console.error(err);
|
|
64
|
+
}
|
|
65
|
+
return getReturnValue();
|
|
66
|
+
};
|
package/package.json
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "verify-image-url",
|
|
3
|
-
"version": "1.
|
|
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
|
-
"main": "dist/index.js",
|
|
6
|
-
"types": "dist/index.d.ts",
|
|
7
|
-
"
|
|
8
|
-
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
"@types/
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "verify-image-url",
|
|
3
|
+
"version": "1.5.1",
|
|
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
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"prepare": "npm run build"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/AwesomeStickz/verify-image-url.git"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"image",
|
|
18
|
+
"url",
|
|
19
|
+
"check",
|
|
20
|
+
"verify",
|
|
21
|
+
"validate"
|
|
22
|
+
],
|
|
23
|
+
"author": "Awesome Stickz",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"bugs": {
|
|
26
|
+
"url": "https://github.com/AwesomeStickz/verify-image-url/issues"
|
|
27
|
+
},
|
|
28
|
+
"homepage": "https://github.com/AwesomeStickz/verify-image-url#readme",
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"cheerio": "^1.2.0",
|
|
31
|
+
"got": "^11.8.2",
|
|
32
|
+
"image-type": "^4.1.0",
|
|
33
|
+
"is-svg": "^6.1.0",
|
|
34
|
+
"is-url": "^1.2.4"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/is-url": "^1.2.28"
|
|
38
|
+
}
|
|
39
|
+
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export declare const verifyImageURL: (url: string, options?: {
|
|
2
|
-
allowSVG?: boolean | undefined;
|
|
3
|
-
proxy?: {
|
|
4
|
-
url: string;
|
|
5
|
-
auth: string;
|
|
6
|
-
} | undefined;
|
|
7
|
-
timeout?: number | undefined;
|
|
8
|
-
} | undefined) => Promise<{
|
|
9
|
-
isImage: boolean;
|
|
10
|
-
imageURL: string;
|
|
11
|
-
}>;
|