How to use the krl-stdlib/types.encode function in krl-stdlib

To help you get started, we’ve selected a few krl-stdlib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Picolab / pico-engine / packages / pico-engine-core / src / modules / event.js View on Github external
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()
    })
  }
github Picolab / pico-engine / packages / pico-engine-core / src / cleanQuery.js View on Github external
if(isBlank(query_orig && query_orig.eci)){
        throw new Error("missing query.eci");
    }
    if(isBlank(query_orig.rid)){
        throw new Error("missing query.rid");
    }
    if(isBlank(query_orig.name)){
        throw new Error("missing query.name");
    }

    var args = {};
    if(_.has(query_orig, "args")){
        //we want to make sure only json-able values are in the args
        //also want to clone it as to not mutate the original copy
        var attrs_json = ktypes.encode(query_orig.args);
        //only if it's a map or array do we consider it valid
        if(attrs_json[0] === "{" || attrs_json[0] === "["){
            args = ktypes.decode(attrs_json);
        }
    }

    return {
        eci: query_orig.eci.trim(),

        rid: query_orig.rid.trim(),

        name: query_orig.name.trim(),

        args: args,
    };
};
github Picolab / pico-engine / packages / pico-engine-core / src / modules / http.js View on Github external
async function httpBase (method, ctx, args) {
  let opts = {
    method: method,
    url: args.url,
    qs: ensureMap(args.qs, {}),
    headers: ensureMap(args.headers, {}),
    auth: ensureMap(args.auth)
  }

  if (_.has(args, 'body')) {
    opts.body = ktypes.toString(args.body)
  } else if (_.has(args, 'json')) {
    opts.body = ktypes.encode(args.json)
    if (!_.has(opts.headers, 'content-type')) {
      opts.headers['content-type'] = 'application/json'
    }
  } else if (_.has(args, 'form')) {
    opts.form = ensureMap(args.form)
  }
  if (args.dontFollowRedirect) {
    opts.followRedirect = false
  }

  let res = await requestP(opts)

  let r = {
    content: res.body,
    content_type: res.headers['content-type'],
    content_length: _.parseInt(res.headers['content-length'], 0) || 0,
github Picolab / pico-engine / packages / pico-engine-core / src / cleanEvent.js View on Github external
if(isBlank(event_orig && event_orig.eci)){
        throw new Error("missing event.eci");
    }
    if(isBlank(event_orig.domain)){
        throw new Error("missing event.domain");
    }
    if(isBlank(event_orig.type)){
        throw new Error("missing event.type");
    }

    var attrs = {};
    if(_.has(event_orig, "attrs")){
        //we want to make sure only json-able values are in the attrs
        //also want to clone it as to not mutate the original copy
        var attrs_json = ktypes.encode(event_orig.attrs);
        //only if it's a map or array do we consider it valid
        if(attrs_json[0] === "{" || attrs_json[0] === "["){
            attrs = ktypes.decode(attrs_json);
        }
    }

    var eid = ktypes.toString(event_orig.eid).trim();
    if(eid.length === 0 || eid === "null"){
        eid = "none";
    }

    return {

        eci: event_orig.eci.trim(),

        eid: eid,