@resolveio/server-lib 12.6.5 → 12.6.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@resolveio/server-lib",
3
- "version": "12.6.5",
3
+ "version": "12.6.7",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "package": "npm run build-prod && npm pack ./dist",
@@ -13,6 +13,7 @@
13
13
  "dependencies": {
14
14
  "@aws-sdk/client-s3": "3.435.0",
15
15
  "@aws-sdk/s3-request-presigner": "3.435.0",
16
+ "async": "3.2.4",
16
17
  "axios": "1.5.1",
17
18
  "body-parser": "1.20.2",
18
19
  "clone": "2.1.2",
package/util/common.d.ts CHANGED
@@ -16,8 +16,8 @@ export declare function mergeUpdates(...updatesToMerge: any[]): {
16
16
  $pull: {};
17
17
  };
18
18
  export declare function getDeepDiff(doc1: any, doc2: any): any;
19
- export declare function deepDiffDetails(obj1: Object, obj2: Object): string;
20
19
  export declare function applyMongoUpdate(uDoc: any, delta: any): any;
20
+ export declare function deepDiffDetails(obj1: Object, obj2: Object): string;
21
21
  export declare function getBinarySize(string: any): number;
22
22
  export declare function pad(num: any, size: any): string;
23
23
  export declare function randomString(length: any, chars: any): string;
package/util/common.js CHANGED
@@ -9,7 +9,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
9
9
  return to.concat(ar || Array.prototype.slice.call(from));
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.mergeDeep = exports.isObject = exports.toTitleCase = exports.randomString = exports.pad = exports.getBinarySize = exports.applyMongoUpdate = exports.deepDiffDetails = exports.getDeepDiff = exports.mergeUpdates = exports.getMongoMergeUpdatedDoc = exports.getMongoUpdates = exports.dateReviver = exports.deepCopy = exports.floorPrecision = exports.roundPrecision = exports.throwErr = exports.buildRbLookups = exports.buildRbSchema = void 0;
12
+ exports.mergeDeep = exports.isObject = exports.toTitleCase = exports.randomString = exports.pad = exports.getBinarySize = exports.deepDiffDetails = exports.applyMongoUpdate = exports.getDeepDiff = exports.mergeUpdates = exports.getMongoMergeUpdatedDoc = exports.getMongoUpdates = exports.dateReviver = exports.deepCopy = exports.floorPrecision = exports.roundPrecision = exports.throwErr = exports.buildRbLookups = exports.buildRbSchema = void 0;
13
13
  var rusDiff = require('rus-diff');
14
14
  var deepDiff = require('deep-diff').diff;
15
15
  var deep_object_diff_1 = require("deep-object-diff");
@@ -195,58 +195,46 @@ function mergeUpdates() {
195
195
  $push: {},
196
196
  $pull: {}
197
197
  };
198
- updatesToMerge.filter(function (a) { return a; }).forEach(function (anUpdate) {
198
+ updatesToMerge.filter(Boolean).forEach(function (anUpdate) {
199
199
  anUpdate.forEach(function (change) {
200
- var path = change.path[0];
201
- for (var i = 1; i < change.path.length; i++) {
202
- path += '.' + change.path[i];
203
- }
204
- if (change.kind === 'E') {
205
- if (typeof (change.rhs) === 'number' && path.endsWith('quantity')) {
206
- if (merged.$inc[path]) {
207
- merged.$inc[path] += change.rhs - change.lhs;
208
- }
209
- else {
210
- merged.$inc[path] = change.rhs - change.lhs;
211
- }
212
- }
213
- else {
214
- merged.$set[path] = change.rhs;
215
- }
216
- }
217
- else if (change.kind === 'A') {
218
- if (change.item.kind === 'N') {
219
- if (!merged.$push[path] || JSON.stringify(merged.$push[path]) !== JSON.stringify(change.item.rhs)) {
220
- if (!merged.$push[path]) {
221
- merged.$push[path] = [];
222
- }
223
- if (!merged.$push[path].filter(function (a) { return JSON.stringify(a) === JSON.stringify(change.item.rhs); }).length) {
224
- merged.$push[path].push(change.item.rhs);
225
- }
226
- }
227
- }
228
- else if (change.item.kind === 'D') {
229
- if (!merged.$pull[path] || JSON.stringify(merged.$pull[path]) !== JSON.stringify(change.item.lhs)) {
230
- if (!merged.$pull[path]) {
231
- merged.$pull[path] = [];
232
- }
233
- if (!merged.$pull[path].filter(function (a) { return JSON.stringify(a) === JSON.stringify(change.item.lhs); }).length) {
234
- merged.$pull[path].push(change.item.lhs);
235
- }
236
- }
237
- }
238
- else if (change.item.kind === 'E') {
239
- console.log('Condition E not satisfied.');
240
- // if (!merged.$pull[path] || JSON.stringify(merged.$pull[path]) !== JSON.stringify(change.item.lhs)) {
241
- // merged.$pull[path] = change.item.lhs;
242
- // }
243
- }
200
+ var path = change.path.join('.');
201
+ switch (change.kind) {
202
+ case 'E':
203
+ handleEditedChange(merged, path, change);
204
+ break;
205
+ case 'A':
206
+ handleArrayChange(merged, path, change);
207
+ break;
208
+ default:
209
+ break;
244
210
  }
245
211
  });
246
212
  });
247
213
  return merged;
248
214
  }
249
215
  exports.mergeUpdates = mergeUpdates;
216
+ function handleEditedChange(merged, path, change) {
217
+ if (typeof (change.rhs) === 'number' && (path.includes('quantity') || path.includes('qty'))) {
218
+ merged.$inc[path] = (merged.$inc[path] || 0) + change.rhs - change.lhs;
219
+ }
220
+ else {
221
+ merged.$set[path] = change.rhs;
222
+ }
223
+ }
224
+ function handleArrayChange(merged, path, change) {
225
+ if (change.item.kind === 'N') {
226
+ merged.$push[path] = merged.$push[path] || [];
227
+ if (!merged.$push[path].some(function (a) { return JSON.stringify(a) === JSON.stringify(change.item.rhs); })) {
228
+ merged.$push[path].push(change.item.rhs);
229
+ }
230
+ }
231
+ else if (change.item.kind === 'D') {
232
+ merged.$pull[path] = merged.$pull[path] || [];
233
+ if (!merged.$pull[path].some(function (a) { return JSON.stringify(a) === JSON.stringify(change.item.lhs); })) {
234
+ merged.$pull[path].push(change.item.lhs);
235
+ }
236
+ }
237
+ }
250
238
  function getDeepDiff(doc1, doc2) {
251
239
  delete doc1._id;
252
240
  delete doc1.__v;
@@ -267,10 +255,167 @@ function getDeepDiff(doc1, doc2) {
267
255
  return deepDiff(doc1, doc2);
268
256
  }
269
257
  exports.getDeepDiff = getDeepDiff;
258
+ function applyMongoUpdate(uDoc, delta) {
259
+ var orig = deepCopy(uDoc);
260
+ if (delta) {
261
+ if (delta.$rename)
262
+ handleRename(uDoc, delta.$rename);
263
+ if (delta.$set)
264
+ handleSet(uDoc, delta.$set);
265
+ if (delta.$inc)
266
+ handleInc(uDoc, delta.$inc);
267
+ if (delta.$unset)
268
+ handleUnset(uDoc, delta.$unset);
269
+ if (delta.$pull)
270
+ handlePull(uDoc, orig, delta.$pull);
271
+ if (delta.$push)
272
+ handlePush(uDoc, delta.$push);
273
+ }
274
+ return uDoc;
275
+ }
276
+ exports.applyMongoUpdate = applyMongoUpdate;
277
+ function handleRename(uDoc, renameMap) {
278
+ for (var k in renameMap) {
279
+ var v = renameMap[k];
280
+ var _a = resolve(uDoc, k), o1 = _a[0], n1 = _a[1];
281
+ var _b = resolve(uDoc, v), o2 = _b[0], n2 = _b[1];
282
+ if (o1 && n1.length === 1) {
283
+ if (o2 && n2.length === 1) {
284
+ o2[n2[0]] = o1[n1[0]];
285
+ delete o1[n1[0]];
286
+ }
287
+ else {
288
+ logError(o2, n2, uDoc, v);
289
+ }
290
+ }
291
+ else {
292
+ logError(o1, n1, uDoc, k);
293
+ }
294
+ }
295
+ }
296
+ function handleSet(uDoc, setMap) {
297
+ for (var k in setMap) {
298
+ var v = setMap[k];
299
+ var _a = resolve(uDoc, k, { force: true }), o = _a[0], n = _a[1];
300
+ if (o && n.length === 1) {
301
+ o[n[0]] = v;
302
+ }
303
+ else {
304
+ logError(o, n, uDoc, k);
305
+ }
306
+ }
307
+ }
308
+ function handleInc(uDoc, incMap) {
309
+ for (var k in incMap) {
310
+ var v = incMap[k];
311
+ var _a = resolve(uDoc, k, { force: true }), o = _a[0], n = _a[1];
312
+ if (o && n.length === 1) {
313
+ o[n[0]] = (o[n[0]] || 0) + v;
314
+ }
315
+ else {
316
+ logError(o, n, uDoc, k);
317
+ }
318
+ }
319
+ }
320
+ function handleUnset(uDoc, unsetMap) {
321
+ for (var k in unsetMap) {
322
+ var _a = resolve(uDoc, k), o = _a[0], n = _a[1];
323
+ if (o && n.length === 1) {
324
+ delete o[n[0]];
325
+ }
326
+ else {
327
+ logError(o, n, uDoc, k);
328
+ }
329
+ }
330
+ }
331
+ function handlePull(uDoc, orig, pullMap) {
332
+ for (var pathk in pullMap) {
333
+ var pullValues = pullMap[pathk];
334
+ var _loop_1 = function (vk) {
335
+ var _a = resolve(uDoc, pathk), o = _a[0], n = _a[1];
336
+ var _b = resolve(orig, pathk), o2 = _b[0], n2 = _b[1];
337
+ if (o && n.length === 1) {
338
+ o[n[0]].splice(o2[n2[0]].findIndex(function (z) { return JSON.stringify(z) === JSON.stringify(vk); }), 1);
339
+ }
340
+ else {
341
+ logError(o, n, uDoc, pathk);
342
+ }
343
+ };
344
+ for (var _i = 0, pullValues_1 = pullValues; _i < pullValues_1.length; _i++) {
345
+ var vk = pullValues_1[_i];
346
+ _loop_1(vk);
347
+ }
348
+ }
349
+ }
350
+ function handlePush(uDoc, pushMap) {
351
+ for (var pathk in pushMap) {
352
+ var pushValues = pushMap[pathk];
353
+ for (var _i = 0, pushValues_1 = pushValues; _i < pushValues_1.length; _i++) {
354
+ var vk = pushValues_1[_i];
355
+ var _a = resolve(uDoc, pathk), o = _a[0], n = _a[1];
356
+ if (o && n.length === 1) {
357
+ o[n[0]].push(vk);
358
+ }
359
+ else {
360
+ logError(o, n, uDoc, pathk);
361
+ }
362
+ }
363
+ }
364
+ }
365
+ function logError(o, n, uDoc, key) {
366
+ console.log('Error', o + '/' + n + ' - couldn\'t resolve for ' + JSON.stringify(uDoc) + ' ' + key);
367
+ }
368
+ function resolve(a, path, options) {
369
+ if (options === void 0) { options = {}; }
370
+ var stack = arrize(path);
371
+ var last = [];
372
+ if (stack.length > 0) {
373
+ last.unshift(stack.pop());
374
+ }
375
+ var e = a;
376
+ while (stack.length > 0) {
377
+ var k = stack.shift();
378
+ if (e[k] !== void 0) {
379
+ e = e[k];
380
+ }
381
+ else {
382
+ if (!options.force) {
383
+ last.unshift(k);
384
+ last = stack.concat(last);
385
+ break;
386
+ }
387
+ var isNextNum = typeof stack[0] === 'number';
388
+ var isLastNum = stack.length === 0 && typeof last[0] === 'number';
389
+ e[k] = isNextNum || isLastNum ? [] : {};
390
+ e = e[k];
391
+ }
392
+ }
393
+ return [e, last];
394
+ }
395
+ function arrize(path, glue) {
396
+ if (glue === void 0) { glue = '.'; }
397
+ if (Array.isArray(path)) {
398
+ return path.slice();
399
+ }
400
+ if (path === void 0 || path === null || path === false || path === '') {
401
+ return [];
402
+ }
403
+ return path.toString().split(glue)
404
+ .map(function (e) {
405
+ if ([void 0, null, false, ''].includes(e)) {
406
+ return null;
407
+ }
408
+ return e.toString();
409
+ })
410
+ .filter(function (e) { return e !== null; });
411
+ }
270
412
  function deepDiffDetails(obj1, obj2) {
271
413
  var diff = (0, deep_object_diff_1.detailedDiff)(obj2, obj1);
272
414
  var diffString = '';
273
415
  var formatObjectDiff = function (obj1, obj2) {
416
+ if (typeof obj1 === 'string' && typeof obj2 === 'string') {
417
+ return "from: \"".concat(obj1, "\" to: \"").concat(obj2, "\"");
418
+ }
274
419
  if (!obj1 || !obj2)
275
420
  return '';
276
421
  return Object.keys(obj1)
@@ -305,200 +450,6 @@ function deepDiffDetails(obj1, obj2) {
305
450
  return diffString;
306
451
  }
307
452
  exports.deepDiffDetails = deepDiffDetails;
308
- function applyMongoUpdate(uDoc, delta) {
309
- var orig, k, pathk, vk, n, n1, n2, name, o, o1, o2, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9a, ref9b, ref10, ref11, ref12a, ref12b, ref13, v;
310
- orig = deepCopy(uDoc);
311
- if (delta != null) {
312
- if (delta.$rename != null) {
313
- ref = delta.$rename;
314
- // tslint:disable-next-line:forin
315
- for (k in ref) {
316
- v = ref[k];
317
- ref1 = resolve(uDoc, k), o1 = ref1[0], n1 = ref1[1];
318
- ref2 = resolve(uDoc, v), o2 = ref2[0], n2 = ref2[1];
319
- if ((o1 != null) && n1.length === 1) {
320
- if ((o2 != null) && n2.length === 1) {
321
- o2[n2[0]] = o1[n1[0]];
322
- delete o1[n1[0]];
323
- }
324
- else {
325
- // throw new Error(o2 + '/' + n2 + ' - couldn\'t resolve first for ' + uDoc + ' ' + v);
326
- console.log('Error', o2 + '/' + n2 + ' - couldn\'t resolve first for ' + uDoc + ' ' + v);
327
- }
328
- }
329
- else {
330
- // throw new Error(o1 + '/' + n1 + ' - couldn\'t resolve second for ' + uDoc + ' ' + k);
331
- console.log('Error', o1 + '/' + n1 + ' - couldn\'t resolve second for ' + uDoc + ' ' + k);
332
- }
333
- }
334
- }
335
- if (delta.$set != null) {
336
- ref3 = delta.$set;
337
- // tslint:disable-next-line:forin
338
- for (k in ref3) {
339
- v = ref3[k];
340
- ref4 = resolve(uDoc, k, {
341
- force: true
342
- }), o = ref4[0], n = ref4[1];
343
- if ((o != null) && n.length === 1) {
344
- o[n[0]] = v;
345
- }
346
- else {
347
- // throw new Error(o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
348
- console.log('Error', o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
349
- }
350
- }
351
- }
352
- if (delta.$inc != null) {
353
- ref5 = delta.$inc;
354
- // tslint:disable-next-line:forin
355
- for (k in ref5) {
356
- v = ref5[k];
357
- ref6 = resolve(uDoc, k, {
358
- force: true
359
- }), o = ref6[0], n = ref6[1];
360
- if ((o != null) && n.length === 1) {
361
- if (o[name = n[0]] == null) {
362
- o[name] = 0;
363
- }
364
- o[n[0]] += v;
365
- }
366
- else {
367
- // throw new Error(o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
368
- console.log('Error', o + '/' + n + ' - couldn\'t set for ' + uDoc + ' ' + k);
369
- }
370
- }
371
- }
372
- if (delta.$unset != null) {
373
- ref7 = delta.$unset;
374
- // tslint:disable-next-line:forin
375
- for (k in ref7) {
376
- v = ref7[k];
377
- ref8 = resolve(uDoc, k), o = ref8[0], n = ref8[1];
378
- if ((o != null) && n.length === 1) {
379
- delete o[n[0]];
380
- }
381
- else {
382
- // throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + k);
383
- console.log('Error', o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + k);
384
- }
385
- }
386
- }
387
- if (delta.$pull != null) {
388
- ref9a = delta.$pull;
389
- // tslint:disable-next-line:forin
390
- for (pathk in ref9a) { // should only loop 1 time
391
- ref9b = ref9a[pathk];
392
- for (var kk = (ref9b.length - 1); kk >= 0; kk--) {
393
- vk = ref9b[kk];
394
- ref10 = resolve(uDoc, pathk), o = ref10[0], n = ref10[1];
395
- ref11 = resolve(orig, pathk), o2 = ref11[0], n2 = ref11[1];
396
- if ((o != null) && n.length === 1) {
397
- o[n[0]].splice(o2[n2[0]].findIndex(function (z) { return JSON.stringify(z) === JSON.stringify(vk); }), 1);
398
- }
399
- else {
400
- // throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
401
- console.log('Error', o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
402
- }
403
- }
404
- }
405
- }
406
- if (delta.$push != null) {
407
- ref12a = delta.$push;
408
- // tslint:disable-next-line:forin
409
- for (pathk in ref12a) { // should only loop 1 time
410
- ref12b = ref12a[pathk];
411
- // for (let kk = 0; kk < ref12b.length; kk++) {
412
- for (var kk = (ref12b.length - 1); kk >= 0; kk--) {
413
- vk = ref12b[kk];
414
- ref13 = resolve(uDoc, pathk), o = ref13[0], n = ref13[1];
415
- if ((o != null) && n.length === 1) {
416
- o[n[0]].push(vk);
417
- }
418
- else {
419
- // throw new Error(o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
420
- console.log('Error', o + '/' + n + ' - couldn\'t unset for ' + uDoc + ' ' + pathk);
421
- }
422
- }
423
- }
424
- }
425
- }
426
- return uDoc;
427
- }
428
- exports.applyMongoUpdate = applyMongoUpdate;
429
- function resolve(a, path, options) {
430
- var e, k, last, stack;
431
- if (options == null) {
432
- options = {};
433
- }
434
- stack = arrize(path);
435
- last = [];
436
- if (stack.length > 0) {
437
- last.unshift(stack.pop());
438
- }
439
- e = a;
440
- if (e !== null) {
441
- while ((k = stack.shift()) !== void 0) {
442
- if (e[k] !== void 0) {
443
- e = e[k];
444
- }
445
- else {
446
- stack.unshift(k);
447
- break;
448
- }
449
- }
450
- }
451
- if (options.force) {
452
- while ((k = stack.shift()) !== void 0) {
453
- if ((typeof stack[0] === 'number') || ((stack.length === 0) && (typeof last[0] === 'number'))) {
454
- e[k] = [];
455
- }
456
- else {
457
- e[k] = {};
458
- }
459
- e = e[k];
460
- }
461
- }
462
- else {
463
- while ((k = stack.pop()) !== void 0) {
464
- last.unshift(k);
465
- }
466
- }
467
- return [e, last];
468
- }
469
- function arrize(path, glue) {
470
- if (glue == null) {
471
- glue = '.';
472
- }
473
- return ((function () {
474
- if (Array.isArray(path)) {
475
- return path.slice(0);
476
- }
477
- else {
478
- switch (path) {
479
- case void 0:
480
- case null:
481
- case false:
482
- case '':
483
- return [];
484
- default:
485
- return path.toString().split(glue);
486
- }
487
- }
488
- })()).map(function (e) {
489
- switch (e) {
490
- case void 0:
491
- case null:
492
- case false:
493
- case '':
494
- return null;
495
- default:
496
- return e.toString();
497
- }
498
- }).filter(function (e) {
499
- return e != null;
500
- });
501
- }
502
453
  function getBinarySize(string) {
503
454
  if (string) {
504
455
  return Buffer.byteLength(string, 'utf8');