Skip to content

Commit

Permalink
feat: Add global disconnect event (#1355)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored May 14, 2019
1 parent 12d0f4b commit 85afcca
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/authentication-oauth/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ export default (options: OauthSetupSettings) => {
const { authService, linkStrategy } = options;
const app = feathersApp as ExpressApplication;
const config = app.get('grant');

if (!config) {
debug('No grant configuration found, skipping Express oAuth setup');
return;
}

const { path } = config.defaults;
const grantApp = grant(config);
const authApp = express();
Expand Down
6 changes: 3 additions & 3 deletions packages/authentication-oauth/test/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ describe('@feathersjs/authentication-oauth/strategy', () => {
});

it('reads configuration from the oauth key', () => {
const testConfigValue = Math.random()
app.get('authentication').oauth.test.hello = testConfigValue
assert.strictEqual(strategy.configuration.hello, testConfigValue)
const testConfigValue = Math.random();
app.get('authentication').oauth.test.hello = testConfigValue;
assert.strictEqual(strategy.configuration.hello, testConfigValue);
});

it('getRedirect', async () => {
Expand Down
19 changes: 7 additions & 12 deletions packages/primus/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ function configurePrimus (config, configurer) {

primus.use('feathers', function (req, res, next) {
req.feathers = {
headers: Object.keys(req.headers).reduce((key, result) => {
headers: Object.keys(req.headers).reduce((result, key) => {
const value = req.headers[key];

return typeof value === 'object' ? result : {
...result,
[key]: value
};
if (typeof value !== 'object') {
result[key] = value;
}

return result;
}, {}),
provider: 'primus'
};
Expand All @@ -63,13 +64,7 @@ function configurePrimus (config, configurer) {
})
);

primus.on('disconnection', spark => {
const { channels } = app;

if (channels.length) {
app.channel(app.channels).leave(getParams(spark));
}
});
primus.on('disconnection', spark => app.emit('disconnect', getParams(spark)));
}

if (typeof configurer === 'function') {
Expand Down
18 changes: 18 additions & 0 deletions packages/primus/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ describe('@feathersjs/primus', () => {
});
});

it('connection and disconnect events (#1243, #1238)', (done) => {
const { app, primus } = options;
const mySocket = new primus.Socket('http://localhost:7888?channel=dctest');

app.on('connection', connection => {
if (connection.channel === 'dctest') {
assert.strictEqual(connection.channel, 'dctest');
app.once('disconnect', disconnection => {
assert.strictEqual(disconnection.channel, 'dctest');
done();
});
setTimeout(() => mySocket.end(), 100);
}
});

assert.ok(mySocket);
});

describe('Service method calls', () => {
describe('(\'method\', \'service\') event format', () => {
describe('Service', () => methodTests('todo', options));
Expand Down
8 changes: 1 addition & 7 deletions packages/socketio/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ function configureSocketio (port, options, config) {
});

io.use((socket, next) => {
socket.once('disconnect', () => {
const { channels } = app;

if (channels.length) {
app.channel(app.channels).leave(getParams(socket));
}
});
socket.once('disconnect', () => app.emit('disconnect', getParams(socket)));
next();
});

Expand Down
15 changes: 15 additions & 0 deletions packages/socketio/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,21 @@ describe.only('@feathersjs/socketio', () => {
});
});

it('connection and disconnect events (#1243, #1238)', (done) => {
const mySocket = io('http://localhost:7886?channel=dctest');

app.once('connection', connection => {
assert.strictEqual(connection.channel, 'dctest');
app.once('disconnect', disconnection => {
assert.strictEqual(disconnection.channel, 'dctest');
done();
});
setTimeout(() => mySocket.close(), 100);
});

assert.ok(mySocket);
});

it('missing parameters in socket call works (#88)', done => {
let service = app.service('todo');
let old = { find: service.find };
Expand Down
7 changes: 7 additions & 0 deletions packages/transport-commons/src/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export function socket ({ done, emit, socketKey, getParams }: SocketOptions) {
app.configure(routing());

app.on('publish', getDispatcher(emit, socketKey));
app.on('disconnect', connection => {
const { channels } = app;

if (channels.length) {
app.channel(app.channels).leave(connection);
}
});

// `connection` event
done.then(provider => provider.on('connection', (connection: any) =>
Expand Down

0 comments on commit 85afcca

Please sign in to comment.