From 09b83df275dec2ce83b0c9d63fda0f09d54491bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Grie=C3=9Fhaber?= Date: Fri, 20 Sep 2024 12:03:25 +0200 Subject: [PATCH] move from old deps.ts style to import maps in deno.jsonc --- configuration.ts | 2 +- deno.jsonc | 14 +++++++++++++ deps.ts | 18 ---------------- docs/advanced/custom-templates.md | 8 +++---- lib/async.ts | 8 +++---- lib/docker-api.ts | 2 +- lib/env.ts | 2 +- lib/event_registry.ts | 5 +++-- lib/lockfile.ts | 5 +++-- lib/notifiers.ts | 35 ++++++++++++++----------------- lib/templates.ts | 10 ++++++--- lib/universal-http.ts | 2 +- main.ts | 2 +- test/templates.test.ts | 2 +- 14 files changed, 57 insertions(+), 58 deletions(-) delete mode 100644 deps.ts diff --git a/configuration.ts b/configuration.ts index d7ef9af..39c6e6c 100644 --- a/configuration.ts +++ b/configuration.ts @@ -1,5 +1,5 @@ +import * as log from "@std/log"; import { CheckedConfiguration, ConfigOption, EnvironmentConfiguration } from "./lib/env.ts"; -import { log } from "./deps.ts"; import { CONTAINER_ACTIONS, ContainerAction } from "./lib/docker-api.ts"; const SUPERVISION_MODES = ["TAGGED", "UNTAGGED", "PREFIXED", "NOTPREFIXED", "ALL"] as const; diff --git a/deno.jsonc b/deno.jsonc index 2b9c3b0..362e8dd 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -16,5 +16,19 @@ "compilerOptions": { "emitDecoratorMetadata": true, "experimentalDecorators": true + }, + "imports": { + "@std/io": "jsr:@std/io@0.224.8", + "@std/log": "jsr:@std/log@0.224.7", + "@std/front-matter": "jsr:@std/front-matter@1.0.5", + "@std/path": "jsr:@std/path@1.0.6", + "@std/async": "jsr:@std/async@1.0.5", + "@std/streams": "jsr:@std/streams@1.0.5", + "@std/ulid": "jsr:@std/ulid@1.0.0", + "@dx/reflect": "jsr:@dx/reflect@0.2.14", + "eta": "https://deno.land/x/eta@v3.5.0/src/index.ts", + "eta/file-handling": "https://deno.land/x/eta@v3.5.0/src/file-handling.ts", + "@b-fuze/deno-dom": "jsr:@b-fuze/deno-dom@0.1.48", + "deno-smtp": "https://deno.land/x/smtp@v0.7.0/mod.ts" } } diff --git a/deps.ts b/deps.ts deleted file mode 100644 index 3d85423..0000000 --- a/deps.ts +++ /dev/null @@ -1,18 +0,0 @@ -export * as io from "https://deno.land/std@0.220.1/io/mod.ts"; -export * as log from "https://deno.land/std@0.220.1/log/mod.ts"; -export * as path from "https://deno.land/std@0.220.1/path/mod.ts"; -export * as async from "https://deno.land/std@0.220.1/async/mod.ts"; -export { ulid } from "https://deno.land/std@0.220.1/ulid/mod.ts"; -export { DelimiterStream, TextLineStream } from "https://deno.land/std@0.220.1/streams/mod.ts"; - -import * as front_matter from "https://deno.land/std@0.220.1/front_matter/mod.ts"; -export const extract_frontmatter = front_matter.createExtractor({ json: JSON.parse }); - -export { Reflect as ReflectMetadata } from "https://deno.land/x/reflect_metadata@v0.1.12/mod.ts"; -export { SmtpClient } from "https://deno.land/x/smtp@v0.7.0/mod.ts"; - -export { Eta } from "https://deno.land/x/eta@v3.4.0/src/index.ts"; -export { type Options as EtaOptions } from "https://deno.land/x/eta@v3.4.0/src/config.ts"; -export { resolvePath as resolve_template_path } from "https://deno.land/x/eta@v3.4.0/src/file-handling.ts"; - -export { DOMParser } from "https://deno.land/x/deno_dom@v0.1.45/deno-dom-wasm.ts"; diff --git a/docs/advanced/custom-templates.md b/docs/advanced/custom-templates.md index dbf3058..a974cd4 100644 --- a/docs/advanced/custom-templates.md +++ b/docs/advanced/custom-templates.md @@ -83,7 +83,7 @@ For `restart` templates the context additionally contains the following properti | `downtime_end` | `Date` | Timestamp at which Dolce started again | [^1]: [`DockerContainerEvent`](https://github.com/dangrie158/dolce/blob/master/lib/event_registry.ts#L4) and -[`DockerApiContainerEvent`](https://github.com/dangrie158/dolce/blob/master/lib/docker-api.ts#L86) + [`DockerApiContainerEvent`](https://github.com/dangrie158/dolce/blob/master/lib/docker-api.ts#L86) ### Helper Functions @@ -133,9 +133,9 @@ action: | `die` | ❌ | | `kill` | ❌ | | `oom` | ❌ | -| `stop` | ⏹️ | -| `pause` | ⏸️ | -| `unpause` | ⏯️ | +| `stop` | ⏹️ | +| `pause` | ⏸️ | +| `unpause` | ⏯️ | | `health_status` | ❓ | Example usage: diff --git a/lib/async.ts b/lib/async.ts index 6dcbdb5..d3a6cd3 100644 --- a/lib/async.ts +++ b/lib/async.ts @@ -1,4 +1,4 @@ -import { async } from "../deps.ts"; +import { deadline } from "@std/async"; export function wait(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); @@ -33,15 +33,15 @@ export function throttle>( export function DeadlinedReader( reader: ReadableStreamDefaultReader, - deadline: number, + timeout: number, ): ReadableStreamDefaultReader { return { ...reader, read: async () => { try { - return await async.deadline(reader.read(), deadline); + return await deadline(reader.read(), timeout); } catch (error) { - if (error instanceof async.DeadlineError) { + if (error instanceof DOMException && error.name === "TimeoutError") { reader.cancel(); return { done: true, value: undefined }; } diff --git a/lib/docker-api.ts b/lib/docker-api.ts index cbf5d13..ca7ee36 100644 --- a/lib/docker-api.ts +++ b/lib/docker-api.ts @@ -1,4 +1,4 @@ -import { TextLineStream } from "../deps.ts"; +import { TextLineStream } from "@std/streams"; import { DeadlinedReader } from "./async.ts"; import { HttpSocket } from "./universal-http.ts"; diff --git a/lib/env.ts b/lib/env.ts index b8a4daa..e2357de 100644 --- a/lib/env.ts +++ b/lib/env.ts @@ -1,4 +1,4 @@ -import { ReflectMetadata } from "../deps.ts"; +import { Reflect as ReflectMetadata } from "@dx/reflect"; /** * Convenience functions to get values from the environment in the requestst type * with a defaultvalue as fallback diff --git a/lib/event_registry.ts b/lib/event_registry.ts index a2ce532..ac48140 100644 --- a/lib/event_registry.ts +++ b/lib/event_registry.ts @@ -1,5 +1,6 @@ +import { ulid } from "@std/ulid"; +import { join as join_path } from "@std/path"; import { DockerApiContainerEvent } from "./docker-api.ts"; -import { path, ulid } from "../deps.ts"; type DockerContainerEvent = DockerApiContainerEvent & { actor_name: string; @@ -38,7 +39,7 @@ export async function register( delivery_callback: DeliveryCallback, backoff_settings: BackoffSettings, ): Promise { - const db_path = path.join(run_directory, "dolce.db"); + const db_path = join_path(run_directory, "dolce.db"); const db = await Deno.openKv(db_path); const registry: EventRegistry = { db, backoff_settings }; diff --git a/lib/lockfile.ts b/lib/lockfile.ts index 0a62082..79d1c39 100644 --- a/lib/lockfile.ts +++ b/lib/lockfile.ts @@ -1,4 +1,5 @@ -import { log, path } from "../deps.ts"; +import { getLogger } from "@std/log"; +import * as path from "@std/path"; import { throttle } from "./async.ts"; type LockFileInformation = { @@ -20,7 +21,7 @@ export enum LockFileRegisterStatus { export class LockFile { static UPDATE_THROTTLE_INTERVAL = 1000; static get logger() { - return log.getLogger("lockfile"); + return getLogger("lockfile"); } private lock_file_path: string; diff --git a/lib/notifiers.ts b/lib/notifiers.ts index 5fa8d0d..a958d89 100644 --- a/lib/notifiers.ts +++ b/lib/notifiers.ts @@ -1,4 +1,5 @@ -import { log, SmtpClient } from "../deps.ts"; +import { SmtpClient } from "deno-smtp"; +import { getLogger } from "@std/log"; import { DockerApiContainerEvent } from "./docker-api.ts"; import { CheckedConfiguration, ConfigOption, EnvironmentConfiguration } from "./env.ts"; import { @@ -38,7 +39,7 @@ export abstract class Notifier { protected abstract send_message(_message: Template): Promise; public static get logger() { - return log.getLogger("notifier"); + return getLogger("notifier"); } } @@ -172,18 +173,19 @@ class TelegramNotifier extends Notifier { static message_class = TelegramTemplate; protected async send_message(message: TelegramTemplate) { - const send_promises = TelegramNotifier.config.recipient_ids.map(async (recipient) => - await fetch(`https://api.telegram.org/bot${TelegramNotifier.config.http_token!}/sendMessage`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - chat_id: recipient, - text: message.text, - parse_mode: "MarkdownV2", + const send_promises = TelegramNotifier.config.recipient_ids.map( + async (recipient) => + await fetch(`https://api.telegram.org/bot${TelegramNotifier.config.http_token!}/sendMessage`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + chat_id: recipient, + text: message.text, + parse_mode: "MarkdownV2", + }), }), - }) ); await Promise.all(send_promises); } @@ -253,9 +255,4 @@ export function try_create( notifier_class.logger.info(`creating ${notifier_class.name}`); return new notifier_class(notifier_class.message_class, hostname); } -export const ALL_NOTIFIERS: ConcreteNotifier[] = [ - SmtpNotifier, - DiscordNotifier, - TelegramNotifier, - AppriseNotifier, -]; +export const ALL_NOTIFIERS: ConcreteNotifier[] = [SmtpNotifier, DiscordNotifier, TelegramNotifier, AppriseNotifier]; diff --git a/lib/templates.ts b/lib/templates.ts index 17a1bc4..ea6e0b2 100644 --- a/lib/templates.ts +++ b/lib/templates.ts @@ -1,6 +1,10 @@ -import { DOMParser, Eta, EtaOptions, extract_frontmatter, path, resolve_template_path } from "../deps.ts"; +import * as front_matter from "@std/front-matter"; +import * as path from "@std/path"; +import { DOMParser } from "@b-fuze/deno-dom"; +import { Eta, type Options as EtaOptions } from "eta"; import { DockerApiContainerEvent } from "./docker-api.ts"; import { Configuration } from "../configuration.ts"; +import { resolvePath as resolve_template_path } from "eta/file-handling"; export type EventTemplateName = "event.eta" | "restart.eta"; export type BaseMessageContext = { @@ -172,7 +176,7 @@ export class EMailTemplate extends Template { async render(context: MessageContext) { const template_path = this.path; const template_contents = await Deno.readTextFile(template_path); - const { attrs, body } = extract_frontmatter(template_contents); + const { attrs, body } = front_matter.extractJson(template_contents); this.frontmatter = attrs; this.html_content = await this.engine.renderStringAsync(body, { ...context, @@ -230,7 +234,7 @@ export class AppriseTemplate extends Template { async render(context: MessageContext) { const template_path = this.path; const template_contents = await Deno.readTextFile(template_path); - const { attrs, body } = extract_frontmatter(template_contents); + const { attrs, body } = front_matter.extractJson(template_contents); this.frontmatter = attrs; this.text_content = await this.engine.renderStringAsync(body, { ...context, diff --git a/lib/universal-http.ts b/lib/universal-http.ts index 9e678a5..eea4e94 100644 --- a/lib/universal-http.ts +++ b/lib/universal-http.ts @@ -1,4 +1,4 @@ -import { DelimiterStream } from "../deps.ts"; +import { DelimiterStream } from "@std/streams"; const UA_VERSION = "2.10.9"; const UA_STRING = `Dolce Container Monitor v${UA_VERSION}`; diff --git a/main.ts b/main.ts index 23f1905..2361c45 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,4 @@ -import { log } from "./deps.ts"; +import * as log from "@std/log"; import { DockerApi, DockerApiContainerEvent, DockerApiEvent, DockerApiEventFilters } from "./lib/docker-api.ts"; import { LockFile, LockFileRegisterStatus } from "./lib/lockfile.ts"; diff --git a/test/templates.test.ts b/test/templates.test.ts index 5e6d3a3..b1634ef 100644 --- a/test/templates.test.ts +++ b/test/templates.test.ts @@ -7,7 +7,7 @@ import { TelegramTemplate, } from "../lib/templates.ts"; import { assert, assertStrictEquals, assertThrows } from "https://deno.land/std@0.204.0/assert/mod.ts"; -import { path } from "../deps.ts"; +import * as path from "@std/path"; const dummy_render_context: RestartMessageContext = { downtime_start: new Date(),