@stemy/backend 5.0.10 → 5.1.1

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.
@@ -614,8 +614,14 @@ function gunzipPromised(data, opts) {
614
614
  });
615
615
  });
616
616
  }
617
- function deleteFromBucket(bucket, fileId) {
617
+ function deleteFromBucket(bucket, id) {
618
+ const fileId = id instanceof ObjectId ? id : new ObjectId(id);
618
619
  return new Promise(((resolve, reject) => {
620
+ if (!id) {
621
+ // We don't care about empty id
622
+ resolve(null);
623
+ return;
624
+ }
619
625
  bucket.delete(fileId, error => {
620
626
  let err = error;
621
627
  if (error) {
@@ -919,12 +925,6 @@ Configuration = __decorate([
919
925
  ], Configuration);
920
926
 
921
927
  let MongoConnector = class MongoConnector {
922
- constructor(configuration) {
923
- this.configuration = configuration;
924
- this.conn = null;
925
- this.db = null;
926
- this.fsBucket = null;
927
- }
928
928
  get connection() {
929
929
  return this.conn;
930
930
  }
@@ -934,6 +934,12 @@ let MongoConnector = class MongoConnector {
934
934
  get bucket() {
935
935
  return this.fsBucket;
936
936
  }
937
+ constructor(configuration) {
938
+ this.configuration = configuration;
939
+ this.conn = null;
940
+ this.db = null;
941
+ this.fsBucket = null;
942
+ }
937
943
  async connect() {
938
944
  if (this.db)
939
945
  return this.db;
@@ -952,14 +958,14 @@ MongoConnector = __decorate([
952
958
  ], MongoConnector);
953
959
 
954
960
  class BaseEntity {
961
+ get id() {
962
+ return this.mId.toHexString();
963
+ }
955
964
  constructor(mId, data, collection) {
956
965
  this.mId = mId;
957
966
  this.data = data;
958
967
  this.collection = collection;
959
968
  }
960
- get id() {
961
- return this.mId.toHexString();
962
- }
963
969
  save() {
964
970
  return this.collection.updateOne({ _id: this.mId }, { $set: this.toJSON() });
965
971
  }
@@ -980,10 +986,6 @@ class BaseEntity {
980
986
  }
981
987
 
982
988
  class Asset extends BaseEntity {
983
- constructor(id, data, collection, bucket) {
984
- super(id, data, collection);
985
- this.bucket = bucket;
986
- }
987
989
  get filename() {
988
990
  return this.data.filename;
989
991
  }
@@ -996,9 +998,17 @@ class Asset extends BaseEntity {
996
998
  get stream() {
997
999
  return this.bucket.openDownloadStream(this.mId);
998
1000
  }
1001
+ constructor(id, data, collection, bucket) {
1002
+ super(id, data, collection);
1003
+ this.bucket = bucket;
1004
+ }
999
1005
  async unlink() {
1000
1006
  return deleteFromBucket(this.bucket, this.mId);
1001
1007
  }
1008
+ async setMeta(metadata) {
1009
+ metadata = Object.assign(this.metadata, metadata || {});
1010
+ await this.collection.updateOne({ _id: this.mId }, { $set: { metadata } });
1011
+ }
1002
1012
  getBuffer() {
1003
1013
  return streamToBuffer(this.stream);
1004
1014
  }
@@ -1021,6 +1031,9 @@ class Asset extends BaseEntity {
1021
1031
  }
1022
1032
 
1023
1033
  class TempAsset {
1034
+ get stream() {
1035
+ return bufferToStream(this.buffer);
1036
+ }
1024
1037
  constructor(buffer, filename, contentType, metadata) {
1025
1038
  this.buffer = buffer;
1026
1039
  this.filename = filename;
@@ -1028,12 +1041,12 @@ class TempAsset {
1028
1041
  this.metadata = metadata;
1029
1042
  this.id = new ObjectId$1().toHexString();
1030
1043
  }
1031
- get stream() {
1032
- return bufferToStream(this.buffer);
1033
- }
1034
1044
  async unlink() {
1035
1045
  throw new Error(`Temp asset '${this.id}' can not be removed!`);
1036
1046
  }
1047
+ async setMeta(meta) {
1048
+ Object.assign(this.metadata, meta || {});
1049
+ }
1037
1050
  async getBuffer() {
1038
1051
  return this.buffer;
1039
1052
  }
@@ -1197,12 +1210,6 @@ Assets = __decorate([
1197
1210
  ], Assets);
1198
1211
 
1199
1212
  class LazyAsset extends BaseEntity {
1200
- constructor(id, data, collection, logger, assets, progresses) {
1201
- super(id, data, collection);
1202
- this.logger = logger;
1203
- this.assets = assets;
1204
- this.progresses = progresses;
1205
- }
1206
1213
  get jobName() {
1207
1214
  return this.data.jobName;
1208
1215
  }
@@ -1212,12 +1219,24 @@ class LazyAsset extends BaseEntity {
1212
1219
  get jobQue() {
1213
1220
  return this.data.jobQue;
1214
1221
  }
1222
+ get createdAt() {
1223
+ return this.data.createdAt;
1224
+ }
1225
+ get updatedAt() {
1226
+ return this.data.updatedAt;
1227
+ }
1215
1228
  get progressId() {
1216
1229
  return this.data.progressId;
1217
1230
  }
1218
1231
  get assetId() {
1219
1232
  return this.data.assetId;
1220
1233
  }
1234
+ constructor(id, data, collection, logger, assets, progresses) {
1235
+ super(id, data, collection);
1236
+ this.logger = logger;
1237
+ this.assets = assets;
1238
+ this.progresses = progresses;
1239
+ }
1221
1240
  async unlink() {
1222
1241
  await this.load();
1223
1242
  if (!this.progressId) {
@@ -1254,14 +1273,19 @@ class LazyAsset extends BaseEntity {
1254
1273
  return this.loadAsset();
1255
1274
  }
1256
1275
  async writeAsset(asset) {
1276
+ this.data.updatedAt = new Date();
1257
1277
  this.data.assetId = asset.id;
1278
+ await asset.setMeta({ lazyId: this.id });
1258
1279
  await this.save();
1259
1280
  return asset;
1260
1281
  }
1261
1282
  async startWorkingOnAsset(fromLoad) {
1283
+ const oldAsset = this.data.assetId;
1284
+ this.data.updatedAt = new Date();
1262
1285
  this.data.progressId = (await this.progresses.create()).id;
1263
1286
  this.data.assetId = null;
1264
1287
  await this.save();
1288
+ await deleteFromBucket(this.assets.bucket, oldAsset);
1265
1289
  const jobParams = JSON.parse(await gunzipPromised(this.data.jobParams));
1266
1290
  await this.progresses.jobMan.enqueueWithName(this.data.jobName, { ...jobParams, lazyId: this.id, fromLoad });
1267
1291
  }
@@ -1461,9 +1485,6 @@ JobManager = __decorate([
1461
1485
  ], JobManager);
1462
1486
 
1463
1487
  class Progress extends BaseEntity {
1464
- constructor(id, data, collection) {
1465
- super(id, data, collection);
1466
- }
1467
1488
  get current() {
1468
1489
  return this.data.current;
1469
1490
  }
@@ -1485,6 +1506,9 @@ class Progress extends BaseEntity {
1485
1506
  get remaining() {
1486
1507
  return this.max > 0 ? this.max - this.current : 0;
1487
1508
  }
1509
+ constructor(id, data, collection) {
1510
+ super(id, data, collection);
1511
+ }
1488
1512
  setMessageBridge(messageBridge) {
1489
1513
  this.messageBridge = messageBridge || this.messageBridge;
1490
1514
  return this;
@@ -1538,19 +1562,6 @@ class Progress extends BaseEntity {
1538
1562
  }
1539
1563
  }
1540
1564
  class SubProgress {
1541
- constructor(parent, progressFrom, progressValue, mMax = 100) {
1542
- this.parent = parent;
1543
- this.progressFrom = progressFrom;
1544
- this.progressValue = progressValue;
1545
- this.mMax = mMax;
1546
- if (progressFrom < 0) {
1547
- throw "Progress from must be bigger than or zero";
1548
- }
1549
- if (progressValue <= 0) {
1550
- throw "Progress value must be bigger than zero";
1551
- }
1552
- this.mCurrent = 0;
1553
- }
1554
1565
  get id() {
1555
1566
  return this.parent.id;
1556
1567
  }
@@ -1575,6 +1586,19 @@ class SubProgress {
1575
1586
  get canceled() {
1576
1587
  return !this.parent || this.parent.canceled;
1577
1588
  }
1589
+ constructor(parent, progressFrom, progressValue, mMax = 100) {
1590
+ this.parent = parent;
1591
+ this.progressFrom = progressFrom;
1592
+ this.progressValue = progressValue;
1593
+ this.mMax = mMax;
1594
+ if (progressFrom < 0) {
1595
+ throw "Progress from must be bigger than or zero";
1596
+ }
1597
+ if (progressValue <= 0) {
1598
+ throw "Progress value must be bigger than zero";
1599
+ }
1600
+ this.mCurrent = 0;
1601
+ }
1578
1602
  setMessageBridge(messageBridge) {
1579
1603
  if (!this.parent)
1580
1604
  return this;
@@ -1731,11 +1755,13 @@ let LazyAssets = class LazyAssets {
1731
1755
  const data = {
1732
1756
  jobName,
1733
1757
  jobParams,
1734
- jobQue
1758
+ jobQue,
1735
1759
  };
1736
1760
  const existingAsset = await this.find(data);
1737
1761
  if (existingAsset)
1738
1762
  return existingAsset;
1763
+ data.createdAt = new Date();
1764
+ data.updatedAt = data.createdAt;
1739
1765
  const res = await this.collection.insertOne(data);
1740
1766
  return new LazyAsset(res.insertedId, data, this.collection, this.logger, this.assets, this.progresses);
1741
1767
  }
@@ -1836,11 +1862,6 @@ IsObjectId = __decorate([
1836
1862
  ], IsObjectId);
1837
1863
 
1838
1864
  let OpenApi = class OpenApi {
1839
- constructor(container, customValidation) {
1840
- this.container = container;
1841
- this.customValidation = customValidation;
1842
- this.docs = null;
1843
- }
1844
1865
  get apiDocs() {
1845
1866
  if (!this.docs)
1846
1867
  this.docs = this.createApiDocs();
@@ -1851,6 +1872,11 @@ let OpenApi = class OpenApi {
1851
1872
  this.docsStr = JSON.stringify(this.apiDocs);
1852
1873
  return this.docsStr;
1853
1874
  }
1875
+ constructor(container, customValidation) {
1876
+ this.container = container;
1877
+ this.customValidation = customValidation;
1878
+ this.docs = null;
1879
+ }
1854
1880
  async schemaToExample(src, req) {
1855
1881
  const maybeRef = src;
1856
1882
  if (maybeRef.$ref) {
@@ -1976,10 +2002,6 @@ Fixtures = __decorate([
1976
2002
 
1977
2003
  const express = express_;
1978
2004
  let BackendProvider = class BackendProvider {
1979
- constructor(config, container) {
1980
- this.config = config;
1981
- this.container = container;
1982
- }
1983
2005
  get io() {
1984
2006
  this.ioServer = this.ioServer || new Server(this.server, {
1985
2007
  path: "/socket",
@@ -2013,6 +2035,10 @@ let BackendProvider = class BackendProvider {
2013
2035
  this.httpServer = this.httpServer || createServer(this.express);
2014
2036
  return this.httpServer;
2015
2037
  }
2038
+ constructor(config, container) {
2039
+ this.config = config;
2040
+ this.container = container;
2041
+ }
2016
2042
  async quickStart() {
2017
2043
  const port = this.config.resolve("appPort");
2018
2044
  const isWorker = this.config.resolve("isWorker");
@@ -2478,6 +2504,9 @@ TemplateRenderer = __decorate([
2478
2504
  ], TemplateRenderer);
2479
2505
 
2480
2506
  let MailSender = class MailSender {
2507
+ get translator() {
2508
+ return this.renderer.translator;
2509
+ }
2481
2510
  constructor(config, renderer) {
2482
2511
  this.config = config;
2483
2512
  this.renderer = renderer;
@@ -2490,9 +2519,6 @@ let MailSender = class MailSender {
2490
2519
  }
2491
2520
  });
2492
2521
  }
2493
- get translator() {
2494
- return this.renderer.translator;
2495
- }
2496
2522
  async sendMail(language, options) {
2497
2523
  const subject = await this.translator.getTranslation(language, options.subject || "-");
2498
2524
  const html = await this.renderer.render(options.template, language, options.context);
@@ -3202,13 +3228,13 @@ TerminalController$1 = __decorate([
3202
3228
  ], TerminalController$1);
3203
3229
 
3204
3230
  let ErrorHandlerMiddleware = class ErrorHandlerMiddleware {
3231
+ get isDev() {
3232
+ return this.configuration.resolve("nodeEnv") === "development";
3233
+ }
3205
3234
  constructor(configuration, translator) {
3206
3235
  this.configuration = configuration;
3207
3236
  this.translator = translator;
3208
3237
  }
3209
- get isDev() {
3210
- return this.configuration.resolve("nodeEnv") === "development";
3211
- }
3212
3238
  async error(error, req, res, next) {
3213
3239
  const result = await this.getResult(error, req, res);
3214
3240
  if (this.isDev) {
@@ -3537,15 +3563,15 @@ CompressionMiddleware = __decorate([
3537
3563
  ], CompressionMiddleware);
3538
3564
 
3539
3565
  class Tree {
3566
+ get parentTree() {
3567
+ return this.container.parent.tree;
3568
+ }
3540
3569
  constructor(container, exists, path) {
3541
3570
  this.container = container;
3542
3571
  this.exists = exists;
3543
3572
  this.path = path;
3544
3573
  this.map = new Map();
3545
3574
  }
3546
- get parentTree() {
3547
- return this.container.parent.tree;
3548
- }
3549
3575
  resolveService() {
3550
3576
  return !this.exists ? null : this.container.resolve(this.path);
3551
3577
  }
@@ -3651,6 +3677,12 @@ class Tree {
3651
3677
  }
3652
3678
 
3653
3679
  class DiContainer {
3680
+ get registeredTokens() {
3681
+ return (this.parent?.registeredTokens || []).concat(this.tokens);
3682
+ }
3683
+ get tree() {
3684
+ return this.root;
3685
+ }
3654
3686
  constructor(container, parent = null) {
3655
3687
  this.container = container;
3656
3688
  this.parent = parent;
@@ -3659,12 +3691,6 @@ class DiContainer {
3659
3691
  this.tokenSet = new Set();
3660
3692
  this.root = new Tree(this, false, "");
3661
3693
  }
3662
- get registeredTokens() {
3663
- return (this.parent?.registeredTokens || []).concat(this.tokens);
3664
- }
3665
- get tree() {
3666
- return this.root;
3667
- }
3668
3694
  beforeResolution(token, callback, options) {
3669
3695
  this.container.beforeResolution(token, callback, options);
3670
3696
  }
@@ -3886,17 +3912,17 @@ function ResponseType(type, options = {}) {
3886
3912
  }
3887
3913
 
3888
3914
  class LazyAssetGenerator {
3889
- constructor(assetResolver, progresses, lazyId) {
3890
- this.assetResolver = assetResolver;
3891
- this.progresses = progresses;
3892
- this.lazyId = lazyId;
3893
- }
3894
3915
  get assets() {
3895
3916
  return this.assetResolver.assets;
3896
3917
  }
3897
3918
  get lazyAssets() {
3898
3919
  return this.assetResolver.lazyAssets;
3899
3920
  }
3921
+ constructor(assetResolver, progresses, lazyId) {
3922
+ this.assetResolver = assetResolver;
3923
+ this.progresses = progresses;
3924
+ this.lazyId = lazyId;
3925
+ }
3900
3926
  async process(messaging) {
3901
3927
  const lazyAsset = await this.lazyAssets.read(this.lazyId);
3902
3928
  let progress = await this.progresses.get(lazyAsset.progressId);