From a923e91bf56ea2e9ea2adddbee7752860acb02cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Pardou-Piquemal?= Date: Thu, 7 May 2020 14:25:41 +0200 Subject: [PATCH] Fix server code style with prettier (#136) --- .prettierrc | 7 +++++++ server/.babelrc | 7 ++----- server/.eslintrc | 4 ++-- server/README.md | 2 +- server/src/inactive_rooms.js | 10 +++------- server/src/index.js | 24 +++++++++--------------- server/src/socket.js | 14 ++++++-------- server/src/store/Memory.js | 3 +-- server/src/store/Redis.js | 4 ++-- server/src/utils/mailer.js | 17 ++++++----------- 10 files changed, 39 insertions(+), 53 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..3836da9 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "trailingComma": "all", + "tabWidth": 2, + "singleQuote": true, + "arrowParens": "avoid", + "printWidth": 120 +} diff --git a/server/.babelrc b/server/.babelrc index 4201cd1..2bc2702 100644 --- a/server/.babelrc +++ b/server/.babelrc @@ -1,7 +1,4 @@ { "presets": ["@babel/preset-env"], - "plugins": [ - "@babel/plugin-transform-async-to-generator", - "@babel/plugin-transform-runtime" - ] -} \ No newline at end of file + "plugins": ["@babel/plugin-transform-async-to-generator", "@babel/plugin-transform-runtime"] +} diff --git a/server/.eslintrc b/server/.eslintrc index 0abbe63..980b6e2 100644 --- a/server/.eslintrc +++ b/server/.eslintrc @@ -1,7 +1,7 @@ { "parser": "babel-eslint", "rules": { - "semi": ["error", "always"], - "quotes": ["error", "single"] + "semi": ["error", "always"], + "quotes": ["error", "single"] } } diff --git a/server/README.md b/server/README.md index 4c30fea..ee9df91 100644 --- a/server/README.md +++ b/server/README.md @@ -1,3 +1,3 @@ # Darkwire Server -This is the backend for [Darkwire](https://github.com/darkwire/darkwire.io). It requires [darkwire-client](../client) in order to run. \ No newline at end of file +This is the backend for [Darkwire](https://github.com/darkwire/darkwire.io). It requires [darkwire-client](../client) in order to run. diff --git a/server/src/inactive_rooms.js b/server/src/inactive_rooms.js index bcb2a94..ca4a7a9 100644 --- a/server/src/inactive_rooms.js +++ b/server/src/inactive_rooms.js @@ -7,16 +7,12 @@ export async function pollForInactiveRooms() { const rooms = (await store.getAll('rooms')) || {}; console.log(`${Object.keys(rooms).length} rooms found`); - Object.keys(rooms).forEach(async (roomId) => { + Object.keys(rooms).forEach(async roomId => { const room = JSON.parse(rooms[roomId]); const timeSinceUpdatedInSeconds = (Date.now() - room.updatedAt) / 1000; - const timeSinceUpdatedInDays = Math.round( - timeSinceUpdatedInSeconds / 60 / 60 / 24 - ); + const timeSinceUpdatedInDays = Math.round(timeSinceUpdatedInSeconds / 60 / 60 / 24); if (timeSinceUpdatedInDays > 7) { - console.log( - `Deleting roomId ${roomId} which hasn't been used in ${timeSinceUpdatedInDays} days` - ); + console.log(`Deleting roomId ${roomId} which hasn't been used in ${timeSinceUpdatedInDays} days`); await store.del('rooms', roomId); } }); diff --git a/server/src/index.js b/server/src/index.js index 277900d..57c942c 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -34,19 +34,16 @@ if ((siteURL || env === 'development') && !isReviewApp) { origin: env === 'development' ? '*' : siteURL, allowMethods: ['GET', 'HEAD', 'POST'], credentials: true, - }) + }), ); } -router.post('/abuse/:roomId', koaBody, async (ctx) => { +router.post('/abuse/:roomId', koaBody, async ctx => { let { roomId } = ctx.params; roomId = roomId.trim(); - if ( - process.env.ABUSE_FROM_EMAIL_ADDRESS && - process.env.ABUSE_TO_EMAIL_ADDRESS - ) { + if (process.env.ABUSE_FROM_EMAIL_ADDRESS && process.env.ABUSE_TO_EMAIL_ADDRESS) { const abuseForRoomExists = await store.get('abuse', roomId); if (!abuseForRoomExists) { mailer.send({ @@ -66,9 +63,7 @@ router.post('/abuse/:roomId', koaBody, async (ctx) => { app.use(router.routes()); const apiHost = process.env.API_HOST; -const cspDefaultSrc = `'self'${ - apiHost ? ` https://${apiHost} wss://${apiHost}` : '' -}`; +const cspDefaultSrc = `'self'${apiHost ? ` https://${apiHost} wss://${apiHost}` : ''}`; function setStaticFileHeaders(ctx) { ctx.set({ @@ -78,8 +73,7 @@ function setStaticFileHeaders(ctx) { 'X-XSS-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'no-referrer', - 'Feature-Policy': - "geolocation 'none'; vr 'none'; payment 'none'; microphone 'none'", + 'Feature-Policy': "geolocation 'none'; vr 'none'; payment 'none'; microphone 'none'", }); } @@ -92,12 +86,12 @@ if (clientDistDirectory) { })(ctx, next); }); - app.use(async (ctx) => { + app.use(async ctx => { setStaticFileHeaders(ctx); await koaSend(ctx, 'index.html', { root: clientDistDirectory }); }); } else { - app.use(async (ctx) => { + app.use(async ctx => { ctx.body = { ready: true }; }); } @@ -117,7 +111,7 @@ if (store.hasSocketAdapter) { const roomHashSecret = process.env.ROOM_HASH_SECRET; -const getRoomIdHash = (id) => { +const getRoomIdHash = id => { if (env === 'development') { return id; } @@ -131,7 +125,7 @@ const getRoomIdHash = (id) => { export const getIO = () => io; -io.on('connection', async (socket) => { +io.on('connection', async socket => { const roomId = socket.handshake.query.roomId; const roomIdHash = getRoomIdHash(roomId); diff --git a/server/src/socket.js b/server/src/socket.js index e5cadb6..0b92b10 100644 --- a/server/src/socket.js +++ b/server/src/socket.js @@ -53,14 +53,14 @@ export default class Socket { if (getStore().hasSocketAdapter) { getIO() .of('/') - .adapter.remoteJoin(socket.id, roomId, (err) => { + .adapter.remoteJoin(socket.id, roomId, err => { if (err) { reject(); } resolve(); }); } else { - socket.join(roomId, (err) => { + socket.join(roomId, err => { if (err) { reject(); } @@ -71,11 +71,11 @@ export default class Socket { } async handleSocket(socket) { - socket.on('ENCRYPTED_MESSAGE', (payload) => { + socket.on('ENCRYPTED_MESSAGE', payload => { socket.to(this._roomId).emit('ENCRYPTED_MESSAGE', payload); }); - socket.on('USER_ENTER', async (payload) => { + socket.on('USER_ENTER', async payload => { let room = await this.fetchRoom(); if (_.isEmpty(room)) { room = { @@ -109,9 +109,7 @@ export default class Socket { socket.on('TOGGLE_LOCK_ROOM', async (data, callback) => { const room = await this.fetchRoom(); - const user = (room.users || []).find( - (u) => u.socketId === socket.id && u.isOwner - ); + const user = (room.users || []).find(u => u.socketId === socket.id && u.isOwner); if (!user) { callback({ @@ -146,7 +144,7 @@ export default class Socket { const newRoom = { ...room, users: (room.users || []) - .filter((u) => u.socketId !== socket.id) + .filter(u => u.socketId !== socket.id) .map((u, index) => ({ ...u, isOwner: index === 0, diff --git a/server/src/store/Memory.js b/server/src/store/Memory.js index e5c463c..4bb7f7e 100644 --- a/server/src/store/Memory.js +++ b/server/src/store/Memory.js @@ -42,7 +42,6 @@ export class MemoryStore { this.store[key][field] += inc; return this.store[key][field]; } - } -export default MemoryStore \ No newline at end of file +export default MemoryStore; diff --git a/server/src/store/Redis.js b/server/src/store/Redis.js index d9dc8d5..4f35e9b 100644 --- a/server/src/store/Redis.js +++ b/server/src/store/Redis.js @@ -34,9 +34,9 @@ export class RedisStore { return this.redis.incrbyAsync(key, field, inc); } - getSocketAdapter() { + getSocketAdapter() { return socketRedis(this.redisUrl); } } -export default RedisStore \ No newline at end of file +export default RedisStore; diff --git a/server/src/utils/mailer.js b/server/src/utils/mailer.js index 9599039..03b1f74 100644 --- a/server/src/utils/mailer.js +++ b/server/src/utils/mailer.js @@ -1,9 +1,6 @@ -require('dotenv').config() +require('dotenv').config(); -const { - MAILGUN_API_KEY, - MAILGUN_DOMAIN, -} = process.env; +const { MAILGUN_API_KEY, MAILGUN_DOMAIN } = process.env; const apiKey = MAILGUN_API_KEY; const domain = MAILGUN_DOMAIN; @@ -11,14 +8,12 @@ const domain = MAILGUN_DOMAIN; let mailgun; if (apiKey && domain) { - mailgun = require('mailgun-js')({apiKey, domain}); + mailgun = require('mailgun-js')({ apiKey, domain }); } -module.exports.send = (data) => { +module.exports.send = data => { if (!mailgun) { return; } - mailgun.messages().send(data, function (error, body) { - - }); -} + mailgun.messages().send(data, function (error, body) {}); +};