Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add global disconnect event #1355

Merged
merged 1 commit into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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