Просмотр исходного кода

refactor: convert to esm (wip)

NGPixel 2 лет назад
Родитель
Сommit
8f94449426

+ 1 - 1
server/core/auth.mjs

@@ -78,7 +78,7 @@ export default {
       const enabledStrategies = await WIKI.db.authentication.getStrategies({ enabledOnly: true })
       for (const stg of enabledStrategies) {
         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.key = stg.id

+ 1 - 1
server/core/scheduler.mjs

@@ -20,7 +20,7 @@ export default {
     this.maxWorkers = WIKI.config.scheduler.workers === 'auto' ? (os.cpus().length - 1) : WIKI.config.scheduler.workers
     if (this.maxWorkers < 1) { this.maxWorkers = 1 }
     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),
       exitHandler: () => WIKI.logger.debug('A worker has gone offline.'),
       onlineHandler: () => WIKI.logger.debug('New worker is online.')

+ 2 - 2
server/helpers/common.mjs

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

+ 3 - 3
server/helpers/page.mjs

@@ -1,5 +1,5 @@
 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 path from 'node:path'
 
@@ -59,7 +59,7 @@ export function parsePath (rawPath, opts = {}) {
     }
   }
 
-  pathObj.path = _.join(pathParts, '/')
+  pathObj.path = pathParts.join('/')
   return pathObj
 }
 
@@ -102,7 +102,7 @@ export function injectPageMetadata(page) {
  * Check if path is a reserved path
  */
 export function isReservedPath(rawPath) {
-  const firstSection = _.head(rawPath.split('/'))
+  const firstSection = head(rawPath.split('/'))
   if (firstSection.length < 1) {
     return true
   } else if (localeSegmentRegex.test(firstSection)) {

+ 2 - 2
server/models/navigation.mjs

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

+ 5 - 5
server/models/pages.mjs

@@ -1,5 +1,5 @@
 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 { generateHash, getFileExtension, injectPageMetadata } from '../helpers/page.mjs'
 import path from 'node:path'
@@ -272,7 +272,7 @@ export class Page extends Model {
     }
 
     // -> 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()
     }
 
@@ -282,7 +282,7 @@ export class Page extends Model {
       locale: opts.locale,
       path: opts.path
     })) {
-      if (!_.isEmpty(opts.scriptCss)) {
+      if (!isEmpty(opts.scriptCss)) {
         scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
       } else {
         scriptCss = ''
@@ -350,8 +350,8 @@ export class Page extends Model {
     const pathParts = page.path.split('/')
     await WIKI.db.tree.addPage({
       id: page.id,
-      parentPath: _.initial(pathParts).join('/'),
-      fileName: _.last(pathParts),
+      parentPath: initial(pathParts).join('/'),
+      fileName: last(pathParts),
       locale: page.localeCode,
       title: page.title,
       meta: {

+ 4 - 4
server/models/storage.mjs

@@ -1,7 +1,7 @@
 import { Model } from 'objection'
 import path from 'node:path'
 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'
 
 /**
@@ -68,7 +68,7 @@ export class Storage extends Model {
   static async ensureModule (moduleName) {
     if (!has(WIKI.storage.modules, moduleName)) {
       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 ]`)
         return true
       } catch (err) {
@@ -89,7 +89,7 @@ export class Storage extends Model {
     const activeModules = uniq(dbTargets.map(t => t.module))
     try {
       // -> 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) {
       //   prevjobs.forEach(job => job.stop())
       // }
@@ -101,7 +101,7 @@ export class Storage extends Model {
 
       // -> Initialize 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.config = target.config
       //   target.fn.mode = target.mode

+ 1 - 1
server/models/tags.mjs

@@ -73,7 +73,7 @@ export class Tag extends Model {
 
     // 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')
 
     // Tags to relate

+ 1 - 1
server/models/users.mjs

@@ -1,6 +1,6 @@
 /* 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 jwt from 'jsonwebtoken'
 import { Model } from 'objection'

+ 4 - 4
server/modules/authentication/local/authentication.js → server/modules/authentication/local/authentication.mjs

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

+ 2 - 2
server/modules/extensions/git/ext.js → server/modules/extensions/git/ext.mjs

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

+ 3 - 3
server/modules/extensions/pandoc/ext.js → server/modules/extensions/pandoc/ext.mjs

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

+ 9 - 7
server/modules/extensions/puppeteer/ext.js → server/modules/extensions/puppeteer/ext.mjs

@@ -1,10 +1,12 @@
-const os = require('os')
-const path = require('path')
-const util = require('util')
-const exec = util.promisify(require('child_process').exec)
-const fs = require('fs-extra')
+import os from 'node:os'
+import path from 'node:path'
+import util from 'node:util'
+import { exec as execSync } from 'node:child_process'
+import fse from 'fs-extra'
 
-module.exports = {
+const exec = util.promisify(execSync)
+
+export default {
   key: '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)',
@@ -15,7 +17,7 @@ module.exports = {
   isInstallable: true,
   async check () {
     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) {
       this.isInstalled = false
     }

+ 14 - 12
server/modules/extensions/sharp/ext.js → server/modules/extensions/sharp/ext.mjs

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

+ 2 - 2
server/modules/rendering/html-core/renderer.js → server/modules/rendering/html-core/renderer.mjs

@@ -6,7 +6,7 @@ const URL = require('url').URL
 
 const mustacheRegExp = /(\{|&#x7b;?){2}(.+?)(\}|&#x7d;?){2}/i
 
-module.exports = {
+export default {
   async render() {
     const $ = cheerio.load(this.input, {
       decodeEntities: true
@@ -21,7 +21,7 @@ module.exports = {
     // --------------------------------
 
     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)
     }
 

+ 2 - 2
server/modules/storage/db/storage.js → server/modules/storage/db/storage.mjs

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

+ 2 - 2
server/tasks/workers/render-page.mjs

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

+ 12 - 5
server/worker.js → server/worker.mjs

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