diff --git a/package-lock.json b/package-lock.json index 7ad86488f4..fdd98c5335 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1171,9 +1171,9 @@ "dev": true }, "@types/node": { - "version": "12.7.1", - "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.1.tgz", - "integrity": "sha512-aK9jxMypeSrhiYofWWBf/T7O+KwaiAHzM4sveCdWPn71lzUSMimRnKzhXDKfKwV1kWoBo2P1aGgaIYGLf9/ljw==", + "version": "12.7.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.2.tgz", + "integrity": "sha512-dyYO+f6ihZEtNPDcWNR1fkoTDf3zAK3lAABDze3mz6POyIercH0lEUawUFXlG8xaQZmm1yEBON/4TsYv/laDYg==", "dev": true }, "@zkochan/cmd-shim": { @@ -3386,9 +3386,9 @@ } }, "graceful-fs": { - "version": "4.2.1", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.1.tgz", - "integrity": "sha512-b9usnbDGnD928gJB3LrCmxoibr3VE4U2SMo5PBuBnokWyDADTqDPXg4YpwKF1trpH+UbGp7QLicO3+aWEy0+mw==", + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.2.tgz", + "integrity": "sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q==", "dev": true }, "handlebars": { @@ -5677,9 +5677,9 @@ } }, "read-cmd-shim": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/read-cmd-shim/-/read-cmd-shim-1.0.1.tgz", - "integrity": "sha1-LV0Vd4ajfAVdIgd8MsU/gynpHHs=", + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/read-cmd-shim/-/read-cmd-shim-1.0.3.tgz", + "integrity": "sha512-HUHb2imlZ8xBJjiZZRx0Ag9JfZ3jxQRfORMQXWCDeHE6PCCnpQrMq6LhyNqEPnMXhMDDIyq/BK7pBbhNy9zDDA==", "dev": true, "requires": { "graceful-fs": "^4.1.2" @@ -5944,9 +5944,9 @@ "dev": true }, "rimraf": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.0.tgz", - "integrity": "sha512-4Liqw7ccABzsWV5BzeZeGRSq7KWIgQYzOcmRDEwSX4WAawlQpcAFXZ1Kid72XYrjSnK5yxOS6Gez/iGusYE/Pw==", + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", + "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", "dev": true, "requires": { "glob": "^7.1.3" diff --git a/packages/authentication-oauth/src/express.ts b/packages/authentication-oauth/src/express.ts index ff17f76cf1..fdc0d480f9 100644 --- a/packages/authentication-oauth/src/express.ts +++ b/packages/authentication-oauth/src/express.ts @@ -33,13 +33,14 @@ export default (options: OauthSetupSettings) => { authApp.get('/:name', (req, res) => { const { feathers_token, ...query } = req.query; + const { name } = req.params as any; if (feathers_token) { debug(`Got feathers_token query parameter to link accounts`, feathers_token); req.session.accessToken = feathers_token; } - res.redirect(`${path}/connect/${req.params.name}?${qs.stringify(query)}`); + res.redirect(`${path}/connect/${name}?${qs.stringify(query)}`); }); authApp.get('/:name/callback', (req: any, res: any) => { @@ -47,7 +48,7 @@ export default (options: OauthSetupSettings) => { }); authApp.get('/:name/authenticate', async (req, res, next) => { - const { name } = req.params; + const { name } = req.params as any; const { accessToken, grant } = req.session; const service = app.defaultAuthentication(authService); const [ strategy ] = service.getStrategies(name) as OAuthStrategy[]; diff --git a/packages/primus/lib/index.js b/packages/primus/lib/index.js index f503ccc2e4..3ebc343185 100644 --- a/packages/primus/lib/index.js +++ b/packages/primus/lib/index.js @@ -6,12 +6,13 @@ const http = require('http'); const Emitter = require('primus-emitter'); const debug = makeDebug('@feathersjs/primus'); -const socketKey = Symbol('@feathersjs/primus/socket'); function configurePrimus (config, configurer) { - return function () { - const app = this; + return function (app) { + // Returns the connection object const getParams = spark => spark.request.feathers; + // Mapping from connection back to its socket + const socketMap = new WeakMap(); if (!app.version || app.version < '3.0.0') { throw new Error('@feathersjs/primus is not compatible with this version of Feathers. Use the latest at @feathersjs/feathers.'); @@ -58,12 +59,7 @@ function configurePrimus (config, configurer) { next(); }, 0); - primus.on('connection', spark => - Object.defineProperty(getParams(spark), socketKey, { - value: spark - }) - ); - + primus.on('connection', spark => socketMap.set(getParams(spark), spark)); primus.on('disconnection', spark => app.emit('disconnect', getParams(spark))); } @@ -81,7 +77,7 @@ function configurePrimus (config, configurer) { app.configure(commons({ done, - socketKey, + socketMap, getParams, emit: 'send' })); @@ -89,5 +85,4 @@ function configurePrimus (config, configurer) { } module.exports = configurePrimus; -module.exports.SOCKET_KEY = socketKey; module.exports.default = configurePrimus; diff --git a/packages/primus/test/index.test.js b/packages/primus/test/index.test.js index da4861c0eb..11801036e7 100644 --- a/packages/primus/test/index.test.js +++ b/packages/primus/test/index.test.js @@ -63,8 +63,7 @@ describe('@feathersjs/primus', () => { options.server.close(done); }); - it('exports default and SOCKET_KEY', () => { - assert.ok(primus.SOCKET_KEY); + it('exports default', () => { assert.strictEqual(primus, primus.default); }); diff --git a/packages/socketio/index.d.ts b/packages/socketio/index.d.ts index 4c5b438ba4..c17797a2a8 100644 --- a/packages/socketio/index.d.ts +++ b/packages/socketio/index.d.ts @@ -8,6 +8,5 @@ interface FeathersSocketIO { (callback?: (io: io.Server) => void): () => void; (options: number | io.ServerOptions, callback?: (io: io.Server) => void): () => void; (port: number, options?: io.ServerOptions, callback?: (io: io.Server) => void): () => void; - readonly SOCKET_KEY: unique symbol; default: FeathersSocketIO; } diff --git a/packages/socketio/lib/index.js b/packages/socketio/lib/index.js index a8467849a9..b52c7e9c45 100644 --- a/packages/socketio/lib/index.js +++ b/packages/socketio/lib/index.js @@ -4,8 +4,6 @@ const http = require('http'); const { socket: commons } = require('@feathersjs/transport-commons'); const debug = require('debug')('@feathersjs/socketio'); -const socketKey = Symbol('@feathersjs/socketio/socket'); - function configureSocketio (port, options, config) { if (typeof port !== 'number') { config = options; @@ -18,9 +16,11 @@ function configureSocketio (port, options, config) { options = {}; } - return function () { - const app = this; + return function (app) { + // Function that gets the connection const getParams = socket => socket.feathers; + // A mapping from connection to socket instance + const socketMap = new WeakMap(); if (!app.version || app.version < '3.0.0') { throw new Error('@feathersjs/socketio is not compatible with this version of Feathers. Use the latest at @feathersjs/feathers.'); @@ -50,16 +50,12 @@ function configureSocketio (port, options, config) { .listen(port || server, options); io.use((socket, next) => { - const connection = { + socket.feathers = { provider: 'socketio', headers: socket.handshake.headers }; - Object.defineProperty(connection, socketKey, { - value: socket - }); - - socket.feathers = connection; + socketMap.set(socket.feathers, socket); next(); }); @@ -89,7 +85,7 @@ function configureSocketio (port, options, config) { app.configure(commons({ done, - socketKey, + socketMap, getParams, emit: 'emit' })); @@ -98,4 +94,3 @@ function configureSocketio (port, options, config) { module.exports = configureSocketio; module.exports.default = configureSocketio; -module.exports.SOCKET_KEY = socketKey; diff --git a/packages/socketio/test/index.test.js b/packages/socketio/test/index.test.js index a49518e7e1..c3f71895be 100644 --- a/packages/socketio/test/index.test.js +++ b/packages/socketio/test/index.test.js @@ -77,8 +77,7 @@ describe.only('@feathersjs/socketio', () => { server.close(done); }); - it('exports default and SOCKET_KEY', () => { - assert.ok(socketio.SOCKET_KEY); + it('exports default', () => { assert.strictEqual(socketio, socketio.default); }); diff --git a/packages/transport-commons/src/socket/index.ts b/packages/transport-commons/src/socket/index.ts index c8b5acbb83..7c1272d1b4 100644 --- a/packages/transport-commons/src/socket/index.ts +++ b/packages/transport-commons/src/socket/index.ts @@ -10,16 +10,16 @@ const debug = Debug('@feathersjs/transport-commons'); export interface SocketOptions { done: Promise; emit: string; - socketKey: any; + socketMap: WeakMap; getParams: (socket: any) => RealTimeConnection; } -export function socket ({ done, emit, socketKey, getParams }: SocketOptions) { +export function socket ({ done, emit, socketMap, getParams }: SocketOptions) { return (app: Application) => { app.configure(channels()); app.configure(routing()); - app.on('publish', getDispatcher(emit, socketKey)); + app.on('publish', getDispatcher(emit, socketMap)); app.on('disconnect', connection => { const { channels } = app; diff --git a/packages/transport-commons/src/socket/utils.ts b/packages/transport-commons/src/socket/utils.ts index a3fd9ca104..73d6dc1995 100644 --- a/packages/transport-commons/src/socket/utils.ts +++ b/packages/transport-commons/src/socket/utils.ts @@ -35,14 +35,13 @@ export function normalizeError (e: any) { return result; } -export function getDispatcher (emit: string, socketKey: any) { +export function getDispatcher (emit: string, socketMap: WeakMap) { 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 = connection[socketKey]; + // The reference between connection and socket is set in `app.setup` + const socket = socketMap.get(connection); if (socket) { const eventName = `${context.path || ''} ${event}`.trim(); diff --git a/packages/transport-commons/test/socket/index.test.ts b/packages/transport-commons/test/socket/index.test.ts index cf2e264b07..f8842be78e 100644 --- a/packages/transport-commons/test/socket/index.test.ts +++ b/packages/transport-commons/test/socket/index.test.ts @@ -17,7 +17,7 @@ describe('@feathersjs/transport-commons', () => { options = { emit: 'emit', done: Promise.resolve(provider), - socketKey: 'test', + socketMap: new WeakMap(), getParams () { return connection; } diff --git a/packages/transport-commons/test/socket/utils.test.ts b/packages/transport-commons/test/socket/utils.test.ts index c690d56daa..b7bba8faba 100644 --- a/packages/transport-commons/test/socket/utils.test.ts +++ b/packages/transport-commons/test/socket/utils.test.ts @@ -9,6 +9,7 @@ import { getDispatcher, runMethod } from '../../src/socket/utils'; +import { RealTimeConnection } from '../../src/channels/channel/base'; describe('socket commons utils', () => { describe('.normalizeError', () => { @@ -59,7 +60,7 @@ describe('socket commons utils', () => { describe('.getDispatcher', () => { it('returns a dispatcher function', () => - assert.strictEqual(typeof getDispatcher('test', 'here'), 'function') + assert.strictEqual(typeof getDispatcher('test', new WeakMap()), 'function') ); describe('dispatcher logic', () => { @@ -67,19 +68,22 @@ describe('socket commons utils', () => { let dummySocket: EventEmitter; let dummyHook: any; let dummyChannel: any; + let dummyConnection: RealTimeConnection; + let dummyMap: WeakMap; beforeEach(() => { - dispatcher = getDispatcher('emit', 'test'); + dummyConnection = {}; + dummyMap = new WeakMap(); + dispatcher = getDispatcher('emit', dummyMap); dummySocket = new EventEmitter(); dummyHook = { result: 'hi' }; dummyChannel = { - connections: [{ - test: dummySocket - }], + connections: [ dummyConnection ], dataFor (): null { return null; } }; + dummyMap.set(dummyConnection, dummySocket); }); it('dispatches a basic event', done => {