pico-engine 1.0.9 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,5 @@
1
1
  # pico-engine
2
2
 
3
- [![Build Status](https://travis-ci.org/Picolab/pico-engine.svg?branch=master)](https://travis-ci.org/Picolab/pico-engine)
4
3
  [![Node version](https://img.shields.io/node/v/pico-engine.svg)](https://nodejs.org/en/download/)
5
4
 
6
5
  An implementation of the [pico-engine](http://www.windley.com/archives/2016/03/rebuilding_krl.shtml) hosted on node.js
@@ -36,7 +36,7 @@ function getUrlFilename(url) {
36
36
  }
37
37
  function RulesetRegistryLoaderFs(engineHomeDir) {
38
38
  const rulesetDir = path.resolve(engineHomeDir, "rulesets");
39
- const db = levelup_1.default(encode(leveldown_1.default(path.resolve(engineHomeDir, "rulesets-db")), {
39
+ const db = (0, levelup_1.default)(encode((0, leveldown_1.default)(path.resolve(engineHomeDir, "rulesets-db")), {
40
40
  keyEncoding: charwise,
41
41
  valueEncoding: safeJsonCodec,
42
42
  }));
@@ -106,7 +106,7 @@ async function fetchKrl(url) {
106
106
  });
107
107
  });
108
108
  }
109
- const res = await cross_fetch_1.default(url);
109
+ const res = await (0, cross_fetch_1.default)(url);
110
110
  if (res.status < 200 || res.status >= 300) {
111
111
  throw new Error("Got a statusCode=" + res.status + " for: " + url);
112
112
  }
package/dist/cli.js CHANGED
@@ -41,7 +41,7 @@ if (typeof process.env.PICO_ENGINE_BASE_URL === "string" &&
41
41
  process.env.PICO_ENGINE_BASE_URL.length > 0) {
42
42
  env.base_url = process.env.PICO_ENGINE_BASE_URL;
43
43
  }
44
- index_1.startEngine(env).catch((err) => {
44
+ (0, index_1.startEngine)(env).catch((err) => {
45
45
  console.error("Failed to start engine.");
46
46
  console.error(err);
47
47
  process.exit(1);
package/dist/index.js CHANGED
@@ -24,24 +24,24 @@ async function startEngine(configuration = {}) {
24
24
  const logFilePath = path.resolve(home, "pico-engine.log");
25
25
  const log = configuration.log
26
26
  ? configuration.log
27
- : krl_stdlib_1.makeKrlLogger(logging_1.makeRotatingFileLogWriter(logFilePath));
27
+ : (0, krl_stdlib_1.makeKrlLogger)((0, logging_1.makeRotatingFileLogWriter)(logFilePath));
28
28
  const core = new pico_engine_core_1.PicoEngineCore({
29
- leveldown: leveldown_1.default(path.resolve(home, "db")),
30
- rsRegLoader: RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs(home),
29
+ leveldown: (0, leveldown_1.default)(path.resolve(home, "db")),
30
+ rsRegLoader: (0, RulesetRegistryLoaderFs_1.RulesetRegistryLoaderFs)(home),
31
31
  log,
32
32
  modules: configuration.modules,
33
33
  useEventInputTime: configuration.useEventInputTime,
34
34
  getPicoLogs(picoId) {
35
- return logging_1.getPicoLogs(logFilePath, picoId);
35
+ return (0, logging_1.getPicoLogs)(logFilePath, picoId);
36
36
  },
37
37
  });
38
38
  await core.start();
39
39
  const rsRegistry = core.rsRegistry;
40
40
  const pf = core.picoFramework;
41
41
  const krl_urls = [
42
- toFileUrl_1.toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.pico-engine-ui.krl")),
43
- toFileUrl_1.toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
44
- toFileUrl_1.toFileUrl(path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")),
42
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.pico-engine-ui.krl")),
43
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.wrangler.krl")),
44
+ (0, toFileUrl_1.toFileUrl)(path.resolve(__dirname, "..", "krl", "io.picolabs.subscription.krl")),
45
45
  ];
46
46
  for (const url of krl_urls) {
47
47
  const { ruleset } = await rsRegistry.flush(url);
@@ -67,7 +67,7 @@ async function startEngine(configuration = {}) {
67
67
  time: 0,
68
68
  });
69
69
  const uiECI = uiChannel.id;
70
- const app = server_1.server(core, uiECI);
70
+ const app = (0, server_1.server)(core, uiECI);
71
71
  if ((!port || !_.isInteger(port) || port < 1) && port !== 0) {
72
72
  port = 3000;
73
73
  }
@@ -93,7 +93,7 @@ async function startEngine(configuration = {}) {
93
93
  domain: "engine",
94
94
  name: "started",
95
95
  data: { attrs: {} },
96
- time: 0,
96
+ time: 0, // TODO remove this typescript requirement
97
97
  }).catch((error) => {
98
98
  log.error("Error signaling engine:started event", { error });
99
99
  // TODO signal all errors engine:error
package/dist/server.js CHANGED
@@ -18,6 +18,17 @@ function server(core, uiECI) {
18
18
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
19
19
  next();
20
20
  });
21
+ app.use('*', (req, res, next) => {
22
+ if (req.method == "OPTIONS") {
23
+ res.header("Allow", "POST, GET, OPTIONS");
24
+ res.header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
25
+ res.status(200);
26
+ res.send();
27
+ }
28
+ else {
29
+ next();
30
+ }
31
+ });
21
32
  app.use(helmet());
22
33
  app.use(express.static(path.resolve(__dirname, "..", "public")));
23
34
  app.use(bodyParser.json({
@@ -63,7 +74,7 @@ function server(core, uiECI) {
63
74
  domain: req.params.domain,
64
75
  name: req.params.name,
65
76
  data: { attrs: mergeGetPost(req) },
66
- time: 0,
77
+ time: 0, // TODO remove this typescript requirement
67
78
  })
68
79
  .then((eid) => {
69
80
  res.json(eid);
@@ -77,7 +88,7 @@ function server(core, uiECI) {
77
88
  domain: req.params.domain,
78
89
  name: req.params.name,
79
90
  data: { attrs: mergeGetPost(req) },
80
- time: 0,
91
+ time: 0, // TODO remove this typescript requirement
81
92
  })
82
93
  .then((data) => {
83
94
  res.json(data);
@@ -95,9 +106,27 @@ function server(core, uiECI) {
95
106
  domain: req.params.domain,
96
107
  name: req.params["type"],
97
108
  data: { attrs },
98
- time: 0,
109
+ time: 0, // TODO remove this typescript requirement
99
110
  })
100
111
  .then((data) => {
112
+ if (data.directives) {
113
+ var _res = _.filter(data.directives, { name: "_cookie" });
114
+ if (_res) {
115
+ _.forEach(_res, function (v) {
116
+ if (v.options && v.options.cookie) {
117
+ res.append("Set-Cookie", v.options.cookie);
118
+ }
119
+ });
120
+ }
121
+ var _res = _.filter(data.directives, { name: "_redirect" });
122
+ if (_res) {
123
+ _.forEach(_res, function (v) {
124
+ if (v.options && v.options.url) {
125
+ return res.redirect(v.options.url);
126
+ }
127
+ });
128
+ }
129
+ }
101
130
  res.json(data);
102
131
  })
103
132
  .catch(next);
@@ -110,7 +139,7 @@ function server(core, uiECI) {
110
139
  domain: req.params.domain,
111
140
  name: req.params.name,
112
141
  data: { attrs },
113
- time: 0,
142
+ time: 0, // TODO remove this typescript requirement
114
143
  }, {
115
144
  eci: req.params.eci,
116
145
  rid: req.params.rid,
@@ -123,15 +152,26 @@ function server(core, uiECI) {
123
152
  .catch(next);
124
153
  });
125
154
  app.all("/c/:eci/query/:rid/:name", function (req, res, next) {
155
+ const funcPart = req.params.name.split(".");
126
156
  core
127
157
  .query({
128
158
  eci: req.params.eci,
129
159
  rid: req.params.rid,
130
- name: req.params.name,
160
+ name: funcPart[0],
131
161
  args: mergeGetPost(req),
132
162
  })
133
163
  .then((data) => {
134
- res.json(data);
164
+ if (funcPart[1] === "html") {
165
+ res.header("Content-Type", "text/html");
166
+ res.end(data);
167
+ }
168
+ else if (funcPart[1] === "txt") {
169
+ res.header("Content-Type", "text/plain");
170
+ res.end(data);
171
+ }
172
+ else {
173
+ res.json(data);
174
+ }
135
175
  })
136
176
  .catch(next);
137
177
  });
@@ -13,7 +13,8 @@ ruleset io.picolabs.pico-engine-ui {
13
13
  .head()
14
14
  }
15
15
  getOtherUiECI = function(eci){
16
- return eci => ctx:query(eci, ctx:rid, "uiECI") | null
16
+ thisPico = ctx:channels.any(function(c){c{"id"}==eci})
17
+ return (eci && not thisPico) => ctx:query(eci, ctx:rid, "uiECI") | null
17
18
  }
18
19
  testingECI = function(){
19
20
  return ent:testingECI
@@ -166,12 +167,12 @@ ruleset io.picolabs.pico-engine-ui {
166
167
  }
167
168
  }
168
169
  always {
169
- ent:x := event:attrs{"x"}.as("Number") if event:attrs >< "x"
170
- ent:y := event:attrs{"y"}.as("Number") if event:attrs >< "y"
171
- ent:width := event:attrs{"width"}.as("Number") if event:attrs >< "width"
172
- ent:height := event:attrs{"height"}.as("Number") if event:attrs >< "height"
173
- ent:name := event:attrs{"name"}.as("String") if event:attrs >< "name"
174
- ent:backgroundColor := event:attrs{"backgroundColor"}.validateColor() if event:attrs >< "backgroundColor"
170
+ ent:x := event:attrs{"x"}.as("Number") if event:attrs{"x"}
171
+ ent:y := event:attrs{"y"}.as("Number") if event:attrs{"y"}
172
+ ent:width := event:attrs{"width"}.as("Number") if event:attrs{"width"}
173
+ ent:height := event:attrs{"height"}.as("Number") if event:attrs{"height"}
174
+ ent:name := event:attrs{"name"}.as("String") if event:attrs{"name"}
175
+ ent:backgroundColor := event:attrs{"backgroundColor"}.validateColor() if event:attrs{"backgroundColor"}
175
176
  }
176
177
  }
177
178
  rule new {
@@ -14,12 +14,12 @@ ruleset io.picolabs.wrangler {
14
14
  >>
15
15
  author "BYU Pico Lab"
16
16
 
17
- provides skyQuery,
17
+ provides skyQuery, picoQuery,
18
18
  channels, createChannel, deleteChannel, //channel
19
19
  rulesetConfig, rulesetMeta, installedRIDs, //ruleset
20
20
  children, parent_eci, name, myself //pico
21
21
 
22
- shares skyQuery,
22
+ shares skyQuery, picoQuery,
23
23
  channels, //channel
24
24
  rulesetConfig, rulesetMeta,installedRIDs, //ruleset
25
25
  children, parent_eci, name, myself, id //pico
@@ -31,7 +31,6 @@ ruleset io.picolabs.wrangler {
31
31
  { "name": "installedRIDs"},
32
32
  {"name":"skyQuery", "args":["eci", "mod", "func", "params","_host","_path","_root_url"]},
33
33
  {"name":"children", "args":[]}
34
- //{"name":"children", "args":["name", "allowRogue"]},
35
34
  ],
36
35
  "events": [
37
36
  { "domain": "wrangler", "name": "new_child_request",
@@ -58,7 +57,7 @@ ruleset io.picolabs.wrangler {
58
57
  // ********************************************************************************************
59
58
 
60
59
  /*
61
- skyQuery is used to programmatically call function inside of other picos from inside a rule.
60
+ picoQuery is used to programmatically call function inside of other picos from inside a rule.
62
61
  parameters;
63
62
  eci - The eci of the pico which contains the function to be called
64
63
  mod - The ruleset ID or alias of the module
@@ -73,13 +72,13 @@ ruleset io.picolabs.wrangler {
73
72
  _root_url - The entire url except eci, mod, func.
74
73
  For example, dependent on _host and _path is
75
74
  "http://localhost:8080/sky/cloud/", which also is the default.
76
- skyQuery on success (if status code of request is 200) returns results of the called function.
77
- skyQuery on failure (if status code of request is not 200) returns a Map of error information which contains;
75
+ picoQuery on success (if status code of request is 200) returns results of the called function.
76
+ picoQuery on failure (if status code of request is not 200) returns a Map of error information which contains;
78
77
  error - general error message.
79
78
  httpStatus - status code returned from http get command.
80
- skyQueryError - The value of the "error key", if it exist, aof the function results.
81
- skyQueryErrorMsg - The value of the "error_str", if it exist, of the function results.
82
- skyQueryReturnValue - The function call results.
79
+ picoQueryError - The value of the "error key", if it exist, aof the function results.
80
+ picoQueryErrorMsg - The value of the "error_str", if it exist, of the function results.
81
+ picoQueryReturnValue - The function call results.
83
82
  */
84
83
  QUERY_SELF_INVALID_HTTP_MAP = {"status_code": 400,
85
84
  "status_line":"HTTP/1.1 400 Pico should not query itself",
@@ -110,9 +109,9 @@ ruleset io.picolabs.wrangler {
110
109
  response_content = response{"content"}.decode();
111
110
  response_error = (response_content.typeof() == "Map" && (not response_content{"error"}.isnull())) => response_content{"error"} | 0;
112
111
  response_error_str = (response_content.typeof() == "Map" && (not response_content{"error_str"}.isnull())) => response_content{"error_str"} | 0;
113
- error = error_info.put({"skyQueryError": response_error,
114
- "skyQueryErrorMsg": response_error_str,
115
- "skyQueryReturnValue": response_content});
112
+ error = error_info.put({"picoQueryError": response_error,
113
+ "picoQueryErrorMsg": response_error_str,
114
+ "picoQueryReturnValue": response_content});
116
115
  is_bad_response = (response_content.isnull() || (response_content == "null") || response_error || response_error_str);
117
116
  // if HTTP status was OK & the response was not null and there were no errors...
118
117
  (status == 200 && not is_bad_response) => response_content | error.klog("error: ")
@@ -126,6 +125,32 @@ ruleset io.picolabs.wrangler {
126
125
  response = (not thisPico) => http:get(web_hook, {}.put(params)) | QUERY_SELF_INVALID_HTTP_MAP;
127
126
  processHTTPResponse(response)
128
127
  }
128
+
129
+ /*
130
+ picoQuery() is an improved skyQuery() that automatically queries picos on the same engine using ctx:query() to avoid
131
+ unnecessary HTTP calls and channel policy issues (family channels can't be accessed over HTTP).
132
+
133
+ The parameters are the same.
134
+ */
135
+
136
+ // this does nothing now, but it may be that we need to return error codes.
137
+ processQueryResponse = function(response) {
138
+ response
139
+ }
140
+
141
+ picoQuery = function(eci, mod, func, params,_host,_path,_root_url) {
142
+ thisPico = ctx:channels.any(function(c){c{"id"}==eci})
143
+ thisHost = _host.isnull() || _host == meta:host;
144
+ web_hook = buildWebHook(eci, mod, func, _host, _path, _root_url)
145
+
146
+ response = (not thisPico) => ((not thisHost) => processHTTPResponse(http:get(web_hook, {}.put(params)))
147
+ | processQueryResponse(ctx:query(eci, mod, func, {}.put(params))))
148
+ | QUERY_SELF_INVALID_HTTP_MAP;
149
+
150
+ response
151
+ }
152
+
153
+
129
154
 
130
155
  // ********************************************************************************************
131
156
  // *** Rulesets ***
@@ -184,7 +209,7 @@ ruleset io.picolabs.wrangler {
184
209
  }
185
210
  }
186
211
 
187
- children = function(name, allowRogue = true) {
212
+ children = function() {
188
213
  convert = function(eci){
189
214
  pico = {}
190
215
  .put("eci",eci)
@@ -350,9 +375,7 @@ ruleset io.picolabs.wrangler {
350
375
  eci=newEci,
351
376
  domain="wrangler",
352
377
  name="pico_created",
353
- attrs={
354
- "name": name
355
- }
378
+ attrs=event:attrs
356
379
  )
357
380
  ctx:event(
358
381
  eci=newUiECI,
@@ -414,6 +437,14 @@ ruleset io.picolabs.wrangler {
414
437
  }
415
438
  }
416
439
 
440
+ rule take_note_of_name_change {
441
+ select when wrangler name_changed
442
+ name re#(.+)# // required
443
+ setting(name)
444
+ fired {
445
+ ent:name := name
446
+ }
447
+ }
417
448
 
418
449
  //-------------------- PARENT PERSPECTIVE ----------------------
419
450
  rule deleteOneChild {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pico-engine",
3
- "version": "1.0.9",
3
+ "version": "1.2.0",
4
4
  "description": "An implementation of the pico-engine targeting node.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -50,8 +50,8 @@
50
50
  "express": "^4.16.4",
51
51
  "helmet": "^3.16.0",
52
52
  "home-dir": "^1.0.0",
53
- "krl-compiler": "^1.0.6",
54
- "krl-stdlib": "^1.0.6",
53
+ "krl-compiler": "^1.2.0",
54
+ "krl-stdlib": "^1.2.0",
55
55
  "level-json-coerce-null": "^1.0.1",
56
56
  "leveldown": "^5.0.2",
57
57
  "levelup": "^4.0.1",
@@ -59,7 +59,7 @@
59
59
  "make-dir": "^3.0.0",
60
60
  "memdown": "^5.1.0",
61
61
  "minimist": "^1.2.5",
62
- "pico-engine-core": "^1.0.8",
62
+ "pico-engine-core": "^1.2.0",
63
63
  "pico-framework": "^0.6.0",
64
64
  "rotating-file-stream": "^1.4.1",
65
65
  "split": "^1.0.1",
@@ -77,7 +77,7 @@
77
77
  "onchange": "^6.0.0",
78
78
  "scriptsp": "^1.1.1",
79
79
  "temp-dir": "^2.0.0",
80
- "ts-node": "^9.1.1",
80
+ "ts-node": "^10.4.0",
81
81
  "typescript": "^4.1.2"
82
82
  },
83
83
  "ava": {
@@ -89,5 +89,5 @@
89
89
  "ts-node/register"
90
90
  ]
91
91
  },
92
- "gitHead": "3d3411499c4a2d5baf36b3178e906b53d336cf73"
92
+ "gitHead": "7052049b68ebf82ac0042dfcfbd8c71470e95652"
93
93
  }
@@ -50,13 +50,17 @@ Raise an event to yourself. This event does not use a channel, only runs on the
50
50
 
51
51
  ## ctx:query(eci, rid, name, args = {})
52
52
 
53
+ Query a local pico.
54
+
53
55
  - `eci` - String - Which channel to send the query over.
54
56
  - `rid` - String - The rid you wish to query.
55
- - `name` - String - The name from the ruleset you wish to query.
57
+ - `name` - String - The name of the function you wish to query.
56
58
  - `args` - Map - Any query function arguments where the map keys match the argument name.
57
59
 
58
60
  ## ctx:event(eci, domain, name, attrs = {})
59
61
 
62
+ Send an event to a local pico.
63
+
60
64
  - `eci` - String - Which channel to signal the event to.
61
65
  - `domain` - String - The event domain.
62
66
  - `name` - String - The event name.