refactor: server code ()

+ 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
server
core
graph/resolvers
master.js
models
modules
rendering
html-image-prefetch
markdown-core
markdown-kroki
markdown-plantuml
search/elasticsearch
setup.js
test/helpers

View File

@ -43,8 +43,7 @@ module.exports = {
let sslOptions = null let sslOptions = null
if (dbUseSSL && _.isPlainObject(dbConfig) && _.get(WIKI.config.db, 'sslOptions.auto', null) === false) { if (dbUseSSL && _.isPlainObject(dbConfig) && _.get(WIKI.config.db, 'sslOptions.auto', null) === false) {
sslOptions = WIKI.config.db.sslOptions sslOptions = WIKI.config.db.sslOptions
// eslint-disable-next-line no-unneeded-ternary sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized !== false
sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized === false ? false : true
if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) { if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) {
sslOptions.ca = fs.readFileSync(path.resolve(WIKI.ROOTPATH, sslOptions.ca)) 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('/') const parentPath = parentHierarchy.map(h => h.slug).join('/')
return _.filter(results, r => { return _.filter(results, r => {
const path = parentPath ? `${parentPath}/${r.slug}` : r.slug 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 // Load middlewares
// ---------------------------------------- // ----------------------------------------
var mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares')) const mw = autoload(path.join(WIKI.SERVERPATH, '/middlewares'))
var ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers')) const ctrl = autoload(path.join(WIKI.SERVERPATH, '/controllers'))
// ---------------------------------------- // ----------------------------------------
// Define Express App // Define Express App
@ -169,7 +169,7 @@ module.exports = async () => {
// ---------------------------------------- // ----------------------------------------
app.use((req, res, next) => { app.use((req, res, next) => {
var err = new Error('Not Found') const err = new Error('Not Found')
err.status = 404 err.status = 404
next(err) 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') sqb.select('a.id', 'a.name', 'a.slug', 'a.parentId').from('assetFolders AS a').join('ancestors', 'ancestors.parentId', 'a.id')
}) })
}).select('*').from('ancestors') }).select('*').from('ancestors')
} } else {
else {
hier = await WIKI.models.knex.withRecursive('ancestors', qb => { hier = await WIKI.models.knex.withRecursive('ancestors', qb => {
qb.select('id', 'name', 'slug', 'parentId').from('assetFolders').where('id', folderId).union(sqb => { 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') 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) { static async createPage(opts) {
// -> Validate path // -> 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() throw new WIKI.Error.PageIllegalPath()
} }
@ -484,7 +484,7 @@ module.exports = class Page extends Model {
} }
// -> Validate path // -> 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() throw new WIKI.Error.PageIllegalPath()
} }
@ -514,7 +514,7 @@ module.exports = class Page extends Model {
} }
// -> Check for existing page at destination path // -> 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, path: opts.destinationPath,
localeCode: opts.destinationLocale localeCode: opts.destinationLocale
}) })
@ -575,7 +575,7 @@ module.exports = class Page extends Model {
path: opts.destinationPath, path: opts.destinationPath,
mode: 'move' mode: 'move'
}) })
// -> Reconnect Links : Validate invalid links to the new path // -> Reconnect Links : Validate invalid links to the new path
await WIKI.models.pages.reconnectLinks({ await WIKI.models.pages.reconnectLinks({
locale: opts.destinationLocale, locale: opts.destinationLocale,
@ -595,7 +595,7 @@ module.exports = class Page extends Model {
if (_.has(opts, 'id')) { if (_.has(opts, 'id')) {
page = await WIKI.models.pages.query().findById(opts.id) page = await WIKI.models.pages.query().findById(opts.id)
} else { } else {
page = await await WIKI.models.pages.query().findOne({ page = await WIKI.models.pages.query().findOne({
path: opts.path, path: opts.path,
localeCode: opts.locale localeCode: opts.locale
}) })

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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