From 88ae0e80ab0be16217ce6d2442944e52bd7bc445 Mon Sep 17 00:00:00 2001 From: Khuda Dad Nomani <32505158+KhudaDad414@users.noreply.github.com> Date: Wed, 3 Jan 2024 12:28:36 +0000 Subject: [PATCH] fix: resolve crypto-websocket error (#668) --- .../crypto-websockets/client/asyncapi.yaml | 10 +- examples/crypto-websockets/client/db.json | 2 +- .../client/package-lock.json | 38 ++-- .../crypto-websockets/server/asyncapi.yaml | 10 +- .../server/lifecycle/updateCryptoPrice.ts | 2 +- .../server/package-lock.json | 192 ++++++++++++++++-- .../crypto-websockets/server/package.json | 3 +- src/adapters/ws/client.ts | 7 +- src/adapters/ws/server.ts | 3 +- src/index.ts | 2 +- 10 files changed, 211 insertions(+), 58 deletions(-) diff --git a/examples/crypto-websockets/client/asyncapi.yaml b/examples/crypto-websockets/client/asyncapi.yaml index 496568046..0a2bafb3c 100644 --- a/examples/crypto-websockets/client/asyncapi.yaml +++ b/examples/crypto-websockets/client/asyncapi.yaml @@ -15,10 +15,10 @@ servers: - $ref: '#/components/securitySchemes/apiKey' - $ref: '#/components/securitySchemes/cert' channels: - /price: + price: address: /price messages: - index.message: + indexGraph: $ref: '#/components/messages/indexGraph' bindings: ws: @@ -27,9 +27,9 @@ operations: index: action: receive channel: - $ref: '#/channels/~1price' + $ref: '#/channels/price' messages: - - $ref: '#/components/messages/indexGraph' + - $ref: '#/channels/price/messages/indexGraph' components: messages: indexGraph: @@ -57,4 +57,4 @@ components: type: apiKey in: user x-remoteServers: - - websockets + - websockets \ No newline at end of file diff --git a/examples/crypto-websockets/client/db.json b/examples/crypto-websockets/client/db.json index 5102a11ff..5f7dc1cb5 100644 --- a/examples/crypto-websockets/client/db.json +++ b/examples/crypto-websockets/client/db.json @@ -1 +1 @@ -[{"time":1692154441640,"price":130,"status":"started"},{"time":1692154442650,"price":140,"status":"intransit"},{"time":1692154442650,"price":140,"status":"intransit"},{"time":1692154443663,"price":180,"status":"intransit"},{"time":1692154443663,"price":180,"status":"intransit"},{"time":1692154444668,"price":180,"status":"intransit"},{"time":1692154444668,"price":180,"status":"intransit"},{"time":1692154445678,"price":160,"status":"intransit"},{"time":1692154445678,"price":160,"status":"intransit"},{"time":1692154446687,"price":120,"status":"intransit"},{"time":1692154446687,"price":120,"status":"intransit"},{"time":1692154447695,"price":110,"status":"intransit"},{"time":1692154447695,"price":110,"status":"intransit"},{"time":1692154448703,"price":130,"status":"intransit"},{"time":1692154448703,"price":130,"status":"intransit"},{"time":1692154449713,"price":130,"status":"intransit"},{"time":1692154449713,"price":130,"status":"intransit"}] \ No newline at end of file +[{"time":1704277182766,"price":140,"status":"started"},{"time":1704277183789,"price":150,"status":"intransit"},{"time":1704277183789,"price":150,"status":"intransit"},{"time":1704277184806,"price":180,"status":"intransit"},{"time":1704277184806,"price":180,"status":"intransit"},{"time":1704277185818,"price":210,"status":"intransit"},{"time":1704277185818,"price":210,"status":"intransit"},{"time":1704277186829,"price":220,"status":"intransit"},{"time":1704277186829,"price":220,"status":"intransit"},{"time":1704277187841,"price":260,"status":"intransit"},{"time":1704277187841,"price":260,"status":"intransit"},{"time":1704277188845,"price":250,"status":"intransit"},{"time":1704277188845,"price":250,"status":"intransit"},{"time":1704277189858,"price":290,"status":"intransit"},{"time":1704277189858,"price":290,"status":"intransit"},{"time":1704277190860,"price":310,"status":"intransit"},{"time":1704277190860,"price":310,"status":"intransit"}] \ No newline at end of file diff --git a/examples/crypto-websockets/client/package-lock.json b/examples/crypto-websockets/client/package-lock.json index 722de8e40..dcf863058 100644 --- a/examples/crypto-websockets/client/package-lock.json +++ b/examples/crypto-websockets/client/package-lock.json @@ -15,14 +15,14 @@ }, "../../..": { "name": "@asyncapi/glee", - "version": "0.26.10", + "version": "0.33.1", "license": "Apache-2.0", "dependencies": { - "@asyncapi/generator": "^1.13.1", - "@asyncapi/html-template": "^0.28.4", - "@asyncapi/markdown-template": "^1.3.3", - "@asyncapi/parser": "^2.1.0", - "@types/jest": "^27.4.0", + "@asyncapi/generator": "^1.15.9", + "@asyncapi/html-template": "^2.0.0", + "@asyncapi/markdown-template": "^1.5.0", + "@asyncapi/parser": "^3.0.2", + "@types/jest": "^29.5.11", "@types/qs": "^6.9.7", "ajv": "^6.12.6", "async": "^3.2.0", @@ -66,15 +66,15 @@ "@typescript-eslint/parser": "^5.9.0", "all-contributors-cli": "^6.14.2", "eslint": "^8.6.0", - "eslint-plugin-jest": "^23.8.2", + "eslint-plugin-jest": "^27.6.0", "eslint-plugin-sonarjs": "^0.19.0", "fs-extra": "^10.1.0", - "jest": "^27.4.7", - "jest-extended": "^1.2.0", + "jest": "^29.7.0", + "jest-extended": "^4.0.2", "jsdoc-to-markdown": "^5.0.3", "markdown-toc": "^1.2.0", "rimraf": "^3.0.2", - "ts-jest": "^27.1.2", + "ts-jest": "^29.1.1", "tsc-watch": "^4.5.0", "typedoc": "^0.23.28", "typedoc-plugin-markdown": "^3.11.8", @@ -98,14 +98,14 @@ "@asyncapi/glee": { "version": "file:../../..", "requires": { - "@asyncapi/generator": "^1.13.1", - "@asyncapi/html-template": "^0.28.4", - "@asyncapi/markdown-template": "^1.3.3", - "@asyncapi/parser": "^2.1.0", + "@asyncapi/generator": "^1.15.9", + "@asyncapi/html-template": "^2.0.0", + "@asyncapi/markdown-template": "^1.5.0", + "@asyncapi/parser": "^3.0.2", "@tsconfig/node14": "^1.0.1", "@types/async": "^3.2.11", "@types/debug": "^4.1.7", - "@types/jest": "^27.4.0", + "@types/jest": "^29.5.11", "@types/qs": "^6.9.7", "@types/socket.io": "^3.0.2", "@types/uri-templates": "^0.1.31", @@ -125,13 +125,13 @@ "emojis": "^1.0.10", "eslint": "^8.6.0", "eslint-plugin-github": "^4.3.5", - "eslint-plugin-jest": "^23.8.2", + "eslint-plugin-jest": "^27.6.0", "eslint-plugin-security": "^1.4.0", "eslint-plugin-sonarjs": "^0.19.0", "fs-extra": "^10.1.0", "got": "^12.5.3", - "jest": "^27.4.7", - "jest-extended": "^1.2.0", + "jest": "^29.7.0", + "jest-extended": "^4.0.2", "jsdoc-to-markdown": "^5.0.3", "kafkajs": "^2.2.3", "markdown-toc": "^1.2.0", @@ -142,7 +142,7 @@ "rimraf": "^3.0.2", "socket.io": "^4.1.2", "terminal-image": "^2.0.0", - "ts-jest": "^27.1.2", + "ts-jest": "^29.1.1", "tsc-watch": "^4.5.0", "typedoc": "^0.23.28", "typedoc-plugin-markdown": "^3.11.8", diff --git a/examples/crypto-websockets/server/asyncapi.yaml b/examples/crypto-websockets/server/asyncapi.yaml index 5a7c59130..41848db70 100644 --- a/examples/crypto-websockets/server/asyncapi.yaml +++ b/examples/crypto-websockets/server/asyncapi.yaml @@ -18,10 +18,10 @@ servers: host: 'localhost:4000' protocol: ws channels: - /price: + price: address: /price messages: - subscribe.message: + indexGraph: $ref: '#/components/messages/indexGraph' bindings: ws: @@ -35,9 +35,9 @@ operations: /price.subscribe: action: send channel: - $ref: '#/channels/~1price' + $ref: '#/channels/price' messages: - - $ref: '#/components/messages/indexGraph' + - $ref: '#/channels/price/messages/indexGraph' components: messages: indexGraph: @@ -64,4 +64,4 @@ components: in: header cert: type: apiKey - in: user + in: user \ No newline at end of file diff --git a/examples/crypto-websockets/server/lifecycle/updateCryptoPrice.ts b/examples/crypto-websockets/server/lifecycle/updateCryptoPrice.ts index ed02a7113..89cb63215 100644 --- a/examples/crypto-websockets/server/lifecycle/updateCryptoPrice.ts +++ b/examples/crypto-websockets/server/lifecycle/updateCryptoPrice.ts @@ -18,7 +18,7 @@ export default async function ({ glee, connection }) { const date = new Date() setTimeout(() => { glee.send(new Message({ - channel: '/price', + channel: 'price', connection, payload: {time: date.getTime(), price: getPrice(), status} })) diff --git a/examples/crypto-websockets/server/package-lock.json b/examples/crypto-websockets/server/package-lock.json index 3139e0dfe..43c7a772a 100644 --- a/examples/crypto-websockets/server/package-lock.json +++ b/examples/crypto-websockets/server/package-lock.json @@ -9,19 +9,20 @@ "version": "1.0.0", "license": "ISC", "dependencies": { - "@asyncapi/glee": "file:../../.." + "@asyncapi/glee": "file:../../..", + "axios": "^1.6.3" } }, "../../..": { "name": "@asyncapi/glee", - "version": "0.26.10", + "version": "0.33.1", "license": "Apache-2.0", "dependencies": { - "@asyncapi/generator": "^1.13.1", - "@asyncapi/html-template": "^0.28.4", - "@asyncapi/markdown-template": "^1.3.3", - "@asyncapi/parser": "^2.1.0", - "@types/jest": "^27.4.0", + "@asyncapi/generator": "^1.15.9", + "@asyncapi/html-template": "^2.0.0", + "@asyncapi/markdown-template": "^1.5.0", + "@asyncapi/parser": "^3.0.2", + "@types/jest": "^29.5.11", "@types/qs": "^6.9.7", "ajv": "^6.12.6", "async": "^3.2.0", @@ -65,15 +66,15 @@ "@typescript-eslint/parser": "^5.9.0", "all-contributors-cli": "^6.14.2", "eslint": "^8.6.0", - "eslint-plugin-jest": "^23.8.2", + "eslint-plugin-jest": "^27.6.0", "eslint-plugin-sonarjs": "^0.19.0", "fs-extra": "^10.1.0", - "jest": "^27.4.7", - "jest-extended": "^1.2.0", + "jest": "^29.7.0", + "jest-extended": "^4.0.2", "jsdoc-to-markdown": "^5.0.3", "markdown-toc": "^1.2.0", "rimraf": "^3.0.2", - "ts-jest": "^27.1.2", + "ts-jest": "^29.1.1", "tsc-watch": "^4.5.0", "typedoc": "^0.23.28", "typedoc-plugin-markdown": "^3.11.8", @@ -86,20 +87,110 @@ "node_modules/@asyncapi/glee": { "resolved": "../../..", "link": true + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.3.tgz", + "integrity": "sha512-fWyNdeawGam70jXSVlKl+SUNVcL6j6W79CuSIPfi6HnDUmSCH6gyUys/HrqHeA/wU0Az41rRgean494d0Jb+ww==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/follow-redirects": { + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" } }, "dependencies": { "@asyncapi/glee": { "version": "file:../../..", "requires": { - "@asyncapi/generator": "^1.13.1", - "@asyncapi/html-template": "^0.28.4", - "@asyncapi/markdown-template": "^1.3.3", - "@asyncapi/parser": "^2.1.0", + "@asyncapi/generator": "^1.15.9", + "@asyncapi/html-template": "^2.0.0", + "@asyncapi/markdown-template": "^1.5.0", + "@asyncapi/parser": "^3.0.2", "@tsconfig/node14": "^1.0.1", "@types/async": "^3.2.11", "@types/debug": "^4.1.7", - "@types/jest": "^27.4.0", + "@types/jest": "^29.5.11", "@types/qs": "^6.9.7", "@types/socket.io": "^3.0.2", "@types/uri-templates": "^0.1.31", @@ -119,13 +210,13 @@ "emojis": "^1.0.10", "eslint": "^8.6.0", "eslint-plugin-github": "^4.3.5", - "eslint-plugin-jest": "^23.8.2", + "eslint-plugin-jest": "^27.6.0", "eslint-plugin-security": "^1.4.0", "eslint-plugin-sonarjs": "^0.19.0", "fs-extra": "^10.1.0", "got": "^12.5.3", - "jest": "^27.4.7", - "jest-extended": "^1.2.0", + "jest": "^29.7.0", + "jest-extended": "^4.0.2", "jsdoc-to-markdown": "^5.0.3", "kafkajs": "^2.2.3", "markdown-toc": "^1.2.0", @@ -136,7 +227,7 @@ "rimraf": "^3.0.2", "socket.io": "^4.1.2", "terminal-image": "^2.0.0", - "ts-jest": "^27.1.2", + "ts-jest": "^29.1.1", "tsc-watch": "^4.5.0", "typedoc": "^0.23.28", "typedoc-plugin-markdown": "^3.11.8", @@ -149,6 +240,67 @@ "word-wrap": "^1.2.3", "ws": "^7.4.6" } + }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "axios": { + "version": "1.6.3", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.3.tgz", + "integrity": "sha512-fWyNdeawGam70jXSVlKl+SUNVcL6j6W79CuSIPfi6HnDUmSCH6gyUys/HrqHeA/wU0Az41rRgean494d0Jb+ww==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "requires": { + "delayed-stream": "~1.0.0" + } + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==" + }, + "follow-redirects": { + "version": "1.15.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", + "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==" + }, + "form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==" + }, + "mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "requires": { + "mime-db": "1.52.0" + } + }, + "proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" } } } diff --git a/examples/crypto-websockets/server/package.json b/examples/crypto-websockets/server/package.json index b7b721a92..905bd520f 100644 --- a/examples/crypto-websockets/server/package.json +++ b/examples/crypto-websockets/server/package.json @@ -12,6 +12,7 @@ "author": "", "license": "ISC", "dependencies": { - "@asyncapi/glee": "file:../../.." + "@asyncapi/glee": "file:../../..", + "axios": "^1.6.3" } } diff --git a/src/adapters/ws/client.ts b/src/adapters/ws/client.ts index 7f2bf0cf0..2c20f4abb 100644 --- a/src/adapters/ws/client.ts +++ b/src/adapters/ws/client.ts @@ -74,9 +74,10 @@ class WsClientAdapter extends Adapter { this.emit('message', msg, client) }) - client.on('error', (err) => { - console.log('GETING ERROR') - this.emit('error', err) + client.on('error', (err: any) => { + const errMessage = `WebSocket client error on channel '${channel}'` + this.emit('error', new Error(errMessage)) + console.error(err) }) } return this diff --git a/src/adapters/ws/server.ts b/src/adapters/ws/server.ts index 2372370b6..2a1781029 100644 --- a/src/adapters/ws/server.ts +++ b/src/adapters/ws/server.ts @@ -182,8 +182,7 @@ class WebSocketsAdapter extends Adapter { private _extractPathname(req: IncomingMessage) { const serverUrl = new URL(this.serverUrlExpanded) - let { pathname } = new URL(req.url, serverUrl) - pathname = pathname.startsWith('/') ? pathname.substring(1) : pathname + const { pathname } = new URL(req.url, serverUrl) return pathname || '/' } diff --git a/src/index.ts b/src/index.ts index 704ac60a1..216b33407 100755 --- a/src/index.ts +++ b/src/index.ts @@ -232,7 +232,7 @@ export default async function GleeAppInitializer() { export function setUpReplyMiddlewares(reply: OperationReplyInterface, app: Glee) { - const channel = reply.channel() + const channel = reply?.channel() if (!channel) return const hasSendOperation = channel.operations().filterBySend().length > 0 if (hasSendOperation) {