wenay-common2 1.0.33 → 1.0.34

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.
@@ -3,6 +3,15 @@ import { type PromiseServerHooks, type RpcLimits } from "./rpc-server";
3
3
  import { DeepSocketListen } from "./listen-deep";
4
4
  import { type SocketTmpl } from "./rpc-protocol";
5
5
  type ListenCallbackBase<T extends any[] = any[]> = ReturnType<typeof funcListenCallbackBase<T>>;
6
+ export declare function createRpcServerAuto<T extends object>({ socket, object: target, socketKey: key, debug, hooks, disconnectListen, limits }: {
7
+ socket: SocketTmpl;
8
+ object: T;
9
+ socketKey: string;
10
+ debug?: boolean;
11
+ hooks?: Omit<PromiseServerHooks<DeepSocketListen<T>>, "resolveTransform">;
12
+ disconnectListen?: ListenCallbackBase<any>;
13
+ limits?: RpcLimits;
14
+ }): void;
6
15
  type ClientProtocol = 'v2' | 'legacy' | null;
7
16
  export declare function createRpcServerAuto2<T extends object>({ socket, object: target, socketKey: key, debug, hooks, disconnectListen, limits, onProtocolDetect, }: {
8
17
  socket: SocketTmpl;
@@ -15,6 +24,7 @@ export declare function createRpcServerAuto2<T extends object>({ socket, object:
15
24
  onProtocolDetect?: (protocol: 'v2' | 'legacy') => void;
16
25
  }): {
17
26
  getProtocol: () => ClientProtocol;
18
- getSchema: () => any;
27
+ getLegacySchema: () => any;
28
+ getResolved: () => any;
19
29
  };
20
30
  export {};
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createRpcServerAuto = createRpcServerAuto;
3
4
  exports.createRpcServerAuto2 = createRpcServerAuto2;
4
5
  const Listen_1 = require("../events/Listen");
5
6
  const listen_socket_1 = require("./listen-socket");
6
7
  const rpc_server_1 = require("./rpc-server");
7
8
  const rpc_protocol_1 = require("./rpc-protocol");
8
9
  const old_ommonsServerMini_1 = require("./old\u0421ommonsServerMini");
9
- function createRpcServerAuto_({ socket, object: target, socketKey: key, debug, hooks, disconnectListen, limits }) {
10
+ function createRpcServerAuto({ socket, object: target, socketKey: key, debug, hooks, disconnectListen, limits }) {
10
11
  const cache = new WeakMap();
11
12
  function getListenSocket(parent, disconnectListen) {
12
13
  let result = cache.get(parent);
@@ -44,7 +45,9 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
44
45
  return getListenSocket(obj);
45
46
  }
46
47
  function transformTree(obj) {
47
- if (obj == null || typeof obj !== 'object')
48
+ if (obj == null || typeof obj === 'function')
49
+ return obj;
50
+ if (typeof obj !== 'object')
48
51
  return obj;
49
52
  const transformed = resolveTransform(obj);
50
53
  if (transformed !== obj)
@@ -52,7 +55,12 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
52
55
  const out = {};
53
56
  for (const k of Object.keys(obj)) {
54
57
  const v = obj[k];
55
- out[k] = typeof v === 'function' ? v : (v != null && typeof v === 'object') ? transformTree(v) : v;
58
+ if (typeof v === 'function')
59
+ out[k] = v;
60
+ else if (v != null && typeof v === 'object')
61
+ out[k] = transformTree(v);
62
+ else
63
+ out[k] = v;
56
64
  }
57
65
  return out;
58
66
  }
@@ -72,22 +80,25 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
72
80
  return out;
73
81
  }
74
82
  const resolved = transformTree(target);
75
- const strictSchema = serialize(resolved);
83
+ const legacySchema = serialize(resolved);
76
84
  let protocol = null;
77
85
  let v2Handler = null;
78
86
  let legacyHandler = null;
87
+ function isLegacyStrictRequest(msg) {
88
+ return msg === '___STRICTLY';
89
+ }
79
90
  function isLegacyMessage(msg) {
80
91
  return (typeof msg === 'object' &&
81
92
  msg !== null &&
82
93
  !Array.isArray(msg) &&
83
- typeof msg.mapId === 'number' &&
84
- (msg.data !== undefined || msg.error !== undefined));
85
- }
86
- function isLegacyStrictRequest(msg) {
87
- return msg === '___STRICTLY';
94
+ typeof msg.mapId === 'number');
88
95
  }
89
96
  function isV2Message(msg) {
90
- return (Array.isArray(msg) && (msg[0] === rpc_protocol_1.Pkt.CALL || msg[0] === rpc_protocol_1.Pkt.PIPE)) || msg === rpc_protocol_1.Pkt.STRICT;
97
+ if (msg === rpc_protocol_1.Pkt.STRICT)
98
+ return true;
99
+ if (Array.isArray(msg) && (msg[0] === rpc_protocol_1.Pkt.CALL || msg[0] === rpc_protocol_1.Pkt.PIPE))
100
+ return true;
101
+ return false;
91
102
  }
92
103
  function initLegacy() {
93
104
  if (legacyHandler)
@@ -97,7 +108,11 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
97
108
  sendMessage: (msg) => socket.emit(key, msg),
98
109
  api: ({ onMessage }) => { onMessageCb = onMessage; },
99
110
  }, resolved);
100
- legacyHandler = (msg) => onMessageCb?.(msg);
111
+ legacyHandler = (msg) => {
112
+ if (!onMessageCb)
113
+ return;
114
+ onMessageCb(msg);
115
+ };
101
116
  }
102
117
  function initV2() {
103
118
  if (v2Handler)
@@ -108,64 +123,72 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
108
123
  on: (e, cb) => { if (e === key)
109
124
  onMsgCb = cb; },
110
125
  };
111
- if (debug) {
112
- const origOn = innerSocket.on.bind(innerSocket);
113
- innerSocket.on = (e, cb) => origOn(e, (d) => { console.log('[RPC IN]', typeof d === 'object' ? JSON.stringify(d) : d); cb(d); });
114
- }
115
126
  (0, rpc_server_1.createRpcServer)({
116
127
  socket: innerSocket,
117
128
  object: target,
118
129
  socketKey: key,
119
- debug: false,
130
+ debug,
120
131
  limits,
121
132
  hooks: {
122
133
  ...hooks,
123
134
  resolveTransform,
124
135
  },
125
136
  });
126
- v2Handler = (msg) => onMsgCb?.(msg);
137
+ v2Handler = (msg) => {
138
+ if (!onMsgCb)
139
+ return;
140
+ onMsgCb(msg);
141
+ };
127
142
  }
128
143
  socket.on(key, (msg) => {
129
144
  if (debug) {
130
- console.log('[RPC-AUTO2]', typeof msg === 'object' ? JSON.stringify(msg) : msg);
145
+ console.log('[RPC-AUTO2 IN]', typeof msg === 'object' ? JSON.stringify(msg) : msg);
131
146
  }
132
- if (protocol === null) {
147
+ if (protocol === 'legacy') {
133
148
  if (isLegacyStrictRequest(msg)) {
134
- protocol = 'legacy';
135
- onProtocolDetect?.('legacy');
136
- socket.emit(key, { STRICTLY: strictSchema });
137
- initLegacy();
138
- return;
139
- }
140
- if (isLegacyMessage(msg)) {
141
- protocol = 'legacy';
142
- onProtocolDetect?.('legacy');
143
- initLegacy();
144
- legacyHandler(msg);
145
- return;
146
- }
147
- if (isV2Message(msg)) {
148
- protocol = 'v2';
149
- onProtocolDetect?.('v2');
150
- initV2();
151
- v2Handler(msg);
149
+ socket.emit(key, { STRICTLY: legacySchema });
152
150
  return;
153
151
  }
152
+ legacyHandler(msg);
154
153
  return;
155
154
  }
156
- if (protocol === 'legacy') {
157
- if (isLegacyStrictRequest(msg)) {
158
- socket.emit(key, { STRICTLY: strictSchema });
159
- return;
160
- }
155
+ if (protocol === 'v2') {
156
+ v2Handler(msg);
157
+ return;
158
+ }
159
+ if (isLegacyStrictRequest(msg)) {
160
+ protocol = 'legacy';
161
+ if (debug)
162
+ console.log('[RPC-AUTO2] Protocol detected: legacy (___STRICTLY)');
163
+ onProtocolDetect?.('legacy');
164
+ initLegacy();
165
+ socket.emit(key, { STRICTLY: legacySchema });
166
+ return;
167
+ }
168
+ if (isLegacyMessage(msg)) {
169
+ protocol = 'legacy';
170
+ if (debug)
171
+ console.log('[RPC-AUTO2] Protocol detected: legacy (mapId message)');
172
+ onProtocolDetect?.('legacy');
173
+ initLegacy();
161
174
  legacyHandler(msg);
175
+ return;
162
176
  }
163
- else {
177
+ if (isV2Message(msg)) {
178
+ protocol = 'v2';
179
+ if (debug)
180
+ console.log('[RPC-AUTO2] Protocol detected: v2');
181
+ onProtocolDetect?.('v2');
182
+ initV2();
164
183
  v2Handler(msg);
184
+ return;
165
185
  }
186
+ if (debug)
187
+ console.warn('[RPC-AUTO2] Unknown message format, ignoring:', msg);
166
188
  });
167
189
  return {
168
190
  getProtocol: () => protocol,
169
- getSchema: () => strictSchema,
191
+ getLegacySchema: () => legacySchema,
192
+ getResolved: () => resolved,
170
193
  };
171
194
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wenay-common2",
3
- "version": "1.0.33",
3
+ "version": "1.0.34",
4
4
  "description": "Common library",
5
5
  "strict": true,
6
6
  "main": "lib/index.js",