From 1d08ea2288645751b4715356ed3c69fdb7365df0 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Sat, 7 Dec 2024 16:50:51 -0300 Subject: [PATCH] split fake routes --- bun.lockb | Bin 64912 -> 64960 bytes index.ts | 2 + package.json | 3 +- packages/fake/.gitignore | 44 ++++++++ packages/fake/README.md | 19 ++++ packages/fake/package.json | 18 +++ packages/fake/src/index.ts | 1 + .../src/routes/fake => fake/src}/room.ts | 44 +++++--- packages/fake/tsconfig.json | 105 ++++++++++++++++++ packages/homeserver/package.json | 2 +- packages/homeserver/src/app.ts | 2 - packages/homeserver/src/plugins/mongodb.ts | 91 +++++++-------- .../src/routes/federation/invite.ts | 6 +- 13 files changed, 272 insertions(+), 65 deletions(-) create mode 100644 packages/fake/.gitignore create mode 100644 packages/fake/README.md create mode 100644 packages/fake/package.json create mode 100644 packages/fake/src/index.ts rename packages/{homeserver/src/routes/fake => fake/src}/room.ts (87%) create mode 100644 packages/fake/tsconfig.json diff --git a/bun.lockb b/bun.lockb index 41fc4180a114750dfd5919f2eeeff4d4184b484e..8a094a193c9ae49912a2fa8b55996c498ed427b2 100755 GIT binary patch delta 9254 zcmeHNdstP~wqJ9@Mi#Jn3oN(B!?)%Z*y3i3xInE`@RbkLEDtCmDsRxXrhryr<*RYZ zN=-8>6)hA|5l!%sk4#IurI~uuvOK-)bUTS=o!?k%uk_lf?|%33efN*%_x;vyj`Bui4oT5u3JMM#n#xFkDoMs~3z<&_`<;cKM(BKqS&~8|sl4JxWVAxXyn_6kf=O9YD=$fE1Nj#)8#Jxx3f_yHQJ>6B3i?$P?#Derl zup*U96%z{zrsZZ!l0Oo-!GS334{i6;7OzSv)DbR62wT zEboGU!@+NWc_>~*I+uS5Rx>xa%XNf%SYHifF1ONM?lYt#TH%1-IlUl1yL51FUbd8% zo0mJW+vI#gu4V}Q$OVeP+`~pNH}HfzztYX6Q;G}9a;8X=b92gyvrDAQ^qwhPY^5uv zj>>nil!xOIm>c~N%$+z!ojp1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Dk!AJL0 z-~jaLo*d2fnu}s+^j^hoUKY;ToPD~ZYmmn%>crcNXs<(*(UHCNI8IdWQ-nVq->2Bk zi@ooTd?I%rHh@<+cN^xlpLK7I- z*tmF!S9*@V-t}ET-vR7*FWB2_Kc3Wsib$d12P?!ux^NKNYP3GHPA_}ohqGt)e5G{& zgM+s=1s+lo%zMu2S0V>*@230(`Npy~Z$cek4;k;%XVQzvaq4p}ltvy>?B>ns+%=cl z1@3)f81k{Gc|++zD-J0ljW$8cymyVs%(OlFY|>ss3+?C7MQG`|nyu9NaG2e^MxF6h z&dG&Mu41b2M0cQ8cCw;7+-_cye%|1i^jT{!i#N1fyv(HC^!8yT!Ms!r{Qmjp!h4-w zdpqB}c2#3G_)h$!)i<|uEOhIzVmGg8cfXmYw#ZCeb35NbaYqy}kkXGR7JX}$mmkrm zQ4r-F>5Z2vy>%pBT%=1!!Xxc?eFtGm9(}6j=7EPYUhf3XuanCqvvY&S^}CQjkw?Qt zI;G+{g0hZ=m)Rrqk$QGp%grGRHn(jD&Xan`_#uxqn!9Cx7jS+Nm90wp=*s>9ulG0f z-JWe7GEQ7Op9zjVuYBPBnWu{OyOxbN@)KoOW#y&^Vy77Sn?IcvoZ82JtQ&*lvKGC2~Cjj{DOV29j%T#wH7=CeB=9Hzg#uC{;FDo~{cKfl}ve%f>e0uz9Bq`rEtIhpjkf^OY zyS|+~;b*;qaw__{P1KgTIpMatE#H{uBl9$L)X*Aslk$()>DX!L_vFW|y$L Z3tTaBrCVBoA8&S%gJHIIrox>9{S#mnPqqL6 diff --git a/index.ts b/index.ts index 91a31c6e..90ee88b3 100644 --- a/index.ts +++ b/index.ts @@ -36,6 +36,7 @@ import Elysia from "elysia"; import { app } from "homeserver"; +import { fakeEndpoints } from "fake"; import { routerWithMongodb } from "homeserver/src/plugins/mongodb"; import { config } from "./config"; @@ -45,6 +46,7 @@ new Elysia() .decorate("config", config) .use(routerWithMongodb(db)) .use(app) + .use(fakeEndpoints) .listen(config.port, (context) => { console.log( `🦊 Elysia is running at http://${context.hostname}:${context.port}`, diff --git a/package.json b/package.json index 7d96f933..294f1d96 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "@bogeychan/elysia-logger": "^0.1.4", "@elysiajs/swagger": "^1.1.6", "elysia": "^1.1.26", - "homeserver": "workspace:*", + "@hs/homeserver": "workspace:*", + "@hs/fake": "workspace:*", "mongodb": "^6.11.0", "node-jsonwebtoken": "^0.0.1", "tweetnacl": "^1.0.3" diff --git a/packages/fake/.gitignore b/packages/fake/.gitignore new file mode 100644 index 00000000..df0219a5 --- /dev/null +++ b/packages/fake/.gitignore @@ -0,0 +1,44 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# next.js +/.next/ +/out/ + +# production +/build + +# misc +.DS_Store +*.pem + +# debug +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# local env files +.env.local +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel + +**/*.trace +**/*.zip +**/*.tar.gz +**/*.tgz +**/*.log +package-lock.json +**/*.bun + +*.key \ No newline at end of file diff --git a/packages/fake/README.md b/packages/fake/README.md new file mode 100644 index 00000000..bebf0610 --- /dev/null +++ b/packages/fake/README.md @@ -0,0 +1,19 @@ +# Elysia with Bun runtime + +## Getting Started + +To get started with this template, simply paste this command into your terminal: + +```bash +bun create elysia ./elysia-example +``` + +## Development + +To start the development server run: + +```bash +bun run dev +``` + +Open http://localhost:3000/ with your browser to see the result. diff --git a/packages/fake/package.json b/packages/fake/package.json new file mode 100644 index 00000000..ff54990f --- /dev/null +++ b/packages/fake/package.json @@ -0,0 +1,18 @@ +{ + "name": "@hs/fake", + "version": "1.0.50", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "dev": "bun run --watch src/index.ts" + }, + "dependencies": { + "@hs/endpoints": "workspace:*", + "@hs/homeserver": "workspace:*", + "elysia": "latest" + }, + "devDependencies": { + "bun-types": "latest" + }, + "main": "./src/index.ts", + "types": "./src/index.ts" +} diff --git a/packages/fake/src/index.ts b/packages/fake/src/index.ts new file mode 100644 index 00000000..fb317351 --- /dev/null +++ b/packages/fake/src/index.ts @@ -0,0 +1 @@ +export * from "./room"; diff --git a/packages/homeserver/src/routes/fake/room.ts b/packages/fake/src/room.ts similarity index 87% rename from packages/homeserver/src/routes/fake/room.ts rename to packages/fake/src/room.ts index 554817a7..bc9feffb 100644 --- a/packages/homeserver/src/routes/fake/room.ts +++ b/packages/fake/src/room.ts @@ -4,15 +4,15 @@ import "@hs/endpoints/src/query"; import "@hs/endpoints/src/server"; import Crypto from "node:crypto"; -import { generateId } from "../../authentication"; -import { roomMemberEvent } from "../../events/m.room.member"; -import { makeUnsignedRequest } from "../../makeRequest"; -import { signEvent } from "../../signEvent"; -import type { EventBase } from "../../events/eventBase"; -import { createSignedEvent } from "../../events/utils/createSignedEvent"; -import { createRoom } from "../../procedures/createRoom"; -import { isConfigContext } from "../../plugins/isConfigContext"; -import { isMongodbContext } from "../../plugins/isMongodbContext"; +import { generateId } from "@hs/homeserver/src/authentication"; +import { isConfigContext } from "@hs/homeserver/src/plugins/isConfigContext"; +import { isMongodbContext } from "@hs/homeserver/src/plugins/isMongodbContext"; +import { createRoom } from "@hs/homeserver/src/procedures/createRoom"; +import { createSignedEvent } from "@hs/homeserver/src/events/utils/createSignedEvent"; +import { signEvent } from "@hs/homeserver/src/signEvent"; +import { roomMemberEvent } from "@hs/homeserver/src/events/m.room.member"; +import { makeUnsignedRequest } from "@hs/homeserver/src/makeRequest"; +import type { EventBase } from "@hs/homeserver/src/events/eventBase"; function createMediaId(length: number) { const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -159,18 +159,21 @@ export const fakeEndpoints = new Elysia({ prefix: "/fake" }) age: 4, // TODO: Check what this is invite_room_state: [ { + // @ts-ignore content: {}, sender: events[0].event.sender, state_key: "", type: "m.room.join_rules", }, { + // @ts-ignore content: {}, sender: events[0].event.sender, state_key: "", type: "m.room.create", }, { + // @ts-ignore content: {}, sender: events[0].event.sender, state_key: events[0].event.sender, @@ -180,6 +183,7 @@ export const fakeEndpoints = new Elysia({ prefix: "/fake" }) }, }), config.signingKey[0], + config.name, ); const inviteEventId = generateId(inviteEvent); @@ -206,7 +210,8 @@ export const fakeEndpoints = new Elysia({ prefix: "/fake" }) options: { body: payload, }, - config, + signingKey: config.signingKey[0], + signingName: config.name, }); console.log(response.status); @@ -250,12 +255,16 @@ export const fakeEndpoints = new Elysia({ prefix: "/fake" }) if (!isConfigContext(context)) { throw new Error("No config context"); } - const { config } = context; + if (!isMongodbContext(context)) { + throw new Error("No mongodb context"); + } + const { + config, + mongo: { eventsCollection }, + } = context; const { sender, roomId, msg, target } = body; - const { eventsCollection } = await import("../../mongodb"); - const create = await eventsCollection.findOne({ "event.room_id": roomId, "event.type": "m.room.create", @@ -311,7 +320,11 @@ export const fakeEndpoints = new Elysia({ prefix: "/fake" }) sender, }; - const signedEvent = await signEvent(event, config.signingKey[0]); + const signedEvent = await signEvent( + event, + config.signingKey[0], + config.name, + ); const eventId = generateId(signedEvent); await eventsCollection.insertOne({ @@ -333,7 +346,8 @@ export const fakeEndpoints = new Elysia({ prefix: "/fake" }) options: { body: payload, }, - config, + signingKey: config.signingKey[0], + signingName: config.name, }); const responseMake = await response.json(); diff --git a/packages/fake/tsconfig.json b/packages/fake/tsconfig.json new file mode 100644 index 00000000..4b712b47 --- /dev/null +++ b/packages/fake/tsconfig.json @@ -0,0 +1,105 @@ +{ + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ + "composite": true /* Enable constraints that allow a TypeScript project to be used with project references. */, + // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ + // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ + // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ + // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ + + /* Language and Environment */ + "target": "ES2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ + // "jsx": "preserve", /* Specify what JSX code is generated. */ + // "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */ + // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ + // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ + // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ + // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ + // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ + // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ + // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ + // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ + + /* Modules */ + "module": "ES2022" /* Specify what module code is generated. */, + // "rootDir": "./", /* Specify the root folder within your source files. */ + "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, + // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ + // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ + // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ + // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ + "types": [ + "bun-types" + ] /* Specify type package names to be included without being referenced in a source file. */, + // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ + // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ + // "resolveJsonModule": true, /* Enable importing .json files. */ + // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ + + /* JavaScript Support */ + // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ + // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ + // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ + + /* Emit */ + "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */, + // "declarationMap": true, /* Create sourcemaps for d.ts files. */ + // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ + "sourceMap": true /* Create source map files for emitted JavaScript files. */, + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + // "outDir": "./", /* Specify an output folder for all emitted files. */ + // "removeComments": true, /* Disable emitting comments. */ + // "noEmit": true, /* Disable emitting files from a compilation. */ + // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ + // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ + // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ + // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ + // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ + // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ + // "newLine": "crlf", /* Set the newline character for emitting files. */ + // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ + // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ + // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ + // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ + // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ + // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ + + /* Interop Constraints */ + // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ + // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, + // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ + // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ + // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ + // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ + // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ + "useUnknownInCatchVariables": true /* Default catch clause variables as 'unknown' instead of 'any'. */, + // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ + // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ + "noUnusedParameters": true /* Raise an error when a function parameter isn't read. */, + // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ + "noImplicitReturns": false /* Enable error reporting for codepaths that do not explicitly return in a function. */, + // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ + // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ + // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ + // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ + "allowUnusedLabels": false /* Disable error reporting for unused labels. */, + "allowUnreachableCode": false /* Disable error reporting for unreachable code. */, + + /* Completeness */ + // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} diff --git a/packages/homeserver/package.json b/packages/homeserver/package.json index 850b614e..b823830e 100644 --- a/packages/homeserver/package.json +++ b/packages/homeserver/package.json @@ -1,5 +1,5 @@ { - "name": "homeserver", + "name": "@hs/homeserver", "version": "1.0.50", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", diff --git a/packages/homeserver/src/app.ts b/packages/homeserver/src/app.ts index 325e00e0..c904b329 100644 --- a/packages/homeserver/src/app.ts +++ b/packages/homeserver/src/app.ts @@ -2,7 +2,6 @@ import { logger } from "@bogeychan/elysia-logger"; import { Elysia } from "elysia"; import { BadJSONError, MatrixError } from "./errors"; -import { fakeEndpoints } from "./routes/fake/room"; import federationEndpoints from "./routes/federation"; import { keyV2Endpoints } from "./routes/key/server"; @@ -31,7 +30,6 @@ export const app = new Elysia({ ) .use(keyV2Endpoints) .use(federationEndpoints) - .use(fakeEndpoints) .onError(async ({ code }) => { if (code === "NOT_FOUND") { return { diff --git a/packages/homeserver/src/plugins/mongodb.ts b/packages/homeserver/src/plugins/mongodb.ts index 838e9ffb..656cd3fe 100644 --- a/packages/homeserver/src/plugins/mongodb.ts +++ b/packages/homeserver/src/plugins/mongodb.ts @@ -6,58 +6,61 @@ import { NotFoundError } from "elysia"; import type { EventBase } from "../events/eventBase"; export const routerWithMongodb = (db: Db) => - new Elysia().decorate("mongo", () => { - const eventsCollection = db.collection("events"); + new Elysia().decorate( + "mongo", + (() => { + const eventsCollection = db.collection("events"); - const getLastEvent = async (roomId: string) => { - const events = await eventsCollection - .find({ "event.room_id": roomId }, { sort: { "event.depth": -1 } }) - .toArray(); + 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}`); - } + if (events.length === 0) { + throw new NotFoundError(`No events found for room ${roomId}`); + } - return events[0]; - }; + 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", - ], + 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, }, - { - // 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(); - }; + ) + .toArray(); + }; - return { - eventsCollection, - getLastEvent, - getAuthEvents, - }; - }); + return { + eventsCollection, + getLastEvent, + getAuthEvents, + }; + })(), + ); export type Context = InferContext>; diff --git a/packages/homeserver/src/routes/federation/invite.ts b/packages/homeserver/src/routes/federation/invite.ts index 9577d3f8..806b31bc 100644 --- a/packages/homeserver/src/routes/federation/invite.ts +++ b/packages/homeserver/src/routes/federation/invite.ts @@ -38,7 +38,8 @@ export const inviteEndpoint = new Elysia().put( method: "GET", domain: event.origin, uri: `/_matrix/federation/v1/make_join/${params.roomId}/${event.state_key}?ver=10`, - config, + signingKey: config.signingKey[0], + signingName: config.name, }); const responseMake = await response.json(); @@ -77,7 +78,8 @@ export const inviteEndpoint = new Elysia().put( options: { body: joinBody, }, - config, + signingKey: config.signingKey[0], + signingName: config.name, }); const responseBody = await responseSend.json();