Gathering detailed insights and metrics for petitservice
Gathering detailed insights and metrics for petitservice
npm install petitservice
Typescript
Module System
Node Version
NPM Version
50.9
Supply Chain
95.9
Quality
77.2
Maintenance
100
Vulnerability
97.9
License
JavaScript (100%)
Total Downloads
22,312
Last Day
1
Last Week
3
Last Month
31
Last Year
1,820
5 Stars
82 Commits
2 Forks
2 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
3.1.0
Package Id
petitservice@3.1.0
Unpacked Size
34.49 kB
Size
11.14 kB
File Count
12
NPM Version
10.7.0
Node Version
20.15.0
Publised On
25 Jun 2024
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
-66.7%
3
Compared to previous week
Last month
-61.7%
31
Compared to previous month
Last year
6.2%
1,820
Compared to previous year
Set of helpers designed for a Microservice architecture.
Available helpers:
Start different services (sequentially) with graceful exit handler.
Supported features:
1const serviceLoader = require('petitservice/lib/serviceLoader'); 2const logger = require('petitservice/lib/logger'); 3 4const config = require('./config'); 5 6return serviceLoader() 7.ping([ 8 config.apiUrl, 9 config.redisUrl, 10 config.amqpUrl, 11 config.mongoUrl, 12]) 13.cache({ host: 'localhost', port: 6379, auth_pass: 'xxx' }) 14.mongo(config.mongoUrl) 15.amq({ 16 amqUrl: 'amqp://guest:guest@localhost:5672', 17 consumerQueue: 'my-consumer-queue', 18 assertQueues: ['my-publisher-queue'], 19 consumer: (data, ack) => { 20 logger.info(data); 21 ack(); // acknowledge the message 22 }, 23}) 24.then(() => { 25 logger.info('Do something during starting...'); 26}) 27.express(() => require('./express_app.js'), config.httpPort) 28.onExit(() => { 29 logger.info('Do something during exit...'); 30}) 31.done(() => { 32 logger.info('Everything is started!'); 33});
urlList
(array of url to ping) format: protocol://[user:pass@]hostname:port
options.failureMax
(optional integer, how many attempts should we try before we exit the process, default: 5)options.frequency
(optional integer, how many milliseconds should wait before checking again hostnames, default: 30000)petitservice/lib/cache
redisOpts
(required object, { host, port, auth_pass })petitservice/lib/mongo
mongoUrl
(required string, mongo url)options
is required.
options.amqUrl
(required string, amq url)options.assertQueues
(optional array, list of queue to create if they haven't been created before. if consumerQueue is set, it will be asserted as well automatically.options.consumerCb(data, ack)
(required function): handles a message to consume. It gets as parameter respectively the received data
(already JSON parsed) and ack
function to run when we want to acknowledge the message.options.consumerQueue
(required string): consumer queue nameoptions.consumerPrefetch
(optional integer): how many message we consume simultaneously - default: 1options.onError(err, msg)
(optional function): error handler when there's an exception on the consumer. Gets as parameter err
as error object and msg
as raw message. Defaults a warning message.expressApp
(required function that returns express app, https://github.com/expressjs/express) - We advice you to use the require inside this function.port
(integer, HTTP port. default: 80
)cb
(function that performs action, can return a promise as well)cb
(function that performs action, can return a promise as well)Connect to RabbitMQ using amqp.node, Asserts queues, and consume messages from a queue.
1const serviceLoader = require('petitservice/lib/serviceLoader'); 2const amq = require('petitservice/lib/amq'); 3 4serviceLoader() 5.amq({ 6 amqUrl: 'amqp://guest:guest@localhost:5672', 7 consumerQueue: 'my-consumer-queue', 8 assertQueues: ['my-publisher-queue'], 9 consumer: (data, ack) => { 10 logger.info(data); 11 ack(); // acknowledge the message 12 }, 13}) 14.done(() => { 15 amq.publish({ myKey: 'myValue' }, 'my-publisher-queue'); 16});
1const amq = require('petitservice/lib/amq'); 2 3// Using serviceLoader 4amq.start({ 5 amqUrl: 'amqp://guest:guest@localhost:5672', 6 consumerQueue: 'my-consumer-queue', 7 assertQueues: ['my-publisher-queue'], 8 consumer: (data, ack) => { 9 logger.info(data); 10 ack(); // acknowledge the message 11 }, 12}) 13.then(() => { 14 // publish a message 15 amq.publish({ myKey: 'myValue' }, 'my-publisher-queue'); 16}); 17
{ persistent: false, expiration: 60000 }
Cache manager using Redis
1const cache = require('petitservice/lib/cache'); 2const logger = require('petitservice/lib/logger'); 3 4cache.start(config.redisUrl); 5 6const userId = 12; 7const getUser = (userId) => models.getUser(userId); 8 9// Get / Set / Delete 10const cacheKey = 'bob'; 11cache.getValue(cacheKey) 12.then((cachedValue) => { 13 if (!cachedValue) { 14 logger.info(`Setting value for ${cacheKey}`); 15 return cache.getValue(cacheKey, 'alice', 60); 16 } 17 logger.info(`I remember ${cachedValue}`); 18}) 19.then((cachedValue) => { 20 logger.info(`Bye ${cacheKey}`); 21 return cache.delValue(cacheKey); 22}); 23 24// Wrap a function 25cache.wrap(userId, () => models.getUser(userId), 10) 26.then((cacheUser) => { 27 logger.info(`Bonjour ${cacheUser.firstName}`); 28}); 29 30// Delayed Execution 31const id = 'abc'; 32const prm = () => { 33 return models.insertKeystroke(id, Math.random()); 34} 35cache.delayedExec(id, prm, 10); // <= prm will be discarded after 10 sec 36cache.delayedExec(id, prm, 10); // <= prm will be resolved after 10 sec
MongoDB helpers
1const mongo = require('petitservice/lib/mongo'); 2const logger = require('petitservice/lib/logger'); 3 4return mongo.start(config.mongoUrl) 5 .then(() => { 6 const db = mongo.db(config.mongoDbname); 7 const collection = db.collection('documents'); 8 // Insert some documents 9 return collection.insertMany([ 10 {a : 1}, {a : 2}, {a : 3} 11 ]) 12 .then(() => collection.find({}).toArray()) 13 .then((docs) => logger.info(docs)); 14 }) 15 .then(() => mongo.close()); 16
Set common middlewares for an express app
1const express = require('express'); 2const expressMiddleWare = require('petitservice/lib/expressMiddleWare'); 3 4const app = express(); 5expressMiddleWare.addStandard(app); 6expressMiddleWare.addCompression(app); 7 8expressMiddleWare.addLogs(app); 9 10app.get('/', (req, res) => { 11 res.send('Bonjour!'); 12}); 13 14expressMiddleWare.addErrorHandlers(app);
404
)500
)Log data on the console (using pino), and report errors to error if enabled
The default LogLevels depends on the NOD_ENV:
debug
for development
envinfo
for production
enverror
for test
env1// You may also set the log level using the environment variable: LOG_LEVEL: 'debug' 2 3const logger = require('petitservice/lib/logger'); 4 5logger.debug('bonjour'); 6logger.info('un café et un croissant chaud'); 7logger.error(new Error('Something broke')); 8 9// You can use middlewares for express 10 11// Request logs 12app.use(logger.requestLogger); 13app.use(logger.errorLogger);
You are welcomed to fork the project and make pull requests. Or just file an issue or suggestion 😊
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
2 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/28 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
license file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More