@typeix/metadata 9.0.0-rc.1 → 9.1.0-rc.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.
package/.eslintrc.js CHANGED
@@ -33,23 +33,23 @@ module.exports = {
33
33
  "accessibility": "explicit"
34
34
  }
35
35
  ],
36
- "@typescript-eslint/indent": [
37
- "error",
38
- 2
39
- ],
40
- "@typescript-eslint/member-delimiter-style": [
36
+ "indent": [
41
37
  "error",
38
+ 2,
42
39
  {
43
- "multiline": {
44
- "delimiter": "semi",
45
- "requireLast": true
46
- },
47
- "singleline": {
48
- "delimiter": "semi",
49
- "requireLast": false
50
- }
40
+ "SwitchCase": 1,
41
+ "flatTernaryExpressions": false,
42
+ "ignoredNodes": [
43
+ "PropertyDefinition[decorators]",
44
+ "ClassProperty[decorators]",
45
+ "PropertyDefinition",
46
+ "ClassProperty",
47
+ "TSUnionType",
48
+ "FunctionExpression[params]:has(Identifier[decorators])"
49
+ ]
51
50
  }
52
51
  ],
52
+
53
53
  "@typescript-eslint/member-ordering": "error",
54
54
  "@typescript-eslint/naming-convention": [
55
55
  "error",
@@ -71,18 +71,17 @@ module.exports = {
71
71
  "@typescript-eslint/no-unused-expressions": "error",
72
72
  "@typescript-eslint/no-unused-vars": "error",
73
73
  "@typescript-eslint/no-use-before-define": "warn",
74
- "@typescript-eslint/quotes": [
74
+ "quotes": [
75
75
  "error",
76
76
  "double",
77
77
  {
78
78
  "avoidEscape": true
79
79
  }
80
80
  ],
81
- "@typescript-eslint/semi": [
81
+ "semi": [
82
82
  "error",
83
83
  "always"
84
84
  ],
85
- "@typescript-eslint/type-annotation-spacing": "error",
86
85
  "array-bracket-spacing": "off",
87
86
  "brace-style": [
88
87
  "error",
@@ -101,7 +100,10 @@ module.exports = {
101
100
  "max-len": [
102
101
  "error",
103
102
  {
104
- "code": 140
103
+ "code": 140,
104
+ "ignoreComments": true,
105
+ "ignoreUrls": true,
106
+ "ignoreRegExpLiterals": true
105
107
  }
106
108
  ],
107
109
  "no-bitwise": "off",
@@ -158,5 +160,16 @@ module.exports = {
158
160
  ]
159
161
  }
160
162
  ]
161
- }
163
+ },
164
+ "overrides": [
165
+ {
166
+ "files": ["**/*-spec.ts", "**/*.spec.ts"],
167
+ "rules": {
168
+ "@typescript-eslint/no-unused-vars": "off",
169
+ "@typescript-eslint/no-empty-function": "off",
170
+ "max-len": "off",
171
+ "no-shadow": "off"
172
+ }
173
+ }
174
+ ]
162
175
  };
package/README.md CHANGED
@@ -12,7 +12,7 @@ tools for building efficient and scalable applications.
12
12
  # @typeix/metadata
13
13
  Metadata API for typescript decorators
14
14
 
15
- [![Build Status][travis-img]][travis-url]
15
+ [![Build Status][actions-img]][actions-url]
16
16
  [![Coverage Status][coverage-img]][coverage-url]
17
17
  ![npm][npm-version-img]
18
18
 
@@ -131,8 +131,107 @@ eg.:
131
131
  ```
132
132
 
133
133
 
134
- [travis-url]: https://circleci.com/gh/typeix/typeix
135
- [travis-img]: https://img.shields.io/circleci/build/github/typeix/typeix/main
136
- [npm-version-img]: https://img.shields.io/npm/v/@typeix/resty
134
+ ## Caching and invalidation
135
+
136
+ `getAllMetadataForTarget` resolves the whole prototype chain, so its result is cached per class
137
+ in a `WeakMap`. The cache is validated against a metadata revision counter that is bumped by
138
+ `defineMetadata` and `deleteMetadata`, which means a decorator applied after a scan is visible on
139
+ the next scan instead of being served stale.
140
+
141
+ ```js
142
+ import {getMetadataVersion, clearMetadataCache, getAllMetadataForTarget} from "@typeix/metadata";
143
+
144
+ getAllMetadataForTarget(User); // scanned and cached
145
+ getMetadataVersion(); // revision the cache entries were built from
146
+ clearMetadataCache(); // force every cached scan to be rebuilt
147
+ ```
148
+
149
+ Two limits are worth knowing:
150
+
151
+ - **Discovery enumerates own prototype keys.** Metadata written for a property that is not
152
+ declared on the class cannot be found, because the key is never enumerated. Declaring the
153
+ property makes it discoverable.
154
+ - **The cache returns a copy of the record array**, so a caller can reorder or truncate it
155
+ without corrupting the cache. The records themselves are shared objects.
156
+
157
+ The chain walk stops at `Function.prototype` and `Object.prototype`, which cannot carry
158
+ typeix decorators. This removes most of the `Reflect.getMetadataKeys` calls on a plain class.
159
+
160
+ ## API
161
+
162
+ ### Creating decorators
163
+
164
+ | Export | Purpose |
165
+ |---|---|
166
+ | `createClassDecorator(fn, args)` | class decorator |
167
+ | `createMethodDecorator(fn, args)` | method decorator, repeatable per method |
168
+ | `createPropertyDecorator(fn, args)` | property decorator |
169
+ | `createParameterDecorator(fn, args)` | parameter decorator, keyed by parameter index |
170
+ | `createParameterAndPropertyDecorator(fn, args)` | both, for a decorator usable in either position |
171
+
172
+ ### Reading metadata
173
+
174
+ | Export | Purpose |
175
+ |---|---|
176
+ | `getMetadata(key, target, targetKey?)` | one metadata value by key |
177
+ | `getOwnMetadata(key, target, targetKey?)` | same, without the prototype chain |
178
+ | `getMetadataKeys(target, targetKey?)` | keys of a target |
179
+ | `getOwnMetadataKeys(target, targetKey?)` | own keys only |
180
+ | `hasMetadata(key, target, targetKey?)` | includes inherited |
181
+ | `hasOwnMetadata(key, target, targetKey?)` | own only |
182
+ | `getClassMetadata(decorator, Class)` | a class decorator's arguments, throws if the decorator was never used |
183
+ | `getPropertyMetadata(decorator, target, key)` | property decorator arguments |
184
+ | `getMethodMetadata(decorator, target, key)` | method decorator arguments |
185
+ | `getParameterMetadata(decorator, target, key)` | parameter decorator arguments |
186
+ | `getAllMetadataForTarget(Class)` | every record of a class and its ancestors |
187
+
188
+ ### Decorator identity
189
+
190
+ | Export | Purpose |
191
+ |---|---|
192
+ | `isDecorator(fn)` | has this decorator been initialised — guard `getClassMetadata` with it |
193
+ | `hasDecorator(fn)` | |
194
+ | `getDecoratorName(fn)` | |
195
+ | `getDecoratorType(fn)` | `"class"`, `"method"`, `"property"` or `"parameter"` |
196
+ | `getDecoratorId(fn)` / `getDecoratorUUID(fn)` | the unique id embedded in the metadata key |
197
+
198
+ ### Writing and invalidation
199
+
200
+ | Export | Purpose |
201
+ |---|---|
202
+ | `defineMetadata(key, value, target, targetKey?)` | low level write, bumps the revision |
203
+ | `getMetadataVersion()` | current metadata revision |
204
+ | `clearMetadataCache()` | force every cached scan to be rebuilt |
205
+
206
+ `deleteMetadata` and `getMetadataForTarget` / `getAllKeysForTarget` / `getAllMetadataKeysForTarget` exist on the
207
+ `./metadata` module but are not re-exported from the package root.
208
+
209
+ ### Rules the decorator factories enforce
210
+
211
+ - **Decorators must be named.** `createClassDecorator` throws for an anonymous function, because the name is part of
212
+ the metadata key.
213
+ - **`design:*` keys cannot be overwritten.** `defineMetadata` rejects `design:type`, `design:paramtypes` and
214
+ `design:returntype`.
215
+ - **Repeatable decorators are stored as an array.** Applying the same decorator twice to one method keeps both
216
+ records, so readers must flatten.
217
+ - **`getClassMetadata` throws when the decorator was never initialised** anywhere in the process. Guard it with
218
+ `isDecorator`.
219
+ - **Derived classes win on a `(metadataKey, propertyKey)` collision**, and inherited records are still returned.
220
+
221
+ ## Performance
222
+
223
+ Measured on Node 26 for a class with 7 decorated members:
224
+
225
+ | | keys looked up | time |
226
+ |---|---|---|
227
+ | one scan, cache miss | 5 `Reflect.getMetadataKeys` | ~2 µs |
228
+ | repeated scan, cache hit | 0 | ~0.04 µs |
229
+ | with one ancestor class | 12 | ~3 µs |
230
+
231
+ Deep inheritance chains stay cheap because the walk stops at the framework prototypes.
232
+
233
+ [actions-url]: https://github.com/typeix/typeix/actions/workflows/ci.yml
234
+ [actions-img]: https://github.com/typeix/typeix/actions/workflows/ci.yml/badge.svg
235
+ [npm-version-img]: https://img.shields.io/npm/v/@typeix/metadata
137
236
  [coverage-img]: https://coveralls.io/repos/github/typeix/typeix/badge.svg?branch=main
138
237
  [coverage-url]: https://coveralls.io/github/typeix/typeix?branch=main
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export { IMetadata, defineMetadata, getMetadata, getOwnMetadata, getMetadataKeys, getOwnMetadataKeys, hasOwnMetadata, hasMetadata, getDecoratorName, getDecoratorType, getDecoratorUUID, getDecoratorId, createClassDecorator, createPropertyDecorator, createParameterDecorator, createParameterAndPropertyDecorator, createMethodDecorator, hasDecorator, isDecorator, getClassMetadata, getPropertyMetadata, getMethodMetadata, getParameterMetadata, getMetadataKeysForTarget, getMetadataForTarget, getAllKeysForTarget, getAllMetadataKeysForTarget, getAllMetadataForTarget, TS_PARAMS, TS_RETURN, TS_TYPE } from "./metadata";
1
+ export { IMetadata, defineMetadata, getMetadata, getOwnMetadata, getMetadataKeys, getOwnMetadataKeys, hasOwnMetadata, hasMetadata, getDecoratorName, getDecoratorType, getDecoratorUUID, getDecoratorId, createClassDecorator, createPropertyDecorator, createParameterDecorator, createParameterAndPropertyDecorator, createMethodDecorator, hasDecorator, isDecorator, getClassMetadata, getPropertyMetadata, getMethodMetadata, getParameterMetadata, getMetadataKeysForTarget, getMetadataForTarget, getAllKeysForTarget, getAllMetadataKeysForTarget, getAllMetadataForTarget, getMetadataVersion, clearMetadataCache, TS_PARAMS, TS_RETURN, TS_TYPE } from "./metadata";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TS_TYPE = exports.TS_RETURN = exports.TS_PARAMS = exports.getAllMetadataForTarget = exports.getAllMetadataKeysForTarget = exports.getAllKeysForTarget = exports.getMetadataForTarget = exports.getMetadataKeysForTarget = exports.getParameterMetadata = exports.getMethodMetadata = exports.getPropertyMetadata = exports.getClassMetadata = exports.isDecorator = exports.hasDecorator = exports.createMethodDecorator = exports.createParameterAndPropertyDecorator = exports.createParameterDecorator = exports.createPropertyDecorator = exports.createClassDecorator = exports.getDecoratorId = exports.getDecoratorUUID = exports.getDecoratorType = exports.getDecoratorName = exports.hasMetadata = exports.hasOwnMetadata = exports.getOwnMetadataKeys = exports.getMetadataKeys = exports.getOwnMetadata = exports.getMetadata = exports.defineMetadata = void 0;
3
+ exports.TS_TYPE = exports.TS_RETURN = exports.TS_PARAMS = exports.clearMetadataCache = exports.getMetadataVersion = exports.getAllMetadataForTarget = exports.getAllMetadataKeysForTarget = exports.getAllKeysForTarget = exports.getMetadataForTarget = exports.getMetadataKeysForTarget = exports.getParameterMetadata = exports.getMethodMetadata = exports.getPropertyMetadata = exports.getClassMetadata = exports.isDecorator = exports.hasDecorator = exports.createMethodDecorator = exports.createParameterAndPropertyDecorator = exports.createParameterDecorator = exports.createPropertyDecorator = exports.createClassDecorator = exports.getDecoratorId = exports.getDecoratorUUID = exports.getDecoratorType = exports.getDecoratorName = exports.hasMetadata = exports.hasOwnMetadata = exports.getOwnMetadataKeys = exports.getMetadataKeys = exports.getOwnMetadata = exports.getMetadata = exports.defineMetadata = void 0;
4
4
  var metadata_1 = require("./metadata");
5
5
  Object.defineProperty(exports, "defineMetadata", { enumerable: true, get: function () { return metadata_1.defineMetadata; } });
6
6
  Object.defineProperty(exports, "getMetadata", { enumerable: true, get: function () { return metadata_1.getMetadata; } });
@@ -29,6 +29,8 @@ Object.defineProperty(exports, "getMetadataForTarget", { enumerable: true, get:
29
29
  Object.defineProperty(exports, "getAllKeysForTarget", { enumerable: true, get: function () { return metadata_1.getAllKeysForTarget; } });
30
30
  Object.defineProperty(exports, "getAllMetadataKeysForTarget", { enumerable: true, get: function () { return metadata_1.getAllMetadataKeysForTarget; } });
31
31
  Object.defineProperty(exports, "getAllMetadataForTarget", { enumerable: true, get: function () { return metadata_1.getAllMetadataForTarget; } });
32
+ Object.defineProperty(exports, "getMetadataVersion", { enumerable: true, get: function () { return metadata_1.getMetadataVersion; } });
33
+ Object.defineProperty(exports, "clearMetadataCache", { enumerable: true, get: function () { return metadata_1.clearMetadataCache; } });
32
34
  Object.defineProperty(exports, "TS_PARAMS", { enumerable: true, get: function () { return metadata_1.TS_PARAMS; } });
33
35
  Object.defineProperty(exports, "TS_RETURN", { enumerable: true, get: function () { return metadata_1.TS_RETURN; } });
34
36
  Object.defineProperty(exports, "TS_TYPE", { enumerable: true, get: function () { return metadata_1.TS_TYPE; } });
@@ -2,6 +2,8 @@ import "reflect-metadata";
2
2
  export declare const TS_PARAMS = "design:paramtypes";
3
3
  export declare const TS_TYPE = "design:type";
4
4
  export declare const TS_RETURN = "design:returntype";
5
+ export declare function getMetadataVersion(): number;
6
+ export declare function clearMetadataCache(): void;
5
7
  export interface IMetadata {
6
8
  args: any;
7
9
  metadataKey: string;
package/dist/metadata.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TS_RETURN = exports.TS_TYPE = exports.TS_PARAMS = void 0;
4
+ exports.getMetadataVersion = getMetadataVersion;
5
+ exports.clearMetadataCache = clearMetadataCache;
4
6
  exports.defineMetadata = defineMetadata;
5
7
  exports.hasMetadata = hasMetadata;
6
8
  exports.hasOwnMetadata = hasOwnMetadata;
@@ -40,17 +42,25 @@ const TX_TYPE = `${TX_PREFIX}:type`;
40
42
  exports.TS_PARAMS = "design:paramtypes";
41
43
  exports.TS_TYPE = "design:type";
42
44
  exports.TS_RETURN = "design:returntype";
45
+ let METADATA_VERSION = 0;
46
+ function getMetadataVersion() {
47
+ return METADATA_VERSION;
48
+ }
49
+ function clearMetadataCache() {
50
+ METADATA_VERSION++;
51
+ }
43
52
  function defineMetadata(metadataKey, metadataValue, target, targetKey) {
44
53
  if ((0, utils_1.inArray)([exports.TS_TYPE, exports.TS_PARAMS, exports.TS_RETURN], metadataKey)) {
45
54
  throw new Error(`You can't override typescript metadata: ${exports.TS_TYPE}, ${exports.TS_PARAMS}, ${exports.TS_RETURN}`);
46
55
  }
47
56
  Reflect.defineMetadata(metadataKey, metadataValue, target, targetKey);
57
+ clearMetadataCache();
48
58
  }
49
59
  function hasMetadata(metadataKey, target, targetKey) {
50
60
  try {
51
61
  return Reflect.hasMetadata(metadataKey, target, targetKey);
52
62
  }
53
- catch (e) {
63
+ catch {
54
64
  return false;
55
65
  }
56
66
  }
@@ -58,7 +68,7 @@ function hasOwnMetadata(metadataKey, target, targetKey) {
58
68
  try {
59
69
  return Reflect.hasOwnMetadata(metadataKey, target, targetKey);
60
70
  }
61
- catch (e) {
71
+ catch {
62
72
  return false;
63
73
  }
64
74
  }
@@ -79,9 +89,13 @@ function deleteMetadata(metadataKey, target, targetKey) {
79
89
  throw new Error(`You can't delete typescript metadata: ${exports.TS_TYPE}, ${exports.TS_PARAMS}, ${exports.TS_RETURN}`);
80
90
  }
81
91
  try {
82
- return Reflect.deleteMetadata(metadataKey, target, targetKey);
92
+ const deleted = Reflect.deleteMetadata(metadataKey, target, targetKey);
93
+ if (deleted) {
94
+ clearMetadataCache();
95
+ }
96
+ return deleted;
83
97
  }
84
- catch (e) {
98
+ catch {
85
99
  return false;
86
100
  }
87
101
  }
@@ -157,8 +171,24 @@ function mapKeyToMetadata(key) {
157
171
  metadataKey: key.metadataKey
158
172
  };
159
173
  }
174
+ function isFrameworkPrototype(target) {
175
+ return target === Function.prototype || target === Object.prototype;
176
+ }
177
+ function keySignature(item) {
178
+ return `${item.metadataKey}\u0000${String(item.propertyKey)}`;
179
+ }
160
180
  function mergeKeys(a, b) {
161
- return a.concat(b.filter((bI) => (0, utils_1.isFalsy)(a.find((aI) => aI.metadataKey === bI.metadataKey && aI.propertyKey === bI.propertyKey))));
181
+ const seen = new Set(a.map(keySignature));
182
+ const merged = a.slice();
183
+ for (const item of b) {
184
+ const signature = keySignature(item);
185
+ if (seen.has(signature)) {
186
+ continue;
187
+ }
188
+ seen.add(signature);
189
+ merged.push(item);
190
+ }
191
+ return merged;
162
192
  }
163
193
  function getMetadataKeysForTarget(target, targetKey) {
164
194
  let _target = (0, utils_1.isObject)(target.prototype) && !!targetKey ? target.prototype : target;
@@ -173,7 +203,7 @@ function getMetadataKeysForTarget(target, targetKey) {
173
203
  }
174
204
  function getMetadataForTarget(target, targetKey) {
175
205
  let keys = getMetadataKeysForTarget(target, targetKey), proto = Object.getPrototypeOf(target);
176
- if (!(0, utils_1.isNull)(proto)) {
206
+ if (!(0, utils_1.isNull)(proto) && !isFrameworkPrototype(proto)) {
177
207
  keys = mergeKeys(keys, getMetadataKeysForTarget(proto, targetKey));
178
208
  }
179
209
  return (0, utils_1.flatten)(keys.map(item => mapKeyToMetadata(item)));
@@ -191,15 +221,34 @@ function getAllKeysForTarget(target) {
191
221
  }));
192
222
  }
193
223
  function getAllMetadataKeysForTarget(target) {
194
- let keys = getAllKeysForTarget(target), metadata = [], proto = Object.getPrototypeOf(target);
195
- keys.forEach(item => metadata = metadata.concat(getMetadataKeysForTarget(item.target, item.key)));
196
- if (!(0, utils_1.isNull)(proto)) {
197
- metadata = mergeKeys(metadata, getAllMetadataKeysForTarget(proto));
224
+ const metadata = [];
225
+ const seen = new Set();
226
+ let current = target;
227
+ while ((0, utils_1.isDefined)(current) && !(0, utils_1.isNull)(current) && !isFrameworkPrototype(current)) {
228
+ for (const entry of getAllKeysForTarget(current)) {
229
+ for (const item of getMetadataKeysForTarget(entry.target, entry.key)) {
230
+ const signature = keySignature(item);
231
+ if (seen.has(signature)) {
232
+ continue;
233
+ }
234
+ seen.add(signature);
235
+ metadata.push(item);
236
+ }
237
+ }
238
+ current = Object.getPrototypeOf(current);
198
239
  }
199
240
  return metadata;
200
241
  }
242
+ const allMetadataCache = new WeakMap();
201
243
  function getAllMetadataForTarget(target) {
202
- return (0, utils_1.flatten)(getAllMetadataKeysForTarget(target).map(item => mapKeyToMetadata(item)));
244
+ const epoch = METADATA_VERSION;
245
+ const cached = allMetadataCache.get(target);
246
+ if ((0, utils_1.isDefined)(cached) && cached.epoch === epoch) {
247
+ return cached.records.slice();
248
+ }
249
+ const records = (0, utils_1.flatten)(getAllMetadataKeysForTarget(target).map(item => mapKeyToMetadata(item)));
250
+ allMetadataCache.set(target, { epoch, records });
251
+ return records.slice();
203
252
  }
204
253
  function getType(target, parameterOrDescriptor, propertyKey, type) {
205
254
  type = type === "mixed" ? (0, utils_1.isNumber)(parameterOrDescriptor) ? "parameter" : "property" : type;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typeix/metadata",
3
- "version": "9.0.0-rc.1",
3
+ "version": "9.1.0-rc.3",
4
4
  "description": "@typeix metadata apis::::",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "homepage": "https://typeix.com",
31
31
  "dependencies": {
32
- "@typeix/utils": "^9.0.0-rc.1",
32
+ "@typeix/utils": "^9.1.0-rc.3",
33
33
  "reflect-metadata": "~0.2.2"
34
34
  },
35
35
  "publishConfig": {
@@ -38,5 +38,5 @@
38
38
  "engines": {
39
39
  "node": "^26.0.0"
40
40
  },
41
- "gitHead": "385075330bf724ae455dfa47d789517bd4757b25"
41
+ "gitHead": "6e55af30f8c771391b0ee5faa6d797c9807c4c88"
42
42
  }