-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
006c60b
commit 6d7647f
Showing
10 changed files
with
71 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { expect } from "@utils/testing.ts" | ||
import { metadata } from "@engine/metadata.ts" | ||
import { version } from "@engine/version.ts" | ||
|
||
// TODO(@lowlighter): change to `[dir.source]` after https://github.com/denoland/deno_std/pull/3692 | ||
Deno.test("metadata()", { permissions: { read: true } }, async (t) => { | ||
await t.step("returns metadata", async () => { | ||
await expect(metadata()).to.eventually.containSubset({ version }).and.include.keys("plugins", "processors", "cli", "server", "webrequest") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { expect, MetricsError } from "@utils/testing.ts" | ||
import { process } from "@engine/process.ts" | ||
import * as dir from "@engine/paths.ts" | ||
|
||
Deno.test("process()", { permissions: { read: [dir.source] } }, async (t) => { | ||
await t.step("returns processed config", async () => { | ||
await expect(process({})).to.eventually.be.null | ||
await expect(process({ plugins: [{ logs: "none" }] })).to.eventually.have.property("result") | ||
await expect(process({ plugins: [{ id: "introduction", handle: "octocat", logs: "none", mock: true }] })).to.eventually.have.property("result") | ||
}) | ||
await t.step("throws on unknown component", async () => { | ||
await expect(process({ plugins: [{ id: "foo", logs: "none" }] })).to.rejectedWith(MetricsError, /could not be loaded/i) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
import { env } from "@utils/io.ts" | ||
Check notice Code scanning / CodeQL Unused variable, import, function or class Note
Unused import env.
|
||
import core from "y/@actions/[email protected]" | ||
Check notice Code scanning / CodeQL Unused variable, import, function or class Note
Unused import core.
|
||
|
||
type compat = any | ||
|
||
function deprecated(message: string) { | ||
console.warn() | ||
} | ||
|
@@ -12,17 +14,17 @@ const to = { | |
}, | ||
} | ||
|
||
export function compat(inputs: any) { | ||
const config = { plugins: [] } as any | ||
export function compat(inputs: compat) { | ||
const config = { plugins: [] } as compat | ||
|
||
/*- | ||
- ❗ `plugin_introduction: yes` ➡️ `plugins: [{id: introduction}]` | ||
- ❌ `` ➡️ `processors: [{id: inject.style, args: {style: ".introduction .title { display: none }"}}]` | ||
*/ | ||
// 🙋 Introduction | ||
if (to.boolean(inputs.plugin_introduction)) { | ||
const plugin = { introduction: {} } as any | ||
deprecated(`<plugin_introduction> is deprecated, use <introduction> plugin`) | ||
const plugin = { introduction: {} } as compat | ||
deprecated(`"plugin_introduction" is deprecated, use <introduction> plugin`) | ||
|
||
deprecated(`<plugin_introduction_title> is deprecated, use <calendar> plugin with <calendar.range: ${duration}>`) | ||
const title = to.boolean(inputs.plugin_introduction_title) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
//Imports | ||
import { Internal, is, toSchema } from "@metrics/components/internal.ts" | ||
import { cli as schema, load } from "@metrics/config.ts" | ||
import { Internal, is, toSchema } from "@engine/components/internal.ts" | ||
import { cli as schema, load } from "@engine/config.ts" | ||
import github from "y/@actions/[email protected]" | ||
import { latest, version } from "@metrics/version.ts" | ||
import { latest, version } from "@engine/version.ts" | ||
import core from "y/@actions/[email protected]" | ||
import { process } from "@metrics/process.ts" | ||
import { process } from "@engine/process.ts" | ||
import { parse } from "std/flags/mod.ts" | ||
import { cyan, gray } from "std/fmt/colors.ts" | ||
import { expandGlobSync } from "std/fs/expand_glob.ts" | ||
|
@@ -33,8 +33,8 @@ class CLI extends Internal { | |
for (const [key, value] of Object.entries(inputs)) { | ||
env.set(`INPUT_${key.replace(/ /g, "_").toUpperCase()}`, `${value}`) | ||
} | ||
console.log(compat(inputs)) | ||
Check warning Code scanning / CodeQL Use of returnless function Warning
the
function compat Error loading related location Loading |
||
} | ||
console.log(compat()) | ||
} | ||
|
||
/** Run metrics */ | ||
|
@@ -49,7 +49,7 @@ class CLI extends Internal { | |
} | ||
if (this.context.check_updates) { | ||
const upstream = await latest() | ||
if (version !== upstream) { | ||
if (version.number !== upstream) { | ||
core.info(`Version ${upstream} is available!`) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,22 @@ | ||
//Imports | ||
import { serveListener } from "std/http/server.ts" | ||
import { server as schema, webrequest } from "@metrics/config.ts" | ||
import { process } from "@metrics/process.ts" | ||
import { server as schema, webrequest } from "@engine/config.ts" | ||
import { process } from "@engine/process.ts" | ||
import { is } from "@utils/validation.ts" | ||
import { env, KV, listen, read } from "@utils/io.ts" | ||
import { Internal } from "@metrics/components/internal.ts" | ||
import { Internal } from "@engine/components/internal.ts" | ||
import * as YAML from "std/yaml/mod.ts" | ||
import { parseHandle } from "@utils/parse.ts" | ||
import { parseHandle } from "@utils/github.ts" | ||
import * as Base64 from "std/encoding/base64.ts" | ||
import { serveDir, serveFile } from "std/http/file_server.ts" | ||
import { fromFileUrl } from "std/path/from_file_url.ts" | ||
import { Status } from "std/http/http_status.ts" | ||
import { metadata } from "@metrics/metadata.ts" | ||
import { metadata } from "@engine/metadata.ts" | ||
import { deferred } from "std/async/deferred.ts" | ||
import { deepMerge } from "std/collections/deep_merge.ts" | ||
import { getCookies, setCookie } from "std/http/cookie.ts" | ||
import { formatValidationError } from "@utils/errors.ts" | ||
import { Secret } from "@utils/secret.ts" | ||
import { Requests } from "@metrics/components/requests.ts" | ||
import { Requests } from "@engine/components/requests.ts" | ||
import { App } from "y/@octokit/[email protected]" | ||
import { client } from "./mod_imports.ts" | ||
try { | ||
|
@@ -72,7 +71,7 @@ class Server extends Internal { | |
/** Start server */ | ||
start() { | ||
this.log.info(`listening on ${this.context.hostname}:${this.context.port}`) | ||
return serveListener(listen({ hostname: this.context.hostname, port: this.context.port }), (request, connection) => this.handle(request, connection)) | ||
return serveListener(listen({ hostname: this.context.hostname, port: this.context.port }), (request, connection) => this.handle(request, connection as { remoteAddr: { hostname: string } })) | ||
} | ||
|
||
/** Metadata */ | ||
|
@@ -138,7 +137,7 @@ class Server extends Internal { | |
if (session && (await this.#kv.has(`sessions.${session}`))) { | ||
return Response.redirect(url.origin, Status.Found) | ||
} | ||
return Response.redirect(this.#app.oauth.getWebFlowAuthorizationUrl({ allowSignup: false }).url, Status.Found) | ||
return Response.redirect(this.#app!.oauth.getWebFlowAuthorizationUrl({ allowSignup: false }).url, Status.Found) | ||
} | ||
// OAuth authorization process | ||
case "/authorize": { | ||
|
@@ -148,9 +147,9 @@ class Server extends Internal { | |
try { | ||
const state = url.searchParams.get("state") | ||
const code = url.searchParams.get("code") | ||
const { authentication: { token, expiresAt: expiration } } = await this.#app.oauth.createToken({ state, code }) | ||
const { authentication: { token, expiresAt: expiration } } = await this.#app!.oauth.createToken({ state, code }) | ||
const ttl = new Date(expiration).getTime() - Date.now() | ||
const { data: { user: { login, avatar_url: avatar } } } = await this.#app.oauth.checkToken({ token }) | ||
const { data: { user: { login, avatar_url: avatar } } } = await this.#app!.oauth.checkToken({ token }) | ||
if (await this.#kv.has(`sessions.login.${login}`)) { | ||
log.trace(`oauth process: user ${login} was already authenticated`) | ||
return Response.redirect(url.origin, Status.Found) | ||
|
@@ -174,7 +173,7 @@ class Server extends Internal { | |
} | ||
try { | ||
const { login, token } = await this.#kv.get<user>(`sessions.${session}`) | ||
await this.#app.oauth.deleteToken({ token }) | ||
await this.#app!.oauth.deleteToken({ token }) | ||
await this.#kv.delete(`sessions.${session}`) | ||
await this.#kv.delete(`sessions.login.${login}`) | ||
log.trace(`oauth process: user ${login} revoked their token`) | ||
|
@@ -205,7 +204,7 @@ class Server extends Internal { | |
// Serve renders | ||
case this.routes.metrics.test(url.pathname): { | ||
const { handle: _handle, ext = "svg" } = url.pathname.match(this.routes.metrics)?.groups ?? {} | ||
const { handle, login } = parseHandle(_handle) | ||
const { login: handle } = parseHandle(_handle, { entity: "user" }) | ||
const _log = log | ||
let user = null as user | null | ||
if (!handle) { | ||
|
@@ -240,7 +239,7 @@ class Server extends Internal { | |
log.trace(context) | ||
} catch (error) { | ||
log.warn(error) | ||
return promise.resolve(new Response(`Bad request: ${formatValidationError(error)}`, { status: Status.BadRequest }))! | ||
return promise.resolve(new Response(`Bad request: ${error}`, { status: Status.BadRequest }))! | ||
} | ||
|
||
// Filter features and apply server configuration | ||
|
@@ -253,7 +252,7 @@ class Server extends Internal { | |
//TODO(@lowlighter): filter enabled plugins and params | ||
} catch (error) { | ||
log.warn(error) | ||
return promise.resolve(new Response(`Bad request: ${formatValidationError(error)}`, { status: Status.BadRequest }))! | ||
return promise.resolve(new Response(`Bad request: ${error}`, { status: Status.BadRequest }))! | ||
} | ||
|
||
// Load user session if available | ||
|
@@ -283,13 +282,13 @@ class Server extends Internal { | |
await this.#kv.get<{ current: number; reset: number; init?: boolean }>(`requests.ratelimit.${user}`) ?? { init: true } | ||
log.trace(`requests ratelimit: ${current + 1} / ${limit} until ${new Date(reset).toISOString()}`) | ||
if (current + 1 > limit) { | ||
log.trace(`requests ratelimit: preventing further requests for "${login}" until ${new Date(reset).toISOString()}`) | ||
log.trace(`requests ratelimit: preventing further requests for "${from}" until ${new Date(reset).toISOString()}`) | ||
return new Response(`Too Many Requests: Rate limit exceeded (wait ~${Math.ceil(Math.max((reset - Date.now()) / 1000, 1))}s)`, { status: Status.TooManyRequests }) | ||
} | ||
if (init) { | ||
await this.#kv.set(`requests.ratelimit.${login}`, { current: current + 1, reset }, { ttl: 1000 * duration }) | ||
await this.#kv.set(`requests.ratelimit.${from}`, { current: current + 1, reset }, { ttl: 1000 * duration }) | ||
} else { | ||
await this.#kv.set(`requests.ratelimit.${login}`, { current: current + 1, reset }) | ||
await this.#kv.set(`requests.ratelimit.${from}`, { current: current + 1, reset }) | ||
} | ||
} | ||
} | ||
|
@@ -358,7 +357,7 @@ class Server extends Internal { | |
switch (action) { | ||
// Stop instance | ||
case "stop": { | ||
if (!this.context.control[token]?.[action]) { | ||
if (!this.context.control?.[token]?.[action]) { | ||
return new Response("Forbidden", { status: Status.Forbidden }) | ||
} | ||
this.log.info("received stop request, server will shutdown in a few seconds") | ||
|