verify-image-url 1.3.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 +8 -0
- package/dist/index.d.ts +11 -7
- package/dist/index.js +73 -48
- package/package.json +39 -39
package/README.md
CHANGED
|
@@ -25,4 +25,12 @@ await verifyImageURL('https://prnt.sc/zrfn0r');
|
|
|
25
25
|
// Also works with SVGs
|
|
26
26
|
await verifyImageURL('https://example.com/example.svg', { allowSVG: true });
|
|
27
27
|
// -> { isImage: true, imageURL: 'https://example.com/example.svg' }
|
|
28
|
+
|
|
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:
|
|
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
|
|
28
36
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
export declare const verifyImageURL: (url: string, options?: {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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,48 +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
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
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
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
"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": "^
|
|
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
|
+
}
|