Skip to content
This repository was archived by the owner on Sep 10, 2024. It is now read-only.

Commit 0f5d10f

Browse files
committed
syn2mas: typescript fixes
1 parent af552bf commit 0f5d10f

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

tools/syn2mas/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/syn2mas/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"eslint-config-google": "^0.14.0",
2222
"eslint-config-prettier": "^9.0.0",
2323
"eslint-plugin-import": "^2.28.1",
24-
"eslint-plugin-matrix-org": "github:matrix-org/eslint-plugin-matrix-org",
24+
"eslint-plugin-matrix-org": "^1.2.1",
2525
"eslint-plugin-unicorn": "^48.0.1",
2626
"prettier": "^3.0.3",
2727
"ts-node": "^10.9.1",

tools/syn2mas/src/db.mts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
import knex, { Knex } from "knex";
22

3-
import { SynapseConfig } from "./types/SynapseConfig";
3+
import { DatabaseConfig, Psycopg2DatabaseConfig, Sqlite3DatabaseConfig, SynapseConfig } from "./types/SynapseConfig";
44
import { MASConfig } from "./types/MASConfig";
55

6+
function isSqlite3(config: DatabaseConfig): config is Sqlite3DatabaseConfig {
7+
return config.name === "sqlite3";
8+
}
9+
10+
function isPsycopg2(config: DatabaseConfig): config is Psycopg2DatabaseConfig {
11+
return config.name === "psycopg2";
12+
}
13+
614
export function connectToSynapseDatabase({ database }: SynapseConfig): Knex<{}, unknown[]> {
7-
if (database?.name === "sqlite3") {
8-
return knex({ client: "sqlite3", connection: { filename: database.args.database }, useNullAsDefault: true });
15+
if (!database) {
16+
throw new Error("Synapse database not configured");
17+
}
18+
19+
if (isSqlite3(database)) {
20+
const filename = database.args?.database;
21+
if (!filename) {
22+
throw new Error("Synapse sqlite3 database not configured");
23+
}
24+
25+
return knex({ client: "sqlite3", connection: { filename }, useNullAsDefault: true });
926
}
1027

11-
if (database.name === "psycopg2") {
28+
if (isPsycopg2(database)) {
1229
return knex({ client: "pg", connection: {
1330
user: database?.args?.user,
1431
database: database?.args?.database,

tools/syn2mas/src/migrate.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export async function migrate(argv?: string[]): Promise<void> {
9595
upstreamProviders.set(providerId, existingProvider);
9696
}
9797

98-
function stringifyAndRedact(input: any): string {
98+
function stringifyAndRedact(input: unknown): string {
9999
const x = JSON.stringify(input);
100100

101101
return x.replace(/("(password_hash|hashed_password|access_token|token)":")[^"]*"/, "$1redacted\"");

tools/syn2mas/src/types/SynapseConfig.d.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,28 @@ export interface SynapseOIDCProvider {
99
client_secret_jwt_key?: string;
1010
}
1111

12-
export interface SynapseConfig {
13-
database?: {
12+
export interface Sqlite3DatabaseConfig {
1413
name: "sqlite3";
1514
args?: {
16-
database: string;
17-
};
18-
} | {
19-
name: "psycopg2";
20-
args?: {
21-
user?: string;
22-
password?: string;
23-
database?: string;
24-
host?: string;
25-
port?: number;
15+
database: string;
2616
};
27-
} | any;
17+
}
2818

19+
export interface Psycopg2DatabaseConfig {
20+
name: "psycopg2";
21+
args?: {
22+
user?: string;
23+
password?: string;
24+
database?: string;
25+
host?: string;
26+
port?: number;
27+
};
28+
}
29+
30+
export type DatabaseConfig = Sqlite3DatabaseConfig | Psycopg2DatabaseConfig | { name?: Omit<string, "sqlite3" | "psycopg2"> };
31+
32+
export interface SynapseConfig {
33+
database?: DatabaseConfig;
2934
oidc_providers?: SynapseOIDCProvider[];
3035
oidc_config?: SynapseOIDCProvider;
3136
allow_guest_access?: boolean;

0 commit comments

Comments
 (0)