Skip to content

Commit 72bf9eb

Browse files
committed
Compatibility updates for package web-dev-server 2.1.
1 parent 302583e commit 72bf9eb

File tree

9 files changed

+69
-33
lines changed

9 files changed

+69
-33
lines changed

.editorconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# http://EditorConfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = tab
7+
tab_width = 4
8+
charset = utf-8
9+
end_of_line = lf
10+
insert_final_newline = false
11+
trim_trailing_whitespace = false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ Temporary Items
5858
# third party node modules
5959
node_modules
6060
package-lock.json
61+
62+
# log files
63+
*.log

.npmignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
Workspace.code-workspace
3+
.editorconfig
4+
*.log
5+
TODO.md

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Example - Chat
22

3-
[![Latest Stable Version](https://img.shields.io/badge/Stable-v1.3.1-brightgreen.svg?style=plastic)](https://github.com/web-dev-server/example-chat/releases)
3+
[![Latest Stable Version](https://img.shields.io/badge/Stable-v2.1.0-brightgreen.svg?style=plastic)](https://github.com/web-dev-server/chat-example-pure-js/releases)
44
[![License](https://img.shields.io/badge/Licence-BSD-brightgreen.svg?style=plastic)](https://github.com/web-dev-server/chat-example-pure-js/blob/master/LICENCE.md)
55

66
Chat example with session authentication. Client scripts written with pure Javascript, no framework needed.
@@ -14,12 +14,12 @@ npm update
1414

1515
## Usage
1616
```shell
17-
node run-server.js
17+
node server.js
1818
```
1919
- open your first web browser on:
2020
- http://localhost:8000/chat/
21-
- login with any user and password located in `chat/data/login-data.csv`
21+
- login with any user and password located in `./chat/data/login-data.csv`
2222
- open your second web browser on:
2323
- http://localhost:8000/chat/
24-
- login with any user and password located in `chat/data/login-data.csv`
24+
- login with any user and password located in `./chat/data/login-data.csv`
2525
- chat between browsers

Workspace.code-workspace

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
],
7+
"settings": {}
8+
}

chat/data/index.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -271,30 +271,32 @@ App.prototype = {
271271
}
272272
}.bind(this));
273273
},
274-
httpRequestHandler: function (request, response, callback) {
275-
if (request.method == 'POST' && typeof(request.query['login-submit']) != 'undefined') {
276-
this._completeWholeRequestInfo(request, function (reqInfo) {
277-
if (this._users.length) {
278-
this._completeReqCredentialsAndTryToAuth(reqInfo, response, callback);
279-
} else {
280-
this._loadCsvLoginData(function (users) {
281-
this._users = users;
282-
this._completeReqCredentialsAndTryToAuth(reqInfo, response, callback);
283-
}.bind(this));
284-
}
285-
}.bind(this));
286-
} else {
287-
response.send('/* No autorization credentials sended. */');
288-
callback();
289-
}
274+
HandleHttpRequest: function (request, response) {
275+
return new Promise(function (resolve, reject) {
276+
if (request.method == 'POST' && typeof(request.query['login-submit']) != 'undefined') {
277+
this._completeWholeRequestInfo(request, function (reqInfo) {
278+
if (this._users.length) {
279+
this._completeReqCredentialsAndTryToAuth(reqInfo, response, resolve);
280+
} else {
281+
this._loadCsvLoginData(function (users) {
282+
this._users = users;
283+
this._completeReqCredentialsAndTryToAuth(reqInfo, response, resolve);
284+
}.bind(this));
285+
}
286+
}.bind(this));
287+
} else {
288+
response.send('/* No autorization credentials sended. */');
289+
resolve();
290+
}
291+
}.bind(this));
290292
},
291-
_completeReqCredentialsAndTryToAuth: function (reqInfo, response, callback) {
293+
_completeReqCredentialsAndTryToAuth: function (reqInfo, response, resolve) {
292294
var request = reqInfo.request,
293295
urlParts, data;
294296
try {
295297
urlParts = Url.parse("http://localhost/?" + reqInfo.textBody, true);
296298
data = urlParts.query;
297-
} catch (e) { }
299+
} catch (e) {}
298300
if (typeof(data) != 'undefined' && data.user.length > 0 && data.pass.length > 0) {
299301

300302

@@ -309,20 +311,20 @@ App.prototype = {
309311
request.session.authorized = true;
310312
request.session.save(function () {
311313
response.send('{"success":true,"id":' + this._users[data.user].id + '}');
312-
callback();
314+
resolve();
313315
}.bind(this));
314316

315317
} else {
316318
response.send('{"success":false');
317-
callback();
319+
resolve();
318320
}
319321
/***************************************************************************/
320322

321323

322324

323325
} else {
324326
response.send('{"success":false');
325-
callback();
327+
resolve();
326328
}
327329
},
328330
_completeWholeRequestInfo: function (request, callback) {

chat/data/login-data.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Id;FullName;User;Password
22
1;James Bond;james.bond;1234
3-
2;Miss Moneypenny;moneypenny;1234
4-
3;Mr. White;mr.white;1234
3+
2;Miss Moneypenny;money.penny;1234
4+
3;Mr. Smith;mr.smith;1234

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"name": "web-dev-server-example-chat-pure-js",
3-
"version": "1.3.1",
3+
"version": "2.1.0",
4+
"keywords": [
5+
"chat",
6+
"javascript",
7+
"node"
8+
],
49
"description": "Chat example with session authentication in pure Javascript.",
510
"homepage": "https://github.com/web-dev-server/example-chat-pure-js",
611
"repository": {
@@ -15,7 +20,7 @@
1520
},
1621
"main": "server.js",
1722
"dependencies": {
18-
"web-dev-server": "^1.3.0",
23+
"web-dev-server": "^2.1",
1924
"ws": "^3.3.2"
2025
}
2126
}

server.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
var WebDevServer = require("web-dev-server");
2-
var devServerInstance = (new WebDevServer())
3-
.SetDocumentRoot(__dirname) // required
2+
3+
WebDevServer.Server.CreateNew()
4+
.SetDocumentRoot(__dirname) // required
45
//.SetPort(8000) // optional, 8000 by default
56
//.SetDomain('localhost') // optional, 'localhost' by default
67
.SetSessionMaxAge(60 * 60 * 24) // optional, 1 hour by default, seconds
7-
.SetSessionHash('SGS2e+9x5$as%SD_AS6s.aHS96s') // optional, session id hash salt
8+
.SetSessionHashSalt('SGS2e+9x5$as%SD_AS6s.aHS96s') // optional, session id hash salt
89
.SetDevelopment(true) // optional, true by default to display Errors and directory content
910
.AddHandler(function (req, res, e, cb) { // optional, to prepend any execution before `web-dev-server` module execution
1011
if (req.url == '/health') {
1112
res.writeHead(200);
1213
res.end('1');
13-
e.preventDefault(); // do not anything else in `web-dev-server` module for this request
14+
e.PreventDefault(); // do not anything else in `web-dev-server` module for this request
1415
}
1516
cb();
1617
})
18+
.AddForbiddenPaths(['/chat/data/login-data.csv'])
1719
.Run();

0 commit comments

Comments
 (0)