switch from require to import of modules and update webdav

This commit is contained in:
Raphael 2023-11-27 13:18:11 +01:00
parent 1a4f9c3808
commit 585d419153
23 changed files with 1161 additions and 1014 deletions

View File

@ -1,8 +1,12 @@
const webpack = require("webpack");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
import webpack from "webpack";
import { CleanWebpackPlugin } from "clean-webpack-plugin";
import CopyPlugin from "copy-webpack-plugin";
import HtmlWebpackPlugin from "html-webpack-plugin";
import path from "path";
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const config = {
entry: {
@ -57,4 +61,4 @@ const config = {
],
};
module.exports = config;
export { config as default };

View File

@ -1,7 +1,7 @@
const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base");
import { merge } from "webpack-merge";
import baseConfig from "./webpack.base.js"
module.exports = merge(baseConfig, {
export default merge(baseConfig, {
mode: "production",
performance: {
hints: false,
@ -13,4 +13,4 @@ module.exports = merge(baseConfig, {
nodeEnv: "production",
},
devtool: false,
});
});

View File

@ -1,6 +1,7 @@
const { merge } = require("webpack-merge");
const baseConfig = require("./webpack.base");
const webpack = require("webpack");
import baseConfig from "./webpack.base.js";
import { merge } from "webpack-merge";
import webpack from "webpack";
const devConfig = merge(baseConfig, {
mode: "development",
@ -11,4 +12,4 @@ const devConfig = merge(baseConfig, {
plugins: [new webpack.NoEmitOnErrorsPlugin()].concat(baseConfig.plugins),
});
module.exports = devConfig;
export { devConfig as default };

1920
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@
"description": "Collaborative Whiteboard / Sketchboard",
"main": "server.js",
"directories": {},
"type": "module",
"scripts": {
"build": "webpack --config config/webpack.build.js",
"start:dev": "apidoc -i scripts/ -o ./public/apidoc/ && node scripts/server.js --mode=development",
@ -44,7 +45,7 @@
"signature_pad": "^4.0.1",
"socket.io-client": "^4.4.0",
"uuid": "^8.1.0",
"webdav": "^4.8.0"
"webdav": "^5.3.0"
},
"devDependencies": {
"@babel/cli": "^7.16.8",
@ -102,4 +103,4 @@
]
]
}
}
}

View File

@ -1,8 +1,8 @@
const util = require("util");
import util from "util";
const { getDefaultConfig, getConfig, deepMergeConfigs, isConfigValid } = require("./utils");
import { getDefaultConfig, getConfig, deepMergeConfigs, isConfigValid } from "./utils.js"
const { getArgs } = require("./../utils");
import { getArgs } from "./../utils.js"
const defaultConfig = getDefaultConfig();
@ -80,4 +80,4 @@ if (!process.env.JEST_WORKER_ID) {
console.info(util.inspect(config, { showHidden: false, depth: null, colors: true }));
}
module.exports = config;
export { config as default };

View File

@ -1,11 +1,16 @@
const path = require("path");
const fs = require("fs");
const yaml = require("js-yaml");
import path from "path";
import fs from "fs";
import yaml from "js-yaml";
const Ajv = require("ajv");
import Ajv from "ajv";
const ajv = new Ajv({ allErrors: true });
const configSchema = require("./config-schema.json");
import configSchema from "./config-schema.json" assert { type: "json" };
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Load a yaml config file from a given path.
@ -13,7 +18,7 @@ const configSchema = require("./config-schema.json");
* @param path
* @return {Object}
*/
function getConfig(path) {
export function getConfig(path) {
return yaml.safeLoad(fs.readFileSync(path, "utf8"));
}
@ -24,7 +29,7 @@ function getConfig(path) {
* @param {boolean} warn Should we warn in console for errors
* @return {boolean}
*/
function isConfigValid(config, warn = true) {
export function isConfigValid(config, warn = true) {
const validate = ajv.compile(configSchema);
const isValidAgainstSchema = validate(config);
@ -44,7 +49,7 @@ function isConfigValid(config, warn = true) {
if (!structureIsValid && warn)
console.warn(
"At least one item under frontend.performance.pointerEventsThrottling" +
"must have fromUserCount set to 0"
"must have fromUserCount set to 0"
);
return isValidAgainstSchema && structureIsValid;
@ -54,7 +59,7 @@ function isConfigValid(config, warn = true) {
* Load the default project config
* @return {Object}
*/
function getDefaultConfig() {
export function getDefaultConfig() {
const defaultConfigPath = path.join(__dirname, "..", "..", "config.default.yml");
return getConfig(defaultConfigPath);
}
@ -68,7 +73,7 @@ function getDefaultConfig() {
* @param overrideConfig
* @return {Object}
*/
function deepMergeConfigs(baseConfig, overrideConfig) {
export function deepMergeConfigs(baseConfig, overrideConfig) {
const out = {};
Object.entries(baseConfig).forEach(([key, val]) => {
@ -85,8 +90,3 @@ function deepMergeConfigs(baseConfig, overrideConfig) {
return out;
}
module.exports.getConfig = getConfig;
module.exports.getDefaultConfig = getDefaultConfig;
module.exports.deepMergeConfigs = deepMergeConfigs;
module.exports.isConfigValid = isConfigValid;

View File

@ -1,7 +1,7 @@
//This file is only for saving the whiteboard.
const fs = require("fs");
const config = require("./config/config");
const { getSafeFilePath } = require("./utils");
import fs from "fs";
import config from "./config/config.js";
import { getSafeFilePath } from "./utils.js";
const FILE_DATABASE_FOLDER = "savedBoards";
var savedBoards = {};
@ -26,7 +26,7 @@ function fileDatabasePath(wid) {
return getSafeFilePath(FILE_DATABASE_FOLDER, wid + ".json");
}
module.exports = {
const s_whiteboard = {
handleEventsAndData: function (content) {
var tool = content["t"]; //Tool witch is used
var wid = content["wid"]; //whiteboard ID
@ -184,3 +184,6 @@ module.exports = {
this.saveToDB(wid);
},
};
export { s_whiteboard as default };

View File

@ -1,29 +1,41 @@
const path = require("path");
import path from "path";
const config = require("./config/config");
const ReadOnlyBackendService = require("./services/ReadOnlyBackendService");
const WhiteboardInfoBackendService = require("./services/WhiteboardInfoBackendService");
const { getSafeFilePath } = require("./utils");
import config from "./config/config.js";
import ROBackendService from "./services/ReadOnlyBackendService.js";
const ReadOnlyBackendService = new ROBackendService();
import WBInfoBackendService from "./services/WhiteboardInfoBackendService.js";
const WhiteboardInfoBackendService = new WBInfoBackendService();
function startBackendServer(port) {
var fs = require("fs-extra");
var express = require("express");
var formidable = require("formidable"); //form upload processing
import { getSafeFilePath } from "./utils.js";
import fs from "fs-extra";
import express from "express";
import formidable from "formidable"; //form upload processing
import createDOMPurify from "dompurify"; //Prevent xss
import { JSDOM } from "jsdom";
import { createClient } from "webdav";
import s_whiteboard from "./s_whiteboard.js";
import http from "http";
import { Server } from "socket.io";
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export default function startBackendServer(port) {
const createDOMPurify = require("dompurify"); //Prevent xss
const { JSDOM } = require("jsdom");
const window = new JSDOM("").window;
const DOMPurify = createDOMPurify(window);
const { createClient } = require("webdav");
var s_whiteboard = require("./s_whiteboard.js");
var app = express();
var server = require("http").Server(app);
var server = http.Server(app);
server.listen(port);
var io = require("socket.io")(server, { path: "/ws-api" });
var io = new Server(server, { path: "/ws-api" });
WhiteboardInfoBackendService.start(io);
console.log("socketserver running on port:" + port);
@ -442,5 +454,3 @@ function startBackendServer(port) {
console.log("unhandledRejection", error.message);
});
}
module.exports = startBackendServer;

View File

@ -1,3 +1,7 @@
import webpack from "webpack";
import WebpackDevServer from "webpack-dev-server";
import config from "../config/webpack.dev.js";
const devServerConfig = {
hot: true,
proxy: {
@ -11,12 +15,7 @@ const devServerConfig = {
},
};
function startFrontendDevServer(port) {
// require here to prevent prod dependency to webpack
const webpack = require("webpack");
const WebpackDevServer = require("webpack-dev-server");
const config = require("../config/webpack.dev");
export default function startFrontendDevServer(port) {
new WebpackDevServer(webpack(config), devServerConfig).start(port, (err) => {
if (err) {
console.log(err);
@ -25,5 +24,3 @@ function startFrontendDevServer(port) {
console.log("Listening on port " + port);
});
}
module.exports = startFrontendDevServer;

View File

@ -1,6 +1,7 @@
const { getArgs } = require("./utils");
const startFrontendDevServer = require("./server-frontend-dev");
const startBackendServer = require("./server-backend");
import { getArgs } from "./utils.js";
import startFrontendDevServer from "./server-frontend-dev.js";
import startBackendServer from "./server-backend.js";
const SERVER_MODES = {
PRODUCTION: 1,

View File

@ -1,6 +1,6 @@
const { v4: uuidv4 } = require("uuid");
import { v4 as uuidv4 } from "uuid";
class ReadOnlyBackendService {
export default class ReadOnlyBackendService {
/**
* Mapping from an editable whiteboard id to the matching read-only whiteboard id
* @type {Map<string, string>}
@ -69,5 +69,3 @@ class ReadOnlyBackendService {
return this._readOnlyIdToId.has(whiteboardId);
}
}
module.exports = new ReadOnlyBackendService();

View File

@ -1,10 +1,11 @@
const config = require("../config/config");
const ReadOnlyBackendService = require("./ReadOnlyBackendService");
import config from "../config/config.js";
import ROnlyBackendService from "./ReadOnlyBackendService.js";
const ReadOnlyBackendService = new ROnlyBackendService()
/**
* Class to hold information related to a whiteboard
*/
class WhiteboardInfo {
export class WhiteboardInfo {
static defaultScreenResolution = { w: 1000, h: 1000 };
/**
@ -107,7 +108,7 @@ class WhiteboardInfo {
/**
* Wrapper class around map to treat both the editable whiteboard and its read-only version the same
*/
class InfoByWhiteBoardMap extends Map {
export class InfoByWhiteBoardMap extends Map {
get(wid) {
const readOnlyId = ReadOnlyBackendService.getReadOnlyId(wid);
return super.get(readOnlyId);
@ -129,7 +130,7 @@ class InfoByWhiteBoardMap extends Map {
}
}
class WhiteboardInfoBackendService {
export default class WhiteboardInfoBackendService {
/**
* @type {Map<string, WhiteboardInfo>}
*/
@ -238,6 +239,4 @@ class WhiteboardInfoBackendService {
if (info) return info.nbConnectedUsers;
else return null;
}
}
module.exports = new WhiteboardInfoBackendService();
}

View File

@ -1,6 +1,6 @@
const path = require("path");
import path from "path";
const getArgs = function () {
export function getArgs () {
const args = {};
process.argv.slice(2, process.argv.length).forEach((arg) => {
// long arg
@ -28,7 +28,7 @@ const getArgs = function () {
* @return {string} A safe to use path combined of rootPath and singleFileSegment
* @throws {Error} If singleFileSegment contains potentially unsafe directory characters or path information
*/
const getSafeFilePath = function (rootPath, singleFileSegment) {
export function getSafeFilePath (rootPath, singleFileSegment) {
var filePath = path.join(rootPath, singleFileSegment);
if (
(path.dirname(filePath) !== rootPath &&
@ -44,9 +44,4 @@ const getSafeFilePath = function (rootPath, singleFileSegment) {
throw new Error(errorMessage + singleFileSegment);
}
return filePath;
};
module.exports = {
getArgs,
getSafeFilePath,
};
};

View File

@ -1,4 +1,4 @@
import { computeDist } from "../utils";
import { computeDist } from "../utils.js";
class Point {
/**

View File

@ -1,21 +1,17 @@
import "jquery-ui/ui/core";
import "jquery-ui/ui/widgets/draggable";
import "jquery-ui/ui/widgets/resizable";
import "jquery-ui-rotatable/jquery.ui.rotatable";
import "jquery-ui/ui/core.js";
import "jquery-ui/ui/widgets/draggable.js";
import "jquery-ui/ui/widgets/resizable.js";
import "jquery-ui-rotatable/jquery.ui.rotatable.js";
import "jquery-ui/themes/base/resizable.css";
import "../css/main.css";
import "./icons";
import main from "./main";
import "./icons.js";
import main from "./main.js";
$(document).ready(function () {
$(function () {
$("head").append(
'<meta name="viewport" content="width=device-width, initial-scale=0.52, maximum-scale=1" />'
);
main();
});
if (module.hot) {
module.hot.accept();
}
});

View File

@ -1,17 +1,17 @@
import keymage from "keymage";
import { io } from "socket.io-client";
import whiteboard from "./whiteboard";
import keybinds from "./keybinds";
import whiteboard from "./whiteboard.js";
import keybinds from "./keybinds.js";
import Picker from "vanilla-picker";
import { dom } from "@fortawesome/fontawesome-svg-core";
import shortcutFunctions from "./shortcutFunctions";
import ReadOnlyService from "./services/ReadOnlyService";
import InfoService from "./services/InfoService";
import { getSubDir } from "./utils";
import ConfigService from "./services/ConfigService";
import shortcutFunctions from "./shortcutFunctions.js";
import ReadOnlyService from "./services/ReadOnlyService.js";
import InfoService from "./services/InfoService.js";
import { getSubDir } from "./utils.js";
import ConfigService from "./services/ConfigService.js";
import { v4 as uuidv4 } from "uuid";
const pdfjsLib = require("pdfjs-dist");
import pdfjsLib from "pdfjs-dist"
const urlParams = new URLSearchParams(window.location.search);
let whiteboardId = urlParams.get("whiteboardid");
@ -171,7 +171,7 @@ function initWhiteboard() {
// request whiteboard from server
$.get(subdir + "/api/loadwhiteboard", { wid: whiteboardId, at: accessToken }).done(
function (data) {
console.log(data);
//console.log(data);
whiteboard.loadData(data);
if (copyfromwid && data.length == 0) {
//Copy from witheboard if current is empty and get parameter is given

View File

@ -1,4 +1,4 @@
import { getThrottling } from "./ConfigService.utils";
import { getThrottling } from "./ConfigService.utils.js";
/**
* Class to hold the configuration sent by the backend

View File

@ -1,4 +1,4 @@
import ConfigService from "./ConfigService";
import ConfigService from "./ConfigService.js";
/**
* Class the handle the information about the whiteboard

View File

@ -1,4 +1,4 @@
import ConfigService from "./ConfigService";
import ConfigService from "./ConfigService.js";
/**
* Class the handle the read-only logic

View File

@ -1,6 +1,6 @@
import Point from "../classes/Point";
import { getCurrentTimeMs } from "../utils";
import ConfigService from "./ConfigService";
import Point from "../classes/Point.js";
import { getCurrentTimeMs } from "../utils.js";
import ConfigService from "./ConfigService.js";
/**
* Class to handle all the throttling logic

View File

@ -1,5 +1,5 @@
import whiteboard from "./whiteboard";
import ReadOnlyService from "./services/ReadOnlyService";
import whiteboard from "./whiteboard.js";
import ReadOnlyService from "./services/ReadOnlyService.js";
/**
* @param {function} callback

View File

@ -1,9 +1,9 @@
import { dom } from "@fortawesome/fontawesome-svg-core";
import Point from "./classes/Point";
import ReadOnlyService from "./services/ReadOnlyService";
import InfoService from "./services/InfoService";
import ThrottlingService from "./services/ThrottlingService";
import ConfigService from "./services/ConfigService";
import Point from "./classes/Point.js";
import ReadOnlyService from "./services/ReadOnlyService.js";
import InfoService from "./services/InfoService.js";
import ThrottlingService from "./services/ThrottlingService.js";
import ConfigService from "./services/ConfigService.js";
import html2canvas from "html2canvas";
import DOMPurify from "dompurify";