Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Convert to ESM #2306

Draft
wants to merge 21 commits into
base: alpha
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
05d6478
refactor: convert to ESM
damianstasik Oct 1, 2022
d1eefba
Refactor tests to use ESM
damianstasik Oct 2, 2022
4bd1480
Properly import scss in all entrypoints
damianstasik Oct 2, 2022
4093f02
Fix linting issues
damianstasik Oct 2, 2022
010dc29
Update test generation script
damianstasik Oct 2, 2022
b61420d
Remove unnecessary mock config
damianstasik Oct 2, 2022
fc52a32
Update remaining pieces of CJS code
damianstasik Oct 2, 2022
cb4f387
Make bin script work
damianstasik Oct 2, 2022
e05ee71
Bring back fixed package.json feature check
damianstasik Oct 2, 2022
105291a
Add jsdoc comments to webpack plugin and config to improve DX
damianstasik Oct 3, 2022
d765965
Merge remote-tracking branch 'upstream/alpha' into convert-to-esm
damianstasik Oct 9, 2022
916640d
Semantic release will take some time before it's ready for ESM, roll …
damianstasik Oct 9, 2022
1ef7b8d
Merge remote-tracking branch 'upstream/alpha' into convert-to-esm
Feb 14, 2023
e56effe
Update deps and use backmergeBranches in semantic-release config
Feb 14, 2023
a276606
Merge remote-tracking branch 'upstream/alpha' into convert-to-esm
damianstasik Jun 10, 2023
c479446
Merge branch 'alpha' into convert-to-esm
damianstasik Jun 10, 2023
95066a1
Add type=module back after git conflict
damianstasik Jun 10, 2023
e1ec9c3
Update relevant packages to latest version
damianstasik Jun 10, 2023
735e6c3
semantic-release now supports esm, convert last cjs file to esm
damianstasik Jun 10, 2023
692079e
Merge remote-tracking branch 'upstream/alpha' into convert-to-esm
damianstasik Jun 10, 2023
8f103cd
Update package-lock.json
damianstasik Jun 10, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Parse-Dashboard/Authentication.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';
var bcrypt = require('bcryptjs');
var csrf = require('csurf');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
const OTPAuth = require('otpauth')
import bcrypt from 'bcryptjs';
import csrf from 'csurf';
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
import OTPAuth from 'otpauth';
import { randomBytes } from 'node:crypto';
import connectFlash from 'connect-flash';
import bodyParser from 'body-parser';
import cookieSession from 'cookie-session';

/**
* Constructor for Authentication class
Expand Down Expand Up @@ -53,11 +56,11 @@ function initialize(app, options) {
cb(null, user);
});

var cookieSessionSecret = options.cookieSessionSecret || require('crypto').randomBytes(64).toString('hex');
var cookieSessionSecret = options.cookieSessionSecret || randomBytes(64).toString('hex');
const cookieSessionMaxAge = options.cookieSessionMaxAge;
app.use(require('connect-flash')());
app.use(require('body-parser').urlencoded({ extended: true }));
app.use(require('cookie-session')({
app.use(connectFlash());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieSession({
key : 'parse_dash',
secret : cookieSessionSecret,
maxAge : cookieSessionMaxAge
Expand Down Expand Up @@ -150,4 +153,4 @@ function authenticate(userToTest, usernameOnly) {
Authentication.prototype.initialize = initialize;
Authentication.prototype.authenticate = authenticate;

module.exports = Authentication;
export default Authentication;
165 changes: 82 additions & 83 deletions Parse-Dashboard/CLI/mfa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const crypto = require('crypto');
const inquirer = require('inquirer');
const OTPAuth = require('otpauth');
const { copy } = require('./utils.js');
import crypto from 'node:crypto';
import inquirer from 'inquirer';
import OTPAuth from 'otpauth';
import { copy } from './utils.js';
import QRCode from 'qrcode';
import bcrypt from 'bcryptjs';
const phrases = {
enterPassword: 'Enter a password:',
enterUsername: 'Enter a username:',
Expand Down Expand Up @@ -80,7 +82,6 @@ const generateSecret = ({ app, username, algorithm, digits, period }) => {
return { config };
};
const showQR = text => {
const QRCode = require('qrcode');
QRCode.toString(text, { type: 'terminal' }, (err, url) => {
console.log(
'\n------------------------------------------------------------------------------' +
Expand Down Expand Up @@ -136,96 +137,94 @@ const showInstructions = ({ app, username, passwordCopied, encrypt, config }) =>
);
}

module.exports = {
async createUser() {
const data = {};
export async function createUser() {
const data = {};

console.log('');
const { username, password } = await inquirer.prompt([
{
type: 'input',
name: 'username',
message: phrases.enterUsername
},
console.log('');
const { username, password } = await inquirer.prompt([
{
type: 'input',
name: 'username',
message: phrases.enterUsername
},
{
type: 'confirm',
name: 'password',
message: 'Do you want to auto-generate a password?'
}
]);
data.user = username;
if (!password) {
const { password } = await inquirer.prompt([
{
type: 'confirm',
type: 'password',
name: 'password',
message: 'Do you want to auto-generate a password?'
message: phrases.enterPassword
}
]);
data.user = username;
if (!password) {
const { password } = await inquirer.prompt([
{
type: 'password',
name: 'password',
message: phrases.enterPassword
}
]);
data.pass = password;
} else {
const password = crypto.randomBytes(20).toString('base64');
data.pass = password;
data.pass = password;
} else {
const password = crypto.randomBytes(20).toString('base64');
data.pass = password;
}
const { mfa, encrypt } = await inquirer.prompt([
{
type: 'confirm',
name: 'encrypt',
message: 'Should the password be encrypted? (strongly recommended, otherwise it is stored in clear-text)'
},
{
type: 'confirm',
name: 'mfa',
message: 'Do you want to enable multi-factor authentication?'
}
const { mfa, encrypt } = await inquirer.prompt([
{
type: 'confirm',
name: 'encrypt',
message: 'Should the password be encrypted? (strongly recommended, otherwise it is stored in clear-text)'
},
{
type: 'confirm',
name: 'mfa',
message: 'Do you want to enable multi-factor authentication?'
}
]);
if (encrypt) {
// Copy the raw password to clipboard
copy(data.pass);
]);
if (encrypt) {
// Copy the raw password to clipboard
copy(data.pass);

// Encrypt password
const bcrypt = require('bcryptjs');
const salt = bcrypt.genSaltSync(10);
data.pass = bcrypt.hashSync(data.pass, salt);
}
const config = {};
if (mfa) {
const { app } = await inquirer.prompt([
{
type: 'input',
name: 'app',
message: phrases.enterAppName
}
]);
const { algorithm, digits, period } = await getAlgorithm();
const secret =generateSecret({ app, username, algorithm, digits, period });
Object.assign(config, secret.config);
showQR(secret.config.url);
}
config.user = data.user;
config.pass = data.pass ;
showInstructions({ app: data.app, username, passwordCopied: true, encrypt, config });
},
async createMFA() {
console.log('');
const { username, app } = await inquirer.prompt([
{
type: 'input',
name: 'username',
message:
'Enter the username for which you want to enable multi-factor authentication:'
},
// Encrypt password
const salt = bcrypt.genSaltSync(10);
data.pass = bcrypt.hashSync(data.pass, salt);
}
const config = {};
if (mfa) {
const { app } = await inquirer.prompt([
{
type: 'input',
name: 'app',
message: phrases.enterAppName
}
]);
const { algorithm, digits, period } = await getAlgorithm();

const { config } = generateSecret({ app, username, algorithm, digits, period });
showQR(config.url);
// Compose config
showInstructions({ app, username, config });
const secret =generateSecret({ app, username, algorithm, digits, period });
Object.assign(config, secret.config);
showQR(secret.config.url);
}
};
config.user = data.user;
config.pass = data.pass ;
showInstructions({ app: data.app, username, passwordCopied: true, encrypt, config });
}

export async function createMFA() {
console.log('');
const { username, app } = await inquirer.prompt([
{
type: 'input',
name: 'username',
message:
'Enter the username for which you want to enable multi-factor authentication:'
},
{
type: 'input',
name: 'app',
message: phrases.enterAppName
}
]);
const { algorithm, digits, period } = await getAlgorithm();

const { config } = generateSecret({ app, username, algorithm, digits, period });
showQR(config.url);
// Compose config
showInstructions({ app, username, config });
}
12 changes: 6 additions & 6 deletions Parse-Dashboard/CLI/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
copy(text) {
const proc = require('child_process').spawn('pbcopy');
proc.stdin.write(text);
proc.stdin.end();
}
import { spawn } from 'node:child_process';

export function copy(text) {
const proc = spawn('pbcopy');
proc.stdin.write(text);
proc.stdin.end();
}
6 changes: 3 additions & 3 deletions Parse-Dashboard/CLIHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { createUser, createMFA } = require('./CLI/mfa');
import { createUser, createMFA } from './CLI/mfa.js';

module.exports = {
export default {
createUser,
createMFA
createMFA,
};
28 changes: 14 additions & 14 deletions Parse-Dashboard/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
'use strict';
const express = require('express');
const path = require('path');
const packageJson = require('package-json');
const csrf = require('csurf');
const Authentication = require('./Authentication.js');
var fs = require('fs');

const currentVersionFeatures = require('../package.json').parseDashboardFeatures;

var newFeaturesInLatestVersion = [];
import express from 'express';
import path from 'node:path';
import packageJson from 'package-json';
import csrf from 'csurf';
import Authentication from './Authentication.js';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';

const { parseDashboardFeatures } = JSON.parse(fs.readFileSync('package.json', 'utf8'));
let newFeaturesInLatestVersion = [];
packageJson('parse-dashboard', { version: 'latest', fullMetadata: true })
.then(latestPackage => {
if (latestPackage.parseDashboardFeatures instanceof Array) {
if (Array.isArray(latestPackage.parseDashboardFeatures)) {
newFeaturesInLatestVersion = latestPackage.parseDashboardFeatures.filter(feature => {
return currentVersionFeatures.indexOf(feature) === -1;
return !parseDashboardFeatures.includes(feature);
});
}
})
Expand Down Expand Up @@ -51,9 +50,10 @@ function checkIfIconsExistForApps(apps, iconsFolder) {
}
}

module.exports = function(config, options) {
export default function app(config, options) {
options = options || {};
var app = express();
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Serve public files.
app.use(express.static(path.join(__dirname,'public')));

Expand Down
7 changes: 3 additions & 4 deletions Parse-Dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* the root directory of this source tree.
*/
// Command line tool for npm start
'use strict'
const CLIHelper = require('./CLIHelper.js');
const startServer = require('./server');
import CLIHelper from './CLIHelper.js';
import startServer from './server.js';
import { program } from 'commander';

const program = require('commander');
program.option('--appId [appId]', 'the app Id of the app you would like to manage.');
program.option('--masterKey [masterKey]', 'the master key of the app you would like to manage.');
program.option('--serverURL [serverURL]', 'the server url of the app you would like to manage.');
Expand Down
18 changes: 10 additions & 8 deletions Parse-Dashboard/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
* the root directory of this source tree.
*/
// Command line tool for npm start
'use strict'
const path = require('path');
const fs = require('fs');
const express = require('express');
const parseDashboard = require('./app');
import path from 'node:path';
import fs from 'node:fs';
import express from 'express';
import parseDashboard from './app.js';
import { fileURLToPath } from 'node:url';
import { createServer } from 'node:https';

module.exports = (options) => {
export default function server(options) {
const host = options.host || process.env.HOST || '0.0.0.0';
const port = options.port || process.env.PORT || 4040;
const mountPath = options.mountPath || process.env.MOUNT_PATH || '/';
Expand Down Expand Up @@ -84,6 +85,7 @@ module.exports = (options) => {
];
}
} else if (!configServerURL && !configMasterKey && !configAppName) {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
configFile = path.join(__dirname, 'parse-dashboard-config.json');
}
} else if (!options.config && process.env.PARSE_DASHBOARD_CONFIG) {
Expand Down Expand Up @@ -159,12 +161,12 @@ module.exports = (options) => {
var privateKey = fs.readFileSync(configSSLKey);
var certificate = fs.readFileSync(configSSLCert);

server = require('https').createServer({
server = createServer({
key: privateKey,
cert: certificate
}, app).listen(port, host, function () {
console.log(`The dashboard is now available at https://${server.address().address}:${server.address().port}${mountPath}`);
});
}
handleSIGs(server);
};
}
Loading