nv-srv-nd-http-util 1.0.19 → 1.0.20
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/res.js +73 -7
package/package.json
CHANGED
package/res.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const _prms = require("nv-facutil-prms")
|
|
1
2
|
|
|
2
3
|
const CORS_OPTIONS_REPLY_HEAD = {
|
|
3
4
|
"Access-Control-Allow-Origin":"*",
|
|
@@ -5,6 +6,12 @@ const CORS_OPTIONS_REPLY_HEAD = {
|
|
|
5
6
|
"Access-Control-Allow-Credentials":"true"
|
|
6
7
|
}
|
|
7
8
|
|
|
9
|
+
const set_cors = (res)=> {
|
|
10
|
+
for(let k in CORS_OPTIONS_REPLY_HEAD) {
|
|
11
|
+
res.setHeader(k,CORS_OPTIONS_REPLY_HEAD[k]);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
8
15
|
const reply_cors_options = (res)=> {
|
|
9
16
|
res.writeHead(200, 'OK', CORS_OPTIONS_REPLY_HEAD);
|
|
10
17
|
res.end();
|
|
@@ -30,28 +37,87 @@ const reply_succ_json_in_head = (res,jdata)=> {
|
|
|
30
37
|
res.end();
|
|
31
38
|
}
|
|
32
39
|
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
33
43
|
////
|
|
34
44
|
const _fs = require("fs");
|
|
35
45
|
const reply_file = (res,fn,res_head={}) => {
|
|
36
46
|
var dnldfn = _path.resolve(fn);
|
|
37
47
|
if(_fs.existsSync(dnldfn)) {
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
var rs$ = _fs.createReadStream(dnldfn);
|
|
49
|
+
rs$.pipe(res);
|
|
40
50
|
} else {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
51
|
+
//delete res_head['content-length'];
|
|
52
|
+
//delete res_head['transfer-encoding'];
|
|
53
|
+
res.writeHead(200, 'OK', res_head);
|
|
44
54
|
res.end();
|
|
45
55
|
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const reply_html = (res,html)=>{
|
|
59
|
+
res.writeHead(200, 'OK',{ 'Content-Type': 'text/html'});
|
|
60
|
+
res.end(html);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
////
|
|
64
|
+
const reply_sse = (res)=>{
|
|
65
|
+
res.writeHead(200, 'OK',{ 'Content-Type': 'text/event-stream',"Cache-Control": "no-cache","Connection":"keep-alive"});
|
|
66
|
+
res.flushHeaders();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const pipe_buf_req_to_ws$_with_cb = (req,ws$,cb=()=>{})=>{ //for test a nodejs BUG
|
|
70
|
+
let [p, rs] = _prms.ps();
|
|
71
|
+
let total = 0;
|
|
72
|
+
////
|
|
73
|
+
if(ws$["#already_started"] !== true) {
|
|
74
|
+
ws$.on("error", (e)=>{ rs([false,e])});
|
|
75
|
+
ws$.on("finish", (e)=>{ console.log("ws on finished!!!");ws$.close(); console.log("ws close!!!");rs([true,total])});
|
|
76
|
+
ws$["#already_started"] = true;
|
|
77
|
+
} else {
|
|
78
|
+
}
|
|
79
|
+
////
|
|
80
|
+
req.on("data",(data)=>{
|
|
81
|
+
ws$.write(data);
|
|
82
|
+
total += data.byteLength;
|
|
83
|
+
cb(total,data);
|
|
84
|
+
});
|
|
85
|
+
req.on("error",(e)=>{rs([false,e])});
|
|
86
|
+
req.on("end", ()=> { console.log("req on end!!!"); ws$.end(); console.log("ws$ end!!!")});
|
|
87
|
+
req.pipe(ws$);
|
|
88
|
+
return(p);
|
|
46
89
|
}
|
|
90
|
+
const pipe_buf_req_to_ws$ = (req,ws$)=>{ //for test a nodejs BUG
|
|
91
|
+
let [p, rs] = _prms.ps();
|
|
92
|
+
////
|
|
93
|
+
if(ws$["#already_started"] !== true) {
|
|
94
|
+
ws$.on("error", (e)=>{rs([false,e])});
|
|
95
|
+
ws$.on("finish", (e)=>{console.log("--------------------------on-finished!!!-----------------");ws$.close(); console.log("ws close!!!");rs([true,null])});
|
|
96
|
+
ws$["#already_started"] = true;
|
|
97
|
+
} else {
|
|
98
|
+
}
|
|
99
|
+
////
|
|
100
|
+
req.on("error",(e)=>{rs([false,e])});
|
|
101
|
+
//req.on("end", ()=>{ws$.end();});
|
|
102
|
+
req.pipe(ws$);
|
|
103
|
+
return(p);
|
|
104
|
+
}
|
|
105
|
+
|
|
47
106
|
|
|
48
107
|
module.exports = {
|
|
108
|
+
set_cors,
|
|
109
|
+
////
|
|
49
110
|
reply_cors_options,
|
|
50
111
|
////
|
|
51
112
|
reply_failed_json_reason,
|
|
52
113
|
reply_failed_json_reason_in_head,
|
|
53
|
-
reply_succ_json,
|
|
54
|
-
reply_succ_json_in_head,
|
|
114
|
+
reply_succ_json,
|
|
115
|
+
reply_succ_json_in_head,
|
|
55
116
|
////
|
|
56
117
|
reply_file,
|
|
118
|
+
reply_sse,
|
|
119
|
+
reply_html,
|
|
120
|
+
pipe_buf_req_to_ws$_with_cb,
|
|
121
|
+
pipe_buf_req_to_ws$,
|
|
122
|
+
////
|
|
57
123
|
}
|