From dee453918e936753199d553607505543f26f2cc9 Mon Sep 17 00:00:00 2001 From: Satnam Singh Brar Date: Fri, 23 Sep 2022 11:04:45 -0700 Subject: [PATCH 1/3] feat: provide defaults for undefined args, kwargs, details for reg/sub --- src/swampyer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/swampyer.ts b/src/swampyer.ts index ddfc638..4ec9743 100644 --- a/src/swampyer.ts +++ b/src/swampyer.ts @@ -391,7 +391,7 @@ export class Swampyer { const [subscriptionId, , details, args, kwargs] = data as MessageData[MessageTypes.Event]; const { uri, handler } = this.subscriptionHandlers[subscriptionId] || {}; try { - handler?.(args, kwargs, details); + handler?.(args ?? [], kwargs ?? {}, details ?? {}); } catch (e) { // eslint-disable-next-line no-console console.error(`An unhandled error occurred while running subscription handler for "${uri}"`, e); @@ -407,7 +407,7 @@ export class Swampyer { [MessageTypes.Invocation, requestId, {}, 'com.error.unavailable', ['No handler available for this request'], {}] ); } else { - Promise.resolve((async () => handler(args, kwargs, details))()) + Promise.resolve((async () => handler(args ?? [], kwargs ?? {}, details ?? {}))()) .then(result => this.transport!._send(MessageTypes.Yield, [requestId, {}, [result], {}])) .catch(e => { // eslint-disable-next-line no-console From 8865326c6a891e563541f2e04f97db4522f19264 Mon Sep 17 00:00:00 2001 From: Satnam Singh Brar Date: Fri, 23 Sep 2022 11:14:09 -0700 Subject: [PATCH 2/3] test: add tests for the changes --- src/swampyer.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/swampyer.test.ts b/src/swampyer.test.ts index ecad2ea..bd34d9a 100644 --- a/src/swampyer.test.ts +++ b/src/swampyer.test.ts @@ -644,6 +644,28 @@ describe(`${Swampyer.prototype.register.name}()`, () => { }); }); + it('provides reasonable defaults for args, kwargs, and details if they are undefined in the invocation data', async () => { + const regHandler = jest.fn().mockImplementation(async () => 'fancy result'); + + const regPromise = wamp.register('com.test.something', regHandler); + + const regRequest = await transportProvider.transport.read(); + expect(regRequest).toEqual([MessageTypes.Register, expect.any(Number), expect.any(Object), 'com.test.something']); + transportProvider.sendToLib(MessageTypes.Registered, [regRequest[1] as number, 1234]); + + const regId = await regPromise; + expect(regId).toEqual(1234); + + const args = undefined; + const kwargs = undefined; + const details = undefined; + transportProvider.sendToLib(MessageTypes.Invocation, [5656, 1234, details!, args!, kwargs!]); + await waitUntilPass(() => expect(regHandler).toBeCalledTimes(1)); + expect(regHandler).toBeCalledWith([], {}, {}); + + expect(await transportProvider.transport.read()).toEqual([MessageTypes.Yield, 5656, {}, ['fancy result'], {}]); + }); + it('multiple reigstrations are kept separate and handled properly when a call() is made for them', async () => { const regHandler1 = jest.fn().mockResolvedValue('fancy result 1'); const regPromise1 = wamp.register('com.test.something', regHandler1); @@ -804,6 +826,22 @@ describe(`${Swampyer.prototype.subscribe.name}()`, () => { expect(subHandler2).toBeCalledWith(['for 2nd sub'], {}, {}); }); + it('provides reasonable defaults for args, kwargs, and details if they are undefined in the event data', async () => { + const subHandler = jest.fn(); + const promise = wamp.subscribe('com.some.uri', subHandler); + const request = await transportProvider.transport.read(); + expect(request).toEqual([MessageTypes.Subscribe, expect.any(Number), expect.any(Object), 'com.some.uri']); + transportProvider.sendToLib(MessageTypes.Subscribed, [request[1] as number, 1234]); + await promise; + + const args = undefined; + const kwargs = undefined; + const details = undefined; + transportProvider.sendToLib(MessageTypes.Event, [1234, 5555, details!, args!, kwargs!]); + expect(subHandler).toBeCalledTimes(1); + expect(subHandler).toBeCalledWith([], {}, {}); + }); + it('throws an error if subscription fails', async () => { const subHandler = jest.fn(); const promise = wamp.subscribe('com.some.uri', subHandler); From db67592cc5dbbd039fdd42d3a48a6cd9d7562150 Mon Sep 17 00:00:00 2001 From: Satnam Singh Brar Date: Fri, 23 Sep 2022 11:22:48 -0700 Subject: [PATCH 3/3] chore: bump version to `1.4.3` --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6a95cf1..12cc57b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "swampyer", - "version": "1.4.2", + "version": "1.4.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "swampyer", - "version": "1.4.2", + "version": "1.4.3", "license": "MIT", "devDependencies": { "@types/jest": "^27.0.2", diff --git a/package.json b/package.json index cdbdd96..bf87fb0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swampyer", - "version": "1.4.2", + "version": "1.4.3", "description": "A lightweight WAMP client implementing the WAMP v2 basic profile", "main": "lib/index.js", "module": "lib/index.js",