diff --git a/packages/server/modules/cli/commands/db/migrate.js b/packages/server/modules/cli/commands/db/migrate.ts similarity index 55% rename from packages/server/modules/cli/commands/db/migrate.js rename to packages/server/modules/cli/commands/db/migrate.ts index 726116c06b..a9577b8383 100644 --- a/packages/server/modules/cli/commands/db/migrate.js +++ b/packages/server/modules/cli/commands/db/migrate.ts @@ -1,10 +1,13 @@ -/** @type {import('yargs').CommandModule} */ -const command = { +import { noop } from 'lodash' +import { CommandModule } from 'yargs' + +const command: CommandModule = { command: 'migrate', describe: 'Migration specific commands', builder(yargs) { return yargs.commandDir('migrate', { extensions: ['js', 'ts'] }).demandCommand() - } + }, + handler: noop } -module.exports = command +export = command diff --git a/packages/server/modules/cli/commands/db/migrate/create.js b/packages/server/modules/cli/commands/db/migrate/create.ts similarity index 54% rename from packages/server/modules/cli/commands/db/migrate/create.js rename to packages/server/modules/cli/commands/db/migrate/create.ts index 76134365dc..8034141086 100644 --- a/packages/server/modules/cli/commands/db/migrate/create.js +++ b/packages/server/modules/cli/commands/db/migrate/create.ts @@ -1,23 +1,23 @@ -const knex = require('@/db/knex') -const { appRoot } = require('@/bootstrap') -const fs = require('fs/promises') -const { logger } = require('@/logging/logging') +import knex from '@/db/knex' +import { appRoot } from '@/bootstrap' +import fs from 'fs/promises' +import { logger } from '@/logging/logging' +import { CommandModule } from 'yargs' /** @type {import('yargs').CommandModule} */ -const command = { +const command: CommandModule = { command: 'create [module]', describe: 'Create a new migration', - builder(yargs) { - return yargs - .positional('name', { - describe: 'Migration name', - type: 'string' - }) - .positional('module', { - describe: 'The server module into which this migration should be generated', - type: 'string', - default: 'core' - }) + builder: { + name: { + describe: 'Migration name', + type: 'string' + }, + module: { + describe: 'The server module into which this migration should be generated', + type: 'string', + default: 'core' + } }, async handler(argv) { const name = argv.name @@ -40,4 +40,4 @@ const command = { } } -module.exports = command +export = command diff --git a/packages/server/modules/cli/commands/db/migrate/down.js b/packages/server/modules/cli/commands/db/migrate/down.ts similarity index 74% rename from packages/server/modules/cli/commands/db/migrate/down.js rename to packages/server/modules/cli/commands/db/migrate/down.ts index 91411fc6ed..c79c7256c1 100644 --- a/packages/server/modules/cli/commands/db/migrate/down.js +++ b/packages/server/modules/cli/commands/db/migrate/down.ts @@ -1,8 +1,8 @@ -const knex = require('@/db/knex') -const { logger } = require('@/logging/logging') +import knex from '@/db/knex' +import { logger } from '@/logging/logging' +import { CommandModule } from 'yargs' -/** @type {import('yargs').CommandModule} */ -const command = { +const command: CommandModule = { command: 'down [times]', describe: 'Undo last migration', builder(yargs) { @@ -29,4 +29,4 @@ const command = { } } -module.exports = command +export = command diff --git a/packages/server/modules/cli/commands/db/migrate/latest.js b/packages/server/modules/cli/commands/db/migrate/latest.ts similarity index 58% rename from packages/server/modules/cli/commands/db/migrate/latest.js rename to packages/server/modules/cli/commands/db/migrate/latest.ts index 765d8ce988..5b73ea855d 100644 --- a/packages/server/modules/cli/commands/db/migrate/latest.js +++ b/packages/server/modules/cli/commands/db/migrate/latest.ts @@ -1,8 +1,8 @@ -const knex = require('@/db/knex') -const { logger } = require('@/logging/logging') +import knex from '@/db/knex' +import { logger } from '@/logging/logging' +import { CommandModule } from 'yargs' -/** @type {import('yargs').CommandModule} */ -const command = { +const command: CommandModule = { command: 'latest', describe: 'Run all migrations that have not yet been run', async handler() { @@ -12,4 +12,4 @@ const command = { } } -module.exports = command +export = command diff --git a/packages/server/modules/cli/commands/db/migrate/rollback.js b/packages/server/modules/cli/commands/db/migrate/rollback.js deleted file mode 100644 index ec101ead8a..0000000000 --- a/packages/server/modules/cli/commands/db/migrate/rollback.js +++ /dev/null @@ -1,15 +0,0 @@ -const knex = require('@/db/knex') -const { logger } = require('@/logging/logging') - -/** @type {import('yargs').CommandModule} */ -const command = { - command: 'rollback', - describe: 'Roll back all migrations', - async handler() { - logger.info('Rolling back migrations...') - await knex.migrate.rollback(null, true) - logger.info('Completed rolling back migrations') - } -} - -module.exports = command diff --git a/packages/server/modules/cli/commands/db/migrate/rollback.ts b/packages/server/modules/cli/commands/db/migrate/rollback.ts new file mode 100644 index 0000000000..9b5563bf55 --- /dev/null +++ b/packages/server/modules/cli/commands/db/migrate/rollback.ts @@ -0,0 +1,15 @@ +import knex from '@/db/knex' +import { logger } from '@/logging/logging' +import { CommandModule } from 'yargs' + +const command: CommandModule = { + command: 'rollback', + describe: 'Roll back all migrations', + async handler() { + logger.info('Rolling back migrations...') + await knex.migrate.rollback(undefined, true) + logger.info('Completed rolling back migrations') + } +} + +export = command diff --git a/packages/server/modules/cli/commands/db/migrate/up.js b/packages/server/modules/cli/commands/db/migrate/up.ts similarity index 57% rename from packages/server/modules/cli/commands/db/migrate/up.js rename to packages/server/modules/cli/commands/db/migrate/up.ts index 04205f2dc6..d06d4c7966 100644 --- a/packages/server/modules/cli/commands/db/migrate/up.js +++ b/packages/server/modules/cli/commands/db/migrate/up.ts @@ -1,8 +1,8 @@ -const knex = require('@/db/knex') -const { logger } = require('@/logging/logging') +import knex from '@/db/knex' +import { logger } from '@/logging/logging' +import { CommandModule } from 'yargs' -/** @type {import('yargs').CommandModule} */ -const command = { +const command: CommandModule = { command: 'up', describe: 'Run next migration that has not yet been run', async handler() { @@ -12,4 +12,4 @@ const command = { } } -module.exports = command +export = command diff --git a/packages/server/modules/cli/commands/db/seed.js b/packages/server/modules/cli/commands/db/seed.ts similarity index 55% rename from packages/server/modules/cli/commands/db/seed.js rename to packages/server/modules/cli/commands/db/seed.ts index b2c32d48d3..c7e73d8746 100644 --- a/packages/server/modules/cli/commands/db/seed.js +++ b/packages/server/modules/cli/commands/db/seed.ts @@ -1,10 +1,13 @@ -/** @type {import('yargs').CommandModule} */ -const command = { +import { noop } from 'lodash' +import { CommandModule } from 'yargs' + +const command: CommandModule = { command: 'seed', describe: 'Seed your local DB with fake data', builder(yargs) { return yargs.commandDir('seed', { extensions: ['js', 'ts'] }).demandCommand() - } + }, + handler: noop } -module.exports = command +export = command diff --git a/packages/server/modules/cli/commands/db/seed/users.js b/packages/server/modules/cli/commands/db/seed/users.ts similarity index 73% rename from packages/server/modules/cli/commands/db/seed/users.js rename to packages/server/modules/cli/commands/db/seed/users.ts index 649189b22a..05bba7187e 100644 --- a/packages/server/modules/cli/commands/db/seed/users.js +++ b/packages/server/modules/cli/commands/db/seed/users.ts @@ -1,9 +1,11 @@ -const { logger } = require('@/logging/logging') -const { Users, ServerAcl } = require('@/modules/core/dbSchema') -const { Roles } = require('@/modules/core/helpers/mainConstants') -const { faker } = require('@faker-js/faker') -const { range } = require('lodash') -const { UniqueEnforcer } = require('enforce-unique') +import { logger } from '@/logging/logging' +import { Users, ServerAcl } from '@/modules/core/dbSchema' +import { Roles } from '@/modules/core/helpers/mainConstants' +import { faker } from '@faker-js/faker' +import { range } from 'lodash' +import { UniqueEnforcer } from 'enforce-unique' +import { CommandModule } from 'yargs' +import { UserRecord } from '@/modules/core/helpers/types' const RETRY_COUNT = 3 const UNIQUE_MAX_TIME = 500 @@ -36,7 +38,7 @@ function createFakeUser() { } } -function generateUsers(count) { +function generateUsers(count: number) { const users = [] for (let i = 0; i < count; i++) { users.push(createFakeUser()) @@ -44,8 +46,8 @@ function generateUsers(count) { return users } -function insertUsers(users) { - return Users.knex().returning(Users.col.id).insert(users) +function insertUsers(users: Partial[]): Promise> { + return Users.knex().returning(Users.col.id).insert(users) } async function* batchedOperationGenerator({ @@ -53,6 +55,14 @@ async function* batchedOperationGenerator({ itemCount, batchSize, retryCount = 3 +}: { + batchInsertGenerator: ( + insertCount: number, + currentItemCount: number + ) => Promise + itemCount: number + batchSize: number + retryCount?: number }) { logger.info('Starting batched operation...') let currentItemCount = 0 @@ -80,6 +90,7 @@ async function* batchedOperationGenerator({ }) batchPromise = batchPromise.catch((e) => { logger.error(e, 'Operation failed all retries') + return [] }) currentItemCount = newItemCount @@ -88,21 +99,19 @@ async function* batchedOperationGenerator({ } } -/** @type {import('yargs').CommandModule} */ -const command = { +const command: CommandModule = { command: 'users [batchsize]', describe: 'Fill the `users` table with a ton of fake users', - builder(yargs) { - return yargs - .positional('count', { - describe: 'User count', - type: 'number' - }) - .positional('batchsize', { - describe: 'Max amount of inserts to process at once', - type: 'number', - default: '500' - }) + builder: { + count: { + describe: 'User count', + type: 'number' + }, + batchsize: { + describe: 'Max amount of inserts to process at once', + type: 'number', + default: '500' + } }, async handler(argv) { const count = argv.count @@ -131,4 +140,4 @@ const command = { } } -module.exports = command +export = command diff --git a/packages/server/modules/cli/index.js b/packages/server/modules/cli/index.ts similarity index 74% rename from packages/server/modules/cli/index.js rename to packages/server/modules/cli/index.ts index cb0bb7c62f..9a3324f825 100644 --- a/packages/server/modules/cli/index.js +++ b/packages/server/modules/cli/index.ts @@ -1,7 +1,8 @@ -const path = require('path') -const yargs = require('yargs') -require('../../bootstrap') -const { logger } = require('@/logging/logging') +/* eslint-disable no-restricted-imports */ +import path from 'path' +import yargs from 'yargs' +import '../../bootstrap' +import { logger } from '@/logging/logging' const execution = yargs .scriptName('yarn cli') @@ -25,5 +26,6 @@ const execution = yargs const promise = Promise.resolve(execution) promise.then(() => { - yargs.exit(0) + // weird TS typing issue + yargs.exit(0, undefined as unknown as Error) }) diff --git a/packages/server/modules/shared/utils/ip.js b/packages/server/modules/shared/utils/ip.ts similarity index 56% rename from packages/server/modules/shared/utils/ip.js rename to packages/server/modules/shared/utils/ip.ts index ff4a081044..41b46ccb30 100644 --- a/packages/server/modules/shared/utils/ip.js +++ b/packages/server/modules/shared/utils/ip.ts @@ -1,15 +1,18 @@ -const getIpFromRequest = (req) => { - let ip +import { Optional } from '@speckle/shared' +import type express from 'express' +import type http from 'http' + +export const getIpFromRequest = (req: express.Request | http.IncomingMessage) => { + let ip: Optional = undefined try { - ip = - req.headers['cf-connecting-ip'] || + ip = (req.headers['cf-connecting-ip'] || req.headers['true-client-ip'] || req.headers['x-real-ip'] || req.headers['x-forwarded-for'] || req.headers['x-original-forwarded-for'] || - req.ip || + ('ip' in req ? req.ip : undefined) || req.connection.remoteAddress || - '' + '') as string } catch { ip = '' } @@ -19,5 +22,3 @@ const getIpFromRequest = (req) => { if (ip.startsWith(ipPrefix) || ip === '') return null return ip } - -module.exports = { getIpFromRequest } diff --git a/packages/server/package.json b/packages/server/package.json index 2a65cebaf1..7b2c2be189 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -29,7 +29,7 @@ "lint:ci": "yarn lint:tsc", "lint:tsc": "tsc --noEmit", "lint:eslint": "eslint .", - "cli": "cross-env LOG_LEVEL=debug LOG_PRETTY=true NODE_ENV=development ts-node ./modules/cli/index.js", + "cli": "cross-env LOG_LEVEL=debug LOG_PRETTY=true NODE_ENV=development ts-node ./modules/cli/index.ts", "cli:download:commit": "cross-env LOG_PRETTY=true LOG_LEVEL=debug yarn cli download commit", "migrate": "yarn cli db migrate", "migrate:test": "cross-env NODE_ENV=test ts-node ./modules/cli/index.js db migrate",