Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
reflect-metadata
Advanced tools
The reflect-metadata npm package is a library that allows developers to perform metadata reflection operations in JavaScript. It is based on the metadata reflection API that is proposed for ECMAScript and is used by frameworks like Angular and decorators to add and read metadata annotations.
Metadata Reflection
Allows defining metadata on a class or property. The code sample shows how to define custom metadata on a class 'MyClass' with a key 'custom' and a value 'metadataValue'.
Reflect.defineMetadata('custom', 'metadataValue', MyClass);
Metadata Retrieval
Enables retrieval of metadata from a class or property. The code sample demonstrates how to retrieve metadata from a class 'MyClass' using the key 'custom'.
let metadataValue = Reflect.getMetadata('custom', MyClass);
Metadata Deletion
Provides functionality to delete metadata from a class or property. The code sample illustrates how to delete metadata associated with a class 'MyClass' using the key 'custom'.
Reflect.deleteMetadata('custom', MyClass);
core-decorators is a library of decorators for JavaScript/TypeScript that provides similar functionality to reflect-metadata. It offers decorators for various purposes such as memoization, autobinding, and deprecation, but does not provide the same low-level metadata reflection API.
class-transformer is a package that allows developers to transform plain objects to class instances and vice versa. It uses decorators to annotate classes and properties, similar to how reflect-metadata uses metadata, but it focuses more on the transformation aspect rather than the reflection of metadata.
class-validator is a package that uses decorators to perform validation on class properties. It leverages metadata to store validation rules, which is a concept similar to reflect-metadata. However, its primary focus is on validation rather than general metadata reflection.
Proposal to add Decorators to ES7, along with a prototype for an ES7 Reflection API for Decorator Metadata
class C {
@Reflect.metadata(metadataKey, metadataValue)
method() {
}
}
Reflect.defineMetadata(metadataKey, metadataValue, C.prototype, "method");
let obj = new C();
let metadataValue = Reflect.getMetadata(metadataKey, obj, "method");
// define metadata on an object or property
Reflect.defineMetadata(metadataKey, metadataValue, target);
Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey);
// check for presence of a metadata key on the prototype chain of an object or property
let result = Reflect.hasMetadata(metadataKey, target);
let result = Reflect.hasMetadata(metadataKey, target, propertyKey);
// check for presence of an own metadata key of an object or property
let result = Reflect.hasOwnMetadata(metadataKey, target);
let result = Reflect.hasOwnMetadata(metadataKey, target, propertyKey);
// get metadata value of a metadata key on the prototype chain of an object or property
let result = Reflect.getMetadata(metadataKey, target);
let result = Reflect.getMetadata(metadataKey, target, propertyKey);
// get metadata value of an own metadata key of an object or property
let result = Reflect.getOwnMetadata(metadataKey, target);
let result = Reflect.getOwnMetadata(metadataKey, target, propertyKey);
// get all metadata keys on the prototype chain of an object or property
let result = Reflect.getMetadataKeys(target);
let result = Reflect.getMetadataKeys(target, propertyKey);
// get all own metadata keys of an object or property
let result = Reflect.getOwnMetadataKeys(target);
let result = Reflect.getOwnMetadataKeys(target, propertyKey);
// delete metadata from an object or property
let result = Reflect.deleteMetadata(metadataKey, target);
let result = Reflect.deleteMetadata(metadataKey, target, propertyKey);
// apply metadata via a decorator to a constructor
@Reflect.metadata(metadataKey, metadataValue)
class C {
// apply metadata via a decorator to a method (property)
@Reflect.metadata(metadataKey, metadataValue)
method() {
}
}
function ParamTypes(...types) {
return (target, propertyKey) => {
const symParamTypes = Symbol.for("design:paramtypes");
if (propertyKey === undefined) {
target[symParamTypes] = types;
}
else {
const symProperties = Symbol.for("design:properties");
let properties, property;
if (Object.prototype.hasOwnProperty.call(target, symProperties)) {
properties = target[symProperties];
}
else {
properties = target[symProperties] = {};
}
if (Object.prototype.hasOwnProperty.call(properties, propertyKey)) {
property = properties[propertyKey];
}
else {
property = properties[propertyKey] = {};
}
property[symParamTypes] = types;
}
};
}
function ParamTypes(...types) {
// as propertyKey is effectively optional, its easier to use here
return (target, propertyKey) => { Reflect.defineMetadata("design:paramtypes", types, target, propertyKey); }
// vs. having multiple overloads with the target and key in the front:
//
// return (target, propertyKey) => {
// if (propertyKey === undefined) {
// Reflect.defineMetadata(target, "design:paramtypes", types);
// }
// else {
// Reflect.defineMetadata(target, propertyKey, "design:paramtypes", types);
// }
// }
//
// vs. having a different methods for the class or a property:
//
// return (target, propertyKey) => {
// if (propertyKey === undefined) {
// Reflect.defineMetadata(target, "design:paramtypes", types);
// }
// else {
// Reflect.definePropertyMetadata(target, propertyKey, "design:paramtypes", types);
// }
// }
}
FAQs
Polyfill for Metadata Reflection API
The npm package reflect-metadata receives a total of 13,037,489 weekly downloads. As such, reflect-metadata popularity was classified as popular.
We found that reflect-metadata demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.