Skip to content

Commit

Permalink
chore: add eslint support
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Mar 26, 2022
1 parent 578dc83 commit 59beb17
Show file tree
Hide file tree
Showing 19 changed files with 1,954 additions and 289 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
**/*{.,-}min.js
test

dist
20 changes: 20 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
env:
browser: true
commonjs: true
es2021: true
extends:
- 'eslint:recommended'
- 'plugin:@typescript-eslint/recommended'
parser: '@typescript-eslint/parser'
parserOptions:
ecmaVersion: 12
plugins:
- '@typescript-eslint'
rules:
indent:
- error
- 4
quotes:
- error
- double

4 changes: 3 additions & 1 deletion bin/crypto_create_CA.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env node
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */
// ---------------------------------------------------------------------------------------------------------------------
// node-opcua
// ---------------------------------------------------------------------------------------------------------------------
Expand All @@ -25,6 +26,7 @@
const updateNotifier = require("update-notifier");
const pkg = require("../package.json");

// eslint-disable-next-line no-undef
require("../dist/crypto_create_CA.js").main(process.argv.splice(2), () => {
/* empty */
updateNotifier({pkg}).notify();
Expand Down
1 change: 1 addition & 0 deletions bin/install_prerequisite.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
"use strict";
// eslint-disable-next-line @typescript-eslint/no-var-requires
var install_prerequisite = require("../dist/lib/misc/install_prerequisite").install_prerequisite;
install_prerequisite(function(err){
if (err) {
Expand Down
118 changes: 60 additions & 58 deletions lib/crypto_create_CA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ import {

// see https://github.com/yargs/yargs/issues/781
import * as commands from "yargs";
const { hideBin } = require('yargs/helpers')
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { hideBin } = require("yargs/helpers")
// eslint-disable-next-line @typescript-eslint/no-var-requires
const argv = require("yargs/yargs")(hideBin(process.argv))


Expand Down Expand Up @@ -353,60 +355,60 @@ interface OptionMap {

function add_standard_option(options: OptionMap, optionName: string) {
switch (optionName) {
case "root":
options.root = {
alias: "r",
type: "string",
default: "{CWD}/certificates",
describe: "the location of the Certificate folder",
};
break;

case "CAFolder":
options.CAFolder = {
alias: "c",
type: "string",
default: "{root}/CA",
describe: "the location of the Certificate Authority folder",
};
break;

case "PKIFolder":
options.PKIFolder = {
type: "string",
default: "{root}/PKI",
describe: "the location of the Public Key Infrastructure",
};
break;
case "root":
options.root = {
alias: "r",
type: "string",
default: "{CWD}/certificates",
describe: "the location of the Certificate folder",
};
break;

case "CAFolder":
options.CAFolder = {
alias: "c",
type: "string",
default: "{root}/CA",
describe: "the location of the Certificate Authority folder",
};
break;

case "silent":
options.silent = {
alias: "s",
type: "boolean",
default: false,
describe: "minimize output",
};
break;

case "privateKey":
options.privateKey = {
alias: "p",
type: "string",
default: "{PKIFolder}/own/private_key.pem",
describe: "the private key to use to generate certificate",
};
break;

case "keySize":
options.keySize = {
alias: ["k", "keyLength"],
type: "number",
default: 2048,
describe: "the private key size in bits (1024|2048|3072|4096)",
};
break;
default:
throw Error("Unknown option " + optionName);
case "PKIFolder":
options.PKIFolder = {
type: "string",
default: "{root}/PKI",
describe: "the location of the Public Key Infrastructure",
};
break;

case "silent":
options.silent = {
alias: "s",
type: "boolean",
default: false,
describe: "minimize output",
};
break;

case "privateKey":
options.privateKey = {
alias: "p",
type: "string",
default: "{PKIFolder}/own/private_key.pem",
describe: "the private key to use to generate certificate",
};
break;

case "keySize":
options.keySize = {
alias: ["k", "keyLength"],
type: "number",
default: 2048,
describe: "the private key size in bits (1024|2048|3072|4096)",
};
break;
default:
throw Error("Unknown option " + optionName);
}
}

Expand Down Expand Up @@ -622,7 +624,7 @@ function createDefaultCertificate(
}

// tslint:disable-next-line:no-empty
let done: ErrorCallback = (err?: Error | null) => {};
let done: ErrorCallback = (err?: Error | null) => {/** */};

function create_default_certificates(dev: boolean, done: ErrorCallback) {
function __create_default_certificates(
Expand Down Expand Up @@ -1079,7 +1081,7 @@ argv
.command(
"dump <certificateFile>",
"display a certificate",
() => {},
() => { /** */},
(yargs: any) => {
dumpCertificate(yargs.certificateFile, (err: Error | null, data?: string) => {
if (!err) {
Expand All @@ -1093,7 +1095,7 @@ argv
.command(
"toder <pemCertificate>",
"convert a certificate to a DER format with finger print",
() => {},
() => {/** */},
(yargs: commands.Argv) => {
function convertToDerFromCommandLine(argv: any, done: ErrorCallback) {
toDer(argv.pemCertificate, (err: Error | null) => done(err!));
Expand All @@ -1105,7 +1107,7 @@ argv
.command(
"fingerprint <certificateFile>",
"print the certificate fingerprint",
() => {},
() => { /** */},
(local_argv: any) => {
const certificate = local_argv.certificateFile;
fingerprint(certificate, (err: Error | null, data?: string) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/misc/hostname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export async function extractFullyQualifiedDomainName(): Promise<string> {
}
if (process.platform === "win32") {
// http://serverfault.com/a/73643/251863
const env = process.env;http://serverfault.com/a/73643/251863
const env = process.env;
_fullyQualifiedDomainNameInCache =
env.COMPUTERNAME + (env.USERDNSDOMAIN && env.USERDNSDOMAIN!.length > 0 ? "." + env.USERDNSDOMAIN! : "");
} else {
Expand Down
7 changes: 4 additions & 3 deletions lib/misc/install_prerequisite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ declare interface WgetInterface {
}

// tslint:disable-next-line:no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires
const wget = require("wget-improved") as WgetInterface;

type CallbackFunc<T> = (err: Error | null, result?: T) => void;
Expand Down Expand Up @@ -91,8 +92,8 @@ function execute(cmd: string, callback: CallbackFunc<ExecuteResult>, cwd?: strin

// xx cwd = cwd ? {cwd: cwd} : {};
const options = {
cwd,
windowsHide: true
cwd,
windowsHide: true
};


Expand All @@ -114,7 +115,7 @@ function execute(cmd: string, callback: CallbackFunc<ExecuteResult>, cwd?: strin
}

function quote(str: string): string {
return '"' + str.replace(/\\/g, "/") + '"';
return "\"" + str.replace(/\\/g, "/") + "\"";
}

function is_expected_openssl_version(strVersion: string): boolean {
Expand Down
15 changes: 5 additions & 10 deletions lib/misc/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const _keys = {
};

export class Subject implements SubjectOptions {

public readonly commonName?: string;
public readonly organization?: string;
public readonly organizationalUnit?: string;
Expand All @@ -51,8 +50,7 @@ export class Subject implements SubjectOptions {
public readonly domainComponent?: string;

constructor(options: SubjectOptions | string) {

if (typeof(options) === "string") {
if (typeof options === "string") {
options = Subject.parse(options);
}
this.commonName = options.commonName;
Expand All @@ -65,9 +63,8 @@ export class Subject implements SubjectOptions {
}

public static parse(str: string): SubjectOptions {

const elements = str.split(/\/(?=[^\/]*?=)/);
const options: any = {};
const elements = str.split(/\/(?=[^/]*?=)/);
const options: Record<string, unknown> = {};

elements.forEach((element: string) => {
if (element.length === 0) {
Expand All @@ -76,17 +73,16 @@ export class Subject implements SubjectOptions {
const s: string[] = element.split("=");

if (s.length !== 2) {
throw new Error("invalid format for " + element);
throw new Error("invalid format for " + element);
}
const longName = (_keys as any)[s[0]];
const value = s[1];
options[longName] = Buffer.from(value,"ascii").toString("utf8");
options[longName] = Buffer.from(value, "ascii").toString("utf8");
});
return options as SubjectOptions;
}

public toString() {

let tmp = "";
if (this.country) {
tmp += "/C=" + this.country;
Expand All @@ -111,5 +107,4 @@ export class Subject implements SubjectOptions {
}
return tmp;
}

}
10 changes: 6 additions & 4 deletions lib/pki/certificate_authority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ function construct_CertificateAuthority(certificateAuthority: CertificateAuthori
}

// tslint:disable:no-empty
displayTitle("Create Certificate Authority (CA)", (err?: Error | null) => { });
displayTitle("Create Certificate Authority (CA)", (err?: Error | null) => { /** */ });

const indexFileAttr = path.join(caRootDir, "index.txt.attr");
if (!fs.existsSync(indexFileAttr)) {
fs.writeFileSync(indexFileAttr, "unique_subject = no");
}

const caConfigFile = certificateAuthority.configFile;
// eslint-disable-next-line no-constant-condition
if (1 || !fs.existsSync(caConfigFile)) {
let data = configurationFileTemplate; // inlineText(configurationFile);
data = data.replace(/%%ROOT_FOLDER%%/, make_path(caRootDir));
Expand All @@ -156,7 +157,7 @@ function construct_CertificateAuthority(certificateAuthority: CertificateAuthori
}

// http://www.akadia.com/services/ssh_test_certificate.html
const subjectOpt = ' -subj "' + subject.toString() + '" ';
const subjectOpt = " -subj \"" + subject.toString() + "\" ";
const options = { cwd: caRootDir };
processAltNames({} as Params);

Expand Down Expand Up @@ -268,8 +269,8 @@ export class CertificateAuthority {
public readonly subject: Subject;

constructor(options: CertificateAuthorityOptions) {
assert(options.hasOwnProperty("location"));
assert(options.hasOwnProperty("keySize"));
assert(Object.prototype.hasOwnProperty.call(options,"location"));
assert(Object.prototype.hasOwnProperty.call(options,"keySize"));
this.location = options.location;
this.keySize = options.keySize || 2048;
this.subject = new Subject(options.subject || defaultSubject);
Expand Down Expand Up @@ -729,6 +730,7 @@ export class CertificateAuthority {
}

// tslint:disable:no-var-requires
// eslint-disable-next-line @typescript-eslint/no-var-requires
const thenify = require("thenify");
const opts = { multiArgs: false };
CertificateAuthority.prototype.initialize = thenify.withCallback(CertificateAuthority.prototype.initialize, opts);
Expand Down
Loading

0 comments on commit 59beb17

Please sign in to comment.