Skip to content

Commit c38a0d2

Browse files
authored
Merge pull request #45 from riversun/hotfix/20220401_sanitize_input_value
Hotfix/20220401 sanitize input value
2 parents ad111be + 444e6c6 commit c38a0d2

File tree

11 files changed

+595
-351
lines changed

11 files changed

+595
-351
lines changed

.npmignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
example
2+
example_chat_server
3+
public
4+
test
5+
gulpfile.js
6+
node_modules
7+
.DS_Store
8+
docs
9+
coverage
10+
.circleci
11+
*.md
12+
.idea
13+
babel.*
14+
jest.*
15+
webpack.*
16+
nodemon.*
17+

dist/chatux.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example_chat_server/server.js

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/**
2+
* Chat Server Example
3+
*/
4+
const express = require('express');
5+
const app = express();
6+
7+
const port = 8081;
8+
9+
if (app.get('env') === 'production') {
10+
app.set('trust proxy', 1); // trust first proxy
11+
}
12+
13+
app.use(express.json())
14+
app.use(express.urlencoded({extended: true}));
15+
16+
// set mddielware for CORS
17+
app.use(function (req, res, next) {
18+
res.header("Access-Control-Allow-Origin", "*");
19+
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
20+
next();
21+
});
22+
23+
//provide json api
24+
app.set('json spaces', 2);
25+
26+
const handleRequest = (req, res, data) => {
27+
28+
const {userInputText, callback} = data;
29+
30+
console.log(`[chat-server] #chat request userInputText:"${userInputText}"`);
31+
32+
//create response object
33+
const response = {
34+
output: []
35+
};
36+
37+
const msg = response.output;
38+
39+
if (!userInputText) {
40+
41+
//if inputText is empty
42+
msg.push({
43+
type: "text",
44+
value: "Hey, please say something!"
45+
});
46+
47+
} else {
48+
49+
if (userInputText == "show buttons") {
50+
51+
//show text and buttons
52+
msg.push({
53+
type: "text",
54+
value: "Ok, I'll show you buttons!",
55+
delayMs: 500 // wait(milliseconds)
56+
});
57+
58+
const opts = [];
59+
opts.push({label: 'label1', value: 'value1'});
60+
opts.push({label: 'label2', value: 'value2'});
61+
opts.push({label: 'label3', value: 'value3'});
62+
63+
msg.push({type: "option", options: opts});
64+
65+
} else if (userInputText == "show image") {
66+
67+
//show text and image
68+
msg.push({
69+
type: "text",
70+
value: "Ok, I'll show you an image!",
71+
delayMs: 500
72+
});
73+
74+
msg.push({
75+
type: "image",
76+
value: " https://upload.wikimedia.org/wikipedia/commons/a/a3/Aptenodytes_forsteri_-Snow_Hill_Island%2C_Antarctica_-adults_and_juvenile-8.jpg"
77+
});
78+
} else if (userInputText == "show link") {
79+
80+
// show html
81+
msg.push({
82+
type: 'html',
83+
value: 'Click <a href="https://github.com/riversun" target="_blank" >here</a> to open a page.',
84+
delayMs: 500
85+
});
86+
87+
88+
} else if (userInputText == "show double") {
89+
90+
91+
msg.push({
92+
type: "text",
93+
value: "Ok, This is the 1st message",
94+
delayMs: 500
95+
});
96+
97+
98+
msg.push({
99+
type: "text",
100+
value: "Ok, This is the 2nd message",
101+
delayMs: 500
102+
});
103+
104+
105+
} else {
106+
//echo inputText
107+
msg.push({
108+
type: "text",
109+
value: "You say '" + userInputText + "'"
110+
});
111+
}
112+
}
113+
114+
if (callback) {
115+
//generate response for JSONP
116+
const responseText = callback + "(" + JSON.stringify(response) + ")";
117+
118+
//Set content-type to "application/javascript"
119+
res.set('Content-Type', 'application/javascript');
120+
res.send(responseText)
121+
122+
console.log(`[chat-server] #chat response responseText:"${responseText}"`);
123+
} else {
124+
//generate response for JSON
125+
126+
res.json(response);
127+
128+
console.log(`[chat-server] #chat response responseText:"${JSON.stringify(response)}"`);
129+
}
130+
131+
};
132+
app.get('/chat', function (req, res) {
133+
console.log(`[chat-server] #chat GET /chat requested" query="${JSON.stringify(req.query)}"`);
134+
135+
//Get "text" parameter from request
136+
const userInputText = req.query.text;
137+
138+
//get callback parameter for jsonp response
139+
const callback = req.query.callback;
140+
handleRequest(req, res, {userInputText, callback});
141+
142+
});
143+
144+
app.post('/chat', function (req, res) {
145+
146+
//Get "text" parameter from request
147+
const data = req.body;
148+
149+
150+
console.log(`[chat-server] #chat POST /chat requested" data="${JSON.stringify(data)}"`);
151+
152+
153+
handleRequest(req, res, {userInputText: data.text});
154+
155+
});
156+
app.listen(port, () => {
157+
console.log('[chat-server] chat server started on port:' + port);
158+
console.log(`[chat-server] To use this chat server,
159+
First start webpack-devserver with "npm start".
160+
Next, go to "http://localhost:8080/index.html?endpoint=http://localhost:8081/chat" .`)
161+
});
162+

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "chatux",
3-
"version": "1.2.0",
3+
"version": "1.3.0",
44
"description": "",
55
"main": "dist/chatux.min.js",
66
"scripts": {
77
"start": "webpack-dev-server",
88
"startr": "set NODE_ENV=test&&webpack-dev-server --config webpack.config.js --mode production",
9+
"start:chat-server": "node example_chat_server/server.js",
910
"build": "webpack --config webpack.config.js",
1011
"test": "echo \"Error: no test specified\" && exit 1",
1112
"release": "set NODE_ENV=test&&webpack --config webpack.config.js --mode production"

0 commit comments

Comments
 (0)