Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
A plugin for hapi.js that integrates the pioc Dependency Injection Container for node.js
A hapi.js plugin for the pioc dependency injection container
When developing node.js applications, it can sometimes be hard to get the
information you need to the place where you need it. It can also be hard to
create mocks for testing when you rely on the native require
function as you've
just hard-coded the path to the service.
By using the Inversion of Control pattern, you decouple a service from the places where it is used.
hapi-pioc extends the hapi server object by adding a lookup
method
that can be used to fetch a service. Thus it follows the Service Locator
pattern.
Within your services however, you are free to rely on Dependency Injection through Constructor Injection, Property Injection or Lazy Property Injection.
Because I honestly believe that Inversion of Control is the right way to implement an application.
The best possible case would've been to use Dependency Injection within the route handlers as well but that didn't work well. Using a Service Locator is still better than fetching services yourself so it seems like a good compromise.
Using the standard hapi API, of course.
When you register your plugins, just also register the hapi-pioc plugin.
Personally, I prefer to use glue and make
this part of a manifest.js
and it might even make sense to separate the services
option into a file of
its own. You should also be able to use this
with confidence for ultimate flexibility.
server.register({
register: require('hapi-pioc'),
options: {
// relative paths are resolved against this path
baseUrl: process.cwd(),
// the name of the exposed method
methodName: 'lookup',
services: {
// use the "value!" prefix to load as value
'value!UserEncryptionPrivateKeyProvider': './lib/services/UserEncryptionPrivateKeyProvider',
// without supports node_modules path resolution
'value!Promise': 'bluebird',
// the value can be any kind of pioc service,
// only strings are resolved as paths
'value!MongoConfiguration': {
url: 'mongodb://localhost:27017/demo',
settings: {
db: {
native_parser: false
}
}
},
'MongoConnection': './lib/services/MongoConnection',
'Post': './lib/services/models/Post',
'User': './lib/services/models/User'
}
}
}, function(err, server) {
// ...
})
And within your route handlers, you can now use the method to lookup services:
server.route({
path: '/{slug}',
method: 'GET',
handler(request, reply) {
var Post = server.methods.lookup('Post');
Post.getBySlug(request.params.slug)
.then(result => createSinglePostAppData(request.path, result))
.then(appData => renderApp(server, appData))
.then(render(reply))
.catch(reply);
}
});
FAQs
A plugin for hapi.js that integrates the pioc Dependency Injection Container for node.js
The npm package hapi-pioc receives a total of 1 weekly downloads. As such, hapi-pioc popularity was classified as not popular.
We found that hapi-pioc 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.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.