chore(tools): add biomejs, apply lint+formatting fixes

This commit is contained in:
Marc 2024-03-14 00:49:34 -04:00
parent 44a9c52c78
commit c63fa19035
Signed by: marc
GPG key ID: 048E042F22B5DC79
4 changed files with 174 additions and 54 deletions

22
biome.json Normal file
View file

@ -0,0 +1,22 @@
{
"$schema": "https://biomejs.dev/schemas/1.6.1/schema.json",
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": true,
"lineWidth": 120
},
"javascript": {
"formatter": {
"semicolons": "asNeeded",
"quoteStyle": "single"
}
}
}

View file

@ -1,18 +1,23 @@
{ {
"name": "healthcheck", "name": "healthcheck",
"version": "noversion", "version": "noversion",
"main": "index.js", "main": "index.js",
"repository": "git@github.com:mcataford/healthcheck.git", "repository": "git@github.com:mcataford/healthcheck.git",
"author": "Marc Cataford <mcat@riseup.net>", "author": "Marc Cataford <mcat@riseup.net>",
"private": true, "private": true,
"packageManager": "yarn@4.1.1", "packageManager": "yarn@4.1.1",
"devDependencies": { "scripts": {
"@types/node": "^20.11.27", "lint": "biome lint .; biome format .",
"typescript": "^5.4.2" "lint:fix": "biome lint . --apply; biome format . --write"
}, },
"dependencies": { "devDependencies": {
"@netlify/blobs": "^7.0.1", "@biomejs/biome": "^1.6.1",
"@netlify/functions": "^2.6.0", "@types/node": "^20.11.27",
"axios": "^1.6.7" "typescript": "^5.4.2"
} },
"dependencies": {
"@netlify/blobs": "^7.0.1",
"@netlify/functions": "^2.6.0",
"axios": "^1.6.7"
}
} }

View file

@ -1,22 +1,20 @@
import { readFile } from 'node:fs/promises'
import axios from 'axios' import axios from 'axios'
import { getStore } from '@netlify/blobs' import { getStore } from '@netlify/blobs'
import type { Context } from '@netlify/functions' import type { Context } from '@netlify/functions'
interface Endpoint { interface Endpoint {
name: string name: string
url: string url: string
} }
interface Configuration { interface Configuration {
endpoints: Endpoint[] endpoints: Endpoint[]
webhook_url: string webhook_url: string
} }
interface EndpointReport extends Endpoint { interface EndpointReport extends Endpoint {
status: number status: number
healthy: boolean healthy: boolean
} }
/* /*
@ -30,21 +28,21 @@ interface EndpointReport extends Endpoint {
* extra status information. * extra status information.
*/ */
async function pingEndpoint(endpoint: Endpoint): Promise<EndpointReport> { async function pingEndpoint(endpoint: Endpoint): Promise<EndpointReport> {
try { try {
const response = await axios.get(endpoint.url) const response = await axios.get(endpoint.url)
return { return {
...endpoint, ...endpoint,
status: response.status, status: response.status,
healthy: true healthy: true,
} }
} catch(e) { } catch (e) {
return { return {
...endpoint, ...endpoint,
healthy: false, healthy: false,
status: e.response.status status: e.response.status,
} }
} }
} }
/* /*
@ -53,14 +51,13 @@ async function pingEndpoint(endpoint: Endpoint): Promise<EndpointReport> {
* The report is assembled line-by-line and joined together as one. * The report is assembled line-by-line and joined together as one.
*/ */
function formatReport(endpointReports: EndpointReport[]): string { function formatReport(endpointReports: EndpointReport[]): string {
const endpointStatuses = endpointReports.map((report: EndpointReport): string => { const endpointStatuses = endpointReports.map((report: EndpointReport): string => {
if (report.healthy) if (report.healthy) return `${report.name} is healthy (${report.status})`
return `${report.name} is healthy (${report.status})`
else
return `🔥 ${report.name} did not respond normally (${report.status})`
})
return endpointStatuses.join('\n') return `🔥 ${report.name} did not respond normally (${report.status})`
})
return endpointStatuses.join('\n')
} }
/* /*
@ -71,15 +68,19 @@ function formatReport(endpointReports: EndpointReport[]): string {
* the available environment variables separately. * the available environment variables separately.
*/ */
export default async (request: Request, context: Context) => { export default async (request: Request, context: Context) => {
//const conf: Configuration = require("../config.json") //const conf: Configuration = require("../config.json")
const configurationStore = getStore('functions') const configurationStore = getStore('functions')
const conf: Configuration = await configurationStore.get('config', { type: 'json' }) const conf: Configuration = await configurationStore.get('config', {
type: 'json',
})
const pings = await Promise.all(conf.endpoints.map(pingEndpoint)) const pings = await Promise.all(conf.endpoints.map(pingEndpoint))
const report = formatReport(pings) const report = formatReport(pings)
const webhook_url = conf.webhook_url.replace('$DISCORD_WEBHOOK_ID', process.env.DISCORD_WEBHOOK_ID).replace('$DISCORD_WEBHOOK_TOKEN', process.env.DISCORD_WEBHOOK_TOKEN) const webhook_url = conf.webhook_url
await axios.post(webhook_url, { content: report }) .replace('$DISCORD_WEBHOOK_ID', process.env.DISCORD_WEBHOOK_ID)
.replace('$DISCORD_WEBHOOK_TOKEN', process.env.DISCORD_WEBHOOK_TOKEN)
await axios.post(webhook_url, { content: report })
return new Response() return new Response()
} }

View file

@ -5,6 +5,97 @@ __metadata:
version: 8 version: 8
cacheKey: 10c0 cacheKey: 10c0
"@biomejs/biome@npm:^1.6.1":
version: 1.6.1
resolution: "@biomejs/biome@npm:1.6.1"
dependencies:
"@biomejs/cli-darwin-arm64": "npm:1.6.1"
"@biomejs/cli-darwin-x64": "npm:1.6.1"
"@biomejs/cli-linux-arm64": "npm:1.6.1"
"@biomejs/cli-linux-arm64-musl": "npm:1.6.1"
"@biomejs/cli-linux-x64": "npm:1.6.1"
"@biomejs/cli-linux-x64-musl": "npm:1.6.1"
"@biomejs/cli-win32-arm64": "npm:1.6.1"
"@biomejs/cli-win32-x64": "npm:1.6.1"
dependenciesMeta:
"@biomejs/cli-darwin-arm64":
optional: true
"@biomejs/cli-darwin-x64":
optional: true
"@biomejs/cli-linux-arm64":
optional: true
"@biomejs/cli-linux-arm64-musl":
optional: true
"@biomejs/cli-linux-x64":
optional: true
"@biomejs/cli-linux-x64-musl":
optional: true
"@biomejs/cli-win32-arm64":
optional: true
"@biomejs/cli-win32-x64":
optional: true
bin:
biome: bin/biome
checksum: 10c0/789580c9aa7e0661a643e82de85f15bcc4ef0c79a099204f234b3769cface5c604b137260a4a60767ab3a692bd7ac19e2de1fc75537ec14e7698f0b6a133cfef
languageName: node
linkType: hard
"@biomejs/cli-darwin-arm64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-darwin-arm64@npm:1.6.1"
conditions: os=darwin & cpu=arm64
languageName: node
linkType: hard
"@biomejs/cli-darwin-x64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-darwin-x64@npm:1.6.1"
conditions: os=darwin & cpu=x64
languageName: node
linkType: hard
"@biomejs/cli-linux-arm64-musl@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-arm64-musl@npm:1.6.1"
conditions: os=linux & cpu=arm64 & libc=musl
languageName: node
linkType: hard
"@biomejs/cli-linux-arm64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-arm64@npm:1.6.1"
conditions: os=linux & cpu=arm64 & libc=glibc
languageName: node
linkType: hard
"@biomejs/cli-linux-x64-musl@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-x64-musl@npm:1.6.1"
conditions: os=linux & cpu=x64 & libc=musl
languageName: node
linkType: hard
"@biomejs/cli-linux-x64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-linux-x64@npm:1.6.1"
conditions: os=linux & cpu=x64 & libc=glibc
languageName: node
linkType: hard
"@biomejs/cli-win32-arm64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-win32-arm64@npm:1.6.1"
conditions: os=win32 & cpu=arm64
languageName: node
linkType: hard
"@biomejs/cli-win32-x64@npm:1.6.1":
version: 1.6.1
resolution: "@biomejs/cli-win32-x64@npm:1.6.1"
conditions: os=win32 & cpu=x64
languageName: node
linkType: hard
"@netlify/blobs@npm:^7.0.1": "@netlify/blobs@npm:^7.0.1":
version: 7.0.1 version: 7.0.1
resolution: "@netlify/blobs@npm:7.0.1" resolution: "@netlify/blobs@npm:7.0.1"
@ -106,6 +197,7 @@ __metadata:
version: 0.0.0-use.local version: 0.0.0-use.local
resolution: "healthcheck@workspace:." resolution: "healthcheck@workspace:."
dependencies: dependencies:
"@biomejs/biome": "npm:^1.6.1"
"@netlify/blobs": "npm:^7.0.1" "@netlify/blobs": "npm:^7.0.1"
"@netlify/functions": "npm:^2.6.0" "@netlify/functions": "npm:^2.6.0"
"@types/node": "npm:^20.11.27" "@types/node": "npm:^20.11.27"