Skip to content

Commit

Permalink
fix: Fix regression in transport commons (#1551)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Sep 9, 2019
1 parent 8d4410a commit ed9e934
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/transport-commons/src/socket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export interface SocketOptions {
done: Promise<any>;
emit: string;
socketMap: WeakMap<RealTimeConnection, any>;
socketKey?: any;
getParams: (socket: any) => RealTimeConnection;
}

export function socket ({ done, emit, socketMap, getParams }: SocketOptions) {
export function socket ({ done, emit, socketMap, socketKey, getParams }: SocketOptions) {
return (app: Application) => {
const leaveChannels = (connection: RealTimeConnection) => {
const { channels } = app;
Expand All @@ -27,7 +28,7 @@ export function socket ({ done, emit, socketMap, getParams }: SocketOptions) {
app.configure(channels());
app.configure(routing());

app.on('publish', getDispatcher(emit, socketMap));
app.on('publish', getDispatcher(emit, socketMap, socketKey));
app.on('disconnect', leaveChannels);
app.on('logout', (_authResult: any, params: Params) => {
const { connection } = params;
Expand Down
6 changes: 3 additions & 3 deletions packages/transport-commons/src/socket/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export function normalizeError (e: any) {
return result;
}

export function getDispatcher (emit: string, socketMap: WeakMap<RealTimeConnection, any>) {
return function (event: string, channel: CombinedChannel, context: HookContext, data: any) {
export function getDispatcher (emit: string, socketMap: WeakMap<RealTimeConnection, any>, socketKey?: any) {
return function (event: string, channel: CombinedChannel, context: HookContext, data?: any) {
debug(`Dispatching '${event}' to ${channel.length} connections`);

channel.connections.forEach(connection => {
// The reference between connection and socket is set in `app.setup`
const socket = socketMap.get(connection);
const socket = socketKey ? connection[socketKey] : socketMap.get(connection);

if (socket) {
const eventName = `${context.path || ''} ${event}`.trim();
Expand Down
22 changes: 22 additions & 0 deletions packages/transport-commons/test/socket/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ describe('socket commons utils', () => {
assert.strictEqual(typeof getDispatcher('test', new WeakMap()), 'function')
);

it('works with backwards compatible socketKey', done => {
const socketKey = Symbol('@feathersjs/test');
const dispatcher = getDispatcher('emit', undefined, socketKey);
const socket = new EventEmitter();
const connection = {
[socketKey]: socket
};
const channel: any = {
connections: [ connection ],
dataFor (): null {
return null;
}
};

socket.once('testing', data => {
assert.strictEqual(data, 'hi');
done();
});

dispatcher('testing', channel, { result: 'hi' } as any);
});

describe('dispatcher logic', () => {
let dispatcher: any;
let dummySocket: EventEmitter;
Expand Down

0 comments on commit ed9e934

Please # to comment.