beech-api 3.8.0 → 3.9.75
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/LICENSE +21 -21
- package/README.md +1715 -1244
- package/index.js +2 -2
- package/package.json +92 -83
- package/packages/cli/beech +9 -10
- package/packages/cli/bin/beech-app.js +389 -389
- package/packages/cli/bin/beech-service.js +262 -132
- package/packages/cli/core/auth/Credentials.js +174 -147
- package/packages/cli/core/auth/Passport.js +664 -592
- package/packages/cli/core/auth/_Request.js +12 -12
- package/packages/cli/core/configure/_gitignore +16 -15
- package/packages/cli/core/configure/_sequelizerc +9 -9
- package/packages/cli/core/configure/app.config-basic.js +55 -55
- package/packages/cli/core/configure/app.config-sequelize.js +88 -88
- package/packages/cli/core/configure/beech.config.js +9 -9
- package/packages/cli/core/configure/global.config-basic.js +8 -8
- package/packages/cli/core/configure/global.config-sequelize.js +8 -8
- package/packages/cli/core/configure/jest.config.js +6 -6
- package/packages/cli/core/configure/jsconfig.json +8 -7
- package/packages/cli/core/configure/passport.config.js +97 -97
- package/packages/cli/core/databases/mysql.js +94 -95
- package/packages/cli/core/databases/sequelize.js +187 -179
- package/packages/cli/core/databases/test.js +251 -170
- package/packages/cli/core/file-walk/file-walk.js +35 -35
- package/packages/cli/core/generator/_endpoints +15 -15
- package/packages/cli/core/generator/_endpoints_basic +42 -42
- package/packages/cli/core/generator/_help +26 -18
- package/packages/cli/core/generator/_help_create +10 -10
- package/packages/cli/core/generator/_help_service +10 -10
- package/packages/cli/core/generator/_helpers +9 -9
- package/packages/cli/core/generator/_helpers_basic +22 -22
- package/packages/cli/core/generator/_models +6 -29
- package/packages/cli/core/generator/_models_basic +13 -22
- package/packages/cli/core/generator/_package +23 -20
- package/packages/cli/core/generator/_scheduler +32 -22
- package/packages/cli/core/generator/_spec +29 -29
- package/packages/cli/core/generator/index.js +1081 -738
- package/packages/cli/core/helpers/2fa.js +106 -85
- package/packages/cli/core/helpers/math.js +115 -103
- package/packages/cli/core/helpers/poolEntity.js +103 -59
- package/packages/cli/core/index.js +264 -188
- package/packages/cli/core/middleware/express/duplicateRequest.js +16 -12
- package/packages/cli/core/middleware/express/jwtCheckAllow.js +86 -68
- package/packages/cli/core/middleware/express/rateLimit.js +29 -17
- package/packages/cli/core/middleware/express/slowDown.js +2 -2
- package/packages/cli/core/middleware/index.js +6 -6
- package/packages/cli/core/middleware/origin/guard/advance.js +75 -74
- package/packages/cli/core/middleware/origin/whitelist/cors.js +94 -94
- package/packages/cli/core/services/http.express.js +481 -441
- package/packages/cli/core/test/check-node.js +21 -0
- package/packages/cli/core/test/utils.js +7 -7
- package/packages/cli/entry +10 -0
- package/packages/lib/index.js +6 -6
- package/packages/lib/src/endpoint.js +947 -532
- package/packages/lib/src/guard.js +60 -60
- package/packages/lib/src/salt.js +3 -3
- package/packages/lib/src/schema.js +96 -93
- package/packages/lib/src/specificExpress.js +7 -7
- package/packages/lib/src/user.js +271 -271
|
@@ -1,133 +1,263 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
const logUpdate = require("log-update");
|
|
3
|
-
const notifier = require("node-notifier");
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const express = require("express");
|
|
6
|
-
const _app_ = express();
|
|
7
|
-
|
|
8
|
-
class Beech {
|
|
9
|
-
constructor() {
|
|
10
|
-
this.embed(process.argv)
|
|
11
|
-
.then(() => this.init()
|
|
12
|
-
.then(status => console.log(status))
|
|
13
|
-
.catch(err => {
|
|
14
|
-
throw err;
|
|
15
|
-
})
|
|
16
|
-
);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
init() {
|
|
20
|
-
return new Promise(async (resolve, reject) => {
|
|
21
|
-
try {
|
|
22
|
-
if (this.option == "serve") {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
2
|
+
const logUpdate = require("log-update");
|
|
3
|
+
const notifier = require("node-notifier");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const express = require("express");
|
|
6
|
+
const _app_ = express();
|
|
7
|
+
|
|
8
|
+
class Beech {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.embed(process.argv)
|
|
11
|
+
.then(() => this.init()
|
|
12
|
+
.then(status => console.log(status))
|
|
13
|
+
.catch(err => {
|
|
14
|
+
throw err;
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
init() {
|
|
20
|
+
return new Promise(async (resolve, reject) => {
|
|
21
|
+
try {
|
|
22
|
+
if (this.option == "serve") {
|
|
23
|
+
// Show comiling msg
|
|
24
|
+
const frames = ['\n[36m[-] Compiling[0m', '\n[36m[\\] Compiling.[0m', '\n[36m[|] Compiling..[0m', '\n[36m[/] Compiling...[0m'];
|
|
25
|
+
let i = 0;
|
|
26
|
+
var refreshCompileIntervalId = setInterval(() => {
|
|
27
|
+
const frame = frames[i = ++i % frames.length];
|
|
28
|
+
logUpdate(`${frame}`);
|
|
29
|
+
}, 300);
|
|
30
|
+
// option logic for silent notify
|
|
31
|
+
let turnNoti = true;
|
|
32
|
+
if (this.argument == "--silent" || this.argument == "-S") {
|
|
33
|
+
turnNoti = false;
|
|
34
|
+
}
|
|
35
|
+
// check project config file exists ?
|
|
36
|
+
if (this.fs.existsSync(this.configFile)) {
|
|
37
|
+
let testServ = require("http").createServer(_app_);
|
|
38
|
+
testServ.listen(this._config_.main_config.app_port, async () => {
|
|
39
|
+
testServ.close();
|
|
40
|
+
// Start real service.
|
|
41
|
+
this.serviceDevStart(this.argument, refreshCompileIntervalId, (err, run) => {
|
|
42
|
+
if(!err && run) {
|
|
43
|
+
// Check turn on noti
|
|
44
|
+
if (turnNoti) {
|
|
45
|
+
this.notiCompile();
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
if(refreshCompileIntervalId) clearInterval(refreshCompileIntervalId);
|
|
50
|
+
logUpdate("\n[101m[ERR] Failed... [0m", err);
|
|
51
|
+
reject();
|
|
52
|
+
}, 1000);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}).on('error', (err) => {
|
|
56
|
+
if(refreshCompileIntervalId) clearInterval(refreshCompileIntervalId);
|
|
57
|
+
console.log("\n[101m Fatal [0m", err);
|
|
58
|
+
reject();
|
|
59
|
+
})
|
|
60
|
+
} else {
|
|
61
|
+
if(refreshCompileIntervalId) clearInterval(refreshCompileIntervalId);
|
|
62
|
+
resolve("\n[101m Fatal [0m The app.conifg.js file is not found.");
|
|
63
|
+
}
|
|
64
|
+
} else if (!this.option || this.option == "-h" || this.option == "?" || this.option == "--help") {
|
|
65
|
+
// help for see avaliable command
|
|
66
|
+
this.help()
|
|
67
|
+
.then(help => resolve(help))
|
|
68
|
+
.catch(err => reject(err));
|
|
69
|
+
} else if (this.option == "build") {
|
|
70
|
+
this.runBuild()
|
|
71
|
+
.then((builded) => (builded) ? resolve("\n[32m[OK] Build completed successfully.\n[0m") : resolve('[33m[X] Build rejected.[0m\n'))
|
|
72
|
+
.catch(err => reject(`\n[101m[ERR] Build failed [0m: ${err}`));
|
|
73
|
+
} else {
|
|
74
|
+
// help for see avaliable command
|
|
75
|
+
this.help()
|
|
76
|
+
.then(help => resolve(help))
|
|
77
|
+
.catch(err => reject(err));
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
reject(error);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
serviceDevStart(argument, refreshCompileIntervalId, cb) {
|
|
86
|
+
if(refreshCompileIntervalId) clearInterval(refreshCompileIntervalId);
|
|
87
|
+
let promise = null;
|
|
88
|
+
try {
|
|
89
|
+
const spawnData = new Promise((resolve) => {
|
|
90
|
+
// check Dev. run service
|
|
91
|
+
if(argument == "-D" || argument == "--dev") {
|
|
92
|
+
console.log("\n[101m Starting Beech service in Development mode [0m");
|
|
93
|
+
promise = this.spawn('npx', ['nodemon', '-q', './cli/beech']); // For Dev.
|
|
94
|
+
} else {
|
|
95
|
+
promise = this.spawn('npx', ['nodemon', '-q', './node_modules/beech-api/packages/cli/beech']); // For Prd.
|
|
96
|
+
}
|
|
97
|
+
resolve(promise.childProcess);
|
|
98
|
+
});
|
|
99
|
+
Promise.all([spawnData]).then((childProcess) => {
|
|
100
|
+
childProcess[0].stdout.on('data', (data) => {
|
|
101
|
+
console.log(data.toString().slice(0, -1));
|
|
102
|
+
});
|
|
103
|
+
// Check process error
|
|
104
|
+
childProcess[0].stderr.on('data', (data) => {
|
|
105
|
+
// Check Error from std Allow for Mysql version error
|
|
106
|
+
if(data.toString().slice(0, 8) != "Ignoring" && data.toString().match(/\[SEQUELIZE0006\]/g) != "[SEQUELIZE0006]") {
|
|
107
|
+
if(data.toString().slice(0, 13) == "node:internal") {
|
|
108
|
+
cb(data.toString(), false);
|
|
109
|
+
} else {
|
|
110
|
+
console.log(data.toString());
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
// Callback first
|
|
115
|
+
cb(null, true);
|
|
116
|
+
});
|
|
117
|
+
} catch (error) {
|
|
118
|
+
cb(error, false);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async runBuild() {
|
|
123
|
+
// detect if current working directory is 'dist' to prevent build inside dist folder
|
|
124
|
+
if (process.cwd().endsWith('dist')) {
|
|
125
|
+
console.log("\n[101m[ERR] Fatal: Cannot run build command inside 'dist' folder.[0m\n");
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
const esbuild = require("esbuild");
|
|
129
|
+
const { glob } = require("glob");
|
|
130
|
+
const JavaScriptObfuscator = require("javascript-obfuscator");
|
|
131
|
+
const fs = require("fs");
|
|
132
|
+
const path = require("path");
|
|
133
|
+
const appRoot = require("app-root-path");
|
|
134
|
+
const _config_ = require(appRoot + "/app.config");
|
|
135
|
+
const beechTxtPath = path.join("./node_modules/beech-api/packages/cli/entry");
|
|
136
|
+
console.log("\n[33m[Obf] Starting Beech Secure Build... [0m\n");
|
|
137
|
+
try {
|
|
138
|
+
const projectFiles = glob.sync("{src/**/*.js,*.config.js}", {
|
|
139
|
+
ignore: ["node_modules/**", "dist/**", "cli/**"],
|
|
140
|
+
posix: true
|
|
141
|
+
});
|
|
142
|
+
// Check file exists to build
|
|
143
|
+
if (fs.existsSync(beechTxtPath)) {
|
|
144
|
+
projectFiles.push(beechTxtPath);
|
|
145
|
+
} else {
|
|
146
|
+
console.log("[101m[!] Warning: Beech entry not found at: [0m", beechTxtPath);
|
|
147
|
+
}
|
|
148
|
+
if (projectFiles.length === 0) {
|
|
149
|
+
console.log("[101m[!] No files found to build. [0m");
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
const MY_BUILD_SEED = _config_.main_config.app_key;
|
|
153
|
+
for (const file of projectFiles) {
|
|
154
|
+
let codeToObfuscate = "";
|
|
155
|
+
let fileName = file;
|
|
156
|
+
if (file === beechTxtPath) {
|
|
157
|
+
codeToObfuscate = fs.readFileSync(file, "utf8");
|
|
158
|
+
fileName = "server.js";
|
|
159
|
+
} else {
|
|
160
|
+
// Log file being processed
|
|
161
|
+
console.log(`[36m[+] dist/${file} [0m`);
|
|
162
|
+
const result = await esbuild.build({
|
|
163
|
+
entryPoints: [file],
|
|
164
|
+
bundle: false,
|
|
165
|
+
minify: true,
|
|
166
|
+
platform: "node",
|
|
167
|
+
format: "cjs",
|
|
168
|
+
write: false,
|
|
169
|
+
});
|
|
170
|
+
codeToObfuscate = result.outputFiles[0].text;
|
|
171
|
+
fileName = file;
|
|
172
|
+
}
|
|
173
|
+
const obfuscatedResult = JavaScriptObfuscator.obfuscate(codeToObfuscate, {
|
|
174
|
+
compact: true,
|
|
175
|
+
seed: MY_BUILD_SEED,
|
|
176
|
+
identifierNamesGenerator: 'hexadecimal',
|
|
177
|
+
renameGlobals: false,
|
|
178
|
+
stringArray: true,
|
|
179
|
+
stringArrayEncoding: ['base64'],
|
|
180
|
+
stringArrayThreshold: 0.8,
|
|
181
|
+
unicodeEscapeSequence: true,
|
|
182
|
+
controlFlowFlattening: false,
|
|
183
|
+
deadCodeInjection: false,
|
|
184
|
+
numbersToExpressions: false,
|
|
185
|
+
splitStrings: false,
|
|
186
|
+
selfDefending: false,
|
|
187
|
+
simplify: true,
|
|
188
|
+
});
|
|
189
|
+
const outPath = path.join("dist", fileName);
|
|
190
|
+
const outDir = path.dirname(outPath);
|
|
191
|
+
if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
|
|
192
|
+
fs.writeFileSync(outPath, obfuscatedResult.getObfuscatedCode());
|
|
193
|
+
}
|
|
194
|
+
// Copy package.json & Modify for Production
|
|
195
|
+
if (fs.existsSync("./package.json")) {
|
|
196
|
+
const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8"));
|
|
197
|
+
// set new start script for production
|
|
198
|
+
pkg.scripts = {
|
|
199
|
+
"prod-start": "node server.js",
|
|
200
|
+
"prod-pm2": "pm2 start server.js --name " + (pkg.name || "beech-api")
|
|
201
|
+
};
|
|
202
|
+
// delete devDependencies for production
|
|
203
|
+
delete pkg.devDependencies;
|
|
204
|
+
// write new package.json to dist
|
|
205
|
+
fs.writeFileSync(
|
|
206
|
+
path.join("dist", "package.json"),
|
|
207
|
+
JSON.stringify(pkg, null, 2)
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
} catch (error) {
|
|
212
|
+
console.log("\n[101m[ERR] Build Error [0m", error.message);
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
notiCompile() {
|
|
218
|
+
notifier.notify({
|
|
219
|
+
title: 'Beech API',
|
|
220
|
+
subtitle: 'Beech service getting started.',
|
|
221
|
+
message: "Service getting started.",
|
|
222
|
+
sound: 'Funk',
|
|
223
|
+
wait: false,
|
|
224
|
+
icon: path.join(__dirname, "/../../public/icon/beech_128.png"),
|
|
225
|
+
contentImage: path.join(__dirname, "../../public/icon/beech_128.png"),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
help() {
|
|
230
|
+
return new Promise((resolve, reject) => {
|
|
231
|
+
try {
|
|
232
|
+
this.fs.readFile(__dirname + "/../core/generator/_help_service", "utf8", (err, data) => {
|
|
233
|
+
if (err) {
|
|
234
|
+
reject(err);
|
|
235
|
+
} else {
|
|
236
|
+
resolve(data);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
} catch (error) {
|
|
240
|
+
reject(error);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
embed(argv) {
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
try {
|
|
248
|
+
this.fs = require("fs");
|
|
249
|
+
this.spawn = require('child-process-promise').spawn;
|
|
250
|
+
this.argv = argv;
|
|
251
|
+
this.option = argv[ 2 ]; // serve|build
|
|
252
|
+
this.argument = argv[ 3 ]; // --silent -S | --dev -D
|
|
253
|
+
this.configFile = path.resolve("./app.config.js");
|
|
254
|
+
this._config_ = require(this.configFile);
|
|
255
|
+
resolve(this);
|
|
256
|
+
} catch (error) {
|
|
257
|
+
reject(error);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
new Beech();
|