@midwayjs/core 4.0.0-beta.5 → 4.0.0-beta.6

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.
@@ -13,7 +13,7 @@ declare enum ObjectType {
13
13
  * @since 4.0.0
14
14
  */
15
15
  export declare class MetadataManager {
16
- protected static readonly metadataSymbol: unique symbol;
16
+ private static _metadataStore;
17
17
  protected static readonly metadataClassSymbol: unique symbol;
18
18
  protected static readonly metadataPropertySymbol: unique symbol;
19
19
  protected static readonly cacheSymbol: unique symbol;
@@ -45,15 +45,15 @@ export declare class MetadataManager {
45
45
  /**
46
46
  * Checks if metadata exists for a target class or property
47
47
  */
48
- static hasMetadata(metadataKey: string | symbol, target: ClassType, propertyKey?: string | symbol): boolean;
48
+ static hasMetadata(metadataKey: string | symbol, target: ClassType | object, propertyKey?: string | symbol): boolean;
49
49
  /**
50
50
  * Checks if own metadata exists for a target class or property
51
51
  */
52
- static hasOwnMetadata(metadataKey: string | symbol, target: ClassType, propertyKey?: string | symbol): boolean;
52
+ static hasOwnMetadata(metadataKey: string | symbol, target: ClassType | object, propertyKey?: string | symbol): boolean;
53
53
  /**
54
54
  * Deletes metadata for a target class or property
55
55
  */
56
- static deleteMetadata(metadataKey: string | symbol, target: ClassType, propertyKey?: string | symbol): void;
56
+ static deleteMetadata(metadataKey: string | symbol, target: ClassType | object, propertyKey?: string | symbol): void;
57
57
  /**
58
58
  * Get all metadata keys on the entire prototype chain
59
59
  * Because we need to get metadata on the entire prototype chain, we do not use cache here, so the performance is poor
@@ -126,6 +126,10 @@ export declare class MetadataManager {
126
126
  private static getUnionKey;
127
127
  private static formatTarget;
128
128
  static ensureTargetType(target: any, type: ObjectType): void;
129
+ private static getOwnProperty;
130
+ private static hasOwnProperty;
131
+ private static setOwnProperty;
132
+ static clear(): void;
129
133
  }
130
134
  export {};
131
135
  //# sourceMappingURL=metadataManager.d.ts.map
@@ -335,33 +335,26 @@ class MetadataManager {
335
335
  * }
336
336
  * }
337
337
  */
338
- // eslint-disable-next-line no-prototype-builtins
339
- if (!target.hasOwnProperty(this.metadataSymbol)) {
340
- const _metadata = {
338
+ if (!this._metadataStore.has(target)) {
339
+ this._metadataStore.set(target, {
341
340
  [this.metadataClassSymbol]: Object.create(null),
342
341
  [this.metadataPropertySymbol]: Object.create(null),
343
- };
344
- Object.defineProperty(target, this.metadataSymbol, {
345
- value: _metadata,
346
- enumerable: false,
347
- configurable: false,
348
342
  });
349
- return _metadata;
350
343
  }
351
- return target[this.metadataSymbol];
344
+ return this._metadataStore.get(target);
352
345
  }
353
346
  static invalidateCache(metadataKey, target, propertyKey) {
354
347
  // remove current cache
355
348
  const unionKey = this.getUnionKey(metadataKey, propertyKey);
356
349
  // Remove the specific cache
357
350
  // eslint-disable-next-line no-prototype-builtins
358
- if (target.hasOwnProperty(this.cacheSymbol)) {
359
- delete target[this.cacheSymbol]?.[unionKey];
351
+ if (this.hasOwnProperty(target, this.cacheSymbol)) {
352
+ delete this.getOwnProperty(target, this.cacheSymbol)?.[unionKey];
360
353
  }
361
354
  // Execute hooks, passing the unionKey to clear
362
- const cleanHooks = target[this.cleanHooksSymbol];
355
+ const cleanHooks = this.getOwnProperty(target, this.cleanHooksSymbol);
363
356
  if (cleanHooks) {
364
- target[this.cleanHooksSymbol] = cleanHooks.filter(hook => !hook(unionKey));
357
+ this.setOwnProperty(target, this.cleanHooksSymbol, cleanHooks.filter(hook => !hook(unionKey)));
365
358
  }
366
359
  }
367
360
  static setCache(metadataKey, target, protoTarget, value, propertyKey) {
@@ -370,42 +363,30 @@ class MetadataManager {
370
363
  if (target !== protoTarget) {
371
364
  this.validCacheConstruct(protoTarget);
372
365
  // Register a clean hook to the prototype target and clean the cache when the prototype target value is changed
373
- protoTarget[this.cleanHooksSymbol].push((keyToClear) => {
366
+ this.getOwnProperty(protoTarget, this.cleanHooksSymbol).push((keyToClear) => {
374
367
  // Only delete cache if the key matches
375
368
  if (keyToClear === unionKey) {
376
- delete target[this.cacheSymbol]?.[unionKey];
369
+ delete this.getOwnProperty(target, this.cacheSymbol)?.[unionKey];
377
370
  // Indicates that this hook can be removed
378
371
  return true;
379
372
  }
380
373
  return false;
381
374
  });
382
375
  }
383
- target[this.cacheSymbol][unionKey] = value;
376
+ this.getOwnProperty(target, this.cacheSymbol)[unionKey] = value;
384
377
  }
385
378
  static validCacheConstruct(target) {
379
+ const metadata = this.getOrCreateMetaObject(target);
386
380
  // eslint-disable-next-line no-prototype-builtins
387
- if (!target.hasOwnProperty(this.cacheSymbol)) {
388
- Object.defineProperty(target, this.cacheSymbol, {
389
- value: Object.create(null),
390
- enumerable: false,
391
- configurable: false,
392
- });
381
+ if (!metadata[this.cacheSymbol]) {
382
+ metadata[this.cacheSymbol] = Object.create(null);
393
383
  }
394
- // eslint-disable-next-line no-prototype-builtins
395
- if (!target.hasOwnProperty(this.cleanHooksSymbol)) {
396
- Object.defineProperty(target, this.cleanHooksSymbol, {
397
- value: [],
398
- enumerable: false,
399
- configurable: false,
400
- writable: true,
401
- });
384
+ if (!metadata[this.cleanHooksSymbol]) {
385
+ metadata[this.cleanHooksSymbol] = [];
402
386
  }
403
387
  }
404
388
  static getCache(metadataKey, target, propertyKey) {
405
- // eslint-disable-next-line no-prototype-builtins
406
- if (target.hasOwnProperty(this.cacheSymbol)) {
407
- return target[this.cacheSymbol]?.[this.getUnionKey(metadataKey, propertyKey)];
408
- }
389
+ return this.getOwnProperty(target, this.cacheSymbol)?.[this.getUnionKey(metadataKey, propertyKey)];
409
390
  }
410
391
  static getUnionKey(metadataKey, propertyKey) {
411
392
  return propertyKey
@@ -413,10 +394,7 @@ class MetadataManager {
413
394
  : metadataKey.toString();
414
395
  }
415
396
  static formatTarget(target) {
416
- // eslint-disable-next-line no-prototype-builtins
417
- let ret = target.hasOwnProperty(this.isClassSymbol)
418
- ? target[this.isClassSymbol]
419
- : undefined;
397
+ let ret = this.getOwnProperty(target, this.isClassSymbol);
420
398
  if (!ret) {
421
399
  const isClassRet = (0, types_1.isClass)(target);
422
400
  if (isClassRet) {
@@ -428,30 +406,38 @@ class MetadataManager {
428
406
  else {
429
407
  ret = ObjectType.Object;
430
408
  }
431
- Object.defineProperty(target, this.isClassSymbol, {
432
- value: ret,
433
- enumerable: false,
434
- configurable: false,
435
- });
409
+ this.setOwnProperty(target, this.isClassSymbol, ret);
436
410
  }
437
411
  return ret === ObjectType.Instance ? target.constructor : target;
438
412
  }
439
413
  static ensureTargetType(target, type) {
440
414
  // eslint-disable-next-line no-prototype-builtins
441
- const ret = target.hasOwnProperty(this.isClassSymbol)
442
- ? target[this.isClassSymbol]
443
- : undefined;
415
+ const ret = this.getOwnProperty(target, this.isClassSymbol);
444
416
  if (!ret) {
445
- Object.defineProperty(target, this.isClassSymbol, {
446
- value: type,
447
- enumerable: false,
448
- configurable: false,
449
- });
417
+ this.setOwnProperty(target, this.isClassSymbol, type);
418
+ }
419
+ }
420
+ static getOwnProperty(target, propertyKey) {
421
+ const _metadata = this.getOrCreateMetaObject(target);
422
+ return _metadata[propertyKey] ?? undefined;
423
+ }
424
+ static hasOwnProperty(target, propertyKey) {
425
+ if (!this._metadataStore.has(target)) {
426
+ return false;
450
427
  }
428
+ const _metadata = this._metadataStore.get(target);
429
+ return _metadata[propertyKey] !== undefined;
430
+ }
431
+ static setOwnProperty(target, propertyKey, value) {
432
+ const _metadata = this.getOrCreateMetaObject(target);
433
+ _metadata[propertyKey] = value;
434
+ }
435
+ static clear() {
436
+ this._metadataStore = new WeakMap();
451
437
  }
452
438
  }
453
439
  exports.MetadataManager = MetadataManager;
454
- MetadataManager.metadataSymbol = Symbol.for('midway.metadata');
440
+ MetadataManager._metadataStore = new WeakMap();
455
441
  MetadataManager.metadataClassSymbol = Symbol.for('midway.metadata.class');
456
442
  MetadataManager.metadataPropertySymbol = Symbol.for('midway.metadata.property');
457
443
  MetadataManager.cacheSymbol = Symbol.for('midway.metadata.cache');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midwayjs/core",
3
- "version": "4.0.0-beta.5",
3
+ "version": "4.0.0-beta.6",
4
4
  "description": "midway core",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  "devDependencies": {
25
25
  "@midwayjs/logger": "^3.0.0",
26
26
  "eventsource": "2.0.2",
27
- "koa": "2.16.2",
27
+ "koa": "3.0.3",
28
28
  "mm": "3.4.0",
29
29
  "raw-body": "2.5.2",
30
30
  "sinon": "17.0.2"
@@ -61,5 +61,5 @@
61
61
  "types": "./dist/functional/index.d.ts"
62
62
  }
63
63
  },
64
- "gitHead": "bfbab99fabbecd15a21df8c003cbd35d4c6ba3ff"
64
+ "gitHead": "a02425777d044ec502d050993bc256dc26d5b586"
65
65
  }