wenay-common2 1.0.34 → 1.0.36

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,15 +3,6 @@ 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;
15
6
  type ClientProtocol = 'v2' | 'legacy' | null;
16
7
  export declare function createRpcServerAuto2<T extends object>({ socket, object: target, socketKey: key, debug, hooks, disconnectListen, limits, onProtocolDetect, }: {
17
8
  socket: SocketTmpl;
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createRpcServerAuto = createRpcServerAuto;
4
3
  exports.createRpcServerAuto2 = createRpcServerAuto2;
5
4
  const Listen_1 = require("../events/Listen");
6
5
  const listen_socket_1 = require("./listen-socket");
7
6
  const rpc_server_1 = require("./rpc-server");
8
7
  const rpc_protocol_1 = require("./rpc-protocol");
9
8
  const old_ommonsServerMini_1 = require("./old\u0421ommonsServerMini");
9
+ const rpc_dynamic_1 = require("./rpc-dynamic");
10
+ const rpc_limits_1 = require("./rpc-limits");
10
11
  function createRpcServerAuto({ socket, object: target, socketKey: key, debug, hooks, disconnectListen, limits }) {
11
12
  const cache = new WeakMap();
12
13
  function getListenSocket(parent, disconnectListen) {
@@ -45,37 +46,48 @@ function createRpcServerAuto2({ socket, object: target, socketKey: key, debug =
45
46
  return getListenSocket(obj);
46
47
  }
47
48
  function transformTree(obj) {
48
- if (obj == null || typeof obj === 'function')
49
- return obj;
50
- if (typeof obj !== 'object')
51
- return obj;
52
- const transformed = resolveTransform(obj);
53
- if (transformed !== obj)
54
- return transformed;
49
+ let current = obj;
50
+ if (!(0, rpc_dynamic_1.isNoStrict)(current)) {
51
+ current = resolveTransform(current);
52
+ }
53
+ if (current == null || typeof current !== 'object' || (0, rpc_dynamic_1.isNoStrict)(current))
54
+ return current;
55
55
  const out = {};
56
- for (const k of Object.keys(obj)) {
57
- const v = obj[k];
58
- if (typeof v === 'function')
59
- out[k] = v;
60
- else if (v != null && typeof v === 'object')
61
- out[k] = transformTree(v);
62
- else
56
+ for (const k of Object.keys(current)) {
57
+ if (!(0, rpc_limits_1.isSafeKey)(k))
58
+ continue;
59
+ const v = current[k];
60
+ if ((0, rpc_dynamic_1.isNoStrict)(v)) {
63
61
  out[k] = v;
62
+ continue;
63
+ }
64
+ out[k] = typeof v === 'function' ? v : (v != null && typeof v === 'object') ? transformTree(v) : v;
64
65
  }
65
66
  return out;
66
67
  }
67
68
  function serialize(obj) {
68
69
  const out = {};
69
70
  for (const k of Object.keys(obj)) {
71
+ if (!(0, rpc_limits_1.isSafeKey)(k))
72
+ continue;
70
73
  const v = obj[k];
71
- if (v == null)
72
- out[k] = 'null';
73
- else if (typeof v === 'function')
74
- out[k] = 'func';
75
- else if (typeof v === 'object')
76
- out[k] = serialize(v);
77
- else
78
- out[k] = 'unknown';
74
+ switch (true) {
75
+ case v == null:
76
+ out[k] = 'null';
77
+ break;
78
+ case (0, rpc_dynamic_1.isNoStrict)(v):
79
+ out[k] = 'dynamic';
80
+ break;
81
+ case typeof v === 'function':
82
+ out[k] = 'func';
83
+ break;
84
+ case typeof v === 'object':
85
+ out[k] = serialize(v);
86
+ break;
87
+ default:
88
+ out[k] = 'unknown';
89
+ break;
90
+ }
79
91
  }
80
92
  return out;
81
93
  }
@@ -14,6 +14,11 @@ function createClient(socket, key, opts) {
14
14
  let strictData = {};
15
15
  let strictWaiters = [];
16
16
  let debug = false;
17
+ const unpackResult = (v) => {
18
+ if (!v)
19
+ return v;
20
+ return v;
21
+ };
17
22
  socket.on(key, (msg) => {
18
23
  if (!Array.isArray(msg))
19
24
  return;
@@ -28,7 +33,7 @@ function createClient(socket, key, opts) {
28
33
  callbacks.delete(cbId);
29
34
  pool.release(cbId);
30
35
  }
31
- msg[3] ? req.fail(msg[3]) : req.ok(msg[2]);
36
+ msg[3] ? req.fail(msg[3]) : req.ok(unpackResult(msg[2]));
32
37
  break;
33
38
  }
34
39
  case rpc_protocol_1.Pkt.CB: {
@@ -23,12 +23,18 @@ function createRpcClientHub(createSocket, schemaBuilder) {
23
23
  const targetSocketKey = schema[key].socketKey || key;
24
24
  const client = (0, rpc_client_1.createRpcClient)({ socketKey: targetSocketKey, socket });
25
25
  facade[key] = client;
26
- if (client && typeof client.initStrict === "function") {
27
- client.initStrict();
26
+ }
27
+ function hi() {
28
+ for (const key in schema) {
29
+ const client = facade[key];
30
+ if (client && typeof client.initStrict === "function") {
31
+ client.initStrict();
32
+ }
28
33
  }
29
34
  }
30
35
  socket?.on("connect", () => {
31
36
  connectCount++;
37
+ hi();
32
38
  onConnectCb?.(connectCount);
33
39
  if (resolveFunc) {
34
40
  const a = resolveFunc;
@@ -186,13 +186,13 @@ function createServer(socket, key, target, hooks, limits) {
186
186
  current = await current;
187
187
  }
188
188
  if (wait)
189
- send([rpc_protocol_1.Pkt.RESP, reqId, current]);
189
+ send([rpc_protocol_1.Pkt.RESP, reqId, (0, rpc_walk_1.packResult)(current)]);
190
190
  }
191
191
  else {
192
192
  const args = (0, rpc_walk_1.unpack)(rawArgsOrSteps, (cbId, cbArgs) => send([rpc_protocol_1.Pkt.CB, cbId, cbArgs]), (cbId) => send([rpc_protocol_1.Pkt.CB_END, cbId]), lim);
193
193
  const res = await fn.apply(ctx, args);
194
194
  if (wait)
195
- send([rpc_protocol_1.Pkt.RESP, reqId, res]);
195
+ send([rpc_protocol_1.Pkt.RESP, reqId, (0, rpc_walk_1.packResult)(res)]);
196
196
  }
197
197
  }
198
198
  catch (e) {
@@ -2,7 +2,9 @@ import { idPool } from "../id-pool";
2
2
  import { type RpcLimits } from "./rpc-limits";
3
3
  export declare function walk(val: any, onLeaf: (v: any) => any, lim?: Required<RpcLimits>, depth?: number): any;
4
4
  export declare function pack(args: any[], pool: idPool, cbStore: Map<number, Function>, cbIds: number[]): any[];
5
+ export declare function packResult(value: any): any;
5
6
  export declare function rpcEndCallback(fn: Function): void;
6
7
  export declare function unpack(args: any[], sender: (id: number, a: any[]) => void, onEnd: (id: number) => void, lim?: Required<RpcLimits>): any[];
8
+ export declare function unpackResult(value: any, lim?: Required<RpcLimits>): any;
7
9
  export declare const errToObj: (e: any) => any;
8
10
  export declare const resolveCA: (path: string[], args: any[]) => [string[], any[]];
@@ -3,10 +3,32 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.resolveCA = exports.errToObj = void 0;
4
4
  exports.walk = walk;
5
5
  exports.pack = pack;
6
+ exports.packResult = packResult;
6
7
  exports.rpcEndCallback = rpcEndCallback;
7
8
  exports.unpack = unpack;
9
+ exports.unpackResult = unpackResult;
8
10
  const rpc_limits_1 = require("./rpc-limits");
9
11
  const FN_MARKER = "$_f";
12
+ const DATE_MARKER = "$_d";
13
+ const MAP_MARKER = "$_m";
14
+ const SET_MARKER = "$_s";
15
+ const REGEXP_MARKER = "$_r";
16
+ const BIGINT_MARKER = "$_b";
17
+ const ALL_MARKERS = new Set([FN_MARKER, DATE_MARKER, MAP_MARKER, SET_MARKER, REGEXP_MARKER, BIGINT_MARKER]);
18
+ const DESERIALIZERS = {
19
+ [DATE_MARKER]: (v) => new Date(v),
20
+ [MAP_MARKER]: (v) => new Map(v),
21
+ [SET_MARKER]: (v) => new Set(v),
22
+ [REGEXP_MARKER]: (v) => new RegExp(v.source, v.flags),
23
+ [BIGINT_MARKER]: (v) => BigInt(v),
24
+ };
25
+ const SERIALIZERS = [
26
+ (v) => v instanceof Date ? [DATE_MARKER, v.valueOf()] : null,
27
+ (v) => v instanceof Map ? [MAP_MARKER, Array.from(v.entries())] : null,
28
+ (v) => v instanceof Set ? [SET_MARKER, Array.from(v.values())] : null,
29
+ (v) => v instanceof RegExp ? [REGEXP_MARKER, { source: v.source, flags: v.flags }] : null,
30
+ (v) => typeof v === "bigint" ? [BIGINT_MARKER, v.toString()] : null,
31
+ ];
10
32
  function walk(val, onLeaf, lim, depth = 0) {
11
33
  if (lim) {
12
34
  if (depth > lim.maxDepth)
@@ -16,7 +38,7 @@ function walk(val, onLeaf, lim, depth = 0) {
16
38
  }
17
39
  if (val == null || typeof val !== "object")
18
40
  return onLeaf(val);
19
- if (val[FN_MARKER] !== undefined)
41
+ if (ALL_MARKERS.has(Object.keys(val)[0]))
20
42
  return onLeaf(val);
21
43
  if (Array.isArray(val)) {
22
44
  if (lim && val.length > lim.maxArrayLen)
@@ -32,6 +54,26 @@ function walk(val, onLeaf, lim, depth = 0) {
32
54
  o[k] = walk(val[k], onLeaf, lim, depth + 1);
33
55
  return o;
34
56
  }
57
+ function deserializeLeaf(leaf, onCallback) {
58
+ if (leaf == null || typeof leaf !== "object")
59
+ return leaf;
60
+ const key = Object.keys(leaf)[0];
61
+ if (!key)
62
+ return leaf;
63
+ if (key === FN_MARKER) {
64
+ return onCallback?.(leaf[FN_MARKER]) ?? leaf;
65
+ }
66
+ const deserialize = DESERIALIZERS[key];
67
+ return deserialize ? deserialize(leaf[key]) : leaf;
68
+ }
69
+ function serializeLeaf(leaf) {
70
+ for (const serializer of SERIALIZERS) {
71
+ const result = serializer(leaf);
72
+ if (result)
73
+ return { [result[0]]: result[1] };
74
+ }
75
+ return leaf;
76
+ }
35
77
  function pack(args, pool, cbStore, cbIds) {
36
78
  return args.map(v => walk(v, leaf => {
37
79
  if (typeof leaf == "function") {
@@ -40,9 +82,12 @@ function pack(args, pool, cbStore, cbIds) {
40
82
  cbIds.push(id);
41
83
  return { [FN_MARKER]: id };
42
84
  }
43
- return leaf;
85
+ return serializeLeaf(leaf);
44
86
  }));
45
87
  }
88
+ function packResult(value) {
89
+ return walk(value, leaf => serializeLeaf(leaf));
90
+ }
46
91
  const _stopRegistry = new WeakMap();
47
92
  function rpcEndCallback(fn) {
48
93
  _stopRegistry.get(fn)?.();
@@ -66,9 +111,12 @@ function unpack(args, sender, onEnd, lim) {
66
111
  _stopRegistry.set(wrapper, () => onEnd(id));
67
112
  return wrapper;
68
113
  }
69
- return leaf;
114
+ return deserializeLeaf(leaf);
70
115
  }, lim));
71
116
  }
117
+ function unpackResult(value, lim) {
118
+ return walk(value, leaf => deserializeLeaf(leaf), lim);
119
+ }
72
120
  const errToObj = (e) => e instanceof Error ? { name: e.name, message: e.message, stack: e.stack } : e;
73
121
  exports.errToObj = errToObj;
74
122
  const resolveCA = (path, args) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wenay-common2",
3
- "version": "1.0.34",
3
+ "version": "1.0.36",
4
4
  "description": "Common library",
5
5
  "strict": true,
6
6
  "main": "lib/index.js",