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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # verify-image-url
2
2
 
3
- A package to check if a URL is an image URL or not and also get the valid image link from it
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
- await verifyImageURL('https://example.com/example.png', { timeout: 10000 }); // Sets timeout to 10 seconds, default is 5
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
- await verifyImageURL('https://prnt.sc/zrfn0r');
22
- // -> { isImage: true, imageURL: 'https://image.prntscr.com/image/-ndZGuDMRfu7oDAR-fESzg.png' }
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
- // Also works with SVGs
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
- // You can also have it send to a proxy if you want
30
- await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com', auth: 'super secret auth' } });
31
- // This sends a POST request to the provided proxy url with the JSON body `{ method: 'GET', url: 'url' }` that the proxy can use to send request and send back the response
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
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.verifyImageURL = void 0;
7
- const got_1 = __importDefault(require("got"));
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;
15
- const getReturnValue = (isImage = false, imageURL = url) => ({ isImage, imageURL });
16
- if (!(0, is_url_1.default)(url))
17
- return getReturnValue();
18
- try {
19
- const responseBuffer = (options === null || options === void 0 ? void 0 : options.proxy) ? (await (0, got_1.default)(options.proxy.url, { headers: { 'User-Agent': 'got', Authorization: (_a = options === null || options === void 0 ? void 0 : options.proxy) === null || _a === void 0 ? void 0 : _a.auth }, method: 'POST', timeout: 5000, json: { method: 'GET', url } })).rawBody : (await (0, got_1.default)(url, { headers: { 'User-Agent': 'got' }, timeout: (_b = options === null || options === void 0 ? void 0 : options.timeout) !== null && _b !== void 0 ? _b : 5000 })).rawBody;
20
- const imageType = (0, image_type_1.default)(responseBuffer);
21
- if (!(imageType === null || imageType === void 0 ? void 0 : imageType.mime.startsWith('image'))) {
22
- if (responseBuffer.includes('og:image') || responseBuffer.includes('itemprop="image"')) {
23
- const dom = new jsdom_1.JSDOM(responseBuffer);
24
- const meta = dom.window.document.querySelector('meta[property="og:image"]') || dom.window.document.querySelector('meta[itemprop="image"]');
25
- if (!(meta === null || meta === void 0 ? void 0 : meta.content))
26
- return getReturnValue();
27
- if (meta.content[0] === '/' && meta.content[1] !== '/')
28
- meta.content = `${new url_1.URL(url).origin}${meta.content}`;
29
- if (!(0, is_url_1.default)(meta.content))
30
- return getReturnValue();
31
- if (!/^https?:/.test(meta.content)) {
32
- if (/^\/\//.test(meta.content))
33
- meta.content = `http:${meta.content}`;
34
- else if (/^\//.test(meta.content))
35
- meta.content = `http:/${meta.content}`;
36
- else
37
- meta.content = `http://${meta.content}`;
38
- }
39
- return getReturnValue(true, meta.content);
40
- }
41
- else if ((options === null || options === void 0 ? void 0 : options.allowSVG) && (0, is_svg_1.default)(responseBuffer))
42
- return getReturnValue(true);
43
- }
44
- else
45
- return getReturnValue(true);
46
- }
47
- catch (err) {
48
- if (err.code !== 'ETIMEDOUT' && err.code !== 'ENOTFOUND' && err.message !== 'Response code 404 (Not Found)')
49
- console.error(err);
50
- }
51
- return getReturnValue();
52
- };
53
- exports.verifyImageURL = verifyImageURL;
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.0",
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
- "scripts": {
8
- "build": "tsc",
9
- "prepare": "npm run build"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "git+https://github.com/AwesomeStickz/verify-image-url.git"
14
- },
15
- "keywords": [
16
- "image",
17
- "url",
18
- "check",
19
- "verify",
20
- "validate"
21
- ],
22
- "author": "Awesome Stickz",
23
- "license": "MIT",
24
- "bugs": {
25
- "url": "https://github.com/AwesomeStickz/verify-image-url/issues"
26
- },
27
- "homepage": "https://github.com/AwesomeStickz/verify-image-url#readme",
28
- "dependencies": {
29
- "got": "^11.8.2",
30
- "image-type": "^4.1.0",
31
- "is-svg": "^4.3.2",
32
- "is-url": "^1.2.4",
33
- "jsdom": "^16.4.0"
34
- },
35
- "devDependencies": {
36
- "@types/is-url": "^1.2.28",
37
- "@types/jsdom": "^16.2.6"
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
- }>;