refactor: convert to esm (wip)

pull/6775/head
NGPixel 2 years ago
parent e9e93eff42
commit 8f94449426
No known key found for this signature in database
GPG Key ID: B755FB6870B30F63

@ -78,7 +78,7 @@ export default {
const enabledStrategies = await WIKI.db.authentication.getStrategies({ enabledOnly: true }) const enabledStrategies = await WIKI.db.authentication.getStrategies({ enabledOnly: true })
for (const stg of enabledStrategies) { for (const stg of enabledStrategies) {
try { try {
const strategy = require(`../modules/authentication/${stg.module}/authentication.js`) const strategy = (await import(`../modules/authentication/${stg.module}/authentication.mjs`)).default
stg.config.callbackURL = `${WIKI.config.host}/login/${stg.id}/callback` stg.config.callbackURL = `${WIKI.config.host}/login/${stg.id}/callback`
stg.config.key = stg.id stg.config.key = stg.id

@ -20,7 +20,7 @@ export default {
this.maxWorkers = WIKI.config.scheduler.workers === 'auto' ? (os.cpus().length - 1) : WIKI.config.scheduler.workers this.maxWorkers = WIKI.config.scheduler.workers === 'auto' ? (os.cpus().length - 1) : WIKI.config.scheduler.workers
if (this.maxWorkers < 1) { this.maxWorkers = 1 } if (this.maxWorkers < 1) { this.maxWorkers = 1 }
WIKI.logger.info(`Initializing Worker Pool (Limit: ${this.maxWorkers})...`) WIKI.logger.info(`Initializing Worker Pool (Limit: ${this.maxWorkers})...`)
this.workerPool = new DynamicThreadPool(1, this.maxWorkers, path.join(WIKI.SERVERPATH, 'worker.js'), { this.workerPool = new DynamicThreadPool(1, this.maxWorkers, path.join(WIKI.SERVERPATH, 'worker.mjs'), {
errorHandler: (err) => WIKI.logger.warn(err), errorHandler: (err) => WIKI.logger.warn(err),
exitHandler: () => WIKI.logger.debug('A worker has gone offline.'), exitHandler: () => WIKI.logger.debug('A worker has gone offline.'),
onlineHandler: () => WIKI.logger.debug('New worker is online.') onlineHandler: () => WIKI.logger.debug('New worker is online.')

@ -81,9 +81,9 @@ export function parseModuleProps (props) {
return transform(props, (result, value, key) => { return transform(props, (result, value, key) => {
let defaultValue = '' let defaultValue = ''
if (isPlainObject(value)) { if (isPlainObject(value)) {
defaultValue = !isNil(value.default) ? value.default : this.getTypeDefaultValue(value.type) defaultValue = !isNil(value.default) ? value.default : getTypeDefaultValue(value.type)
} else { } else {
defaultValue = this.getTypeDefaultValue(value) defaultValue = getTypeDefaultValue(value)
} }
set(result, key, { set(result, key, {
default: defaultValue, default: defaultValue,

@ -1,5 +1,5 @@
import qs from 'querystring' import qs from 'querystring'
import { fromPairs, get, initial, invert, isEmpty, last } from 'lodash-es' import { fromPairs, get, head, initial, invert, isEmpty, last } from 'lodash-es'
import crypto from 'node:crypto' import crypto from 'node:crypto'
import path from 'node:path' import path from 'node:path'
@ -59,7 +59,7 @@ export function parsePath (rawPath, opts = {}) {
} }
} }
pathObj.path = _.join(pathParts, '/') pathObj.path = pathParts.join('/')
return pathObj return pathObj
} }
@ -102,7 +102,7 @@ export function injectPageMetadata(page) {
* Check if path is a reserved path * Check if path is a reserved path
*/ */
export function isReservedPath(rawPath) { export function isReservedPath(rawPath) {
const firstSection = _.head(rawPath.split('/')) const firstSection = head(rawPath.split('/'))
if (firstSection.length < 1) { if (firstSection.length < 1) {
return true return true
} else if (localeSegmentRegex.test(firstSection)) { } else if (localeSegmentRegex.test(firstSection)) {

@ -1,5 +1,5 @@
import { Model } from 'objection' import { Model } from 'objection'
import { has } from 'lodash-es' import { has, intersection } from 'lodash-es'
/** /**
* Navigation model * Navigation model
@ -59,7 +59,7 @@ export class Navigation extends Model {
static getAuthorizedItems(tree = [], groups = []) { static getAuthorizedItems(tree = [], groups = []) {
return tree.filter(leaf => { return tree.filter(leaf => {
return leaf.visibilityMode === 'all' || _.intersection(leaf.visibilityGroups, groups).length > 0 return leaf.visibilityMode === 'all' || intersection(leaf.visibilityGroups, groups).length > 0
}) })
} }
} }

@ -1,5 +1,5 @@
import { Model } from 'objection' import { Model } from 'objection'
import { find, get, has, isEmpty, isString, pick } from 'lodash-es' import { find, get, has, initial, isEmpty, isString, last, pick } from 'lodash-es'
import { Type as JSBinType } from 'js-binary' import { Type as JSBinType } from 'js-binary'
import { generateHash, getFileExtension, injectPageMetadata } from '../helpers/page.mjs' import { generateHash, getFileExtension, injectPageMetadata } from '../helpers/page.mjs'
import path from 'node:path' import path from 'node:path'
@ -272,7 +272,7 @@ export class Page extends Model {
} }
// -> Check for empty content // -> Check for empty content
if (!opts.content || _.trim(opts.content).length < 1) { if (!opts.content || opts.content.trim().length < 1) {
throw new WIKI.Error.PageEmptyContent() throw new WIKI.Error.PageEmptyContent()
} }
@ -282,7 +282,7 @@ export class Page extends Model {
locale: opts.locale, locale: opts.locale,
path: opts.path path: opts.path
})) { })) {
if (!_.isEmpty(opts.scriptCss)) { if (!isEmpty(opts.scriptCss)) {
scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
} else { } else {
scriptCss = '' scriptCss = ''
@ -350,8 +350,8 @@ export class Page extends Model {
const pathParts = page.path.split('/') const pathParts = page.path.split('/')
await WIKI.db.tree.addPage({ await WIKI.db.tree.addPage({
id: page.id, id: page.id,
parentPath: _.initial(pathParts).join('/'), parentPath: initial(pathParts).join('/'),
fileName: _.last(pathParts), fileName: last(pathParts),
locale: page.localeCode, locale: page.localeCode,
title: page.title, title: page.title,
meta: { meta: {

@ -1,7 +1,7 @@
import { Model } from 'objection' import { Model } from 'objection'
import path from 'node:path' import path from 'node:path'
import fs from 'node:fs/promises' import fs from 'node:fs/promises'
import { capitalize, find, has, hasIn, uniq } from 'lodash-es' import { capitalize, find, has, hasIn, remove, uniq } from 'lodash-es'
import yaml from 'js-yaml' import yaml from 'js-yaml'
/** /**
@ -68,7 +68,7 @@ export class Storage extends Model {
static async ensureModule (moduleName) { static async ensureModule (moduleName) {
if (!has(WIKI.storage.modules, moduleName)) { if (!has(WIKI.storage.modules, moduleName)) {
try { try {
WIKI.storage.modules[moduleName] = require(`../modules/storage/${moduleName}/storage`) WIKI.storage.modules[moduleName] = (await import(`../modules/storage/${moduleName}/storage.mjs`)).default
WIKI.logger.debug(`Activated storage module ${moduleName}: [ OK ]`) WIKI.logger.debug(`Activated storage module ${moduleName}: [ OK ]`)
return true return true
} catch (err) { } catch (err) {
@ -89,7 +89,7 @@ export class Storage extends Model {
const activeModules = uniq(dbTargets.map(t => t.module)) const activeModules = uniq(dbTargets.map(t => t.module))
try { try {
// -> Stop and delete existing jobs // -> Stop and delete existing jobs
// const prevjobs = _.remove(WIKI.scheduler.jobs, job => job.name === 'sync-storage') // const prevjobs = remove(WIKI.scheduler.jobs, job => job.name === 'sync-storage')
// if (prevjobs.length > 0) { // if (prevjobs.length > 0) {
// prevjobs.forEach(job => job.stop()) // prevjobs.forEach(job => job.stop())
// } // }
@ -101,7 +101,7 @@ export class Storage extends Model {
// -> Initialize targets // -> Initialize targets
// for (const target of this.targets) { // for (const target of this.targets) {
// const targetDef = _.find(WIKI.data.storage, ['key', target.key]) // const targetDef = find(WIKI.data.storage, ['key', target.key])
// target.fn = require(`../modules/storage/${target.key}/storage`) // target.fn = require(`../modules/storage/${target.key}/storage`)
// target.fn.config = target.config // target.fn.config = target.config
// target.fn.mode = target.mode // target.fn.mode = target.mode

@ -73,7 +73,7 @@ export class Tag extends Model {
// Fetch current page tags // Fetch current page tags
const targetTags = existingTags.filter(t => _.includes(tags, t.tag)) const targetTags = existingTags.filter(t => tags.includes(t.tag))
const currentTags = await page.$relatedQuery('tags') const currentTags = await page.$relatedQuery('tags')
// Tags to relate // Tags to relate

@ -1,6 +1,6 @@
/* global WIKI */ /* global WIKI */
import { difference, find, first, flatten, flattenDeep, get, has, isArray, isEmpty, isNil, last, set, toString, truncate, uniq } from 'lodash-es' import { difference, find, first, flatten, flattenDeep, get, has, isArray, isEmpty, isNil, isString, last, set, toString, truncate, uniq } from 'lodash-es'
import tfa from 'node-2fa' import tfa from 'node-2fa'
import jwt from 'jsonwebtoken' import jwt from 'jsonwebtoken'
import { Model } from 'objection' import { Model } from 'objection'

@ -1,16 +1,16 @@
/* global WIKI */ /* global WIKI */
const bcrypt = require('bcryptjs-then') import bcrypt from 'bcryptjs-then'
// ------------------------------------ // ------------------------------------
// Local Account // Local Account
// ------------------------------------ // ------------------------------------
const LocalStrategy = require('passport-local').Strategy import { Strategy } from 'passport-local'
module.exports = { export default {
init (passport, conf) { init (passport, conf) {
passport.use(conf.key, passport.use(conf.key,
new LocalStrategy({ new Strategy({
usernameField: 'email', usernameField: 'email',
passwordField: 'password' passwordField: 'password'
}, async (uEmail, uPassword, done) => { }, async (uEmail, uPassword, done) => {

@ -1,6 +1,6 @@
const cmdExists = require('command-exists') import cmdExists from 'command-exists'
module.exports = { export default {
key: 'git', key: 'git',
title: 'Git', title: 'Git',
description: 'Distributed version control system. Required for the Git storage module.', description: 'Distributed version control system. Required for the Git storage module.',

@ -1,7 +1,7 @@
const cmdExists = require('command-exists') import cmdExists from 'command-exists'
const os = require('os') import os from 'node:os'
module.exports = { export default {
key: 'pandoc', key: 'pandoc',
title: 'Pandoc', title: 'Pandoc',
description: 'Convert between markup formats. Required for converting from other formats such as MediaWiki, AsciiDoc, Textile and other wikis.', description: 'Convert between markup formats. Required for converting from other formats such as MediaWiki, AsciiDoc, Textile and other wikis.',

@ -1,10 +1,12 @@
const os = require('os') import os from 'node:os'
const path = require('path') import path from 'node:path'
const util = require('util') import util from 'node:util'
const exec = util.promisify(require('child_process').exec) import { exec as execSync } from 'node:child_process'
const fs = require('fs-extra') import fse from 'fs-extra'
module.exports = { const exec = util.promisify(execSync)
export default {
key: 'puppeteer', key: 'puppeteer',
title: 'Puppeteer', title: 'Puppeteer',
description: 'Headless chromium browser for server-side rendering. Required for generating PDF versions of pages and render content elements on the server (e.g. Mermaid diagrams)', description: 'Headless chromium browser for server-side rendering. Required for generating PDF versions of pages and render content elements on the server (e.g. Mermaid diagrams)',
@ -15,7 +17,7 @@ module.exports = {
isInstallable: true, isInstallable: true,
async check () { async check () {
try { try {
this.isInstalled = await fs.pathExists(path.join(WIKI.SERVERPATH, 'node_modules/puppeteer-core/.local-chromium')) this.isInstalled = await fse.pathExists(path.join(WIKI.SERVERPATH, 'node_modules/puppeteer-core/.local-chromium'))
} catch (err) { } catch (err) {
this.isInstalled = false this.isInstalled = false
} }

@ -1,11 +1,13 @@
const fs = require('fs-extra') import fse from 'fs-extra'
const os = require('os') import os from 'node:os'
const path = require('path') import path from 'node:path'
const util = require('util') import util from 'node:util'
const exec = util.promisify(require('child_process').exec) import { exec as execSync } from 'node:child_process'
const { pipeline } = require('stream/promises') import { pipeline } from 'node:stream/promises'
module.exports = { const exec = util.promisify(execSync)
export default {
key: 'sharp', key: 'sharp',
title: 'Sharp', title: 'Sharp',
description: 'Process and transform images. Required to generate thumbnails of uploaded images and perform transformations.', description: 'Process and transform images. Required to generate thumbnails of uploaded images and perform transformations.',
@ -15,7 +17,7 @@ module.exports = {
isInstalled: false, isInstalled: false,
isInstallable: true, isInstallable: true,
async check () { async check () {
this.isInstalled = await fs.pathExists(path.join(WIKI.SERVERPATH, 'node_modules/sharp/wiki_installed.txt')) this.isInstalled = await fse.pathExists(path.join(WIKI.SERVERPATH, 'node_modules/sharp/wiki_installed.txt'))
return this.isInstalled return this.isInstalled
}, },
async install () { async install () {
@ -25,7 +27,7 @@ module.exports = {
timeout: 120000, timeout: 120000,
windowsHide: true windowsHide: true
}) })
await fs.ensureFile(path.join(WIKI.SERVERPATH, 'node_modules/sharp/wiki_installed.txt')) await fse.ensureFile(path.join(WIKI.SERVERPATH, 'node_modules/sharp/wiki_installed.txt'))
this.isInstalled = true this.isInstalled = true
WIKI.logger.info(stdout) WIKI.logger.info(stdout)
WIKI.logger.warn(stderr) WIKI.logger.warn(stderr)
@ -36,7 +38,7 @@ module.exports = {
sharp: null, sharp: null,
async load () { async load () {
if (!this.sharp) { if (!this.sharp) {
this.sharp = require('sharp') this.sharp = (await import('sharp')).default
} }
}, },
/** /**
@ -57,13 +59,13 @@ module.exports = {
this.load() this.load()
if (inputPath) { if (inputPath) {
inputStream = fs.createReadStream(inputPath) inputStream = fse.createReadStream(inputPath)
} }
if (!inputStream) { if (!inputStream) {
throw new Error('Failed to open readable input stream for image resizing.') throw new Error('Failed to open readable input stream for image resizing.')
} }
if (outputPath) { if (outputPath) {
outputStream = fs.createWriteStream(outputPath) outputStream = fse.createWriteStream(outputPath)
} }
if (!outputStream) { if (!outputStream) {
throw new Error('Failed to open writable output stream for image resizing.') throw new Error('Failed to open writable output stream for image resizing.')

@ -6,7 +6,7 @@ const URL = require('url').URL
const mustacheRegExp = /(\{|&#x7b;?){2}(.+?)(\}|&#x7d;?){2}/i const mustacheRegExp = /(\{|&#x7b;?){2}(.+?)(\}|&#x7d;?){2}/i
module.exports = { export default {
async render() { async render() {
const $ = cheerio.load(this.input, { const $ = cheerio.load(this.input, {
decodeEntities: true decodeEntities: true
@ -21,7 +21,7 @@ module.exports = {
// -------------------------------- // --------------------------------
for (let child of _.reject(this.children, ['step', 'post'])) { for (let child of _.reject(this.children, ['step', 'post'])) {
const renderer = require(`../${child.key}/renderer.js`) const renderer = require(`../${child.key}/renderer.mjs`)
await renderer.init($, child.config) await renderer.init($, child.config)
} }

@ -1,6 +1,6 @@
const fs = require('fs-extra') import fs from 'node:fs/promises'
module.exports = { export default {
async activated () { }, async activated () { },
async deactivated () { }, async deactivated () { },
async init () { }, async init () { },

@ -24,8 +24,8 @@ export async function task ({ payload }) {
WIKI.logger.warn(`Failed to render page ID ${payload.id} because content was empty: [ FAILED ]`) WIKI.logger.warn(`Failed to render page ID ${payload.id} because content was empty: [ FAILED ]`)
} }
for (let core of pipeline) { for (const core of pipeline) {
const renderer = require(`../../modules/rendering/${core.key}/renderer.js`) const renderer = (await import(`../../modules/rendering/${core.key}/renderer.mjs`)).default
output = await renderer.render.call({ output = await renderer.render.call({
config: core.config, config: core.config,
children: core.children, children: core.children,

@ -1,6 +1,9 @@
import { ThreadWorker } from 'poolifier' import { ThreadWorker } from 'poolifier'
import { kebabCase } from 'lodash-es' import { kebabCase } from 'lodash-es'
import path from 'node:path' import path from 'node:path'
import configSvc from './core/config.mjs'
import logger from './core/logger.mjs'
import db from './core/db.mjs'
// ---------------------------------------- // ----------------------------------------
// Init Minimal Core // Init Minimal Core
@ -11,12 +14,11 @@ const WIKI = {
ROOTPATH: process.cwd(), ROOTPATH: process.cwd(),
INSTANCE_ID: 'worker', INSTANCE_ID: 'worker',
SERVERPATH: path.join(process.cwd(), 'server'), SERVERPATH: path.join(process.cwd(), 'server'),
Error: require('./helpers/error'), configSvc,
configSvc: require('./core/config'),
ensureDb: async () => { ensureDb: async () => {
if (WIKI.db) { return true } if (WIKI.db) { return true }
WIKI.db = require('./core/db').init(true) WIKI.db = await db.init(true)
try { try {
await WIKI.configSvc.loadFromDb() await WIKI.configSvc.loadFromDb()
@ -32,8 +34,13 @@ const WIKI = {
} }
global.WIKI = WIKI global.WIKI = WIKI
WIKI.configSvc.init(true) await WIKI.configSvc.init(true)
WIKI.logger = require('./core/logger').init()
// ----------------------------------------
// Init Logger
// ----------------------------------------
WIKI.logger = logger.init()
// ---------------------------------------- // ----------------------------------------
// Execute Task // Execute Task
Loading…
Cancel
Save