Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}, function(err){
if(err) return callback(err);
var root = {
type: ktypes.typeOf(val)
};
switch(root.type){
case "Null":
root.value = null;
break;
case "Function":
case "Action":
root.type = "String";
root.value = ktypes.toString(val);
break;
case "Map":
case "Array":
_.each(val, function(v, k){
ops.push({
type: "put",
key: key_prefix.concat(["value", k]),
value: v,
});
});
// this `value` helps _.set in the toObj db dump set the right type
root.value = root.type === "Array" ? [] : {};
break;
default:
root.value = val;
}
], async function (ctx, args) {
if (!_.has(args, 'url')) {
throw new Error('http:' + method.toLowerCase() + ' needs a url string')
}
if (!ktypes.isString(args.url)) {
throw new TypeError('http:' + method.toLowerCase() + ' was given ' + ktypes.toString(args.url) + ' instead of a url string')
}
if (_.has(args, 'autosend')) {
const event = cleanEvent(args.autosend)
httpBase(method, ctx, args)
.then(r => {
core.signalEvent(Object.assign({}, event, {
attrs: Object.assign({}, event.attrs, r)
}))
})
.catch(err => {
ctx.log('error', err + '')// TODO better handling
})
return
}
var onKV = function (data) {
var keyPrefix = data.key
var val = data.value
// NOTE: not sharing code with DB.js b/c migrations should be immutable
// i.e. produce the same result regardless of previous codebase states
var indexType = ktypes.typeOf(val)
var rootValue = { type: indexType }
switch (indexType) {
case 'Null':
rootValue.value = null
break
case 'Function':
case 'Action':
rootValue.type = 'String'
rootValue.value = ktypes.toString(val)
break
case 'Map':
case 'Array':
_.each(val, function (v, k) {
dbOps.push({
type: 'put',
key: keyPrefix.concat(['value', k]),
var putPVar = function(ldb, key_prefix, query, val, callback){
query = ktypes.isNull(query) ? [] : query;
query = ktypes.isArray(query) ? query : [query];
// do this before toKeyPath b/c it will convert all parts to stings
var isArrayIndex = _.isInteger(query[0]) && query[0] >= 0;
var path = toKeyPath(query);
if(_.size(path) > 0){
var subkeyPrefix = key_prefix.concat(["value", path[0]]);
var subPath = _.tail(path);
ldb.get(key_prefix, function(err, oldRoot){
if(err && ! err.notFound){
callback(err);
return;
}
var ops = [];
var type = oldRoot && oldRoot.type;
if(type !== "Map" && type !== "Array"){
type = isArrayIndex ? "Array" : "Map";
}
ctx = core.mkCTX({
query: ctx.query,
pico_id: ctx.pico_id,
rid: rs.rid,
scope: rs.scope
})
var val = ctx.scope.get(ctx.query.name)
if (_.isFunction(val)) {
val = await runKRL(function (ctx, args) {
// use ctx.applyFn so it behaves like any other fn call
// i.e. errors on trying defaction like a function
return ctx.applyFn(val, ctx, args)
}, ctx, ctx.query.args)
}
// To ensure we don't leak out functions etc.
return ktypes.decode(ktypes.encode(val))
}
event = cleanEvent(args.event)
} catch (err) {
return callback(err)
}
if (args.host) {
var url = args.host
url += '/sky/event'
url += '/' + event.eci
url += '/' + event.eid
url += '/' + event.domain
url += '/' + event.type
request({
method: 'POST',
url: url,
headers: {'content-type': 'application/json'},
body: ktypes.encode(event.attrs)
}, function (err, res, body) {
if (err) {
ctx.log('error', err + '')// TODO better handling
}
// ignore
})
callback()
return
}
core.signalEvent(event)
callback()
})
}
module.exports = async function runAction (ctx, domain, id, args, setting) {
var returns = []
if (domain) {
var modAction = await ctx.modules.get(ctx, domain, id)
if (!ktypes.isAction(modAction)) {
throw new Error('`' + domain + ':' + id + '` is not an action')
}
returns = await modAction(ctx, args)
} else if (id === 'noop') {
returns = []// returns nothing
} else if (ctx.scope.has(id)) {
var definedAction = ctx.scope.get(id)
if (!ktypes.isAction(definedAction)) {
throw new Error('`' + id + '` is not defined as an action')
}
returns = await definedAction(ctx, args)
} else if (id === 'send_directive' || id === 'sendDirective') {
returns = await sendDirective(ctx, args)
} else {
throw new Error('`' + id + '` is not defined')
}
_.each(setting, function (id, i) {
var val = returns[i]
if (val === void 0 || _.isNaN(val)) {
val = null
}
ctx.scope.set(id, val)
})
}
module.exports = async function runAction (ctx, domain, id, args, setting) {
var returns = []
if (domain) {
var modAction = await ctx.modules.get(ctx, domain, id)
if (!ktypes.isAction(modAction)) {
throw new Error('`' + domain + ':' + id + '` is not an action')
}
returns = await modAction(ctx, args)
} else if (id === 'noop') {
returns = []// returns nothing
} else if (ctx.scope.has(id)) {
var definedAction = ctx.scope.get(id)
if (!ktypes.isAction(definedAction)) {
throw new Error('`' + id + '` is not defined as an action')
}
returns = await definedAction(ctx, args)
} else if (id === 'send_directive' || id === 'sendDirective') {
returns = await sendDirective(ctx, args)
} else {
throw new Error('`' + id + '` is not defined')
}
var delPVar = function(ldb, key_prefix, query, callback){
var path = ktypes.isNull(query) ? [] : toKeyPath(query);
if(_.size(path) > 0){
key_prefix = key_prefix.concat(["value", _.head(path)]);
var sub_path = _.tail(path);
if( ! _.isEmpty(sub_path)){
ldb.get(key_prefix, function(err, data){
if(err && err.notFound){
data = {};
}else if(err){
callback(err);
return;
}
var val = _.omit(data, sub_path);
if(_.isEmpty(val)){
ldb.del(key_prefix, callback);
}else{
ldb.put(key_prefix, val, callback);
if (!_.has(args, 'name')) {
throw new Error('engine:newChannel needs a name string')
}
if (!_.has(args, 'type')) {
throw new Error('engine:newChannel needs a type string')
}
picoId = await core.db.assertPicoID(picoId)
policyId = await core.db.assertPolicyID(policyId)
return core.db.newChannel({
pico_id: picoId,
name: ktypes.toString(args.name),
type: ktypes.toString(args.type),
policy_id: policyId
})
}),