From 5eff78c1da00f0569135029fb3fcf37446242e49 Mon Sep 17 00:00:00 2001 From: Sylvie Date: Mon, 27 Nov 2023 10:32:30 -0700 Subject: [PATCH] refactor: update all imports for ESM --- backend/UserConfig.ts | 2 +- backend/app.ts | 16 +++--- backend/data.ts | 10 ++-- backend/routers/_frontend.ts | 4 +- backend/routers/api.ts | 18 +++---- backend/routers/index.ts | 14 ++--- backend/s3.ts | 4 +- backend/sql/json.ts | 102 +++++++++++++++++------------------ backend/sql/mysql.ts | 12 ++--- backend/sql/postgres.ts | 61 +++++++++++---------- common/fix-frontend-js.js | 7 +-- 11 files changed, 125 insertions(+), 125 deletions(-) diff --git a/backend/UserConfig.ts b/backend/UserConfig.ts index 735088c..fa16b37 100644 --- a/backend/UserConfig.ts +++ b/backend/UserConfig.ts @@ -2,7 +2,7 @@ import { UserConfiguration, UserConfigTypeChecker, PostgresConfiguration } from import fs from 'fs-extra'; import { path } from '@tycrek/joint'; -import { log } from './log'; +import { log } from './log.js'; const FILEPATH = path.join('.ass-data/userconfig.json'); diff --git a/backend/app.ts b/backend/app.ts index 45afb53..8583019 100644 --- a/backend/app.ts +++ b/backend/app.ts @@ -8,14 +8,14 @@ import express, { Request, Response, NextFunction, RequestHandler, json as BodyP import { path, isProd } from '@tycrek/joint'; import { epcss } from '@tycrek/express-postcss'; -import { log } from './log'; -import { get } from './data'; -import { UserConfig } from './UserConfig'; -import { DBManager } from './sql/database'; -import { JSONDatabase } from './sql/json'; -import { MySQLDatabase } from './sql/mysql'; -import { PostgreSQLDatabase } from './sql/postgres'; -import { buildFrontendRouter } from './routers/_frontend'; +import { log } from './log.js'; +import { get } from './data.js'; +import { UserConfig } from './UserConfig.js'; +import { DBManager } from './sql/database.js'; +import { JSONDatabase } from './sql/json.js'; +import { MySQLDatabase } from './sql/mysql.js'; +import { PostgreSQLDatabase } from './sql/postgres.js'; +import { buildFrontendRouter } from './routers/_frontend.js'; /** * Top-level metadata exports diff --git a/backend/data.ts b/backend/data.ts index 118105f..fb08ec0 100644 --- a/backend/data.ts +++ b/backend/data.ts @@ -1,8 +1,8 @@ import { AssFile, AssUser, NID } from 'ass'; -import { log } from './log'; -import { UserConfig } from './UserConfig'; -import { DBManager } from './sql/database'; +import { log } from './log.js'; +import { UserConfig } from './UserConfig.js'; +import { DBManager } from './sql/database.js'; /** * Switcher type for exported functions @@ -13,9 +13,9 @@ type DataSector = 'files' | 'users'; * database kind -> name mapping */ const DBNAMES = { - 'mysql': 'MySQL', + 'mysql': 'MySQL', 'postgres': 'PostgreSQL', - 'json': 'JSON' + 'json': 'JSON' }; export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Promise => new Promise(async (resolve, reject) => { diff --git a/backend/routers/_frontend.ts b/backend/routers/_frontend.ts index 13eaf53..729bbda 100644 --- a/backend/routers/_frontend.ts +++ b/backend/routers/_frontend.ts @@ -1,8 +1,8 @@ import { Router } from 'express'; import { path } from '@tycrek/joint'; -import { App } from '../app'; -import { UserConfig } from '../UserConfig'; +import { App } from '../app.js'; +import { UserConfig } from '../UserConfig.js'; /** * Builds a basic router for loading a page with frontend JS diff --git a/backend/routers/api.ts b/backend/routers/api.ts index d5bad9b..818f904 100644 --- a/backend/routers/api.ts +++ b/backend/routers/api.ts @@ -3,15 +3,15 @@ import { AssUser, AssUserNewReq } from 'ass'; import * as bcrypt from 'bcrypt' import { Router, json as BodyParserJson, RequestHandler } from 'express'; -import * as data from '../data'; -import { log } from '../log'; -import { nanoid } from '../generators'; -import { UserConfig } from '../UserConfig'; -import { rateLimiterMiddleware, setRateLimiter } from '../ratelimit'; -import { DBManager } from '../sql/database'; -import { JSONDatabase } from '../sql/json'; -import { MySQLDatabase } from '../sql/mysql'; -import { PostgreSQLDatabase } from '../sql/postgres'; +import * as data from '../data.js'; +import { log } from '../log.js'; +import { nanoid } from '../generators.js'; +import { UserConfig } from '../UserConfig.js'; +import { rateLimiterMiddleware, setRateLimiter } from '../ratelimit.js'; +import { DBManager } from '../sql/database.js'; +import { JSONDatabase } from '../sql/json.js'; +import { MySQLDatabase } from '../sql/mysql.js'; +import { PostgreSQLDatabase } from '../sql/postgres.js'; const router = Router({ caseSensitive: true }); diff --git a/backend/routers/index.ts b/backend/routers/index.ts index eed8d13..b62aa59 100644 --- a/backend/routers/index.ts +++ b/backend/routers/index.ts @@ -6,13 +6,13 @@ import crypto from 'crypto'; import { Router } from 'express'; import { Readable } from 'stream'; -import * as data from '../data'; -import { log } from '../log'; -import { App } from '../app'; -import { random } from '../generators'; -import { UserConfig } from '../UserConfig'; -import { getFileS3, uploadFileS3 } from '../s3'; -import { rateLimiterMiddleware } from '../ratelimit'; +import * as data from '../data.js'; +import { log } from '../log.js'; +import { App } from '../app.js'; +import { random } from '../generators.js'; +import { UserConfig } from '../UserConfig.js'; +import { getFileS3, uploadFileS3 } from '../s3.js'; +import { rateLimiterMiddleware } from '../ratelimit.js'; const router = Router({ caseSensitive: true }); diff --git a/backend/s3.ts b/backend/s3.ts index 16c39ef..31fe298 100644 --- a/backend/s3.ts +++ b/backend/s3.ts @@ -12,8 +12,8 @@ import { AbortMultipartUploadCommand, } from "@aws-sdk/client-s3"; -import { log } from './log'; -import { UserConfig } from './UserConfig'; +import { log } from './log.js'; +import { UserConfig } from './UserConfig.js'; const NYR = 'S3 not ready'; diff --git a/backend/sql/json.ts b/backend/sql/json.ts index 173058f..9a2233f 100644 --- a/backend/sql/json.ts +++ b/backend/sql/json.ts @@ -3,9 +3,9 @@ import { AssFile, AssUser, FilesSchema, UsersSchema } from 'ass'; import path, { resolve } from 'path'; import fs from 'fs-extra'; -import { Database, DatabaseTable, DatabaseValue } from './database'; -import { log } from '../log'; -import { nanoid } from '../generators'; +import { Database, DatabaseTable, DatabaseValue } from './database.js'; +import { log } from '../log.js'; +import { nanoid } from '../generators.js'; /** * Absolute filepaths for JSON data files @@ -32,62 +32,62 @@ const SECTORMAP = { } as { [index: string]: string }; const bothWriter = async (files: FilesSchema, users: UsersSchema) => { - await fs.writeJson(PATHS.files, files, { spaces: '\t' }); - await fs.writeJson(PATHS.users, users, { spaces: '\t' }); + await fs.writeJson(PATHS.files, files, { spaces: '\t' }); + await fs.writeJson(PATHS.users, users, { spaces: '\t' }); }; /** * Creates a JSON file with a given empty data template */ const createEmptyJson = (filepath: string, emptyData: any): Promise => new Promise(async (resolve, reject) => { - try { - if (!(await fs.pathExists(filepath))) { - await fs.ensureFile(filepath); - await fs.writeJson(filepath, emptyData, { spaces: '\t' }); - } - resolve(void 0); - } catch (err) { - reject(err); - } + try { + if (!(await fs.pathExists(filepath))) { + await fs.ensureFile(filepath); + await fs.writeJson(filepath, emptyData, { spaces: '\t' }); + } + resolve(void 0); + } catch (err) { + reject(err); + } }); /** * Ensures the data files exist and creates them if required */ export const ensureFiles = (): Promise => new Promise(async (resolve, reject) => { - log.debug('Checking data files'); - - try { - - // * Default files.json - await createEmptyJson(PATHS.files, { - files: {}, - useSql: false, - meta: {} - } as FilesSchema); - - // * Default users.json - await createEmptyJson(PATHS.users, { - tokens: [], - users: {}, - cliKey: nanoid(32), - useSql: false, - meta: {} - } as UsersSchema); - - log.debug('Data files exist'); - resolve(); - } catch (err) { - log.error('Failed to verify existence of data files'); - reject(err); - } + log.debug('Checking data files'); + + try { + + // * Default files.json + await createEmptyJson(PATHS.files, { + files: {}, + useSql: false, + meta: {} + } as FilesSchema); + + // * Default users.json + await createEmptyJson(PATHS.users, { + tokens: [], + users: {}, + cliKey: nanoid(32), + useSql: false, + meta: {} + } as UsersSchema); + + log.debug('Data files exist'); + resolve(); + } catch (err) { + log.error('Failed to verify existence of data files'); + reject(err); + } }); /** * JSON database. i know json isnt sql, shut up. */ export class JSONDatabase implements Database { - public open(): Promise { return Promise.resolve() } + public open(): Promise { return Promise.resolve() } public close(): Promise { return Promise.resolve() } public configure(): Promise { @@ -97,7 +97,7 @@ export class JSONDatabase implements Database { resolve(); }); } - + public put(table: DatabaseTable, key: string, data: DatabaseValue): Promise { return new Promise(async (resolve, reject) => { if (table == 'assfiles') { @@ -117,19 +117,19 @@ export class JSONDatabase implements Database { // Save the files await bothWriter(filesJson, usersJson); - + resolve() } else if (table == 'assusers') { // ? Local JSON - const usersJson = await fs.readJson(PATHS.users) as UsersSchema; + const usersJson = await fs.readJson(PATHS.users) as UsersSchema; - // Check if key already exists - if (usersJson.users[key] != null) return reject(new Error(`User key ${key} already exists`)); + // Check if key already exists + if (usersJson.users[key] != null) return reject(new Error(`User key ${key} already exists`)); - // Otherwise add the data - usersJson.users[key] = data as AssUser; + // Otherwise add the data + usersJson.users[key] = data as AssUser; - await fs.writeJson(PATHS.users, usersJson, { spaces: '\t' }); + await fs.writeJson(PATHS.users, usersJson, { spaces: '\t' }); resolve(); } @@ -139,7 +139,7 @@ export class JSONDatabase implements Database { public get(table: DatabaseTable, key: string): Promise { return new Promise(async (resolve, reject) => { const data = (await fs.readJson(PATHMAP[table]))[SECTORMAP[table]][key]; - (!data) ? resolve(undefined) : resolve(data); + (!data) ? resolve(undefined) : resolve(data); }); } @@ -148,5 +148,5 @@ export class JSONDatabase implements Database { const data = (await fs.readJson(PATHMAP[table]))[SECTORMAP[table]]; (!data) ? resolve({}) : resolve(data); }); - } + } } \ No newline at end of file diff --git a/backend/sql/mysql.ts b/backend/sql/mysql.ts index 3e90b47..dd19829 100644 --- a/backend/sql/mysql.ts +++ b/backend/sql/mysql.ts @@ -2,9 +2,9 @@ import { AssFile, AssUser, NID, UploadToken } from 'ass'; import mysql, { Pool } from 'mysql2/promise'; -import { log } from '../log'; -import { UserConfig } from '../UserConfig'; -import { Database, DatabaseTable, DatabaseValue } from './database'; +import { log } from '../log.js'; +import { UserConfig } from '../UserConfig.js'; +import { Database, DatabaseTable, DatabaseValue } from './database.js'; export class MySQLDatabase implements Database { private _pool: Pool; @@ -50,7 +50,7 @@ export class MySQLDatabase implements Database { return issue; } - public open() { return Promise.resolve(); } + public open() { return Promise.resolve(); } public close() { return Promise.resolve(); } /** @@ -144,7 +144,7 @@ VALUES ('${key}', '${JSON.stringify(data)}'); }); } - public get(table: DatabaseTable, key: NID): Promise { + public get(table: DatabaseTable, key: NID): Promise { return new Promise(async (resolve, reject) => { try { // Run query @@ -161,7 +161,7 @@ VALUES ('${key}', '${JSON.stringify(data)}'); } // todo: unknown if this works - public getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue }> { + public getAll(table: DatabaseTable): Promise<{ [index: string]: DatabaseValue }> { return new Promise(async (resolve, reject) => { try { // Run query // ! this may not work as expected diff --git a/backend/sql/postgres.ts b/backend/sql/postgres.ts index 7b879f7..52e7fdc 100644 --- a/backend/sql/postgres.ts +++ b/backend/sql/postgres.ts @@ -1,44 +1,43 @@ import { PostgresConfiguration } from 'ass'; -import { Client } from 'pg'; - -import { log } from '../log'; -import { Database, DatabaseTable, DatabaseValue } from './database'; -import { UserConfig } from '../UserConfig'; +import pg from 'pg'; +import { log } from '../log.js'; +import { Database, DatabaseTable, DatabaseValue } from './database.js'; +import { UserConfig } from '../UserConfig.js'; /** * database adapter for postgresql */ export class PostgreSQLDatabase implements Database { - private _client: Client; + private _client: pg.Client; /** * validate config */ private _validateConfig(): string | undefined { // make sure the configuration exists - if (!UserConfig.ready) return 'User configuration not ready'; - if (typeof UserConfig.config.database != 'object') return 'PostgreSQL configuration missing'; - if (UserConfig.config.database.kind != "postgres") return 'Database not set to PostgreSQL, but PostgreSQL is in use, something has gone terribly wrong'; - if (typeof UserConfig.config.database.options != 'object') return 'PostgreSQL configuration missing'; + if (!UserConfig.ready) return 'User configuration not ready'; + if (typeof UserConfig.config.database != 'object') return 'PostgreSQL configuration missing'; + if (UserConfig.config.database.kind != "postgres") return 'Database not set to PostgreSQL, but PostgreSQL is in use, something has gone terribly wrong'; + if (typeof UserConfig.config.database.options != 'object') return 'PostgreSQL configuration missing'; - let config = UserConfig.config.database.options; + let config = UserConfig.config.database.options; - // check the postgres config - const checker = (val: string) => val != null && val !== ''; - const issue = - !checker(config.host) ? 'Missing PostgreSQL Host' - : !checker(config.user) ? 'Missing PostgreSQL User' - : !checker(config.password) ? 'Missing PostgreSQL Password' - : !checker(config.database) ? 'Missing PostgreSQL Database' - // ! Blame VS Code for this weird indentation - : undefined; + // check the postgres config + const checker = (val: string) => val != null && val !== ''; + const issue = + !checker(config.host) ? 'Missing PostgreSQL Host' + : !checker(config.user) ? 'Missing PostgreSQL User' + : !checker(config.password) ? 'Missing PostgreSQL Password' + : !checker(config.database) ? 'Missing PostgreSQL Database' + // ! Blame VS Code for this weird indentation + : undefined; - return issue; + return issue; } - public open(): Promise { + public open(): Promise { return new Promise(async (resolve, reject) => { try { // config check @@ -49,7 +48,7 @@ export class PostgreSQLDatabase implements Database { let config = UserConfig.config.database!.options! as PostgresConfiguration; // set up the client - this._client = new Client({ + this._client = new pg.Client({ host: config.host, port: config.port, user: config.user, @@ -90,7 +89,7 @@ export class PostgreSQLDatabase implements Database { return new Promise(async (resolve, reject) => { try { await this._client.query( -`CREATE TABLE IF NOT EXISTS asstables ( + `CREATE TABLE IF NOT EXISTS asstables ( name TEXT PRIMARY KEY, version INT NOT NULL );`); @@ -105,7 +104,7 @@ export class PostgreSQLDatabase implements Database { } const assTableSchema = '(id TEXT PRIMARY KEY, data JSON NOT NULL)' - + // add missing tables if (!seenRows.has('assfiles')) { log.warn('PostgreSQL', 'assfiles missing, repairing...') @@ -149,8 +148,8 @@ export class PostgreSQLDatabase implements Database { return new Promise(async (resolve, reject) => { try { const queries = { - assfiles: 'INSERT INTO assfiles (id, data) VALUES ($1, $2);', - assusers: 'INSERT INTO assusers (id, data) VALUES ($1, $2);', + assfiles: 'INSERT INTO assfiles (id, data) VALUES ($1, $2);', + assusers: 'INSERT INTO assusers (id, data) VALUES ($1, $2);', asstokens: 'INSERT INTO asstokens (id, data) VALUES ($1, $2);' }; @@ -167,8 +166,8 @@ export class PostgreSQLDatabase implements Database { return new Promise(async (resolve, reject) => { try { const queries = { - assfiles: 'SELECT data FROM assfiles WHERE id = $1::text;', - assusers: 'SELECT data FROM assusers WHERE id = $1::text;', + assfiles: 'SELECT data FROM assfiles WHERE id = $1::text;', + assusers: 'SELECT data FROM assusers WHERE id = $1::text;', asstokens: 'SELECT data FROM asstokens WHERE id = $1::text;' }; @@ -185,8 +184,8 @@ export class PostgreSQLDatabase implements Database { return new Promise(async (resolve, reject) => { try { const queries = { - assfiles: 'SELECT json_object_agg(id, data) AS stuff FROM assfiles;', - assusers: 'SELECT json_object_agg(id, data) AS stuff FROM assusers;', + assfiles: 'SELECT json_object_agg(id, data) AS stuff FROM assfiles;', + assusers: 'SELECT json_object_agg(id, data) AS stuff FROM assusers;', asstokens: 'SELECT json_object_agg(id, data) AS stuff FROM asstokens;' }; diff --git a/common/fix-frontend-js.js b/common/fix-frontend-js.js index 010336f..9e740ac 100644 --- a/common/fix-frontend-js.js +++ b/common/fix-frontend-js.js @@ -1,6 +1,7 @@ -const fs = require('fs-extra'); -const { path } = require('@tycrek/joint'); -const log = new (require('@tycrek/log').TLog)(); +import fs from 'fs-extra'; +import { path } from '@tycrek/joint'; +import { TLog } from '@tycrek/log'; +const log = new TLog(); const FILES = { prefix: 'dist/frontend',