Skip to content

Commit 777da86

Browse files
Transfer to Es modules (#1125)
* Transfer to es modules for lib/ and app.js * Transfer to es modules for test/ * Add eslint rinore as specberus * fix test .eslintrc.js * close eslint no-unpublished-import * convert setGlobalInfo into function * fix mailer not found * fix __dirname not found * fix test-job * fix test-orchestrator * resolve Immutable * fix sendFile undefined * fix test-history * fix test-jobs * use process.env for config; delete console * apply suggestion #1125 * use import for global config * fix test config file * file-type 18.4.0 * named export --------- Co-authored-by: Denis Ah-Kang <[email protected]>
1 parent 7311da1 commit 777da86

32 files changed

+334
-272
lines changed

.eslintrc.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

.eslintrc.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"extends": ["airbnb-base", "prettier", "plugin:node/recommended"],
3+
"plugins": ["prettier", "node"],
4+
"env": {
5+
"node": true,
6+
"es2022": true
7+
},
8+
"parserOptions": {
9+
"ecmaVersion": 2022,
10+
"allowImportExportEverywhere": true
11+
},
12+
"rules": {
13+
"prettier/prettier": "error",
14+
"strict": "off",
15+
"consistent-return": "off",
16+
"import/extensions": "off",
17+
"global-require": "off",
18+
"no-restricted-syntax": "warn",
19+
"guard-for-in": "warn",
20+
"prefer-destructuring": "warn",
21+
"import/prefer-default-export": "off",
22+
"no-unpublished-import": "off",
23+
"node/no-unsupported-features/es-syntax": [
24+
"warn",
25+
{
26+
"version": ">=18.0.0",
27+
"ignores": ["modules"]
28+
}
29+
]
30+
},
31+
"overrides": [
32+
{
33+
"files": ["test/**/*.js"],
34+
"env": {
35+
"mocha": true
36+
}
37+
},
38+
{
39+
"files": ["assets/**/*.js"],
40+
"env": {
41+
"browser": true
42+
}
43+
}
44+
]
45+
}

app.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,30 @@
55

66
'use strict';
77

8-
const express = require('express');
9-
const compression = require('compression');
10-
const bodyParser = require('body-parser');
11-
const multer = require('multer');
12-
const path = require('path');
13-
const Fs = require('fs');
14-
const { Map } = require('immutable');
15-
const { v4: uuidv4 } = require('uuid');
16-
17-
const passport = require('passport');
18-
const LdapAuth = require('ldapauth-fork');
19-
const { BasicStrategy } = require('passport-http');
20-
const Job = require('./lib/job');
21-
const Orchestrator = require('./lib/orchestrator');
22-
const RequestState = require('./lib/request-state');
23-
const SpecberusWrapper = require('./lib/specberus-wrapper');
24-
const mailer = require('./lib/mailer');
25-
26-
const meta = require('./package.json');
27-
28-
// Configuration file
29-
const config = process.env.CONFIG || 'config.js';
30-
// eslint-disable-next-line import/no-dynamic-require
31-
require(`./${config}`);
32-
// eslint-disable-next-line no-console
33-
console.log(`Loading config: ${config}`);
8+
import express from 'express';
9+
import compression from 'compression';
10+
import bodyParser from 'body-parser';
11+
import multer from 'multer';
12+
import path from 'path';
13+
import Fs from 'fs';
14+
import pkg from 'immutable';
15+
import { v4 as uuidv4 } from 'uuid';
16+
import passport from 'passport';
17+
18+
import LdapAuth from 'ldapauth-fork';
19+
import { BasicStrategy } from 'passport-http';
20+
import Job from './lib/job.js';
21+
import Orchestrator from './lib/orchestrator.js';
22+
import RequestState from './lib/request-state.js';
23+
import SpecberusWrapper from './lib/specberus-wrapper.js';
24+
import sendMessage from './lib/mailer.js';
25+
26+
import { importJSON } from './lib/util.js';
27+
28+
await import(`${process.cwd()}/${process.env.CONFIG || 'config.js'}`);
29+
30+
const { Map } = pkg;
31+
const meta = importJSON('./package.json', import.meta.url);
3432

3533
const app = express();
3634
const requests = {};
@@ -66,12 +64,14 @@ app.set('trust proxy', true);
6664

6765
// Index Page
6866
app.get('/', (request, response) => {
69-
response.sendFile(`${__dirname}/views/index.html`);
67+
// eslint-disable-next-line no-undef
68+
response.sendFile(`${process.cwd()}/views/index.html`);
7069
});
7170

7271
// New UI
7372
app.get('/ui', (request, response) => {
74-
response.sendFile(`${__dirname}/views/web-interface.html`);
73+
// eslint-disable-next-line no-undef
74+
response.sendFile(`${process.cwd()}/views/web-interface.html`);
7575
});
7676

7777
// API methods
@@ -204,7 +204,7 @@ const processRequest = (req, res, isTar) => {
204204
},
205205
requests[id].results,
206206
)
207-
.then(state => {
207+
.then(async state => {
208208
// eslint-disable-next-line no-console
209209
console.log(`[${state.get('status').toUpperCase()}] ${url}`);
210210
dumpJobResult(
@@ -214,7 +214,7 @@ const processRequest = (req, res, isTar) => {
214214
// eslint-disable-next-line no-console
215215
if (dryRun) console.log('Dry-run: omitting e-mail notification');
216216
else {
217-
mailer.sendMessage(
217+
sendMessage(
218218
id,
219219
state,
220220
requests[id],

config-dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ global.LDAP_GROUPS = [
4242
'cn=32113,ou=groups,dc=w3,dc=org',
4343
'cn=49309,ou=groups,dc=w3,dc=org',
4444
];
45-
global.SKIP_VALIDATION = false;
45+
global.SKIP_VALIDATION = false;

config.js.example

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
'use strict';
2-
31
global.DEFAULT_TEMP_LOCATION = '/var/www/';
42
// DEFAULT_HTTP_LOCATION without trailing slash
53
global.DEFAULT_HTTP_LOCATION = 'http://spec-server';
@@ -11,7 +9,8 @@ global.SPEC_GENERATOR = 'http://localhost:3002/';
119
global.USERNAME = '';
1210
global.PASSWORD = '';
1311
global.W3C_PUBSYSTEM_URL = 'http://localhost:3001/publish';
14-
global.TR_INSTALL_CMD = 'mkdir -p /var/www/$dest && cp -R $source /var/www/$dest';
12+
global.TR_INSTALL_CMD =
13+
'mkdir -p /var/www/$dest && cp -R $source /var/www/$dest';
1514
global.UPDATE_TR_SHORTLINK_CMD = '#';
1615
global.MAIL_SENDER = 'Echidna <[email protected]>';
1716
global.MAIL_REPLYTO = '[email protected]';
@@ -23,12 +22,12 @@ global.ALLOWED_CLIENTS = [
2322
// W3C:
2423
/https?:\/\/(www\.)?w3c?\.org/i,
2524
// GitHub:
26-
/https?:\/\/w3c\.github\.io/i
25+
/https?:\/\/w3c\.github\.io/i,
2726
];
28-
global.LDAP_URL = "ldaps://localhost:636";
27+
global.LDAP_URL = 'ldaps://localhost:636';
2928
global.LDAP_SEARCH_BASE = 'ou=user,dc=example,dc=org';
3029
// LDAP_BIND_DN must contain the placeholder {{username}}
3130
global.LDAP_BIND_DN = 'uid={{username}},ou=user,dc=example,dc=org';
3231
global.GH_TOKEN = '123foobar';
3332
global.GH_DIRECTOR_TEAM_ID = '2797096';
34-
global.GH_COMM_TEAM_ID = '2794457';
33+
global.GH_COMM_TEAM_ID = '2794457';

lib/document-downloader.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44

55
'use strict';
66

7-
const Fs = require('fs');
8-
const Request = require('request');
9-
const { List } = require('immutable');
10-
const Promise = require('promise');
11-
const Url = require('url');
12-
const Mkdirp = require('mkdirp');
13-
const { dirname } = require('path');
14-
const fileType = require('file-type');
15-
const tar = require('tar-stream');
7+
import Fs from 'fs';
8+
import Request from 'request';
9+
import pkg from 'immutable';
10+
import Promise from 'promise';
11+
import Url from 'url';
12+
import { mkdirp } from 'mkdirp';
13+
import { dirname } from 'path';
14+
import { fileTypeFromBuffer } from 'file-type';
15+
import tar from 'tar-stream';
16+
17+
const { List } = pkg;
1618

1719
/**
1820
* @exports lib/document-downloader
@@ -52,7 +54,7 @@ DocumentDownloader.fetchAll = urls =>
5254
DocumentDownloader.install = (dest, content) => {
5355
const path = dirname(dest);
5456

55-
Mkdirp.sync(path);
57+
mkdirp.sync(path);
5658
return Promise.denodeify(Fs.writeFile)(dest, content);
5759
};
5860

@@ -89,7 +91,7 @@ DocumentDownloader.fetchAndInstall = (url, dest) => {
8991
.then(pathExists => (!pathExists ? mkdir(dest) : null))
9092
.then(() =>
9193
_.fetch(url).then(content =>
92-
fileType.fromBuffer(content).then(type => {
94+
fileTypeFromBuffer(content).then(type => {
9395
if (type && type.mime !== 'application/x-tar') {
9496
throw new TypeError('Only tar, html and manifest are supported.');
9597
} else if (type && type.mime === 'application/x-tar') {
@@ -105,7 +107,7 @@ DocumentDownloader.fetchAndInstall = (url, dest) => {
105107
}
106108
const path = dirname(`${dest}/${header.name}`);
107109

108-
Mkdirp.sync(path);
110+
mkdirp.sync(path);
109111
Fs.writeFileSync(`${dest}/${header.name}`, data);
110112
}
111113
});
@@ -167,4 +169,4 @@ DocumentDownloader.getFilenames = function getFilenames(manifest) {
167169
}, new List());
168170
};
169171

170-
module.exports = DocumentDownloader;
172+
export default DocumentDownloader;

lib/history.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
'use strict';
66

7-
const Fs = require('fs');
8-
const Path = require('path');
9-
const { Stack } = require('immutable');
10-
const meta = require('../package.json');
7+
import Fs from 'fs';
8+
import Path from 'path';
9+
import pkg from 'immutable';
10+
import { importJSON } from './util.js';
1111

12-
const config = process.env.CONFIG || 'config.js';
13-
// eslint-disable-next-line import/no-dynamic-require
14-
require(`../${config}`);
12+
const meta = importJSON('../package.json', import.meta.url);
13+
const { Stack } = pkg;
1514

1615
// TODO Move this function out to keep the class pure
1716
function appendToLog(filename, message) {
@@ -74,4 +73,4 @@ History.prototype = {
7473
},
7574
};
7675

77-
module.exports = History;
76+
export default History;

lib/ip-checker.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44

55
'use strict';
66

7-
const { List } = require('immutable');
8-
const Promise = require('promise');
9-
const { Octokit } = require('@octokit/core');
10-
const fs = require('fs');
11-
const ipRangeCheck = require('ip-range-check');
7+
import Promise from 'promise';
8+
import { Octokit } from '@octokit/core';
9+
import fs from 'fs';
10+
import ipRangeCheck from 'ip-range-check';
11+
import pkg from 'immutable';
12+
import path from 'path';
13+
import { fileURLToPath } from 'url';
14+
15+
const { List } = pkg;
16+
// eslint-disable-next-line no-underscore-dangle
17+
const __filenameNew = fileURLToPath(import.meta.url);
18+
// eslint-disable-next-line no-underscore-dangle
19+
const __dirname = path.dirname(__filenameNew);
1220

1321
/**
1422
* @exports lib/ip-checker
@@ -27,6 +35,7 @@ IPChecker.check = ip =>
2735
const ghIp = octokit.request('GET /meta');
2836
// downloaded from https://www.microsoft.com/en-us/download/details.aspx?id=56519
2937
const azureIp = fs.promises
38+
// eslint-disable-next-line no-undef
3039
.readFile(`${__dirname}/../ServiceTags_Public.json`)
3140
.then(JSON.parse);
3241
Promise.all([ghIp, azureIp])
@@ -57,4 +66,4 @@ IPChecker.check = ip =>
5766
.catch(e => console.error(e));
5867
});
5968

60-
module.exports = IPChecker;
69+
export default IPChecker;

lib/job.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

3-
const { List } = require('immutable');
3+
import pkg from 'immutable';
44

5+
const { List } = pkg;
56
/**
67
* @file A job is the state of a publication step, whether it is pending, finished or failed.
78
*
@@ -32,6 +33,7 @@ function Job(status, errors) {
3233
* @param {*} v - The value to set
3334
* @returns {Job} The resulting job
3435
*/
36+
// eslint-disable-next-line consistent-return
3537
this.set = (k, v) => {
3638
switch (k) {
3739
case 'status':
@@ -53,4 +55,4 @@ function Job(status, errors) {
5355
Object.freeze(this);
5456
}
5557

56-
module.exports = Job;
58+
export default Job;

lib/json-http-service.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
'use strict';
77

8-
const Promise = require('promise');
9-
const Request = require('request');
8+
import Promise from 'promise';
9+
import Request from 'request';
1010

1111
/**
1212
* @exports lib/json-http-service
@@ -62,4 +62,4 @@ JsonHttpService.prototype = {
6262
},
6363
};
6464

65-
module.exports = JsonHttpService;
65+
export default JsonHttpService;

0 commit comments

Comments
 (0)