From bb4243f24b609952eae8a231a83310e27e06c6be Mon Sep 17 00:00:00 2001 From: frederic charette Date: Fri, 27 Mar 2020 19:47:37 -0400 Subject: [PATCH] added tests, prepublish scripts --- .gitignore | 15 +++ packages/ipc/CHANGELOG.md | 127 ---------------------- packages/ipc/LICENSE | 13 --- packages/ipc/package.json | 3 +- packages/ipc/src/ipc.ts | 6 +- packages/ipc/tests/unit/ipc.spec.ts | 37 ++++++- packages/kalm/CHANGELOG.md | 127 ---------------------- packages/kalm/LICENSE | 13 --- packages/kalm/package.json | 3 +- packages/tcp/CHANGELOG.md | 127 ---------------------- packages/tcp/LICENSE | 13 --- packages/tcp/package.json | 3 +- packages/tcp/src/tcp.ts | 6 +- packages/tcp/tests/unit/tcp.spec.ts | 37 ++++++- packages/udp/CHANGELOG.md | 127 ---------------------- packages/udp/LICENSE | 13 --- packages/udp/package.json | 3 +- packages/udp/src/udp.ts | 2 +- packages/udp/tests/unit/udp.spec.ts | 37 ++++++- packages/webrtc/CHANGELOG.md | 127 ---------------------- packages/webrtc/LICENSE | 13 --- packages/webrtc/package.json | 3 +- packages/webrtc/src/webrtc.ts | 5 +- packages/webrtc/tests/unit/webrtc.spec.ts | 40 ++++++- packages/ws/CHANGELOG.md | 127 ---------------------- packages/ws/LICENSE | 13 --- packages/ws/package.json | 3 +- packages/ws/src/ws.ts | 6 +- packages/ws/tests/unit/ws.spec.ts | 46 +++++++- 29 files changed, 228 insertions(+), 867 deletions(-) delete mode 100644 packages/ipc/CHANGELOG.md delete mode 100644 packages/ipc/LICENSE delete mode 100644 packages/kalm/CHANGELOG.md delete mode 100644 packages/kalm/LICENSE delete mode 100644 packages/tcp/CHANGELOG.md delete mode 100644 packages/tcp/LICENSE delete mode 100644 packages/udp/CHANGELOG.md delete mode 100644 packages/udp/LICENSE delete mode 100644 packages/webrtc/CHANGELOG.md delete mode 100644 packages/webrtc/LICENSE delete mode 100644 packages/ws/CHANGELOG.md delete mode 100644 packages/ws/LICENSE diff --git a/.gitignore b/.gitignore index 7f78ea9..00e8ee6 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,18 @@ packages/tcp/bin packages/udp/bin packages/ws/bin packages/webrtc/bin + +# publish-only files +packages/kalm/LICENSE +packages/ipc/LICENSE +packages/tcp/LICENSE +packages/udp/LICENSE +packages/ws/LICENSE +packages/webrtc/LICENSE + +packages/kalm/CHANGELOG.md +packages/ipc/CHANGELOG.md +packages/tcp/CHANGELOG.md +packages/udp/CHANGELOG.md +packages/ws/CHANGELOG.md +packages/webrtc/CHANGELOG.md \ No newline at end of file diff --git a/packages/ipc/CHANGELOG.md b/packages/ipc/CHANGELOG.md deleted file mode 100644 index 7715330..0000000 --- a/packages/ipc/CHANGELOG.md +++ /dev/null @@ -1,127 +0,0 @@ -# Changelog - -## [v3.3.0] - 2020-01-30 - -commit: [af46059](https://github.com/kalm/kalm.js/commit/af4605958c567b5243887f911850a3c0eb6c6659) - -### Added -- Adds webrtc package and examples -- Adds the getChannels method on Client - -### Removed -- Removed home implementation of EventEmitter in favor of Node's - -## [v3.2.3] - 2020-01-14 - -commit: [c188225](https://github.com/kalm/kalm.js/commit/c18822532a49f2026eddf44cfbe3cfc1521110f8) - -### Added -- Added pre-hook for lint on commit - -### Changed -- Cleaned up Types management and typings file accessibility -- Migrated test suite to Jest and centralized test tooling -- Removed output rollup, and using only tsc with none modules -- Fixed linting (was not targeting .ts files properly) -- Fixed stats events (were previously unreachable, now exposed through client emitter as .*) -- Fixed timeout behavior (only logged, now actually disconnects) - -## [v3.1.2] - 2019-07-01 - -commit: [fac8047](https://github.com/kalm/kalm.js/commit/fac8047d4b7048d56803505103159e16d8f518a8) - -### Changed -- Changed dev tooling from lerna to yarn workspaces -- Changed dev tooling from tslint to eslint + @typescript-eslint -- Housekeeping - -## [v3.0.0] - 2019-01-18 - -commit: [a4c687d](https://github.com/kalm/kalm.js/commit/a4c687dd5786a70723d9d0964a9d189220d58418) - -### Added -- New monorepo structure -- Massive new changes to the interface - -### Changed -- Serialization is now a toggle for json/binary -- Re-written the entire codebase in Typescript - -### Removed -- Transports are no longer bundled and must be installed separately and must be instantiated with options. - - [ipc](https://www.npmjs.com/package/@kalm/ipc) - - [tcp](https://www.npmjs.com/package/@kalm/tcp) - - [udp](https://www.npmjs.com/package/@kalm/udp) - - [ws](https://www.npmjs.com/package/@kalm/ws) -- Profiles become routines and must be instantiated with options. -- No more session stores -- No more encryption - -## [v2.6.1] - 2018-01-27 - -commit: [7393d17](https://github.com/kalm/kalm.js/commit/7393d17efb02088d7283ba83108fd7ab15e3d39e) - -### Added -- Added package-lock.json file -- Added server reference in the client object - -## [v2.5.0] - 2017-09-21 - -commit: [2c687f6](https://github.com/kalm/kalm.js/commit/2c687f6074787af6b39c10abe19669fe20e7b02d) - -### Added -- Added engines reqs - -### Changed -- Some minor performance tuning - -## [v2.4.0] - 2017-09-01 - -commit: [a7b8f95](https://github.com/kalm/kalm.js/commit/a7b8f950da56cbe35c538dc02e3dcc0e6d3a3db3) - -### Changed -- Tuned performances -- Added build targets for Node 8.x and 6.x -- UDP client cache (tied with socketTimeout) -- Bumped dependencies -- Proper callback on disconnect - -### Removed -- Dropped support for Node 4.x - -## [v2.3.0] - 2017-07-25 - -commit: [f323bcd](https://github.com/kalm/kalm.js/commit/f323bcdc163faa40b0f8515fd4a8759e5180f516) - -### Added -- Added realtime profile -- Added JSDoc -- Added parameter validation and error messages - -### Changed -- Better performances (3x with default congestion) - -## [v2.2.0] - 2017-06-21 - -commit: [b9f3bdd](https://github.com/kalm/kalm.js/commit/b9f3bdd50de8dae2b92a0866d234a0cb2e72f22b) - -### Added -- Added support for node 8 - -## [v2.1.0] - 2017-06-20 - -commit: [129146f](https://github.com/kalm/kalm.js/commit/129146feeab14e94a540a4d9c54e05a4614fdb39) - -### Changed -- Simplified and optimized queue system logic -- Now allowing 0 to be passed as tick value in profiles -- Added warning when non-serialized message are sent and serial config is `null` -- Fixed hanging packets on maxBytes just reached - -## [v2.0.0] - 2017-03-02 - -commit: [b5209ec](https://github.com/kalm/kalm.js/commit/b5209ec4d3ab86000b72b502d120f0a5b4da85af) - -### Added -- Migrated the codebase from the [original repo](https://github.com/fed135/kalm) -- Implemeted new interface diff --git a/packages/ipc/LICENSE b/packages/ipc/LICENSE deleted file mode 100644 index c6746cb..0000000 --- a/packages/ipc/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2020 Frederic Charette - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/packages/ipc/package.json b/packages/ipc/package.json index dc45378..380555c 100644 --- a/packages/ipc/package.json +++ b/packages/ipc/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "../../scripts/build.sh ipc", "clean": "../../scripts/cleanup.sh", - "test": "jest ./tests" + "test": "jest ./tests", + "prepublish": "cp ../../LICENSE ./LICENSE && cp ../../CHANGELOG.md ./CHANGELOG.md" }, "funding": { "type": "Open Collective", diff --git a/packages/ipc/src/ipc.ts b/packages/ipc/src/ipc.ts index d408963..1d1f6db 100644 --- a/packages/ipc/src/ipc.ts +++ b/packages/ipc/src/ipc.ts @@ -13,7 +13,7 @@ interface IPCSocket extends net.Socket { } } -export function ipc({ socketTimeout = 30000, path = '/tmp/app.socket-' }: IPCConfig = {}): KalmTransport { +function ipc({ socketTimeout = 30000, path = '/tmp/app.socket-' }: IPCConfig = {}): KalmTransport { return function socket(params: ClientConfig, emitter: NodeJS.EventEmitter): Socket { let listener: net.Server; @@ -25,8 +25,8 @@ export function ipc({ socketTimeout = 30000, path = '/tmp/app.socket-' }: IPCCon function remote(handle: IPCSocket): Remote { return { - host: handle._server._pipeName, - port: handle._handle.fd, + host: handle && handle._server && handle._server._pipeName || null, + port: handle && handle._handle && handle._handle.fd || null, }; } diff --git a/packages/ipc/tests/unit/ipc.spec.ts b/packages/ipc/tests/unit/ipc.spec.ts index 392b854..b543f77 100644 --- a/packages/ipc/tests/unit/ipc.spec.ts +++ b/packages/ipc/tests/unit/ipc.spec.ts @@ -1,5 +1,40 @@ +import { EventEmitter } from 'events'; import * as ipc from '../../src/ipc'; describe('IPC transport', () => { - it('TODO', () => { expect(ipc).not.toBeUndefined(); }); + it('basic setup', () => { + expect(typeof ipc.default).toBe('function'); + const transport = ipc.default(); + expect(typeof transport).toBe('function'); + const socket = transport({}, new EventEmitter()); + + expect(socket).toHaveProperty('bind', expect.any(Function)); + expect(socket).toHaveProperty('connect', expect.any(Function)); + expect(socket).toHaveProperty('disconnect', expect.any(Function)); + expect(socket).toHaveProperty('remote', expect.any(Function)); + expect(socket).toHaveProperty('stop', expect.any(Function)); + expect(socket).toHaveProperty('send', expect.any(Function)); + }); + + describe('Given an empty handle reference and no configs', () => { + const transport = ipc.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return null values', () => { + expect(socket.remote()).toEqual({ host: null, port: null }); + }); + }); + }); + + describe('Given a handle reference and no configs', () => { + const transport = ipc.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return handle\'s values', () => { + expect(socket.remote({ _server: { _pipeName: '/foo' }, _handle: { fd: 12345 } })).toEqual({ host: '/foo', port: 12345 }); + }); + }); + }); }); diff --git a/packages/kalm/CHANGELOG.md b/packages/kalm/CHANGELOG.md deleted file mode 100644 index 7715330..0000000 --- a/packages/kalm/CHANGELOG.md +++ /dev/null @@ -1,127 +0,0 @@ -# Changelog - -## [v3.3.0] - 2020-01-30 - -commit: [af46059](https://github.com/kalm/kalm.js/commit/af4605958c567b5243887f911850a3c0eb6c6659) - -### Added -- Adds webrtc package and examples -- Adds the getChannels method on Client - -### Removed -- Removed home implementation of EventEmitter in favor of Node's - -## [v3.2.3] - 2020-01-14 - -commit: [c188225](https://github.com/kalm/kalm.js/commit/c18822532a49f2026eddf44cfbe3cfc1521110f8) - -### Added -- Added pre-hook for lint on commit - -### Changed -- Cleaned up Types management and typings file accessibility -- Migrated test suite to Jest and centralized test tooling -- Removed output rollup, and using only tsc with none modules -- Fixed linting (was not targeting .ts files properly) -- Fixed stats events (were previously unreachable, now exposed through client emitter as .*) -- Fixed timeout behavior (only logged, now actually disconnects) - -## [v3.1.2] - 2019-07-01 - -commit: [fac8047](https://github.com/kalm/kalm.js/commit/fac8047d4b7048d56803505103159e16d8f518a8) - -### Changed -- Changed dev tooling from lerna to yarn workspaces -- Changed dev tooling from tslint to eslint + @typescript-eslint -- Housekeeping - -## [v3.0.0] - 2019-01-18 - -commit: [a4c687d](https://github.com/kalm/kalm.js/commit/a4c687dd5786a70723d9d0964a9d189220d58418) - -### Added -- New monorepo structure -- Massive new changes to the interface - -### Changed -- Serialization is now a toggle for json/binary -- Re-written the entire codebase in Typescript - -### Removed -- Transports are no longer bundled and must be installed separately and must be instantiated with options. - - [ipc](https://www.npmjs.com/package/@kalm/ipc) - - [tcp](https://www.npmjs.com/package/@kalm/tcp) - - [udp](https://www.npmjs.com/package/@kalm/udp) - - [ws](https://www.npmjs.com/package/@kalm/ws) -- Profiles become routines and must be instantiated with options. -- No more session stores -- No more encryption - -## [v2.6.1] - 2018-01-27 - -commit: [7393d17](https://github.com/kalm/kalm.js/commit/7393d17efb02088d7283ba83108fd7ab15e3d39e) - -### Added -- Added package-lock.json file -- Added server reference in the client object - -## [v2.5.0] - 2017-09-21 - -commit: [2c687f6](https://github.com/kalm/kalm.js/commit/2c687f6074787af6b39c10abe19669fe20e7b02d) - -### Added -- Added engines reqs - -### Changed -- Some minor performance tuning - -## [v2.4.0] - 2017-09-01 - -commit: [a7b8f95](https://github.com/kalm/kalm.js/commit/a7b8f950da56cbe35c538dc02e3dcc0e6d3a3db3) - -### Changed -- Tuned performances -- Added build targets for Node 8.x and 6.x -- UDP client cache (tied with socketTimeout) -- Bumped dependencies -- Proper callback on disconnect - -### Removed -- Dropped support for Node 4.x - -## [v2.3.0] - 2017-07-25 - -commit: [f323bcd](https://github.com/kalm/kalm.js/commit/f323bcdc163faa40b0f8515fd4a8759e5180f516) - -### Added -- Added realtime profile -- Added JSDoc -- Added parameter validation and error messages - -### Changed -- Better performances (3x with default congestion) - -## [v2.2.0] - 2017-06-21 - -commit: [b9f3bdd](https://github.com/kalm/kalm.js/commit/b9f3bdd50de8dae2b92a0866d234a0cb2e72f22b) - -### Added -- Added support for node 8 - -## [v2.1.0] - 2017-06-20 - -commit: [129146f](https://github.com/kalm/kalm.js/commit/129146feeab14e94a540a4d9c54e05a4614fdb39) - -### Changed -- Simplified and optimized queue system logic -- Now allowing 0 to be passed as tick value in profiles -- Added warning when non-serialized message are sent and serial config is `null` -- Fixed hanging packets on maxBytes just reached - -## [v2.0.0] - 2017-03-02 - -commit: [b5209ec](https://github.com/kalm/kalm.js/commit/b5209ec4d3ab86000b72b502d120f0a5b4da85af) - -### Added -- Migrated the codebase from the [original repo](https://github.com/fed135/kalm) -- Implemeted new interface diff --git a/packages/kalm/LICENSE b/packages/kalm/LICENSE deleted file mode 100644 index c6746cb..0000000 --- a/packages/kalm/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2020 Frederic Charette - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/packages/kalm/package.json b/packages/kalm/package.json index fd85e02..7088706 100644 --- a/packages/kalm/package.json +++ b/packages/kalm/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "../../scripts/build.sh kalm", "clean": "../../scripts/cleanup.sh", - "test": "jest ./tests" + "test": "jest ./tests", + "prepublish": "cp ../../LICENSE ./LICENSE && cp ../../CHANGELOG.md ./CHANGELOG.md" }, "funding": { "type": "Open Collective", diff --git a/packages/tcp/CHANGELOG.md b/packages/tcp/CHANGELOG.md deleted file mode 100644 index 7715330..0000000 --- a/packages/tcp/CHANGELOG.md +++ /dev/null @@ -1,127 +0,0 @@ -# Changelog - -## [v3.3.0] - 2020-01-30 - -commit: [af46059](https://github.com/kalm/kalm.js/commit/af4605958c567b5243887f911850a3c0eb6c6659) - -### Added -- Adds webrtc package and examples -- Adds the getChannels method on Client - -### Removed -- Removed home implementation of EventEmitter in favor of Node's - -## [v3.2.3] - 2020-01-14 - -commit: [c188225](https://github.com/kalm/kalm.js/commit/c18822532a49f2026eddf44cfbe3cfc1521110f8) - -### Added -- Added pre-hook for lint on commit - -### Changed -- Cleaned up Types management and typings file accessibility -- Migrated test suite to Jest and centralized test tooling -- Removed output rollup, and using only tsc with none modules -- Fixed linting (was not targeting .ts files properly) -- Fixed stats events (were previously unreachable, now exposed through client emitter as .*) -- Fixed timeout behavior (only logged, now actually disconnects) - -## [v3.1.2] - 2019-07-01 - -commit: [fac8047](https://github.com/kalm/kalm.js/commit/fac8047d4b7048d56803505103159e16d8f518a8) - -### Changed -- Changed dev tooling from lerna to yarn workspaces -- Changed dev tooling from tslint to eslint + @typescript-eslint -- Housekeeping - -## [v3.0.0] - 2019-01-18 - -commit: [a4c687d](https://github.com/kalm/kalm.js/commit/a4c687dd5786a70723d9d0964a9d189220d58418) - -### Added -- New monorepo structure -- Massive new changes to the interface - -### Changed -- Serialization is now a toggle for json/binary -- Re-written the entire codebase in Typescript - -### Removed -- Transports are no longer bundled and must be installed separately and must be instantiated with options. - - [ipc](https://www.npmjs.com/package/@kalm/ipc) - - [tcp](https://www.npmjs.com/package/@kalm/tcp) - - [udp](https://www.npmjs.com/package/@kalm/udp) - - [ws](https://www.npmjs.com/package/@kalm/ws) -- Profiles become routines and must be instantiated with options. -- No more session stores -- No more encryption - -## [v2.6.1] - 2018-01-27 - -commit: [7393d17](https://github.com/kalm/kalm.js/commit/7393d17efb02088d7283ba83108fd7ab15e3d39e) - -### Added -- Added package-lock.json file -- Added server reference in the client object - -## [v2.5.0] - 2017-09-21 - -commit: [2c687f6](https://github.com/kalm/kalm.js/commit/2c687f6074787af6b39c10abe19669fe20e7b02d) - -### Added -- Added engines reqs - -### Changed -- Some minor performance tuning - -## [v2.4.0] - 2017-09-01 - -commit: [a7b8f95](https://github.com/kalm/kalm.js/commit/a7b8f950da56cbe35c538dc02e3dcc0e6d3a3db3) - -### Changed -- Tuned performances -- Added build targets for Node 8.x and 6.x -- UDP client cache (tied with socketTimeout) -- Bumped dependencies -- Proper callback on disconnect - -### Removed -- Dropped support for Node 4.x - -## [v2.3.0] - 2017-07-25 - -commit: [f323bcd](https://github.com/kalm/kalm.js/commit/f323bcdc163faa40b0f8515fd4a8759e5180f516) - -### Added -- Added realtime profile -- Added JSDoc -- Added parameter validation and error messages - -### Changed -- Better performances (3x with default congestion) - -## [v2.2.0] - 2017-06-21 - -commit: [b9f3bdd](https://github.com/kalm/kalm.js/commit/b9f3bdd50de8dae2b92a0866d234a0cb2e72f22b) - -### Added -- Added support for node 8 - -## [v2.1.0] - 2017-06-20 - -commit: [129146f](https://github.com/kalm/kalm.js/commit/129146feeab14e94a540a4d9c54e05a4614fdb39) - -### Changed -- Simplified and optimized queue system logic -- Now allowing 0 to be passed as tick value in profiles -- Added warning when non-serialized message are sent and serial config is `null` -- Fixed hanging packets on maxBytes just reached - -## [v2.0.0] - 2017-03-02 - -commit: [b5209ec](https://github.com/kalm/kalm.js/commit/b5209ec4d3ab86000b72b502d120f0a5b4da85af) - -### Added -- Migrated the codebase from the [original repo](https://github.com/fed135/kalm) -- Implemeted new interface diff --git a/packages/tcp/LICENSE b/packages/tcp/LICENSE deleted file mode 100644 index c6746cb..0000000 --- a/packages/tcp/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2020 Frederic Charette - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/packages/tcp/package.json b/packages/tcp/package.json index f37ffc3..3c4cb9c 100644 --- a/packages/tcp/package.json +++ b/packages/tcp/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "../../scripts/build.sh tcp", "clean": "../../scripts/cleanup.sh", - "test": "jest ./tests" + "test": "jest ./tests", + "prepublish": "cp ../../LICENSE ./LICENSE && cp ../../CHANGELOG.md ./CHANGELOG.md" }, "funding": { "type": "Open Collective", diff --git a/packages/tcp/src/tcp.ts b/packages/tcp/src/tcp.ts index b39e1ad..549a75d 100644 --- a/packages/tcp/src/tcp.ts +++ b/packages/tcp/src/tcp.ts @@ -4,7 +4,7 @@ import net from 'net'; /* Methods -------------------------------------------------------------------*/ -export function tcp({ socketTimeout = 30000 }: TCPConfig = {}): KalmTransport { +function tcp({ socketTimeout = 30000 }: TCPConfig = {}): KalmTransport { return function socket(params: ClientConfig, emitter: NodeJS.EventEmitter): Socket { let listener: net.Server; @@ -16,8 +16,8 @@ export function tcp({ socketTimeout = 30000 }: TCPConfig = {}): KalmTransport { function remote(handle: net.Socket): Remote { return { - host: handle.remoteAddress, - port: handle.remotePort, + host: handle && handle.remoteAddress || null, + port: handle && handle.remotePort || null, }; } diff --git a/packages/tcp/tests/unit/tcp.spec.ts b/packages/tcp/tests/unit/tcp.spec.ts index b024b76..d17e732 100644 --- a/packages/tcp/tests/unit/tcp.spec.ts +++ b/packages/tcp/tests/unit/tcp.spec.ts @@ -1,5 +1,40 @@ +import { EventEmitter } from 'events'; import * as tcp from '../../src/tcp'; describe('TCP transport', () => { - it('TODO', () => { expect(tcp).not.toBeUndefined(); }); + it('basic setup', () => { + expect(typeof tcp.default).toBe('function'); + const transport = tcp.default(); + expect(typeof transport).toBe('function'); + const socket = transport({}, new EventEmitter()); + + expect(socket).toHaveProperty('bind', expect.any(Function)); + expect(socket).toHaveProperty('connect', expect.any(Function)); + expect(socket).toHaveProperty('disconnect', expect.any(Function)); + expect(socket).toHaveProperty('remote', expect.any(Function)); + expect(socket).toHaveProperty('stop', expect.any(Function)); + expect(socket).toHaveProperty('send', expect.any(Function)); + }); + + describe('Given an empty handle reference and no configs', () => { + const transport = tcp.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return null values', () => { + expect(socket.remote()).toEqual({ host: null, port: null }); + }); + }); + }); + + describe('Given a handle reference and no configs', () => { + const transport = tcp.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return handle\'s values', () => { + expect(socket.remote({ remoteAddress: '127.0.0.1', remotePort: 3000 })).toEqual({ host: '127.0.0.1', port: 3000 }); + }); + }); + }); }); diff --git a/packages/udp/CHANGELOG.md b/packages/udp/CHANGELOG.md deleted file mode 100644 index 7715330..0000000 --- a/packages/udp/CHANGELOG.md +++ /dev/null @@ -1,127 +0,0 @@ -# Changelog - -## [v3.3.0] - 2020-01-30 - -commit: [af46059](https://github.com/kalm/kalm.js/commit/af4605958c567b5243887f911850a3c0eb6c6659) - -### Added -- Adds webrtc package and examples -- Adds the getChannels method on Client - -### Removed -- Removed home implementation of EventEmitter in favor of Node's - -## [v3.2.3] - 2020-01-14 - -commit: [c188225](https://github.com/kalm/kalm.js/commit/c18822532a49f2026eddf44cfbe3cfc1521110f8) - -### Added -- Added pre-hook for lint on commit - -### Changed -- Cleaned up Types management and typings file accessibility -- Migrated test suite to Jest and centralized test tooling -- Removed output rollup, and using only tsc with none modules -- Fixed linting (was not targeting .ts files properly) -- Fixed stats events (were previously unreachable, now exposed through client emitter as .*) -- Fixed timeout behavior (only logged, now actually disconnects) - -## [v3.1.2] - 2019-07-01 - -commit: [fac8047](https://github.com/kalm/kalm.js/commit/fac8047d4b7048d56803505103159e16d8f518a8) - -### Changed -- Changed dev tooling from lerna to yarn workspaces -- Changed dev tooling from tslint to eslint + @typescript-eslint -- Housekeeping - -## [v3.0.0] - 2019-01-18 - -commit: [a4c687d](https://github.com/kalm/kalm.js/commit/a4c687dd5786a70723d9d0964a9d189220d58418) - -### Added -- New monorepo structure -- Massive new changes to the interface - -### Changed -- Serialization is now a toggle for json/binary -- Re-written the entire codebase in Typescript - -### Removed -- Transports are no longer bundled and must be installed separately and must be instantiated with options. - - [ipc](https://www.npmjs.com/package/@kalm/ipc) - - [tcp](https://www.npmjs.com/package/@kalm/tcp) - - [udp](https://www.npmjs.com/package/@kalm/udp) - - [ws](https://www.npmjs.com/package/@kalm/ws) -- Profiles become routines and must be instantiated with options. -- No more session stores -- No more encryption - -## [v2.6.1] - 2018-01-27 - -commit: [7393d17](https://github.com/kalm/kalm.js/commit/7393d17efb02088d7283ba83108fd7ab15e3d39e) - -### Added -- Added package-lock.json file -- Added server reference in the client object - -## [v2.5.0] - 2017-09-21 - -commit: [2c687f6](https://github.com/kalm/kalm.js/commit/2c687f6074787af6b39c10abe19669fe20e7b02d) - -### Added -- Added engines reqs - -### Changed -- Some minor performance tuning - -## [v2.4.0] - 2017-09-01 - -commit: [a7b8f95](https://github.com/kalm/kalm.js/commit/a7b8f950da56cbe35c538dc02e3dcc0e6d3a3db3) - -### Changed -- Tuned performances -- Added build targets for Node 8.x and 6.x -- UDP client cache (tied with socketTimeout) -- Bumped dependencies -- Proper callback on disconnect - -### Removed -- Dropped support for Node 4.x - -## [v2.3.0] - 2017-07-25 - -commit: [f323bcd](https://github.com/kalm/kalm.js/commit/f323bcdc163faa40b0f8515fd4a8759e5180f516) - -### Added -- Added realtime profile -- Added JSDoc -- Added parameter validation and error messages - -### Changed -- Better performances (3x with default congestion) - -## [v2.2.0] - 2017-06-21 - -commit: [b9f3bdd](https://github.com/kalm/kalm.js/commit/b9f3bdd50de8dae2b92a0866d234a0cb2e72f22b) - -### Added -- Added support for node 8 - -## [v2.1.0] - 2017-06-20 - -commit: [129146f](https://github.com/kalm/kalm.js/commit/129146feeab14e94a540a4d9c54e05a4614fdb39) - -### Changed -- Simplified and optimized queue system logic -- Now allowing 0 to be passed as tick value in profiles -- Added warning when non-serialized message are sent and serial config is `null` -- Fixed hanging packets on maxBytes just reached - -## [v2.0.0] - 2017-03-02 - -commit: [b5209ec](https://github.com/kalm/kalm.js/commit/b5209ec4d3ab86000b72b502d120f0a5b4da85af) - -### Added -- Migrated the codebase from the [original repo](https://github.com/fed135/kalm) -- Implemeted new interface diff --git a/packages/udp/LICENSE b/packages/udp/LICENSE deleted file mode 100644 index c6746cb..0000000 --- a/packages/udp/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2020 Frederic Charette - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/packages/udp/package.json b/packages/udp/package.json index 0e3d5b2..6df222a 100644 --- a/packages/udp/package.json +++ b/packages/udp/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "../../scripts/build.sh udp", "clean": "../../scripts/cleanup.sh", - "test": "jest ./tests" + "test": "jest ./tests", + "prepublish": "cp ../../LICENSE ./LICENSE && cp ../../CHANGELOG.md ./CHANGELOG.md" }, "funding": { "type": "Open Collective", diff --git a/packages/udp/src/udp.ts b/packages/udp/src/udp.ts index fa898df..3bd43b6 100644 --- a/packages/udp/src/udp.ts +++ b/packages/udp/src/udp.ts @@ -26,7 +26,7 @@ function udp({ type = 'udp4', localAddr = '0.0.0.0', reuseAddr = true, socketTim } function remote(handle: SocketHandle): Remote { - return handle as Remote; + return handle || { host: null, port: null } as Remote; } function send(handle: UDPSocketHandle, payload: Buffer | number[]): void { diff --git a/packages/udp/tests/unit/udp.spec.ts b/packages/udp/tests/unit/udp.spec.ts index d43e569..8187a0f 100644 --- a/packages/udp/tests/unit/udp.spec.ts +++ b/packages/udp/tests/unit/udp.spec.ts @@ -1,5 +1,40 @@ +import { EventEmitter } from 'events'; import * as udp from '../../src/udp'; describe('UDP transport', () => { - it('TODO', () => { expect(udp).not.toBeUndefined(); }); + it('basic setup', () => { + expect(typeof udp.default).toBe('function'); + const transport = udp.default(); + expect(typeof transport).toBe('function'); + const socket = transport({}, new EventEmitter()); + + expect(socket).toHaveProperty('bind', expect.any(Function)); + expect(socket).toHaveProperty('connect', expect.any(Function)); + expect(socket).toHaveProperty('disconnect', expect.any(Function)); + expect(socket).toHaveProperty('remote', expect.any(Function)); + expect(socket).toHaveProperty('stop', expect.any(Function)); + expect(socket).toHaveProperty('send', expect.any(Function)); + }); + + describe('Given an empty handle reference and no configs', () => { + const transport = udp.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return null values', () => { + expect(socket.remote()).toEqual({ host: null, port: null }); + }); + }); + }); + + describe('Given a handle reference and no configs', () => { + const transport = udp.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return handle\'s values', () => { + expect(socket.remote({ host: '127.0.0.1', port: 3000 })).toEqual({ host: '127.0.0.1', port: 3000 }); + }); + }); + }); }); diff --git a/packages/webrtc/CHANGELOG.md b/packages/webrtc/CHANGELOG.md deleted file mode 100644 index 7715330..0000000 --- a/packages/webrtc/CHANGELOG.md +++ /dev/null @@ -1,127 +0,0 @@ -# Changelog - -## [v3.3.0] - 2020-01-30 - -commit: [af46059](https://github.com/kalm/kalm.js/commit/af4605958c567b5243887f911850a3c0eb6c6659) - -### Added -- Adds webrtc package and examples -- Adds the getChannels method on Client - -### Removed -- Removed home implementation of EventEmitter in favor of Node's - -## [v3.2.3] - 2020-01-14 - -commit: [c188225](https://github.com/kalm/kalm.js/commit/c18822532a49f2026eddf44cfbe3cfc1521110f8) - -### Added -- Added pre-hook for lint on commit - -### Changed -- Cleaned up Types management and typings file accessibility -- Migrated test suite to Jest and centralized test tooling -- Removed output rollup, and using only tsc with none modules -- Fixed linting (was not targeting .ts files properly) -- Fixed stats events (were previously unreachable, now exposed through client emitter as .*) -- Fixed timeout behavior (only logged, now actually disconnects) - -## [v3.1.2] - 2019-07-01 - -commit: [fac8047](https://github.com/kalm/kalm.js/commit/fac8047d4b7048d56803505103159e16d8f518a8) - -### Changed -- Changed dev tooling from lerna to yarn workspaces -- Changed dev tooling from tslint to eslint + @typescript-eslint -- Housekeeping - -## [v3.0.0] - 2019-01-18 - -commit: [a4c687d](https://github.com/kalm/kalm.js/commit/a4c687dd5786a70723d9d0964a9d189220d58418) - -### Added -- New monorepo structure -- Massive new changes to the interface - -### Changed -- Serialization is now a toggle for json/binary -- Re-written the entire codebase in Typescript - -### Removed -- Transports are no longer bundled and must be installed separately and must be instantiated with options. - - [ipc](https://www.npmjs.com/package/@kalm/ipc) - - [tcp](https://www.npmjs.com/package/@kalm/tcp) - - [udp](https://www.npmjs.com/package/@kalm/udp) - - [ws](https://www.npmjs.com/package/@kalm/ws) -- Profiles become routines and must be instantiated with options. -- No more session stores -- No more encryption - -## [v2.6.1] - 2018-01-27 - -commit: [7393d17](https://github.com/kalm/kalm.js/commit/7393d17efb02088d7283ba83108fd7ab15e3d39e) - -### Added -- Added package-lock.json file -- Added server reference in the client object - -## [v2.5.0] - 2017-09-21 - -commit: [2c687f6](https://github.com/kalm/kalm.js/commit/2c687f6074787af6b39c10abe19669fe20e7b02d) - -### Added -- Added engines reqs - -### Changed -- Some minor performance tuning - -## [v2.4.0] - 2017-09-01 - -commit: [a7b8f95](https://github.com/kalm/kalm.js/commit/a7b8f950da56cbe35c538dc02e3dcc0e6d3a3db3) - -### Changed -- Tuned performances -- Added build targets for Node 8.x and 6.x -- UDP client cache (tied with socketTimeout) -- Bumped dependencies -- Proper callback on disconnect - -### Removed -- Dropped support for Node 4.x - -## [v2.3.0] - 2017-07-25 - -commit: [f323bcd](https://github.com/kalm/kalm.js/commit/f323bcdc163faa40b0f8515fd4a8759e5180f516) - -### Added -- Added realtime profile -- Added JSDoc -- Added parameter validation and error messages - -### Changed -- Better performances (3x with default congestion) - -## [v2.2.0] - 2017-06-21 - -commit: [b9f3bdd](https://github.com/kalm/kalm.js/commit/b9f3bdd50de8dae2b92a0866d234a0cb2e72f22b) - -### Added -- Added support for node 8 - -## [v2.1.0] - 2017-06-20 - -commit: [129146f](https://github.com/kalm/kalm.js/commit/129146feeab14e94a540a4d9c54e05a4614fdb39) - -### Changed -- Simplified and optimized queue system logic -- Now allowing 0 to be passed as tick value in profiles -- Added warning when non-serialized message are sent and serial config is `null` -- Fixed hanging packets on maxBytes just reached - -## [v2.0.0] - 2017-03-02 - -commit: [b5209ec](https://github.com/kalm/kalm.js/commit/b5209ec4d3ab86000b72b502d120f0a5b4da85af) - -### Added -- Migrated the codebase from the [original repo](https://github.com/fed135/kalm) -- Implemeted new interface diff --git a/packages/webrtc/LICENSE b/packages/webrtc/LICENSE deleted file mode 100644 index c6746cb..0000000 --- a/packages/webrtc/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2020 Frederic Charette - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/packages/webrtc/package.json b/packages/webrtc/package.json index bc9b3b4..703ea13 100644 --- a/packages/webrtc/package.json +++ b/packages/webrtc/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "../../scripts/build.sh webrtc", "clean": "../../scripts/cleanup.sh", - "test": "jest ./tests" + "test": "jest ./tests", + "prepublish": "cp ../../LICENSE ./LICENSE && cp ../../CHANGELOG.md ./CHANGELOG.md" }, "funding": { "type": "Open Collective", diff --git a/packages/webrtc/src/webrtc.ts b/packages/webrtc/src/webrtc.ts index 1c61056..d9468ff 100644 --- a/packages/webrtc/src/webrtc.ts +++ b/packages/webrtc/src/webrtc.ts @@ -65,10 +65,7 @@ function webrtc(config: WebRTCConfig = {}): KalmTransport { } function remote(handle: any): Remote { - return { - host: handle, - port: handle || 0, - }; + return handle || { host: null, port: null }; } function disconnect(handle) { diff --git a/packages/webrtc/tests/unit/webrtc.spec.ts b/packages/webrtc/tests/unit/webrtc.spec.ts index 0bdb197..ec609e4 100644 --- a/packages/webrtc/tests/unit/webrtc.spec.ts +++ b/packages/webrtc/tests/unit/webrtc.spec.ts @@ -1,5 +1,41 @@ +import { EventEmitter } from 'events'; import * as webrtc from '../../src/webrtc'; -describe('WebRTC transport', () => { - it('TODO', () => { expect(webrtc).not.toBeUndefined(); }); +describe('webrtc transport', () => { + it('basic setup', () => { + expect(typeof webrtc.default).toBe('function'); + const transport = webrtc.default(); + expect(typeof transport).toBe('function'); + const socket = transport({}, new EventEmitter()); + + expect(socket).toHaveProperty('bind', expect.any(Function)); + expect(socket).toHaveProperty('connect', expect.any(Function)); + expect(socket).toHaveProperty('disconnect', expect.any(Function)); + expect(socket).toHaveProperty('remote', expect.any(Function)); + expect(socket).toHaveProperty('stop', expect.any(Function)); + expect(socket).toHaveProperty('send', expect.any(Function)); + expect(socket).toHaveProperty('negociate', expect.any(Function)); + }); + + describe('Given an empty handle reference and no configs', () => { + const transport = webrtc.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return null values', () => { + expect(socket.remote()).toEqual({ host: null, port: null }); + }); + }); + }); + + describe('Given a handle reference and no configs', () => { + const transport = webrtc.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return handle\'s values', () => { + expect(socket.remote({ host: '127.0.0.1', port: 3000 })).toEqual({ host: '127.0.0.1', port: 3000 }); + }); + }); + }); }); diff --git a/packages/ws/CHANGELOG.md b/packages/ws/CHANGELOG.md deleted file mode 100644 index 7715330..0000000 --- a/packages/ws/CHANGELOG.md +++ /dev/null @@ -1,127 +0,0 @@ -# Changelog - -## [v3.3.0] - 2020-01-30 - -commit: [af46059](https://github.com/kalm/kalm.js/commit/af4605958c567b5243887f911850a3c0eb6c6659) - -### Added -- Adds webrtc package and examples -- Adds the getChannels method on Client - -### Removed -- Removed home implementation of EventEmitter in favor of Node's - -## [v3.2.3] - 2020-01-14 - -commit: [c188225](https://github.com/kalm/kalm.js/commit/c18822532a49f2026eddf44cfbe3cfc1521110f8) - -### Added -- Added pre-hook for lint on commit - -### Changed -- Cleaned up Types management and typings file accessibility -- Migrated test suite to Jest and centralized test tooling -- Removed output rollup, and using only tsc with none modules -- Fixed linting (was not targeting .ts files properly) -- Fixed stats events (were previously unreachable, now exposed through client emitter as .*) -- Fixed timeout behavior (only logged, now actually disconnects) - -## [v3.1.2] - 2019-07-01 - -commit: [fac8047](https://github.com/kalm/kalm.js/commit/fac8047d4b7048d56803505103159e16d8f518a8) - -### Changed -- Changed dev tooling from lerna to yarn workspaces -- Changed dev tooling from tslint to eslint + @typescript-eslint -- Housekeeping - -## [v3.0.0] - 2019-01-18 - -commit: [a4c687d](https://github.com/kalm/kalm.js/commit/a4c687dd5786a70723d9d0964a9d189220d58418) - -### Added -- New monorepo structure -- Massive new changes to the interface - -### Changed -- Serialization is now a toggle for json/binary -- Re-written the entire codebase in Typescript - -### Removed -- Transports are no longer bundled and must be installed separately and must be instantiated with options. - - [ipc](https://www.npmjs.com/package/@kalm/ipc) - - [tcp](https://www.npmjs.com/package/@kalm/tcp) - - [udp](https://www.npmjs.com/package/@kalm/udp) - - [ws](https://www.npmjs.com/package/@kalm/ws) -- Profiles become routines and must be instantiated with options. -- No more session stores -- No more encryption - -## [v2.6.1] - 2018-01-27 - -commit: [7393d17](https://github.com/kalm/kalm.js/commit/7393d17efb02088d7283ba83108fd7ab15e3d39e) - -### Added -- Added package-lock.json file -- Added server reference in the client object - -## [v2.5.0] - 2017-09-21 - -commit: [2c687f6](https://github.com/kalm/kalm.js/commit/2c687f6074787af6b39c10abe19669fe20e7b02d) - -### Added -- Added engines reqs - -### Changed -- Some minor performance tuning - -## [v2.4.0] - 2017-09-01 - -commit: [a7b8f95](https://github.com/kalm/kalm.js/commit/a7b8f950da56cbe35c538dc02e3dcc0e6d3a3db3) - -### Changed -- Tuned performances -- Added build targets for Node 8.x and 6.x -- UDP client cache (tied with socketTimeout) -- Bumped dependencies -- Proper callback on disconnect - -### Removed -- Dropped support for Node 4.x - -## [v2.3.0] - 2017-07-25 - -commit: [f323bcd](https://github.com/kalm/kalm.js/commit/f323bcdc163faa40b0f8515fd4a8759e5180f516) - -### Added -- Added realtime profile -- Added JSDoc -- Added parameter validation and error messages - -### Changed -- Better performances (3x with default congestion) - -## [v2.2.0] - 2017-06-21 - -commit: [b9f3bdd](https://github.com/kalm/kalm.js/commit/b9f3bdd50de8dae2b92a0866d234a0cb2e72f22b) - -### Added -- Added support for node 8 - -## [v2.1.0] - 2017-06-20 - -commit: [129146f](https://github.com/kalm/kalm.js/commit/129146feeab14e94a540a4d9c54e05a4614fdb39) - -### Changed -- Simplified and optimized queue system logic -- Now allowing 0 to be passed as tick value in profiles -- Added warning when non-serialized message are sent and serial config is `null` -- Fixed hanging packets on maxBytes just reached - -## [v2.0.0] - 2017-03-02 - -commit: [b5209ec](https://github.com/kalm/kalm.js/commit/b5209ec4d3ab86000b72b502d120f0a5b4da85af) - -### Added -- Migrated the codebase from the [original repo](https://github.com/fed135/kalm) -- Implemeted new interface diff --git a/packages/ws/LICENSE b/packages/ws/LICENSE deleted file mode 100644 index c6746cb..0000000 --- a/packages/ws/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2020 Frederic Charette - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. diff --git a/packages/ws/package.json b/packages/ws/package.json index ad64026..5488c9e 100644 --- a/packages/ws/package.json +++ b/packages/ws/package.json @@ -6,7 +6,8 @@ "scripts": { "build": "../../scripts/build.sh ws", "clean": "../../scripts/cleanup.sh", - "test": "jest ./tests" + "test": "jest ./tests", + "prepublish": "cp ../../LICENSE ./LICENSE && cp ../../CHANGELOG.md ./CHANGELOG.md" }, "funding": { "type": "Open Collective", diff --git a/packages/ws/src/ws.ts b/packages/ws/src/ws.ts index 8322360..39e8db1 100644 --- a/packages/ws/src/ws.ts +++ b/packages/ws/src/ws.ts @@ -48,13 +48,13 @@ function ws({ cert, key, secure }: WSConfig = {}): KalmTransport { } function remote(handle: WebSocket & { headers: any, connection: any }): Remote { - const h = handle.headers; + const h = handle && handle.headers || {}; return { host: ( (h && h['x-forwarded-for'] && h['x-forwarded-for'].split(',')[0]) - || (handle.connection && handle.connection.remoteAddress || '0.0.0.0') + || (handle && handle.connection && handle.connection.remoteAddress || null) ), - port: handle.connection && handle.connection.remotePort || 0, + port: handle && handle.connection && handle.connection.remotePort || null, }; } diff --git a/packages/ws/tests/unit/ws.spec.ts b/packages/ws/tests/unit/ws.spec.ts index 6923313..fdd70b4 100644 --- a/packages/ws/tests/unit/ws.spec.ts +++ b/packages/ws/tests/unit/ws.spec.ts @@ -1,5 +1,47 @@ +import { EventEmitter } from 'events'; import * as ws from '../../src/ws'; -describe('WS transport', () => { - it('TODO', () => { expect(ws).not.toBeUndefined(); }); +describe('ws transport', () => { + it('basic setup', () => { + expect(typeof ws.default).toBe('function'); + const transport = ws.default(); + expect(typeof transport).toBe('function'); + const socket = transport({}, new EventEmitter()); + + expect(socket).toHaveProperty('bind', expect.any(Function)); + expect(socket).toHaveProperty('connect', expect.any(Function)); + expect(socket).toHaveProperty('disconnect', expect.any(Function)); + expect(socket).toHaveProperty('remote', expect.any(Function)); + expect(socket).toHaveProperty('stop', expect.any(Function)); + expect(socket).toHaveProperty('send', expect.any(Function)); + }); + + describe('Given an empty handle reference and no configs', () => { + const transport = ws.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return null values', () => { + expect(socket.remote()).toEqual({ host: null, port: null }); + }); + }); + }); + + describe('Given a handle reference and no configs', () => { + const transport = ws.default(); + const socket = transport({}, new EventEmitter()); + + describe('when fetching remote', () => { + it('should return handle\'s values from headers', () => { + expect(socket.remote({ + headers: { 'x-forwarded-for': '127.0.0.1' }, + connection: { remotePort: 3000 }, + })).toEqual({ host: '127.0.0.1', port: 3000 }); + }); + + it('should return handle\'s values from connection', () => { + expect(socket.remote({ connection: { remoteAddress: '127.0.0.1', remotePort: 3000 } })).toEqual({ host: '127.0.0.1', port: 3000 }); + }); + }); + }); });