Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

async-deco

Package Overview
Dependencies
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-deco - npm Package Compare versions

Comparing version 7.0.4 to 7.1.0

2

package.json
{
"name": "async-deco",
"version": "7.0.4",
"version": "7.1.0",
"description": "A collection of decorators for adding features to asynchronous functions (callback or promise based).",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -65,11 +65,20 @@ async-deco

var logger = addLogger(function (event, payload, ts) {
function log(event, payload, ts, key) {
// log here
});
}
function getKey(...) { // same arguments as the decorated function
return key;
}
var logger = addLogger(log, getKey);
```
This methods is called with the following arguments:
The decorator is created passing 2 functions. A log function and an optional getKey function.
The log function is called with the following arguments:
* evt: the name of the event
* payload: an object with additional information about this event
* ts: the time stamp for this event (in ms)
* key: a string representing a single execution. It can be used to understand what logs belong to the same execution.
The getKey function (optional) takes the same arguments of the decorated function and returns the key (see descrition above). The default is a random string.
The resulting decorator can wrap a function:

@@ -81,3 +90,3 @@ ```js

var log = defaultLogger.call(this);
log('event-name', { ... data ... })
log('event-name', { ... data ... });
});

@@ -94,4 +103,4 @@ ```

var decorator = compose(
addLogger(function (evt, payload, ts) {
console.log(ts, evt, payload);
addLogger(function (evt, payload, ts, key) {
console.log(ts, evt, payload, key);
}),

@@ -113,7 +122,7 @@ logDecorator(),

```js
1459770371655, "start", undefined
1459770371675, "timeout", { ms: 20 }
1459770371675, "retry", { times: 1 }
1459770371695, "timeout", { ms: 20 }
1459770371700, "end", { result: ... }
1459770371655, "start", undefined "12345"
1459770371675, "timeout", { ms: 20 } "12345"
1459770371675, "retry", { times: 1 } "12345"
1459770371695, "timeout", { ms: 20 } "12345"
1459770371700, "end", { result: ... } "12345"
```

@@ -147,19 +156,5 @@

---------
It enables the logging for the whole chain of decorators (if combined together).
```js
var addLogger = require('async-deco/utils/add-logger');
It enables the logging for the whole chain of decorators. Read the description in the "Logging" paragraph.
You can use this decorator multiple times to add multiple loggers (and multiple keys).
var logger = addLogger(function (event, payload, ts) {
// log here
});
var f = logger(function () { /* decorated function */ });
```
The logger takes these arguments:
* evt: the name of the event
* payload: an object with additional information about this event
* ts: the timestamp for this event
You can use this decorator multiple times to add multiple loggers.
Log

@@ -166,0 +161,0 @@ ---

@@ -5,2 +5,6 @@ var assert = require('chai').assert;

function getLogkey() {
return 'key';
}
describe('log (callback)', function () {

@@ -13,8 +17,8 @@ var wrapped;

log = [];
var logger = function (type, obj, ts) {
log.push({type: type, obj: obj});
var logger = function (type, obj, ts, key) {
log.push({type: type, obj: obj, key: key});
};
wrapped = logDecorator();
addLog = addLogger(logger);
addLog = addLogger(logger, getLogkey);
});

@@ -29,4 +33,4 @@

assert.deepEqual(log, [
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}},
{type: 'log-end', obj: {result: 6}}
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}, key: 'key'},
{type: 'log-end', obj: {result: 6}, key: 'key'}
]);

@@ -44,4 +48,4 @@ done();

assert.deepEqual(log, [
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}},
{type: 'log-error', obj: {err: new Error('error!')}}
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}, key: 'key'},
{type: 'log-error', obj: {err: new Error('error!')}, key: 'key'}
]);

@@ -48,0 +52,0 @@ done();

@@ -5,2 +5,6 @@ var assert = require('chai').assert;

function getLogkey() {
return 'key';
}
describe('log (promise)', function () {

@@ -13,8 +17,8 @@ var wrapped;

log = [];
var logger = function (type, obj, ts) {
log.push({type: type, obj: obj});
var logger = function (type, obj, ts, key) {
log.push({type: type, obj: obj, key: key});
};
wrapped = logDecorator();
addLog = addLogger(logger);
addLog = addLogger(logger, getLogkey);
});

@@ -32,4 +36,4 @@

assert.deepEqual(log, [
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}},
{type: 'log-end', obj: {result: 6}}
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}, key: 'key'},
{type: 'log-end', obj: {result: 6}, key: 'key'}
]);

@@ -49,4 +53,4 @@ done();

assert.deepEqual(log, [
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}},
{type: 'log-error', obj: {err: new Error('error!')}}
{type: 'log-start', obj: {args: [1, 2, 3], context: log[0].obj.context}, key: 'key'},
{type: 'log-error', obj: {err: new Error('error!')}, key: 'key'}
]);

@@ -53,0 +57,0 @@ done();

var buildLogger = require('./build-logger');
var keyGetter = require('memoize-cache-utils/key-getter');
function addLogger(log) {
function addLogger(log, getKey) {
getKey = keyGetter(getKey || function () { return Math.floor(Math.random() * 10000000).toString(); });
return function (func) {
return function () {
var context = buildLogger(this, log);
var args = Array.prototype.slice.call(arguments, 0);
var logKey = getKey.apply(this, args);
var context = buildLogger(this, log, logKey);
return func.apply(context, args);

@@ -9,0 +12,0 @@ };

@@ -16,6 +16,7 @@ // gets an object and a logger function.

var ts = Date.now();
var logger;
var logger, logKey;
for (var i = 0; i < this.__log_funcs.length; i++) {
logger = this.__log_funcs[i];
logger(evt, payload, ts);
logger = this.__log_funcs[i].logger;
logKey = this.__log_funcs[i].logKey;
logger(evt, payload, ts, logKey);
}

@@ -33,9 +34,8 @@ }).bind(this),

module.exports = function buildLogger(obj, logger) {
module.exports = function buildLogger(obj, logger, logKey) {
if (!(typeof obj === 'object' && '__log' in obj)) {
obj = wrap(obj);
obj = wrap(obj); // it only decorate once
}
obj.__log_funcs.push(logger);
obj.__log_funcs.push({ logger: logger, logKey: logKey });
return obj;
};
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc