backend-core-bm 1.0.0 → 1.0.4

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.
@@ -0,0 +1,6 @@
1
+ module.exports = (reqKey, reqValue) => {
2
+ return (req, res, next) => {
3
+ if (req.headers[reqKey] != reqValue) res.status(403).send("Forbidden");
4
+ return next();
5
+ };
6
+ };
package/core.js CHANGED
@@ -62,7 +62,7 @@ un.textEncryptBase64 = (text, encrypting = true) => {
62
62
  * normalize path, also replace ~ with $home
63
63
  */
64
64
  un.filePathNormalize = (...path) =>
65
- u.stringReplace(paths.normalize(paths.join(...path)), { "~": process.env.HOME, "\\\\\\\\": "/", "\\\\": "/" });
65
+ u.stringReplace(paths.normalize(paths.join(...path)), { "^~": process.env.HOME, "\\\\\\\\": "/", "\\\\": "/" });
66
66
 
67
67
  un.filePathFull = (...path) => paths.resolve(...path);
68
68
 
@@ -120,10 +120,11 @@ un.fileMkdirTouch = (path, recursive = true) => {
120
120
  });
121
121
  };
122
122
 
123
- un.fileMove = async (source, target, mkdir = true, overwrite = true) => {
123
+ un.fileMove = async (source, target, mkdir = true, overwrite = true, onlyCopy = false) => {
124
124
  source = un.filePathNormalize(source);
125
125
  target = un.filePathNormalize(target);
126
126
  if (mkdir) fse.mkdirpSync(paths.dirname(target));
127
+ if (onlyCopy) return fse.copySync(source, target, { overwrite });
127
128
  return fse.moveSync(source, target, { overwrite });
128
129
  };
129
130
 
package/framework.js CHANGED
@@ -7,6 +7,7 @@ const loggerHandle = require("./Addon/loggerHandle");
7
7
  const secretHandle = require("./Addon/secretHandle");
8
8
  const envHandle = require("./Addon/envHandle");
9
9
  const mwHandle = require("./Addon/middleware");
10
+ const workerAuth = require("./Addon/workerAuth");
10
11
  require("./typedef");
11
12
 
12
13
  module.exports = class Framework {
@@ -46,12 +47,17 @@ module.exports = class Framework {
46
47
  logger: un.filePathNormalize(__dirname, "../../Logger"),
47
48
  secret: un.filePathNormalize(__dirname, "../../Personal"),
48
49
  },
50
+ router: {},
51
+ oauth: {
52
+ enable: false,
53
+ reqKey: "workerauth",
54
+ reqValue: un.uuid(),
55
+ },
49
56
  secret: {
50
57
  filename: "config.js",
51
- keys: ["master", "listen"],
58
+ keys: ["master", "listen", "oauth"],
52
59
  additional: {},
53
60
  },
54
- router: {},
55
61
  },
56
62
  config
57
63
  );
@@ -117,6 +123,7 @@ module.exports = class Framework {
117
123
  });
118
124
 
119
125
  task.add("requests", async () => {
126
+ if (this.config.oauth.enable) this.app.use(workerAuth(this.config.oauth.reqKey, this.config.oauth.reqValue));
120
127
  for (let i in this.config.router) this.app.all(i, mwHandle(this.logger, this.config.router[i]));
121
128
 
122
129
  this.app.use((error, req, res, next) => {
@@ -131,9 +138,10 @@ module.exports = class Framework {
131
138
  });
132
139
 
133
140
  task.add("listening", async () => {
134
- this.server = this.app.listen(this.config.listen, () =>
135
- this.logger.info(`server listen on http port ${this.config.listen}`)
136
- );
141
+ let infomsg = `Listening on port ${this.config.listen}`;
142
+ if (this.config.oauth.enable)
143
+ infomsg += ` oauth key: ${this.config.oauth.reqKey} value: ${this.config.oauth.reqValue}`;
144
+ this.server = this.app.listen(this.config.listen, () => this.logger.info(infomsg));
137
145
  });
138
146
 
139
147
  task.add("pre-terminate", () => {
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  const un = require("./core.js");
2
2
  const Framework = require("./framework");
3
+ const typedef = () => require("./typedef");
3
4
 
4
- module.exports = { un, Framework };
5
+ module.exports = { un, Framework, typedef };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backend-core-bm",
3
- "version": "1.0.0",
3
+ "version": "1.0.4",
4
4
  "main": "index.js",
5
5
  "description": "utils",
6
6
  "scripts": {
@@ -25,7 +25,7 @@
25
25
  "colors": "^1.4.0",
26
26
  "download": "^6.2.5",
27
27
  "express": "^4.17.1",
28
- "fs-extra": "^9.0.1",
28
+ "fs-extra": "^10.0.0",
29
29
  "html-entities": "^1.3.1",
30
30
  "iconv-lite": "^0.6.2",
31
31
  "readdirp": "^3.4.0",
package/typedef.js CHANGED
@@ -9,4 +9,5 @@
9
9
  * @property {{devOverride:true, type : "on" | "off" | "bunyan-dev" | "bunyan", bunyan : { name : string, baseLevel: "trace" | "debug" | "info" | "warn" | "error" | "fatal" }}} logger
10
10
  * @property {{logger:string, secret:string}} directories
11
11
  * @property {{filename: string, keys: string[], additional: {}}} secret
12
+ * @property {{enable:false, reqKey: string, reqValue: string}} oauth
12
13
  */