proto.io 0.0.165 → 0.0.167

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 (31) 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 +1 -1
  5. package/dist/adapters/storage/progres.mjs +1 -1
  6. package/dist/client.d.ts +3 -3
  7. package/dist/client.js +1 -1
  8. package/dist/client.mjs +2 -2
  9. package/dist/index.d.ts +3 -3
  10. package/dist/index.js +62 -33
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +65 -36
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/internals/{index-bCACA0cS.d.ts → index-BQggoDNX.d.ts} +2 -2
  15. package/dist/internals/index-BQggoDNX.d.ts.map +1 -0
  16. package/dist/internals/{index-Dz3jvqxZ.js → index-CyzpkgJB.js} +8 -21
  17. package/dist/internals/index-CyzpkgJB.js.map +1 -0
  18. package/dist/internals/{index-BibByOcU.mjs → index-Dyfia5Om.mjs} +9 -22
  19. package/dist/internals/index-Dyfia5Om.mjs.map +1 -0
  20. package/dist/internals/{index-DaDfXlay.d.ts → index-rkqvel7o.d.ts} +2 -2
  21. package/dist/internals/index-rkqvel7o.d.ts.map +1 -0
  22. package/dist/internals/{index-RPh4TX0T.d.ts → index-y8EePsDY.d.ts} +8 -5
  23. package/dist/internals/index-y8EePsDY.d.ts.map +1 -0
  24. package/dist/internals/{random-B1P0EZO5.mjs → random-DrURPPxr.mjs} +2 -2
  25. package/dist/internals/{random-B1P0EZO5.mjs.map → random-DrURPPxr.mjs.map} +1 -1
  26. package/package.json +3 -3
  27. package/dist/internals/index-BibByOcU.mjs.map +0 -1
  28. package/dist/internals/index-DaDfXlay.d.ts.map +0 -1
  29. package/dist/internals/index-Dz3jvqxZ.js.map +0 -1
  30. package/dist/internals/index-RPh4TX0T.d.ts.map +0 -1
  31. package/dist/internals/index-bCACA0cS.d.ts.map +0 -1
@@ -1,5 +1,5 @@
1
- import { T as TSchema, P as ProtoService } from '../../internals/index-RPh4TX0T.js';
2
- import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-DaDfXlay.js';
1
+ import { T as TSchema, P as ProtoService } from '../../internals/index-y8EePsDY.js';
2
+ import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-rkqvel7o.js';
3
3
  import '@o2ter/utils-js';
4
4
  import 'jsonwebtoken';
5
5
  import '@o2ter/server-js';
@@ -1,5 +1,5 @@
1
- import { P as ProtoService } from '../../internals/index-RPh4TX0T.js';
2
- import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-DaDfXlay.js';
1
+ import { P as ProtoService } from '../../internals/index-y8EePsDY.js';
2
+ import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-rkqvel7o.js';
3
3
  import '@o2ter/utils-js';
4
4
  import 'jsonwebtoken';
5
5
  import '@o2ter/server-js';
@@ -1,7 +1,7 @@
1
1
  import * as _google_cloud_storage from '@google-cloud/storage';
2
2
  import { Storage } from '@google-cloud/storage';
3
- import { P as ProtoService } from '../../internals/index-RPh4TX0T.js';
4
- import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-DaDfXlay.js';
3
+ import { P as ProtoService } from '../../internals/index-y8EePsDY.js';
4
+ import { F as FileStorageBase, a as FileStorageOptions } from '../../internals/index-rkqvel7o.js';
5
5
  import '@o2ter/utils-js';
6
6
  import 'jsonwebtoken';
7
7
  import '@o2ter/server-js';
@@ -1,5 +1,5 @@
1
1
  import { Pool, PoolClient, PoolConfig } from 'pg';
2
- import { _ as _TValue, o as EventData, T as TSchema, Q as QuerySelector, p as DecodedQuery, F as FindOptions, R as RelationOptions, q as DecodedSortOption, I as InsertOptions, r as TValue, t as FindOneOptions, u as TUpdateOp, v as FieldSelectorExpression, w as QueryExpression, x as TStorage, y as TransactionOptions, h as TObject, z as TQueryRandomOptions, A as TPubSub } from '../../internals/index-RPh4TX0T.js';
2
+ import { _ as _TValue, o as EventData, T as TSchema, Q as QuerySelector, p as DecodedQuery, F as FindOptions, R as RelationOptions, q as DecodedSortOption, I as InsertOptions, r as TValue, t as FindOneOptions, u as TUpdateOp, v as FieldSelectorExpression, w as QueryExpression, x as TStorage, y as TransactionOptions, h as TObject, z as TQueryRandomOptions, A as TPubSub } from '../../internals/index-y8EePsDY.js';
3
3
  import * as _o2ter_utils_js from '@o2ter/utils-js';
4
4
  import { asyncStream } from '@o2ter/utils-js';
5
5
  import 'jsonwebtoken';
@@ -7,7 +7,7 @@ import Decimal from 'decimal.js';
7
7
  import { escapeLiteral, escapeIdentifier } from 'pg/lib/utils';
8
8
  import { a as QueryCoditionalSelector, b as QueryFieldSelector, c as QueryExpressionSelector, d as QueryDistanceExpression, e as QueryCoditionalExpression, f as QueryComparisonExpression, g as QueryNotExpression, h as QueryArrayExpression, i as QueryValueExpression, j as QueryKeyExpression, Q as QuerySelector, F as FieldSelectorExpression } from '../../internals/index-BejQNqvC.mjs';
9
9
  import '@o2ter/crypto-js';
10
- import { r as resolveColumn, a as resolveDataType, g as generateId, Q as QueryValidator } from '../../internals/random-B1P0EZO5.mjs';
10
+ import { r as resolveColumn, a as resolveDataType, g as generateId, Q as QueryValidator } from '../../internals/random-DrURPPxr.mjs';
11
11
  import { P as PVK } from '../../internals/private-BUpLAMZi.mjs';
12
12
 
13
13
  //
package/dist/client.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { P as ProtoClient } from './internals/index-bCACA0cS.js';
2
- export { c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-bCACA0cS.js';
1
+ import { P as ProtoClient } from './internals/index-BQggoDNX.js';
2
+ export { c as classExtends, e as isFile, a as isObject, i as isQuery, d as isRole, b as isUser } from './internals/index-BQggoDNX.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-RPh4TX0T.js';
4
+ export { D as DeserializeOptions, S as SerializeOptions, d as TSerializable, e as deserialize, s as serialize } from './internals/index-y8EePsDY.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-Dz3jvqxZ.js');
5
+ var index = require('./internals/index-CyzpkgJB.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-BibByOcU.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-BibByOcU.mjs';
1
+ import { c as ProtoClient } from './internals/index-Dyfia5Om.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-Dyfia5Om.mjs';
3
3
  export { Decimal } from 'decimal.js';
4
4
  import './internals/index-BYbMU-Ao.mjs';
5
5
  import 'lodash';
package/dist/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import * as socket_io from 'socket.io';
2
2
  import { Router } from 'express';
3
3
  import { Server } from '@o2ter/server-js';
4
- import { T as TSchema, _ as _TValue, P as ProtoService, a as ProtoServiceOptions, b as ProtoServiceKeyOptions } from './internals/index-RPh4TX0T.js';
5
- export { D as DeserializeOptions, S as SerializeOptions, c as TFileStorage, d as TSerializable, e as deserialize, s as serialize } from './internals/index-RPh4TX0T.js';
4
+ import { T as TSchema, _ as _TValue, P as ProtoService, a as ProtoServiceOptions, b as ProtoServiceKeyOptions } from './internals/index-y8EePsDY.js';
5
+ export { D as DeserializeOptions, S as SerializeOptions, c as TFileStorage, d as TSerializable, e as deserialize, s as serialize } from './internals/index-y8EePsDY.js';
6
6
  import Decimal from 'decimal.js';
7
7
  export { Decimal } from 'decimal.js';
8
- 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-bCACA0cS.js';
8
+ 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-BQggoDNX.js';
9
9
  import '@o2ter/utils-js';
10
10
  import 'jsonwebtoken';
11
11
  import 'lodash';
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ var serverJs = require('@o2ter/server-js');
7
7
  var random = require('./internals/random-q0PeamQE.js');
8
8
  var _private = require('./internals/private-CSB1Ep4g.js');
9
9
  var utilsJs = require('@o2ter/utils-js');
10
- var index = require('./internals/index-Dz3jvqxZ.js');
10
+ var index = require('./internals/index-CyzpkgJB.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');
@@ -249,14 +249,7 @@ const dispatcher = (proto, options, disableSecurity) => {
249
249
  const proxy = (x) => {
250
250
  const self = x;
251
251
  const proxy = _.create(self);
252
- const _prototypes = (x) => {
253
- const prototype = Object.getPrototypeOf(x);
254
- if (_.isNil(prototype) || prototype === Object.prototype)
255
- return [];
256
- return [prototype, ..._prototypes(prototype)];
257
- };
258
- const prototypes = _prototypes(proxy);
259
- for (const name of _.uniq(_.flatMap(prototypes, x => Object.getOwnPropertyNames(x)))) {
252
+ for (const name of _.uniq(_.flatMap(utilsJs.prototypes(proxy), x => Object.getOwnPropertyNames(x)))) {
260
253
  if (name === 'constructor')
261
254
  continue;
262
255
  if (_.isFunction(self[name])) {
@@ -858,6 +851,12 @@ class ProtoInternal {
858
851
  validateSchemaName(options.schema);
859
852
  const schema = mergeSchema(defaultSchema, options.fileStorage.schema, options.schema);
860
853
  validateSchema(schema);
854
+ if (!_.every(options.roleInheritKeys, k => {
855
+ const type = random.resolveDataType(schema, 'Role', k);
856
+ return type && index$1.isRelation(type) && _.includes(['User', 'Role'], type.target);
857
+ })) {
858
+ throw Error(`Invalid role keys`);
859
+ }
861
860
  this.options = {
862
861
  ...options,
863
862
  schema,
@@ -1296,29 +1295,7 @@ const signUser = async (proto, res, user, options) => {
1296
1295
  //
1297
1296
  const scheduleOp = {
1298
1297
  expireDocument: async (proto) => {
1299
- for (const className of proto.classes()) {
1300
- if (className === 'File') {
1301
- const found = proto.storage.find({
1302
- className: 'File',
1303
- filter: index$2.QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1304
- matches: {},
1305
- includes: ['_id', '_expired_at', 'token'],
1306
- objectIdSize: 0
1307
- });
1308
- for await (const item of found) {
1309
- const token = item.get('token');
1310
- if (!_.isEmpty(token))
1311
- await proto.fileStorage.destroy(proto, token);
1312
- }
1313
- }
1314
- await proto.storage.deleteMany({
1315
- className,
1316
- filter: index$2.QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1317
- includes: ['_id', '_expired_at'],
1318
- matches: {},
1319
- objectIdSize: 0
1320
- });
1321
- }
1298
+ await proto.gc();
1322
1299
  }
1323
1300
  };
1324
1301
  const schedule = (proto) => {
@@ -1380,6 +1357,7 @@ class ProtoService extends index.ProtoType {
1380
1357
  constructor(options) {
1381
1358
  super();
1382
1359
  this[_private.PVK] = new ProtoInternal({
1360
+ roleInheritKeys: [],
1383
1361
  objectIdSize: 10,
1384
1362
  maxFetchLimit: 1000,
1385
1363
  maxUploadSize: 20 * 1024 * 1024,
@@ -1465,6 +1443,32 @@ class ProtoService extends index.ProtoType {
1465
1443
  const payload = _.create(this, { session });
1466
1444
  return _.assign(payload, _.isFunction(attrs) ? attrs(payload) : attrs);
1467
1445
  }
1446
+ async userRoles(user) {
1447
+ const roleInheritKeys = this[_private.PVK].options.roleInheritKeys;
1448
+ const schema = this.schema;
1449
+ const userKeys = _.filter(roleInheritKeys, k => {
1450
+ const type = random.resolveDataType(schema, 'Role', k);
1451
+ return !!type && index$1.isRelation(type) && type.target === 'User';
1452
+ });
1453
+ const roleKeys = _.filter(roleInheritKeys, k => {
1454
+ const type = random.resolveDataType(schema, 'Role', k);
1455
+ return !!type && index$1.isRelation(type) && type.target === 'Role';
1456
+ });
1457
+ let queue = await this.Query('Role')
1458
+ .or(_.map(_.uniq(['users', ...userKeys]), k => q => q.isIntersect(k, [user])))
1459
+ .includes('name')
1460
+ .find({ master: true });
1461
+ let roles = queue;
1462
+ while (!_.isEmpty(queue)) {
1463
+ queue = await this.Query('Role')
1464
+ .or(_.map(_.uniq(['roles', ...roleKeys]), k => q => q.isIntersect(k, queue)))
1465
+ .notContainsIn('_id', _.compact(_.map(roles, x => x.objectId)))
1466
+ .includes('name')
1467
+ .find({ master: true });
1468
+ roles = _.uniqBy([...roles, ...queue], x => x.objectId);
1469
+ }
1470
+ return roles;
1471
+ }
1468
1472
  async becomeUser(req, user, options) {
1469
1473
  if (!user.objectId)
1470
1474
  throw Error('Invalid user object');
@@ -1569,6 +1573,31 @@ class ProtoService extends index.ProtoType {
1569
1573
  yield self.rebind(object);
1570
1574
  });
1571
1575
  }
1576
+ async gc(classNames) {
1577
+ for (const className of _.castArray(classNames ?? this.classes())) {
1578
+ if (className === 'File') {
1579
+ const found = this.storage.find({
1580
+ className: 'File',
1581
+ filter: index$2.QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1582
+ matches: {},
1583
+ includes: ['_id', '_expired_at', 'token'],
1584
+ objectIdSize: 0
1585
+ });
1586
+ for await (const item of found) {
1587
+ const token = item.get('token');
1588
+ if (!_.isEmpty(token))
1589
+ await this.fileStorage.destroy(this, token);
1590
+ }
1591
+ }
1592
+ await this.storage.deleteMany({
1593
+ className,
1594
+ filter: index$2.QuerySelector.decode({ _expired_at: { $lt: new Date() } }),
1595
+ includes: ['_id', '_expired_at'],
1596
+ matches: {},
1597
+ objectIdSize: 0
1598
+ });
1599
+ }
1600
+ }
1572
1601
  }
1573
1602
 
1574
1603
  //
@@ -1635,7 +1664,7 @@ const encodeError = (error) => {
1635
1664
  if (error instanceof String)
1636
1665
  return { message: error };
1637
1666
  if (error instanceof Error)
1638
- return { ...error, message: error.message };
1667
+ return { message: error.message };
1639
1668
  return error;
1640
1669
  };
1641
1670
  const response = async (res, callback) => {