verify-image-url 1.2.6 → 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 CHANGED
@@ -21,4 +21,12 @@ await verifyImageURL('https://example.com/example.png', { timeout: 10000 }); //
21
21
  await verifyImageURL('https://prnt.sc/zrfn0r');
22
22
  // -> { isImage: true, imageURL: 'https://image.prntscr.com/image/-ndZGuDMRfu7oDAR-fESzg.png' }
23
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
24
+
25
+ // Also works with SVGs
26
+ await verifyImageURL('https://example.com/example.svg', { allowSVG: true });
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
24
32
  ```
package/dist/index.d.ts CHANGED
@@ -1,6 +1,11 @@
1
1
  export declare const verifyImageURL: (url: string, options?: {
2
- timeout: number;
3
- }) => Promise<{
2
+ allowSVG?: boolean | undefined;
3
+ proxy?: {
4
+ url: string;
5
+ auth: string;
6
+ } | undefined;
7
+ timeout?: number | undefined;
8
+ } | undefined) => Promise<{
4
9
  isImage: boolean;
5
10
  imageURL: string;
6
11
  }>;
package/dist/index.js CHANGED
@@ -4,23 +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"));
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");
11
13
  const verifyImageURL = async (url, options) => {
12
- var _a;
14
+ var _a, _b;
13
15
  const getReturnValue = (isImage = false, imageURL = url) => ({ isImage, imageURL });
14
16
  if (!(0, is_url_1.default)(url))
15
17
  return getReturnValue();
16
18
  try {
17
- const responseBuffer = (await (0, got_1.default)(url, { headers: { 'User-Agent': 'got' }, timeout: (_a = options === null || options === void 0 ? void 0 : options.timeout) !== null && _a !== void 0 ? _a : 5000 })).rawBody;
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;
18
20
  const imageType = (0, image_type_1.default)(responseBuffer);
19
21
  if (!(imageType === null || imageType === void 0 ? void 0 : imageType.mime.startsWith('image'))) {
20
- if (responseBuffer.includes('og:image')) {
22
+ if (responseBuffer.includes('og:image') || responseBuffer.includes('itemprop="image"')) {
21
23
  const dom = new jsdom_1.JSDOM(responseBuffer);
22
- const meta = dom.window.document.querySelector('meta[property="og:image"]');
23
- if (!meta || !meta.content || !(0, is_url_1.default)(meta.content))
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))
24
30
  return getReturnValue();
25
31
  if (!/^https?:/.test(meta.content)) {
26
32
  if (/^\/\//.test(meta.content))
@@ -32,6 +38,8 @@ const verifyImageURL = async (url, options) => {
32
38
  }
33
39
  return getReturnValue(true, meta.content);
34
40
  }
41
+ else if ((options === null || options === void 0 ? void 0 : options.allowSVG) && (0, is_svg_1.default)(responseBuffer))
42
+ return getReturnValue(true);
35
43
  }
36
44
  else
37
45
  return getReturnValue(true);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "verify-image-url",
3
- "version": "1.2.6",
3
+ "version": "1.4.0",
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",
@@ -28,6 +28,7 @@
28
28
  "dependencies": {
29
29
  "got": "^11.8.2",
30
30
  "image-type": "^4.1.0",
31
+ "is-svg": "^4.3.2",
31
32
  "is-url": "^1.2.4",
32
33
  "jsdom": "^16.4.0"
33
34
  },