Node: Migrate to ES Modules

This commit is contained in:
Cohee
2024-10-10 22:37:22 +03:00
parent 5a52196331
commit d52b4fbbde
74 changed files with 1291 additions and 1140 deletions

View File

@ -2,9 +2,9 @@
* When applied, this middleware will ensure the request contains the required header for basic authentication and only
* allow access to the endpoint after successful authentication.
*/
const { getAllUserHandles, toKey, getPasswordHash } = require('../users.js');
const { getConfig, getConfigValue } = require('../util.js');
const storage = require('node-persist');
import storage from 'node-persist';
import { getAllUserHandles, toKey, getPasswordHash } from '../users.js';
import { getConfig, getConfigValue } from '../util.js';
const PER_USER_BASIC_AUTH = getConfigValue('perUserBasicAuth', false);
const ENABLE_ACCOUNTS = getConfigValue('enableUserAccounts', false);
@ -49,4 +49,4 @@ const basicAuthMiddleware = async function (request, response, callback) {
return unauthorizedResponse(response);
};
module.exports = basicAuthMiddleware;
export default basicAuthMiddleware;

View File

@ -14,7 +14,7 @@ function decodeFileName(str) {
* @param {import('express').Response} _res Response
* @param {import('express').NextFunction} next Next middleware
*/
function multerMonkeyPatch(req, _res, next) {
export default function multerMonkeyPatch(req, _res, next) {
try {
if (req.file) {
req.file.originalname = decodeFileName(req.file.originalname);
@ -26,5 +26,3 @@ function multerMonkeyPatch(req, _res, next) {
next();
}
}
module.exports = multerMonkeyPatch;

View File

@ -1,9 +1,9 @@
const path = require('path');
const fs = require('fs');
const ipMatching = require('ip-matching');
import * as path from 'node:path';
import * as fs from 'node:fs';
import ipMatching from 'ip-matching';
const { getIpFromRequest } = require('../express-common');
const { color, getConfigValue } = require('../util');
import { getIpFromRequest } from '../express-common.js';
import { color, getConfigValue } from '../util.js';
const whitelistPath = path.join(process.cwd(), './whitelist.txt');
const enableForwardedWhitelist = getConfigValue('enableForwardedWhitelist', false);
@ -50,7 +50,7 @@ function getForwardedIp(req) {
* @param {boolean} listen If listen mode is enabled via config or command line
* @returns {import('express').RequestHandler} The middleware function
*/
function whitelistMiddleware(whitelistMode, listen) {
export default function whitelistMiddleware(whitelistMode, listen) {
return function (req, res, next) {
const clientIp = getIpFromRequest(req);
const forwardedIp = getForwardedIp(req);
@ -82,5 +82,3 @@ function whitelistMiddleware(whitelistMode, listen) {
next();
};
}
module.exports = whitelistMiddleware;