From d896a7828e4e265efe991c21c0c01eb1054b8a96 Mon Sep 17 00:00:00 2001 From: Justin Dhillon Date: Fri, 26 Jan 2024 21:20:31 -0800 Subject: [PATCH 1/2] add full types & add better error message --- bin.ts | 6 +++--- checkLink.ts | 2 +- index.ts | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bin.ts b/bin.ts index bfbcb81..cd153d4 100755 --- a/bin.ts +++ b/bin.ts @@ -4,13 +4,13 @@ import linkInspector from "./index"; import {dirname, basename} from 'path'; import fs from 'fs'; -const args = process.argv.slice(2); +const args: string[] = process.argv.slice(2); if (args.length === 0) { console.error("no link or path given"); } -async function writeLink(link: string, path: any) { +async function writeLink(link: string, path: string) { console.log("Broken Link:", link); if (path) { @@ -23,7 +23,7 @@ async function writeLink(link: string, path: any) { } for (const arg of args) { - let path = ''; + let path: string = ''; try {new URL(arg)} catch { diff --git a/checkLink.ts b/checkLink.ts index 9986055..9014457 100644 --- a/checkLink.ts +++ b/checkLink.ts @@ -2,7 +2,7 @@ const axios = require("axios"); // Return true if link is broken export async function checkLink(link: string): Promise { - const params = { + const params: object = { headers: { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, br", diff --git a/index.ts b/index.ts index b56340d..602cee0 100644 --- a/index.ts +++ b/index.ts @@ -2,9 +2,9 @@ import { checkLink } from "./checkLink"; import fs from 'fs'; let PROCESSES: string[][] = []; -let CURRENTPROCESS = 0; -let RUNNINGPROCESSES = 0; -const MAXPROCESSES = 100; +let CURRENTPROCESS: number = 0; +let RUNNINGPROCESSES: number = 0; +const MAXPROCESSES: number = 100; async function runProcess(callback: any) { if (MAXPROCESSES <= RUNNINGPROCESSES || PROCESSES.length === CURRENTPROCESS) return; @@ -24,7 +24,7 @@ async function runProcess(callback: any) { runProcess(callback); } -export default async function linkInspector(arg: string, callback: any, path='') { +export default async function linkInspector(arg: string, callback: any, path: string = '') { try { // If arg is a link new URL(arg); PROCESSES.push([arg, path]); @@ -33,7 +33,7 @@ export default async function linkInspector(arg: string, callback: any, path='') } catch {} try { // If arg is a path - const stats = fs.statSync(arg); + const stats: fs.Stats = fs.statSync(arg); // Skip files over 100mb if (100*1024*1024 < stats.size) return @@ -48,17 +48,17 @@ export default async function linkInspector(arg: string, callback: any, path='') } // Handle file - const content: any = fs.readFileSync(arg, 'utf8'); + const content: string = fs.readFileSync(arg, 'utf8'); // Skip binary files if (!/^[\x00-\x7F]*$/.test(content)) return; // Get all the links - const urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; + const urlRegex: RegExp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; const links: string[] = content.match(urlRegex) || []; - const directoryIndex = arg.indexOf(path); - const pathAfterDirectory = arg.substring(directoryIndex); + const directoryIndex: number = arg.indexOf(path); + const pathAfterDirectory: string = arg.substring(directoryIndex); for (const link of links) { try { // Runs link inspector on each link @@ -68,6 +68,6 @@ export default async function linkInspector(arg: string, callback: any, path='') } catch {} } } catch { - console.error("Error: Not a valid link or path") + console.error(`Error: ${arg} is not a valid link or path`); } } From 92f21266c53bf58b8e0fb3f907e93d5d05d09c3e Mon Sep 17 00:00:00 2001 From: Justin Dhillon Date: Fri, 26 Jan 2024 21:21:24 -0800 Subject: [PATCH 2/2] update dependencies --- package-lock.json | 28 ++++++++++++++-------------- package.json | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 70ef9dc..65ee51b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "link-inspector", - "version": "2.0.1", + "version": "2.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "link-inspector", - "version": "2.0.1", + "version": "2.0.2", "license": "AGPL-3.0-or-later", "dependencies": { "axios": "^1.6.5" @@ -710,9 +710,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.11.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.6.tgz", - "integrity": "sha512-+EOokTnksGVgip2PbYbr3xnR7kZigh4LbybAfBAw5BpnQ+FqBYUsvCEjYd70IXKlbohQ64mzEYmMtlWUY8q//Q==", + "version": "20.11.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.7.tgz", + "integrity": "sha512-GPmeN1C3XAyV5uybAf4cMLWT9fDWcmQhZVtMFu7OR32WjrqGG+Wnk2V1d0bmtUyE/Zy1QJ9BxyiTih9z8Oks8A==", "dev": true, "peer": true, "dependencies": { @@ -804,9 +804,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.6.5", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.5.tgz", - "integrity": "sha512-Ii012v05KEVuUoFWmMW/UQv9aRIc3ZwkWDcM+h5Il8izZCtRVpDUfwpoFf7eOtajT3QiGR4yDUx7lPqHJULgbg==", + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", + "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", "dependencies": { "follow-redirects": "^1.15.4", "form-data": "^4.0.0", @@ -1094,9 +1094,9 @@ } }, "node_modules/fastq": { - "version": "1.16.0", - "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.16.0.tgz", - "integrity": "sha512-ifCoaXsDrsdkWTtiNJX5uzHDsrck5TzfKKDcuFFTIrrc/BS076qgEIfoIy1VeZqViznfKiysPYTh/QeHtnIsYA==", + "version": "1.17.0", + "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.17.0.tgz", + "integrity": "sha512-zGygtijUMT7jnk3h26kUms3BkSDp4IfIKjmnqI2tvx6nuBfiF1UqOxbnLfzdv+apBy+53oaImsKtMw/xYbW+1w==", "dev": true, "dependencies": { "reusify": "^1.0.4" @@ -1399,9 +1399,9 @@ "dev": true }, "node_modules/lru-cache": { - "version": "10.1.0", - "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.1.0.tgz", - "integrity": "sha512-/1clY/ui8CzjKFyjdvwPWJUYKiFVXG2I2cY0ssG7h4+hwk+XOIX7ZSG9Q7TW8TW3Kp3BUSqgFWBLgL4PJ+Blag==", + "version": "10.2.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.2.0.tgz", + "integrity": "sha512-2bIM8x+VAf6JT4bKAljS1qUWgMsqZRPGJS6FSahIMPVvctcNhyVp7AJu7quxOW9jwkryBReKZY5tY5JYv2n/7Q==", "dev": true, "engines": { "node": "14 || >=16.14" diff --git a/package.json b/package.json index aa670f8..8815b97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "link-inspector", - "version": "2.0.1", + "version": "2.0.2", "author": "Justin Dhillon", "license": "AGPL-3.0-or-later",