verify-image-url 1.3.0 → 1.4.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 +4 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +13 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,4 +25,8 @@ 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', 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
|
|
28
32
|
```
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export declare const verifyImageURL: (url: string, options?: {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
allowSVG?: boolean | undefined;
|
|
3
|
+
proxy?: {
|
|
4
|
+
url: string;
|
|
5
|
+
auth: string;
|
|
6
|
+
} | undefined;
|
|
7
|
+
timeout?: number | undefined;
|
|
8
|
+
} | undefined) => Promise<{
|
|
5
9
|
isImage: boolean;
|
|
6
10
|
imageURL: string;
|
|
7
11
|
}>;
|
package/dist/index.js
CHANGED
|
@@ -4,24 +4,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.verifyImageURL = void 0;
|
|
7
|
-
const jsdom_1 = require("jsdom");
|
|
8
|
-
const is_url_1 = __importDefault(require("is-url"));
|
|
9
|
-
const image_type_1 = __importDefault(require("image-type"));
|
|
10
7
|
const got_1 = __importDefault(require("got"));
|
|
8
|
+
const image_type_1 = __importDefault(require("image-type"));
|
|
11
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");
|
|
12
13
|
const verifyImageURL = async (url, options) => {
|
|
13
|
-
var _a;
|
|
14
|
+
var _a, _b;
|
|
14
15
|
const getReturnValue = (isImage = false, imageURL = url) => ({ isImage, imageURL });
|
|
15
16
|
if (!(0, is_url_1.default)(url))
|
|
16
17
|
return getReturnValue();
|
|
17
18
|
try {
|
|
18
|
-
const responseBuffer = (await (0, got_1.default)(url, { headers: { 'User-Agent': 'got' }, timeout: (
|
|
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;
|
|
19
20
|
const imageType = (0, image_type_1.default)(responseBuffer);
|
|
20
21
|
if (!(imageType === null || imageType === void 0 ? void 0 : imageType.mime.startsWith('image'))) {
|
|
21
|
-
if (responseBuffer.includes('og:image')) {
|
|
22
|
+
if (responseBuffer.includes('og:image') || responseBuffer.includes('itemprop="image"')) {
|
|
22
23
|
const dom = new jsdom_1.JSDOM(responseBuffer);
|
|
23
|
-
const meta = dom.window.document.querySelector('meta[property="og:image"]');
|
|
24
|
-
if (!meta ||
|
|
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))
|
|
25
30
|
return getReturnValue();
|
|
26
31
|
if (!/^https?:/.test(meta.content)) {
|
|
27
32
|
if (/^\/\//.test(meta.content))
|
package/package.json
CHANGED