lz-nframe 1.0.2 → 1.0.3

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/lib/NApp.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  描述:
6
6
  应用基类
7
7
  *******************************************************************************/
8
+ /// <reference types="node" resolution-mode="require"/>
8
9
  import { EventEmitter } from 'events';
9
10
  export default abstract class NApp extends EventEmitter {
10
11
  protected appName: string;
@@ -0,0 +1,55 @@
1
+ /*******************************************************************************
2
+ 文件: NCrypto.ts
3
+ 创建: 2026年05月10日
4
+ 作者: 老张
5
+ 描述:
6
+ 常用编码与摘要工具:Base64、RC4 流密码、MD5/SHA1/SHA256。
7
+ 说明:RC4 已过时,仅建议用于兼容旧协议;新系统请使用现代 AEAD 算法。
8
+ *******************************************************************************/
9
+ /// <reference types="node" resolution-mode="require"/>
10
+ /** 可参与编码或摘要的二进制或文本输入(文本按 UTF-8 编码为字节) */
11
+ export type NCryptoInput = string | Buffer | Uint8Array;
12
+ export default class NCrypto {
13
+ /**
14
+ * 将数据编码为 Base64 字符串。
15
+ */
16
+ static base64Encode(data: NCryptoInput): string;
17
+ /**
18
+ * 将 Base64 字符串解码。
19
+ * @param asUtf8 为 true 时返回 UTF-8 文本;默认返回 Buffer 便于承载任意二进制。
20
+ */
21
+ static base64Decode(b64: string): Buffer;
22
+ static base64Decode(b64: string, asUtf8: true): string;
23
+ /**
24
+ * RC4 加密(流密码:与解密使用同一变换)。
25
+ */
26
+ static rc4Encrypt(data: NCryptoInput, key: NCryptoInput): Buffer;
27
+ /**
28
+ * RC4 解密(与加密相同,满足对称语义)。
29
+ */
30
+ static rc4Decrypt(data: NCryptoInput, key: NCryptoInput): Buffer;
31
+ /**
32
+ * MD5 摘要,返回小写十六进制字符串。
33
+ */
34
+ static md5(data: NCryptoInput): string;
35
+ /**
36
+ * SHA-1 摘要,返回小写十六进制字符串。
37
+ */
38
+ static sha1(data: NCryptoInput): string;
39
+ /**
40
+ * SHA-256 摘要,返回小写十六进制字符串。
41
+ */
42
+ static sha256(data: NCryptoInput): string;
43
+ /**
44
+ * 统一将输入转为 Buffer:字符串按 UTF-8,Buffer/Uint8Array 原样拷贝视图。
45
+ */
46
+ private static toBuffer;
47
+ /**
48
+ * 纯算法 RC4:KSA 初始化 S 盒,PRGA 生成密钥流并与数据异或。
49
+ */
50
+ private static rc4Transform;
51
+ /**
52
+ * Node crypto 摘要,输出 hex。
53
+ */
54
+ private static hashHex;
55
+ }
package/lib/NCrypto.js ADDED
@@ -0,0 +1,108 @@
1
+ /*******************************************************************************
2
+ 文件: NCrypto.ts
3
+ 创建: 2026年05月10日
4
+ 作者: 老张
5
+ 描述:
6
+ 常用编码与摘要工具:Base64、RC4 流密码、MD5/SHA1/SHA256。
7
+ 说明:RC4 已过时,仅建议用于兼容旧协议;新系统请使用现代 AEAD 算法。
8
+ *******************************************************************************/
9
+ import { createHash } from 'node:crypto';
10
+ export default class NCrypto {
11
+ /**
12
+ * 将数据编码为 Base64 字符串。
13
+ */
14
+ static base64Encode(data) {
15
+ return NCrypto.toBuffer(data).toString('base64');
16
+ }
17
+ static base64Decode(b64, asUtf8) {
18
+ const buf = Buffer.from(b64, 'base64');
19
+ if (asUtf8 === true) {
20
+ return buf.toString('utf8');
21
+ }
22
+ return buf;
23
+ }
24
+ /**
25
+ * RC4 加密(流密码:与解密使用同一变换)。
26
+ */
27
+ static rc4Encrypt(data, key) {
28
+ const dataBuf = NCrypto.toBuffer(data);
29
+ const keyBuf = NCrypto.toBuffer(key);
30
+ if (keyBuf.length === 0) {
31
+ throw new Error('RC4 密钥长度不能为 0');
32
+ }
33
+ return NCrypto.rc4Transform(dataBuf, keyBuf);
34
+ }
35
+ /**
36
+ * RC4 解密(与加密相同,满足对称语义)。
37
+ */
38
+ static rc4Decrypt(data, key) {
39
+ return NCrypto.rc4Encrypt(data, key);
40
+ }
41
+ /**
42
+ * MD5 摘要,返回小写十六进制字符串。
43
+ */
44
+ static md5(data) {
45
+ return NCrypto.hashHex('md5', data);
46
+ }
47
+ /**
48
+ * SHA-1 摘要,返回小写十六进制字符串。
49
+ */
50
+ static sha1(data) {
51
+ return NCrypto.hashHex('sha1', data);
52
+ }
53
+ /**
54
+ * SHA-256 摘要,返回小写十六进制字符串。
55
+ */
56
+ static sha256(data) {
57
+ return NCrypto.hashHex('sha256', data);
58
+ }
59
+ /**
60
+ * 统一将输入转为 Buffer:字符串按 UTF-8,Buffer/Uint8Array 原样拷贝视图。
61
+ */
62
+ static toBuffer(data) {
63
+ if (typeof data === 'string') {
64
+ return Buffer.from(data, 'utf8');
65
+ }
66
+ if (Buffer.isBuffer(data)) {
67
+ return data;
68
+ }
69
+ return Buffer.from(data);
70
+ }
71
+ /**
72
+ * 纯算法 RC4:KSA 初始化 S 盒,PRGA 生成密钥流并与数据异或。
73
+ */
74
+ static rc4Transform(data, key) {
75
+ const s = new Uint8Array(256);
76
+ for (let i = 0; i < 256; i++) {
77
+ s[i] = i;
78
+ }
79
+ let j = 0;
80
+ for (let i = 0; i < 256; i++) {
81
+ j = (j + s[i] + key[i % key.length]) % 256;
82
+ const si = s[i];
83
+ s[i] = s[j];
84
+ s[j] = si;
85
+ }
86
+ let i = 0;
87
+ j = 0;
88
+ const out = Buffer.alloc(data.length);
89
+ for (let k = 0; k < data.length; k++) {
90
+ i = (i + 1) % 256;
91
+ j = (j + s[i]) % 256;
92
+ const si = s[i];
93
+ s[i] = s[j];
94
+ s[j] = si;
95
+ const t = (s[i] + s[j]) % 256;
96
+ out[k] = data[k] ^ s[t];
97
+ }
98
+ return out;
99
+ }
100
+ /**
101
+ * Node crypto 摘要,输出 hex。
102
+ */
103
+ static hashHex(algorithm, data) {
104
+ const buf = NCrypto.toBuffer(data);
105
+ // 使用 Uint8Array 视图满足当前 @types/node 下 Hash.update 对 BinaryLike 的推导
106
+ return createHash(algorithm).update(new Uint8Array(buf)).digest('hex');
107
+ }
108
+ }
@@ -7,6 +7,7 @@
7
7
  - 支持自动处理中文 URL 编码
8
8
  - 支持泛型与类型安全
9
9
  *******************************************************************************/
10
+ /// <reference types="node" resolution-mode="require"/>
10
11
  import * as http from 'http';
11
12
  /**
12
13
  * 请求配置接口
@@ -126,10 +126,10 @@ export default class NHttpClient {
126
126
  // 依次检查各代理 header
127
127
  const candidates = [
128
128
  getHeader('x-real-ip'),
129
- getHeader('cf-connecting-ip'), // Cloudflare
130
- getHeader('true-client-ip'), // Akamai / Cloudflare Enterprise
129
+ getHeader('cf-connecting-ip'),
130
+ getHeader('true-client-ip'),
131
131
  getHeader('x-client-ip'),
132
- getHeader('x-forwarded-for'), // 通用代理,可能含多个 IP
132
+ getHeader('x-forwarded-for'),
133
133
  getHeader('proxy-client-ip'),
134
134
  getHeader('wl-proxy-client-ip'),
135
135
  ];
@@ -21,6 +21,7 @@
21
21
  }, {contentType: ContentType.TextPlain})
22
22
  .start(3000);
23
23
  *******************************************************************************/
24
+ /// <reference types="node" resolution-mode="require"/>
24
25
  import * as http from 'http';
25
26
  export default class NSimpleHttpServer {
26
27
  private server;
@@ -219,4 +219,4 @@ export var ContentType;
219
219
  ContentType["TextHtml"] = "Content-Type: text/html; charset=utf-8;";
220
220
  ContentType["TextPlain"] = "Content-Type: text/plain; charset=utf-8;";
221
221
  ContentType["ApplicationJson"] = "Content-Type: application/json; charset=utf-8;";
222
- })(ContentType || (ContentType = {}));
222
+ })(ContentType = ContentType || (ContentType = {}));
@@ -11,6 +11,8 @@ websocket事件:
11
11
  ✦onclose websocket关闭时触发该事件
12
12
  ✦onmessage 接收到消息
13
13
  *******************************************************************************/
14
+ /// <reference types="node" resolution-mode="require"/>
15
+ /// <reference types="node" resolution-mode="require"/>
14
16
  import { EventEmitter } from 'events';
15
17
  export default class NWSClient extends EventEmitter {
16
18
  static EventError: string;
@@ -11,6 +11,8 @@ websocket事件:
11
11
  ✦onclose websocket关闭时触发该事件
12
12
  ✦onmessage 接收到消息
13
13
  *******************************************************************************/
14
+ /// <reference types="node" resolution-mode="require"/>
15
+ /// <reference types="node" resolution-mode="require"/>
14
16
  import { EventEmitter } from 'events';
15
17
  import { NWSSocket } from './NWSSocket.js';
16
18
  export default class NWSServer extends EventEmitter {
@@ -73,6 +73,7 @@
73
73
  TODO:
74
74
  1.路由
75
75
  *******************************************************************************/
76
+ /// <reference types="node" resolution-mode="require"/>
76
77
  import { EventEmitter } from 'events';
77
78
  /**
78
79
  * 协议名说明(PacketType):
@@ -14,6 +14,7 @@
14
14
  ✦_si_ 服务端下发 resign 时须回传登录载荷;服务端下发 ok 表示登录完成
15
15
  ✦_hb_ 心跳,收到后原样应答
16
16
  *******************************************************************************/
17
+ /// <reference types="node" resolution-mode="require"/>
17
18
  import { EventEmitter } from 'events';
18
19
  /** 与 NWSUserMgrJ 配套的 JSON WebSocket 客户端 */
19
20
  export default class NWSUserClientJ extends EventEmitter {
@@ -26,6 +26,7 @@
26
26
 
27
27
  TODO 复用http服务端口,https://blog.csdn.net/qq_44856695/article/details/120250286
28
28
  *******************************************************************************/
29
+ /// <reference types="node" resolution-mode="require"/>
29
30
  import { EventEmitter } from 'events';
30
31
  import NWSServer from './NWSServer.js';
31
32
  /** 基于NWSServer的实时联网用户管理,通信格式使用json */
package/lib/index.d.ts CHANGED
@@ -18,5 +18,7 @@ export type { NWSServiceHubOptions, NWSServicePeerConfig } from './NWSServiceHub
18
18
  export { default as NWSUserClientJ } from './NWSUserClientJ.js';
19
19
  export { default as NWSUserMgrJ } from './NWSUserMgrJ.js';
20
20
  export type { NestedKeyOf, PlainType, PlainValue, ValueAtPath } from './NType.js';
21
+ export { default as NCrypto } from './NCrypto.js';
22
+ export type { NCryptoInput } from './NCrypto.js';
21
23
  /** 与旧版 main 指向 NApp.js 一致:默认导出仍为 NApp */
22
24
  export { default } from './NApp.js';
package/lib/index.js CHANGED
@@ -14,5 +14,6 @@ export { default as NWSServer } from './NWSServer.js';
14
14
  export { default as NWSServiceHub } from './NWSServiceHub.js';
15
15
  export { default as NWSUserClientJ } from './NWSUserClientJ.js';
16
16
  export { default as NWSUserMgrJ } from './NWSUserMgrJ.js';
17
+ export { default as NCrypto } from './NCrypto.js';
17
18
  /** 与旧版 main 指向 NApp.js 一致:默认导出仍为 NApp */
18
19
  export { default } from './NApp.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lz-nframe",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./lib/index.js",
@@ -15,8 +15,8 @@
15
15
  },
16
16
  "scripts": {
17
17
  "build": "tsc",
18
- "test": "node --import tsx --test test/NConfigsMgr.test.ts test/NHttpClient.test.ts test/NWSClient.test.ts test/NWSUserClientJ.test.ts test/NWSServiceHub.test.ts",
19
- "test:watch": "node --import tsx --test --watch test/NConfigsMgr.test.ts test/NHttpClient.test.ts test/NWSClient.test.ts test/NWSUserClientJ.test.ts test/NWSServiceHub.test.ts"
18
+ "test": "node --import tsx --test test/NConfigsMgr.test.ts test/NHttpClient.test.ts test/NWSClient.test.ts test/NWSUserClientJ.test.ts test/NWSServiceHub.test.ts test/NCrypto.test.ts",
19
+ "test:watch": "node --import tsx --test --watch test/NConfigsMgr.test.ts test/NHttpClient.test.ts test/NWSClient.test.ts test/NWSUserClientJ.test.ts test/NWSServiceHub.test.ts test/NCrypto.test.ts"
20
20
  },
21
21
  "author": "",
22
22
  "license": "ISC",