@stemy/backend 2.7.4 → 2.8.3

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.
@@ -36,7 +36,7 @@ import * as Handlebars from 'handlebars';
36
36
  import { compare } from 'bcrypt';
37
37
  import moment from 'moment';
38
38
 
39
- var __awaiter$u = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
39
+ var __awaiter$v = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
40
40
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
41
41
  return new (P || (P = Promise))(function (resolve, reject) {
42
42
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -238,7 +238,7 @@ function hydratePopulated(modelType, json) {
238
238
  return object;
239
239
  }
240
240
  function paginateAggregations(model, aggregations, params, metaProjection = {}) {
241
- return __awaiter$u(this, void 0, void 0, function* () {
241
+ return __awaiter$v(this, void 0, void 0, function* () {
242
242
  const sortField = !isString(params.sort) || !params.sort ? null : (params.sort.startsWith("-") ? params.sort.substr(1) : params.sort);
243
243
  const sortAggregation = !sortField ? [] : [{
244
244
  $sort: { [sortField]: sortField == params.sort ? 1 : -1 }
@@ -337,7 +337,7 @@ function readFile(path) {
337
337
  });
338
338
  }
339
339
  function readAndDeleteFile(path, timeout = 5000) {
340
- return __awaiter$u(this, void 0, void 0, function* () {
340
+ return __awaiter$v(this, void 0, void 0, function* () {
341
341
  const data = yield readFile(path);
342
342
  setTimeout(() => {
343
343
  unlink(path, () => {
@@ -347,7 +347,7 @@ function readAndDeleteFile(path, timeout = 5000) {
347
347
  });
348
348
  }
349
349
  function writeFile(path, data) {
350
- return __awaiter$u(this, void 0, void 0, function* () {
350
+ return __awaiter$v(this, void 0, void 0, function* () {
351
351
  yield mkdirRecursive(dirname(path));
352
352
  return new Promise((res, rej) => {
353
353
  writeFile$1(path, data, err => {
@@ -426,7 +426,7 @@ function ResolveEntity(model, extraCheck) {
426
426
  const paramName = modelName.toLowerCase();
427
427
  return createParamDecorator({
428
428
  required: false,
429
- value: (action) => __awaiter$u(this, void 0, void 0, function* () {
429
+ value: (action) => __awaiter$v(this, void 0, void 0, function* () {
430
430
  const req = action.request;
431
431
  const token = req.header(`x-${paramName}-token`);
432
432
  const id = req.params[`${paramName}Id`];
@@ -750,7 +750,7 @@ var __decorate$w = (this && this.__decorate) || function (decorators, target, ke
750
750
  else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
751
751
  return c > 3 && r && Object.defineProperty(target, key, r), r;
752
752
  };
753
- var __awaiter$t = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
753
+ var __awaiter$u = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
754
754
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
755
755
  return new (P || (P = Promise))(function (resolve, reject) {
756
756
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -775,7 +775,7 @@ const fontProps = [
775
775
  ];
776
776
  let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
777
777
  static getMimeType(buffer, mimeType) {
778
- return __awaiter$t(this, void 0, void 0, function* () {
778
+ return __awaiter$u(this, void 0, void 0, function* () {
779
779
  try {
780
780
  mimeType = (yield fromBuffer(buffer)).mime;
781
781
  }
@@ -807,7 +807,7 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
807
807
  return imageTypes.indexOf(contentType) >= 0;
808
808
  }
809
809
  static copyImageMeta(buffer, metadata) {
810
- return __awaiter$t(this, void 0, void 0, function* () {
810
+ return __awaiter$u(this, void 0, void 0, function* () {
811
811
  const output = yield sharp$3(buffer).rotate().toBuffer({ resolveWithObject: true });
812
812
  Object.assign(metadata, output.info);
813
813
  return output.data;
@@ -824,7 +824,7 @@ let AssetProcessor = AssetProcessor_1 = class AssetProcessor {
824
824
  });
825
825
  }
826
826
  process(buffer, metadata, contentType) {
827
- return __awaiter$t(this, void 0, void 0, function* () {
827
+ return __awaiter$u(this, void 0, void 0, function* () {
828
828
  if (AssetProcessor_1.isImage(contentType)) {
829
829
  buffer = yield AssetProcessor_1.copyImageMeta(buffer, metadata);
830
830
  }
@@ -876,9 +876,11 @@ let Configuration = class Configuration {
876
876
  }).replace(/\./gi, "_").replace(/^_/, "").toUpperCase();
877
877
  const envValue = process.env[envName];
878
878
  if (typeof envValue !== "undefined") {
879
- return isFunction(param.resolver)
879
+ const value = isFunction(param.resolver)
880
880
  ? param.resolver(envValue)
881
881
  : convertValue(envValue, getType(param.defaultValue));
882
+ console.log(`Processing param value`, name, envName, envValue, value);
883
+ return value;
882
884
  }
883
885
  return param.defaultValue;
884
886
  }
@@ -899,7 +901,7 @@ var __decorate$u = (this && this.__decorate) || function (decorators, target, ke
899
901
  var __metadata$o = (this && this.__metadata) || function (k, v) {
900
902
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
901
903
  };
902
- var __awaiter$s = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
904
+ var __awaiter$t = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
903
905
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
904
906
  return new (P || (P = Promise))(function (resolve, reject) {
905
907
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
@@ -925,7 +927,7 @@ let MongoConnector = class MongoConnector {
925
927
  return this.fsBucket;
926
928
  }
927
929
  connect() {
928
- return __awaiter$s(this, void 0, void 0, function* () {
930
+ return __awaiter$t(this, void 0, void 0, function* () {
929
931
  if (this.db)
930
932
  return this.db;
931
933
  this.conn = (yield connect(this.configuration.resolve("mongoUri"), {
@@ -946,6 +948,43 @@ MongoConnector = __decorate$u([
946
948
  __metadata$o("design:paramtypes", [Configuration])
947
949
  ], MongoConnector);
948
950
 
951
+ var __awaiter$s = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
952
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
953
+ return new (P || (P = Promise))(function (resolve, reject) {
954
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
955
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
956
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
957
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
958
+ });
959
+ };
960
+ class BaseEntity {
961
+ constructor(mId, data, collection) {
962
+ this.mId = mId;
963
+ this.data = data;
964
+ this.collection = collection;
965
+ }
966
+ get id() {
967
+ return this.mId.toHexString();
968
+ }
969
+ save() {
970
+ return this.collection.updateOne({ _id: this.mId }, { $set: this.toJSON() });
971
+ }
972
+ load() {
973
+ return __awaiter$s(this, void 0, void 0, function* () {
974
+ const res = yield this.collection.findOne({ _id: this.mId });
975
+ this.deleted = !res;
976
+ this.data = res || {};
977
+ return this;
978
+ });
979
+ }
980
+ toJSON() {
981
+ const ret = Object.assign({}, this.data);
982
+ delete ret._id;
983
+ ret.id = this.id;
984
+ return ret;
985
+ }
986
+ }
987
+
949
988
  var __awaiter$r = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
950
989
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
951
990
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -962,14 +1001,10 @@ const cropInterface = {
962
1001
  w: "number",
963
1002
  h: "number"
964
1003
  };
965
- class Asset {
966
- constructor(fileId, filename, contentType, metadata, bucket, collection) {
967
- this.fileId = fileId;
968
- this.filename = filename;
969
- this.contentType = contentType;
970
- this.metadata = metadata;
1004
+ class Asset extends BaseEntity {
1005
+ constructor(id, data, collection, bucket) {
1006
+ super(id, data, collection);
971
1007
  this.bucket = bucket;
972
- this.collection = collection;
973
1008
  }
974
1009
  static toCropRegion(cropInfo) {
975
1010
  let crop = cropInfo;
@@ -1064,15 +1099,21 @@ class Asset {
1064
1099
  }
1065
1100
  });
1066
1101
  }
1067
- get id() {
1068
- return this.fileId.toHexString();
1102
+ get filename() {
1103
+ return this.data.filename;
1104
+ }
1105
+ get contentType() {
1106
+ return this.data.contentType;
1107
+ }
1108
+ get metadata() {
1109
+ return this.data.metadata;
1069
1110
  }
1070
1111
  get stream() {
1071
- return this.bucket.openDownloadStream(this.fileId);
1112
+ return this.bucket.openDownloadStream(this.mId);
1072
1113
  }
1073
1114
  unlink() {
1074
1115
  return __awaiter$r(this, void 0, void 0, function* () {
1075
- return deleteFromBucket(this.bucket, this.fileId);
1116
+ return deleteFromBucket(this.bucket, this.mId);
1076
1117
  });
1077
1118
  }
1078
1119
  getBuffer() {
@@ -1086,7 +1127,7 @@ class Asset {
1086
1127
  : metadata.downloadCount + 1;
1087
1128
  metadata.firstDownload = metadata.firstDownload || new Date();
1088
1129
  metadata.lastDownload = new Date();
1089
- yield this.collection.updateOne({ _id: this.fileId }, { $set: { metadata } });
1130
+ yield this.collection.updateOne({ _id: this.mId }, { $set: { metadata } });
1090
1131
  return this.stream;
1091
1132
  });
1092
1133
  }
@@ -1100,14 +1141,6 @@ class Asset {
1100
1141
  return Asset.toImage(yield this.download(metadata), this.metadata, params);
1101
1142
  });
1102
1143
  }
1103
- toJSON() {
1104
- return {
1105
- id: this.id,
1106
- filename: this.filename,
1107
- contentType: this.contentType,
1108
- metadata: this.metadata
1109
- };
1110
- }
1111
1144
  }
1112
1145
 
1113
1146
  var __decorate$t = (this && this.__decorate) || function (decorators, target, key, desc) {
@@ -1168,8 +1201,12 @@ let Assets = class Assets {
1168
1201
  reject(error.message || error);
1169
1202
  })
1170
1203
  .on("finish", () => {
1171
- const asset = new Asset(uploaderStream.id, metadata.filename, contentType, metadata, this.bucket, this.collection);
1172
- this.collection.updateOne({ _id: uploaderStream.id }, { $set: asset.toJSON() }).then(() => {
1204
+ const asset = new Asset(uploaderStream.id, {
1205
+ filename: metadata.filename,
1206
+ contentType,
1207
+ metadata
1208
+ }, this.collection, this.bucket);
1209
+ asset.save().then(() => {
1173
1210
  resolve(asset);
1174
1211
  }, error => {
1175
1212
  reject(error.message || error);
@@ -1200,7 +1237,20 @@ let Assets = class Assets {
1200
1237
  find(where) {
1201
1238
  return __awaiter$q(this, void 0, void 0, function* () {
1202
1239
  const data = yield this.collection.findOne(where);
1203
- return !data ? null : new Asset(data._id, data.filename, data.contentType, data.metadata, this.bucket, this.collection);
1240
+ return !data ? null : new Asset(data._id, data, this.collection, this.bucket);
1241
+ });
1242
+ }
1243
+ findMany(where) {
1244
+ return __awaiter$q(this, void 0, void 0, function* () {
1245
+ const cursor = this.collection.find(where);
1246
+ const items = (yield cursor.toArray()) || [];
1247
+ const result = [];
1248
+ for (let item of items) {
1249
+ if (!item)
1250
+ continue;
1251
+ result.push(new Asset(item._id, item, this.collection, this.bucket));
1252
+ }
1253
+ return result;
1204
1254
  });
1205
1255
  }
1206
1256
  unlink(id) {
@@ -1227,52 +1277,61 @@ var __awaiter$p = (this && this.__awaiter) || function (thisArg, _arguments, P,
1227
1277
  step((generator = generator.apply(thisArg, _arguments || [])).next());
1228
1278
  });
1229
1279
  };
1230
- class LazyAsset {
1231
- constructor(lazyId, jobName, jobParams, jobQue, mProgressId, mAssetId, assets, progresses, jobMan, collection) {
1232
- this.lazyId = lazyId;
1233
- this.jobName = jobName;
1234
- this.jobParams = jobParams;
1235
- this.jobQue = jobQue;
1236
- this.mProgressId = mProgressId;
1237
- this.mAssetId = mAssetId;
1280
+ class LazyAsset extends BaseEntity {
1281
+ constructor(id, data, collection, assets, progresses, jobMan) {
1282
+ super(id, data, collection);
1238
1283
  this.assets = assets;
1239
1284
  this.progresses = progresses;
1240
1285
  this.jobMan = jobMan;
1241
- this.collection = collection;
1242
1286
  }
1243
- get id() {
1244
- return this.lazyId.toHexString();
1287
+ get jobName() {
1288
+ return this.data.jobName;
1289
+ }
1290
+ get jobParams() {
1291
+ return this.data.jobParams;
1292
+ }
1293
+ get jobQue() {
1294
+ return this.data.jobQue;
1245
1295
  }
1246
1296
  get progressId() {
1247
- return this.mProgressId;
1297
+ return this.data.progressId;
1248
1298
  }
1249
1299
  get assetId() {
1250
- return this.mAssetId;
1300
+ return this.data.assetId;
1251
1301
  }
1252
1302
  unlink() {
1253
1303
  return __awaiter$p(this, void 0, void 0, function* () {
1254
- yield this.collection.deleteOne({ _id: this.lazyId });
1255
- return deleteFromBucket(this.assets.bucket, new ObjectId(this.mAssetId));
1304
+ yield this.load();
1305
+ if (!this.progressId) {
1306
+ yield this.collection.deleteOne({ _id: this.mId });
1307
+ }
1308
+ return deleteFromBucket(this.assets.bucket, new ObjectId(this.assetId));
1256
1309
  });
1257
1310
  }
1258
1311
  startWorking() {
1259
- if (this.mProgressId)
1260
- return;
1261
- this.startWorkingOnAsset().then(() => {
1262
- console.log(`Started working on lazy asset: ${this.id}`);
1263
- }).catch(reason => {
1264
- console.log(`Can't start working on lazy asset: ${this.id}\nReason: ${reason}`);
1312
+ this.load().then(() => {
1313
+ if (this.deleted)
1314
+ return;
1315
+ const progressPromise = !this.progressId ? Promise.resolve(null) : this.progresses.get(this.progressId).then(p => p.cancel());
1316
+ progressPromise.then(() => {
1317
+ this.startWorkingOnAsset().then(() => {
1318
+ console.log(`Started working on lazy asset: ${this.id}`);
1319
+ }).catch(reason => {
1320
+ console.log(`Can't start working on lazy asset: ${this.id}\nReason: ${reason}`);
1321
+ });
1322
+ });
1265
1323
  });
1266
1324
  }
1267
1325
  loadAsset() {
1268
1326
  return __awaiter$p(this, void 0, void 0, function* () {
1269
- if (this.mAssetId) {
1270
- return this.assets.read(this.mAssetId);
1327
+ yield this.load();
1328
+ if (this.deleted)
1329
+ return null;
1330
+ if (this.assetId) {
1331
+ return this.assets.read(this.assetId);
1271
1332
  }
1272
- if (this.mProgressId) {
1273
- yield this.progresses.waitToFinish(this.mProgressId);
1274
- const data = yield this.collection.findOne({ _id: this.lazyId });
1275
- this.mAssetId = data.assetId;
1333
+ if (this.progressId) {
1334
+ yield this.progresses.waitToFinish(this.progressId);
1276
1335
  return this.loadAsset();
1277
1336
  }
1278
1337
  yield this.startWorkingOnAsset();
@@ -1281,30 +1340,17 @@ class LazyAsset {
1281
1340
  }
1282
1341
  writeAsset(asset) {
1283
1342
  return __awaiter$p(this, void 0, void 0, function* () {
1284
- this.mAssetId = asset.id;
1343
+ this.data.assetId = asset.id;
1285
1344
  yield this.save();
1286
1345
  return asset;
1287
1346
  });
1288
1347
  }
1289
- save() {
1290
- return this.collection.updateOne({ _id: this.lazyId }, { $set: this.toJSON() });
1291
- }
1292
- toJSON() {
1293
- return {
1294
- id: this.id,
1295
- jobName: this.jobName,
1296
- jobParams: this.jobParams,
1297
- jobQue: this.jobQue,
1298
- progressId: this.progressId,
1299
- assetId: this.assetId,
1300
- };
1301
- }
1302
1348
  startWorkingOnAsset() {
1303
1349
  return __awaiter$p(this, void 0, void 0, function* () {
1304
- const progress = yield this.progresses.create();
1305
- this.mProgressId = progress.id;
1350
+ const { id } = yield this.progresses.create();
1351
+ this.data.progressId = id;
1306
1352
  yield this.save();
1307
- yield this.jobMan.enqueueWithName(this.jobName, Object.assign(Object.assign({}, this.jobParams), { lazyId: this.id }));
1353
+ yield this.jobMan.enqueueWithName(this.data.jobName, Object.assign(Object.assign({}, this.data.jobParams), { lazyId: this.id }));
1308
1354
  });
1309
1355
  }
1310
1356
  }
@@ -1513,36 +1559,31 @@ var __awaiter$n = (this && this.__awaiter) || function (thisArg, _arguments, P,
1513
1559
  step((generator = generator.apply(thisArg, _arguments || [])).next());
1514
1560
  });
1515
1561
  };
1516
- class Progress {
1517
- constructor(progressId, mCurrent, mMax, mMessage, mError, client, collection) {
1518
- this.progressId = progressId;
1519
- this.mCurrent = mCurrent;
1520
- this.mMax = mMax;
1521
- this.mMessage = mMessage;
1522
- this.mError = mError;
1562
+ class Progress extends BaseEntity {
1563
+ constructor(id, data, collection, client) {
1564
+ super(id, data, collection);
1523
1565
  this.client = client;
1524
- this.collection = collection;
1525
- }
1526
- get id() {
1527
- return this.progressId.toHexString();
1528
1566
  }
1529
1567
  get current() {
1530
- return this.mCurrent;
1568
+ return this.data.current;
1531
1569
  }
1532
1570
  get max() {
1533
- return this.mMax;
1571
+ return this.data.max;
1534
1572
  }
1535
1573
  get message() {
1536
- return this.mMessage;
1574
+ return this.data.message;
1537
1575
  }
1538
1576
  get error() {
1539
- return this.mError;
1577
+ return this.data.error;
1578
+ }
1579
+ get canceled() {
1580
+ return this.data.canceled;
1540
1581
  }
1541
1582
  get percent() {
1542
- return this.mMax > 0 ? Math.round(this.mCurrent / this.mMax * 100) : 0;
1583
+ return this.max > 0 ? Math.round(this.current / this.max * 100) : 0;
1543
1584
  }
1544
1585
  get remaining() {
1545
- return this.mMax > 0 ? this.mMax - this.mCurrent : 0;
1586
+ return this.max > 0 ? this.max - this.current : 0;
1546
1587
  }
1547
1588
  createSubProgress(progressValue, max, message) {
1548
1589
  return __awaiter$n(this, void 0, void 0, function* () {
@@ -1550,10 +1591,10 @@ class Progress {
1550
1591
  yield this.advance(progressValue);
1551
1592
  }
1552
1593
  if (message !== null) {
1553
- this.mMessage = message;
1594
+ this.data.message = message;
1554
1595
  yield this.save();
1555
1596
  }
1556
- return new SubProgress(this, this.mCurrent, progressValue, Math.max(max, 1));
1597
+ return new SubProgress(this, this.current, progressValue, Math.max(max, 1));
1557
1598
  });
1558
1599
  }
1559
1600
  setMax(max) {
@@ -1561,13 +1602,19 @@ class Progress {
1561
1602
  if (isNaN(max) || max <= 0) {
1562
1603
  throw "Max progress value must be bigger than zero";
1563
1604
  }
1564
- this.mMax = max;
1605
+ this.data.max = max;
1606
+ yield this.save();
1607
+ });
1608
+ }
1609
+ setMessage(message) {
1610
+ return __awaiter$n(this, void 0, void 0, function* () {
1611
+ this.data.message = message;
1565
1612
  yield this.save();
1566
1613
  });
1567
1614
  }
1568
1615
  setError(error) {
1569
1616
  return __awaiter$n(this, void 0, void 0, function* () {
1570
- this.mError = error;
1617
+ this.data.error = error;
1571
1618
  yield this.save();
1572
1619
  });
1573
1620
  }
@@ -1576,63 +1623,60 @@ class Progress {
1576
1623
  if (isNaN(value) || value <= 0) {
1577
1624
  throw "Advance value must be bigger than zero";
1578
1625
  }
1579
- this.mCurrent = Math.min(this.mMax, this.mCurrent + value);
1626
+ yield this.load();
1627
+ if (this.deleted || this.canceled)
1628
+ return null;
1629
+ this.data.current = Math.min(this.max, this.current + value);
1580
1630
  yield this.save();
1581
1631
  if (!this.client)
1582
1632
  return;
1583
1633
  this.client.emit("background-progress", this.id);
1584
1634
  });
1585
1635
  }
1586
- toJSON() {
1587
- return {
1588
- id: this.id,
1589
- current: this.current,
1590
- max: this.max,
1591
- message: this.message,
1592
- error: this.error
1593
- };
1594
- }
1595
- save() {
1596
- return this.collection.updateOne({ _id: this.progressId }, { $set: this.toJSON() });
1636
+ cancel() {
1637
+ return __awaiter$n(this, void 0, void 0, function* () {
1638
+ this.data.canceled = true;
1639
+ yield this.save();
1640
+ });
1597
1641
  }
1598
1642
  }
1599
1643
  class SubProgress {
1600
- constructor(parent, progressFrom, progressValue, max = 100) {
1644
+ constructor(parent, progressFrom, progressValue, mMax = 100) {
1601
1645
  this.parent = parent;
1602
1646
  this.progressFrom = progressFrom;
1603
1647
  this.progressValue = progressValue;
1604
- this.max = max;
1648
+ this.mMax = mMax;
1605
1649
  if (progressFrom < 0) {
1606
1650
  throw "Progress from must be bigger than or zero";
1607
1651
  }
1608
1652
  if (progressValue <= 0) {
1609
1653
  throw "Progress value must be bigger than zero";
1610
1654
  }
1611
- this.currentValue = 0;
1655
+ this.mCurrent = 0;
1612
1656
  }
1613
1657
  get id() {
1614
1658
  return this.parent.id;
1615
1659
  }
1660
+ get current() {
1661
+ return this.mCurrent;
1662
+ }
1663
+ get max() {
1664
+ return this.mMax;
1665
+ }
1616
1666
  get message() {
1617
1667
  return this.parent.message;
1618
1668
  }
1619
- set message(value) {
1620
- this.parent.message = value;
1621
- }
1622
1669
  get error() {
1623
1670
  return this.parent.error;
1624
1671
  }
1625
- set error(value) {
1626
- this.parent.error = value;
1627
- }
1628
1672
  get percent() {
1629
1673
  return this.parent.percent;
1630
1674
  }
1631
- get current() {
1632
- return this.currentValue;
1633
- }
1634
1675
  get remaining() {
1635
- return this.max - this.currentValue;
1676
+ return this.max - this.mCurrent;
1677
+ }
1678
+ get canceled() {
1679
+ return !this.parent || this.parent.canceled;
1636
1680
  }
1637
1681
  createSubProgress(progressValue, max, message) {
1638
1682
  return __awaiter$n(this, void 0, void 0, function* () {
@@ -1640,8 +1684,7 @@ class SubProgress {
1640
1684
  yield this.advance(progressValue);
1641
1685
  }
1642
1686
  if (message !== null) {
1643
- this.message = message;
1644
- yield this.parent.save();
1687
+ yield this.setMessage(message);
1645
1688
  }
1646
1689
  return new SubProgress(this, this.current, progressValue, Math.max(max, 1));
1647
1690
  });
@@ -1651,14 +1694,22 @@ class SubProgress {
1651
1694
  if (isNaN(max) || max <= 0) {
1652
1695
  throw "Max progress value must be bigger than zero";
1653
1696
  }
1654
- this.max = max;
1697
+ this.mMax = max;
1655
1698
  yield this.save();
1656
1699
  });
1657
1700
  }
1701
+ setMessage(message) {
1702
+ return __awaiter$n(this, void 0, void 0, function* () {
1703
+ if (!this.parent)
1704
+ return null;
1705
+ yield this.parent.setMessage(message);
1706
+ });
1707
+ }
1658
1708
  setError(error) {
1659
1709
  return __awaiter$n(this, void 0, void 0, function* () {
1660
- this.error = error || null;
1661
- yield this.save();
1710
+ if (!this.parent)
1711
+ return null;
1712
+ yield this.parent.setError(error);
1662
1713
  });
1663
1714
  }
1664
1715
  advance(value = 1) {
@@ -1666,13 +1717,20 @@ class SubProgress {
1666
1717
  if (isNaN(value) || value <= 0) {
1667
1718
  throw "Advance value must be bigger than zero";
1668
1719
  }
1669
- this.currentValue = Math.min(this.max, this.currentValue + value);
1720
+ this.mCurrent = Math.min(this.max, this.mCurrent + value);
1670
1721
  yield this.save();
1671
1722
  });
1672
1723
  }
1724
+ cancel() {
1725
+ return __awaiter$n(this, void 0, void 0, function* () {
1726
+ if (!this.parent)
1727
+ return null;
1728
+ yield this.parent.cancel();
1729
+ });
1730
+ }
1673
1731
  save() {
1674
1732
  return __awaiter$n(this, void 0, void 0, function* () {
1675
- const ratio = this.max > 0 ? this.currentValue / this.max : 0;
1733
+ const ratio = this.max > 0 ? this.mCurrent / this.max : 0;
1676
1734
  const newProgress = this.progressFrom + Math.round(this.progressValue * ratio);
1677
1735
  const current = this.parent.current;
1678
1736
  if (newProgress <= current)
@@ -1680,6 +1738,11 @@ class SubProgress {
1680
1738
  yield this.parent.advance(newProgress);
1681
1739
  });
1682
1740
  }
1741
+ load() {
1742
+ return __awaiter$n(this, void 0, void 0, function* () {
1743
+ return null;
1744
+ });
1745
+ }
1683
1746
  toJSON() {
1684
1747
  return this.parent.toJSON();
1685
1748
  }
@@ -1740,7 +1803,7 @@ let Progresses = class Progresses {
1740
1803
  find(where) {
1741
1804
  return __awaiter$m(this, void 0, void 0, function* () {
1742
1805
  const data = yield this.collection.findOne(where);
1743
- return !data ? null : new Progress(data._id, data.current, data.max, data.message, data.error, this.client, this.collection);
1806
+ return !data ? null : new Progress(data._id, data, this.collection, this.client);
1744
1807
  });
1745
1808
  }
1746
1809
  create(max = 100) {
@@ -1748,11 +1811,15 @@ let Progresses = class Progresses {
1748
1811
  if (isNaN(max) || max <= 0) {
1749
1812
  throw "Max progress value must be bigger than zero";
1750
1813
  }
1751
- const res = yield this.collection.insertOne({
1814
+ const data = {
1752
1815
  current: 0,
1753
- max
1754
- });
1755
- return new Progress(res.insertedId, 0, max, "", "", this.client, this.collection);
1816
+ max: max,
1817
+ message: "",
1818
+ error: "",
1819
+ canceled: false
1820
+ };
1821
+ const res = yield this.collection.insertOne(data);
1822
+ return new Progress(res.insertedId, data, this.collection, this.client);
1756
1823
  });
1757
1824
  }
1758
1825
  remove(id) {
@@ -1797,12 +1864,16 @@ let LazyAssets = class LazyAssets {
1797
1864
  create(jobType, jobParams = {}, jobQue = "main") {
1798
1865
  return __awaiter$l(this, void 0, void 0, function* () {
1799
1866
  const jobName = this.jobMan.tryResolve(jobType, Object.assign(Object.assign({}, jobParams), { lazyId: "" }));
1800
- const res = yield this.collection.insertOne({
1867
+ const data = {
1801
1868
  jobName,
1802
1869
  jobParams,
1803
1870
  jobQue
1804
- });
1805
- return new LazyAsset(res.insertedId, jobName, jobParams, jobQue, null, null, this.assets, this.progresses, this.jobMan, this.collection);
1871
+ };
1872
+ const existingAsset = yield this.find(data);
1873
+ if (existingAsset)
1874
+ return existingAsset;
1875
+ const res = yield this.collection.insertOne(data);
1876
+ return new LazyAsset(res.insertedId, data, this.collection, this.assets, this.progresses, this.jobMan);
1806
1877
  });
1807
1878
  }
1808
1879
  read(id) {
@@ -1815,7 +1886,7 @@ let LazyAssets = class LazyAssets {
1815
1886
  const data = yield this.collection.findOne(where);
1816
1887
  return !data
1817
1888
  ? null
1818
- : new LazyAsset(data._id, data.jobName, data.jobParams, data.jobQue, data.progressId, data.assetId, this.assets, this.progresses, this.jobMan, this.collection);
1889
+ : new LazyAsset(data._id, data, this.collection, this.assets, this.progresses, this.jobMan);
1819
1890
  });
1820
1891
  }
1821
1892
  unlink(id) {
@@ -3285,8 +3356,9 @@ CompressionMiddleware = __decorate([
3285
3356
  ], CompressionMiddleware);
3286
3357
 
3287
3358
  class Tree {
3288
- constructor(container, path) {
3359
+ constructor(container, exists, path) {
3289
3360
  this.container = container;
3361
+ this.exists = exists;
3290
3362
  this.path = path;
3291
3363
  this.map = new Map();
3292
3364
  }
@@ -3294,7 +3366,7 @@ class Tree {
3294
3366
  return this.container.parent.tree;
3295
3367
  }
3296
3368
  resolveService() {
3297
- return this.container.resolve(this.path);
3369
+ return !this.exists ? null : this.container.resolve(this.path);
3298
3370
  }
3299
3371
  resolveLeaves() {
3300
3372
  let map;
@@ -3335,7 +3407,7 @@ class Tree {
3335
3407
  parentTree = parentTree.resolveAncestor(path);
3336
3408
  }
3337
3409
  catch (e) {
3338
- parentTree = new Tree(this.container, "");
3410
+ parentTree = new Tree(this.container, false, "");
3339
3411
  }
3340
3412
  const pathParts = path.split(".");
3341
3413
  let tree = this;
@@ -3358,7 +3430,7 @@ class Tree {
3358
3430
  }
3359
3431
  resolvePath(path, throwError = true) {
3360
3432
  const absolutePath = !this.path ? path : `${this.path}.${path}`;
3361
- let tree = new Tree(this.container, absolutePath);
3433
+ let tree = new Tree(this.container, false, absolutePath);
3362
3434
  try {
3363
3435
  tree = this.resolveAncestor(path);
3364
3436
  }
@@ -3381,13 +3453,16 @@ class Tree {
3381
3453
  return this;
3382
3454
  }
3383
3455
  const pathParts = path.split(".");
3456
+ const maxIx = pathParts.length - 1;
3384
3457
  let tree = this;
3385
3458
  path = this.path;
3386
- for (let part of pathParts) {
3459
+ for (let ix = 0; ix <= maxIx; ix++) {
3460
+ const part = pathParts[ix];
3387
3461
  if (!tree.map.has(part)) {
3388
- tree.map.set(part, new Tree(this.container, !path ? part : `${path}.${part}`));
3462
+ tree.map.set(part, new Tree(this.container, false, !path ? part : `${path}.${part}`));
3389
3463
  }
3390
3464
  tree = tree.map.get(part);
3465
+ tree.exists = tree.exists || ix == maxIx;
3391
3466
  path = tree.path;
3392
3467
  }
3393
3468
  return this;
@@ -3401,14 +3476,14 @@ class DiContainer {
3401
3476
  container["wrapperContainer"] = this;
3402
3477
  this.tokens = [];
3403
3478
  this.tokenSet = new Set();
3404
- this.myTree = new Tree(this, "");
3479
+ this.root = new Tree(this, false, "");
3405
3480
  }
3406
3481
  get registeredTokens() {
3407
3482
  var _a;
3408
3483
  return (((_a = this.parent) === null || _a === void 0 ? void 0 : _a.registeredTokens) || []).concat(this.tokens);
3409
3484
  }
3410
3485
  get tree() {
3411
- return this.myTree;
3486
+ return this.root;
3412
3487
  }
3413
3488
  beforeResolution(token, callback, options) {
3414
3489
  this.container.beforeResolution(token, callback, options);
@@ -3469,7 +3544,7 @@ class DiContainer {
3469
3544
  this.tokenSet.add(token);
3470
3545
  this.tokens.push(token);
3471
3546
  if (isString(token)) {
3472
- this.myTree.addPath(token);
3547
+ this.root.addPath(token);
3473
3548
  }
3474
3549
  return this;
3475
3550
  }
@@ -3527,9 +3602,14 @@ class LazyAssetGenerator {
3527
3602
  process() {
3528
3603
  return __awaiter$1(this, void 0, void 0, function* () {
3529
3604
  const lazyAsset = yield this.lazyAssets.read(this.lazyId);
3530
- const progress = yield this.progresses.get(lazyAsset.progressId);
3605
+ let progress = yield this.progresses.get(lazyAsset.progressId);
3606
+ if (!progress || progress.canceled)
3607
+ return null;
3531
3608
  try {
3532
3609
  const asset = yield this.generate(progress);
3610
+ progress = yield progress.load();
3611
+ if (!progress || progress.canceled)
3612
+ return null;
3533
3613
  yield lazyAsset.writeAsset(asset);
3534
3614
  }
3535
3615
  catch (e) {