Hoist up the biome config and use it for syn2mas

This commit is contained in:
Quentin Gliech
2024-10-22 13:48:03 +02:00
parent 1ad2ecb38d
commit 0061c258d1
26 changed files with 273 additions and 4765 deletions

View File

@@ -4,7 +4,7 @@ root = true
charset=utf-8
end_of_line = lf
[*.{ts,tsx,js,cjs,mjs,css,json,graphql}]
[*.{ts,tsx,cts,mts,js,cjs,mjs,css,json,graphql}]
indent_style = space
indent_size = 2
insert_final_newline = true

35
biome.json Normal file
View File

@@ -0,0 +1,35 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"organizeImports": {
"enabled": true
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignore": [
".devcontainer/**",
"docs/**",
"translations/**",
"policies/**",
"crates/**",
"frontend/src/gql/**",
"frontend/src/routeTree.gen.ts",
"**/coverage/**",
"**/dist/**"
]
},
"formatter": {
"enabled": true,
"useEditorconfig": true,
"ignore": ["frontend/.storybook/locales.ts", "frontend/locales/*.json"]
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}

View File

@@ -1,26 +0,0 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"organizeImports": {
"enabled": true
},
"files": {
"ignore": [
"**/dist/**",
"**/__generated__/**",
"**/coverage/**",
"src/gql/**",
"src/routeTree.gen.ts"
]
},
"formatter": {
"enabled": true,
"useEditorconfig": true,
"ignore": [".storybook/locales.ts", "locales/*.json"]
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
}
}

View File

@@ -3,27 +3,30 @@
"readKey": "a7633943728394577700-c0f9f1df124fbdbe76b2c7dfcbfe574476d56509e0da6180e2a321dbbe056c40",
"upload": {
"type": "json",
"files": [{
"file": "file.json",
"pattern": "translations/en.json",
"features": [
"arb_metadata",
"plural_object"
]
}, {
"file": "frontend.json",
"pattern": "frontend/locales/en.json",
"features": ["plural_postfix_dd"]
}]
"files": [
{
"file": "file.json",
"pattern": "translations/en.json",
"features": ["arb_metadata", "plural_object"]
},
{
"file": "frontend.json",
"pattern": "frontend/locales/en.json",
"features": ["plural_postfix_dd"]
}
]
},
"download": {
"metadataFileTs": "frontend/.storybook/locales.ts",
"files": [{
"conditions": "equals: ${file}, file.json",
"output": "translations/${lang}.json"
}, {
"conditions": "equals: ${file}, frontend.json",
"output": "frontend/locales/${lang}.json"
}]
"files": [
{
"conditions": "equals: ${file}, file.json",
"output": "translations/${lang}.json"
},
{
"conditions": "equals: ${file}, frontend.json",
"output": "frontend/locales/${lang}.json"
}
]
}
}

View File

@@ -1,60 +0,0 @@
// Copyright 2024 New Vector Ltd.
// Copyright 2023, 2024 The Matrix.org Foundation C.I.C.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
const HEADER_TEMPLATE = `\
// Copyright %%CURRENT_YEAR%% New Vector Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
`;
/** @type {import('eslint').Linter.Config} */
module.exports = {
root: true,
plugins: ["matrix-org"],
extends: [
"plugin:prettier/recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"plugin:matrix-org/typescript",
],
env: {
browser: false,
node: true,
},
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.eslint.json",
},
rules: {
"matrix-org/require-copyright-header": ["error", HEADER_TEMPLATE],
"import/order": [
"error",
{
"newlines-between": "always",
alphabetize: { order: "asc" },
},
],
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/await-thenable": "error",
// False-positive because of id128 and log4js
"import/no-named-as-default-member": "off",
},
settings: {
"import/parsers": {
"@typescript-eslint/parser": [".ts", ".mts"],
},
"import/resolver": {
typescript: true,
node: true,
},
},
ignorePatterns: ["dist"],
};

File diff suppressed because it is too large Load Diff

View File

@@ -12,13 +12,7 @@
"bin": {
"syn2mas": "dist/index.js"
},
"files": [
"src",
"dist",
"package.json",
"LICENSE",
"README.md"
],
"files": ["src", "dist", "package.json", "LICENSE", "README.md"],
"publishConfig": {
"access": "public"
},
@@ -27,26 +21,16 @@
"build": "tsc",
"dev": "tsx src/index.ts",
"lint": "npm run lint:types && npm run lint:style",
"lint:style": "eslint . .eslintrc.cjs",
"lint:style": "biome check",
"lint:types": "tsc --noEmit",
"start": "node dist/index.js"
},
"devDependencies": {
"@biomejs/biome": "^1.9.4",
"@tsconfig/node22": "^22.0.0",
"@tsconfig/strictest": "^2.0.2",
"@types/command-line-args": "^5.2.2",
"@types/node": "^22.0.0",
"@typescript-eslint/eslint-plugin": "^7.3.1",
"@typescript-eslint/parser": "^7.3.1",
"eslint": "^8.52.0",
"eslint-config-google": "^0.14.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "npm:eslint-plugin-i@^2.28.1",
"eslint-plugin-jest": "^27.9.0",
"eslint-plugin-matrix-org": "^1.2.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-unicorn": "^51.0.1",
"prettier": "^3.0.3",
"tsx": "^4.16.2",
"typescript": "^5.2.2"

View File

@@ -6,15 +6,15 @@
import { readFile } from "node:fs/promises";
import { Knex } from "knex";
import type { Knex } from "knex";
import log4js from "log4js";
import { parse } from "ts-command-line-args";
import yaml from "yaml";
import { connectToSynapseDatabase } from "./db.mjs";
import {
type SynapseOIDCProvider,
synapseConfig as synapseConfigSchema,
SynapseOIDCProvider,
} from "./schemas/synapse.mjs";
import type { SAccessToken } from "./types/SAccessToken.d.ts";
import type { SRefreshToken } from "./types/SRefreshToken.d.ts";

View File

@@ -7,18 +7,18 @@
import { readFile } from "node:fs/promises";
import type { SecureContextOptions } from "node:tls";
import knex, { Knex } from "knex";
import knex, { type Knex } from "knex";
import {
import type {
MASConfig,
DatabaseConfig as MASDatabaseConfig,
URIDatabaseConfig as MASURIDatabaseConfig,
} from "./schemas/mas.mjs";
import { SynapseConfig } from "./schemas/synapse.mjs";
import type { SynapseConfig } from "./schemas/synapse.mjs";
export async function connectToSynapseDatabase({
database,
}: SynapseConfig): Promise<Knex<{}, unknown[]>> {
}: SynapseConfig): Promise<Knex> {
if (!database) {
throw new Error("Synapse database not configured");
}
@@ -32,22 +32,22 @@ export async function connectToSynapseDatabase({
}
const connection: Knex.PgConnectionConfig = {};
database.args.database && (connection.database = database.args.database);
database.args.dbname && (connection.database = database.args.dbname);
database.args.user && (connection.user = database.args.user);
database.args.password && (connection.password = database.args.password);
database.args.host && (connection.host = database.args.host);
typeof database.args.port === "number" &&
(connection.port = database.args.port);
typeof database.args.port === "string" &&
(connection.port = parseInt(database.args.port));
if (database.args.database) connection.database = database.args.database;
if (database.args.dbname) connection.database = database.args.dbname;
if (database.args.user) connection.user = database.args.user;
if (database.args.password) connection.password = database.args.password;
if (database.args.host) connection.host = database.args.host;
if (typeof database.args.port === "number")
connection.port = database.args.port;
if (typeof database.args.port === "string")
connection.port = Number.parseInt(database.args.port);
const ssl: SecureContextOptions = {};
database.args.sslcert && (ssl.cert = await readFile(database.args.sslcert));
database.args.sslrootcert &&
(ssl.ca = await readFile(database.args.sslrootcert));
database.args.sslkey && (ssl.key = await readFile(database.args.sslkey));
database.args.sslpassword && (ssl.passphrase = database.args.sslpassword);
if (database.args.sslcert) ssl.cert = await readFile(database.args.sslcert);
if (database.args.sslrootcert)
ssl.ca = await readFile(database.args.sslrootcert);
if (database.args.sslkey) ssl.key = await readFile(database.args.sslkey);
if (database.args.sslpassword) ssl.passphrase = database.args.sslpassword;
if (Object.keys(ssl).length > 0) {
connection.ssl = ssl;
@@ -62,21 +62,21 @@ export async function connectToSynapseDatabase({
const isUriConfig = (
database: MASDatabaseConfig,
): database is MASURIDatabaseConfig =>
typeof (database as Record<string, unknown>)["uri"] === "string";
"uri" in database && typeof database.uri === "string";
export async function connectToMASDatabase({
database,
}: MASConfig): Promise<Knex<{}, unknown[]>> {
}: MASConfig): Promise<Knex> {
const connection: Knex.PgConnectionConfig = {};
const ssl: SecureContextOptions = {};
if (isUriConfig(database)) {
connection.connectionString = database.uri;
} else {
database.database && (connection.database = database.database);
database.username && (connection.user = database.username);
database.password && (connection.password = database.password);
database.host && (connection.host = database.host);
database.port && (connection.port = database.port);
if (database.database) connection.database = database.database;
if (database.username) connection.user = database.username;
if (database.password) connection.password = database.password;
if (database.host) connection.host = database.host;
if (database.port) connection.port = database.port;
}
if (database.ssl_ca) {

View File

@@ -6,7 +6,7 @@
// Please see LICENSE in the repository root for full details.
import log4js from "log4js";
import { ArgumentConfig, parse } from "ts-command-line-args";
import { type ArgumentConfig, parse } from "ts-command-line-args";
import { advisor } from "./advisor.mjs";
import { migrate } from "./migrate.mjs";

View File

@@ -11,7 +11,7 @@ import log4js from "log4js";
import { parse } from "ts-command-line-args";
import yaml from "yaml";
import { connectToSynapseDatabase, connectToMASDatabase } from "./db.mjs";
import { connectToMASDatabase, connectToSynapseDatabase } from "./db.mjs";
import { masConfig as masConfigSchema } from "./schemas/mas.mjs";
import { synapseConfig as synapseConfigSchema } from "./schemas/synapse.mjs";
import type { MCompatAccessToken } from "./types/MCompatAccessToken.d.ts";
@@ -104,7 +104,7 @@ export async function migrate(): Promise<void> {
let fatals = 0;
function fatal(message: string): void {
log.fatal(message);
warnings.forEach((w) => log.warn(w));
for (const w of warnings) log.warn(w);
if (!args.dryRun) {
process.exit(1);
}
@@ -186,7 +186,7 @@ export async function migrate(): Promise<void> {
.from("users")
.first();
if (parseInt(`${existingMasUsers?.count ?? 0}`) > 0) {
if (Number.parseInt(`${existingMasUsers?.count ?? 0}`) > 0) {
fatal(
`Found ${existingMasUsers?.count} existing users in MAS. Refusing to continue. Please clean MAS and try again.`,
);
@@ -204,14 +204,16 @@ export async function migrate(): Promise<void> {
}
// users => users
const userCreatedAt = new Date(parseInt(`${user.creation_ts}`) * 1000);
const userCreatedAt = new Date(
Number.parseInt(`${user.creation_ts}`) * 1000,
);
const masUser = {
user_id: makeUuid(userCreatedAt),
username: localpart,
created_at: userCreatedAt,
locked_at: user.deactivated === 1 ? userCreatedAt : null,
};
executions.push(() => mas.insert(masUser!).into("users"));
executions.push(() => mas.insert(masUser).into("users"));
log.debug(`${stringifyAndRedact(user)} => ${stringifyAndRedact(masUser)}`);
// users.password_hash => user_passwords
if (user.password_hash) {
@@ -245,7 +247,9 @@ export async function migrate(): Promise<void> {
);
continue;
}
const threePidCreatedAt = new Date(parseInt(`${threePid.added_at}`));
const threePidCreatedAt = new Date(
Number.parseInt(`${threePid.added_at}`),
);
const masUserEmail: MUserEmail = {
user_email_id: makeUuid(threePidCreatedAt),
user_id: masUser.user_id,
@@ -255,7 +259,7 @@ export async function migrate(): Promise<void> {
if (threePid.validated_at) {
masUserEmail.confirmed_at = new Date(
parseInt(`${threePid.validated_at}`),
Number.parseInt(`${threePid.validated_at}`),
);
}
@@ -275,8 +279,8 @@ export async function migrate(): Promise<void> {
);
executions.push(() =>
mas("users")
.where({ user_id: masUser!.user_id })
.update({ primary_user_email_id: primaryEmail!.user_email_id }),
.where({ user_id: masUser?.user_id })
.update({ primary_user_email_id: primaryEmail?.user_email_id }),
);
}
@@ -287,12 +291,12 @@ export async function migrate(): Promise<void> {
.where({ user_id: user.name });
for (const externalId of synapseExternalIds) {
try {
if (!upstreamProviders.has(externalId.auth_provider)) {
const provider = upstreamProviders.get(externalId.auth_provider);
if (!provider) {
throw new Error(
`Unknown upstream provider ${externalId.auth_provider}`,
);
}
const provider = upstreamProviders.get(externalId.auth_provider)!;
const masUpstreamOauthLink: MUpstreamOauthLink = {
upstream_oauth_link_id: makeUuid(userCreatedAt),
user_id: masUser.user_id,
@@ -333,7 +337,7 @@ export async function migrate(): Promise<void> {
.whereNotNull("device_id");
for (const accessToken of synapseAccessTokens) {
const tokenCreatedAt = accessToken.last_validated
? new Date(parseInt(`${accessToken.last_validated}`))
? new Date(Number.parseInt(`${accessToken.last_validated}`))
: masUser.created_at;
const masCompatSession: MCompatSession = {
compat_session_id: makeUuid(tokenCreatedAt),
@@ -462,7 +466,7 @@ export async function migrate(): Promise<void> {
log.info(
`Completed migration ${args.dryRun ? "dry-run " : ""}of ${synapseUsers} users with ${fatals} fatals and ${warnings.length} warnings:`,
);
warnings.forEach((w) => log.warn(w));
for (const w of warnings) log.warn(w);
if (fatals > 0) {
throw new Error(`Migration failed with ${fatals} fatals`);
}

View File

@@ -4,9 +4,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { MCompatSession } from "./MCompatSession";
import type { MCompatSession } from "./MCompatSession";
import { UUID } from "./index";
import type { UUID } from "./index";
/*
+------------------------+--------------------------+-----------+

View File

@@ -4,10 +4,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { MCompatAccessToken } from "./MCompatAccessToken";
import { MCompatSession } from "./MCompatSession";
import type { MCompatAccessToken } from "./MCompatAccessToken";
import type { MCompatSession } from "./MCompatSession";
import { UUID } from "./index";
import type { UUID } from "./index";
/*
+-------------------------+--------------------------+-----------+

View File

@@ -4,9 +4,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { MUser } from "./MUser";
import type { MUser } from "./MUser";
import { UUID } from "./index";
import type { UUID } from "./index";
/*
+-------------------+--------------------------+-----------+

View File

@@ -4,10 +4,10 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { MUpstreamOauthProvider } from "./MUpstreamOauthProvider";
import { MUser } from "./MUser";
import type { MUpstreamOauthProvider } from "./MUpstreamOauthProvider";
import type { MUser } from "./MUser";
import { UUID } from "./index";
import type { UUID } from "./index";
/*
+----------------------------+--------------------------+-----------+

View File

@@ -4,7 +4,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { UUID } from "./index";
import type { UUID } from "./index";
/*
+----------------------------+--------------------------+-----------+

View File

@@ -4,9 +4,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { MUserEmail } from "./MUserEmail";
import type { MUserEmail } from "./MUserEmail";
import { UUID } from "./index";
import type { UUID } from "./index";
export interface MUser {
user_id: UUID<MUser>;

View File

@@ -4,9 +4,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { MUser } from "./MUser";
import type { MUser } from "./MUser";
import { UUID } from "./index";
import type { UUID } from "./index";
/*
+---------------+--------------------------+-----------+

View File

@@ -4,9 +4,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { MUser } from "./MUser";
import type { MUser } from "./MUser";
import { UUID } from "./index";
import type { UUID } from "./index";
export interface MUserPassword {
user_password_id: UUID<MUserPassword>;

View File

@@ -4,9 +4,9 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { SRefreshToken } from "./SRefreshToken";
import type { SRefreshToken } from "./SRefreshToken";
import { Id, SynapseUserId } from "./index";
import type { Id, SynapseUserId } from "./index";
/*
CREATE TABLE access_tokens (

View File

@@ -4,7 +4,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { Id, SynapseUserId } from "./index";
import type { Id, SynapseUserId } from "./index";
/*
);

View File

@@ -4,7 +4,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { SynapseUserId, UnixTimestamp } from "./index";
import type { SynapseUserId, UnixTimestamp } from "./index";
export interface SUser {
name: SynapseUserId; // '@test2:localhost:8008'

View File

@@ -4,7 +4,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { SynapseUserId } from "./index";
import type { SynapseUserId } from "./index";
export interface SUserExternalId {
auth_provider: string;

View File

@@ -4,7 +4,7 @@
// SPDX-License-Identifier: AGPL-3.0-only
// Please see LICENSE in the repository root for full details.
import { SynapseUserId } from "./index";
import type { SynapseUserId } from "./index";
/*
CREATE TABLE user_threepids (

View File

@@ -1,15 +1,11 @@
{
"extends": [
"@tsconfig/strictest/tsconfig.json",
"@tsconfig/node22/tsconfig.json",
"@tsconfig/node22/tsconfig.json"
],
"compilerOptions": {
"noEmit": true,
"allowJs": true,
"allowJs": true
},
"include": [
".eslintrc.cjs",
"src/**/*.mts",
"src/**/*.ts"
]
"include": [".eslintrc.cjs", "src/**/*.mts", "src/**/*.ts"]
}

View File

@@ -7,6 +7,6 @@
"outDir": "dist",
"rootDir": "src",
"sourceMap": true,
"declaration": false,
"declaration": false
}
}
}