biz-a-cli 2.3.1 → 2.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/hub.js CHANGED
@@ -1,100 +1,79 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const yargs = require('yargs');
4
- const IDLE_SOCKET_TIMEOUT_MILLISECONDS = 1000 * 30;
3
+ import yargs from 'yargs';
4
+ import { io as ioc } from "socket.io-client";
5
+ import hubEvent from './hubEvent.js'
5
6
 
6
- let argv = yargs
7
- .usage('Usage: $0 --server [string] --subdomain [string] --hostname [string] --port [number]')
7
+ const argv = yargs(process.argv.slice(2))
8
+ .usage('Usage: $0 [options]')
8
9
  .options('s', {
9
10
  alias: 'server',
10
- describe: '(Required) Tunnel server endpoint'
11
+ default: 'https://biz-a.herokuapp.com',
12
+ describe: '(Required) Tunnel server endpoint',
13
+ type: 'string',
14
+ demandOption: true
11
15
  })
12
16
  .options('sub', {
13
17
  alias: 'subdomain',
14
- describe: '(Required) Public URL the tunnel server is forwarding to us'
18
+ describe: '(Required) Public URL the tunnel server is forwarding to us',
19
+ type: 'string',
20
+ demandOption: true
15
21
  })
16
22
  .options('h', {
17
23
  alias: 'hostname',
18
24
  default: '127.0.0.1',
19
- describe: 'Address of local server for forwarding over socket-tunnel'
25
+ describe: 'Address of local server for forwarding over socket-tunnel',
26
+ type: 'string',
27
+ demandOption: true
20
28
  })
21
29
  .options('p', {
22
30
  alias: 'port',
23
- describe: '(Required) Port of local server for forwarding over socket-tunnel'
31
+ default: 212,
32
+ describe: '(Required) Port of local server for forwarding over socket-tunnel',
33
+ type: 'number',
34
+ demandOption: true
35
+ })
36
+ .options('sp', {
37
+ alias: 'serverport',
38
+ default: 3002,
39
+ describe: 'Server Port',
40
+ type: 'number',
41
+ demandOption: false
24
42
  })
25
43
  .argv;
26
44
 
27
45
  if (argv.help) {
28
- yargs.showHelp();
46
+ yargs().showHelp();
29
47
  process.exit();
30
48
  }
31
49
 
32
50
  if (!argv['server'] || !argv['subdomain'] || !argv['port']) {
33
- for (var key in ['server', 'subdomain', 'port']) {
51
+ for (let key in ['server', 'subdomain', 'port']) {
34
52
  if (argv[key]) continue;
35
53
 
36
54
  console.log('Error: Required option, but nothing found');
37
55
 
38
- yargs.showHelp();
39
-
56
+ yargs().showHelp();
40
57
  process.exit();
41
58
  }
42
59
  }
43
60
 
44
- return new Promise((resolve, reject) => {
45
- const net = require('node:net');
46
- const ss = require('socket.io-stream');
47
- let socket = require('socket.io-client')(argv['server']);
48
-
49
- socket.on('connect', () => {
50
- console.log(new Date() + ': connected to socket server');
51
- console.log(new Date() + ': requesting subdomain ' + argv['subdomain'] + ' via ' + argv['server']);
52
-
53
- socket.emit('createTunnel', argv['subdomain'], (err) => {
54
- if (err) {
55
- console.log(new Date() + ': [error] ' + err);
56
- reject(err);
57
- } else {
58
- console.log(new Date() + ': registered with server successfully');
59
-
60
- // clean and concat requested url
61
- let url;
62
- // let subdomain = argv['subdomain'].toString();
63
- let server = argv['server'].toString();
64
-
65
- url = server;
66
-
67
- // resolve promise with requested URL
68
- resolve(url);
69
- }
70
- });
71
- });
72
- socket.on('incomingClient', (clientId) => {
73
- console.log(clientId, 'incoming clientId')
74
- let client = net.connect(argv['port'], argv['hostname']);
75
-
76
- client.on('connect', () => {
77
- console.log(`client connected to ${argv['hostname']}:${argv['port']}`)
78
- let s = ss.createStream();
79
- s.pipe(client).pipe(s);
61
+ //
62
+ import express from 'express';
63
+ import cors from 'cors';
64
+ const app = express();
65
+ import { runCliScript } from '../callbackController.js'
80
66
 
81
- s.on('end', () => {
82
- client.destroy();
83
- });
84
- ss(socket).emit(clientId, s);
85
- })
67
+ app.use(cors());
68
+ app.use(express.json());
86
69
 
87
- client.setTimeout(IDLE_SOCKET_TIMEOUT_MILLISECONDS);
88
- client.on('timeout', () => {
89
- client.end();
90
- });
70
+ app.use('/cb', runCliScript);
71
+ const port = 3002;
91
72
 
92
- client.on('error', () => {
93
- // handle connection refusal (create a stream and immediately close it)
94
- let s = ss.createStream();
95
- ss(socket).emit(clientId, s);
96
- s.end();
97
- });
98
- });
73
+ app.listen((argv.serverport || port), () => {
74
+ console.log(`Biz-A is listening at ${process.env.HOST || 'http://localhost'}:${argv.serverport || port} `);
75
+ });
76
+ //
99
77
 
100
- })
78
+ let socket = ioc(argv['server']);
79
+ hubEvent(socket, argv);
@@ -0,0 +1,72 @@
1
+ import { Axios } from 'axios-observable';
2
+ import net from 'node:net';
3
+ import { createRequire } from "module";
4
+ const require = createRequire(import.meta.url);
5
+ const ss = require('socket.io-stream'); //SCY: Temporary, next will be replaced with import
6
+
7
+ const IDLE_SOCKET_TIMEOUT_MILLISECONDS = 1000 * 30;
8
+
9
+ // export default (argv) => {
10
+ export default async (socket, argv) => new Promise((resolve, reject) => {
11
+ socket.on('connect', () => {
12
+ console.log(new Date() + ': connected to socket server');
13
+ console.log(new Date() + ': requesting subdomain ' + argv['subdomain'] + ' via ' + argv['server']);
14
+
15
+ socket.emit('createTunnel', argv['subdomain'], (err) => {
16
+ if (err) {
17
+ console.log(new Date() + ': [error] ' + err);
18
+ reject(err);
19
+ } else {
20
+ console.log(new Date() + ': registered with server successfully');
21
+
22
+ // clean and concat requested url
23
+ let url;
24
+ // let subdomain = argv['subdomain'].toString();
25
+ let server = argv['server'].toString();
26
+
27
+ url = server;
28
+
29
+ // resolve promise with requested URL
30
+ resolve(url);
31
+ }
32
+ });
33
+ });
34
+
35
+ socket.on('incomingClient', (clientId) => {
36
+ console.log(clientId, 'incoming clientId')
37
+ let client = net.connect(argv['port'], argv['hostname']);
38
+
39
+ client.on('connect', () => {
40
+ console.log(`client connected to ${argv['hostname']}:${argv['port']}`)
41
+ let s = ss.createStream();
42
+ s.pipe(client).pipe(s);
43
+
44
+ s.on('end', () => {
45
+ client.destroy();
46
+ });
47
+ ss(socket).emit(clientId, s);
48
+ })
49
+
50
+ client.setTimeout(IDLE_SOCKET_TIMEOUT_MILLISECONDS);
51
+ client.on('timeout', () => {
52
+ client.end();
53
+ });
54
+
55
+ client.on('error', () => {
56
+ // handle connection refusal (create a stream and immediately close it)
57
+ let s = ss.createStream();
58
+ ss(socket).emit(clientId, s);
59
+ s.end();
60
+ });
61
+ })
62
+
63
+ socket.on('cli-req', (data, callback) => {
64
+ const { path, method, ...remainData } = data;
65
+
66
+ Axios.request({
67
+ method: data.method,
68
+ url: `${process.env.HOST || 'http://localhost'}:${argv.serverport}/cb${path}`,
69
+ data: remainData
70
+ }).subscribe(result => callback(result.data));
71
+ });
72
+ })
package/bin/index.js CHANGED
@@ -1,102 +1,114 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- const yargs = require("yargs");
4
- const axios = require("axios");
5
- const fs = require("fs").promises;
3
+ import yargs from "yargs";
4
+ import axios from "axios";
5
+ import { promises as fs } from "fs";
6
+ import { runInThisContext } from "vm";
6
7
 
7
- const vm= require("vm");
8
- const { option, number } = require("yargs");
9
-
10
- const options = yargs
11
- .option("s", {alias:"server", describe:"Server URL", type:"string", demandOption:true})
12
- .option("i", {alias:"dbindex", describe:"database index", type:number, demandOption:true})
13
- // .option("d", {alias:"dir", describe:"Directory", type:"string", demandOption:false})
14
- .option("sub", {alias:"subdomain", describe:"Subdomain", type:"string", demandOption:false})
8
+ const options = yargs(process.argv.slice(2))
9
+ .option("s", {
10
+ alias: "server",
11
+ describe: "Server URL",
12
+ type: "string",
13
+ demandOption: true
14
+ })
15
+ .option("i", {
16
+ alias: "dbindex",
17
+ describe: "database index",
18
+ type: "number",
19
+ demandOption: true
20
+ })
21
+ // .option("d", {
22
+ // alias: "dir",
23
+ // describe: "Directory",
24
+ // type: "string",
25
+ // demandOption: false
26
+ // })
27
+ .option("sub", {
28
+ alias: "subdomain",
29
+ describe: "Subdomain",
30
+ type: "string",
31
+ demandOption: false
32
+ })
15
33
  .argv;
16
34
 
17
- const sendFile = async (filename, data)=>{
35
+ const sendFile = async (filename, data) => {
18
36
  const baseUrl = 'fina/rest/TOrmMethod/%22setTemplate%22'
19
- const url = options.sub ?
20
- `${options.server}/hub/${baseUrl}?subdomain=${options.sub}` :
21
- `http://${options.server}:212/${baseUrl}`
22
- // console.log('url', url)
23
-
24
- const headers={
25
- 'Content-Type':'text/plain'
37
+ const url = options.sub ?
38
+ `${options.server}/hub/${baseUrl}?subdomain=${options.sub}` :
39
+ `http://${options.server}:212/${baseUrl}`
40
+
41
+ const headers = {
42
+ 'Content-Type': 'text/plain'
26
43
  }
27
- const param = {_parameters: [options.dbindex, filename, data]};
28
- return await axios.post(url, param, {headers:headers});
44
+ const param = { _parameters: [options.dbindex, filename, data] };
45
+ return await axios.post(url, param, { headers: headers });
29
46
  }
30
47
 
31
- function getParam(funcName){
32
- var params = funcName.split('(');
48
+ function getParam(funcName) {
49
+ let params = funcName.split('(');
33
50
  return params[1].split(')')[0].split(',');
34
51
  }
35
52
 
36
- function replacer(key,value){
37
- if (typeof value == 'function'){
38
- let arr = value.toString().replace(/(\r\n|\n|\r)/gm,"°").split("°");
39
- if (arr.length < 3) throw 'Function must be minimal 3 lines';
40
- return [
41
- 'window.Function',
42
- getParam( arr[0] ),
43
- arr.slice( 1, arr.length-1 )
44
- ];
53
+ function replacer(key, value) {
54
+ if (typeof value == 'function') {
55
+ let arr = value.toString().replace(/(\r\n|\n|\r)/gm, "°").split("°");
56
+ if (arr.length < 3) throw 'Function must be minimal 3 lines';
57
+ return [
58
+ 'window.Function',
59
+ getParam(arr[0]),
60
+ arr.slice(1, arr.length - 1)
61
+ ];
45
62
  } else {
46
- return value;
47
- }
63
+ return value;
64
+ }
48
65
  }
49
66
 
50
- const readTemplateFile= async (filename)=>{
67
+ const readTemplateFile = async (filename) => {
51
68
  const data = await fs.readFile(filename);
52
69
  const ext = filename.split('.');
53
70
 
54
- if (ext[ext.length-1] == 'js'){
71
+ if (ext[ext.length - 1] == 'js') {
55
72
  let result;
56
- try{
57
- const vmResult = vm.runInThisContext(data);
73
+ try {
74
+ const vmResult = runInThisContext(data);
58
75
  result = JSON.stringify(vmResult(), replacer);
59
- } catch(ReferenceError) {
76
+ } catch (error) {
60
77
  const fn = `file://${process.cwd()}\\${filename}`;
61
- const lib = await import(fn);
78
+ const lib = await import(fn);
62
79
  result = JSON.stringify(lib.default(), replacer);
63
80
  }
64
-
65
- //console.log(result);
81
+
66
82
  return Buffer.from(result).toString('hex');
67
- //console.log(result);
68
-
83
+
69
84
  } else {
70
85
  return Buffer.from(data).toString('hex');
71
86
  //return JSON.stringify(JSON.parse(data));
72
87
  }
73
88
  }
74
89
 
75
- const loopFiles = async (dir) =>{
76
- try{
90
+ const loopFiles = async (dir) => {
91
+ try {
77
92
  const files = await fs.readdir(dir);
78
- for (const file of files){
93
+ for (const file of files) {
79
94
  //const p = path.join(dir, file);
80
95
  if (file == 'package.json' || file == 'package-lock.json') continue;
81
96
 
82
97
  const stat = await fs.stat(file);
83
98
 
84
99
  if (stat.isFile()) {
85
-
86
100
  const data = await readTemplateFile(file);
87
-
88
-
89
- //console.log(data.toString('hex'));
90
101
  const res = await sendFile(file, data.toString('hex'));
91
- if (res.data.success){
102
+ if (res.data.success) {
92
103
  console.log(`file: ${file} uploaded`);
93
104
  } else {
94
105
  console.error(res.data.error);
95
106
  }
96
- }
107
+ }
97
108
  }
98
- } catch (e){
99
- console.error(e.response && e.response.data ? e.response.data: e);
109
+ } catch (e) {
110
+ // console.error(e.response && e.response.data ? e.response.data : e);
111
+ console.error(e.response?.data ? e.response.data : e);
100
112
  }
101
113
  }
102
114
 
package/bin/proxy.js CHANGED
@@ -1,20 +1,23 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
- const express = require('express');
4
- const cors = require('cors');
5
- const app = express();
6
- const proxyCtl = require('../../biz-a/proxyController');
7
- const { option, number } = require("yargs");
8
- const yargs = require('yargs');
3
+ import express from 'express';
4
+ import cors from 'cors';
5
+ const app = express();
6
+ import { proxy } from '../proxyController.js';
7
+ import yargs from 'yargs';
9
8
 
10
-
11
- const options = yargs
12
- .option('p', {alias:'port', describe:'Port to use', type:'string', demandOption:false})
9
+ const options = yargs(process.argv.slice(2))
10
+ .option('p', {
11
+ alias: 'port',
12
+ describe: 'Port to use',
13
+ type: 'string',
14
+ demandOption: false
15
+ })
13
16
  .argv;
14
17
 
15
18
  app.use(cors());
16
19
 
17
- app.use('/proxy', proxyCtl.proxy);
20
+ app.use('/proxy', proxy);
18
21
 
19
22
  const port = 3000;
20
23
 
package/bin/watcher.js ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env node
2
+
3
+ import express from 'express';
4
+ import cors from 'cors';
5
+ const app = express();
6
+ import yargs from 'yargs';
7
+
8
+ import * as timer from '../scheduler/timer.js'
9
+
10
+ process.on('uncaughtException', (err) => { //debug
11
+ console.log('Unhandled Exception:', err);
12
+ });
13
+
14
+ process.on('unhandledRejection', (err) => { //debug
15
+ console.log('Unhandled Rejection:', err);
16
+ });
17
+
18
+ const options = yargs(process.argv.slice(2))
19
+ .option('c', {
20
+ alias: 'company',
21
+ describe: 'Company',
22
+ type: 'string',
23
+ demandOption: true
24
+ })
25
+ .option('p', {
26
+ alias: 'port',
27
+ describe: 'Port to use',
28
+ type: 'string',
29
+ demandOption: false
30
+ })
31
+ // .option('m', {
32
+ // alias: 'mode',
33
+ // describe: 'Fill with "prod" / "dev"',
34
+ // type: 'string',
35
+ // demandOption: false
36
+ // })
37
+ .argv;
38
+
39
+
40
+ app.use(cors());
41
+ timer.runScheduler(options.company);
42
+
43
+ const port = 3001;
44
+
45
+ app.listen((options.port || port), () => {
46
+ console.log(`Biz-A CLI is listening at ${process.env.HOST || 'http://localhost'}:${options.port || port} `);
47
+ });
48
+
49
+ // export const MODE = (options.mode == 'prod') ? 'production' : 'development';
@@ -0,0 +1,19 @@
1
+ import { loadCliScript, extractFunctionScript } from "./scheduler/datalib.js";
2
+ import { getCompanyObjectId, getSelectedConfig } from "./scheduler/configController.js";
3
+
4
+ export async function runCliScript(req, res) {
5
+ const data = req.body;
6
+ const company = await getCompanyObjectId(data.query.companyId);
7
+ const config = await getSelectedConfig(company._id);
8
+ const selectedConfig = config[0];
9
+
10
+ loadCliScript(selectedConfig, data.query).subscribe({
11
+ next: data => {
12
+ let functions = extractFunctionScript(data);
13
+ res.send(functions.onInit(selectedConfig, data.body));
14
+
15
+ console.log(`Run Callback Successfully!`);
16
+ },
17
+ error: error => console.log(error.response.data || error)
18
+ })
19
+ }
package/constanta.js ADDED
@@ -0,0 +1,8 @@
1
+ export const ECODE = {
2
+ API: 'EAPI',
3
+ FULL: 'full'
4
+ }
5
+ export const MSG = {
6
+ E_USER_PASS_IN_API: 'Username or password is not valid. Check database no:1 in API',
7
+ E_LICENSE_FULL: 'The license is full.',
8
+ }
@@ -0,0 +1,10 @@
1
+ export const envDev = {
2
+ production: false,
3
+ cdmUrl: 'https://imm-cdm-dev.herokuapp.com',
4
+ BIZA_MONGO_LINK: 'mongodb+srv://imm_biza:' +
5
+ encodeURIComponent('imm@2023') + '@cluster0.z2yr8kp.mongodb.net/?retryWrites=true&w=majority',
6
+ CDM_MONGO_LINK: 'mongodb+srv://imm_cdm:' +
7
+ encodeURIComponent('imm@2019') + '@imm-cdm-dev.rf6wr.mongodb.net/?retryWrites=true&w=majority',
8
+ COMPANY_REGISTER: 'companyregister-dev',
9
+ BIZA_SERVER_LINK: 'https://biz-a-dev-41e7c93a25e5.herokuapp.com'
10
+ };
package/envs/env.js ADDED
@@ -0,0 +1,10 @@
1
+ export const env = {
2
+ production: true,
3
+ cdmUrl: 'https://imm-cdm.herokuapp.com',
4
+ BIZA_MONGO_LINK: 'mongodb+srv://imm_biza:' +
5
+ encodeURIComponent('imm@2023') + '@cluster0.lezywmk.mongodb.net/?retryWrites=true&w=majority',
6
+ CDM_MONGO_LINK: 'mongodb+srv://imm_cdm:' +
7
+ encodeURIComponent('imm@2019') + '@imm-cdm.ohcqt.mongodb.net/?retryWrites=true&w=majority',
8
+ COMPANY_REGISTER: 'companyregister',
9
+ BIZA_SERVER_LINK: 'https://biz-a.herokuapp.com'
10
+ };
@@ -0,0 +1,26 @@
1
+ import * as nodemailer from "nodemailer";
2
+
3
+ function createEmailTransport(req, config) {
4
+ return nodemailer.createTransport(
5
+ req.body.smtpConfig ? req.body.smtpConfig : config.smtp
6
+ );
7
+ }
8
+
9
+ export function sendMailWatcher(req) {
10
+ try {
11
+ let config = req.body.config;
12
+ const transporter = createEmailTransport(req, config);
13
+ transporter.sendMail(req.body.mailOptions, (err, info) => {
14
+ if (err) {
15
+ console.log(err.message);
16
+ return err.message;
17
+ } else {
18
+ console.log(info);
19
+ return info;
20
+ }
21
+ })
22
+ } catch (err) {
23
+ console.log(err.message);
24
+ return err.message;
25
+ }
26
+ }
package/package.json CHANGED
@@ -1,25 +1,46 @@
1
1
  {
2
2
  "name": "biz-a-cli",
3
- "version": "2.3.1",
3
+ "nameDev": "biz-a-cli-dev",
4
+ "version": "2.3.3",
5
+ "versionDev": "0.0.20",
4
6
  "description": "",
5
7
  "main": "bin/index.js",
8
+ "type": "module",
9
+ "engines": {
10
+ "node": "20.9.0",
11
+ "npm": "10.1.0"
12
+ },
6
13
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
14
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch a",
15
+ "test1": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
16
+ "dev": "node --watch server.js"
8
17
  },
9
18
  "author": "Imamatek",
10
19
  "license": "ISC",
11
20
  "bin": {
12
- "uploadbiza": "./bin/index.js",
13
- "proxy": "./bin/proxy.js",
14
- "hub": "./bin/hub.js"
21
+ "uploadbiza": "bin/index.js",
22
+ "proxy": "bin/proxy.js",
23
+ "hub": "bin/hub.js",
24
+ "watcher": "bin/watcher.js"
15
25
  },
16
26
  "dependencies": {
17
- "axios": "^0.27.2",
27
+ "axios": "^1.6.8",
28
+ "axios-observable": "^2.0.0",
18
29
  "cors": "^2.8.5",
19
- "express": "^4.18.2",
30
+ "dayjs": "^1.11.10",
31
+ "express": "^4.18.3",
32
+ "mongodb": "^6.5.0",
20
33
  "net": "^1.0.2",
21
- "socket.io-client": "^4.7.1",
34
+ "nodemailer": "^6.9.12",
35
+ "socket.io-client": "^4.7.5",
22
36
  "socket.io-stream": "^0.9.1",
23
- "yargs": "^17.4.1"
37
+ "yargs": "^17.7.2"
38
+ },
39
+ "devDependencies": {
40
+ "jest": "^29.7.0",
41
+ "socket.io": "^4.7.5"
42
+ },
43
+ "jest": {
44
+ "transform": {}
24
45
  }
25
- }
46
+ }