Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .eslintignore

This file was deleted.

30 changes: 0 additions & 30 deletions .eslintrc.json

This file was deleted.

89 changes: 89 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { createRequire } from 'node:module'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import mocha from 'eslint-plugin-mocha'

const require = createRequire(import.meta.url)
const { FlatCompat } = require('@eslint/eslintrc')
const compat = new FlatCompat({ baseDirectory: path.dirname(fileURLToPath(import.meta.url)) })
const withFiles = (configs, files) => configs.map((config) => ({ ...config, files }))

export default [
{
ignores: ['coverage/**', 'dist/**', 'node_modules/**', 'example/**', 'types/**'],
},
...withFiles(
compat.config({
extends: [
'plugin:import/recommended',
'plugin:promise/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
'plugin:@typescript-eslint/stylistic-type-checked',
],
plugins: ['import', 'promise'],
parserOptions: { project: './tsconfig.json' },
rules: {
'@typescript-eslint/no-explicit-any': 'warn',
'@typescript-eslint/no-unsafe-argument': 'warn',
'@typescript-eslint/no-unsafe-assignment': 'warn',
'@typescript-eslint/no-unsafe-call': 'warn',
'@typescript-eslint/no-unsafe-member-access': 'warn',
'@typescript-eslint/no-unsafe-return': 'warn',
'@typescript-eslint/prefer-nullish-coalescing': 'warn',
'@typescript-eslint/prefer-optional-chain': 'warn',
'import/no-named-as-default': 0,
'max-len': ['error', {
code: 120,
ignoreStrings: true,
ignoreTrailingComments: true,
}],
'no-param-reassign': 'off',
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.mjs', '.ts'],
},
},
},
}),
['index.ts', 'lib/**/*.ts'],
),
...withFiles(
compat.config({
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:promise/recommended',
],
plugins: ['import'],
parserOptions: { project: './tsconfig.tests.json' },
}),
['tests/**/*.ts'],
),
{
...mocha.configs.recommended,
files: ['tests/**/*.ts'],
},
{
files: ['tests/**/*.ts'],
rules: {
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-require-imports': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-unused-expressions': 0,
'func-names': 0,
'import/extensions': 0,
'import/no-unresolved': 0,
'max-len': ['error', {
code: 120,
ignoreStrings: true,
ignoreTrailingComments: true,
}],
'no-console': 0,
'no-param-reassign': 'off',
'no-unused-expressions': 0,
'prefer-arrow-callback': 0,
'promise/always-return': 0,
},
},
]
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable import/no-import-module-exports */

import setOptionsAndReturnOpenGraphResults from './lib/openGraphScraper';
import type {
ErrorResult,
Expand Down Expand Up @@ -39,7 +39,7 @@ async function run(options: OpenGraphScraperOptions): Promise<ErrorResult | Succ
response: undefined,
html: undefined,
};
// eslint-disable-next-line @typescript-eslint/no-throw-literal
// eslint-disable-next-line @typescript-eslint/only-throw-error
throw returnError;
}
const returnSuccess: SuccessResult = {
Expand Down
8 changes: 4 additions & 4 deletions lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
// find all of the open graph info in the meta tags
$('meta').each((index, meta) => {
if (!meta.attribs || (!meta.attribs.property && !meta.attribs.name)) return;
const property = meta.attribs.property || meta.attribs.name;
const content: any = meta.attribs.content || meta.attribs.value;
const property = meta.attribs.property ?? meta.attribs.name;
const content: any = meta.attribs.content ?? meta.attribs.value;

Check warning on line 27 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unexpected any. Specify a different type

Check warning on line 27 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unexpected any. Specify a different type

Check warning on line 27 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unexpected any. Specify a different type

Check warning on line 27 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unexpected any. Specify a different type
metaFields.forEach((item) => {
if (item && property.toLowerCase() === item.property.toLowerCase()) {

Check warning on line 29 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Prefer using an optional chain expression instead, as it's more concise and easier to read

Check warning on line 29 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Prefer using an optional chain expression instead, as it's more concise and easier to read

Check warning on line 29 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Prefer using an optional chain expression instead, as it's more concise and easier to read

Check warning on line 29 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Prefer using an optional chain expression instead, as it's more concise and easier to read
// check if fieldName is one of mediaMapperProperties
if (
item.fieldName === 'musicSongDisc'
Expand Down Expand Up @@ -57,20 +57,20 @@
if (!ogObject[item.fieldName]) {
ogObject[item.fieldName] = [content];
} else {
ogObject[item.fieldName]?.push(content);

Check warning on line 60 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check warning on line 60 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check warning on line 60 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check warning on line 60 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unsafe argument of type `any` assigned to a parameter of type `string`
}
} else {
ogObject[item.fieldName] = content;

Check warning on line 63 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unsafe assignment of an `any` value

Check warning on line 63 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unsafe assignment of an `any` value

Check warning on line 63 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unsafe assignment of an `any` value

Check warning on line 63 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unsafe assignment of an `any` value
}
}
});

if (options.customMetaTags) {
options.customMetaTags.forEach((item) => {
if (!ogObject.customMetaTags) ogObject.customMetaTags = {};
ogObject.customMetaTags ??= {};
if (item && property.toLowerCase() === item.property.toLowerCase()) {

Check warning on line 71 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Prefer using an optional chain expression instead, as it's more concise and easier to read

Check warning on line 71 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Prefer using an optional chain expression instead, as it's more concise and easier to read

Check warning on line 71 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Prefer using an optional chain expression instead, as it's more concise and easier to read

Check warning on line 71 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Prefer using an optional chain expression instead, as it's more concise and easier to read
if (!item.multiple) {
ogObject.customMetaTags[item.fieldName] = content;

Check warning on line 73 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unsafe assignment of an `any` value

Check warning on line 73 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unsafe assignment of an `any` value

Check warning on line 73 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unsafe assignment of an `any` value

Check warning on line 73 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unsafe assignment of an `any` value
} else if (!ogObject.customMetaTags[item.fieldName]) {
ogObject.customMetaTags[item.fieldName] = [content];
} else if (Array.isArray(ogObject.customMetaTags[item.fieldName])) {
Expand All @@ -94,13 +94,13 @@

$('script').each((index, script) => {
if (script.attribs.type && script.attribs.type === 'application/ld+json') {
if (!ogObject.jsonLD) ogObject.jsonLD = [];
ogObject.jsonLD ??= [];
let scriptText = $(script).text().trim();
if (scriptText) {
scriptText = scriptText.replace(/(\r\n|\n|\r)/gm, ''); // remove newlines
scriptText = unescapeScriptText(scriptText);
try {
ogObject.jsonLD.push(JSON.parse(scriptText));

Check warning on line 103 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unsafe argument of type `any` assigned to a parameter of type `object`

Check warning on line 103 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unsafe argument of type `any` assigned to a parameter of type `object`

Check warning on line 103 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unsafe argument of type `any` assigned to a parameter of type `object`

Check warning on line 103 in lib/extract.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unsafe argument of type `any` assigned to a parameter of type `object`
} catch (error: unknown) {
if (options.jsonLDOptions?.logOnJSONParseError) {
console.error('Error parsing JSON-LD script tag:', error);
Expand Down
2 changes: 1 addition & 1 deletion lib/isPublicAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function isBlockedIPv4(address: string) {

function parseIPv6(address: string) {
let normalized = address.toLowerCase();
const ipv4Suffix = normalized.match(/(\d{1,3}(?:\.\d{1,3}){3})$/)?.[1];
const ipv4Suffix = (/(\d{1,3}(?:\.\d{1,3}){3})$/.exec(normalized))?.[1];

if (ipv4Suffix) {
const octets = ipv4Suffix.split('.').map(Number);
Expand Down
20 changes: 10 additions & 10 deletions lib/isUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function assertString(input) {
}

// https://github.com/validatorjs/validator.js/blob/master/src/lib/util/merge.js
// eslint-disable-next-line @typescript-eslint/default-param-last

function merge(obj = { }, defaults) {
// eslint-disable-next-line no-restricted-syntax

for (const key in defaults) {
if (typeof obj[key] === 'undefined') {
obj[key] = defaults[key];
Expand Down Expand Up @@ -66,7 +66,7 @@ function isFQDN(str, options) {

if (
!options.allow_numeric_tld
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument

&& !/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(tld)
) {
return false;
Expand Down Expand Up @@ -132,8 +132,8 @@ function isIP(str, version = '') {
assertString(str);
version = String(version);
if (!version) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return isIP(str, 4) || isIP(str, 6);
return [isIP(str, 4), isIP(str, 6)].some(Boolean);
}
if (version === '4') {
return IPv4AddressRegExp.test(str);
Expand Down Expand Up @@ -211,7 +211,7 @@ export default function isURL(url, options) {
return false;
}

// eslint-disable-next-line @typescript-eslint/naming-convention

let protocol; let auth; let host; let port; let port_str; let split; let
ipv6;

Expand Down Expand Up @@ -269,13 +269,13 @@ export default function isURL(url, options) {

port_str = null;
ipv6 = null;
// eslint-disable-next-line @typescript-eslint/naming-convention

const ipv6_match = hostname.match(wrappedIpv6);
if (ipv6_match) {
host = '';
// eslint-disable-next-line prefer-destructuring

ipv6 = ipv6_match[1];
port_str = ipv6_match[2] || null;
port_str = ipv6_match[2] ?? null;
} else {
split = hostname.split(':');
host = split.shift();
Expand Down Expand Up @@ -305,7 +305,7 @@ export default function isURL(url, options) {
return false;
}

host = host || ipv6;
host = host ?? ipv6;

if (options.host_blacklist && checkHost(host, options.host_blacklist)) {
return false;
Expand Down
56 changes: 28 additions & 28 deletions lib/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
return 0;
}

const aRes = a.url.match(/\.(\w{2,5})$/);
const aRes = /\.(\w{2,5})$/.exec(a.url);
const aExt = (aRes?.[1].toLowerCase()) ?? null;
const bRes = b.url.match(/\.(\w{2,5})$/);
const bRes = /\.(\w{2,5})$/.exec(b.url);
const bExt = (bRes?.[1].toLowerCase()) ?? null;

if (aExt === 'gif' && bExt !== 'gif') {
Expand All @@ -70,9 +70,9 @@
};

// lodash zip replacement
const zip = (array: any, ...args: any) => {

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unexpected any. Specify a different type

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unexpected any. Specify a different type

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unexpected any. Specify a different type

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unexpected any. Specify a different type

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unexpected any. Specify a different type

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unexpected any. Specify a different type

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unexpected any. Specify a different type

Check warning on line 73 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unexpected any. Specify a different type
if (array === undefined) return [];
return array

Check warning on line 75 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (26)

Unsafe return of a value of type `any`

Check warning on line 75 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (22)

Unsafe return of a value of type `any`

Check warning on line 75 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (20)

Unsafe return of a value of type `any`

Check warning on line 75 in lib/media.ts

View workflow job for this annotation

GitHub Actions / buildAndTest (24)

Unsafe return of a value of type `any`
.map((value: any, idx: number) => [value, ...args.map((arr: []) => arr[idx])]);
};

Expand All @@ -95,18 +95,18 @@
?? ogObject.ogImageType
?? ogObject.ogImageAlt
) {
ogObject.ogImageSecureURL = ogObject.ogImageSecureURL ? ogObject.ogImageSecureURL : [];
ogObject.ogImageURL = ogObject.ogImageURL ? ogObject.ogImageURL : [];
ogObject.ogImageProperty = ogObject.ogImageProperty ? ogObject.ogImageProperty : [];
ogObject.ogImageSecureURL = ogObject.ogImageSecureURL ?? [];
ogObject.ogImageURL = ogObject.ogImageURL ?? [];
ogObject.ogImageProperty = ogObject.ogImageProperty ?? [];
// set ogImageProperty to ogImageSecureURL if it exists
// eslint-disable-next-line max-len
ogObject.ogImageProperty = (ogObject.ogImageSecureURL.length !== 0) ? ogObject.ogImageSecureURL : ogObject.ogImageProperty;
// fall back to ogImageURL if ogImageProperty isn't set
ogObject.ogImageProperty = (ogObject.ogImageProperty.length !== 0) ? ogObject.ogImageProperty : ogObject.ogImageURL;
ogObject.ogImageWidth = ogObject.ogImageWidth ? ogObject.ogImageWidth : [];
ogObject.ogImageHeight = ogObject.ogImageHeight ? ogObject.ogImageHeight : [];
ogObject.ogImageType = ogObject.ogImageType ? ogObject.ogImageType : [];
ogObject.ogImageAlt = ogObject.ogImageAlt ? ogObject.ogImageAlt : [];
ogObject.ogImageWidth = ogObject.ogImageWidth ?? [];
ogObject.ogImageHeight = ogObject.ogImageHeight ?? [];
ogObject.ogImageType = ogObject.ogImageType ?? [];
ogObject.ogImageAlt = ogObject.ogImageAlt ?? [];
}

// format images and limit to 10
Expand All @@ -124,10 +124,10 @@

// sets ogVideo property/width/height/type to empty array if one these exists
if (ogObject.ogVideoProperty ?? ogObject.ogVideoWidth ?? ogObject.ogVideoHeight ?? ogObject.ogVideoType) {
ogObject.ogVideoProperty = ogObject.ogVideoProperty ? ogObject.ogVideoProperty : [];
ogObject.ogVideoWidth = ogObject.ogVideoWidth ? ogObject.ogVideoWidth : [];
ogObject.ogVideoHeight = ogObject.ogVideoHeight ? ogObject.ogVideoHeight : [];
ogObject.ogVideoType = ogObject.ogVideoType ? ogObject.ogVideoType : [];
ogObject.ogVideoProperty = ogObject.ogVideoProperty ?? [];
ogObject.ogVideoWidth = ogObject.ogVideoWidth ?? [];
ogObject.ogVideoHeight = ogObject.ogVideoHeight ?? [];
ogObject.ogVideoType = ogObject.ogVideoType ?? [];
}

// format videos and limit to 10
Expand All @@ -150,12 +150,12 @@
?? ogObject.twitterImageHeight
?? ogObject.twitterImageAlt
) {
ogObject.twitterImageSrc = ogObject.twitterImageSrc ? ogObject.twitterImageSrc : [];
// eslint-disable-next-line max-len
ogObject.twitterImageProperty = ogObject.twitterImageProperty ? ogObject.twitterImageProperty : ogObject.twitterImageSrc; // deafult to twitterImageSrc
ogObject.twitterImageWidth = ogObject.twitterImageWidth ? ogObject.twitterImageWidth : [];
ogObject.twitterImageHeight = ogObject.twitterImageHeight ? ogObject.twitterImageHeight : [];
ogObject.twitterImageAlt = ogObject.twitterImageAlt ? ogObject.twitterImageAlt : [];
ogObject.twitterImageSrc = ogObject.twitterImageSrc ?? [];
ogObject.twitterImageProperty = ogObject.twitterImageProperty ?? ogObject.twitterImageSrc; // deafult to twitterImageSrc
ogObject.twitterImageWidth = ogObject.twitterImageWidth ?? [];
ogObject.twitterImageHeight = ogObject.twitterImageHeight ?? [];
ogObject.twitterImageAlt = ogObject.twitterImageAlt ?? [];
}

// format twitter images and limit to 10
Expand All @@ -176,10 +176,10 @@
?? ogObject.twitterPlayerHeight
?? ogObject.twitterPlayerStream
) {
ogObject.twitterPlayerProperty = ogObject.twitterPlayerProperty ? ogObject.twitterPlayerProperty : [];
ogObject.twitterPlayerWidth = ogObject.twitterPlayerWidth ? ogObject.twitterPlayerWidth : [];
ogObject.twitterPlayerHeight = ogObject.twitterPlayerHeight ? ogObject.twitterPlayerHeight : [];
ogObject.twitterPlayerStream = ogObject.twitterPlayerStream ? ogObject.twitterPlayerStream : [];
ogObject.twitterPlayerProperty = ogObject.twitterPlayerProperty ?? [];
ogObject.twitterPlayerWidth = ogObject.twitterPlayerWidth ?? [];
ogObject.twitterPlayerHeight = ogObject.twitterPlayerHeight ?? [];
ogObject.twitterPlayerStream = ogObject.twitterPlayerStream ?? [];
}

// format twitter player and limit to 10
Expand All @@ -195,10 +195,10 @@

// sets music property/songTrack/songDisc to empty array if one these exists
if (ogObject.musicSongProperty ?? ogObject.musicSongTrack ?? ogObject.musicSongDisc ?? ogObject.musicSongUrl) {
ogObject.musicSongUrl = ogObject.musicSongUrl ? ogObject.musicSongUrl : [];
ogObject.musicSongProperty = ogObject.musicSongProperty ? ogObject.musicSongProperty : ogObject.musicSongUrl; // deafult to musicSongUrl
ogObject.musicSongTrack = ogObject.musicSongTrack ? ogObject.musicSongTrack : [];
ogObject.musicSongDisc = ogObject.musicSongDisc ? ogObject.musicSongDisc : [];
ogObject.musicSongUrl = ogObject.musicSongUrl ?? [];
ogObject.musicSongProperty = ogObject.musicSongProperty ?? ogObject.musicSongUrl; // deafult to musicSongUrl
ogObject.musicSongTrack = ogObject.musicSongTrack ?? [];
ogObject.musicSongDisc = ogObject.musicSongDisc ?? [];
}

// format music songs and limit to 10
Expand All @@ -209,7 +209,7 @@
.sort(mediaSorterMusicSong);

// remove old values since everything will live under the main property
fields.filter((item) => (item.multiple && item.fieldName?.match('(ogImage|ogVideo|twitter|musicSong).*')))
fields.filter((item) => (item.multiple && /(ogImage|ogVideo|twitter|musicSong).*/.exec(item.fieldName)))
.forEach((item) => {
delete ogObject[item.fieldName];
});
Expand Down
4 changes: 2 additions & 2 deletions lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Agent, fetch, Headers, type RequestInit,
} from 'undici';
import { decode } from 'iconv-lite';
import { CheerioAPI, load } from 'cheerio';
import { load, type CheerioAPI } from 'cheerio';
import chardet from 'chardet';
import type { OpenGraphScraperOptions } from './types';
import { assertPublicUrl, isPublicAddress } from './isPublicAddress';
Expand Down Expand Up @@ -164,7 +164,7 @@ export default async function requestAndResultsFormatter(options: OpenGraphScrap
let response;
const requestDispatcher = createRequestDispatcher();
try {
// eslint-disable-next-line no-control-regex

const isLatin1 = /^[\u0000-\u00ff]{0,}$/;

let url = options.url ?? '';
Expand Down
Loading
Loading