diff --git a/package-lock.json b/package-lock.json index 658d2f6..f9e1497 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "body-parser": "^1.19.0", "cache-manager": "^3.4.4", "cache-manager-redis-store": "^2.0.0", + "cacheable-lookup": "^6.1.0", "command-exists": "^1.2.9", "commander": "^8.0.0", "comment-json": "^4.1.1", @@ -2395,9 +2396,9 @@ } }, "node_modules/cacheable-lookup": { - "version": "5.0.4", - "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", - "integrity": "sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==", + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-6.1.0.tgz", + "integrity": "sha512-KJ/Dmo1lDDhmW2XDPMo+9oiy/CeqosPguPCrgcVzKyZrL6pM1gU2GmPY/xo6OQPTUaA/c0kwHuywB4E6nmT9ww==", "engines": { "node": ">=10.6.0" } @@ -4421,6 +4422,14 @@ "url": "https://github.com/sindresorhus/got?sponsor=1" } }, + "node_modules/got/node_modules/cacheable-lookup": { + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", + "integrity": "sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==", + "engines": { + "node": ">=10.6.0" + } + }, "node_modules/graceful-fs": { "version": "4.2.10", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.10.tgz", @@ -12324,9 +12333,9 @@ } }, "cacheable-lookup": { - "version": "5.0.4", - "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", - "integrity": "sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==" + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-6.1.0.tgz", + "integrity": "sha512-KJ/Dmo1lDDhmW2XDPMo+9oiy/CeqosPguPCrgcVzKyZrL6pM1gU2GmPY/xo6OQPTUaA/c0kwHuywB4E6nmT9ww==" }, "cacheable-request": { "version": "7.0.2", @@ -13900,6 +13909,13 @@ "lowercase-keys": "^2.0.0", "p-cancelable": "^2.0.0", "responselike": "^2.0.0" + }, + "dependencies": { + "cacheable-lookup": { + "version": "5.0.4", + "resolved": "https://registry.npmjs.org/cacheable-lookup/-/cacheable-lookup-5.0.4.tgz", + "integrity": "sha512-2/kNscPhpcxrOigMZzbiWF7dz8ilhb/nIHU3EyZiXWXpeq/au8qJ8VhdftMkty3n7Gj6HIGalQG8oiBNB3AJgA==" + } } }, "graceful-fs": { diff --git a/package.json b/package.json index e0ba95c..24ef0e3 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "body-parser": "^1.19.0", "cache-manager": "^3.4.4", "cache-manager-redis-store": "^2.0.0", + "cacheable-lookup": "^6.1.0", "command-exists": "^1.2.9", "commander": "^8.0.0", "comment-json": "^4.1.1", diff --git a/src/index.ts b/src/index.ts index d0de954..acc8aef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,9 @@ import winston from 'winston'; import 'winston-daily-rotate-file'; import dayjs from 'dayjs'; +import http from 'http'; +import https from 'https'; +import CacheableLookup from 'cacheable-lookup'; import utc from 'dayjs/plugin/utc.js'; import advancedFormat from 'dayjs/plugin/advancedFormat'; import tz from 'dayjs/plugin/timezone'; @@ -9,7 +12,6 @@ import relTime from 'dayjs/plugin/relativeTime.js'; import sameafter from 'dayjs/plugin/isSameOrAfter.js'; import samebefore from 'dayjs/plugin/isSameOrBefore.js'; import weekOfYear from 'dayjs/plugin/weekOfYear.js'; -import {Manager} from "./Subreddit/Manager"; import {Command, Argument} from 'commander'; import { @@ -40,6 +42,17 @@ dayjs.extend(tz); dayjs.extend(advancedFormat); dayjs.extend(weekOfYear); +const cacheable = new CacheableLookup({ + // cache dns entries for 60 seconds + maxTtl: 60, + // fallback to node lookup for 10 minutes in the event of a failure for 10 minutes + fallbackDuration: 600 +}); + +// replace node native request agents, globally, so they used cached dns lookup +cacheable.install(http.globalAgent); +cacheable.install(https.globalAgent); + const commentReg = parseLinkIdentifier([COMMENT_URL_ID]); const submissionReg = parseLinkIdentifier([SUBMISSION_URL_ID]);