@squiz/optimization-utils 4.4.0 → 4.6.0

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/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './types/class-utilities-types';
2
+ export * from './retries/Retries';
2
3
  export * from './object/getProperty';
4
+ export * from './metadata/CopyReflection';
3
5
  export * from './measurement/performance';
4
6
  export * from './data-structures/Result';
5
7
  export * from './change-tracker/ChangeTracker';
package/dist/index.js CHANGED
@@ -15,7 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./types/class-utilities-types"), exports);
18
+ __exportStar(require("./retries/Retries"), exports);
18
19
  __exportStar(require("./object/getProperty"), exports);
20
+ __exportStar(require("./metadata/CopyReflection"), exports);
19
21
  __exportStar(require("./measurement/performance"), exports);
20
22
  __exportStar(require("./data-structures/Result"), exports);
21
23
  __exportStar(require("./change-tracker/ChangeTracker"), exports);
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gEAA8C;AAC9C,uDAAqC;AACrC,4DAA0C;AAC1C,2DAAyC;AACzC,iEAA+C;AAC/C,mDAAiC;AACjC,iDAA+B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gEAA8C;AAC9C,oDAAkC;AAClC,uDAAqC;AACrC,4DAA0C;AAC1C,4DAA0C;AAC1C,2DAAyC;AACzC,iEAA+C;AAC/C,mDAAiC;AACjC,iDAA+B"}
@@ -0,0 +1,5 @@
1
+ import 'reflect-metadata';
2
+ export declare class CopyReflection {
3
+ static copyMetadata(source: object, target: object): void;
4
+ static copyPropertyMetadata(source: object, target: object, propertyKey: string | symbol): void;
5
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CopyReflection = void 0;
4
+ require("reflect-metadata");
5
+ class CopyReflection {
6
+ static copyMetadata(source, target) {
7
+ const metadataKeys = Reflect.getMetadataKeys(source);
8
+ metadataKeys.forEach((key) => {
9
+ const metadata = Reflect.getMetadata(key, source);
10
+ Reflect.defineMetadata(key, metadata, target);
11
+ });
12
+ }
13
+ static copyPropertyMetadata(source, target, propertyKey) {
14
+ const metadataKeys = Reflect.getMetadataKeys(source, propertyKey);
15
+ metadataKeys.forEach((key) => {
16
+ const metadata = Reflect.getMetadata(key, source, propertyKey);
17
+ Reflect.defineMetadata(key, metadata, target, propertyKey);
18
+ });
19
+ }
20
+ }
21
+ exports.CopyReflection = CopyReflection;
22
+ //# sourceMappingURL=CopyReflection.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopyReflection.js","sourceRoot":"","sources":["../../src/metadata/CopyReflection.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAE1B,MAAa,cAAc;IAClB,MAAM,CAAC,YAAY,CAAC,MAAc,EAAE,MAAc;QACvD,MAAM,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;QAErD,YAAY,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC3B,MAAM,QAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;YAElD,OAAO,CAAC,cAAc,CAAC,GAAG,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;QAChD,CAAC,CAAC,CAAC;IACL,CAAC;IAEM,MAAM,CAAC,oBAAoB,CAChC,MAAc,EACd,MAAc,EACd,WAA4B;QAE5B,MAAM,YAAY,GAAG,OAAO,CAAC,eAAe,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;QAElE,YAAY,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC3B,MAAM,QAAQ,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;YAE/D,OAAO,CAAC,cAAc,CAAC,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;QAC7D,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAxBD,wCAwBC"}
@@ -0,0 +1,5 @@
1
+ export declare function Retry({ delay, retries, retryableErrors, }: {
2
+ retries: number;
3
+ delay: number;
4
+ retryableErrors: Array<new (...args: Array<any>) => Error>;
5
+ }): MethodDecorator;
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Retry = Retry;
4
+ const promises_1 = require("timers/promises");
5
+ const inversify_1 = require("inversify");
6
+ const optimization_logger_1 = require("@squiz/optimization-logger");
7
+ const CopyReflection_1 = require("../metadata/CopyReflection");
8
+ function Retry({ delay, retries, retryableErrors, }) {
9
+ return (target, propertyKey, descriptor) => {
10
+ const injection = (0, inversify_1.inject)(optimization_logger_1.Logger);
11
+ const loggerSymbol = Symbol('logger');
12
+ injection(target, loggerSymbol);
13
+ const originalMethod = descriptor.value;
14
+ descriptor.value = async function (...args) {
15
+ const logger = this[loggerSymbol] ||
16
+ (0, optimization_logger_1.createLogger)();
17
+ let attempt = 0;
18
+ while (attempt <= retries) {
19
+ try {
20
+ return await originalMethod.apply(this, args);
21
+ }
22
+ catch (error) {
23
+ logger.debug({
24
+ message: `Attempt ${attempt} of ${retries}`,
25
+ });
26
+ attempt++;
27
+ if (!retryableErrors.some((errType) => error instanceof errType)) {
28
+ logger.error({
29
+ message: `Non-retryable error encountered`,
30
+ error,
31
+ });
32
+ throw error;
33
+ }
34
+ const isMaxRetriesReached = attempt > retries;
35
+ if (isMaxRetriesReached) {
36
+ logger.error({
37
+ message: `Max retries (${retries}) reached`,
38
+ error,
39
+ });
40
+ throw error;
41
+ }
42
+ const _delay = delay * Math.pow(2, attempt - 1);
43
+ logger.debug({
44
+ message: `Retrying in ${_delay}ms`,
45
+ attempt,
46
+ delay: _delay,
47
+ });
48
+ await (0, promises_1.setTimeout)(_delay);
49
+ }
50
+ }
51
+ };
52
+ CopyReflection_1.CopyReflection.copyPropertyMetadata(originalMethod, descriptor.value, propertyKey);
53
+ };
54
+ }
55
+ //# sourceMappingURL=Retries.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Retries.js","sourceRoot":"","sources":["../../src/retries/Retries.ts"],"names":[],"mappings":";;AAKA,sBAyEC;AA9ED,8CAA6C;AAC7C,yCAAmC;AACnC,oEAAkE;AAClE,+DAA4D;AAE5D,SAAgB,KAAK,CAAC,EACpB,KAAK,EACL,OAAO,EACP,eAAe,GAMhB;IACC,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,UAA8B,EAAQ,EAAE;QACnE,MAAM,SAAS,GAAG,IAAA,kBAAM,EAAC,4BAAM,CAAC,CAAC;QACjC,MAAM,YAAY,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;QAEtC,SAAS,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;QAEhC,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAExC,UAAU,CAAC,KAAK,GAAG,KAAK,WACtB,GAAG,IAAoB;YAEvB,MAAM,MAAM,GACT,IAA+C,CAAC,YAAY,CAAC;gBAC9D,IAAA,kCAAY,GAAE,CAAC;YAEjB,IAAI,OAAO,GAAG,CAAC,CAAC;YAEhB,OAAO,OAAO,IAAI,OAAO,EAAE,CAAC;gBAC1B,IAAI,CAAC;oBACH,OAAO,MAAM,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBAChD,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,MAAM,CAAC,KAAK,CAAC;wBACX,OAAO,EAAE,WAAW,OAAO,OAAO,OAAO,EAAE;qBAC5C,CAAC,CAAC;oBACH,OAAO,EAAE,CAAC;oBAEV,IAAI,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,KAAK,YAAY,OAAO,CAAC,EAAE,CAAC;wBACjE,MAAM,CAAC,KAAK,CAAC;4BACX,OAAO,EAAE,iCAAiC;4BAC1C,KAAK;yBACN,CAAC,CAAC;wBACH,MAAM,KAAK,CAAC;oBACd,CAAC;oBAED,MAAM,mBAAmB,GAAG,OAAO,GAAG,OAAO,CAAC;oBAE9C,IAAI,mBAAmB,EAAE,CAAC;wBACxB,MAAM,CAAC,KAAK,CAAC;4BACX,OAAO,EAAE,gBAAgB,OAAO,WAAW;4BAC3C,KAAK;yBACN,CAAC,CAAC;wBACH,MAAM,KAAK,CAAC;oBACd,CAAC;oBAED,MAAM,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;oBAEhD,MAAM,CAAC,KAAK,CAAC;wBACX,OAAO,EAAE,eAAe,MAAM,IAAI;wBAClC,OAAO;wBACP,KAAK,EAAE,MAAM;qBACd,CAAC,CAAC;oBAEH,MAAM,IAAA,qBAAU,EAAC,MAAM,CAAC,CAAC;gBAC3B,CAAC;YACH,CAAC;QACH,CAAC,CAAC;QAEF,+BAAc,CAAC,oBAAoB,CACjC,cAAc,EACd,UAAU,CAAC,KAAK,EAChB,WAAW,CACZ,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squiz/optimization-utils",
3
- "version": "4.4.0",
3
+ "version": "4.6.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",