telos-server 1.0.11 → 1.0.13

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telos-server",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "description": "An Extensible CMS Server for Telos Origin.",
5
5
  "main": "telosServer.json",
6
6
  "dependencies": {
package/serverUtils.js CHANGED
@@ -30,6 +30,33 @@ var fileTypes = [
30
30
  'mp4'
31
31
  ];
32
32
 
33
+ function getAllFiles(directory) {
34
+
35
+ let items = { };
36
+
37
+ if(Array.isArray(directory)) {
38
+
39
+ directory.forEach(value => {
40
+ Object.assign(items, getAllFiles(value));
41
+ });
42
+
43
+ return items;
44
+ }
45
+
46
+ fs.readdirSync(directory).forEach(value => {
47
+
48
+ value = directory + path.sep + value;
49
+
50
+ if(fs.statSync(value).isFile())
51
+ items[value] = getFileData(value);
52
+
53
+ else
54
+ Object.assign(items, getAllFiles(value));
55
+ })
56
+
57
+ return items;
58
+ }
59
+
33
60
  function getFile(uri, directories) {
34
61
 
35
62
  try {
@@ -47,37 +74,42 @@ function getFile(uri, directories) {
47
74
  directories
48
75
  ).map(file => {
49
76
 
50
- let type = file.includes(".") ?
51
- file.substring(file.lastIndexOf(".") + 1) : null;
77
+ let data = getFileData(file);
52
78
 
53
- let result = {
54
- file: file,
55
- folder: fs.lstatSync(file).isDirectory(),
56
- meta: file.split(/[\/\\]/).map(item =>
57
- item.split(".").slice(1).reduce(
58
- (value, item) => {
79
+ return data.meta.private == null ? data : null;
80
+ })[0];
81
+ }
59
82
 
60
- item = item.split("-");
83
+ catch(error) {
84
+ return null;
85
+ }
86
+ }
61
87
 
62
- let key = item[0].toLowerCase()
88
+ function getFileData(file) {
63
89
 
64
- if(key != type)
65
- value[key] = item.slice(1).join("-");
90
+ let type = file.includes(".") ?
91
+ file.substring(file.lastIndexOf(".") + 1) : null;
66
92
 
67
- return value;
68
- },
69
- { }
70
- )
71
- ).reduce((value, item) => Object.assign(value, item), { }),
72
- type: type
73
- }
93
+ return {
94
+ file: file,
95
+ folder: fs.lstatSync(file).isDirectory(),
96
+ meta: file.split(/[\/\\]/).map(item =>
97
+ item.split(".").slice(1).reduce(
98
+ (value, item) => {
74
99
 
75
- return result.meta.private == null ? result : null;
76
- })[0];
77
- }
100
+ item = item.split("-");
78
101
 
79
- catch(error) {
80
- return null;
102
+ let key = item[0].toLowerCase()
103
+
104
+ if(key != type)
105
+ value[key] = item.slice(1).join("-");
106
+
107
+ return value;
108
+ },
109
+ { }
110
+ )
111
+ ).reduce((value, item) => Object.assign(value, item), { }),
112
+ type: type
81
113
  }
82
114
  }
83
115
 
@@ -242,7 +274,9 @@ function processRequest(request, protocol, callback) {
242
274
  module.exports = {
243
275
  extensionTypes,
244
276
  fileTypes,
277
+ getAllFiles,
245
278
  getFile,
279
+ getFileData,
246
280
  getFiles,
247
281
  isHTTPJSON,
248
282
  processRequest
package/telosEngine.js ADDED
@@ -0,0 +1,40 @@
1
+ var busNet = use("bus-net");
2
+
3
+ var telosEngine = {
4
+ initialization: null,
5
+ interval: null,
6
+ query: (packet) => {
7
+
8
+ try {
9
+
10
+ packet = JSON.parse(packet);
11
+
12
+ if(packet.tags.includes("telos-origin") &&
13
+ packet.tags.includes("initialize")) {
14
+
15
+ telosEngine.initialization = packet;
16
+
17
+ telosEngine.interval = setInterval(
18
+ () => { busNet.call(`{"tags":["telos-engine"]}`); },
19
+ 1000 / 60
20
+ );
21
+
22
+ return;
23
+ }
24
+
25
+ if(packet.tags.includes("telos-configuration") &&
26
+ packet.tags.length == 1) {
27
+
28
+ return telosEngine.initialization = packet;
29
+ }
30
+ }
31
+
32
+ catch(error) {
33
+ return;
34
+ }
35
+ },
36
+ tags: ["telos-origin", "telos-engine"]
37
+ }
38
+
39
+ if(typeof module == "object")
40
+ module.exports = telosEngine;
@@ -1,7 +1,6 @@
1
1
  var fs = use("fs");
2
2
  var path = use("path");
3
3
  var pup = require("universal-preprocessor");
4
- var pupLangs = require("universal-preprocessor/pupLangs");
5
4
 
6
5
  let extensionTypes = {
7
6
  'txt': 'text/plain',
@@ -161,7 +160,7 @@ function middlewareText(packet, file) {
161
160
  function preprocess(string, file) {
162
161
 
163
162
  return (file != null ? file.meta.pup != null : true) ?
164
- pup.preprocess(pupLangs, string) : string;
163
+ pup.preprocess(string) : string;
165
164
  }
166
165
 
167
166
  module.exports = {
package/telosRouter.js CHANGED
@@ -16,6 +16,42 @@ var telosRouter = {
16
16
 
17
17
  packet = JSON.parse(packet);
18
18
 
19
+ if(packet.tags != null) {
20
+
21
+ if(packet.tags.length == 1 &&
22
+ packet.tags[0] == "telos-engine") {
23
+
24
+ telosRouter.tasks = telosRouter.tasks != null ?
25
+ telosRouter.tasks :
26
+ Object.values(
27
+ serverUtils.getAllFiles(
28
+ telosRouter.config.directories
29
+ )
30
+ ).filter(
31
+ item => item != null
32
+ ).filter(
33
+ item =>
34
+ Object.keys(item.meta).includes("task") &&
35
+ item.type == "js"
36
+ ).map(
37
+ item => item.file
38
+ );
39
+
40
+ telosRouter.tasks.forEach(item => {
41
+
42
+ try {
43
+ use(item)();
44
+ }
45
+
46
+ catch(error) {
47
+ console.log(error);
48
+ }
49
+ });
50
+
51
+ return;
52
+ }
53
+ }
54
+
19
55
  let middleware = [];
20
56
 
21
57
  apint.queryUtilities(
@@ -119,6 +155,7 @@ var telosRouter = {
119
155
  `
120
156
  };
121
157
  },
158
+ tasks: null,
122
159
  tags: ["telos-origin", "telos-router"]
123
160
  };
124
161
 
package/telosServer.js CHANGED
@@ -9,102 +9,127 @@ var telosServer = {
9
9
 
10
10
  serverUtils.processRequest(request, "http", (data) => {
11
11
 
12
- let responses = busNet.call(data);
12
+ (new Promise((resolve, reject) => {
13
13
 
14
- let status = 200;
14
+ let results = busNet.call(data);
15
+ let responses = [];
15
16
 
16
- let headers = {
17
- 'Content-Type': 'text/plain',
18
- 'Access-Control-Allow-Origin': '*',
19
- 'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
20
- };
17
+ results.forEach(item => {
21
18
 
22
- let body = [];
23
- let file = false;
19
+ if(item instanceof Promise) {
24
20
 
25
- let max = -Infinity;
21
+ item.then((result => {
26
22
 
27
- responses.filter(item => item != null).forEach(item => {
23
+ responses.push(result);
28
24
 
29
- item.priority = item.priority != null ? item.priority : 0;
25
+ if(responses.length == results.length)
26
+ resolve(responses);
27
+ }));
28
+ }
29
+
30
+ else
31
+ responses.push(item);
30
32
 
31
- if(item.priority > max)
32
- max = item.priority;
33
- });
33
+ if(responses.length == results.length)
34
+ resolve(responses);
35
+ });
36
+ })).then((responses) => {
34
37
 
35
- responses.filter(
36
- item => {
38
+ let status = 200;
37
39
 
38
- if(item == null)
39
- return
40
+ let headers = {
41
+ 'Content-Type': 'text/plain',
42
+ 'Access-Control-Allow-Origin': '*',
43
+ 'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
44
+ };
40
45
 
41
- if(item.priority != max)
42
- return false;
46
+ let body = [];
47
+ let file = false;
43
48
 
44
- delete item.priority;
49
+ let max = -Infinity;
45
50
 
46
- if(typeof item == "object") {
51
+ responses.filter(item => item != null).forEach(item => {
47
52
 
48
- if(item.file != null) {
49
-
50
- if(item.file)
51
- file = true;
53
+ item.priority = item.priority != null ? item.priority : 0;
54
+
55
+ if(item.priority > max)
56
+ max = item.priority;
57
+ });
58
+
59
+ responses.filter(
60
+ item => {
61
+
62
+ if(item == null)
63
+ return
52
64
 
53
- delete item.file;
65
+ if(item.priority != max)
66
+ return false;
67
+
68
+ delete item.priority;
69
+
70
+ if(typeof item == "object") {
71
+
72
+ if(item.file != null) {
73
+
74
+ if(item.file)
75
+ file = true;
76
+
77
+ delete item.file;
78
+ }
54
79
  }
80
+
81
+ return serverUtils.isHTTPJSON(item);
55
82
  }
56
-
57
- return serverUtils.isHTTPJSON(item);
58
- }
59
- ).filter(
60
- item => item.request == null
61
- ).forEach(item => {
83
+ ).filter(
84
+ item => item.request == null
85
+ ).forEach(item => {
62
86
 
63
- if(item.response != null) {
87
+ if(item.response != null) {
64
88
 
65
- if(item.response.status != null) {
89
+ if(item.response.status != null) {
66
90
 
67
- if(item.response.status > status)
68
- status = item.response.status;
91
+ if(item.response.status > status)
92
+ status = item.response.status;
93
+ }
69
94
  }
70
- }
71
95
 
72
- if(item.headers != null)
73
- Object.assign(headers, item.headers);
96
+ if(item.headers != null)
97
+ Object.assign(headers, item.headers);
74
98
 
75
- if(item.body != null)
76
- body.push("" + item.body);
77
- });
99
+ if(item.body != null)
100
+ body.push("" + item.body);
101
+ });
78
102
 
79
- response.writeHead(status, headers);
103
+ response.writeHead(status, headers);
80
104
 
81
- if(file) {
105
+ if(file) {
82
106
 
83
- if(body.length > 0) {
107
+ if(body.length > 0) {
84
108
 
85
- fs.readFile(body[0], function(error, data) {
109
+ fs.readFile(body[0], function(error, data) {
86
110
 
87
- if(error) {
88
- response.statusCode = 500;
89
- response.end(`ERROR: ${error}.`);
90
- }
91
-
92
- else
93
- response.end(data);
94
- });
111
+ if(error) {
112
+ response.statusCode = 500;
113
+ response.end(`ERROR: ${error}.`);
114
+ }
115
+
116
+ else
117
+ response.end(data);
118
+ });
119
+ }
95
120
  }
96
- }
97
121
 
98
- else {
122
+ else {
99
123
 
100
- if(body.length == 1)
101
- response.write(body[0]);
102
-
103
- else if(body.length > 1)
104
- response.write(JSON.stringify(body));
105
-
106
- response.end();
107
- }
124
+ if(body.length == 1)
125
+ response.write(body[0]);
126
+
127
+ else if(body.length > 1)
128
+ response.write(JSON.stringify(body));
129
+
130
+ response.end();
131
+ }
132
+ });
108
133
  });
109
134
  }),
110
135
  query: (packet) => {
package/telosServer.json CHANGED
@@ -1,5 +1,11 @@
1
1
  {
2
2
  "utilities": {
3
+ "telosEngine": {
4
+ "source": "telos-server/telosEngine.js",
5
+ "properties": {
6
+ "type": "bus-module"
7
+ }
8
+ },
3
9
  "telosServer": {
4
10
  "source": "telos-server/telosServer.js",
5
11
  "properties": {