97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
|
|
class TapNodeTools {
|
|
private smartshellInstance: plugins.smartshell.Smartshell;
|
|
|
|
constructor() {}
|
|
|
|
private qenv: plugins.qenv.Qenv;
|
|
public async getQenv(): Promise<plugins.qenv.Qenv> {
|
|
this.qenv = this.qenv || new plugins.qenv.Qenv('./', '.nogit/');
|
|
return this.qenv;
|
|
}
|
|
public async getEnvVarOnDemand(envVarNameArg: string): Promise<string> {
|
|
const qenv = await this.getQenv();
|
|
return qenv.getEnvVarOnDemand(envVarNameArg);
|
|
}
|
|
|
|
public async runCommand(commandArg: string): Promise<any> {
|
|
if (!this.smartshellInstance) {
|
|
this.smartshellInstance = new plugins.smartshell.Smartshell({
|
|
executor: 'bash',
|
|
});
|
|
}
|
|
const result = await this.smartshellInstance.exec(commandArg);
|
|
return result;
|
|
}
|
|
|
|
public async createHttpsCert(
|
|
commonName: string = 'localhost',
|
|
allowSelfSigned: boolean = true
|
|
): Promise<{ key: string; cert: string }> {
|
|
if (allowSelfSigned) {
|
|
// set node to allow self-signed certificates
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
}
|
|
|
|
// Generate a key pair
|
|
const keys = plugins.smartcrypto.nodeForge.pki.rsa.generateKeyPair(2048);
|
|
|
|
// Create a self-signed certificate
|
|
const cert = plugins.smartcrypto.nodeForge.pki.createCertificate();
|
|
cert.publicKey = keys.publicKey;
|
|
cert.serialNumber = '01';
|
|
cert.validity.notBefore = new Date();
|
|
cert.validity.notAfter = new Date();
|
|
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
|
|
|
|
const attrs = [
|
|
{ name: 'commonName', value: commonName },
|
|
{ name: 'countryName', value: 'US' },
|
|
{ shortName: 'ST', value: 'California' },
|
|
{ name: 'localityName', value: 'San Francisco' },
|
|
{ name: 'organizationName', value: 'My Company' },
|
|
{ shortName: 'OU', value: 'Dev' },
|
|
];
|
|
cert.setSubject(attrs);
|
|
cert.setIssuer(attrs);
|
|
|
|
// Sign the certificate with its own private key (self-signed)
|
|
cert.sign(keys.privateKey, plugins.smartcrypto.nodeForge.md.sha256.create());
|
|
|
|
// PEM encode the private key and certificate
|
|
const pemKey = plugins.smartcrypto.nodeForge.pki.privateKeyToPem(keys.privateKey);
|
|
const pemCert = plugins.smartcrypto.nodeForge.pki.certificateToPem(cert);
|
|
|
|
return {
|
|
key: pemKey,
|
|
cert: pemCert,
|
|
};
|
|
}
|
|
|
|
/**
|
|
* create and return a smartmongo instance
|
|
*/
|
|
public async createSmartmongo() {
|
|
const smartmongoMod = await import('@push.rocks/smartmongo');
|
|
const smartmongoInstance = new smartmongoMod.SmartMongo();
|
|
await smartmongoInstance.start();
|
|
return smartmongoInstance;
|
|
}
|
|
|
|
/**
|
|
* create and return a smarts3 instance
|
|
*/
|
|
public async createSmarts3() {
|
|
const smarts3Mod = await import('@push.rocks/smarts3');
|
|
const smarts3Instance = new smarts3Mod.Smarts3({
|
|
port: 3003,
|
|
cleanSlate: true,
|
|
});
|
|
await smarts3Instance.start();
|
|
return smarts3Instance;
|
|
}
|
|
}
|
|
|
|
export const tapNodeTools = new TapNodeTools();
|