proto.io 0.0.160 → 0.0.161

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/dist/index.mjs CHANGED
@@ -1168,13 +1168,24 @@ const sessionId = (proto, request) => {
1168
1168
  const session = _session(proto, request);
1169
1169
  return sessionMap.get(request)?.sessionId ?? session?.sessionId;
1170
1170
  };
1171
- const sessionInfoMap = new WeakMap();
1171
+ const userCacheMap = new WeakMap;
1172
1172
  const fetchSessionInfo = async (proto, userId) => {
1173
- const user = _.isString(userId) ? await proto.Query('User').get(userId, { master: true }) : undefined;
1174
- const roles = user instanceof TUser ? _.filter(await proto.userRoles(user), x => !_.isEmpty(x.name)) : [];
1173
+ if (!userId)
1174
+ return {};
1175
+ if (!userCacheMap.has(proto[PVK]))
1176
+ userCacheMap.set(proto[PVK], {});
1177
+ const cache = userCacheMap.get(proto[PVK]);
1178
+ if (_.isNil(cache[userId]))
1179
+ cache[userId] = (async () => {
1180
+ const user = _.isString(userId) ? await proto.Query('User').get(userId, { master: true }) : undefined;
1181
+ const _roles = user instanceof TUser ? _.filter(await proto.userRoles(user), x => !_.isEmpty(x.name)) : [];
1182
+ cache[userId] = undefined;
1183
+ return { user, _roles };
1184
+ })();
1185
+ const { user, _roles } = await cache[userId];
1175
1186
  return {
1176
- _roles: roles,
1177
- user: user instanceof TUser ? user : undefined,
1187
+ user: user?.clone(),
1188
+ _roles: _.map(_roles, x => x.clone()),
1178
1189
  };
1179
1190
  };
1180
1191
  const sessionWithToken = async (proto, token) => {
@@ -1186,6 +1197,7 @@ const sessionWithToken = async (proto, token) => {
1186
1197
  loginedAt: info?.user ? session?.loginedAt : undefined,
1187
1198
  };
1188
1199
  };
1200
+ const sessionInfoMap = new WeakMap();
1189
1201
  const session = async (proto, request) => {
1190
1202
  const session = _session(proto, request);
1191
1203
  const cached = sessionInfoMap.get(request);