mm_machine 2.9.6 → 2.9.8
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/drive.js +480 -675
- package/index.js +48 -1497
- package/mod.js +452 -494
- package/package.json +3 -4
- package/scanner.js +62 -0
- package/dir_scanner.js +0 -262
- package/id_gen.js +0 -138
- package/registry.js +0 -634
- package/word.js +0 -209
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_machine",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.8",
|
|
4
4
|
"description": "A flexible Node.js plugin mechanism system for dynamic loading, management and execution of modules. Supports hot reload, lifecycle management, and modern JavaScript features.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "node ./
|
|
7
|
+
"test": "node ./tests/run.js",
|
|
8
8
|
"dev": "nodemon ./test.js"
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
@@ -30,8 +30,7 @@
|
|
|
30
30
|
"mod.js",
|
|
31
31
|
"index.js",
|
|
32
32
|
"drive.js",
|
|
33
|
-
"
|
|
34
|
-
"id_gen.js",
|
|
33
|
+
"scanner.js",
|
|
35
34
|
"registry.js",
|
|
36
35
|
"word.js",
|
|
37
36
|
"README.md"
|
package/scanner.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 扫描类
|
|
3
|
+
* 负责按目录或文件名模式检索文件
|
|
4
|
+
*/
|
|
5
|
+
class Scanner {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数
|
|
8
|
+
* @param {object} config 配置项
|
|
9
|
+
*/
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = Object.assign(
|
|
12
|
+
{
|
|
13
|
+
ignore: ['*/node_modules/*', '*/.git/*', '*/cache/*', '*/backup/*']
|
|
14
|
+
},
|
|
15
|
+
config
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 扫描模块(按目录搜索)
|
|
22
|
+
* 先找所有匹配 dirname 的子目录,再在每个子目录中找匹配 filename 的文件
|
|
23
|
+
* @param {string} dir 根目录
|
|
24
|
+
* @param {string} filename 目标文件名
|
|
25
|
+
* @param {string} dirname 目标目录名
|
|
26
|
+
* @returns {Array} 文件路径数组
|
|
27
|
+
*/
|
|
28
|
+
Scanner.prototype.scan = async function (dir, filename, dirname) {
|
|
29
|
+
var dirs = await this.scanDir(dir, dirname);
|
|
30
|
+
var results = await Promise.all(
|
|
31
|
+
dirs.map((d) => {
|
|
32
|
+
return $.file.getAllAsync(d, filename);
|
|
33
|
+
})
|
|
34
|
+
);
|
|
35
|
+
return results.flat();
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 扫描模块目录(按目录搜索)
|
|
40
|
+
* 在指定目录下找所有匹配 dirname 的子目录
|
|
41
|
+
* @param {string} dir 根目录
|
|
42
|
+
* @param {string} dirname 目标目录名
|
|
43
|
+
* @returns {Array} 目录路径数组
|
|
44
|
+
*/
|
|
45
|
+
Scanner.prototype.scanDir = async function (dir, dirname) {
|
|
46
|
+
return await $.dir.getAllAsync(dir, dirname);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 扫描模块文件(按文件名搜索)
|
|
51
|
+
* 在指定目录下找所有匹配 filename 的文件
|
|
52
|
+
* @param {string} dir 根目录
|
|
53
|
+
* @param {string} filename 目标文件名
|
|
54
|
+
* @returns {Array} 文件路径数组
|
|
55
|
+
*/
|
|
56
|
+
Scanner.prototype.scanFile = async function (dir, filename) {
|
|
57
|
+
return await $.file.getAllAsync(dir, filename);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
module.exports = {
|
|
61
|
+
Scanner
|
|
62
|
+
};
|
package/dir_scanner.js
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
var fs = require('fs');
|
|
2
|
-
var path = require('path');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* DirScanner 目录检索器
|
|
6
|
-
* 独立工具类,负责扫描目录查找 JSON 配置文件
|
|
7
|
-
* 不依赖 Manager 或 Registry,可独立使用
|
|
8
|
-
*/
|
|
9
|
-
class DirScanner {
|
|
10
|
-
/**
|
|
11
|
-
* 构造函数
|
|
12
|
-
* @param {object} [cfg] 配置参数
|
|
13
|
-
* @param {number} [cfg.cache_ttl=30000] 缓存有效期(毫秒),默认 30 秒
|
|
14
|
-
*/
|
|
15
|
-
constructor(cfg) {
|
|
16
|
-
var opts = cfg || {};
|
|
17
|
-
this._cache = new Map();
|
|
18
|
-
this._cache_ttl = opts.cache_ttl || 30000;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 规范化扫描参数
|
|
24
|
-
* 处理参数重载:options 可选,force 可以是第三个参数
|
|
25
|
-
* @param {object|boolean} opts_in 配置对象或 force 标志
|
|
26
|
-
* @param {boolean} force_in force 标志
|
|
27
|
-
* @returns {{options: object, force: boolean}} 规范化后的参数对象
|
|
28
|
-
* @private
|
|
29
|
-
*/
|
|
30
|
-
DirScanner.prototype._normScanOpts = function (opts_in, force_in) {
|
|
31
|
-
var opts = opts_in;
|
|
32
|
-
var force = force_in;
|
|
33
|
-
if (typeof opts === 'boolean') {
|
|
34
|
-
force = opts;
|
|
35
|
-
opts = null;
|
|
36
|
-
}
|
|
37
|
-
return { options: opts || {}, force: force || false };
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 获取缓存结果
|
|
42
|
-
* @param {string} key 缓存键
|
|
43
|
-
* @returns {Array|null} 缓存结果,不存在或已过期返回 null
|
|
44
|
-
* @private
|
|
45
|
-
*/
|
|
46
|
-
DirScanner.prototype._getCache = function (key) {
|
|
47
|
-
var entry = this._cache.get(key);
|
|
48
|
-
if (!entry) return null;
|
|
49
|
-
if (Date.now() - entry.time > this._cache_ttl) {
|
|
50
|
-
this._cache.delete(key);
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
return entry.results;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 设置缓存
|
|
58
|
-
* @param {string} key 缓存键
|
|
59
|
-
* @param {Array} results 扫描结果
|
|
60
|
-
* @private
|
|
61
|
-
*/
|
|
62
|
-
DirScanner.prototype._setCache = function (key, results) {
|
|
63
|
-
this._cache.set(key, { results: results, time: Date.now() });
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* 递归扫描指定目录,查找匹配的 JSON 配置文件
|
|
68
|
-
* @param {string} dir 要扫描的目录路径
|
|
69
|
-
* @param {string} pattern 文件匹配模式(如 '*.json')
|
|
70
|
-
* @param {object} [opts_in] 可选项
|
|
71
|
-
* @param {string[]} [opts_in.exclude=['node_modules', '.git']] 排除的目录名列表
|
|
72
|
-
* @param {Function} [opts_in.filter] 自定义过滤器,接收 {file, name} 对象,返回 true 表示保留
|
|
73
|
-
* @param {boolean} [force_in] 是否强制重新扫描(忽略缓存)
|
|
74
|
-
* @returns {Array<{file: string, name: string}>} 扫描结果数组
|
|
75
|
-
*/
|
|
76
|
-
DirScanner.prototype.scanDir = function (dir, pattern, opts_in, force_in) {
|
|
77
|
-
if (typeof dir !== 'string' || !dir) {
|
|
78
|
-
throw new TypeError('目录路径必须是字符串');
|
|
79
|
-
}
|
|
80
|
-
if (typeof pattern !== 'string' || !pattern) {
|
|
81
|
-
throw new TypeError('文件匹配模式必须是字符串');
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
var opts = this._normScanOpts(opts_in, force_in);
|
|
85
|
-
var force = opts.force;
|
|
86
|
-
var options = opts.options;
|
|
87
|
-
|
|
88
|
-
// 缓存检查
|
|
89
|
-
var cache_key = dir + '::' + pattern;
|
|
90
|
-
if (!force) {
|
|
91
|
-
var cached = this._getCache(cache_key);
|
|
92
|
-
if (cached) return cached;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
var exclude = options.exclude || ['node_modules', '.git'];
|
|
96
|
-
var filter = options.filter || null;
|
|
97
|
-
var results = [];
|
|
98
|
-
var abs_dir = path.resolve(dir);
|
|
99
|
-
|
|
100
|
-
// 检查目录是否存在
|
|
101
|
-
if (!fs.existsSync(abs_dir)) {
|
|
102
|
-
return results;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
this._scanDirRecu(abs_dir, pattern, exclude, filter, results);
|
|
106
|
-
|
|
107
|
-
// 设置缓存
|
|
108
|
-
this._setCache(cache_key, results);
|
|
109
|
-
return results;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 递归扫描目录内部方法
|
|
114
|
-
* @param {string} dir 当前目录
|
|
115
|
-
* @param {string} pattern 匹配模式
|
|
116
|
-
* @param {string[]} exclude 排除目录
|
|
117
|
-
* @param {Function|null} filter 自定义过滤器
|
|
118
|
-
* @param {Array} results 结果数组
|
|
119
|
-
* @private
|
|
120
|
-
*/
|
|
121
|
-
DirScanner.prototype._scanDirRecu = function (dir, pattern, exclude, filter, results) {
|
|
122
|
-
var items;
|
|
123
|
-
try {
|
|
124
|
-
// eslint-disable-next-line mm_eslint/variable-name
|
|
125
|
-
items = fs.readdirSync(dir, { withFileTypes: true });
|
|
126
|
-
} catch {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
for (var i = 0; i < items.length; i++) {
|
|
131
|
-
var item = items[i];
|
|
132
|
-
var cur_path = path.join(dir, item.name);
|
|
133
|
-
|
|
134
|
-
if (item.isDirectory()) {
|
|
135
|
-
// 跳过排除目录
|
|
136
|
-
if (exclude.indexOf(item.name) !== -1) {
|
|
137
|
-
continue;
|
|
138
|
-
}
|
|
139
|
-
this._scanDirRecu(cur_path, pattern, exclude, filter, results);
|
|
140
|
-
} else if (item.isFile()) {
|
|
141
|
-
// 检查文件是否匹配模式
|
|
142
|
-
if (!this._matchPattern(item.name, pattern)) {
|
|
143
|
-
continue;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
var name = this._extractName(cur_path);
|
|
147
|
-
var entry = { file: cur_path, name: name };
|
|
148
|
-
|
|
149
|
-
// 应用自定义过滤器
|
|
150
|
-
if (filter) {
|
|
151
|
-
if (!filter(entry)) {
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
results.push(entry);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 检查文件名是否匹配模式
|
|
163
|
-
* @param {string} filename 文件名
|
|
164
|
-
* @param {string} pattern 匹配模式(如 *.json)
|
|
165
|
-
* @returns {boolean} 是否匹配
|
|
166
|
-
* @private
|
|
167
|
-
*/
|
|
168
|
-
DirScanner.prototype._matchPattern = function (filename, pattern) {
|
|
169
|
-
// 简单通配符匹配
|
|
170
|
-
if (pattern === '*') return true;
|
|
171
|
-
if (pattern === '*' + path.extname(filename)) return true;
|
|
172
|
-
if (pattern === filename) return true;
|
|
173
|
-
|
|
174
|
-
// 将 * 转换为正则
|
|
175
|
-
var regex_str = '^' + pattern.replace(/\./g, '\\.').replace(/\*/g, '.*') + '$';
|
|
176
|
-
return new RegExp(regex_str).test(filename);
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* 从 JSON 文件提取 name
|
|
181
|
-
* 优先读取 JSON 文件中的 name 字段,失败则从文件名去扩展名
|
|
182
|
-
* @param {string} file_path 文件路径
|
|
183
|
-
* @returns {string} 模块名称
|
|
184
|
-
* @private
|
|
185
|
-
*/
|
|
186
|
-
DirScanner.prototype._extractName = function (file_path) {
|
|
187
|
-
try {
|
|
188
|
-
var content = fs.readFileSync(file_path, 'utf8');
|
|
189
|
-
var config = JSON.parse(content);
|
|
190
|
-
if (config && config.name) {
|
|
191
|
-
return config.name;
|
|
192
|
-
}
|
|
193
|
-
} catch {
|
|
194
|
-
// JSON 解析失败,忽略
|
|
195
|
-
}
|
|
196
|
-
// 从文件名去扩展名
|
|
197
|
-
return path.basename(file_path, path.extname(file_path));
|
|
198
|
-
};
|
|
199
|
-
|
|
200
|
-
/**
|
|
201
|
-
* 扫描并解析 JSON 配置文件
|
|
202
|
-
* @param {string} dir 要扫描的目录路径
|
|
203
|
-
* @param {string} pattern 文件匹配模式
|
|
204
|
-
* @param {object} [options] 可选项(同 scanDir)
|
|
205
|
-
* @param {boolean} [force] 是否强制重新扫描
|
|
206
|
-
* @returns {Array<{file: string, config: object}>} 扫描结果数组,JSON 解析失败时跳过该文件
|
|
207
|
-
*/
|
|
208
|
-
DirScanner.prototype.scanAndParse = function (dir, pattern, options, force) {
|
|
209
|
-
var files = this.scanDir(dir, pattern, options, force);
|
|
210
|
-
var results = [];
|
|
211
|
-
|
|
212
|
-
for (var i = 0; i < files.length; i++) {
|
|
213
|
-
var entry = files[i];
|
|
214
|
-
try {
|
|
215
|
-
var content = fs.readFileSync(entry.file, 'utf8');
|
|
216
|
-
var config = JSON.parse(content);
|
|
217
|
-
results.push({ file: entry.file, config: config });
|
|
218
|
-
} catch {
|
|
219
|
-
// JSON 解析失败时跳过该文件
|
|
220
|
-
continue;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
return results;
|
|
225
|
-
};
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* 扫描目录并一键注册到 Manager
|
|
229
|
-
* @param {string} dir 要扫描的目录路径
|
|
230
|
-
* @param {object} manager Manager 实例
|
|
231
|
-
* @param {boolean} [force] 是否强制重新扫描
|
|
232
|
-
* @returns {number} 成功注册的模块数量
|
|
233
|
-
*/
|
|
234
|
-
DirScanner.prototype.scanAndRegister = function (dir, manager, force) {
|
|
235
|
-
if (!manager || typeof manager.registerMod !== 'function') {
|
|
236
|
-
throw new TypeError('manager 必须包含 registerMod 方法');
|
|
237
|
-
}
|
|
238
|
-
var entries = this.scanAndParse(dir, '*.json', null, force);
|
|
239
|
-
var count = 0;
|
|
240
|
-
|
|
241
|
-
for (var i = 0; i < entries.length; i++) {
|
|
242
|
-
var entry = entries[i];
|
|
243
|
-
try {
|
|
244
|
-
manager.registerMod(entry.file, entry.config);
|
|
245
|
-
count++;
|
|
246
|
-
} catch {
|
|
247
|
-
// 单个模块注册失败不影响其他模块
|
|
248
|
-
continue;
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return count;
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* 清除所有扫描缓存
|
|
257
|
-
*/
|
|
258
|
-
DirScanner.prototype.clearCache = function () {
|
|
259
|
-
this._cache.clear();
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
module.exports = { DirScanner };
|
package/id_gen.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* IdGen 雪花算法 ID 生成器
|
|
3
|
-
* 基于雪花算法(Snowflake)的分布式唯一 ID 生成器,生成 64 位整数 ID
|
|
4
|
-
*/
|
|
5
|
-
class IdGen {
|
|
6
|
-
/**
|
|
7
|
-
* 构造函数
|
|
8
|
-
* @param {object} config 配置参数
|
|
9
|
-
* @param {number} config.data_center_no 数据中心编号 (0-31)
|
|
10
|
-
* @param {number} config.machine_no 机器编号 (0-31)
|
|
11
|
-
* @param {number} [config.epoch=1700000000000] 自定义起始时间戳(毫秒),默认 2023-11-15 00:00:00 UTC
|
|
12
|
-
* @param {number} [config.sequence=0] 初始序列号
|
|
13
|
-
*/
|
|
14
|
-
constructor(config = {}) {
|
|
15
|
-
this.data_center_no = config.data_center_no || config.dc_id || 0;
|
|
16
|
-
this.machine_no = config.machine_no || config.worker_id || 0;
|
|
17
|
-
this.sequence = config.sequence || 0;
|
|
18
|
-
this.epoch = config.epoch || 1700000000000;
|
|
19
|
-
this.last_timestamp = -1;
|
|
20
|
-
|
|
21
|
-
this._initBits();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 获取当前时间戳
|
|
27
|
-
* @returns {number} 当前时间戳
|
|
28
|
-
*/
|
|
29
|
-
IdGen.prototype._timeGen = function () {
|
|
30
|
-
return Date.now();
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 等待下一毫秒
|
|
35
|
-
* @param {number} last_timestamp 上次时间戳
|
|
36
|
-
* @returns {number} 新的时间戳
|
|
37
|
-
*/
|
|
38
|
-
IdGen.prototype._tilNextMillis = function (last_timestamp) {
|
|
39
|
-
var timestamp = this._timeGen();
|
|
40
|
-
while (timestamp <= last_timestamp) {
|
|
41
|
-
timestamp = this._timeGen();
|
|
42
|
-
}
|
|
43
|
-
return timestamp;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 初始化位运算参数
|
|
48
|
-
* @private
|
|
49
|
-
*/
|
|
50
|
-
IdGen.prototype._initBits = function () {
|
|
51
|
-
this.dc_id_bits = 5;
|
|
52
|
-
this.worker_id_bits = 5;
|
|
53
|
-
this.sequence_bits = 12;
|
|
54
|
-
|
|
55
|
-
this.max_dc_id = -1 ^ (-1 << this.dc_id_bits);
|
|
56
|
-
this.max_worker_id = -1 ^ (-1 << this.worker_id_bits);
|
|
57
|
-
this.sequence_mask = -1 ^ (-1 << this.sequence_bits);
|
|
58
|
-
|
|
59
|
-
this.worker_id_shift = this.sequence_bits;
|
|
60
|
-
this.dc_id_shift = this.sequence_bits + this.worker_id_bits;
|
|
61
|
-
this.timestamp_left_shift = this.sequence_bits + this.worker_id_bits + this.dc_id_bits;
|
|
62
|
-
|
|
63
|
-
if (this.data_center_no > this.max_dc_id || this.data_center_no < 0) {
|
|
64
|
-
throw new TypeError('数据中心编号必须在0-' + this.max_dc_id + '之间');
|
|
65
|
-
}
|
|
66
|
-
if (this.machine_no > this.max_worker_id || this.machine_no < 0) {
|
|
67
|
-
throw new TypeError('机器编号必须在0-' + this.max_worker_id + '之间');
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 生成下一个ID
|
|
73
|
-
* @returns {string} 唯一ID
|
|
74
|
-
*/
|
|
75
|
-
IdGen.prototype.nextId = function () {
|
|
76
|
-
var timestamp = this._timeGen();
|
|
77
|
-
|
|
78
|
-
if (timestamp < this.last_timestamp) {
|
|
79
|
-
throw new Error('时钟回拨,拒绝生成ID');
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (this.last_timestamp === timestamp) {
|
|
83
|
-
this.sequence = (this.sequence + 1) & this.sequence_mask;
|
|
84
|
-
if (this.sequence === 0) {
|
|
85
|
-
timestamp = this._tilNextMillis(this.last_timestamp);
|
|
86
|
-
}
|
|
87
|
-
} else {
|
|
88
|
-
this.sequence = 0;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this.last_timestamp = timestamp;
|
|
92
|
-
|
|
93
|
-
var id =
|
|
94
|
-
(BigInt(timestamp - this.epoch) << BigInt(this.timestamp_left_shift)) |
|
|
95
|
-
(BigInt(this.data_center_no) << BigInt(this.dc_id_shift)) |
|
|
96
|
-
(BigInt(this.machine_no) << BigInt(this.worker_id_shift)) |
|
|
97
|
-
BigInt(this.sequence);
|
|
98
|
-
|
|
99
|
-
return id.toString();
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 生成带前缀的ID
|
|
104
|
-
* @param {string} prefix 前缀
|
|
105
|
-
* @returns {string} 带前缀的唯一ID
|
|
106
|
-
*/
|
|
107
|
-
IdGen.prototype.generateId = function (prefix) {
|
|
108
|
-
if (typeof prefix !== 'string') {
|
|
109
|
-
throw new TypeError('前缀必须是字符串');
|
|
110
|
-
}
|
|
111
|
-
return prefix + '_' + this.nextId();
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* 解析ID信息
|
|
116
|
-
* @param {string} id ID字符串
|
|
117
|
-
* @returns {object} ID信息对象
|
|
118
|
-
*/
|
|
119
|
-
IdGen.prototype.parseId = function (id) {
|
|
120
|
-
if (typeof id !== 'string') {
|
|
121
|
-
throw new TypeError('ID必须是字符串');
|
|
122
|
-
}
|
|
123
|
-
var num = BigInt(id.replace(/^[a-zA-Z_]+_/, ''));
|
|
124
|
-
var timestamp = Number((num >> 22n) + BigInt(this.epoch));
|
|
125
|
-
var dc_id = Number((num >> 17n) & 31n);
|
|
126
|
-
var worker_id = Number((num >> 12n) & 31n);
|
|
127
|
-
var sequence = Number(num & 4095n);
|
|
128
|
-
|
|
129
|
-
return {
|
|
130
|
-
timestamp: timestamp,
|
|
131
|
-
datetime: new Date(timestamp).toISOString(),
|
|
132
|
-
data_center_no: dc_id,
|
|
133
|
-
machine_no: worker_id,
|
|
134
|
-
sequence: sequence
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
module.exports = { IdGen };
|