From 93c0af613f97cd70dddd454150d8174c1dee3577 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 8 Nov 2017 14:58:48 +0900 Subject: [PATCH] Improve performance --- src/api/bot/core.ts | 2 +- src/api/endpoints/drive/files/create.ts | 1 + src/api/endpoints/i/change_password.ts | 6 +++--- src/api/endpoints/i/regenerate_token.ts | 2 +- src/api/private/signin.ts | 2 +- src/api/private/signup.ts | 4 ++-- src/file/server.ts | 3 +++ 7 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/api/bot/core.ts b/src/api/bot/core.ts index 53fb18119..832439088 100644 --- a/src/api/bot/core.ts +++ b/src/api/bot/core.ts @@ -232,7 +232,7 @@ class SigninContext extends Context { } } else { // Compare password - const same = bcrypt.compareSync(query, this.temporaryUser.password); + const same = await bcrypt.compare(query, this.temporaryUser.password); if (same) { this.bot.signin(this.temporaryUser); diff --git a/src/api/endpoints/drive/files/create.ts b/src/api/endpoints/drive/files/create.ts index 43dca7762..7967c3118 100644 --- a/src/api/endpoints/drive/files/create.ts +++ b/src/api/endpoints/drive/files/create.ts @@ -20,6 +20,7 @@ module.exports = (file, params, user) => new Promise(async (res, rej) => { return rej('file is required'); } + // TODO: 非同期にしたい。Promise対応してないんだろうか... const buffer = fs.readFileSync(file.path); fs.unlink(file.path, (err) => { if (err) console.log(err); }); diff --git a/src/api/endpoints/i/change_password.ts b/src/api/endpoints/i/change_password.ts index faceded29..16f1a2e4e 100644 --- a/src/api/endpoints/i/change_password.ts +++ b/src/api/endpoints/i/change_password.ts @@ -22,15 +22,15 @@ module.exports = async (params, user) => new Promise(async (res, rej) => { if (newPasswordErr) return rej('invalid new_password param'); // Compare password - const same = bcrypt.compareSync(currentPassword, user.password); + const same = await bcrypt.compare(currentPassword, user.password); if (!same) { return rej('incorrect password'); } // Generate hash of password - const salt = bcrypt.genSaltSync(8); - const hash = bcrypt.hashSync(newPassword, salt); + const salt = await bcrypt.genSalt(8); + const hash = await bcrypt.hash(newPassword, salt); await User.update(user._id, { $set: { diff --git a/src/api/endpoints/i/regenerate_token.ts b/src/api/endpoints/i/regenerate_token.ts index f96d10ebf..653468330 100644 --- a/src/api/endpoints/i/regenerate_token.ts +++ b/src/api/endpoints/i/regenerate_token.ts @@ -20,7 +20,7 @@ module.exports = async (params, user) => new Promise(async (res, rej) => { if (passwordErr) return rej('invalid password param'); // Compare password - const same = bcrypt.compareSync(password, user.password); + const same = await bcrypt.compare(password, user.password); if (!same) { return rej('incorrect password'); diff --git a/src/api/private/signin.ts b/src/api/private/signin.ts index c7dc24398..068988792 100644 --- a/src/api/private/signin.ts +++ b/src/api/private/signin.ts @@ -40,7 +40,7 @@ export default async (req: express.Request, res: express.Response) => { } // Compare password - const same = bcrypt.compareSync(password, user.password); + const same = await bcrypt.compare(password, user.password); if (same) { const expires = 1000 * 60 * 60 * 24 * 365; // One Year diff --git a/src/api/private/signup.ts b/src/api/private/signup.ts index bcc17a876..7a6b9c098 100644 --- a/src/api/private/signup.ts +++ b/src/api/private/signup.ts @@ -54,8 +54,8 @@ export default async (req: express.Request, res: express.Response) => { } // Generate hash of password - const salt = bcrypt.genSaltSync(8); - const hash = bcrypt.hashSync(password, salt); + const salt = await bcrypt.genSalt(8); + const hash = await bcrypt.hash(password, salt); // Generate secret const secret = generateUserToken(); diff --git a/src/file/server.ts b/src/file/server.ts index 449fa2d74..f6e218dc1 100644 --- a/src/file/server.ts +++ b/src/file/server.ts @@ -33,11 +33,13 @@ app.get('/', (req, res) => { }); app.get('/default-avatar.jpg', (req, res) => { + // TODO: 非同期にしたい。Promise対応してないんだろうか... const file = fs.readFileSync(`${__dirname}/assets/avatar.jpg`); send(file, 'image/jpeg', req, res); }); app.get('/app-default.jpg', (req, res) => { + // TODO: 非同期にしたい。Promise対応してないんだろうか... const file = fs.readFileSync(`${__dirname}/assets/dummy.png`); send(file, 'image/png', req, res); }); @@ -54,6 +56,7 @@ async function raw(data: Buffer, type: string, download: boolean, res: express.R async function thumbnail(data: Buffer, type: string, resize: number, res: express.Response): Promise { if (!/^image\/.*$/.test(type)) { + // TODO: 非同期にしたい。Promise対応してないんだろうか... data = fs.readFileSync(`${__dirname}/assets/not-an-image.png`); }