diff --git a/packages/transport-commons/src/socket/index.ts b/packages/transport-commons/src/socket/index.ts index 2aefea7748..e81787422b 100644 --- a/packages/transport-commons/src/socket/index.ts +++ b/packages/transport-commons/src/socket/index.ts @@ -11,11 +11,10 @@ export interface SocketOptions { done: Promise; emit: string; socketMap: WeakMap; - socketKey?: any; getParams: (socket: any) => RealTimeConnection; } -export function socket ({ done, emit, socketMap, socketKey, getParams }: SocketOptions) { +export function socket ({ done, emit, socketMap, getParams }: SocketOptions) { return (app: Application) => { const leaveChannels = (connection: RealTimeConnection) => { const { channels } = app; @@ -28,7 +27,7 @@ export function socket ({ done, emit, socketMap, socketKey, getParams }: SocketO app.configure(channels()); app.configure(routing()); - app.on('publish', getDispatcher(emit, socketMap, socketKey)); + app.on('publish', getDispatcher(emit, socketMap)); app.on('disconnect', leaveChannels); app.on('logout', (_authResult: any, params: Params) => { const { connection } = params; diff --git a/packages/transport-commons/src/socket/utils.ts b/packages/transport-commons/src/socket/utils.ts index 9ccaa111e9..7eecac3240 100644 --- a/packages/transport-commons/src/socket/utils.ts +++ b/packages/transport-commons/src/socket/utils.ts @@ -34,13 +34,13 @@ export function normalizeError (e: any) { return result; } -export function getDispatcher (emit: string, socketMap: WeakMap, 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 = socketKey ? connection[socketKey] : socketMap.get(connection); + const socket = socketMap.get(connection); if (socket) { const eventName = `${context.path || ''} ${event}`.trim(); diff --git a/packages/transport-commons/test/socket/utils.test.ts b/packages/transport-commons/test/socket/utils.test.ts index 9695ac1499..70b9ece6ba 100644 --- a/packages/transport-commons/test/socket/utils.test.ts +++ b/packages/transport-commons/test/socket/utils.test.ts @@ -63,28 +63,6 @@ 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; diff --git a/packages/ws/LICENSE b/packages/ws/LICENSE new file mode 100644 index 0000000000..3f395cc665 --- /dev/null +++ b/packages/ws/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2021 Feathers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/packages/ws/README.md b/packages/ws/README.md new file mode 100644 index 0000000000..1682b05916 --- /dev/null +++ b/packages/ws/README.md @@ -0,0 +1,23 @@ +# @feathersjs/ws + +[![CI](https://github.com/feathersjs/feathers/workflows/CI/badge.svg)](https://github.com/feathersjs/feathers/actions?query=workflow%3ACI) +[![Dependency Status](https://img.shields.io/david/feathersjs/feathers.svg?style=flat-square&path=packages/ws)](https://david-dm.org/feathersjs/feathers?path=packages/ws) +[![Download Status](https://img.shields.io/npm/dm/@feathersjs/ws.svg?style=flat-square)](https://www.npmjs.com/package/@feathersjs/ws) + +> A high performance Feathers websocket transport + +## Installation + +``` +npm install @feathersjs/ws --save +``` + +## Documentation + +Refer to the [Feathers documentation](https://docs.feathersjs.com) for more details. + +## License + +Copyright (c) 2021 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors) + +Licensed under the [MIT license](LICENSE). diff --git a/packages/ws/package.json b/packages/ws/package.json new file mode 100644 index 0000000000..ef08ed198f --- /dev/null +++ b/packages/ws/package.json @@ -0,0 +1,62 @@ +{ + "name": "@feathersjs/ws", + "description": "A high performance Feathers websocket transport", + "version": "0.0.0", + "homepage": "https://feathersjs.com", + "main": "lib/", + "keywords": [ + "feathers", + "feathers-plugin" + ], + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/daffl" + }, + "repository": { + "type": "git", + "url": "git://github.com/feathersjs/feathers.git" + }, + "author": { + "name": "Feathers contributors", + "email": "hello@feathersjs.com", + "url": "https://feathersjs.com" + }, + "contributors": [], + "bugs": { + "url": "https://github.com/feathersjs/feathers/issues" + }, + "engines": { + "node": ">= 12" + }, + "files": [ + "CHANGELOG.md", + "LICENSE", + "README.md", + "src/**", + "lib/**", + "*.d.ts", + "*.js" + ], + "scripts": { + "prepublish": "npm run compile", + "compile": "shx rm -rf lib/ && tsc", + "test": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts" + }, + "directories": { + "lib": "lib" + }, + "publishConfig": { + "access": "public" + }, + "dependencies": { + }, + "devDependencies": { + "@types/mocha": "^9.0.0", + "@types/node": "^16.9.4", + "mocha": "^9.1.1", + "shx": "^0.3.3", + "ts-node": "^10.2.1", + "typescript": "^4.4.3" + } +} diff --git a/packages/ws/src/index.ts b/packages/ws/src/index.ts new file mode 100644 index 0000000000..4c8c8ad654 --- /dev/null +++ b/packages/ws/src/index.ts @@ -0,0 +1,3 @@ +export function hello () { + return 'Hello'; +} diff --git a/packages/ws/test/index.test.ts b/packages/ws/test/index.test.ts new file mode 100644 index 0000000000..5f0a0021e0 --- /dev/null +++ b/packages/ws/test/index.test.ts @@ -0,0 +1,8 @@ +import assert from 'assert'; +import { hello } from '../src'; + +describe('@feathersjs/ws', () => { + it('initializes', async () => { + assert.strictEqual(hello(), 'Hello'); + }); +}); diff --git a/packages/ws/tsconfig.json b/packages/ws/tsconfig.json new file mode 100644 index 0000000000..316fd41336 --- /dev/null +++ b/packages/ws/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig", + "include": [ + "src/**/*.ts" + ], + "compilerOptions": { + "outDir": "lib" + } +}