A module to gather very common services.
npm install common-services[//]: # ( )
[//]: # (This file is automatically generated by a metapak)
[//]: # (module. Do not change it except between the)
[//]: # (content:start/end flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )


[//]: # (::contents:start)
This module contains various common injectable services that are often used into
a wide range of applications.
The services provided here are meant to have a very tiny surface API in order to
be easily mocked but also implemented with different technologies.
For example, the counter service could be implemented with a distributed
architecture, the codeGenerator though a database...
The services are designed to be used with
Knifecycle a simple but feature
complete dependency injection tool but can also be used by hand.
[//]: # (::contents:end)
Promise.<function()> | Promise.<function()>Instantiate the codeGenerator service
Promise.<function()>Instantiate the counter service
Promise.<Object>Instantiate the delay service
Promise.<Object>Allow to import ES modules.
Promise.<Object>Instantiate the lock service
Promise.<function()>Instantiate the logging service
Promise.<function()>Instantiate the LRU Pool service
Promise.<function()>Instantiate the random service
Promise.<function()>Instantiate the resolve service
Promise.<string>Allow to resolve a path with the module system.
Promise.<function()>Instantiate the time service
Promise.<function()> \| Promise.<function()>Kind: global function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services to inject |
| [services.CHARS_SET] | Object | EXPLICIT_CHARS | An optional char set to pick cars into |
| [services.random] | Object | Math.random | An optional random function to replace the Math.random one used by default |
| [services.log] | Object | noop | An optional logging function |
Example
``js
import {
DEFAULT_LOGGER,
initCodeGenerator,
initLog,
} from 'common-services';
const log = await initLog({
logger: DEFAULT_LOGGER,
});
const codeGenerator = await initCodeGenerator({
log,
});
`
Kind: inner method of initCodeGenerator
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
`js`
console.log([
codeGenerator(),
codeGenerator(),
codeGenerator(),
]);
// Prints: ABCDEF,GHJKMN,PRSTUV
Promise.<function()>Kind: global function
Returns: Promise.<function()> - A promise of the counter function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services to inject |
| [services.COUNTER] | Object | DEFAULT_COUNTER | An optional configuration object |
| [services.log] | Object | noop | An optional logging function |
Example
`js
import {
initCounter,
initLog,
} from 'common-services';
const log = await initLog({
logger: DEFAULT_LOGGER
});
const counter = await initCounter({
COUNTER: { firstCount: 1 },
log,
});
`
Kind: inner method of initCounter
Returns: Promise.<number> - A promise of the current count
Example
`js`
console.log([
counter(),
counter(),
counter(),
]);
// Prints: 1,2,3
Promise.<Object>Kind: global function
Returns: Promise.<Object> - A promise of the delay service
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services to inject |
| [services.log] | function | noop | A logging function |
Example
`js
import {
DEFAULT_LOGGER,
initDelay,
initLog,
} from 'common-services';
const log = await initLog({
logger: DEFAULT_LOGGER
});
const delay = await initDelay({
log,
});
`
* initDelay(services) ⇒ Promise.<Object>
* ~create(delay) ⇒ Promise
* ~clear(promise) ⇒ Promise
Kind: inner method of initDelay
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
`js`
await delay.create(1000);
console.log('1000 ms elapsed!');
Kind: inner method of initDelay
Returns: Promise - A promise resolved when cancellation is done.
| Param | Type | Description |
| --- | --- | --- |
| promise | Promise | The promise of the delay to cancel |
Example
`js`
try {
const delayPromise = delay.create(1000);
await Promise.all(delayPromise, delay.clear(delayPromise));
console.log('1000 ms elapsed!');
} catch (err) {
if(err.code != 'E_DELAY_CLEARED') {
trow err;
}
console.log('Cancelled!'));
}
// Prints: Cancelled!
Promise.<Object>Kind: global function
Returns: Promise.<Object> - A promise of an imported module.
| Param | Type | Description |
| --- | --- | --- |
| path | string | The module path |
Promise.<Object>Kind: global function
Returns: Promise.<Object> - A promise of the lock service
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services to inject |
| [services.LOCKS_MAP] | Map | | A map to store le current locks (optional) |
| [services.LOCK_TIMEOUT] | Number | Infitiny | The timeout in milliseconds for the lock to be released. |
| [services.log] | function | | A logging function |
| [services.delay] | Object | | A delay service like the common-services one |
Example
`js
import {
DEFAULT_LOGGER,
initLog,
initDelay,
initLock
} from 'common-services';
import ms from 'ms';
const log = await initLog({
logger: DEFAULT_LOGGER
});
const delay = await initDelay({ log });
const lock = await initLock({
LOCK_TIMEOUT: ms('5s'),
delay,
log,
});
run();
async function run() {
// The following async jobs are done sequentially
// if they have the same resourceKey value
await Promise.all(asynTasks.map(async (asyncTask) => {
await lock.take(asyncTask.resourceKey);
await myAsyncStuff1(asyncTask);
await myAsyncStuff2(asyncTask);
await myAsyncStuff3(asyncTask);
lock.release(asyncTask.resourceKey);
});
}
`
* initLock(services) ⇒ Promise.<Object>
* ~take(key) ⇒ Promise
* ~release(key) ⇒ void
Kind: inner method of initLock
Returns: Promise - A promise to be resolved when the lock
is gained or rejected if the lock release
timeout is reached.
| Param | Type | Description |
| --- | --- | --- |
| key | String | A unique key for the locked resource |
Kind: inner method of initLock
| Param | Type | Description |
| --- | --- | --- |
| key | String | A unique key for the resource to release |
Promise.<function()>Kind: global function
Returns: Promise.<function()> - A promise of the logging function
| Param | Type | Description |
| --- | --- | --- |
| services | Object | The services to inject |
| services.logger | Object | The logger object that output the logs |
Example
`js
import {
DEFAULT_LOGGER,
initLog,
} from 'common-services';
const log = await initLog({
logger: DEFAULT_LOGGER,
});
`
Kind: inner method of initLog
| Param | Type | Description |
| --- | --- | --- |
| type | String | Log type |
| ...args | \* | Log contents |
Example
`js`
log('debug', 'Luke, I am your father!')
Promise.<function()>Kind: global function
Returns: Promise.<function()> - A promise of the LRUPool service
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services to inject |
| [services.log] | Object | noop | A logging function |
Example
`js
import {
DEFAULT_LOGGER,
initLog,
initLRUPool
} from 'common-services';
const log = await initLog({
logger: DEFAULT_LOGGER,
});
const random = await initLRUPool({
MAX_POOL_SIZE: 50,
poolManager: {
// ...
},
log,
});
`
Kind: inner method of initRandom
Returns: number - The random number
Example
`js`
random()
// Prints: 0.3141592653589793
Promise.<function()>Kind: global function
Returns: Promise.<function()> - A promise of the random function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services to inject |
| [services.log] | Object | noop | A logging function |
Example
`js
import {
DEFAULT_LOGGER,
initLog,
initRandom
} from 'common-services';
const log = await initLog({
logger: DEFAULT_LOGGER,
});
const random = await initRandom({
log,
});
`
Kind: inner method of initRandom
Returns: number - The random number
Example
`js`
random()
// Prints: 0.3141592653589793
Promise.<function()> serviceKind: global function
Returns: Promise.<function()> - A promise of the
resolve service | Param | Type | Description |
| --- | --- | --- |
| services | Object | The services to inject |
| services.MAIN_FILE_URL | String | An URL pointing to the main file run |
| [services.log] | function | A logging function |
Example
`js
import {
DEFAULT_LOGGER,
initLog,
initResolve,
} from 'common-services';const log = await initLog({
logger: DEFAULT_LOGGER,
});
const resolve = initResolve({
MAIN_FILE_URL: import.meta.url,
log,
});
resolve('./myfile.ts');
}
`
resolve(path) ⇒ Promise.<string>
Allow to resolve a path with the module system.Kind: global function
Returns: Promise.<string> - A promise of a fully qualified module path
| Param | Type | Description |
| --- | --- | --- |
| path | string | The serializable constants to gather |
initTime(services) ⇒ Promise.<function()>
Instantiate the time serviceKind: global function
Returns: Promise.<function()> - A promise of the time function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | Object | | The services to inject |
| [services.log] | Object | noop | A logging function |
Example
`js
import {
DEFAULT_LOGGER,
initLog,
initTime,
} from 'common-services';const log = await initLog({
logger: DEFAULT_LOGGER,
});
const time = await initTime({
log,
});
`
$3
Returns the current timestampKind: inner method of initTime
Returns: number - The current timestamp
Example
`js
time()
// Prints: 1326585600000
``