@maiyunnet/kebab 3.1.14 → 3.1.15

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/index.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * ------------------------
9
9
  */
10
10
  /** --- 当前系统版本号 --- */
11
- export declare const VER = "3.1.14";
11
+ export declare const VER = "3.1.15";
12
12
  /** --- 框架根目录,以 / 结尾 --- */
13
13
  export declare const ROOT_PATH: string;
14
14
  export declare const LIB_PATH: string;
package/index.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * ------------------------
10
10
  */
11
11
  /** --- 当前系统版本号 --- */
12
- export const VER = '3.1.14';
12
+ export const VER = '3.1.15';
13
13
  // --- 服务端用的路径 ---
14
14
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
15
15
  /** --- /xxx/xxx --- */
package/lib/ws.d.ts CHANGED
@@ -97,6 +97,7 @@ export declare class Socket {
97
97
  /** --- 绑定监听 --- */
98
98
  on(event: 'message', cb: (msg: {
99
99
  'opcode': EOpcode;
100
+ 'buffer': Buffer;
100
101
  'data': Buffer | string;
101
102
  }) => void | Promise<void>): this;
102
103
  on(event: 'error', cb: (error: any) => void | Promise<void>): this;
package/lib/ws.js CHANGED
@@ -31,6 +31,7 @@ export class Socket {
31
31
  this._close = false;
32
32
  /** --- 未绑定自定义监听事件的默认执行函数 --- */
33
33
  this._on = {
34
+ /** --- 消息 --- */
34
35
  message: (msg) => {
35
36
  this._waitMsg.push(msg);
36
37
  },
@@ -135,28 +136,18 @@ export class Socket {
135
136
  }
136
137
  /** --- 创建成功后第一时间绑定事件 --- */
137
138
  _bindEvent() {
138
- this._ws.on('message', (msg) => {
139
+ this._ws.on('message', msg => {
139
140
  (async () => {
140
141
  if (msg.opcode === EOpcode.CLOSE) {
141
142
  return;
142
143
  }
143
- let data = '';
144
- if ('data' in msg) {
145
- data = Buffer.concat(msg.data);
146
- if (msg.opcode === EOpcode.TEXT) {
147
- data = data.toString();
148
- }
149
- }
150
- else {
151
- data = await (msg.opcode === EOpcode.TEXT ? msg.toString() : msg.toBuffer());
152
- }
144
+ const buf = 'data' in msg ? Buffer.concat(msg.data) : await msg.toBuffer();
153
145
  this._on.message({
154
146
  'opcode': msg.opcode,
155
- 'data': data
147
+ 'buffer': buf,
148
+ 'data': msg.opcode === EOpcode.TEXT ? buf.toString() : buf,
156
149
  });
157
- })().catch(() => {
158
- // --- nothing ---
159
- });
150
+ })().catch(() => { });
160
151
  }).on('drain', () => {
161
152
  this._on.drain();
162
153
  }).on('error', (e) => {
@@ -292,11 +283,7 @@ function bindPipe(s1, s2) {
292
283
  switch (msg.opcode) {
293
284
  case EOpcode.TEXT:
294
285
  case EOpcode.BINARY: {
295
- if (typeof msg.data === 'string') {
296
- s2.writeText(msg.data);
297
- break;
298
- }
299
- s2.writeBinary(msg.data);
286
+ s2.writeBinary(msg.buffer);
300
287
  break;
301
288
  }
302
289
  case EOpcode.CLOSE: {
@@ -325,11 +312,7 @@ function bindPipe(s1, s2) {
325
312
  switch (msg.opcode) {
326
313
  case EOpcode.TEXT:
327
314
  case EOpcode.BINARY: {
328
- if (typeof msg.data === 'string') {
329
- s1.writeText(msg.data);
330
- break;
331
- }
332
- s1.writeBinary(msg.data);
315
+ s1.writeBinary(msg.buffer);
333
316
  break;
334
317
  }
335
318
  case EOpcode.CLOSE: {
@@ -426,7 +409,7 @@ export async function rsocket(ctr, host, port) {
426
409
  switch (msg.opcode) {
427
410
  case EOpcode.TEXT:
428
411
  case EOpcode.BINARY: {
429
- socket.write(msg.data);
412
+ socket.write(msg.buffer);
430
413
  break;
431
414
  }
432
415
  case EOpcode.CLOSE: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "3.1.14",
3
+ "version": "3.1.15",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -0,0 +1,5 @@
1
+ import * as sCtr from '#kebab/sys/ctr.js';
2
+ export default class extends sCtr.Ctr {
3
+ onLoad(): boolean;
4
+ onClose(): void;
5
+ }
@@ -0,0 +1,15 @@
1
+ import * as sCtr from '#kebab/sys/ctr.js';
2
+ import * as lCore from '#kebab/lib/core.js';
3
+ export default class extends sCtr.Ctr {
4
+ onLoad() {
5
+ lCore.debug('WebSocket test main onLoad.');
6
+ setTimeout(() => {
7
+ this._writeText('Hello, world!');
8
+ this._end();
9
+ }, 2_000);
10
+ return true;
11
+ }
12
+ onClose() {
13
+ lCore.debug('WebSocket test main onClose');
14
+ }
15
+ }