8000 fix: Fix regression in transport commons by daffl · Pull Request #1551 · feathersjs/feathers · GitHub
[go: up one dir, main page]

Skip to content
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
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;
}

e 10000 xport 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