verify-image-url 1.4.0 → 1.5.0

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
@@ -27,6 +27,10 @@ await verifyImageURL('https://example.com/example.svg', { allowSVG: true });
27
27
  // -> { isImage: true, imageURL: 'https://example.com/example.svg' }
28
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?url=' } });
31
+ // This sends a GET request to https://proxy.example.com?url=https://example.com/example.png
32
+
33
+ // Or if you'd like to protect your proxy with an auth, you can provide an auth like this:
30
34
  await verifyImageURL('https://example.com/example.png', { proxy: { url: 'https://proxy.example.com', auth: 'super secret auth' } });
31
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
32
36
  ```
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
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
- }>;
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
+ }>;
package/dist/index.js CHANGED
@@ -1,53 +1,73 @@
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
+ "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;
15
+ const getReturnValue = (isImage = false, imageURL = url) => ({ isImage, imageURL });
16
+ if (!(0, is_url_1.default)(url))
17
+ return getReturnValue();
18
+ try {
19
+ let requestURL;
20
+ const requestOptions = {
21
+ headers: {
22
+ 'User-Agent': 'got',
23
+ },
24
+ timeout: (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000,
25
+ };
26
+ // 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) {
28
+ requestURL = options.proxy.url;
29
+ requestOptions.method = 'POST';
30
+ requestOptions.json = { method: 'GET', url };
31
+ requestOptions.headers.Authorization = options.proxy.auth;
32
+ }
33
+ // 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)
35
+ requestURL = `${options.proxy.url}${encodeURIComponent(url)}`;
36
+ // Otherwise, send the request directly
37
+ else
38
+ 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'))) {
42
+ 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))
46
+ 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))
50
+ 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}`;
56
+ else
57
+ meta.content = `http://${meta.content}`;
58
+ }
59
+ return getReturnValue(true, meta.content);
60
+ }
61
+ else if ((options === null || options === void 0 ? void 0 : options.allowSVG) && (0, is_svg_1.default)(responseBuffer.toString()))
62
+ return getReturnValue(true);
63
+ }
64
+ else
65
+ return getReturnValue(true);
66
+ }
67
+ catch (err) {
68
+ if (err.code !== 'ETIMEDOUT' && err.code !== 'ENOTFOUND' && err.message !== 'Response code 404 (Not Found)')
69
+ console.error(err);
70
+ }
71
+ return getReturnValue();
72
+ };
73
+ exports.verifyImageURL = verifyImageURL;
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.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": "^6.1.0",
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
+ }