Skip to content

Commit f949c1d

Browse files
committed
chore(package) v2.0.0
1 parent 9dbffbb commit f949c1d

File tree

5 files changed

+62
-14
lines changed

5 files changed

+62
-14
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2018.03.30, v2.0.0
2+
3+
feature:
4+
- (gritty) add auth
5+
- (gritty) drop support of node < 4
6+
7+
18
2018.03.12, v1.5.2
29

310
feature:

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,19 @@ gritty('body', {
5858

5959
```js
6060
const prefix = '/gritty'; // default
61-
const authCheck = (accept, reject) => (username, password) => {
61+
62+
// legacy
63+
const authCheck = (socket, success) => {
64+
susccess();
65+
};
66+
67+
const auth = (accept, reject) => (username, password) => {
6268
accept();
6369
};
6470

6571
gritty.listen(socket, {
6672
prefix,
73+
auth, // optional
6774
authCheck, // optional
6875
})
6976
```
@@ -157,3 +164,4 @@ MIT
157164
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"
158165
[CoverageURL]: https://coveralls.io/github/cloudcmd/gritty?branch=master
159166
[CoverageIMGURL]: https://coveralls.io/repos/cloudcmd/gritty/badge.svg?branch=master&service=github
167+

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gritty",
3-
"version": "1.5.2",
3+
"version": "2.0.0",
44
"author": "coderaiser <[email protected]> (https://github.com/coderaiser)",
55
"description": "Web terminal emulator",
66
"bin": {

server/gritty.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,33 @@ module.exports.listen = (socket, options) => {
7878
check(socket, options);
7979

8080
const prefix = options.prefix;
81+
const auth = options.auth;
8182
const authCheck = options.authCheck;
8283

8384
socket
8485
.of(prefix || '/gritty')
8586
.on('connection', (socket) => {
8687
const connect = connectionWraped(options, socket);
8788

88-
if (!authCheck)
89+
if (authCheck)
90+
return authCheck(socket, connect);
91+
92+
if (!auth)
8993
return connection(options, socket);
9094

9195
const reject = () => socket.emit('reject');
92-
socket.on('auth', authCheck(connect, reject));
96+
socket.on('auth', auth(connect, reject));
9397
});
9498
};
9599

96100
function check(socket, options) {
97101
if (!socket)
98102
throw Error('socket could not be empty!');
99103

100-
const authCheck = options.authCheck;
104+
const auth = options.auth;
101105

102-
if (authCheck && typeof authCheck !== 'function')
103-
throw Error('options.authCheck should be a function!');
106+
if (auth && typeof auth !== 'function')
107+
throw Error('options.auth should be a function!');
104108
}
105109

106110
function connection(options, socket) {

test/server/gritty.js

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test('gritty: listen: args: no', (t) => {
2020
t.end();
2121
});
2222

23-
test('gritty: listen: args: authCheck', (t) => {
23+
test('gritty: listen: args: auth', (t) => {
2424
const socket = {};
2525
const on = sinon.stub().returns(socket)
2626
const of = sinon.stub().returns(socket);
@@ -29,10 +29,10 @@ test('gritty: listen: args: authCheck', (t) => {
2929
socket.of = of;
3030

3131
const fn = () => gritty.listen(socket, {
32-
authCheck: 'hello'
32+
auth: 'hello'
3333
});
3434

35-
t.throws(fn, /options.authCheck should be a function!/, 'should throw when no args');
35+
t.throws(fn, /options.auth should be a function!/, 'should throw when no args');
3636

3737
t.end();
3838
});
@@ -158,15 +158,15 @@ test('gritty: server: socket: emit data', (t) => {
158158
});
159159
});
160160

161-
test('gritty: server: socket: authCheck', (t) => {
162-
const authCheck = currify((accept, reject, username, password) => {
161+
test('gritty: server: socket: auth', (t) => {
162+
const auth = currify((accept, reject, username, password) => {
163163
if (username !== 'hello' || password !== 'world')
164164
return reject();
165165

166166
accept();
167167
});
168168

169-
before({authCheck}, (port, after) => {
169+
before({auth}, (port, after) => {
170170
const socket = io(`http://localhost:${port}/gritty`);
171171

172172
socket.once('connect', () => {
@@ -248,7 +248,36 @@ test('gritty: server: socket: test env', (t) => {
248248
});
249249
});
250250

251+
test('gritty: server: socket: authCheck', (t) => {
252+
const authCheck = (socket, connection) => {
253+
socket.on('auth', ({username, password}) => {
254+
if (username !== 'hello' || password !== 'world')
255+
return socket.emit('reject');
256+
257+
connection();
258+
socket.emit('accept');
259+
});
260+
};
261+
262+
before({authCheck}, (port, after) => {
263+
const socket = io(`http://localhost:${port}/gritty`);
264+
265+
socket.once('connect', () => {
266+
socket.emit('auth', {
267+
username: 'hello',
268+
password: 'world',
269+
});
270+
271+
socket.on('accept', () => {
272+
t.pass('should emit accepet');
273+
socket.close();
274+
after();
275+
t.end();
276+
});
277+
});
278+
});
279+
});
280+
251281
function clean(name) {
252282
delete require.cache[require.resolve(name)];
253283
}
254-

0 commit comments

Comments
 (0)