telos-server 1.0.11 → 1.0.12
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 +1 -1
- package/telosMiddleware.js +1 -2
- package/telosServer.js +91 -66
package/package.json
CHANGED
package/telosMiddleware.js
CHANGED
|
@@ -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(
|
|
163
|
+
pup.preprocess(string) : string;
|
|
165
164
|
}
|
|
166
165
|
|
|
167
166
|
module.exports = {
|
package/telosServer.js
CHANGED
|
@@ -9,102 +9,127 @@ var telosServer = {
|
|
|
9
9
|
|
|
10
10
|
serverUtils.processRequest(request, "http", (data) => {
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
(new Promise((resolve, reject) => {
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
let results = busNet.call(data);
|
|
15
|
+
let responses = [];
|
|
15
16
|
|
|
16
|
-
|
|
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
|
-
|
|
23
|
-
let file = false;
|
|
19
|
+
if(item instanceof Promise) {
|
|
24
20
|
|
|
25
|
-
|
|
21
|
+
item.then((result => {
|
|
26
22
|
|
|
27
|
-
|
|
23
|
+
responses.push(result);
|
|
28
24
|
|
|
29
|
-
|
|
25
|
+
if(responses.length == results.length)
|
|
26
|
+
resolve(responses);
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
else
|
|
31
|
+
responses.push(item);
|
|
30
32
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
if(responses.length == results.length)
|
|
34
|
+
resolve(responses);
|
|
35
|
+
});
|
|
36
|
+
})).then((responses) => {
|
|
34
37
|
|
|
35
|
-
|
|
36
|
-
item => {
|
|
38
|
+
let status = 200;
|
|
37
39
|
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
42
|
-
|
|
46
|
+
let body = [];
|
|
47
|
+
let file = false;
|
|
43
48
|
|
|
44
|
-
|
|
49
|
+
let max = -Infinity;
|
|
45
50
|
|
|
46
|
-
|
|
51
|
+
responses.filter(item => item != null).forEach(item => {
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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
|
-
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
).filter(
|
|
60
|
-
item => item.request == null
|
|
61
|
-
).forEach(item => {
|
|
83
|
+
).filter(
|
|
84
|
+
item => item.request == null
|
|
85
|
+
).forEach(item => {
|
|
62
86
|
|
|
63
|
-
|
|
87
|
+
if(item.response != null) {
|
|
64
88
|
|
|
65
|
-
|
|
89
|
+
if(item.response.status != null) {
|
|
66
90
|
|
|
67
|
-
|
|
68
|
-
|
|
91
|
+
if(item.response.status > status)
|
|
92
|
+
status = item.response.status;
|
|
93
|
+
}
|
|
69
94
|
}
|
|
70
|
-
}
|
|
71
95
|
|
|
72
|
-
|
|
73
|
-
|
|
96
|
+
if(item.headers != null)
|
|
97
|
+
Object.assign(headers, item.headers);
|
|
74
98
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
99
|
+
if(item.body != null)
|
|
100
|
+
body.push("" + item.body);
|
|
101
|
+
});
|
|
78
102
|
|
|
79
|
-
|
|
103
|
+
response.writeHead(status, headers);
|
|
80
104
|
|
|
81
|
-
|
|
105
|
+
if(file) {
|
|
82
106
|
|
|
83
|
-
|
|
107
|
+
if(body.length > 0) {
|
|
84
108
|
|
|
85
|
-
|
|
109
|
+
fs.readFile(body[0], function(error, data) {
|
|
86
110
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
122
|
+
else {
|
|
99
123
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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) => {
|