Skip to content

Commit

Permalink
ES Modules 방식으로 마이그레이션
Browse files Browse the repository at this point in the history
  • Loading branch information
tirr-c committed Mar 24, 2024
1 parent 1a8283c commit afbfd42
Show file tree
Hide file tree
Showing 43 changed files with 136 additions and 137 deletions.
6 changes: 3 additions & 3 deletions bootstrap/bootstrap_host.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as bunyan from 'bunyan';
import * as fs from 'fs';
import Config from '../src/config';
import Model from '../src/model/model';
import Config from '../src/config.js';
import Model from '../src/model/model.js';

import { HARDWARE_LAB, LOUNGE, PRACTICE_SERVER, SOFTWARE_LAB } from './hosts_info';
import { HARDWARE_LAB, LOUNGE, PRACTICE_SERVER, SOFTWARE_LAB } from './hosts_info.js';

const config: Config = JSON.parse(fs.readFileSync('config.json', { encoding: 'utf-8' }));

Expand Down
4 changes: 2 additions & 2 deletions bootstrap/rebuild_group_cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as bunyan from 'bunyan';
import * as fs from 'fs';
import Config from '../src/config';
import Model from '../src/model/model';
import Config from '../src/config.js';
import Model from '../src/model/model.js';

const config: Config = JSON.parse(fs.readFileSync('config.json', { encoding: 'utf-8' }));

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"bugs": {
"url": "https://github.com/bacchus-snu/id/issues"
},
"type": "module",
"dependencies": {
"@koa/cors": "^5.0.0",
"@phc/format": "^1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/api/email.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Bunyan from 'bunyan';
import * as nodemailer from 'nodemailer';
import Config from '../config';
import { InvalidEmailError, ResendLimitExeededError } from '../model/errors';
import Config from '../config.js';
import { InvalidEmailError, ResendLimitExeededError } from '../model/errors.js';

export interface EmailOption {
address: string;
Expand Down
12 changes: 6 additions & 6 deletions src/api/handlers/emails.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IMiddleware } from 'koa-router';
import z from 'zod';
import Config from '../../config';
import { EmailAddress } from '../../model/email_addresses';
import { EmailInUseError, InvalidEmailError, ResendLimitExeededError } from '../../model/errors';
import Model from '../../model/model';
import { sendEmail } from '../email';
import emailVerificationTemplate from '../templates/verification_email_template';
import Config from '../../config.js';
import { EmailAddress } from '../../model/email_addresses.js';
import { EmailInUseError, InvalidEmailError, ResendLimitExeededError } from '../../model/errors.js';
import Model from '../../model/model.js';
import { sendEmail } from '../email.js';
import emailVerificationTemplate from '../templates/verification_email_template.js';

export function sendVerificationEmail(model: Model, config: Config): IMiddleware {
const bodySchema = z.object({
Expand Down
6 changes: 3 additions & 3 deletions src/api/handlers/groups.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { IMiddleware } from 'koa-router';
import z from 'zod';
import { BadParameterError } from '../../model/errors';
import Model from '../../model/model';
import { User } from '../../model/users';
import { BadParameterError } from '../../model/errors.js';
import Model from '../../model/model.js';
import { User } from '../../model/users.js';

export function listGroups(model: Model): IMiddleware {
return async (ctx, next) => {
Expand Down
8 changes: 4 additions & 4 deletions src/api/handlers/login.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IMiddleware } from 'koa-router';

import z from 'zod';
import Config from '../../config';
import { AuthorizationError, ControllableError, NoSuchEntryError } from '../../model/errors';
import Model from '../../model/model';
import { SignatureError, verifyPubkeyReq } from '../pubkey';
import Config from '../../config.js';
import { AuthorizationError, ControllableError, NoSuchEntryError } from '../../model/errors.js';
import Model from '../../model/model.js';
import { SignatureError, verifyPubkeyReq } from '../pubkey.js';

const loginBodySchema = z.object({
username: z.string(),
Expand Down
4 changes: 2 additions & 2 deletions src/api/handlers/nss.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IMiddleware } from 'koa-router';

import { NoSuchEntryError } from '../../model/errors';
import Model from '../../model/model';
import { NoSuchEntryError } from '../../model/errors.js';
import Model from '../../model/model.js';

export function getPasswd(model: Model): IMiddleware {
return async (ctx, next) => {
Expand Down
2 changes: 1 addition & 1 deletion src/api/handlers/shells.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IMiddleware } from 'koa-router';
import Model from '../../model/model';
import Model from '../../model/model.js';

export function getShells(model: Model): IMiddleware {
return async (ctx, next) => {
Expand Down
12 changes: 6 additions & 6 deletions src/api/handlers/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { createPublicKey } from 'crypto';
import { jwtVerify } from 'jose';
import { IMiddleware } from 'koa-router';
import z from 'zod';
import Config from '../../config';
import { EmailAddress } from '../../model/email_addresses';
import { InvalidEmailError, ResendLimitExeededError, UserExistsError } from '../../model/errors';
import Model from '../../model/model';
import { sendEmail } from '../email';
import changePasswordTemplate from '../templates/change_password_email_template';
import Config from '../../config.js';
import { EmailAddress } from '../../model/email_addresses.js';
import { InvalidEmailError, ResendLimitExeededError, UserExistsError } from '../../model/errors.js';
import Model from '../../model/model.js';
import { sendEmail } from '../email.js';
import changePasswordTemplate from '../templates/change_password_email_template.js';

export function createUser(model: Model, config: Config): IMiddleware {
const bodySchema = z.object({
Expand Down
25 changes: 13 additions & 12 deletions src/api/router.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import bodyParser from 'koa-bodyparser';
import Router from 'koa-router';
import Config from '../config';
import Model from '../model/model';
import createOIDCRouter from '../oidc/routes';
import { checkVerificationEmailToken, sendVerificationEmail } from './handlers/emails';
import type OIDCProvider from 'oidc-provider';
import type { Configuration as OIDCConfiguration } from 'oidc-provider';

import Config from '../config.js';
import Model from '../model/model.js';
import createOIDCRouter from '../oidc/routes.js';
import { checkVerificationEmailToken, sendVerificationEmail } from './handlers/emails.js';
import {
acceptGroup,
applyGroup,
Expand All @@ -12,21 +15,19 @@ import {
listMembers,
listPending,
rejectGroup,
} from './handlers/groups';
import { checkLogin, login, loginLegacy, loginPAM, logout } from './handlers/login';
import { getGroup, getPasswd } from './handlers/nss';
import { getShells } from './handlers/shells';
} from './handlers/groups.js';
import { checkLogin, login, loginLegacy, loginPAM, logout } from './handlers/login.js';
import { getGroup, getPasswd } from './handlers/nss.js';
import { getShells } from './handlers/shells.js';
import {
changePassword,
checkChangePasswordEmailToken,
createUser,
getUserEmails,
getUserInfo,
sendChangePasswordEmail,
} from './handlers/users';
import { changeUserShell, getUserShell } from './handlers/users';
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import type OIDCProvider, { Configuration as OIDCConfiguration } from 'oidc-provider';
} from './handlers/users.js';
import { changeUserShell, getUserShell } from './handlers/users.js';

export function createRouter(
model: Model,
Expand Down
12 changes: 6 additions & 6 deletions src/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import * as Bunyan from 'bunyan';
import * as crypto from 'crypto';
import Koa from 'koa';
import mount from 'koa-mount';
import OIDCProvider from 'oidc-provider';

import Config from '../config';
import Model from '../model/model';
import createOIDCConfig from '../oidc/configuration';
import { createRouter } from './router';
import Config from '../config.js';
import Model from '../model/model.js';
import createOIDCConfig from '../oidc/configuration.js';
import { createRouter } from './router.js';

const createServer = async (config: Config, log: Bunyan, inputModel?: Model) => {
const createServer = (config: Config, log: Bunyan, inputModel?: Model) => {
const model = inputModel ?? new Model(config, log);
const OIDCProvider = (await import('oidc-provider')).default;
const oidcConfig = createOIDCConfig(model, config.oidc);
const oidcProvider = new OIDCProvider(config.oidc.issuer, oidcConfig);

Expand Down
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Configuration.
*/

// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import type { ClientMetadata, JWKS } from 'oidc-provider';

/**
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as bunyan from 'bunyan';
import * as fs from 'fs';
import createAPIServer from './api/server';
import Config from './config';
import createAPIServer from './api/server.js';
import Config from './config.js';

const config: Config = JSON.parse(fs.readFileSync('config.json', { encoding: 'utf-8' }));

Expand All @@ -10,8 +10,8 @@ const log = bunyan.createLogger({
level: config.logLevel,
});

async function run() {
const apiServer = await createAPIServer(config, log);
function run() {
const apiServer = createAPIServer(config, log);
apiServer.listen(config.api.listenPort, config.api.listenHost);
}

Expand Down
4 changes: 2 additions & 2 deletions src/model/email_addresses.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as crypto from 'crypto';
import moment from 'moment';
import { ExpiredTokenError, NoSuchEntryError } from './errors';
import Transaction from './transaction';
import { ExpiredTokenError, NoSuchEntryError } from './errors.js';
import Transaction from './transaction.js';

interface EmailAddressRow {
address_local: string;
Expand Down
6 changes: 3 additions & 3 deletions src/model/groups.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NoSuchEntryError } from './errors';
import Transaction from './transaction';
import { Translation } from './translation';
import { NoSuchEntryError } from './errors.js';
import Transaction from './transaction.js';
import { Translation } from './translation.js';

interface GroupRow {
idx: number;
Expand Down
6 changes: 3 additions & 3 deletions src/model/hosts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AuthorizationError, NoSuchEntryError } from './errors';
import Model from './model';
import Transaction from './transaction';
import { AuthorizationError, NoSuchEntryError } from './errors.js';
import Model from './model.js';
import Transaction from './transaction.js';

interface HostRow {
idx: number;
Expand Down
20 changes: 10 additions & 10 deletions src/model/model.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import * as Bunyan from 'bunyan';
import * as pg from 'pg';
import Config from '../config';
import EmailAddresses from './email_addresses';
import { ControllableError } from './errors';
import Groups from './groups';
import Hosts from './hosts';
import OAuth from './oauth';
import Permissions from './permissions';
import Shells from './shells';
import Transaction from './transaction';
import Users from './users';
import Config from '../config.js';
import EmailAddresses from './email_addresses.js';
import { ControllableError } from './errors.js';
import Groups from './groups.js';
import Hosts from './hosts.js';
import OAuth from './oauth.js';
import Permissions from './permissions.js';
import Shells from './shells.js';
import Transaction from './transaction.js';
import Users from './users.js';

const PSQL_SERIALIZATION_FAILURE = '40001';
const PSQL_DEADLOCK_DETECTED = '40P01';
Expand Down
5 changes: 2 additions & 3 deletions src/model/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import type { AllClientMetadata } from 'oidc-provider';

import { NoSuchEntryError } from './errors';
import Transaction from './transaction';
import { NoSuchEntryError } from './errors.js';
import Transaction from './transaction.js';

export default class OAuth {
public async getClientById(tr: Transaction, id: string): Promise<AllClientMetadata> {
Expand Down
8 changes: 4 additions & 4 deletions src/model/permissions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NoSuchEntryError } from './errors';
import Model from './model';
import Transaction from './transaction';
import { Translation } from './translation';
import { NoSuchEntryError } from './errors.js';
import Model from './model.js';
import Transaction from './transaction.js';
import { Translation } from './translation.js';

export default class Permissions {
constructor(private readonly model: Model) {
Expand Down
2 changes: 1 addition & 1 deletion src/model/shells.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Transaction from './transaction';
import Transaction from './transaction.js';

export default class Shells {
constructor() {
Expand Down
6 changes: 3 additions & 3 deletions src/model/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
ExpiredTokenError,
NoSuchEntryError,
NotActivatedError,
} from './errors';
import Model from './model';
import Transaction from './transaction';
} from './errors.js';
import Model from './model.js';
import Transaction from './transaction.js';

// see language enum in schema.sql
export type Language = 'ko' | 'en';
Expand Down
1 change: 0 additions & 1 deletion src/oidc/account.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import type { Account } from 'oidc-provider';

class OIDCAccount implements Account {
Expand Down
7 changes: 3 additions & 4 deletions src/oidc/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Redis from 'ioredis';
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import { Redis } from 'ioredis';
import type { Adapter, AdapterPayload } from 'oidc-provider';

import { NoSuchEntryError } from '../model/errors';
import Model from '../model/model';
import { NoSuchEntryError } from '../model/errors.js';
import Model from '../model/model.js';

const grantable = new Set([
'AccessToken',
Expand Down
9 changes: 4 additions & 5 deletions src/oidc/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import type { Configuration } from 'oidc-provider';
import Config from '../config';
import type Model from '../model/model';
import OIDCAccount from './account';
import AdapterFactory from './adapter';
import Config from '../config.js';
import type Model from '../model/model.js';
import OIDCAccount from './account.js';
import AdapterFactory from './adapter.js';

const claims = {
openid: ['sub', 'username', 'groups'],
Expand Down
3 changes: 1 addition & 2 deletions src/oidc/redis.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Redis from 'ioredis';
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import { Redis } from 'ioredis';
import type { Adapter, AdapterPayload } from 'oidc-provider';

const grantable = new Set([
Expand Down
8 changes: 3 additions & 5 deletions src/oidc/routes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { strict as assert } from 'node:assert';

import Router from 'koa-router';
// @ts-expect-error: https://github.com/microsoft/TypeScript/issues/49721
import type OIDCProvider from 'oidc-provider';
import { errors as oidcErrors } from 'oidc-provider';
import * as z from 'zod';

import Model from '../model/model';
import Model from '../model/model.js';

const loginSchema = z.object({
username: z.string().nonempty(),
Expand All @@ -24,13 +24,11 @@ export default (model: Model, provider: OIDCProvider) => {
const router = new Router();

router.use(async (ctx, next) => {
const { errors } = await import('oidc-provider');

ctx.set('cache-control', 'no-store');
try {
await next();
} catch (err) {
if (err instanceof errors.SessionNotFound) {
if (err instanceof oidcErrors.SessionNotFound) {
ctx.status = err.status;
const { message: error, error_description: desc } = err;
ctx.body = { error, desc };
Expand Down
6 changes: 3 additions & 3 deletions test/_setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import test from 'ava';
import Logger, * as bunyan from 'bunyan';
import * as fs from 'fs';
import { Server } from 'node:http';
import createAPIServer from '../src/api/server';
import Config from '../src/config';
import Model from '../src/model/model';
import createAPIServer from '../src/api/server.js';
import Config from '../src/config.js';
import Model from '../src/model/model.js';

export let config: Config;
export let log: Logger;
Expand Down
Loading

0 comments on commit afbfd42

Please sign in to comment.