biz-a-cli 2.3.70 → 2.3.72

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,182 +1,233 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  if (!process.env.NODE_ENV) {
4
- process.env.NODE_ENV = 'production';
5
- };
4
+ process.env.NODE_ENV = "production";
5
+ }
6
6
 
7
- import yargs from 'yargs';
7
+ import yargs from "yargs";
8
8
  import { io as ioClient } from "socket.io-client";
9
- import { streamEvent, hubEvent, RECONNECT_SOCKET_DELAY, status } from './hubEvent.js'
9
+ import {
10
+ streamEvent,
11
+ hubEvent,
12
+ RECONNECT_SOCKET_DELAY,
13
+ status,
14
+ } from "./hubEvent.js";
10
15
  import { createLogger, transports, format } from "winston";
11
- import os from 'os';
12
- import { directHubEvent, localhostTunnel, createSocketServer }from './directHubEvent.js'
13
- import { env } from "../envs/env.js"
16
+ import os from "os";
17
+ import {
18
+ directHubEvent,
19
+ localhostTunnel,
20
+ createSocketServer,
21
+ } from "./directHubEvent.js";
22
+ import { env } from "../envs/env.js";
14
23
 
15
24
  const logger = createLogger({
16
- level: 'info',
17
- transports: [
18
- new transports.File({ filename: 'log/error.log', level: 'error' }),
19
- new transports.File({ filename: 'log/debug.log', level: 'debug' }),
20
- new transports.File({ filename: 'log/info.log', level: 'info' }),
21
- ],
22
- })
23
- if (process.env.NODE_ENV !== 'production') {
24
- logger.add(new transports.Console({ format: format.simple(), level: 'info' }))
25
+ level: "info",
26
+ transports: [
27
+ new transports.File({ filename: "log/error.log", level: "error" }),
28
+ new transports.File({ filename: "log/debug.log", level: "debug" }),
29
+ new transports.File({ filename: "log/info.log", level: "info" }),
30
+ ],
31
+ });
32
+ if (process.env.NODE_ENV !== "production") {
33
+ logger.add(
34
+ new transports.Console({ format: format.simple(), level: "info" }),
35
+ );
25
36
  }
26
37
 
27
- process.on('uncaughtException', (err) => { //debug
28
- console.log('Unhandled Exception:', err);
29
- logger.error('Unhandled Exception:', err)
38
+ process.on("uncaughtException", (err) => {
39
+ //debug
40
+ console.log("Unhandled Exception:", err);
41
+ logger.error("Unhandled Exception:", err);
30
42
  });
31
43
 
32
- process.on('unhandledRejection', (err) => { //debug
33
- console.log('Unhandled Rejection:', err);
34
- logger.error('Unhandled Rejection:', err)
44
+ process.on("unhandledRejection", (err) => {
45
+ //debug
46
+ console.log("Unhandled Rejection:", err);
47
+ logger.error("Unhandled Rejection:", err);
35
48
  });
36
49
 
37
50
  const defaultPort = 3002;
38
51
  const argv = yargs(process.argv.slice(2))
39
- .usage('Usage: $0 [options]')
40
- .options('s', {
41
- alias: 'server',
42
- default: env.BIZA_SERVER_LINK,
43
- describe: '(Required) Tunnel server endpoint',
44
- type: 'string',
45
- demandOption: false
46
- })
47
- .options('sub', {
48
- alias: 'subdomain',
49
- describe: 'Public URL the tunnel server is forwarding to us',
50
- type: 'string',
51
- demandOption: true
52
- })
53
- .options('h', {
54
- alias: 'hostname',
55
- default: '127.0.0.1',
56
- describe: 'Address of API server for forwarding over socket-tunnel. Please emit "HTTP" or "HTTPS" from address',
57
- type: 'string',
58
- demandOption: false
59
- })
60
- .options('p', {
61
- alias: 'port',
62
- default: 212,
63
- describe: 'Port of API server for forwarding over socket-tunnel',
64
- type: 'number',
65
- demandOption: false
66
- })
67
- .options('secure', {
68
- default: false,
69
- describe: 'Is API server using ssl (HTTPS) ?',
70
- type: 'boolean',
71
- demandOption: false
72
- })
73
- .options('publish', {
74
- default: true,
75
- describe: 'Will the CLI be published to the internet??',
76
- type: 'boolean',
77
- demandOption: false
78
- })
79
- .options('d', {
80
- alias: 'dbindex',
81
- default: 2,
82
- describe: 'Biz-A Database Index (Callback Feature)',
83
- type: 'number',
84
- demandOption: false
85
- })
86
- .options('sp', {
87
- alias: 'serverport',
88
- default: defaultPort,
89
- describe: 'Express Port (Callback Feature)',
90
- type: 'number',
91
- demandOption: false
92
- })
93
- .options('hs', {
94
- alias: 'hubServer',
95
- default: `${env.BIZA_HUB_SERVER_LINK}`,
96
- describe: 'BizA hub',
97
- type: 'string',
98
- demandOption: false
99
- })
100
- .argv;
52
+ .usage("Usage: $0 [options]")
53
+ .options("s", {
54
+ alias: "server",
55
+ default: env.BIZA_SERVER_LINK,
56
+ describe: "(Required) Tunnel server endpoint",
57
+ type: "string",
58
+ demandOption: false,
59
+ })
60
+ .options("sub", {
61
+ alias: "subdomain",
62
+ describe: "Public URL the tunnel server is forwarding to us",
63
+ type: "string",
64
+ demandOption: true,
65
+ })
66
+ .options("h", {
67
+ alias: "hostname",
68
+ default: "127.0.0.1",
69
+ describe:
70
+ 'Address of API server for forwarding over socket-tunnel. Please emit "HTTP" or "HTTPS" from address',
71
+ type: "string",
72
+ demandOption: false,
73
+ })
74
+ .options("p", {
75
+ alias: "port",
76
+ default: 212,
77
+ describe: "Port of API server for forwarding over socket-tunnel",
78
+ type: "number",
79
+ demandOption: false,
80
+ })
81
+ .options("secure", {
82
+ default: false,
83
+ describe: "Is API server using ssl (HTTPS) ?",
84
+ type: "boolean",
85
+ demandOption: false,
86
+ })
87
+ .options("publish", {
88
+ default: true,
89
+ describe: "Will the CLI be published to the internet??",
90
+ type: "boolean",
91
+ demandOption: false,
92
+ })
93
+ .options("d", {
94
+ alias: "dbindex",
95
+ default: 2,
96
+ describe: "Biz-A Database Index (Callback Feature)",
97
+ type: "number",
98
+ demandOption: false,
99
+ })
100
+ .options("sp", {
101
+ alias: "serverport",
102
+ default: defaultPort,
103
+ describe: "Express Port (Callback Feature)",
104
+ type: "number",
105
+ demandOption: false,
106
+ })
107
+ .options("hs", {
108
+ alias: "hubServer",
109
+ default: `${env.BIZA_HUB_SERVER_LINK}`,
110
+ describe: "BizA hub",
111
+ type: "string",
112
+ demandOption: false,
113
+ }).argv;
101
114
 
102
115
  if (argv.help) {
103
- yargs().showHelp();
104
- process.exit();
116
+ yargs().showHelp();
117
+ process.exit();
105
118
  }
106
119
 
107
- if (!argv['server'] || !argv['subdomain'] || !argv['port']) {
108
- for (let key in ['server', 'subdomain', 'port']) {
109
- if (argv[key]) continue;
120
+ if (!argv["server"] || !argv["subdomain"] || !argv["port"]) {
121
+ for (let key in ["server", "subdomain", "port"]) {
122
+ if (argv[key]) continue;
110
123
 
111
- console.log('Error: Required option, but nothing found');
124
+ console.log("Error: Required option, but nothing found");
112
125
 
113
- yargs().showHelp();
114
- process.exit();
115
- }
126
+ yargs().showHelp();
127
+ process.exit();
128
+ }
116
129
  }
117
130
 
118
- import express from 'express';
119
- import compression from 'compression';
120
- import cors from 'cors';
131
+ import express from "express";
132
+ import compression from "compression";
133
+ import cors from "cors";
121
134
  const app = express();
122
- import { runCliScript } from '../callbackController.js'
123
- import fs from "fs"
124
- import http from 'http'
125
- import https from 'https'
126
- import path from 'node:path'
135
+ import { runCliScript } from "../callbackController.js";
136
+ import fs from "fs";
137
+ import http from "http";
138
+ import https from "https";
139
+ import path from "node:path";
127
140
 
128
141
  app.use(compression());
129
142
  app.use(cors());
130
- app.use(express.json({ limit: '100mb' }));
143
+ app.use(express.json({ limit: "100mb" }));
131
144
 
132
- app.set('args', argv);
145
+ app.set("args", argv);
133
146
 
134
- let socket = null
135
- app.use('/cb', (req, res)=>{
136
- req.socket = socket
137
- runCliScript(req, res)
147
+ let socket = null;
148
+ app.use("/cb", (req, res) => {
149
+ req.socket = socket;
150
+ runCliScript(req, res);
138
151
  });
139
152
 
140
- app.use('/status', (req, res)=>{
141
- res.status(200).json(status(argv));
153
+ app.use("/status", (req, res) => {
154
+ res.status(200).json(status(argv));
142
155
  });
143
156
 
144
157
  // create HTTP(s) Server
145
- const keyFile = path.join(import.meta.dirname, "../cert/key.pem")
146
- const certFile = path.join(import.meta.dirname, "../cert/cert.pem")
147
- const rootFile = path.join(import.meta.dirname, "../cert/root.pem")
148
- const isHttps = (fs.existsSync(keyFile) && fs.existsSync(certFile) && fs.existsSync(rootFile))
149
- const getProtocol = ()=>(isHttps ? 'Https://' : 'Http://')
150
- let server = isHttps ? https.createServer({key: fs.readFileSync(keyFile), cert: fs.readFileSync(certFile), ca: fs.readFileSync(rootFile),}, app) : http.createServer(app)
151
- argv.cliAddress = ()=>{
152
- const ip = Object.values(os.networkInterfaces()).flat().reduce((ip, {family, address, internal})=> ip || !internal && family==='IPv4' && address, undefined)
153
- return {ip, port: argv.serverport, address: `${ip}:${argv.serverport}`, publicUrl: argv.connectedPublicUrl, hubUrl: argv.connectedHubUrl};
158
+ const keyFile = path.join(import.meta.dirname, "../cert/key.pem");
159
+ const certFile = path.join(import.meta.dirname, "../cert/cert.pem");
160
+ const rootFile = path.join(import.meta.dirname, "../cert/root.pem");
161
+ const isHttps =
162
+ fs.existsSync(keyFile) &&
163
+ fs.existsSync(certFile) &&
164
+ fs.existsSync(rootFile);
165
+ const getProtocol = () => (isHttps ? "Https://" : "Http://");
166
+ let server = isHttps
167
+ ? https.createServer(
168
+ {
169
+ key: fs.readFileSync(keyFile),
170
+ cert: fs.readFileSync(certFile),
171
+ ca: fs.readFileSync(rootFile),
172
+ },
173
+ app,
174
+ )
175
+ : http.createServer(app);
176
+ argv.cliAddress = () => {
177
+ const ip = Object.values(os.networkInterfaces())
178
+ .flat()
179
+ .reduce(
180
+ (ip, { family, address, internal }) =>
181
+ ip || (!internal && family === "IPv4" && address),
182
+ undefined,
183
+ );
184
+ return {
185
+ ip,
186
+ port: argv.serverport,
187
+ address: `${ip}:${argv.serverport}`,
188
+ publicUrl: argv.connectedPublicUrl,
189
+ hubUrl: argv.connectedHubUrl,
190
+ };
154
191
  };
155
192
  server.listen(argv.serverport, () => {
156
- const info = argv.cliAddress()
157
- console.log(`${new Date()}: CLI is listening at ${getProtocol() + (process.env.HOST || info.ip || 'localhost')}:${info.port} `);
193
+ const info = argv.cliAddress();
194
+ console.log(
195
+ `${new Date()}: CLI is listening at ${getProtocol() + (process.env.HOST || info.ip || "localhost")}:${info.port} `,
196
+ );
158
197
  });
159
198
 
160
- await streamEvent(ioClient(argv['server'], {query: {isSockStream: true }}), argv); // as socket client to BizA SERVER
199
+ await streamEvent(
200
+ ioClient(argv["server"], { query: { isSockStream: true } }),
201
+ argv,
202
+ ); // as socket client to BizA SERVER
161
203
 
162
204
  // as socket client to BizA HUB
163
205
  if (argv.hubServer) {
164
- socket = hubEvent(ioClient(argv['hubServer'], { reconnectionDelay: RECONNECT_SOCKET_DELAY, reconnectionDelayMax: RECONNECT_SOCKET_DELAY, query: {isCLI: true, room: argv['subdomain']} }), argv, (url)=>{
165
- if (url!==argv.connectedHubUrl) {
166
- argv.connectedHubUrl = url
167
- }
168
- })
206
+ socket = hubEvent(
207
+ ioClient(argv["hubServer"], {
208
+ reconnectionDelay: RECONNECT_SOCKET_DELAY,
209
+ reconnectionDelayMax: RECONNECT_SOCKET_DELAY,
210
+ query: { isCLI: true, room: argv["subdomain"] },
211
+ }),
212
+ argv,
213
+ (url) => {
214
+ if (url !== argv.connectedHubUrl) {
215
+ argv.connectedHubUrl = url;
216
+ }
217
+ },
218
+ );
169
219
  }
170
220
 
171
221
  // as socket server for BizA Client and BizA devTools
172
222
  directHubEvent(createSocketServer(server, argv.cliAddress().ip), argv);
173
223
 
174
- if (argv.publish==true) {
175
- localhostTunnel(argv.serverport, (url)=>{ // publish CLI
176
- if (url!==argv.connectedPublicUrl) {
177
- argv.connectedPublicUrl = url
178
- }
179
- })
224
+ if (argv.publish == true) {
225
+ localhostTunnel(argv.serverport, (url) => {
226
+ // publish CLI
227
+ if (url !== argv.connectedPublicUrl) {
228
+ argv.connectedPublicUrl = url;
229
+ }
230
+ });
180
231
  }
181
232
 
182
- export { app }
233
+ export { app };