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

clues

Package Overview
Dependencies
Maintainers
2
Versions
158
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clues - npm Package Compare versions

Comparing version 3.5.0-rc-3 to 3.5.0-rc-5

20

clues.js

@@ -102,3 +102,3 @@ (function(self) {

// If fn name is private or promise private is true, reject when called directly
if (fn && (!caller || caller == '__user__') && ((typeof(fn) === 'function' && fn.name == 'private') || (fn.then && fn.private)))
if (fn && (!caller || caller == '__user__') && ((typeof(fn) === 'function' && (fn.name == '$private' || fn.name == 'private')) || (fn.then && fn.private)))
return clues.Promise.rejected({ref : ref, message: ref+' not defined', fullref:fullref,caller: caller, notDefined:true});

@@ -147,3 +147,10 @@

duration = new Date();
return fn.apply(logic || {}, args);
return clues.Promise.try(function() {
return fn.apply(logic || {}, args);
})
.catch(function(e) {
if (e && e.stack && typeof $global.$logError === 'function')
$global.$logError(e, fullref);
throw e;
});
})

@@ -153,4 +160,5 @@ .then(function(d) {

$global.$duration(fullref,[(new Date()-duration),(new Date())-wait]);
return (typeof d == 'string' || typeof d == 'number') ? d : clues(logic,d,$global,caller,fullref,last);
},function(e) {
return (typeof d == 'string' || typeof d == 'number') ? d : clues(logic,d,$global,caller,fullref);
})
.catch(function(e) {
if (typeof e !== 'object')

@@ -162,6 +170,8 @@ e = { message : e};

e.caller = e.caller || caller || '';
if (fn && fn.name == '$noThrow')
return e;
throw e;
});
if (fn.name == 'private')
if (fn.name == 'private' || fn.name == '$private')
value.private = true;

@@ -168,0 +178,0 @@

{
"name": "clues",
"version": "3.5.0-rc-3",
"version": "3.5.0-rc-5",
"description": "Lightweight logic tree solver using promises.",

@@ -17,6 +17,6 @@ "keywords": [

"dependencies": {
"bluebird": "~3.1.1"
"bluebird": "~3.4.0"
},
"devDependencies": {
"mocha": "~2.3.4"
"mocha": "~2.5.3"
},

@@ -23,0 +23,0 @@ "license": "MIT",

@@ -87,3 +87,4 @@ [![NPM Version][npm-image]][npm-url]

* Any [array whose last element is a function](#using-special-arrays-to-define-functions) will be evaluated as a function... Angular style
* Any function explicitly named [`private`](#making-anonymous-functions-private) (regardless of the property name) will not be accessible directly
* Any function explicitly named [`$private`](#making-anonymous-functions-private) (regardless of the property name) will not be accessible directly
* Any function explicitly named `$noThrow` will return any error as an object not as a rejection (similar to `__` prefix in argument names)

@@ -148,2 +149,4 @@ That's pretty much it.

You can provide a function called `$logError` in the `$global` object to record any true javascript errors (with `.stack`) when they occur. The `$logError` function will be called with the error object as first argument and `fullref` as the second argument. For proper logging it is important to capture the error at source, as the dependencies might be optional - in which case the error never reaches the original request.
### making arguments optional with the underscore prefix

@@ -507,7 +510,7 @@ If any argument to a function resolves as a rejected promise (i.e. errored) then the function will not run and simply be rejected as well. But sometimes we want to continue nevertheless. Any argument to any function can be made optional by prefixing the argument name with an underscore. If the resolution of the optional argument returns a rejected promise (or the optional argument does not exist), then the value of this argument to the function will simply be `undefined`.

#### making anonymous functions private
An even easier way to declare functions as private is simply [naming](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) them `private`. Any functions named `private` will not be accessible directly, only indirectly through a different function, as an input argument. Specifically the `caller` has to be a defined value and not equal to `__user__`). Here is a quick example:
An even easier way to declare functions as private is simply [naming](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name) them `$private` (or `private`). Any functions named `$private` will not be accessible directly, only indirectly through a different function, as an input argument. Specifically the `caller` has to be a defined value and not equal to `__user__`). Here is a quick example:
```js
var facts = {
a : function private() { return 2; },
a : function $private() { return 2; },
b : function(a) { return a+2; }

@@ -514,0 +517,0 @@ };

@@ -73,4 +73,89 @@ var clues = require('../clues'),

});
describe('function named $noThrow',function() {
it('should return the error obj',function() {
return clues(facts,function $noThrow(DEP) {
return DEP;
})
.then(function(e) {
assert.equal(e.ref,'ERR');
assert.equal(e.message,'Could not process');
},function() {
throw 'Should return the error object';
});
});
it('should return the error obj for subsequent fn',function() {
return clues(facts,function $noThrow() {
return ['DEP',Object];
})
.then(function(e) {
assert.equal(e.ref,'ERR');
assert.equal(e.message,'Could not process');
},function() {
throw 'Should return the error object';
});
});
});
describe('$logError',function() {
var logic = {
stack_error: function() { throw new Error('error');},
stack_error_promise: function() { return clues.Promise.reject(new Error('error'));},
rejection: function() { throw 'error';},
rejection_promise: function() { return Promise.reject('error');},
optional: function(_stack_error,_rejection) {
return 'OK';
}
};
it('should log an error with stack',function() {
var facts = Object.create(logic),error,fullref;
return clues(facts,'stack_error',{$logError:function(e,f) {error = e;fullref = f;}})
.catch(Object)
.then(function() {
assert.equal(error && error.message,'error');
assert.equal(fullref,'stack_error');
});
});
it('should log a rejected promise with stack',function() {
var facts = Object.create(logic),error,fullref;
return clues(facts,'stack_error_promise',{$logError:function(e,f) {error = e; fullref = f;}})
.catch(Object)
.then(function() {
assert.equal(error && error.message,'error');
assert.equal(fullref,'stack_error_promise');
});
});
it('should not log an error with no stack',function() {
var facts = Object.create(logic),error;
return clues(facts,'rejection',{$logError:function(e) {error = e;}})
.catch(Object)
.then(function() {
assert.equal(error,undefined);
});
});
it('should not log a rejection with no stack',function() {
var facts = Object.create(logic),error;
return clues(facts,'rejection',{$logError:function(e) {error = e;}})
.catch(Object)
.then(function() {
assert.equal(error,undefined);
});
});
it('should log an error even if only used optionally',function() {
var facts = Object.create(logic),error;
return clues(facts,'optional',{$logError:function(e) {error = e;}})
.catch(Object)
.then(function() {
assert.equal(error.message,'error');
});
});
});
});
});

@@ -10,3 +10,3 @@ var clues = require('../clues'),

M1 : function() { return Promise.delay(100,10); },
M2 : function private() { return Promise.delay(20,300); },
M2 : function $private() { return Promise.delay(20,300); },
M3 : ['M1','M2',function private(M1,M2) { return M1+M2; }],

@@ -13,0 +13,0 @@ M4 : function() { return Promise.delay(150,70); },

@@ -12,3 +12,3 @@ var clues = require('../clues'),

if (!value || value instanceof Date) return value;
if (typeof value === 'function' && value.name === 'private') return undefined;
if (typeof value === 'function' && (value.name === 'private' || value.name === '$private')) return undefined;
if (typeof value === 'function' || value.length && typeof value[value.length-1] === 'function')

@@ -51,7 +51,20 @@ return debug ? '[Function]' : undefined;

err.status = 500;
if (typeof options.logger === 'function')
options.logger(err);
if (!options.debug) {
err.message = 'Internal Error';
delete err.stack;
err = {
error : true,
message : 'Internal Error',
status : 500
};
}
}
if (options.single)
return {
error : true,
message : err.message,
status : err.status,
xflash : err.xflash
};
return err;

@@ -110,3 +123,3 @@ }

txt[ref] = d;
txt = first+stringify(txt,pretty,options.debug);
txt = first+stringify(txt,pretty,options.debug,req);
first = '';

@@ -120,5 +133,4 @@ _res.write(txt.slice(1,txt.length-1)+',\t\n');

// The api request is either determined by options.select, req.param.fn or by remaining url
var data = (options.select || decodeURI((req.params && req.params.fn) || req.url.slice(1).replace(/\//g,'.').replace(/\?.*/,'')).split(','))
var data = (options.select || decodeURIComponent((req.params && req.params.fn) || req.url.slice(1).replace(/\//g,'.').replace(/\?.*/,'')).split(','))
.map(function(ref) {
ref = ref.replace(/\//g,'.');
if (ref === '' && options.debug) ref = facts;

@@ -129,3 +141,4 @@ return clues(facts,ref,$global,'__user__')

if (options.single) {
_res.send(d.error ? (d.status||400) : 200, stringify(d,pretty,options.debug)+'\n');
_res.status(d.error ? (d.status||400) : 200)
.end(stringify(d,pretty,options.debug,req));
_res.write = noop;

@@ -132,0 +145,0 @@ _res.end = noop;

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