telos-server 1.0.5 → 1.0.7
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/serverUtils.js +0 -29
- package/telosRouter.js +19 -16
- package/telosServer.js +94 -89
package/package.json
CHANGED
package/serverUtils.js
CHANGED
|
@@ -30,33 +30,6 @@ var fileTypes = [
|
|
|
30
30
|
'mp4'
|
|
31
31
|
];
|
|
32
32
|
|
|
33
|
-
function getArgOptions(args) {
|
|
34
|
-
|
|
35
|
-
let options = { };
|
|
36
|
-
|
|
37
|
-
args.forEach((item, index) => {
|
|
38
|
-
|
|
39
|
-
if(item.startsWith("-") && index < args.length - 1)
|
|
40
|
-
options[item.substring(1)] = args[index + 1];
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
return options;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function getConfigOptions() {
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
|
|
50
|
-
return JSON.parse(
|
|
51
|
-
fs.readFileSync("./APInt.json", "utf-8")
|
|
52
|
-
).utilities["telos-config"].properties;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
catch(error) {
|
|
56
|
-
return { };
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
33
|
function getFile(uri, directories) {
|
|
61
34
|
|
|
62
35
|
try {
|
|
@@ -269,8 +242,6 @@ function processRequest(request, protocol, callback) {
|
|
|
269
242
|
module.exports = {
|
|
270
243
|
extensionTypes,
|
|
271
244
|
fileTypes,
|
|
272
|
-
getArgOptions,
|
|
273
|
-
getConfigOptions,
|
|
274
245
|
getFile,
|
|
275
246
|
getFiles,
|
|
276
247
|
isHTTPJSON,
|
package/telosRouter.js
CHANGED
|
@@ -2,19 +2,11 @@ var apint = require("apint");
|
|
|
2
2
|
var path = require("path");
|
|
3
3
|
var serverUtils = require("./serverUtils.js");
|
|
4
4
|
|
|
5
|
-
var config = serverUtils.getConfigOptions();
|
|
6
|
-
|
|
7
|
-
config.directories = config.directories != null ?
|
|
8
|
-
config.directories : [process.cwd() + path.sep + "telos"];
|
|
9
|
-
|
|
10
|
-
config.directories = config.directories.map(
|
|
11
|
-
item => item.startsWith("./") ?
|
|
12
|
-
process.cwd() + path.sep + item.substring(2) : item
|
|
13
|
-
);
|
|
14
|
-
|
|
15
|
-
config.default = { };
|
|
16
|
-
|
|
17
5
|
var telosRouter = {
|
|
6
|
+
config: {
|
|
7
|
+
directories: [process.cwd() + path.sep + "telos"],
|
|
8
|
+
default: { }
|
|
9
|
+
},
|
|
18
10
|
middleware: [],
|
|
19
11
|
query: (packet) => {
|
|
20
12
|
|
|
@@ -51,6 +43,17 @@ var telosRouter = {
|
|
|
51
43
|
|
|
52
44
|
telosRouter.middleware = middleware;
|
|
53
45
|
|
|
46
|
+
Object.assign(
|
|
47
|
+
telosRouter.config,
|
|
48
|
+
packet.content.options.options
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
telosRouter.config.directories =
|
|
52
|
+
telosRouter.config.directories.map(
|
|
53
|
+
item => item.startsWith("./") ?
|
|
54
|
+
process.cwd() + path.sep + item.substring(2) : item
|
|
55
|
+
);
|
|
56
|
+
|
|
54
57
|
return;
|
|
55
58
|
}
|
|
56
59
|
|
|
@@ -64,7 +67,7 @@ var telosRouter = {
|
|
|
64
67
|
|
|
65
68
|
let file = serverUtils.getFile(
|
|
66
69
|
packet.request.uri,
|
|
67
|
-
config.directories
|
|
70
|
+
telosRouter.config.directories
|
|
68
71
|
);
|
|
69
72
|
|
|
70
73
|
let response = telosRouter.middleware.map(
|
|
@@ -96,18 +99,18 @@ var telosRouter = {
|
|
|
96
99
|
<!DOCTYPE HTML>
|
|
97
100
|
<html lang="en-US">
|
|
98
101
|
<head>
|
|
99
|
-
${config.default.missing != null ?
|
|
102
|
+
${telosRouter.config.default.missing != null ?
|
|
100
103
|
`<meta
|
|
101
104
|
http-equiv="refresh"
|
|
102
105
|
content="0; url=${
|
|
103
|
-
config.default.missing
|
|
106
|
+
telosRouter.config.default.missing
|
|
104
107
|
}"
|
|
105
108
|
/>` :
|
|
106
109
|
""
|
|
107
110
|
}
|
|
108
111
|
</head>
|
|
109
112
|
<body>
|
|
110
|
-
${config.default.missing == null ?
|
|
113
|
+
${telosRouter.config.default.missing == null ?
|
|
111
114
|
"<pre>404: Not Found</pre>" :
|
|
112
115
|
""
|
|
113
116
|
}
|
package/telosServer.js
CHANGED
|
@@ -5,132 +5,137 @@ var https = require("https");
|
|
|
5
5
|
var busNet = use("bus-net");
|
|
6
6
|
|
|
7
7
|
var telosServer = {
|
|
8
|
-
|
|
8
|
+
process: ((request, response) => {
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
serverUtils.processRequest(request, "http", (data) => {
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
let responses = busNet.call(data);
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
!packet.tags.includes("initialize")) {
|
|
14
|
+
let status = 200;
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
let headers = {
|
|
17
|
+
'Content-Type': 'text/plain',
|
|
18
|
+
'Access-Control-Allow-Origin': '*',
|
|
19
|
+
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
|
|
20
|
+
};
|
|
20
21
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
let body = [];
|
|
23
|
+
let file = false;
|
|
24
|
+
|
|
25
|
+
let max = -Infinity;
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
responses.filter(item => item != null).forEach(item => {
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
item.priority = item.priority != null ? item.priority : 0;
|
|
28
30
|
|
|
29
|
-
|
|
31
|
+
if(item.priority > max)
|
|
32
|
+
max = item.priority;
|
|
33
|
+
});
|
|
30
34
|
|
|
31
|
-
|
|
35
|
+
responses.filter(
|
|
36
|
+
item => {
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
'Access-Control-Allow-Origin': '*',
|
|
36
|
-
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
|
|
37
|
-
};
|
|
38
|
+
if(item == null)
|
|
39
|
+
return
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
+
if(item.priority != max)
|
|
42
|
+
return false;
|
|
41
43
|
|
|
42
|
-
|
|
44
|
+
delete item.priority;
|
|
43
45
|
|
|
44
|
-
|
|
46
|
+
if(typeof item == "object") {
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
if(item.file != null) {
|
|
49
|
+
|
|
50
|
+
if(item.file)
|
|
51
|
+
file = true;
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
53
|
+
delete item.file;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return serverUtils.isHTTPJSON(item);
|
|
58
|
+
}
|
|
59
|
+
).filter(
|
|
60
|
+
item => item.request == null
|
|
61
|
+
).forEach(item => {
|
|
51
62
|
|
|
52
|
-
|
|
53
|
-
item => {
|
|
63
|
+
if(item.response != null) {
|
|
54
64
|
|
|
55
|
-
|
|
56
|
-
return
|
|
65
|
+
if(item.response.status != null) {
|
|
57
66
|
|
|
58
|
-
if(item.
|
|
59
|
-
|
|
67
|
+
if(item.response.status > status)
|
|
68
|
+
status = item.response.status;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
60
71
|
|
|
61
|
-
|
|
72
|
+
if(item.headers != null)
|
|
73
|
+
Object.assign(headers, item.headers);
|
|
62
74
|
|
|
63
|
-
|
|
75
|
+
if(item.body != null)
|
|
76
|
+
body.push("" + item.body);
|
|
77
|
+
});
|
|
64
78
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if(item.file)
|
|
68
|
-
file = true;
|
|
79
|
+
response.writeHead(status, headers);
|
|
69
80
|
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return serverUtils.isHTTPJSON(item);
|
|
75
|
-
}
|
|
76
|
-
).filter(
|
|
77
|
-
item => item.request == null
|
|
78
|
-
).forEach(item => {
|
|
81
|
+
if(file) {
|
|
79
82
|
|
|
80
|
-
|
|
83
|
+
if(body.length > 0) {
|
|
81
84
|
|
|
82
|
-
|
|
85
|
+
fs.readFile(body[0], function(error, data) {
|
|
83
86
|
|
|
84
|
-
|
|
85
|
-
|
|
87
|
+
if(error) {
|
|
88
|
+
response.statusCode = 500;
|
|
89
|
+
response.end(`ERROR: ${error}.`);
|
|
86
90
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
+
|
|
92
|
+
else
|
|
93
|
+
response.end(data);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
91
97
|
|
|
92
|
-
|
|
93
|
-
body.push("" + item.body);
|
|
94
|
-
});
|
|
98
|
+
else {
|
|
95
99
|
|
|
96
|
-
|
|
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
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}),
|
|
110
|
+
query: (packet) => {
|
|
97
111
|
|
|
98
|
-
|
|
112
|
+
try {
|
|
99
113
|
|
|
100
|
-
|
|
114
|
+
packet = JSON.parse(packet);
|
|
101
115
|
|
|
102
|
-
|
|
116
|
+
if(!packet.tags.includes("telos-origin") ||
|
|
117
|
+
!packet.tags.includes("initialize")) {
|
|
103
118
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
else
|
|
110
|
-
response.end(data);
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
114
122
|
|
|
115
|
-
|
|
123
|
+
catch(error) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
116
126
|
|
|
117
|
-
|
|
118
|
-
response.write(body[0]);
|
|
119
|
-
|
|
120
|
-
else if(body.length > 1)
|
|
121
|
-
response.write(JSON.stringify(body));
|
|
127
|
+
let options = packet.content.options.options;
|
|
122
128
|
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
});
|
|
126
|
-
});
|
|
129
|
+
if(options.port != false) {
|
|
127
130
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
telosServer.server = http.createServer(telosServer.process);
|
|
132
|
+
|
|
133
|
+
telosServer.server.listen(
|
|
134
|
+
process.env.PORT || (options.port != null ? options.port : 80)
|
|
135
|
+
);
|
|
132
136
|
|
|
133
|
-
|
|
137
|
+
console.log("TELOS SERVER ON!");
|
|
138
|
+
}
|
|
134
139
|
},
|
|
135
140
|
server: null,
|
|
136
141
|
tags: ["telos-origin", "telos-server"]
|