Skip to content

Commit

Permalink
chore: reorganize project according with responsibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed Dec 7, 2024
1 parent 44d4d27 commit f1c1e42
Show file tree
Hide file tree
Showing 63 changed files with 131 additions and 113 deletions.
Binary file modified bun.lockb
Binary file not shown.
14 changes: 2 additions & 12 deletions packages/main/src/config.ts → config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import { type SigningKey, getKeyPair } from "./keys";
import { type EncryptionValidAlgorithm, signText } from "./signJson";

export interface Config {
path: string;
signingKeyPath: string;
port: number;
signingKey: SigningKey[];
name: string;
version: string;
}

import { getKeyPair } from "homeserver";
import type { Config } from "homeserver/src/plugins/config";
const { CONFIG_FOLDER = "." } = process.env;

const getConfig = async (): Promise<Config> => {
Expand Down
17 changes: 16 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,19 @@
// // /v1/openid/userinfo
// // /v1/hierarchy/{roomID}

import "myapp";
import Elysia from "elysia";
import { app } from "homeserver";
import { routerWithMongodb } from "homeserver/src/plugins/mongodb";

import { config } from "./config";
import { db } from "./mongo";

new Elysia()
.decorate("config", config)
.use(routerWithMongodb(db))
.use(app)
.listen(config.port, (context) => {
console.log(
`🦊 Elysia is running at http://${context.hostname}:${context.port}`,
);
});
12 changes: 12 additions & 0 deletions mongo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MongoClient } from "mongodb";

const MONGODB_URI = process.env.MONGODB_URI;
if (!MONGODB_URI) {
throw new Error(
"Please define the MONGODB_URI environment variable inside .env",
);
}

const client = await MongoClient.connect(MONGODB_URI);

export const db = client.db(MONGODB_URI.split("/").pop());
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"dependencies": {
"@bogeychan/elysia-logger": "^0.1.4",
"@elysiajs/swagger": "^1.1.6",
"elysia": "^1.1.26",
"homeserver": "workspace:*",
"mongodb": "^6.11.0",
"node-jsonwebtoken": "^0.0.1",
"tweetnacl": "^1.0.3"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "myapp",
"name": "homeserver",
"version": "1.0.50",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
Expand All @@ -12,5 +12,6 @@
"devDependencies": {
"bun-types": "latest"
},
"module": "src/index.js"
"main": "./src/index.ts",
"types": "./src/index.ts"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 3 additions & 11 deletions packages/main/src/index.ts → packages/homeserver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ import swagger from "@elysiajs/swagger";
import "@hs/endpoints/src/query";
import "@hs/endpoints/src/server";
import { app } from "./app";
import { config } from "./config";
import { routerWithConfig } from "./plugins/config";
import { routerWithMongodb } from "./plugins/mongodb";

console.log(config);
import { getKeyPair } from "./keys";

app
.use(swagger())
.use(routerWithMongodb)
.use(routerWithConfig)
.get("/", () => "")
.onError(async ({ error, request }) => {
if (!request.body) {
Expand All @@ -25,9 +20,6 @@ app
console.log("body ->", body);

return error;
})
.listen(config.port, () => {
console.log(
`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`,
);
});

export { app, getKeyPair };
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import { authorizationHeaders, computeHash } from "./authentication";
import type { Config } from "./config";
import type { SigningKey } from "./keys";

import { signJson } from "./signJson";

export const makeRequest = async ({
method,
domain,
uri,
options = {},
config,
signingKey,
signingName,
}: {
method: string;
domain: string;
uri: string;
options?: Record<string, any>;
config: Config;
signingKey: SigningKey;
signingName: string;
}) => {
const signingKey = config.signingKey[0];

const body =
options.body &&
(await signJson(
computeHash({ ...options.body, signatures: {} }),
config.signingKey[0],
config.name,
signingKey,
signingName,
));

console.log("body ->", method, domain, uri, body);

const auth = await authorizationHeaders(
config.name,
signingName,
signingKey,
domain,
method,
Expand All @@ -53,18 +54,18 @@ export const makeUnsignedRequest = async ({
domain,
uri,
options = {},
config,
signingKey,
signingName,
}: {
method: string;
domain: string;
uri: string;
options?: Record<string, any>;
config: Config;
signingKey: SigningKey;
signingName: string;
}) => {
const signingKey = config.signingKey[0];

const auth = await authorizationHeaders(
config.name,
signingName,
signingKey,
domain,
method,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import Elysia from "elysia";
import type { InferContext } from "elysia";
import type { SigningKey } from "../keys";

import { config } from "../config";
export interface Config {
path: string;
signingKeyPath: string;
port: number;
signingKey: SigningKey[];
name: string;
version: string;
}

export const routerWithConfig = new Elysia().decorate("config", config);
export const routerWithConfig = new Elysia().decorate("config", {} as Config);

export type Context = InferContext<typeof routerWithConfig>;
67 changes: 67 additions & 0 deletions packages/homeserver/src/plugins/mongodb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Elysia from "elysia";
import type { InferContext } from "elysia";
import { type Db, MongoClient } from "mongodb";

import { NotFoundError } from "elysia";
import type { EventBase } from "../events/eventBase";

export const routerWithMongodb = (db: Db) =>
new Elysia().decorate("mongo", () => {
const eventsCollection = db.collection<EventStore>("events");

const getLastEvent = async (roomId: string) => {
const events = await eventsCollection
.find({ "event.room_id": roomId }, { sort: { "event.depth": -1 } })
.toArray();

if (events.length === 0) {
throw new NotFoundError(`No events found for room ${roomId}`);
}

return events[0];
};

const getAuthEvents = async (roomId: string) => {
return eventsCollection
.find(
{
"event.room_id": roomId,
$or: [
{
"event.type": {
$in: [
"m.room.create",
"m.room.power_levels",
"m.room.join_rules",
],
},
},
{
// Lots of room members, when including the join ones it fails the auth check
"event.type": "m.room.member",
"event.content.membership": "invite",
},
],
},
{
projection: {
_id: 1,
},
},
)
.toArray();
};

return {
eventsCollection,
getLastEvent,
getAuthEvents,
};
});

export type Context = InferContext<ReturnType<typeof routerWithMongodb>>;

export type EventStore = {
_id: string;
event: EventBase;
};
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ export const fakeEndpoints = new Elysia({ prefix: "/fake" })
options: {
body: payload,
},
config,
});

const responseMake = await response.json();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, it } from "bun:test";
import nacl from "tweetnacl";
import { app } from "../../app";
import { fromBinaryData, toBinaryData } from "../../binaryData";
import type { Config } from "../../config";
import type { Config } from "../../../../../config";
import { EncryptionValidAlgorithm } from "../../signJson";
import Elysia from "elysia";
import { generateKeyPairsFromString } from "../../keys";
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
63 changes: 0 additions & 63 deletions packages/main/src/mongodb.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/main/src/plugins/mongodb.ts

This file was deleted.

0 comments on commit f1c1e42

Please sign in to comment.