proto.io 0.0.153 → 0.0.155

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.
Files changed (38) hide show
  1. package/dist/adapters/file/database.d.ts +2 -2
  2. package/dist/adapters/file/filesystem.d.ts +2 -2
  3. package/dist/adapters/file/google-cloud-storage.d.ts +2 -2
  4. package/dist/adapters/storage/progres.d.ts +11 -7
  5. package/dist/adapters/storage/progres.js +55 -14
  6. package/dist/adapters/storage/progres.js.map +1 -1
  7. package/dist/adapters/storage/progres.mjs +55 -14
  8. package/dist/adapters/storage/progres.mjs.map +1 -1
  9. package/dist/client.d.ts +3 -3
  10. package/dist/client.js +1 -1
  11. package/dist/client.mjs +2 -2
  12. package/dist/index.d.ts +3 -3
  13. package/dist/index.js +64 -31
  14. package/dist/index.js.map +1 -1
  15. package/dist/index.mjs +65 -32
  16. package/dist/index.mjs.map +1 -1
  17. package/dist/internals/{index-c-CaNuVY.mjs → index-B01TqoO1.mjs} +19 -4
  18. package/dist/internals/index-B01TqoO1.mjs.map +1 -0
  19. package/dist/internals/{index-m4WmZoWj.d.ts → index-BLVOKfv3.d.ts} +17 -6
  20. package/dist/internals/index-BLVOKfv3.d.ts.map +1 -0
  21. package/dist/internals/{index-D1YGwuNK.js → index-Bs06MNCK.js} +19 -4
  22. package/dist/internals/index-Bs06MNCK.js.map +1 -0
  23. package/dist/internals/{index-DeD3CiUK.d.ts → index-DSU589g6.d.ts} +2 -2
  24. package/dist/internals/index-DSU589g6.d.ts.map +1 -0
  25. package/dist/internals/{index-DQm5Xz3Q.d.ts → index-DmcvF1dx.d.ts} +3 -2
  26. package/dist/internals/index-DmcvF1dx.d.ts.map +1 -0
  27. package/dist/internals/{random-KZTFMK9-.mjs → random-DXre8Q46.mjs} +11 -1
  28. package/dist/internals/random-DXre8Q46.mjs.map +1 -0
  29. package/dist/internals/{random-DBDmz54D.js → random-DfnhGdY1.js} +11 -1
  30. package/dist/internals/random-DfnhGdY1.js.map +1 -0
  31. package/package.json +1 -1
  32. package/dist/internals/index-D1YGwuNK.js.map +0 -1
  33. package/dist/internals/index-DQm5Xz3Q.d.ts.map +0 -1
  34. package/dist/internals/index-DeD3CiUK.d.ts.map +0 -1
  35. package/dist/internals/index-c-CaNuVY.mjs.map +0 -1
  36. package/dist/internals/index-m4WmZoWj.d.ts.map +0 -1
  37. package/dist/internals/random-DBDmz54D.js.map +0 -1
  38. package/dist/internals/random-KZTFMK9-.mjs.map +0 -1
package/dist/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { P as ProtoClient } from './internals/index-DQm5Xz3Q.js';
2
- export { c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-DQm5Xz3Q.js';
1
+ import { P as ProtoClient } from './internals/index-DmcvF1dx.js';
2
+ export { c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-DmcvF1dx.js';
3
3
  export { Decimal } from 'decimal.js';
4
- export { D as DeserializeOptions, S as SerializeOptions, d as TSerializable, e as deserialize, s as serialize } from './internals/index-m4WmZoWj.js';
4
+ export { D as DeserializeOptions, S as SerializeOptions, d as TSerializable, e as deserialize, s as serialize } from './internals/index-BLVOKfv3.js';
5
5
  import '@o2ter/utils-js';
6
6
  import 'socket.io-client';
7
7
  import '@socket.io/component-emitter';
package/dist/client.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var index = require('./internals/index-D1YGwuNK.js');
5
+ var index = require('./internals/index-Bs06MNCK.js');
6
6
  var Decimal = require('decimal.js');
7
7
  require('./internals/index-CVutVPmd.js');
8
8
  require('lodash');
package/dist/client.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { c as ProtoClient } from './internals/index-c-CaNuVY.mjs';
2
- export { e as classExtends, d as deserialize, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser, s as serialize } from './internals/index-c-CaNuVY.mjs';
1
+ import { c as ProtoClient } from './internals/index-B01TqoO1.mjs';
2
+ export { e as classExtends, d as deserialize, j as isFile, f as isObject, i as isQuery, h as isRole, g as isUser, s as serialize } from './internals/index-B01TqoO1.mjs';
3
3
  export { Decimal } from 'decimal.js';
4
4
  import './internals/index-BmRuvHVZ.mjs';
5
5
  import 'lodash';
package/dist/index.d.ts CHANGED
@@ -2,11 +2,11 @@ import * as socket_io from 'socket.io';
2
2
  import * as socket_io_dist_typed_events from 'socket.io/dist/typed-events';
3
3
  import * as express_serve_static_core from 'express-serve-static-core';
4
4
  import { Server, RequestHandler } from '@o2ter/server-js';
5
- import { T as TSchema, _ as _TValue, P as ProtoService, a as ProtoServiceOptions, b as ProtoServiceKeyOptions } from './internals/index-m4WmZoWj.js';
6
- export { D as DeserializeOptions, S as SerializeOptions, c as TFileStorage, d as TSerializable, e as deserialize, s as serialize } from './internals/index-m4WmZoWj.js';
5
+ import { T as TSchema, _ as _TValue, P as ProtoService, a as ProtoServiceOptions, b as ProtoServiceKeyOptions } from './internals/index-BLVOKfv3.js';
6
+ export { D as DeserializeOptions, S as SerializeOptions, c as TFileStorage, d as TSerializable, e as deserialize, s as serialize } from './internals/index-BLVOKfv3.js';
7
7
  import Decimal from 'decimal.js';
8
8
  export { Decimal } from 'decimal.js';
9
- export { P as ProtoClient, c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-DQm5Xz3Q.js';
9
+ export { P as ProtoClient, c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-DmcvF1dx.js';
10
10
  import '@o2ter/utils-js';
11
11
  import 'jsonwebtoken';
12
12
  import 'lodash';
package/dist/index.js CHANGED
@@ -4,10 +4,10 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _ = require('lodash');
6
6
  var serverJs = require('@o2ter/server-js');
7
- var random = require('./internals/random-DBDmz54D.js');
7
+ var random = require('./internals/random-DfnhGdY1.js');
8
8
  var _private = require('./internals/private-CSB1Ep4g.js');
9
9
  var utilsJs = require('@o2ter/utils-js');
10
- var index = require('./internals/index-D1YGwuNK.js');
10
+ var index = require('./internals/index-Bs06MNCK.js');
11
11
  var index$1 = require('./internals/index-CVutVPmd.js');
12
12
  var jwt = require('jsonwebtoken');
13
13
  var node_buffer = require('node:buffer');
@@ -295,22 +295,25 @@ const proxy = (x) => {
295
295
  //
296
296
  class ProtoQuery extends index.TQuery {
297
297
  _proto;
298
- constructor(className, proto) {
298
+ _opts;
299
+ constructor(className, proto, opts) {
299
300
  super(className);
300
301
  this._proto = proto;
302
+ this._opts = opts;
301
303
  }
302
304
  get _queryOptions() {
303
305
  return {
304
306
  className: this.className,
307
+ relatedBy: this._opts.relatedBy,
305
308
  ...this[_private.PVK].options,
306
309
  };
307
310
  }
308
311
  _dispatcher(options) {
309
- if (this instanceof InsecureProtoQuery) {
312
+ if (this._opts.insecure) {
310
313
  if (options?.master !== true)
311
314
  throw Error('No permission');
312
315
  }
313
- return dispatcher(options?.session ?? this._proto, options ?? {}, this instanceof InsecureProtoQuery);
316
+ return dispatcher(options?.session ?? this._proto, options ?? {}, !!this._opts.insecure);
314
317
  }
315
318
  explain(options) {
316
319
  return this._dispatcher(options).explain(this._queryOptions);
@@ -319,7 +322,7 @@ class ProtoQuery extends index.TQuery {
319
322
  return this._dispatcher(options).count(this._queryOptions);
320
323
  }
321
324
  clone(options) {
322
- const clone = new ProtoQuery(this.className, this._proto);
325
+ const clone = new ProtoQuery(this.className, this._proto, this._opts);
323
326
  clone[_private.PVK].options = options ?? { ...this[_private.PVK].options };
324
327
  return clone;
325
328
  }
@@ -512,13 +515,6 @@ class ProtoQuery extends index.TQuery {
512
515
  return this._dispatcher(options).deleteMany(this._queryOptions);
513
516
  }
514
517
  }
515
- class InsecureProtoQuery extends ProtoQuery {
516
- clone(options) {
517
- const clone = new InsecureProtoQuery(this.className, this._proto);
518
- clone[_private.PVK].options = options ?? { ...this[_private.PVK].options };
519
- return clone;
520
- }
521
- }
522
518
 
523
519
  //
524
520
  // defaults.ts
@@ -1109,7 +1105,12 @@ const _sessionWithToken = (proto, token) => {
1109
1105
  const payload = proto[_private.PVK].jwtVarify(token, 'login');
1110
1106
  if (!_.isObject(payload))
1111
1107
  return;
1112
- return payload;
1108
+ return {
1109
+ ...payload,
1110
+ sessionId: payload.sessionId ?? cryptoJs.randomUUID(),
1111
+ createdAt: payload.createdAt && _.isSafeInteger(payload.createdAt) ? new Date(payload.createdAt) : new Date,
1112
+ loginedAt: payload.loginedAt && _.isSafeInteger(payload.loginedAt) ? new Date(payload.loginedAt) : new Date,
1113
+ };
1113
1114
  };
1114
1115
  const _session = (proto, request) => {
1115
1116
  const cached = sessionMap.get(request);
@@ -1118,6 +1119,7 @@ const _session = (proto, request) => {
1118
1119
  sessionMap.set(request, {
1119
1120
  sessionId: cryptoJs.randomUUID(),
1120
1121
  createdAt: new Date,
1122
+ loginedAt: new Date,
1121
1123
  });
1122
1124
  const jwtToken = proto[_private.PVK].options.jwtToken;
1123
1125
  if (_.isEmpty(jwtToken))
@@ -1133,14 +1135,9 @@ const _session = (proto, request) => {
1133
1135
  }
1134
1136
  if (_.isEmpty(authorization))
1135
1137
  return;
1136
- const payload = proto[_private.PVK].jwtVarify(authorization, 'login');
1137
- if (!_.isObject(payload))
1138
+ const session = _sessionWithToken(proto, authorization);
1139
+ if (!_.isObject(session))
1138
1140
  return;
1139
- const session = {
1140
- ...payload,
1141
- sessionId: payload.sessionId ?? cryptoJs.randomUUID(),
1142
- createdAt: payload.createdAt && _.isSafeInteger(payload.createdAt) ? new Date(payload.createdAt) : new Date,
1143
- };
1144
1141
  sessionMap.set(request, session);
1145
1142
  return session;
1146
1143
  };
@@ -1159,18 +1156,29 @@ const fetchSessionInfo = async (proto, userId) => {
1159
1156
  };
1160
1157
  const sessionWithToken = async (proto, token) => {
1161
1158
  const session = _sessionWithToken(proto, token);
1162
- const sessionId = session?.sessionId;
1163
1159
  const info = await fetchSessionInfo(proto, session?.user);
1164
- return { sessionId, ...info };
1160
+ return {
1161
+ ...session ?? {},
1162
+ ...info,
1163
+ loginedAt: info?.user ? session?.loginedAt : undefined,
1164
+ };
1165
1165
  };
1166
1166
  const session = async (proto, request) => {
1167
1167
  const session = _session(proto, request);
1168
1168
  const cached = sessionInfoMap.get(request);
1169
1169
  if (cached)
1170
- return { ...session ?? {}, ...cached };
1170
+ return {
1171
+ ...session ?? {},
1172
+ ...cached,
1173
+ loginedAt: cached?.user ? session?.loginedAt : undefined,
1174
+ };
1171
1175
  const info = await fetchSessionInfo(proto, session?.user);
1172
1176
  sessionInfoMap.set(request, info);
1173
- return { ...session ?? {}, ...info };
1177
+ return {
1178
+ ...session ?? {},
1179
+ ...info,
1180
+ loginedAt: info?.user ? session?.loginedAt : undefined,
1181
+ };
1174
1182
  };
1175
1183
  const sessionIsMaster = (proto, request) => {
1176
1184
  const user = request.header(index.MASTER_USER_HEADER_NAME);
@@ -1187,6 +1195,7 @@ const signUser = async (proto, res, user, options) => {
1187
1195
  const token = proto[_private.PVK].jwtSign({
1188
1196
  sessionId: session?.sessionId ?? cryptoJs.randomUUID(),
1189
1197
  createdAt: session?.createdAt?.getTime() ?? Date.now(),
1198
+ loginedAt: user ? session?.loginedAt?.getTime() ?? Date.now() : undefined,
1190
1199
  user: user?.objectId,
1191
1200
  cookieOptions,
1192
1201
  }, options?.jwtSignOptions ?? 'login');
@@ -1335,10 +1344,22 @@ class ProtoService extends index.ProtoType {
1335
1344
  return _.keys(this[_private.PVK].options.schema);
1336
1345
  }
1337
1346
  Query(className) {
1338
- return new ProtoQuery(className, this);
1347
+ return new ProtoQuery(className, this, {});
1348
+ }
1349
+ Relation(className, object, key) {
1350
+ const objectId = object.objectId;
1351
+ if (!objectId)
1352
+ throw Error('Invalid object');
1353
+ return new ProtoQuery(className, this, {
1354
+ relatedBy: {
1355
+ className: object.className,
1356
+ objectId,
1357
+ key,
1358
+ },
1359
+ });
1339
1360
  }
1340
1361
  InsecureQuery(className) {
1341
- return new InsecureProtoQuery(className, this);
1362
+ return new ProtoQuery(className, this, { insecure: true });
1342
1363
  }
1343
1364
  get sessionId() {
1344
1365
  if (this.session)
@@ -1603,6 +1624,16 @@ const decodeFormStream = (req, onFile) => {
1603
1624
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1604
1625
  // THE SOFTWARE.
1605
1626
  //
1627
+ const verifyRelatedBy = (relatedBy) => {
1628
+ if (!_.isPlainObject(relatedBy))
1629
+ return;
1630
+ if (!_.isString(relatedBy.className) || _.isEmpty(relatedBy.className))
1631
+ throw Error('Invalid option');
1632
+ if (!_.isString(relatedBy.objectId) || _.isEmpty(relatedBy.objectId))
1633
+ throw Error('Invalid option');
1634
+ if (!_.isString(relatedBy.key) || _.isEmpty(relatedBy.key))
1635
+ throw Error('Invalid option');
1636
+ };
1606
1637
  var classesRoute = (router, proto) => {
1607
1638
  router.post('/classes/:name', serverJs.Server.text({ type: '*/*' }), async (req, res) => {
1608
1639
  res.setHeader('Cache-Control', ['no-cache', 'no-store']);
@@ -1611,9 +1642,10 @@ var classesRoute = (router, proto) => {
1611
1642
  if (!_.includes(classes, name))
1612
1643
  return res.sendStatus(404);
1613
1644
  await response(res, async () => {
1614
- const { operation, context, silent, random, attributes, update, setOnInsert, ...options } = index.deserialize(req.body);
1645
+ const { operation, context, silent, random, attributes, update, setOnInsert, relatedBy, ...options } = index.deserialize(req.body);
1646
+ verifyRelatedBy(relatedBy);
1615
1647
  const payload = proto.connect(req);
1616
- const query = payload.Query(name);
1648
+ const query = relatedBy ? payload.Relation(name, payload.Object(relatedBy.className, relatedBy.objectId), relatedBy.key) : payload.Query(name);
1617
1649
  query[_private.PVK].options = options;
1618
1650
  const opts = { master: payload.isMaster, context, silent };
1619
1651
  switch (operation) {
@@ -1658,8 +1690,9 @@ var classesRoute = (router, proto) => {
1658
1690
  });
1659
1691
  const createQuery = (payload, req, checkLimit) => {
1660
1692
  const { name } = req.params;
1661
- const query = payload.Query(name);
1662
- const { filter, sort, includes, skip, limit, } = req.query;
1693
+ const { filter, sort, includes, skip, limit, relatedBy, } = req.query;
1694
+ verifyRelatedBy(relatedBy);
1695
+ const query = relatedBy ? payload.Relation(name, payload.Object(relatedBy.className, relatedBy.objectId), relatedBy.key) : payload.Query(name);
1663
1696
  query[_private.PVK].options.filter = !_.isEmpty(filter) && _.isString(filter) ? _.castArray(index.deserialize(filter)) : [];
1664
1697
  query[_private.PVK].options.sort = _.isPlainObject(sort) && _.every(_.values(sort), _.isNumber) ? sort : undefined;
1665
1698
  query[_private.PVK].options.includes = _.isArray(includes) && _.every(includes, _.isString) ? includes : undefined;