From 2f54d416256426af6de01c9c46036c03844a5e06 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sun, 20 Oct 2024 07:48:27 +0200 Subject: [PATCH] Incomplete progress, transition toward postgres --- deno.json | 5 +- deno.lock | 127 ++++++++++++++++++++++++++++++++++++--- src/database/kv.ts | 12 ++++ src/database/postgres.ts | 22 +++++++ src/dv/dotinoVeloce.ts | 15 +++-- src/fedify/kv.ts | 12 ---- src/fedify/redis.ts | 11 ---- src/main.ts | 8 +-- 8 files changed, 171 insertions(+), 41 deletions(-) create mode 100644 src/database/kv.ts create mode 100644 src/database/postgres.ts delete mode 100644 src/fedify/kv.ts delete mode 100644 src/fedify/redis.ts diff --git a/deno.json b/deno.json index 5944e6e..6b0df48 100644 --- a/deno.json +++ b/deno.json @@ -1,13 +1,14 @@ { "imports": { "@@npm/ioredis": "npm:ioredis@^5.4.1", + "@@x/escape": "https://deno.land/x/escape@1.3.0/mod.ts", "@fedify/fedify": "jsr:@fedify/fedify@^1.0.2", - "@fedify/redis": "jsr:@fedify/redis@^0.3.0", + "@fedify/postgres": "jsr:@fedify/postgres@^0.1.0", "@hongminhee/x-forwarded-fetch": "jsr:@hongminhee/x-forwarded-fetch@^0.2.0", "@logtape/logtape": "jsr:@logtape/logtape@^0.6.3", "@opentelemetry/api": "npm:@opentelemetry/api@^1.9.0", "@std/assert": "jsr:@std/assert@1", - "@@x/escape": "https://deno.land/x/escape@1.3.0/mod.ts" + "@@npm/postgres": "npm:postgres@^3.4.4" }, "unstable": [ "temporal" diff --git a/deno.lock b/deno.lock index 16ac9f1..901372b 100644 --- a/deno.lock +++ b/deno.lock @@ -4,7 +4,7 @@ "specifiers": { "jsr:@fedify/fedify@^1.0.0": "jsr:@fedify/fedify@1.0.2", "jsr:@fedify/fedify@^1.0.2": "jsr:@fedify/fedify@1.0.2", - "jsr:@fedify/redis@^0.3.0": "jsr:@fedify/redis@0.3.0", + "jsr:@fedify/postgres@^0.1.0": "jsr:@fedify/postgres@0.1.0", "jsr:@hongminhee/x-forwarded-fetch@^0.2.0": "jsr:@hongminhee/x-forwarded-fetch@0.2.0", "jsr:@hugoalh/http-header-link@^1.0.2": "jsr:@hugoalh/http-header-link@1.0.2", "jsr:@hugoalh/is-string-singleline@1.0.2": "jsr:@hugoalh/is-string-singleline@1.0.2", @@ -20,12 +20,14 @@ "npm:@phensley/language-tag@^1.9.0": "npm:@phensley/language-tag@1.9.0", "npm:@types/node": "npm:@types/node@18.16.19", "npm:asn1js@^3.0.5": "npm:asn1js@3.0.5", + "npm:graffle@next": "npm:graffle@8.0.0-next.68_@opentelemetry+api@1.9.0_graphql@16.9.0", "npm:ioredis@^5.4.1": "npm:ioredis@5.4.1", "npm:json-canon@^1.0.1": "npm:json-canon@1.0.1", "npm:jsonld@^8.3.2": "npm:jsonld@8.3.2", "npm:multibase@^4.0.6": "npm:multibase@4.0.6", "npm:multicodec@^3.2.1": "npm:multicodec@3.2.1", "npm:pkijs@^3.2.4": "npm:pkijs@3.2.4", + "npm:postgres@^3.4.4": "npm:postgres@3.4.4", "npm:uri-template-router@^0.0.16": "npm:uri-template-router@0.0.16", "npm:url-template@^3.1.1": "npm:url-template@3.1.1" }, @@ -50,12 +52,11 @@ "npm:url-template@^3.1.1" ] }, - "@fedify/redis@0.3.0": { - "integrity": "48068af7ad24d4f6c6935d6f869659faeb92281a9e796921f9ba7d94e74f8cfc", + "@fedify/postgres@0.1.0": { + "integrity": "350e3e535372d84acebe7392ae98495e388f30d75165d52c6d32a8dc6e940b80", "dependencies": [ "jsr:@fedify/fedify@^1.0.0", - "jsr:@logtape/logtape@^0.6.3", - "npm:ioredis@^5.4.1" + "npm:postgres@^3.4.4" ] }, "@hongminhee/x-forwarded-fetch@0.2.0": { @@ -108,10 +109,38 @@ "integrity": "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==", "dependencies": {} }, + "@graphql-typed-document-node/core@3.2.0_graphql@16.9.0": { + "integrity": "sha512-mB9oAsNCm9aM3/SOv4YtBMqZbYj10R7dkq8byBqxGY/ncFwhf2oQzMV+LCRlWoDSEBJ3COiR1yeDvMtsoOsuFQ==", + "dependencies": { + "graphql": "graphql@16.9.0" + } + }, "@ioredis/commands@1.2.0": { "integrity": "sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg==", "dependencies": {} }, + "@molt/command@0.9.0": { + "integrity": "sha512-1JI8dAlpqlZoXyKWVQggX7geFNPxBpocHIXQCsnxDjKy+3WX4SGyZVJXuLlqRRrX7FmQCuuMAfx642ovXmPA9g==", + "dependencies": { + "@molt/types": "@molt/types@0.2.0", + "alge": "alge@0.8.1", + "chalk": "chalk@5.3.0", + "lodash.camelcase": "lodash.camelcase@4.3.0", + "lodash.snakecase": "lodash.snakecase@4.1.1", + "readline-sync": "readline-sync@1.4.10", + "string-length": "string-length@6.0.0", + "strip-ansi": "strip-ansi@7.1.0", + "ts-toolbelt": "ts-toolbelt@9.6.0", + "type-fest": "type-fest@4.26.1", + "zod": "zod@3.23.8" + } + }, + "@molt/types@0.2.0": { + "integrity": "sha512-p6ChnEZDGjg9PYPec9BK6Yp5/DdSrYQvXTBAtgrnqX6N36cZy37ql1c8Tc5LclfIYBNG7EZp8NBcRTYJwyi84g==", + "dependencies": { + "ts-toolbelt": "ts-toolbelt@9.6.0" + } + }, "@multiformats/base-x@4.0.1": { "integrity": "sha512-eMk0b9ReBbV23xXU693TAIrLyeO5iTgBZGSJfpqriG8UkYvr/hC9u9pyMlAakDNHWmbhMZCDs6KQO0jzKD8OTw==", "dependencies": {} @@ -140,6 +169,19 @@ "event-target-shim": "event-target-shim@5.0.1" } }, + "alge@0.8.1": { + "integrity": "sha512-kiV9nTt+XIauAXsowVygDxMZLplZxDWt0W8plE/nB32/V2ziM/P/TxDbSVK7FYIUt2Xo16h3/htDh199LNPCKQ==", + "dependencies": { + "lodash.ismatch": "lodash.ismatch@4.4.0", + "remeda": "remeda@1.61.0", + "ts-toolbelt": "ts-toolbelt@9.6.0", + "zod": "zod@3.23.8" + } + }, + "ansi-regex@6.1.0": { + "integrity": "sha512-7HSX4QQb4CspciLpVFwyRe79O3xsIZDDLER21kERQ71oaPodF8jL725AgJMFAYbooIqolJoRLuM81SpeUkpkvA==", + "dependencies": {} + }, "asn1js@3.0.5": { "integrity": "sha512-FVnvrKJwpt9LP2lAMl8qZswRNm3T4q9CON+bxldk2iwk3FFpuwhx2FfinyitizWHsVYyaY+y5JzDR0rCMV5yTQ==", "dependencies": { @@ -156,6 +198,10 @@ "integrity": "sha512-0CNTVCLZggSh7bc5VkX5WWPWO+cyZbNd07IHIsSXLia/eAq+r836hgk+8BKoEh7949Mda87VUOitx5OddVj64A==", "dependencies": {} }, + "chalk@5.3.0": { + "integrity": "sha512-dLitG79d+GV1Nb/VYcCDFivJeK1hiukt9QjRNVOsUtTy1rR1YJsmpGGTZ3qJos+uw7WmWF4wUwBd9jxjocFC2w==", + "dependencies": {} + }, "cluster-key-slot@1.1.2": { "integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==", "dependencies": {} @@ -191,6 +237,20 @@ "fetch-blob": "fetch-blob@3.2.0" } }, + "graffle@8.0.0-next.68_@opentelemetry+api@1.9.0_graphql@16.9.0": { + "integrity": "sha512-oaigY1yEX48cUSiFJKtigjGw4ooA7JtLwafSUQuuAgbl0qtnaSmAGrfSRpb9RhxF0y/HEzJNEMYUaea085cn8w==", + "dependencies": { + "@graphql-typed-document-node/core": "@graphql-typed-document-node/core@3.2.0_graphql@16.9.0", + "@molt/command": "@molt/command@0.9.0", + "@opentelemetry/api": "@opentelemetry/api@1.9.0", + "graphql": "graphql@16.9.0", + "is-plain-obj": "is-plain-obj@4.1.0" + } + }, + "graphql@16.9.0": { + "integrity": "sha512-GGTKBX4SD7Wdb8mqeDLni2oaRGYQWjWHGKPQ24ZMnUtKfcsVoiv4uX8+LJr1K6U5VW2Lu1BwJnj7uiori0YtRw==", + "dependencies": {} + }, "ioredis@5.4.1": { "integrity": "sha512-2YZsvl7jopIa1gaePkeMtd9rAcSjOOjPtpcLlOeusyO+XH2SK5ZcT+UCrElPP+WVIInh2TzeI4XW9ENaSLVVHA==", "dependencies": { @@ -205,6 +265,10 @@ "standard-as-callback": "standard-as-callback@2.1.0" } }, + "is-plain-obj@4.1.0": { + "integrity": "sha512-+Pgi+vMuUNkJyExiMBt5IlFoMyKnr5zhJ4Uspz58WOhBF5QoIZkFyNHIbBAtHwzVAgk5RtndVNsDRN61/mmDqg==", + "dependencies": {} + }, "json-canon@1.0.1": { "integrity": "sha512-PQcj4PFOTAQxE8PgoQ4KrM0DcKWZd7S3ELOON8rmysl9I8JuFMgxu1H9v+oZsTPjjkpeS3IHPwLjr7d+gKygnw==", "dependencies": {} @@ -230,6 +294,10 @@ "integrity": "sha512-CasD9OCEQSFIam2U8efFK81Yeg8vNMTBUqtMOHlrcWQHqUX3HeCl9Dr31u4toV7emlH8Mymk5+9p0lL6mKb/Xw==", "dependencies": {} }, + "lodash.camelcase@4.3.0": { + "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==", + "dependencies": {} + }, "lodash.defaults@4.2.0": { "integrity": "sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ==", "dependencies": {} @@ -238,6 +306,14 @@ "integrity": "sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg==", "dependencies": {} }, + "lodash.ismatch@4.4.0": { + "integrity": "sha512-fPMfXjGQEV9Xsq/8MTSgUf255gawYRbjwMyDbcvDhXgV7enSZA0hynz6vMPnpAb5iONEzBHBPsT+0zes5Z301g==", + "dependencies": {} + }, + "lodash.snakecase@4.1.1": { + "integrity": "sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw==", + "dependencies": {} + }, "lru-cache@6.0.0": { "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", "dependencies": { @@ -288,6 +364,10 @@ "tslib": "tslib@2.7.0" } }, + "postgres@3.4.4": { + "integrity": "sha512-IbyN+9KslkqcXa8AO9fxpk97PA4pzewvpi2B3Dwy9u4zpV32QicaEdgmF3eSQUzdRk7ttDHQejNgAEr4XoeH4A==", + "dependencies": {} + }, "pvtsutils@1.3.5": { "integrity": "sha512-ARvb14YB9Nm2Xi6nBq1ZX6dAM0FsJnuk+31aUp4TrcZEdKUlSqOqsxJHUPJDNE3qiIp+iUPEIeR6Je/tgV7zsA==", "dependencies": { @@ -304,6 +384,10 @@ "setimmediate": "setimmediate@1.0.5" } }, + "readline-sync@1.4.10": { + "integrity": "sha512-gNva8/6UAe8QYepIQH/jQ2qn91Qj0B9sYjMBBs3QOB8F2CXcKgLxQaJRP76sWVRQt+QU+8fAkCbCvjjMFu7Ycw==", + "dependencies": {} + }, "redis-errors@1.2.0": { "integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==", "dependencies": {} @@ -314,6 +398,10 @@ "redis-errors": "redis-errors@1.2.0" } }, + "remeda@1.61.0": { + "integrity": "sha512-caKfSz9rDeSKBQQnlJnVW3mbVdFgxgGWQKq1XlFokqjf+hQD5gxutLGTTY2A/x24UxVyJe9gH5fAkFI63ULw4A==", + "dependencies": {} + }, "setimmediate@1.0.5": { "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==", "dependencies": {} @@ -322,10 +410,30 @@ "integrity": "sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==", "dependencies": {} }, + "string-length@6.0.0": { + "integrity": "sha512-1U361pxZHEQ+FeSjzqRpV+cu2vTzYeWeafXFLykiFlv4Vc0n3njgU8HrMbyik5uwm77naWMuVG8fhEF+Ovb1Kg==", + "dependencies": { + "strip-ansi": "strip-ansi@7.1.0" + } + }, + "strip-ansi@7.1.0": { + "integrity": "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==", + "dependencies": { + "ansi-regex": "ansi-regex@6.1.0" + } + }, + "ts-toolbelt@9.6.0": { + "integrity": "sha512-nsZd8ZeNUzukXPlJmTBwUAuABDe/9qtVDelJeT/qW0ow3ZS3BsQJtNkan1802aM9Uf68/Y8ljw86Hu0h5IUW3w==", + "dependencies": {} + }, "tslib@2.7.0": { "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==", "dependencies": {} }, + "type-fest@4.26.1": { + "integrity": "sha512-yOGpmOAL7CkKe/91I5O3gPICmJNLJ1G4zFYVAsRHg7M64biSnPtRj0WNQt++bRkjYOqjWXrhnUw1utzmVErAdg==", + "dependencies": {} + }, "uint8arrays@3.1.1": { "integrity": "sha512-+QJa8QRnbdXVpHYjLoTpJIdCTiw9Ir62nocClWuXIq2JIh4Uta0cQsTSpFL678p2CN8B+XSApwcU+pQEqVpKWg==", "dependencies": { @@ -357,6 +465,10 @@ "yallist@4.0.0": { "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==", "dependencies": {} + }, + "zod@3.23.8": { + "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==", + "dependencies": {} } } }, @@ -508,12 +620,13 @@ "workspace": { "dependencies": [ "jsr:@fedify/fedify@^1.0.2", - "jsr:@fedify/redis@^0.3.0", + "jsr:@fedify/postgres@^0.1.0", "jsr:@hongminhee/x-forwarded-fetch@^0.2.0", "jsr:@logtape/logtape@^0.6.3", "jsr:@std/assert@1", "npm:@opentelemetry/api@^1.9.0", - "npm:ioredis@^5.4.1" + "npm:ioredis@^5.4.1", + "npm:postgres@^3.4.4" ] } } diff --git a/src/database/kv.ts b/src/database/kv.ts new file mode 100644 index 0000000..72a3b7e --- /dev/null +++ b/src/database/kv.ts @@ -0,0 +1,12 @@ +import { PostgresKvStore } from "@fedify/postgres/kv" +import { getLogger } from "@logtape/logtape" +import Postgres from "@@npm/postgres" + + +const l = getLogger(["dotino-veloce", "fedify", "kv"]) + + +export function createPostgresKvStore(postgres: Postgres.Sql): PostgresKvStore { + l.info`Creating Postgres key-value store...` + return new PostgresKvStore(postgres, {}) +} diff --git a/src/database/postgres.ts b/src/database/postgres.ts new file mode 100644 index 0000000..477d110 --- /dev/null +++ b/src/database/postgres.ts @@ -0,0 +1,22 @@ +import Postgres from "@@npm/postgres" +import { getLogger } from "@logtape/logtape" + + +const l = getLogger(["dotino-veloce", "fedify", "postgres"]) + + +export function createPostgres(connString: string): Postgres.Sql { + l.info`Creating Postgres object with string: ${connString}` + return Postgres(connString) +} + +export function createPostgresFromEnv(): Postgres.Sql { + l.debug`Getting connection string from environment variable DOTINO_POSTGRES_STRING...` + const connString = Deno.env.get("DOTINO_POSTGRES_STRING") + if(!connString) { + l.error`DOTINO_POSTGRES_STRING is unset.` + throw new Error("DOTINO_POSTGRES_STRING is unset.") + } + + return createPostgres(connString) +} diff --git a/src/dv/dotinoVeloce.ts b/src/dv/dotinoVeloce.ts index 0f1acd9..2cea1d7 100644 --- a/src/dv/dotinoVeloce.ts +++ b/src/dv/dotinoVeloce.ts @@ -23,6 +23,7 @@ export class DotinoVeloce { this.federation .setActorDispatcher("/users/{identifier}", this.#actorHandler.bind(this)) .mapHandle(this.#actorMapper.bind(this)) + .setKeyPairsDispatcher(this.#actorKeys.bind(this)) this.federation .setInboxListeners("/inbox/{identifier}", "/inbox") @@ -193,22 +194,26 @@ export class DotinoVeloce { return actor } - async #actorHandler(ctx: Context, handle: string) { + async #actorHandler(ctx: Context, handle: string): Promise { l.info`Handling actor with handle: ${handle}` let actor = null - actor ??= this.#serviceActor(ctx, handle) - actor ??= this.#playerActor(ctx, handle) - actor ??= this.#guildActor(ctx, handle) + actor ??= await this.#serviceActor(ctx, handle) + actor ??= await this.#playerActor(ctx, handle) + actor ??= await this.#guildActor(ctx, handle) return actor } - async #actorMapper(_ctx: Context, handle: string) { + async #actorMapper(_ctx: Context, handle: string): Promise { return handle } + async #actorKeys(ctx: Context, handle: string): Promise { + throw "TODO: Not implemented" + } + async #followHandler(ctx: Context, follow: Follow) { l.info`Handling follow request: ${follow}` diff --git a/src/fedify/kv.ts b/src/fedify/kv.ts deleted file mode 100644 index 8eb7050..0000000 --- a/src/fedify/kv.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Redis } from "@@npm/ioredis" -import { RedisKvStore } from "@fedify/redis/kv" -import { getLogger } from "@logtape/logtape" - - -const l = getLogger(["dotino-veloce", "fedify", "kv"]) - - -export function createRedisKvStore(redis: Redis) { - l.debug`Creating Redis key-value store...` - return new RedisKvStore(redis, {}) -} diff --git a/src/fedify/redis.ts b/src/fedify/redis.ts deleted file mode 100644 index 1838846..0000000 --- a/src/fedify/redis.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { getLogger } from "@logtape/logtape" -import { Redis } from "@@npm/ioredis" - - -const l = getLogger(["dotino-veloce", "fedify", "redis"]) - - -export function createRedis() { - l.debug`Creating Redis object...` - return new Redis({}) -} diff --git a/src/main.ts b/src/main.ts index 4afbac7..ca598a0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,17 +1,17 @@ import { doServe } from "./deno/server.ts" -import { createRedis } from "./fedify/redis.ts" import { initLogging } from "./deno/logging.ts" -import { createRedisKvStore } from "./fedify/kv.ts" import { StratzAPI } from "./stratz/api.ts" import { behindProxy, Fetch } from "@hongminhee/x-forwarded-fetch" import { createRouter } from "./deno/router.ts" import { DotinoVeloce } from "./dv/dotinoVeloce.ts" +import { createPostgresKvStore } from "./database/kv.ts" +import { createPostgresFromEnv } from "./database/postgres.ts" async function main() { await initLogging() - const redis = createRedis() - const kv = createRedisKvStore(redis) + const postgres = createPostgresFromEnv() + const kv = createPostgresKvStore(postgres) const stratz = StratzAPI.fromEnv() const ap = new DotinoVeloce(kv, stratz) const router = createRouter(ap)