Skip to content

Commit 044b6b9

Browse files
committed
refactor: fix lint
Signed-off-by: BoHong Li <raccoon@hackmd.io>
1 parent 6c968f9 commit 044b6b9

File tree

1 file changed

+59
-52
lines changed

1 file changed

+59
-52
lines changed

lib/response.js

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
'use strict'
22
// response
33
// external modules
4-
var fs = require('fs')
5-
var path = require('path')
6-
var markdownpdf = require('markdown-pdf')
7-
var shortId = require('shortid')
8-
var querystring = require('querystring')
9-
var request = require('request')
10-
var moment = require('moment')
4+
const fs = require('fs')
5+
const path = require('path')
6+
const markdownpdf = require('markdown-pdf')
7+
const shortId = require('shortid')
8+
const querystring = require('querystring')
9+
const request = require('request')
10+
const moment = require('moment')
1111

1212
// core
13-
var config = require('./config')
14-
var logger = require('./logger')
15-
var models = require('./models')
16-
var utils = require('./utils')
17-
var history = require('./history')
13+
const config = require('./config')
14+
const logger = require('./logger')
15+
const models = require('./models')
16+
const utils = require('./utils')
17+
const history = require('./history')
1818

1919
// public
20-
exports.errorForbidden = function (res) {
20+
exports.errorForbidden = errorForbidden
21+
exports.errorNotFound = errorNotFound
22+
exports.errorBadRequest = errorBadRequest
23+
exports.errorTooLong = errorTooLong
24+
exports.errorInternalError = errorInternalError
25+
exports.errorServiceUnavailable = errorServiceUnavailable
26+
exports.newNote = newNote
27+
exports.showNote = showNote
28+
exports.showPublishNote = showPublishNote
29+
exports.showPublishSlide = showPublishSlide
30+
exports.showIndex = showIndex
31+
exports.noteActions = noteActions
32+
exports.publishNoteActions = publishNoteActions
33+
exports.publishSlideActions = publishSlideActions
34+
exports.githubActions = githubActions
35+
exports.gitlabActions = gitlabActions
36+
37+
function errorForbidden (res) {
2138
const { req } = res
2239
if (req.user) {
2340
responseError(res, '403', 'Forbidden', 'oh no.')
@@ -26,31 +43,21 @@ exports.errorForbidden = function (res) {
2643
res.redirect(config.serverURL + '/')
2744
}
2845
}
29-
exports.errorNotFound = function (res) {
46+
function errorNotFound (res) {
3047
responseError(res, '404', 'Not Found', 'oops.')
3148
}
32-
exports.errorBadRequest = function (res) {
49+
function errorBadRequest (res) {
3350
responseError(res, '400', 'Bad Request', 'something not right.')
3451
}
35-
exports.errorTooLong = function (res) {
52+
function errorTooLong (res) {
3653
responseError(res, '413', 'Payload Too Large', 'Shorten your note!')
3754
}
38-
exports.errorInternalError = function (res) {
55+
function errorInternalError (res) {
3956
responseError(res, '500', 'Internal Error', 'wtf.')
4057
}
41-
exports.errorServiceUnavailable = function (res) {
58+
function errorServiceUnavailable (res) {
4259
res.status(503).send("I'm busy right now, try again later.")
4360
}
44-
exports.newNote = newNote
45-
exports.showNote = showNote
46-
exports.showPublishNote = showPublishNote
47-
exports.showPublishSlide = showPublishSlide
48-
exports.showIndex = showIndex
49-
exports.noteActions = noteActions
50-
exports.publishNoteActions = publishNoteActions
51-
exports.publishSlideActions = publishSlideActions
52-
exports.githubActions = githubActions
53-
exports.gitlabActions = gitlabActions
5461

5562
function responseError (res, code, detail, msg) {
5663
res.status(code).render('error.ejs', {
@@ -115,15 +122,15 @@ function newNote (req, res, next) {
115122
var owner = null
116123
var body = ''
117124
if (req.body && req.body.length > config.documentMaxLength) {
118-
return response.errorTooLong(res)
125+
return errorTooLong(res)
119126
} else if (req.body) {
120127
body = req.body
121128
}
122129
body = body.replace(/[\r]/g, '')
123130
if (req.isAuthenticated()) {
124131
owner = req.user.id
125132
} else if (!config.allowAnonymous) {
126-
return response.errorForbidden(res)
133+
return errorForbidden(res)
127134
}
128135
models.Note.create({
129136
ownerId: owner,
@@ -137,7 +144,7 @@ function newNote (req, res, next) {
137144
return res.redirect(config.serverURL + '/' + models.Note.encodeNoteId(note.id))
138145
}).catch(function (err) {
139146
logger.error(err)
140-
return response.errorInternalError(res)
147+
return errorInternalError(res)
141148
})
142149
}
143150

@@ -157,7 +164,7 @@ function findNote (req, res, callback, include) {
157164
models.Note.parseNoteId(id, function (err, _id) {
158165
if (err) {
159166
logger.error(err)
160-
return response.errorInternalError(res)
167+
return errorInternalError(res)
161168
}
162169
models.Note.findOne({
163170
where: {
@@ -170,17 +177,17 @@ function findNote (req, res, callback, include) {
170177
req.alias = noteId
171178
return newNote(req, res)
172179
} else {
173-
return response.errorNotFound(res)
180+
return errorNotFound(res)
174181
}
175182
}
176183
if (!checkViewPermission(req, note)) {
177-
return response.errorForbidden(res)
184+
return errorForbidden(res)
178185
} else {
179186
return callback(note)
180187
}
181188
}).catch(function (err) {
182189
logger.error(err)
183-
return response.errorInternalError(res)
190+
return errorInternalError(res)
184191
})
185192
})
186193
}
@@ -211,7 +218,7 @@ function showPublishNote (req, res, next) {
211218
}
212219
note.increment('viewcount').then(function (note) {
213220
if (!note) {
214-
return response.errorNotFound(res)
221+
return errorNotFound(res)
215222
}
216223
var body = note.content
217224
var extracted = models.Note.extractMeta(body)
@@ -240,7 +247,7 @@ function showPublishNote (req, res, next) {
240247
return renderPublish(data, res)
241248
}).catch(function (err) {
242249
logger.error(err)
243-
return response.errorInternalError(res)
250+
return errorInternalError(res)
244251
})
245252
}, include)
246253
}
@@ -317,7 +324,7 @@ function actionPDF (req, res, note) {
317324
markdownpdf().from.string(content).to(path, function () {
318325
if (!fs.existsSync(path)) {
319326
logger.error('PDF seems to not be generated as expected. File doesn\'t exist: ' + path)
320-
return response.errorInternalError(res)
327+
return errorInternalError(res)
321328
}
322329
var stream = fs.createReadStream(path)
323330
var filename = title
@@ -352,10 +359,10 @@ function actionRevision (req, res, note) {
352359
models.Revision.getPatchedNoteRevisionByTime(note, time, function (err, content) {
353360
if (err) {
354361
logger.error(err)
355-
return response.errorInternalError(res)
362+
return errorInternalError(res)
356363
}
357364
if (!content) {
358-
return response.errorNotFound(res)
365+
return errorNotFound(res)
359366
}
360367
res.set({
361368
'Access-Control-Allow-Origin': '*', // allow CORS as API
@@ -367,13 +374,13 @@ function actionRevision (req, res, note) {
367374
res.send(content)
368375
})
369376
} else {
370-
return response.errorNotFound(res)
377+
return errorNotFound(res)
371378
}
372379
} else {
373380
models.Revision.getNoteRevisions(note, function (err, data) {
374381
if (err) {
375382
logger.error(err)
376-
return response.errorInternalError(res)
383+
return errorInternalError(res)
377384
}
378385
var out = {
379386
revision: data
@@ -413,7 +420,7 @@ function noteActions (req, res, next) {
413420
actionPDF(req, res, note)
414421
} else {
415422
logger.error('PDF export failed: Disabled by config. Set "allowPDFExport: true" to enable. Check the documentation for details')
416-
response.errorForbidden(res)
423+
errorForbidden(res)
417424
}
418425
break
419426
case 'gist':
@@ -478,7 +485,7 @@ function githubActionGist (req, res, note) {
478485
var code = req.query.code
479486
var state = req.query.state
480487
if (!code || !state) {
481-
return response.errorForbidden(res)
488+
return errorForbidden(res)
482489
} else {
483490
var data = {
484491
client_id: config.github.clientID,
@@ -518,14 +525,14 @@ function githubActionGist (req, res, note) {
518525
res.setHeader('referer', '')
519526
res.redirect(body.html_url)
520527
} else {
521-
return response.errorForbidden(res)
528+
return errorForbidden(res)
522529
}
523530
})
524531
} else {
525-
return response.errorForbidden(res)
532+
return errorForbidden(res)
526533
}
527534
} else {
528-
return response.errorForbidden(res)
535+
return errorForbidden(res)
529536
}
530537
})
531538
}
@@ -553,7 +560,7 @@ function gitlabActionProjects (req, res, note) {
553560
id: req.user.id
554561
}
555562
}).then(function (user) {
556-
if (!user) { return response.errorNotFound(res) }
563+
if (!user) { return errorNotFound(res) }
557564
var ret = { baseURL: config.gitlab.baseURL, version: config.gitlab.version }
558565
ret.accesstoken = user.accessToken
559566
ret.profileid = user.profileid
@@ -570,10 +577,10 @@ function gitlabActionProjects (req, res, note) {
570577
)
571578
}).catch(function (err) {
572579
logger.error('gitlab action projects failed: ' + err)
573-
return response.errorInternalError(res)
580+
return errorInternalError(res)
574581
})
575582
} else {
576-
return response.errorForbidden(res)
583+
return errorForbidden(res)
577584
}
578585
}
579586

@@ -591,7 +598,7 @@ function showPublishSlide (req, res, next) {
591598
if ((note.alias && shortid !== note.alias) || (!note.alias && shortid !== note.shortid)) { return res.redirect(config.serverURL + '/p/' + (note.alias || note.shortid)) }
592599
note.increment('viewcount').then(function (note) {
593600
if (!note) {
594-
return response.errorNotFound(res)
601+
return errorNotFound(res)
595602
}
596603
var body = note.content
597604
var extracted = models.Note.extractMeta(body)
@@ -622,7 +629,7 @@ function showPublishSlide (req, res, next) {
622629
return renderPublishSlide(data, res)
623630
}).catch(function (err) {
624631
logger.error(err)
625-
return response.errorInternalError(res)
632+
return errorInternalError(res)
626633
})
627634
}, include)
628635
}

0 commit comments

Comments
 (0)