@cqse/commons 0.1.0-beta.8 → 1.0.0-beta.2

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.
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConfigurationWithOverwrites = void 0;
4
+ exports.makeConfigProxy = makeConfigProxy;
5
+ const Contract_1 = require("./Contract");
6
+ const ConfigParser_1 = require("./ConfigParser");
7
+ const node_crypto_1 = require("node:crypto");
8
+ /**
9
+ * Configuration with a possibility to overwrite configuration values for this app run,
10
+ * delegating to the base configuration if no overwrite was performed.
11
+ * For type-safe access, use `makeConfigProxy` to create a proxy object.
12
+ */
13
+ class ConfigurationWithOverwrites {
14
+ constructor(allParameters, redefinableParameters, baseConfiguration, overwrites) {
15
+ this.baseConfiguration = Contract_1.Contract.requireDefined(baseConfiguration);
16
+ this.redefinableParameters = Contract_1.Contract.requireDefined(redefinableParameters);
17
+ this.allParameters = Contract_1.Contract.requireDefined(allParameters);
18
+ this.overwrites = overwrites !== null && overwrites !== void 0 ? overwrites : {};
19
+ this.hash = undefined;
20
+ }
21
+ /**
22
+ * Retrieves the value of a configuration parameter based on the provided parameter ID.
23
+ */
24
+ get(parameterId) {
25
+ var _a;
26
+ this.assertParameterExists(parameterId);
27
+ return (_a = this.overwrites[parameterId]) !== null && _a !== void 0 ? _a : this.baseConfiguration[parameterId];
28
+ }
29
+ /**
30
+ * Sets the specified parameter with a given value in the `overwrites` map.
31
+ */
32
+ set(parameterId, value) {
33
+ if (this.redefinableParameters.lookupParameter(parameterId) === undefined) {
34
+ throw new Error(`Unknown configuration parameter: ${parameterId}`);
35
+ }
36
+ this.overwriteConfig(parameterId, value);
37
+ this.hash = undefined;
38
+ }
39
+ /**
40
+ * Updates or removes a configuration-overwrite for the given parameter.
41
+ */
42
+ overwriteConfig(parameter, value) {
43
+ const parameterId = (0, ConfigParser_1.parameterNameToParameterId)(parameter);
44
+ this.assertParameterExists(parameterId);
45
+ if (value === undefined) {
46
+ delete this.overwrites[parameterId];
47
+ return;
48
+ }
49
+ this.overwrites[parameterId] = this.castConfigArgument(parameterId, value);
50
+ this.hash = undefined;
51
+ }
52
+ castConfigArgument(parameterId, value) {
53
+ const parameter = this.getParameter(parameterId);
54
+ const type = parameter.type;
55
+ if (type === 'int') {
56
+ if (typeof value === 'number') {
57
+ return Math.floor(value);
58
+ }
59
+ if (typeof value === 'string') {
60
+ return parseInt(value, 10);
61
+ }
62
+ if (typeof value === 'boolean') {
63
+ return value ? 1 : 0;
64
+ }
65
+ }
66
+ else if (type === 'bool') {
67
+ if (typeof value === 'boolean') {
68
+ return value;
69
+ }
70
+ if (typeof value === 'string') {
71
+ return value.toLowerCase() === 'true';
72
+ }
73
+ if (typeof value === 'number') {
74
+ return value !== 0;
75
+ }
76
+ }
77
+ else if (type === 'string[]') {
78
+ if (value === undefined || value == null) {
79
+ return [];
80
+ }
81
+ if (Array.isArray(value)) {
82
+ return value.map(String);
83
+ }
84
+ if (typeof value === 'string') {
85
+ return value.split(',').map(s => s.trim());
86
+ }
87
+ }
88
+ else {
89
+ // string type
90
+ return String(value);
91
+ }
92
+ throw new Error(`Cannot cast value of type ${typeof value} to ${type}`);
93
+ }
94
+ assertParameterExists(parameterId) {
95
+ if (this.allParameters.lookupParameter(parameterId) === undefined) {
96
+ throw new Error(`Unknown configuration parameter: ${parameterId}`);
97
+ }
98
+ }
99
+ getParameter(parameterId) {
100
+ const parameter = this.redefinableParameters.lookupParameter(parameterId);
101
+ if (parameter === undefined) {
102
+ throw new Error(`Unknown configuration parameter: ${parameterId}`);
103
+ }
104
+ return parameter;
105
+ }
106
+ /**
107
+ * Computes a hash for the given (possibly overwritten) configuration.
108
+ */
109
+ getHash() {
110
+ if (this.hash === undefined) {
111
+ const hash = (0, node_crypto_1.createHash)('sha256');
112
+ for (const [key, value] of Object.entries(this.baseConfiguration)) {
113
+ hash.update(key);
114
+ hash.update(String(value));
115
+ }
116
+ for (const [key, value] of Object.entries(this.overwrites)) {
117
+ hash.update(key);
118
+ hash.update(String(value));
119
+ }
120
+ this.hash = hash.digest('hex');
121
+ }
122
+ return this.hash;
123
+ }
124
+ /**
125
+ * Copies the given configuration along with the overrides.
126
+ */
127
+ copy() {
128
+ const overwritesCopy = JSON.parse(JSON.stringify(this.overwrites));
129
+ return new ConfigurationWithOverwrites(this.allParameters, this.redefinableParameters, this.baseConfiguration, overwritesCopy);
130
+ }
131
+ }
132
+ exports.ConfigurationWithOverwrites = ConfigurationWithOverwrites;
133
+ /**
134
+ * Creates a proxy object to retrieve and set config options via the attributes in the `BaseConfigType` interface.
135
+ */
136
+ function makeConfigProxy(configuration) {
137
+ return new Proxy(configuration, {
138
+ get(_, property) {
139
+ return configuration.get(property.toString());
140
+ },
141
+ set(target, property, value) {
142
+ configuration.set(property.toString(), value);
143
+ return true;
144
+ }
145
+ });
146
+ }
147
+ //# sourceMappingURL=ConfigWithOverwrites.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ConfigWithOverwrites.js","sourceRoot":"","sources":["../src/ConfigWithOverwrites.ts"],"names":[],"mappings":";;;AA+JA,0CAWC;AA1KD,yCAAsC;AACtC,iDAMwB;AACxB,6CAAyC;AAEzC;;;;GAIG;AACH,MAAa,2BAA2B;IAYvC,YAAY,aAAsC,EAAE,qBAA8C,EAC/F,iBAAkD,EAAE,UAA4C;QAClG,IAAI,CAAC,iBAAiB,GAAG,mBAAQ,CAAC,cAAc,CAAC,iBAAiB,CAAC,CAAC;QACpE,IAAI,CAAC,qBAAqB,GAAG,mBAAQ,CAAC,cAAc,CAAC,qBAAqB,CAAC,CAAC;QAC5E,IAAI,CAAC,aAAa,GAAG,mBAAQ,CAAC,cAAc,CAAC,aAAa,CAAC,CAAC;QAC5D,IAAI,CAAC,UAAU,GAAG,UAAU,aAAV,UAAU,cAAV,UAAU,GAAI,EAAmD,CAAC;QACpF,IAAI,CAAC,IAAI,GAAG,SAAS,CAAC;IACv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AAC,KAAK,CAAC,CAAC;QAChC,CAAC;QAED,OAAO,IAAI,CAAC,IAAI,CAAC;IAClB,CAAC;IAED;;OAEG;IACI,IAAI;QACV,MAAM,cAAc,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;QACnE,OAAO,IAAI,2BAA2B,CAAiB,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,qBAAqB,EAAE,IAAI,CAAC,iBAAiB,EAC5H,cAAc,CAAC,CAAC;IAClB,CAAC;CAED;AA3ID,kEA2IC;AAED;;GAEG;AACH,SAAgB,eAAe,CAC9B,aAA0D;IAC1D,OAAO,IAAI,KAAK,CAAC,aAAa,EAAE;QAC/B,GAAG,CAAC,CAAC,EAAE,QAAyB;YAC/B,OAAO,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC,CAAC;QAC/C,CAAC;QACD,GAAG,CAAC,MAAM,EAAE,QAAyB,EAAE,KAAsB;YAC5D,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,EAAE,KAAK,CAAC,CAAC;YAC9C,OAAO,IAAI,CAAC;QACb,CAAC;KACD,CAA8B,CAAC;AACjC,CAAC"}
package/lib/Strings.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removePrefix = void 0;
3
+ exports.removePrefix = removePrefix;
4
4
  /**
5
5
  * Remove the given prefix, if present, from the given string.
6
6
  *
@@ -15,5 +15,4 @@ function removePrefix(prefix, removeFrom) {
15
15
  }
16
16
  return removeFrom;
17
17
  }
18
- exports.removePrefix = removePrefix;
19
18
  //# sourceMappingURL=Strings.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Strings.js","sourceRoot":"","sources":["../src/Strings.ts"],"names":[],"mappings":";;;AAAA;;;;;;;GAOG;AACH,SAAgB,YAAY,CAAC,MAAc,EAAE,UAAkB;IAC9D,IAAI,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QACnC,OAAO,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,UAAU,CAAC;AACnB,CAAC;AALD,oCAKC"}
1
+ {"version":3,"file":"Strings.js","sourceRoot":"","sources":["../src/Strings.ts"],"names":[],"mappings":";;AAQA,oCAKC;AAbD;;;;;;;GAOG;AACH,SAAgB,YAAY,CAAC,MAAc,EAAE,UAAkB;IAC9D,IAAI,UAAU,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QACnC,OAAO,UAAU,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;IAC5C,CAAC;IACD,OAAO,UAAU,CAAC;AACnB,CAAC"}
package/lib/index.d.ts CHANGED
@@ -2,4 +2,7 @@
2
2
  export * from './Contract';
3
3
  export * from './Exceptions';
4
4
  export * from './Strings';
5
+ export * from './ConfigParser';
6
+ export * from './ConfigWithOverwrites';
7
+ export * from './CollectorConfig';
5
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC"}
package/lib/index.js CHANGED
@@ -18,4 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
18
18
  __exportStar(require("./Contract"), exports);
19
19
  __exportStar(require("./Exceptions"), exports);
20
20
  __exportStar(require("./Strings"), exports);
21
+ __exportStar(require("./ConfigParser"), exports);
22
+ __exportStar(require("./ConfigWithOverwrites"), exports);
23
+ __exportStar(require("./CollectorConfig"), exports);
21
24
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA4C;AAC5C,6CAA2B;AAC3B,+CAA6B;AAC7B,4CAA0B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4CAA4C;AAC5C,6CAA2B;AAC3B,+CAA6B;AAC7B,4CAA0B;AAC1B,iDAA+B;AAC/B,yDAAuC;AACvC,oDAAkC"}
package/package.json CHANGED
@@ -1,14 +1,11 @@
1
1
  {
2
2
  "name": "@cqse/commons",
3
- "version": "0.1.0-beta.8",
3
+ "version": "1.0.0-beta.2",
4
4
  "description": "A collection of generic functionality for TypeScript projects.",
5
5
  "author": "CQSE GmbH",
6
6
  "main": "lib/index.js",
7
7
  "license": "Apache-2.0",
8
- "repository": {
9
- "type": "git",
10
- "url": "https://github.com/cqse/teamscale-javascript-profiler.git"
11
- },
8
+ "homepage": "https://docs.teamscale.com/howto/setting-up-profiler-tga/javascript/",
12
9
  "files": [
13
10
  "dist/**/*",
14
11
  "lib/**/*"
@@ -17,16 +14,16 @@
17
14
  "typescript-optional": "^2.0.1"
18
15
  },
19
16
  "devDependencies": {
20
- "@babel/core": "^7.24.4",
21
- "@babel/preset-env": "^7.24.4",
22
- "@types/jest": "^29.5.12",
23
- "@types/node": "^20.12.6",
17
+ "@babel/core": "^7.26.8",
18
+ "@babel/preset-env": "^7.26.8",
19
+ "@types/jest": "^29.5.14",
20
+ "@types/node": "^22.13.4",
24
21
  "babel-jest": "^29.7.0",
25
22
  "jest": "^29.7.0",
26
- "rimraf": "^5.0.5",
27
- "ts-jest": "^29.1.2",
23
+ "rimraf": "^6.0.1",
24
+ "ts-jest": "^29.2.5",
28
25
  "ts-node": "^10.9.2",
29
- "typescript": "^5.4.4"
26
+ "typescript": "^5.7.3"
30
27
  },
31
28
  "publishConfig": {
32
29
  "access": "public"