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.
common-services
Advanced tools
A module to gather very common services and their mocks.
This module contains various common injectable services I use into a lot of applications.
Promise.<function()>
| Promise.<function()>
Instantiate the codeGenerator service
Promise.<function()>
Instantiate the counter service
Promise.<Object>
Instantiate the delay service
Promise.<Object>
Instantiate the delay service mock
Promise.<function()>
Instantiate the logging service
Promise.<function()>
Instantiate the logging mock
Promise.<function()>
Instantiate the random service
Promise.<function()>
Instantiate the random service mock
Promise.<function()>
Instantiate the time service
Promise.<function()>
Instantiate the time service mock
Promise.<function()>
| Promise.<function()>
Instantiate the codeGenerator service
Kind: global function
Param | Type | Description |
---|---|---|
services | Object | The services to inject |
[services.CHARS_SET] | Object | An optional char set to pick cars into |
[services.random] | Object | An optional random function to replace the Math.random one used by default |
[services.log] | Object | An optional logging function |
Example
import initCodeGeneratorService from 'common-services/src/codeGenerator';
const codeGenerator = await initCodeGeneratorService({
log: console.log.bind(console),
});
Promise.<String>
Returns a random code
Kind: inner method of initCodeGeneratorService
Returns: Promise.<String>
- A promise of the generated code
Param | Type | Default | Description |
---|---|---|---|
[length] | Number | 6 | An optional custon code length (defaults to 6) |
Example
console.log([
codeGenerator(),
codeGenerator(),
codeGenerator(),
]);
// Prints: ABCDEF,GHJKMN,PRSTUV
Promise.<function()>
Instantiate the counter service
Kind: global function
Returns: Promise.<function()>
- A promise of the counter function
Param | Type | Description |
---|---|---|
services | Object | The services to inject |
[services.COUNTER] | Object | An optional configuration object |
[services.log] | Object | An optional logging function |
Example
import initCounterService from 'common-services/src/counter';
const counter = await initCounterService({
COUNTER: { firstCount: 1 },
log: console.log.bind(console),
});
Promise.<number>
Returns the current count and increment the counter
Kind: inner method of initCounterService
Returns: Promise.<number>
- A promise of the current count
Example
console.log([
counter(),
counter(),
counter(),
]);
// Prints: 1,2,3
Promise.<Object>
Instantiate the delay service
Kind: global function
Returns: Promise.<Object>
- A promise of the delay service
Param | Type | Description |
---|---|---|
services | Object | The services to inject |
[services.log] | function | A logging function |
Example
import initDelayService from 'common-services/src/delay';
const delay = await initDelayService({
log: console.log.bind(console)
});
Promise.<Object>
Promise
Promise
Promise
Create a new delay
Kind: inner method of initDelayService
Returns: Promise
- A promise to be resolved after that delay
or rejected if it is cancelled.
Param | Type | Description |
---|---|---|
delay | Number | The delay in ms |
Example
delay.create(1000)
.then(() => console.log('1000 ms elapsed!'))
.catch(() => console.log('Cancelled!'));
// Prints: 1000 ms elapsed!
Promise
Cancel an earlier created delay
Kind: inner method of initDelayService
Returns: Promise
- A promise resolved when cancellation is done.
Param | Type | Description |
---|---|---|
promise | Promise | The promise of the delay to cancel |
Example
const delayed = delay.create(1000)
.then(() => console.log('1000 ms elapsed!'))
.catch(() => console.log('Cancelled!'));
clear(delayed)
// Prints: Cancelled!
Promise.<Object>
Instantiate the delay service mock
Kind: global function
Returns: Promise.<Object>
- A promise of the mocked delay service
Example
import initDelayMock from 'common-services/src/delay.mock';
import assert from 'assert';
const delay = await initDelayMock();
const delayPromise = delay.create(1000);
delay.resolve(delayPromise);
delayPromise.then(() => {
// Any code here will execute immediatly
// instead of after a 1000ms delay
});
Promise.<function()>
Instantiate the logging service
Kind: global function
Returns: Promise.<function()>
- A promise of the logging function
Param | Type | Default | Description |
---|---|---|---|
services | Object | The services to inject | |
[services.logger] | Object | The logger to use | |
[services.debug] | function | noop | A debugging function |
Example
import initLogService from 'common-services/src/log';
const log = await initLogService({
logger: require('winston'),
debug: require('debug')('myapp'),
});
void
Logging function
Kind: inner method of initLogService
Param | Type | Description |
---|---|---|
type | String | Log type |
...args | * | Log contents |
Example
log('debug', 'Luke, I am your father!')
Promise.<function()>
Instantiate the logging mock
Kind: global function
Returns: Promise.<function()>
- A promise of the mocked
logging function
Example
import initLogMock from 'common-services/src/log.mock';
import assert from 'assert';
const log = await initLogMock();
log('info', 'Hello!');
log('error', 'Aouch!');
assert.deepEqual(log.args, [[
'info', 'Hello!'
], [
'error', 'Aouch!'
]]);
log.reset();
assert.deepEqual(log.args, []);
Promise.<function()>
Instantiate the random service
Kind: global function
Returns: Promise.<function()>
- A promise of the random function
Param | Type | Description |
---|---|---|
services | Object | The services to inject |
[services.log] | Object | A logging function |
Example
import initRandomService from 'common-services/src/random';
const random = await initRandomService({
log: console.log.bind(console),
});
number
Returns a new random number
Kind: inner method of initRandomService
Returns: number
- The random number
Example
random()
// Prints: 0.3141592653589793
Promise.<function()>
Instantiate the random service mock
Kind: global function
Returns: Promise.<function()>
- A promise of the mocked
random function
Example
import initRandomMock from 'common-services/src/random.mock';
import assert from 'assert';
const random = await initRandomMock();
random.returns(0.5); // A good limit value to test ;)
assert.equal(random(), 0.5);
assert.deepEqual(random.args, [[]], 'Called once');
random.reset();
assert.deepEqual(random.args, []);
Promise.<function()>
Instantiate the time service
Kind: global function
Returns: Promise.<function()>
- A promise of the time function
Param | Type | Description |
---|---|---|
services | Object | The services to inject |
[services.log] | Object | A logging function |
Example
import initTimeService from 'common-services/src/time';
const time = await initTimeService({
log: console.log.bind(console),
});
number
Returns the current timestamp
Kind: inner method of initTimeService
Returns: number
- The current timestamp
Example
time()
// Prints: 1326585600000
Promise.<function()>
Instantiate the time service mock
Kind: global function
Returns: Promise.<function()>
- A promise of the mocked
time function
Example
import initTimeMock from 'common-services/src/time.mock';
import assert from 'assert';
const time = await initTimeMock();
// Let's returns Thomas birth date (OMG ya father
// talking me about its childrens :D).
time.returns(new Date('2014-01-26T00:00:00.000Z'));
assert.equal(time(), 1390694400000);
assert.deepEqual(time.args, [[]], 'Called once');
time.reset();
assert.deepEqual(time.args, []);
FAQs
A module to gather very common services.
The npm package common-services receives a total of 1,088 weekly downloads. As such, common-services popularity was classified as popular.
We found that common-services demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 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.