@jnode/server 1.0.0 → 1.0.2
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 -2
- package/src/handle.js +11 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jnode/server",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Simple web server package for Node.js.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,7 +14,6 @@
|
|
|
14
14
|
"JustNode",
|
|
15
15
|
"HTTP",
|
|
16
16
|
"HTTPS",
|
|
17
|
-
"HTTP2",
|
|
18
17
|
"Server",
|
|
19
18
|
"Web",
|
|
20
19
|
"Website",
|
package/src/handle.js
CHANGED
|
@@ -10,7 +10,7 @@ by JustNode Dev Team / JustApple
|
|
|
10
10
|
const processFinal = require('./final.js');
|
|
11
11
|
const processError = require('./error.js');
|
|
12
12
|
|
|
13
|
-
//
|
|
13
|
+
//error handler function
|
|
14
14
|
async function handleStatus(req, res, map, p, e, status) {
|
|
15
15
|
let statusCode;
|
|
16
16
|
let defaultMap;
|
|
@@ -31,37 +31,37 @@ async function handleStatus(req, res, map, p, e, status) {
|
|
|
31
31
|
};
|
|
32
32
|
break;
|
|
33
33
|
default:
|
|
34
|
-
return status; //
|
|
34
|
+
return status; //if status is not a special error, return it directly
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
return await processFinal(req, res, map[status] ?? defaultMap, p, e);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
//
|
|
40
|
+
//process function with error handling
|
|
41
41
|
async function safeProcessFinal(req, res, map, p, e, finalObject) {
|
|
42
42
|
try {
|
|
43
43
|
let status = await processFinal(req, res, finalObject, p, e);
|
|
44
44
|
|
|
45
|
-
//
|
|
45
|
+
//handle special status codes
|
|
46
46
|
status = await handleStatus(req, res, map, p, e, status);
|
|
47
47
|
|
|
48
|
-
//
|
|
48
|
+
//fall into loop error
|
|
49
49
|
if ((typeof status === 'string') && status.startsWith('!')) {
|
|
50
50
|
throw new Error('Process may fall into infinity loop.');
|
|
51
51
|
}
|
|
52
52
|
return status;
|
|
53
53
|
} catch (err) {
|
|
54
|
-
//
|
|
54
|
+
//emit error
|
|
55
55
|
e.emitError(err);
|
|
56
56
|
|
|
57
|
-
//
|
|
57
|
+
//internal server error
|
|
58
58
|
return processError(req, res, map, p, e);
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
62
|
//process HandleObject
|
|
63
63
|
async function processHandle(req, res, map, p, e) {
|
|
64
|
-
//
|
|
64
|
+
//protocol upgrade
|
|
65
65
|
if (
|
|
66
66
|
req.headers.connection &&
|
|
67
67
|
req.headers.connection.toLowerCase() === 'upgrade' &&
|
|
@@ -70,17 +70,17 @@ async function processHandle(req, res, map, p, e) {
|
|
|
70
70
|
return await safeProcessFinal(req, res, map, p, e, map['^' + req.headers.upgrade]);
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
//
|
|
73
|
+
//method check
|
|
74
74
|
if (map['@' + req.method]) {
|
|
75
75
|
return await safeProcessFinal(req, res, map, p, e, map['@' + req.method]);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
//
|
|
78
|
+
//get final object by function
|
|
79
79
|
if (map['>']) {
|
|
80
80
|
return await safeProcessFinal(req, res, map, p, e, await map['>'](req, res, map, p, e));
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
//
|
|
83
|
+
//process final
|
|
84
84
|
return await safeProcessFinal(req, res, map, p, e, map);
|
|
85
85
|
}
|
|
86
86
|
|