proto.io 0.0.139 → 0.0.141

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
@@ -3,8 +3,8 @@ import { Server } from '@o2ter/server-js';
3
3
  import { Q as QueryValidator, g as generateId } from './internals/random-Dn9rhwfh.mjs';
4
4
  import { P as PVK } from './internals/private-BUpLAMZi.mjs';
5
5
  import { asyncStream, asyncIterableToArray, isBinaryData, base64ToBuffer } from '@o2ter/utils-js';
6
- import { T as TQuery, M as MASTER_USER_HEADER_NAME, a as MASTER_PASS_HEADER_NAME, A as AUTH_COOKIE_KEY, b as TUser, P as ProtoType, s as serialize, d as deserialize, U as UPLOAD_TOKEN_HEADER_NAME } from './internals/index-C8hhBfuh.mjs';
7
- export { c as ProtoClient, e as classExtends, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser } from './internals/index-C8hhBfuh.mjs';
6
+ import { T as TQuery, M as MASTER_USER_HEADER_NAME, a as MASTER_PASS_HEADER_NAME, A as AUTH_COOKIE_KEY, b as TUser, P as ProtoType, s as serialize, d as deserialize, U as UPLOAD_TOKEN_HEADER_NAME } from './internals/index-ZIegYNKT.mjs';
7
+ export { c as ProtoClient, e as classExtends, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser } from './internals/index-ZIegYNKT.mjs';
8
8
  import { d as decodeUpdateOp, T as TObject, i as isShape, a as isPointer, b as isRelation, c as defaultObjectKeyTypes, e as isPrimitive } from './internals/index-EOtjV6U_.mjs';
9
9
  import jwt from 'jsonwebtoken';
10
10
  import { Blob } from 'node:buffer';
@@ -692,15 +692,30 @@ const validateShapedObject = (schema, dataType) => {
692
692
  }
693
693
  }
694
694
  };
695
- const validateSchema = (schema) => {
695
+ const validateSchemaPermission = (schema) => {
696
696
  if (!_.isNil(schema['_Schema']) || !_.isNil(schema['_Config']))
697
697
  throw Error('Reserved name of class');
698
+ for (const [, _schema] of _.toPairs(schema)) {
699
+ for (const [key] of _.toPairs(_schema.fields)) {
700
+ if (_.includes(TObject.defaultKeys, key))
701
+ throw Error(`Reserved field name: ${key}`);
702
+ }
703
+ const fields = _.keys(_schema.fields);
704
+ for (const key of _.keys(_schema.fieldLevelPermissions)) {
705
+ if (!fields.includes(key))
706
+ throw Error(`Invalid field permission: ${key}`);
707
+ }
708
+ for (const key of _schema.secureFields ?? []) {
709
+ if (!fields.includes(key))
710
+ throw Error(`Invalid field permission: ${key}`);
711
+ }
712
+ }
713
+ };
714
+ const validateSchema = (schema) => {
698
715
  for (const [className, _schema] of _.toPairs(schema)) {
699
716
  if (!className.match(QueryValidator.patterns.name))
700
717
  throw Error(`Invalid class name: ${className}`);
701
718
  for (const [key, dataType] of _.toPairs(_schema.fields)) {
702
- if (_.includes(TObject.defaultKeys, key))
703
- throw Error(`Reserved field name: ${key}`);
704
719
  if (!key.match(QueryValidator.patterns.name))
705
720
  throw Error(`Invalid field name: ${key}`);
706
721
  if (isShape(dataType)) {
@@ -716,15 +731,6 @@ const validateSchema = (schema) => {
716
731
  validateForeignField(schema, key, dataType);
717
732
  }
718
733
  }
719
- const fields = _.keys(_schema.fields);
720
- for (const key of _.keys(_schema.fieldLevelPermissions)) {
721
- if (!fields.includes(key))
722
- throw Error(`Invalid field permission: ${key}`);
723
- }
724
- for (const key of _schema.secureFields ?? []) {
725
- if (!fields.includes(key))
726
- throw Error(`Invalid field permission: ${key}`);
727
- }
728
734
  }
729
735
  };
730
736
  const mergeSchema = (...schemas) => _.reduce(schemas, (acc, schema) => ({
@@ -756,10 +762,12 @@ class ProtoInternal {
756
762
  functions = {};
757
763
  triggers = {};
758
764
  constructor(options) {
759
- validateSchema(options.schema);
765
+ validateSchemaPermission(options.schema);
766
+ const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
767
+ validateSchema(schema);
760
768
  this.options = {
761
769
  ...options,
762
- schema: mergeSchema(defaultSchema, options.fileStorage.schema, options.schema),
770
+ schema,
763
771
  };
764
772
  }
765
773
  async prepare() {
@@ -1080,7 +1088,7 @@ const _sessionWithToken = (proto, token) => {
1080
1088
  const _session = (proto, request) => {
1081
1089
  const cached = sessionMap.get(request);
1082
1090
  if (cached)
1083
- return cached?.payload;
1091
+ return cached;
1084
1092
  sessionMap.set(request, { sessionId: randomUUID() });
1085
1093
  const jwtToken = proto[PVK].options.jwtToken;
1086
1094
  if (_.isEmpty(jwtToken))
@@ -1099,11 +1107,12 @@ const _session = (proto, request) => {
1099
1107
  const payload = proto[PVK].jwtVarify(authorization, 'login');
1100
1108
  if (!_.isObject(payload))
1101
1109
  return;
1102
- sessionMap.set(request, {
1110
+ const session = {
1103
1111
  sessionId: payload.sessionId ?? randomUUID(),
1104
- payload,
1105
- });
1106
- return payload;
1112
+ ...payload,
1113
+ };
1114
+ sessionMap.set(request, session);
1115
+ return session;
1107
1116
  };
1108
1117
  const sessionId = (proto, request) => {
1109
1118
  const session = _session(proto, request);
@@ -1126,13 +1135,12 @@ const sessionWithToken = async (proto, token) => {
1126
1135
  };
1127
1136
  const session = async (proto, request) => {
1128
1137
  const session = _session(proto, request);
1129
- const sessionId = sessionMap.get(request)?.sessionId ?? session?.sessionId;
1130
1138
  const cached = sessionInfoMap.get(request);
1131
1139
  if (cached)
1132
- return { sessionId, ...cached };
1140
+ return { ...session ?? {}, ...cached };
1133
1141
  const info = await fetchSessionInfo(proto, session?.user);
1134
1142
  sessionInfoMap.set(request, info);
1135
- return { sessionId, ...info };
1143
+ return { ...session ?? {}, ...info };
1136
1144
  };
1137
1145
  const sessionIsMaster = (proto, request) => {
1138
1146
  const user = request.header(MASTER_USER_HEADER_NAME);
@@ -1302,17 +1310,18 @@ class ProtoService extends ProtoType {
1302
1310
  return this.session.sessionId;
1303
1311
  return this.req ? sessionId(this, this.req) : undefined;
1304
1312
  }
1305
- async currentUser() {
1313
+ async sessionInfo() {
1306
1314
  if (this.session)
1307
- return this.session.user;
1308
- const _session = this.req ? await session(this, this.req) : undefined;
1309
- return _session?.user;
1315
+ return this.session;
1316
+ return this.req ? session(this, this.req) : undefined;
1317
+ }
1318
+ async currentUser() {
1319
+ const session = await this.sessionInfo();
1320
+ return session?.user;
1310
1321
  }
1311
1322
  async currentRoles() {
1312
- if (this.session)
1313
- return this.session.roles ?? [];
1314
- const _session = this.req ? await session(this, this.req) : undefined;
1315
- return _session?.roles ?? [];
1323
+ const session = await this.sessionInfo();
1324
+ return session?.roles ?? [];
1316
1325
  }
1317
1326
  get isMaster() {
1318
1327
  return this.req ? sessionIsMaster(this, this.req) === 'valid' : false;
@@ -2095,6 +2104,9 @@ const ProtoRoute = async (options) => {
2095
2104
  res.status(400).json({ message: 'Invalid token' });
2096
2105
  });
2097
2106
  router.get('/health', (req, res) => { res.sendStatus(200); });
2107
+ router.get('/sessionInfo', async (req, res) => {
2108
+ await response(res, () => proto.connect(req).sessionInfo());
2109
+ });
2098
2110
  classesRoute(router, proto);
2099
2111
  functionRoute(router, proto);
2100
2112
  filesRoute(router, proto);