Detalhes do pacote

common-services

nfroidure31.9kMIT17.0.1

A module to gather very common services.

services, common, knifecycle

readme (leia-me)

common-services

A module to gather very common services.

GitHub license Coverage Status

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.

API

Functions

initCodeGenerator(services)Promise.<function()> | Promise.<function()>

Instantiate the codeGenerator service

initCounter(services)Promise.<function()>

Instantiate the counter service

initDelay(services)Promise.<Object>

Instantiate the delay service

initImporter(path)Promise.<Object>

Allow to import ES modules.

initLock(services)Promise.<Object>

Instantiate the lock service

initLog(services)Promise.<function()>

Instantiate the logging service

initRandom(services)Promise.<function()>

Instantiate the LRU Pool service

initRandom(services)Promise.<function()>

Instantiate the random service

initResolve(services)Promise.<function()>

Instantiate the resolve service

resolve(path)Promise.<string>

Allow to resolve a path with the module system.

initTime(services)Promise.<function()>

Instantiate the time service

initCodeGenerator(services) ⇒ Promise.<function()> | Promise.<function()>

Instantiate the codeGenerator service

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

import {
  DEFAULT_LOGGER,
  initCodeGenerator,
  initLog,
} from 'common-services';

const log = await initLog({
  logger: DEFAULT_LOGGER,
});

const codeGenerator = await initCodeGenerator({
  log,
});

initCodeGenerator~codeGenerator([length]) ⇒ Promise.<String>

Returns a random code

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

console.log([
  codeGenerator(),
  codeGenerator(),
  codeGenerator(),
]);
// Prints: ABCDEF,GHJKMN,PRSTUV

initCounter(services) ⇒ Promise.<function()>

Instantiate the counter service

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

import {
  initCounter,
  initLog,
} from 'common-services';

const log = await initLog({
  logger: DEFAULT_LOGGER
});

const counter = await initCounter({
  COUNTER: { firstCount: 1 },
  log,
});

initCounter~counter() ⇒ Promise.<number>

Returns the current count and increment the counter

Kind: inner method of initCounter
Returns: Promise.<number> - A promise of the current count
Example

console.log([
  counter(),
  counter(),
  counter(),
]);
// Prints: 1,2,3

initDelay(services) ⇒ Promise.<Object>

Instantiate the delay service

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

import {
  DEFAULT_LOGGER,
  initDelay,
  initLog,
} from 'common-services';

const log = await initLog({
  logger: DEFAULT_LOGGER
});

const delay = await initDelay({
  log,
});

initDelay~create(delay) ⇒ Promise

Create a new delay

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

await delay.create(1000);
console.log('1000 ms elapsed!');

initDelay~clear(promise) ⇒ Promise

Cancel an earlier created delay

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

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!

initImporter(path) ⇒ Promise.<Object>

Allow to import ES modules.

Kind: global function
Returns: Promise.<Object> - A promise of an imported module.

Param Type Description
path string The module path

initLock(services) ⇒ Promise.<Object>

Instantiate the lock service

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

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~take(key) ⇒ Promise

Take the lock on the given resource key

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

initLock~release(key) ⇒ void

Release the lock on the given resource key

Kind: inner method of initLock

Param Type Description
key String A unique key for the resource to release

initLog(services) ⇒ Promise.<function()>

Instantiate the logging service

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

import {
  DEFAULT_LOGGER,
  initLog,
} from 'common-services';

const log = await initLog({
  logger: DEFAULT_LOGGER,
});

initLog~log(type, ...args) ⇒ void

Logging function

Kind: inner method of initLog

Param Type Description
type String Log type
...args * Log contents

Example

log('debug', 'Luke, I am your father!')

initRandom(services) ⇒ Promise.<function()>

Instantiate the LRU Pool service

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

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,
});

initRandom~random() ⇒ number

Returns a new random number

Kind: inner method of initRandom
Returns: number - The random number
Example

random()
// Prints: 0.3141592653589793

initRandom(services) ⇒ Promise.<function()>

Instantiate the random service

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

import {
  DEFAULT_LOGGER,
  initLog,
  initRandom
} from 'common-services';

const log = await initLog({
  logger: DEFAULT_LOGGER,
});

const random = await initRandom({
  log,
});

initRandom~random() ⇒ number

Returns a new random number

Kind: inner method of initRandom
Returns: number - The random number
Example

random()
// Prints: 0.3141592653589793

initResolve(services) ⇒ Promise.<function()>

Instantiate the resolve service

Kind: 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

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 service

Kind: 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

import {
  DEFAULT_LOGGER,
  initLog,
  initTime,
} from 'common-services';

const log = await initLog({
  logger: DEFAULT_LOGGER,
});

const time = await initTime({
  log,
});

initTime~time() ⇒ number

Returns the current timestamp

Kind: inner method of initTime
Returns: number - The current timestamp
Example

time()
// Prints: 1326585600000

Authors

License

MIT

changelog (log de mudanças)

17.0.1 (2024-12-06)

17.0.0 (2024-12-04)

16.0.4 (2024-07-15)

Features

  • lrupool: add a LRU pool service (cf3a8cf)

16.0.3 (2024-05-28)

Bug Fixes

  • dependencies: fix dependencies updates (b973966)

16.0.2 (2024-05-28)

16.0.1 (2024-02-27)

Bug Fixes

  • log: avoid logging resolve service initialization (8fe1cbc)

16.0.0 (2024-02-27)

Features

15.1.0 (2024-02-24)

15.0.0 (2023-08-20)

Code Refactoring

  • process: remove the process service (59051e2)

BREAKING CHANGES

  • process: No more process service.

14.0.0 (2023-08-16)

13.0.0 (2023-08-12)

12.1.0 (2022-12-31)

Features

  • services: add new resolve and importer services (503d556)

12.0.0 (2022-11-10)

11.1.0 (2022-08-30)

11.0.1 (2022-06-15)

Bug Fixes

  • ci: remove old travis file (5ef03e8)
  • locks: fix the lock service (a9b75a1)

11.0.0 (2022-06-12)

10.0.3 (2022-05-29)

Bug Fixes

  • log: fix logs for undefined values (12fdc04)

10.0.2 (2022-05-25)

10.0.1 (2022-05-25)

10.0.0 (2021-11-01)

9.0.1 (2021-04-09)

9.0.0 (2020-11-22)

Bug Fixes

  • lock: fix memory leaks and release order (ebba7f7)

8.0.2 (2020-10-18)

8.0.1 (2020-09-14)

8.0.0 (2020-09-14)

7.1.5 (2020-07-17)

Bug Fixes

  • process: cleanup the process service listeners (8b91cac)

7.1.4 (2020-04-03)

7.1.3 (2020-04-03)

7.1.2 (2020-04-02)

7.1.1 (2020-04-01)

Bug Fixes

  • core: fix types for webpack esm builds (067cfed)

7.1.0 (2020-04-01)

Features

  • core: add support for esm (b1b17e3)

7.0.0 (2020-03-20)

6.2.0 (2020-01-19)

6.1.0 (2020-01-02)

Features

  • types: add configuration types (00a9948)

6.0.2 (2019-12-10)

6.0.1 (2019-12-07)

6.0.0 (2019-12-01)

Features

  • core: convert to TypeScript (5159ddd), closes #1

5.0.3 (2019-02-02)

Bug Fixes

  • Delay service: Fix provider declaration (0527c14)

5.0.2 (2019-02-02)

5.0.1 (2018-11-11)

5.0.0 (2018-11-04)

chore

  • Dependencies: Update dependencies (ee0bdbb)

BREAKING CHANGES

  • Dependencies: Drops NodeJS < 8 support

4.0.0 (2018-10-19)

3.2.0 (2018-09-27)

Features

  • Lock service: Add a service to manage locks on resources (34a2f00)

3.1.1 (2018-09-16)

3.1.0 (2018-09-16)

Features

  • Code generator service: Add a service to generate random human readable codes (799bcb2)

3.0.1 (2018-05-17)

Bug Fixes

  • Build: Update Babel build (c786630)

3.0.0 (2018-03-18)

2.0.0 (2018-03-17)

build

  • Metapak: Update metapak-nfroidure to the last version (a658044)

Features

  • Counter service: Add a simple counter service (2f45d49)

BREAKING CHANGES

  • Metapak: Main file moves to the dist/ folder.

1.0.0 (2017-12-02)

Features

  • Services: Add time, delay, random, process and log services (95cb505)