Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@poppinss/macroable
Advanced tools
Extend classes from outside in using Macros and getters
Macroable offers a simple API for adding properties and getters to the class prototype. You might not even need this package, if you are happy writing Object.defineProperty
calls yourself.
Install the package from npm packages registry as follows.
npm i @poppinss/macroable
# yarn lovers
yarn add @poppinss/macroable
And import the Macroable
class.
import Macroable from '@poppinss/macroable'
export class Route extends Macroable {}
Now, you can add properties to the Route class from outside-in. This is usually needed, when you want the consumer of your classes to be able to extend them by adding custom properties.
Getters are added to the class prototype directly.
Route.macro('head', function (uri, callback) {
return this.route(['HEAD'], uri, callback)
})
And now, you can will be use the head
method from an instance of the Route
class.
const route = new Route()
route.head('/', () => {})
Adding a macro is same as writing the following code in JavaScript.
Route.prototype.head = function () {
}
Getters are added to the class prototype using the Object.defineProperty
. The implementation of a getter is always a function.
Route.getter('version', function () {
return 'v1'
})
And now access the version as follows.
const route = new Route()
route.version // v1
Adding a getter is same as writing the following code in JavaScript.
Object.defineProperty(Route.prototype, 'version', {
get() {
return 'v1'
},
configurable: false,
enumerable: false,
})
Singleton getters are also defined on the class prototype. However, their values are cached after the first access.
const singleton = true
Mysql.getter('version', function () {
return this.config.driver.split('-')[1]
}, singleton)
Adding a singleton getter is same as writing the following code in JavaScript.
Object.defineProperty(Mysql.prototype, 'version', {
get() {
const value = this.config.driver.split('-')[1]
// Cache value on the class instance
Object.defineProperty(this, 'version', {
configurable: false,
enumerable: false,
value: value,
writable: false,
})
return value
},
configurable: false,
enumerable: false,
})
You will have to use module augmentation in order to define the types for the dynamically added properties.
FAQs
Extend classes from outside in using Macros and getters
The npm package @poppinss/macroable receives a total of 35,807 weekly downloads. As such, @poppinss/macroable popularity was classified as popular.
We found that @poppinss/macroable demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers 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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
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.