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/.editorconfig +16 -0
- package/bin/app.js +659 -256
- package/bin/hub.js +185 -134
- package/bin/hubEvent.js +412 -284
- package/bin/log/debug.log +12 -0
- package/bin/log/error.log +12 -0
- package/bin/log/exception.log +6 -0
- package/bin/log/info.log +12 -0
- package/log/debug.log +181 -0
- package/log/error.log +7 -0
- package/log/exception.log +3 -0
- package/log/info.log +6 -0
- package/package.json +70 -71
- package/tests/app.test.js +1093 -588
- package/tests/hubPublish.test.js +231 -0
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
|
-
|
|
5
|
-
}
|
|
4
|
+
process.env.NODE_ENV = "production";
|
|
5
|
+
}
|
|
6
6
|
|
|
7
|
-
import yargs from
|
|
7
|
+
import yargs from "yargs";
|
|
8
8
|
import { io as ioClient } from "socket.io-client";
|
|
9
|
-
import {
|
|
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
|
|
12
|
-
import {
|
|
13
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
})
|
|
23
|
-
if (process.env.NODE_ENV !==
|
|
24
|
-
|
|
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(
|
|
28
|
-
|
|
29
|
-
|
|
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(
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
104
|
-
|
|
116
|
+
yargs().showHelp();
|
|
117
|
+
process.exit();
|
|
105
118
|
}
|
|
106
119
|
|
|
107
|
-
if (!argv[
|
|
108
|
-
|
|
109
|
-
|
|
120
|
+
if (!argv["server"] || !argv["subdomain"] || !argv["port"]) {
|
|
121
|
+
for (let key in ["server", "subdomain", "port"]) {
|
|
122
|
+
if (argv[key]) continue;
|
|
110
123
|
|
|
111
|
-
|
|
124
|
+
console.log("Error: Required option, but nothing found");
|
|
112
125
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
126
|
+
yargs().showHelp();
|
|
127
|
+
process.exit();
|
|
128
|
+
}
|
|
116
129
|
}
|
|
117
130
|
|
|
118
|
-
import express from
|
|
119
|
-
import compression from
|
|
120
|
-
import cors from
|
|
131
|
+
import express from "express";
|
|
132
|
+
import compression from "compression";
|
|
133
|
+
import cors from "cors";
|
|
121
134
|
const app = express();
|
|
122
|
-
import { runCliScript } from
|
|
123
|
-
import fs from "fs"
|
|
124
|
-
import http from
|
|
125
|
-
import https from
|
|
126
|
-
import path from
|
|
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:
|
|
143
|
+
app.use(express.json({ limit: "100mb" }));
|
|
131
144
|
|
|
132
|
-
app.set(
|
|
145
|
+
app.set("args", argv);
|
|
133
146
|
|
|
134
|
-
let socket = null
|
|
135
|
-
app.use(
|
|
136
|
-
|
|
137
|
-
|
|
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(
|
|
141
|
-
|
|
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,
|
|
146
|
-
const certFile = path.join(import.meta.dirname,
|
|
147
|
-
const rootFile = path.join(import.meta.dirname,
|
|
148
|
-
const isHttps =
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
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
|
-
|
|
157
|
-
|
|
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(
|
|
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
|
-
|
|
165
|
-
|
|
166
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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 };
|