mm_os 4.0.0 → 4.0.2

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/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # MOS 服务端架构
1
+ # MM_OS 服务端架构
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/mm_os.svg)](https://badge.fury.io/js/mm_os)
4
4
  [![npm downloads](https://img.shields.io/npm/dm/mm_os.svg)](https://www.npmjs.com/package/mm_os)
@@ -6,9 +6,16 @@
6
6
 
7
7
  ## 项目简介
8
8
 
9
- MOS 是一个轻量级的服务端架构,提供了完整的服务端开发框架,包括 Web、WebSocket、MQTT、Socket 等多种通信协议支持,以及丰富的组件和模块。
9
+ MM_OS 是一个轻量级的服务端架构,提供了完整的服务端开发框架,包括 Web、WebSocket、MQTT、Socket 等多种通信协议支持,以及丰富的组件和模块。
10
10
 
11
- 适用于快速构建网站、小程序后台、AI应用、物联网应用、中小型游戏服务端。
11
+ **适用场景:**
12
+ - **网站建设**:快速构建企业网站、电商平台、内容管理系统
13
+ - **小程序**:微信小程序、支付宝小程序等小程序后台服务
14
+ - **AI 应用**:AI 接口服务、智能助手、机器学习推理服务
15
+ - **物联网 (IOT/AIOT)**:物联网设备管理、传感器数据采集、智能硬件控制
16
+ - **游戏服务端**:中小型游戏服务端、实时对战游戏、游戏后台管理
17
+
18
+ 支持 **AI、GAME、IOT、AIOT、游戏、物联网、小程序、网站建设** 等多种应用场景。
12
19
 
13
20
  ## 核心功能
14
21
 
@@ -20,7 +27,7 @@ MOS 是一个轻量级的服务端架构,提供了完整的服务端开发框
20
27
 
21
28
  ## 设计理念:配置与逻辑分离
22
29
 
23
- MOS 框架的核心设计原则是 **配置与逻辑分离**。无论是 `core` 核心模块还是 `com` 公共组件,每个功能模块都遵循统一的结构:
30
+ MM_OS 框架的核心设计原则是 **配置与逻辑分离**。无论是 `core` 核心模块还是 `com` 公共组件,每个功能模块都遵循统一的结构:
24
31
 
25
32
  ### 模块组成
26
33
 
@@ -110,7 +117,7 @@ startServer();
110
117
 
111
118
  ## 项目目录结构
112
119
 
113
- 基于 MOS 框架开发的项目采用标准化目录结构:
120
+ 基于 MM_OS 框架开发的项目采用标准化目录结构:
114
121
 
115
122
  ```
116
123
  ├── app/ # 应用目录,包含多个独立应用
@@ -205,7 +212,7 @@ startServer();
205
212
 
206
213
  ### 创建 API 接口
207
214
 
208
- API 是 MOS 框架的核心组件,每个 API 包含三个文件:
215
+ API 是 MM_OS 框架的核心组件,每个 API 包含三个文件:
209
216
 
210
217
  #### 1. api.json - API 配置
211
218
 
@@ -404,7 +411,7 @@ exports.main = main;
404
411
 
405
412
  ## 配置说明
406
413
 
407
- MOS 支持多种配置选项,包括:
414
+ MM_OS 支持多种配置选项,包括:
408
415
 
409
416
  - **web**:Web 服务器配置
410
417
  - **mysql**:MySQL 数据库配置
@@ -460,7 +467,7 @@ MOS 支持多种配置选项,包括:
460
467
 
461
468
  ## 全局对象
462
469
 
463
- MOS 提供了丰富的全局对象供开发使用:
470
+ MM_OS 提供了丰富的全局对象供开发使用:
464
471
 
465
472
  ### `$` 对象
466
473
 
package/README_EN.md CHANGED
@@ -1,4 +1,4 @@
1
- # MOS Server Architecture
1
+ # MM_OS Server Architecture
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/mm_os.svg)](https://badge.fury.io/js/mm_os)
4
4
  [![npm downloads](https://img.shields.io/npm/dm/mm_os.svg)](https://www.npmjs.com/package/mm_os)
@@ -6,9 +6,16 @@
6
6
 
7
7
  ## Project Introduction
8
8
 
9
- MOS is a lightweight server architecture that provides a complete server development framework, including support for multiple communication protocols such as Web, WebSocket, MQTT, Socket, as well as rich components and modules.
9
+ MM_OS is a lightweight server architecture that provides a complete server development framework, including support for multiple communication protocols such as Web, WebSocket, MQTT, Socket, as well as rich components and modules.
10
10
 
11
- Suitable for quickly building websites, mini-program backends, AI applications, IoT applications, and small-to-medium game servers.
11
+ **Use Cases:**
12
+ - **Website Development**: Quickly build enterprise websites, e-commerce platforms, content management systems
13
+ - **Mini Programs**: Backend services for WeChat Mini Programs, Alipay Mini Programs, etc.
14
+ - **AI Applications**: AI API services, intelligent assistants, machine learning inference services
15
+ - **IoT/AIoT**: IoT device management, sensor data collection, smart hardware control
16
+ - **Game Servers**: Small-to-medium game servers, real-time battle games, game backend management
17
+
18
+ Supports **AI, GAME, IOT, AIOT, Games, Internet of Things, Mini Programs, Website Development** and other application scenarios.
12
19
 
13
20
  ## Core Features
14
21
 
@@ -20,7 +27,7 @@ Suitable for quickly building websites, mini-program backends, AI applications,
20
27
 
21
28
  ## Design Philosophy: Configuration and Logic Separation
22
29
 
23
- The core design principle of MOS framework is **separation of configuration and logic**. Both `core` modules and `com` components follow a unified structure:
30
+ The core design principle of MM_OS framework is **separation of configuration and logic**. Both `core` modules and `com` components follow a unified structure:
24
31
 
25
32
  ### Module Composition
26
33
 
@@ -111,7 +118,7 @@ startServer();
111
118
 
112
119
  ## Project Directory Structure
113
120
 
114
- Projects developed with MOS framework follow a standardized directory structure:
121
+ Projects developed with MM_OS framework follow a standardized directory structure:
115
122
 
116
123
  ```
117
124
  ├── app/ # Application directory containing multiple apps
@@ -206,7 +213,7 @@ Define application routing rules:
206
213
 
207
214
  ### Create API Interface
208
215
 
209
- API is the core component of MOS framework. Each API consists of three files:
216
+ API is the core component of MM_OS framework. Each API consists of three files:
210
217
 
211
218
  #### 1. api.json - API Configuration
212
219
 
@@ -405,7 +412,7 @@ exports.main = main;
405
412
 
406
413
  ## Configuration
407
414
 
408
- MOS supports multiple configuration options:
415
+ MM_OS supports multiple configuration options:
409
416
 
410
417
  - **web**: Web server configuration
411
418
  - **mysql**: MySQL database configuration
@@ -461,7 +468,7 @@ MOS supports multiple configuration options:
461
468
 
462
469
  ## Global Objects
463
470
 
464
- MOS provides rich global objects for development:
471
+ MM_OS provides rich global objects for development:
465
472
 
466
473
  ### `$` Object
467
474
 
package/package.json CHANGED
@@ -1,8 +1,24 @@
1
1
  {
2
2
  "name": "mm_os",
3
- "description": "超级美眉服务端框架,用于快速构建应用程序,可快速构建网站、小程序后台、AI应用、物联网应用、中小型游戏服务端。",
4
- "version": "4.0.0",
3
+ "description": "MM_OS服务端架构,用于快速构建应用程序,支持网站建设、小程序后台、AI应用、物联网(IOT/AIOT)、游戏服务端等多种场景。",
4
+ "version": "4.0.2",
5
5
  "main": "index.js",
6
+ "keywords": [
7
+ "AI",
8
+ "GAME",
9
+ "IOT",
10
+ "AIOT",
11
+ "游戏",
12
+ "物联网",
13
+ "小程序",
14
+ "网站建设",
15
+ "服务端框架",
16
+ "NodeJS",
17
+ "WebSocket",
18
+ "MQTT",
19
+ "Socket",
20
+ "koa"
21
+ ],
6
22
  "author": "qww",
7
23
  "license": "ISC",
8
24
  "scripts": {
@@ -36,13 +52,7 @@
36
52
  "mm_statics": "^1.8.4",
37
53
  "mm_xml": "^1.1.7",
38
54
  "mqtt": "^5.15.1",
39
- "openai": "^6.39.0",
40
- "sharp": "^0.34.5",
41
- "cos-nodejs-sdk-v5": "^2.15.4",
42
- "jimp": "^1.6.1",
43
- "qq-guild-bot": "^2.9.5",
44
- "tencentcloud-sdk-nodejs-faceid": "^4.1.233",
45
- "tinify": "^1.8.2"
55
+ "openai": "^6.39.0"
46
56
  },
47
57
  "devDependencies": {
48
58
  "@types/jest": "^30.0.0",
@@ -66,6 +76,7 @@
66
76
  "adapter/",
67
77
  "com/",
68
78
  "common/",
69
- "core/"
79
+ "core/",
80
+ "ulits/"
70
81
  ]
71
82
  }
@@ -0,0 +1,134 @@
1
+ /**
2
+ * ID生成器
3
+ * 基于雪花算法实现,生成唯一ID
4
+ */
5
+ class IdGen {
6
+ /**
7
+ * 构造函数
8
+ * @param {object} config 配置参数
9
+ * @param {number} config.worker_id 工作机器ID (0-31)
10
+ * @param {number} config.dc_id 数据中心ID (0-31)
11
+ * @param {number} config.sequence 序列号
12
+ */
13
+ constructor(config = {}) {
14
+ this.worker_id = config.worker_id || 0;
15
+ this.dc_id = config.dc_id || 0;
16
+ this.sequence = config.sequence || 0;
17
+
18
+ this.worker_id_bits = 5;
19
+ this.dc_id_bits = 5;
20
+ this.max_worker_id = -1 ^ (-1 << this.worker_id_bits);
21
+ this.max_dc_id = -1 ^ (-1 << this.dc_id_bits);
22
+ this.sequence_bits = 12;
23
+
24
+ this.worker_id_shift = this.sequence_bits;
25
+ this.dc_id_shift = this.sequence_bits + this.worker_id_bits;
26
+ this.timestamp_left_shift = this.sequence_bits + this.worker_id_bits + this.dc_id_bits;
27
+ this.sequence_mask = -1 ^ (-1 << this.sequence_bits);
28
+
29
+ this.last_timestamp = -1;
30
+
31
+ this.twepoch = 1288834974657;
32
+
33
+ if (this.worker_id > this.max_worker_id || this.worker_id < 0) {
34
+ throw new TypeError(`工作机器ID必须在0-${this.max_worker_id}之间`);
35
+ }
36
+ if (this.dc_id > this.max_dc_id || this.dc_id < 0) {
37
+ throw new TypeError(`数据中心ID必须在0-${this.max_dc_id}之间`);
38
+ }
39
+ }
40
+ }
41
+
42
+ /**
43
+ * 获取当前时间戳
44
+ * @returns {number} 当前时间戳
45
+ */
46
+ IdGen.prototype._timeGen = function () {
47
+ return Date.now();
48
+ };
49
+
50
+ /**
51
+ * 等待下一毫秒
52
+ * @param {number} last_timestamp 上次时间戳
53
+ * @returns {number} 新的时间戳
54
+ */
55
+ IdGen.prototype._tilNextMillis = function (last_timestamp) {
56
+ var timestamp = this._timeGen();
57
+ while (timestamp <= last_timestamp) {
58
+ timestamp = this._timeGen();
59
+ }
60
+ return timestamp;
61
+ };
62
+
63
+ /**
64
+ * 生成下一个ID
65
+ * @returns {string} 唯一ID
66
+ */
67
+ IdGen.prototype.nextId = function () {
68
+ var timestamp = this._timeGen();
69
+
70
+ if (timestamp < this.last_timestamp) {
71
+ throw new Error('时钟回拨,拒绝生成ID');
72
+ }
73
+
74
+ if (this.last_timestamp === timestamp) {
75
+ this.sequence = (this.sequence + 1) & this.sequence_mask;
76
+ if (this.sequence === 0) {
77
+ timestamp = this._tilNextMillis(this.last_timestamp);
78
+ }
79
+ } else {
80
+ this.sequence = 0;
81
+ }
82
+
83
+ this.last_timestamp = timestamp;
84
+
85
+ var id = (BigInt(timestamp - this.twepoch) << BigInt(this.timestamp_left_shift))
86
+ | (BigInt(this.dc_id) << BigInt(this.dc_id_shift))
87
+ | (BigInt(this.worker_id) << BigInt(this.worker_id_shift))
88
+ | BigInt(this.sequence);
89
+
90
+ return id.toString();
91
+ };
92
+
93
+ /**
94
+ * 生成带前缀的ID
95
+ * @param {string} prefix 前缀
96
+ * @returns {string} 带前缀的唯一ID
97
+ */
98
+ IdGen.prototype.genId = function (prefix) {
99
+ if (typeof prefix !== 'string') {
100
+ throw new TypeError('前缀必须是字符串');
101
+ }
102
+ return prefix + '_' + this.nextId();
103
+ };
104
+
105
+ /**
106
+ * 解析ID信息
107
+ * @param {string} id ID字符串
108
+ * @returns {object} ID信息对象
109
+ */
110
+ IdGen.prototype.parseId = function (id) {
111
+ if (typeof id !== 'string') {
112
+ throw new TypeError('ID必须是字符串');
113
+ }
114
+ var num = BigInt(id.replace(/^[a-zA-Z_]+_/, ''));
115
+ var timestamp = Number((num >> 22n) + BigInt(this.twepoch));
116
+ var dc_id = Number((num >> 17n) & 31n);
117
+ var worker_id = Number((num >> 12n) & 31n);
118
+ var sequence = Number(num & 4095n);
119
+
120
+ return {
121
+ timestamp: timestamp,
122
+ datetime: new Date(timestamp).toISOString(),
123
+ dc_id: dc_id,
124
+ worker_id: worker_id,
125
+ sequence: sequence
126
+ };
127
+ };
128
+
129
+ var idGen = new IdGen();
130
+
131
+ module.exports = {
132
+ IdGen,
133
+ idGen
134
+ };
package/ulits/word.js ADDED
@@ -0,0 +1,206 @@
1
+ /**
2
+ * 单词工具类
3
+ * @class Word
4
+ */
5
+ class Word {
6
+ /**
7
+ * 构造函数
8
+ */
9
+ constructor() {
10
+
11
+ }
12
+ }
13
+
14
+ /**
15
+ * 分割单词
16
+ * @param {string} name 名称
17
+ * @returns {Array} 单词数组
18
+ */
19
+ Word.prototype.splitWords = function (name) {
20
+ if (!name || name.length === 0) {
21
+ return [];
22
+ }
23
+
24
+ var words = [];
25
+ var current_word = '';
26
+ var last_char_type = null;
27
+
28
+ for (var i = 0; i < name.length; i++) {
29
+ var char = name[i];
30
+ var char_info = this._getCharInfo(char);
31
+
32
+ // 处理下划线和数字
33
+ if (char_info.is_under || char_info.is_digit) {
34
+ last_char_type = this._handleSepa(words, current_word, char, char_info);
35
+ current_word = '';
36
+ continue;
37
+ }
38
+
39
+ // 处理第一个字符
40
+ if (current_word.length === 0) {
41
+ current_word = char;
42
+ last_char_type = char_info.type;
43
+ continue;
44
+ }
45
+
46
+ // 处理字符转换
47
+ var result = this._handleCharTran(words, current_word, char, last_char_type, char_info.type);
48
+ words = result.words;
49
+ current_word = result.current_word;
50
+ last_char_type = char_info.type;
51
+ }
52
+
53
+ // 添加最后一个单词
54
+ if (current_word.length > 0) {
55
+ words.push(current_word);
56
+ }
57
+
58
+ return words;
59
+ };
60
+
61
+ /**
62
+ * 获取字符信息
63
+ * @param {string} char 字符
64
+ * @returns {object} 字符信息
65
+ * @private
66
+ */
67
+ Word.prototype._getCharInfo = function (char) {
68
+ var is_upper = char >= 'A' && char <= 'Z';
69
+ var is_lower = char >= 'a' && char <= 'z';
70
+ var is_digit = char >= '0' && char <= '9';
71
+ var type = is_upper ? 'upper' : (is_lower ? 'lower' : (is_digit ? 'digit' : 'other'));
72
+ return { is_upper, is_lower, is_digit, is_under: char === '_', type };
73
+ };
74
+
75
+ /**
76
+ * 处理分隔符
77
+ * @param {Array} words 单词数组
78
+ * @param {string} current_word 当前单词
79
+ * @param {string} char 字符
80
+ * @param {object} char_info 字符信息
81
+ * @returns {string} 新的字符类型
82
+ * @private
83
+ */
84
+ Word.prototype._handleSepa = function (words, current_word, char, char_info) {
85
+ if (current_word.length > 0) {
86
+ words.push(current_word);
87
+ }
88
+ if (char_info.is_digit) {
89
+ words.push(char);
90
+ return 'digit';
91
+ }
92
+ return 'underscore';
93
+ };
94
+
95
+ /**
96
+ * 处理字符转换
97
+ * @param {Array} words 单词数组
98
+ * @param {string} current_word 当前单词
99
+ * @param {string} char 字符
100
+ * @param {string} last_type 上一个字符类型
101
+ * @param {string} current_type 当前字符类型
102
+ * @returns {object} 处理结果
103
+ * @private
104
+ */
105
+ Word.prototype._handleCharTran = function (
106
+ words, current_word, char, last_type, current_type
107
+ ) {
108
+ var is_last_letter = last_type === 'upper' || last_type === 'lower';
109
+ var is_current_letter = current_type === 'upper' || current_type === 'lower';
110
+
111
+ if (!is_last_letter || !is_current_letter) {
112
+ return { words, current_word: current_word + char };
113
+ }
114
+
115
+ if (last_type === 'upper' && current_type === 'lower') {
116
+ // 大写转小写
117
+ if (current_word.length > 1) {
118
+ var last_char = current_word[current_word.length - 1];
119
+ words.push(current_word.substring(0, current_word.length - 1));
120
+ return { words, current_word: last_char + char };
121
+ }
122
+ return { words, current_word: current_word + char };
123
+ }
124
+
125
+ if (last_type === 'lower' && current_type === 'upper') {
126
+ // 小写转大写
127
+ words.push(current_word);
128
+ return { words, current_word: char };
129
+ }
130
+
131
+ // 相同类型
132
+ return { words, current_word: current_word + char };
133
+ };
134
+
135
+ /**
136
+ * 将名称改为帕斯卡命名法
137
+ * @param {string} name 名称
138
+ * @returns {string} 帕斯卡命名法的名称
139
+ */
140
+ Word.prototype.toPascalCase = function (name) {
141
+ var words = this.splitWords(name);
142
+ for (let i = 0; i < words.length; i++) {
143
+ words[i] = words[i].toLowerCase();
144
+ words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
145
+ }
146
+ return words.join('');
147
+ };
148
+
149
+ /**
150
+ * 将名称改为驼峰命名法
151
+ * @param {string} name 名称
152
+ * @returns {string} 驼峰命名法的名称
153
+ */
154
+ Word.prototype.toCamelCase = function (name) {
155
+ var words = this.splitWords(name);
156
+ for (let i = 0; i < words.length; i++) {
157
+ words[i] = words[i].toLowerCase();
158
+ if (i > 0) {
159
+ words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
160
+ }
161
+ }
162
+ return words.join('');
163
+ };
164
+
165
+ /**
166
+ * 将名称改为小写蛇形(下划线)命名法
167
+ * @param {string} name 名称
168
+ * @returns {string} 小写蛇形(下划线)命名法的名称
169
+ */
170
+ Word.prototype.toSnakeCase = function (name) {
171
+ var words = this.splitWords(name);
172
+ for (let i = 0; i < words.length; i++) {
173
+ words[i] = words[i].toLowerCase();
174
+ }
175
+ return words.join('_');
176
+ };
177
+
178
+ /**
179
+ * 将名称改为大写蛇形(下划线)命名法
180
+ * @param {string} name 名称
181
+ * @returns {string} 大写蛇形(下划线)命名法的名称
182
+ */
183
+ Word.prototype.toUpperSnakeCase = function (name) {
184
+ var words = this.splitWords(name);
185
+ for (let i = 0; i < words.length; i++) {
186
+ words[i] = words[i].toUpperCase();
187
+ }
188
+ return words.join('_');
189
+ };
190
+
191
+ /**
192
+ * 将名称改为小写短横线(短横线)命名法
193
+ * @param {string} name 名称
194
+ * @returns {string} 小写短横线(短横线)命名法的名称
195
+ */
196
+ Word.prototype.toKebabCase = function (name) {
197
+ var words = this.splitWords(name);
198
+ for (let i = 0; i < words.length; i++) {
199
+ words[i] = words[i].toLowerCase();
200
+ }
201
+ return words.join('-');
202
+ };
203
+
204
+ module.exports = {
205
+ Word
206
+ };