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 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,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
- 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
+ // 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
- // Or if you'd like to protect your proxy with an auth, you can provide an auth like this:
34
- await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com', auth: 'super secret auth' } });
35
- // 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
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
- "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, _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 (!(0, is_url_1.default)(url))
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: (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000,
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 ((_b = options === null || options === void 0 ? void 0 : options.proxy) === null || _b === void 0 ? void 0 : _b.auth) {
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 ((_c = options === null || options === void 0 ? void 0 : options.proxy) === null || _c === void 0 ? void 0 : _c.url)
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 (0, got_1.default)(requestURL, requestOptions)).rawBody;
40
- const imageType = (0, image_type_1.default)(responseBuffer);
41
- if (!(imageType === null || imageType === void 0 ? void 0 : imageType.mime.startsWith('image'))) {
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 dom = new jsdom_1.JSDOM(responseBuffer);
44
- const meta = dom.window.document.querySelector('meta[property="og:image"]') || dom.window.document.querySelector('meta[itemprop="image"]');
45
- if (!(meta === null || meta === void 0 ? void 0 : meta.content))
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 (meta.content[0] === '/' && meta.content[1] !== '/')
48
- meta.content = `${new url_1.URL(url).origin}${meta.content}`;
49
- if (!(0, is_url_1.default)(meta.content))
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(meta.content)) {
52
- if (/^\/\//.test(meta.content))
53
- meta.content = `http:${meta.content}`;
54
- else if (/^\//.test(meta.content))
55
- meta.content = `http:/${meta.content}`;
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
- meta.content = `http://${meta.content}`;
51
+ metaContent = `http://${metaContent}`;
58
52
  }
59
- return getReturnValue(true, meta.content);
53
+ return getReturnValue(true, metaContent);
60
54
  }
61
- else if ((options === null || options === void 0 ? void 0 : options.allowSVG) && (0, is_svg_1.default)(responseBuffer.toString()))
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.0",
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
  }
package/dist/index.d.ts DELETED
@@ -1,11 +0,0 @@
1
- export declare const verifyImageURL: (url: string, options?: {
2
- allowSVG?: boolean;
3
- proxy?: {
4
- url: string;
5
- auth?: string;
6
- };
7
- timeout?: number;
8
- }) => Promise<{
9
- isImage: boolean;
10
- imageURL: string;
11
- }>;