From bb1e38cd08c1945b20b497caeff69a65b8329629 Mon Sep 17 00:00:00 2001 From: Vincent Date: Mon, 15 Jul 2024 14:19:41 +0200 Subject: [PATCH] Port breach data fetching cronjob to TS --- package.json | 3 ++- src/db/tables/breaches.js | 4 ++-- .../syncBreaches.ts} | 22 +++++++++---------- src/scripts/s3.js | 6 ++++- 4 files changed, 20 insertions(+), 15 deletions(-) rename src/scripts/{syncBreaches.js => cronjobs/syncBreaches.ts} (86%) diff --git a/package.json b/package.json index 5633cbe48..a2ef69c89 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dev:cron:first-data-broker-removal-fixed": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/firstDataBrokerRemovalFixed.tsx", "dev:cron:monthly-activity": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/monthlyActivity.tsx", "dev:cron:db-delete-unverified-subscribers": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/deleteUnverifiedSubscribers.ts", + "dev:cron:db-pull-breaches": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/syncBreaches.ts", "dev:cron:onerep-limits-alert": "tsx --tsconfig tsconfig.cronjobs.json src/scripts/cronjobs/onerepStatsAlert.ts", "dev:nimbus": "node --watch-path config/nimbus.yaml src/scripts/build/nimbusTypes.js", "build": "npm run get-location-data && npm run build-glean && npm run build-nimbus && next build && npm run build-cronjobs", @@ -27,7 +28,7 @@ "cron:monthly-activity": "node dist/scripts/cronjobs/monthlyActivity.js", "cron:breach-alerts": "node src/scripts/emailBreachAlerts.js", "cron:db-delete-unverified-subscribers": "node dist/scripts/cronjobs/deleteUnverifiedSubscribers.js", - "cron:db-pull-breaches": "node src/scripts/syncBreaches.js", + "cron:db-pull-breaches": "node dist/scripts/cronjobs/syncBreaches.js", "cron:remote-settings-pull-breaches": "node scripts/updatebreaches.js", "cron:onerep-limits-alert": "node dist/scripts/cronjobs/onerepStatsAlert.js", "db:migrate": "node -r dotenv-flow/config node_modules/knex/bin/cli.js migrate:latest --knexfile src/db/knexfile.js", diff --git a/src/db/tables/breaches.js b/src/db/tables/breaches.js index 9cbf75cc0..993012001 100644 --- a/src/db/tables/breaches.js +++ b/src/db/tables/breaches.js @@ -88,8 +88,8 @@ async function upsertBreaches(hibpBreaches) { /** * Update logo path of a breach by name * - * @param {string} name - * @param {string} faviconUrl + * @param {string} name + * @param {string | null} faviconUrl */ // Not covered by tests; mostly side-effects. See test-coverage.md#mock-heavy /* c8 ignore start */ diff --git a/src/scripts/syncBreaches.js b/src/scripts/cronjobs/syncBreaches.ts similarity index 86% rename from src/scripts/syncBreaches.js rename to src/scripts/cronjobs/syncBreaches.ts index d8eafb610..02d7e4a41 100644 --- a/src/scripts/syncBreaches.js +++ b/src/scripts/cronjobs/syncBreaches.ts @@ -12,13 +12,14 @@ import { readdir } from "node:fs/promises"; import os from "node:os"; import Sentry from "@sentry/nextjs"; -import { req, formatDataClassesArray } from "../utils/hibp.js"; +import { req, formatDataClassesArray } from "../../utils/hibp.js"; import { getAllBreaches, upsertBreaches, updateBreachFaviconUrl, -} from "../db/tables/breaches.js"; -import { uploadToS3 } from "./s3.js"; +} from "../../db/tables/breaches.js"; +import { uploadToS3 } from "../s3.js"; +import type { Breach } from "../../app/functions/universal/breach.js"; const SENTRY_SLUG = "cron-sync-breaches"; @@ -32,7 +33,7 @@ const checkInId = Sentry.captureCheckIn({ status: "in_progress", }); -export async function getBreachIcons(breaches) { +export async function getBreachIcons(breaches: Breach[]) { // make logofolder if it doesn't exist const logoFolder = os.tmpdir(); console.log(`Logo folder: ${logoFolder}`); @@ -82,20 +83,19 @@ export async function getBreachIcons(breaches) { } // Get breaches and upserts to DB -const breachesResponse = await req("/breaches"); -const breaches = []; +const breachesResponse: Breach[] = await req("/breaches"); +const breaches: Breach[] = []; const seen = new Set(); for (const breach of breachesResponse) { breach.DataClasses = formatDataClassesArray(breach.DataClasses); - breach.LogoPath = /[^/]*$/.exec(breach.LogoPath)[0]; + breach.LogoPath = /[^/]*$/.exec(breach.LogoPath)![0]; breaches.push(breach); seen.add(breach.Name + breach.BreachDate); // sanity check: corrupt data structure if (!isValidBreach(breach)) throw new Error( - "Breach data structure is not valid", - JSON.stringify(breach), + "Breach data structure is not valid: " + JSON.stringify(breach), ); } @@ -128,10 +128,10 @@ setTimeout(process.exit, 1000); /** * Null check for some required field * - * @param {object} breach breach object from HIBP + * @param breach breach object from HIBP * @returns Boolean is it a valid breach */ -function isValidBreach(breach) { +function isValidBreach(breach: Breach) { return ( breach.Name !== undefined && breach.BreachDate !== undefined && diff --git a/src/scripts/s3.js b/src/scripts/s3.js index 3a40eda17..11f5fcc78 100644 --- a/src/scripts/s3.js +++ b/src/scripts/s3.js @@ -24,6 +24,10 @@ const s3 = new S3({ }, }); +/** + * @param {string} fileName + * @param {Buffer} fileStream + */ export async function uploadToS3(fileName, fileStream) { console.log("Attempt to upload to s3: ", fileName); const uploadParams = { @@ -37,7 +41,7 @@ export async function uploadToS3(fileName, fileStream) { params: uploadParams, }).done(); console.log("Successfully uploaded data to " + Bucket + "/" + fileName); - } catch (err) { + } catch (/** @type {any} */ err) { console.error(err, err.stack); } }