refactor: server code (#2545)

+ Remove duplicated await
+ Replace some legacy codes with ES6
+ Fix some of eslint problems
This commit is contained in:
Jafar Akhondali 2020-10-14 18:46:27 +03:30 committed by GitHub
parent 0a1f0ac9e3
commit 5ba36ee421
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 39 deletions

View File

@ -43,8 +43,7 @@ module.exports = {
let sslOptions = null
if (dbUseSSL && _.isPlainObject(dbConfig) && _.get(WIKI.config.db, 'sslOptions.auto', null) === false) {
sslOptions = WIKI.config.db.sslOptions
// eslint-disable-next-line no-unneeded-ternary
sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized === false ? false : true
sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized !== false
if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) {
sslOptions.ca = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.ca))
}

View File

@ -39,7 +39,7 @@ module.exports = {
const parentPath = parentHierarchy.map(h => h.slug).join('/')
return _.filter(results, r => {
const path = parentPath ? `${parentPath}/${r.slug}` : r.slug
return WIKI.auth.checkAccess(context.req.user, ['read:assets'], { path });
return WIKI.auth.checkAccess(context.req.user, ['read:assets'], { path })
})
}
},

View File

@ -26,8 +26,8 @@ module.exports = async () => {
// Load middlewares
// ----------------------------------------
var mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares'))
var ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers'))
const mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares'))
const ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers'))
// ----------------------------------------
// Define Express App
@ -169,7 +169,7 @@ module.exports = async () => {
// ----------------------------------------
app.use((req, res, next) => {
var err = new Error('Not Found')
const err = new Error('Not Found')
err.status = 404
next(err)
})

View File

@ -47,8 +47,7 @@ module.exports = class AssetFolder extends Model {
sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id')
})
}).select('*').from('ancestors')
}
else {
} else {
hier = await WIKI.models.knex.withRecursive('ancestors', qb => {
qb.select('id', 'name', 'slug', 'parentId').from('assetFolders').where('id', folderId).union(sqb => {
sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id')

View File

@ -231,7 +231,7 @@ module.exports = class Page extends Model {
*/
static async createPage(opts) {
// -> Validate path
if (opts.path.indexOf('.') >= 0 || opts.path.indexOf(' ') >= 0 || opts.path.indexOf('\\') >= 0 || opts.path.indexOf('//') >= 0) {
if (opts.path.includes('.') || opts.path.includes(' ') || opts.path.includes('\\') || opts.path.includes('//')) {
throw new WIKI.Error.PageIllegalPath()
}
@ -484,7 +484,7 @@ module.exports = class Page extends Model {
}
// -> Validate path
if (opts.destinationPath.indexOf('.') >= 0 || opts.destinationPath.indexOf(' ') >= 0 || opts.destinationPath.indexOf('\\') >= 0 || opts.destinationPath.indexOf('//') >= 0) {
if (opts.destinationPath.includes('.') || opts.destinationPath.includes(' ') || opts.destinationPath.includes('\\') || opts.destinationPath.includes('//')) {
throw new WIKI.Error.PageIllegalPath()
}
@ -514,7 +514,7 @@ module.exports = class Page extends Model {
}
// -> Check for existing page at destination path
const destPage = await await WIKI.models.pages.query().findOne({
const destPage = await WIKI.models.pages.query().findOne({
path: opts.destinationPath,
localeCode: opts.destinationLocale
})
@ -575,7 +575,7 @@ module.exports = class Page extends Model {
path: opts.destinationPath,
mode: 'move'
})
// -> Reconnect Links : Validate invalid links to the new path
await WIKI.models.pages.reconnectLinks({
locale: opts.destinationLocale,
@ -595,7 +595,7 @@ module.exports = class Page extends Model {
if (_.has(opts, 'id')) {
page = await WIKI.models.pages.query().findById(opts.id)
} else {
page = await await WIKI.models.pages.query().findOne({
page = await WIKI.models.pages.query().findOne({
path: opts.path,
localeCode: opts.locale
})

View File

@ -17,7 +17,7 @@ const prefetch = async (element) => {
const contentType = response.headers[`content-type`]
const image = Buffer.from(response.body).toString('base64')
element.attr('src', `data:${contentType};base64,${image}`)
element.removeClass('prefetch-candidate');
element.removeClass('prefetch-candidate')
}
module.exports = {

View File

@ -1,12 +1,12 @@
const renderEm = (tokens, idx, opts, env, slf) => {
const token = tokens[idx];
const token = tokens[idx]
if (token.markup === '_') {
token.tag = 'u';
token.tag = 'u'
}
return slf.renderToken(tokens, idx, opts);
return slf.renderToken(tokens, idx, opts)
}
module.exports = (md) => {
md.renderer.rules.em_open = renderEm;
md.renderer.rules.em_close = renderEm;
md.renderer.rules.em_open = renderEm
md.renderer.rules.em_close = renderEm
}

View File

@ -73,13 +73,8 @@ module.exports = {
continue
}
var closeMarkerMatched = true
for (i = 0; i < closeMarker.length; ++i) {
if (closeMarker[i] !== state.src[start + i]) {
closeMarkerMatched = false
break
}
}
const i = closeMarker.findIndex(item => item !== state.src[start + i])
const closeMarkerMatched = i !== -1
if (!closeMarkerMatched) {
continue

View File

@ -74,13 +74,8 @@ module.exports = {
continue
}
var closeMarkerMatched = true
for (i = 0; i < closeMarker.length; ++i) {
if (closeMarker[i] !== state.src[start + i]) {
closeMarkerMatched = false
break
}
}
const i = closeMarker.findIndex(item => item !== state.src[start + i])
const closeMarkerMatched = i !== -1
if (!closeMarkerMatched) {
continue
@ -112,7 +107,7 @@ module.exports = {
altToken
)
var zippedCode = encode64(zlib.deflateRawSync('@startuml\n' + contents + '\n@enduml').toString('binary'))
const zippedCode = encode64(zlib.deflateRawSync('@startuml\n' + contents + '\n@enduml').toString('binary'))
token = state.push('uml_diagram', 'img', 0)
// alt is constructed from children. No point in populating it here.

View File

@ -104,7 +104,7 @@ module.exports = {
},
{
query_string: {
query: "*" + q + "*"
query: `*${q}*`
}
}
],

View File

@ -378,7 +378,7 @@ module.exports = () => {
// ----------------------------------------
app.use(function (req, res, next) {
var err = new Error('Not Found')
const err = new Error('Not Found')
err.status = 404
next(err)
})

View File

@ -7,7 +7,7 @@ describe('helpers/page/injectPageMetadata', () => {
isPublished: true,
updatedAt: new Date(),
content: 'TEST CONTENT',
createdAt: new Date('2019-01-01'),
createdAt: new Date('2019-01-01')
}
it('returns the page content by default when content type is unknown', () => {
@ -20,7 +20,7 @@ describe('helpers/page/injectPageMetadata', () => {
const markdownPage = {
...page,
contentType: 'markdown',
editorKey: 'markdown',
editorKey: 'markdown'
}
const expected = `---
@ -42,7 +42,7 @@ TEST CONTENT`
const htmlPage = {
...page,
contentType: 'html',
editorKey: 'html',
editorKey: 'html'
}
const expected = `<!--