Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
adonis-context
Advanced tools
A context provider for Adonis based on async_hooks
used to store
data for a group of related asynchronous function calls such as a request.
Most applications do just fine without any sort of per-request context. Data from the request can be explicitly passed through the rest of the application. There are however some cases that make having a request scoped data store very appealing.
adonis install adonis-context
After installing the package, make sure to follow the directions in instructions.md on how to set up the provider.
Be default, the provider will ensure that each HTTP request is executed in a
unique context. Getting an instance of Context
anywhere within the request
lifecyle will return a store specific to that request.
class SomeMiddleware {
static get inject () {
return ['Context']
}
constructor (context) {
this.context = context
}
async handle ({ request }, next) {
this.context.set('some.key', keyForRequest(request))
await next()
}
}
class SomeService {
static get inject () {
return ['Context']
}
constructor (context) {
this.key = context.get('some.key', 'default key')
}
}
The ContextProvider
allows you to run any arbitrary code inside of a context.
For example, you may want to run each worker job in a seperate context.
const manager = use('Context/Manager')
await manager.run(async () => {
// this will be run in a context
})
A default context exists that will be used whenever Context
is resolved
outside of a current context. This makes it easy to write code that works with
or without context. By default the store is empty, but it can be initialized
with some data in bootstraping hooks, or a service provider's boot method.
const manager = use('Context/Manager')
manager.default.get('foo', 'somedefaultvalue') // => 'somedefaultvalue'
Context/Store
(aliased as Context
)set(key, value)
query.count
). When using
nested keys, it is possible for set
to throw a RuntimeException
when
attempting to set a property of an existing non-object value.const context = use('Context')
context.set('service.apiKey', 'userapikey')
get(key, fallback)
set
method, nested values can be retrieved using dot to seperate the
keys in the string.null
.const context = use('Context')
const config = use('Config')
context.get('service.apiKey', config.get('service.apiKey'))
Context/Manager
run(next)
const manager = use('Context/Manager')
await manager.run(async () => {
// this will be run in a context
})
disable()
Disable async_hooks
for the context provider. While disabled, context will be
lost for code executing in future runs of the event loop.
const manager = use('Context/Manager')
manager.disable()
enable()
Enable async_hooks
for the context provider after it has been disabled. The
hooks for the provider are enabled by default.
const manager = use('Context/Manager')
manager.disable()
manager.enable()
Using context may not be right for all projects. There are a few things you should be aware of before using it.
This package has not been tested extensively yet. Make sure you test thoroughly before deploying it in a production application. If you try it out, I would appreciate feedback.
async_hooks
API stabilityThis package is based on the Node.js async_hooks
API. It is currently
listed as Stability: 1 - Experimental
. With that being said, it has been in
the works for a long time. I would be surprised to see a lot of change.
The async_hooks module provides an API to register callbacks tracking the lifetime of asynchronous resources created inside a Node.js application.
I have not run any real world benchmarks yet, but it is expected that there will
be some performace cost of tracking context with async_hooks
. The performance
of async_hooks
and ways that it can be improved are currently being discussed
(see https://github.com/nodejs/benchmarking/issues/181).
Copyright 2018 Brent Burgoyne
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Context provider for the Adonis framework based on async_hooks
The npm package adonis-context receives a total of 12 weekly downloads. As such, adonis-context popularity was classified as not popular.
We found that adonis-context demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.