From 66506e947262c23d520a0f60c25f087ddd4e9907 Mon Sep 17 00:00:00 2001 From: meenahoda Date: Fri, 25 Feb 2022 16:40:47 +0000 Subject: [PATCH 1/7] feat: store input with execution --- lib/Executions.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/Executions.js b/lib/Executions.js index 18e7cee3..f5775e39 100644 --- a/lib/Executions.js +++ b/lib/Executions.js @@ -115,7 +115,7 @@ module.exports = class Executions { ) if (!['NOAUTH', 'STATE_MACHINE_NOT_FOUND'].includes(data.status)) { - await this.storeFromServerRequest({ ...data, executionTitle: title }) + await this.storeFromServerRequest({ ...data, executionTitle: title, executionInput: input }) } return data @@ -126,7 +126,7 @@ module.exports = class Executions { * @param {object} execDesc - Execution description which was the result of the request. */ async storeFromServerRequest (execDesc) { - const { ctx, status, executionName, executionTitle, stateMachineName } = execDesc + const { ctx, status, executionName, executionTitle, executionInput, stateMachineName } = execDesc const date = new Date() const stateMachine = { name: stateMachineName } @@ -140,6 +140,7 @@ module.exports = class Executions { const res = { executionTitle, + executionInput, status, executionName, stateMachine, From 78f576ed1d4de2b32a72a7e9864aff67572c0178 Mon Sep 17 00:00:00 2001 From: meenahoda Date: Fri, 25 Feb 2022 16:59:28 +0000 Subject: [PATCH 2/7] feat: load execution into vuex --- lib/Executions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Executions.js b/lib/Executions.js index f5775e39..c889aeb2 100644 --- a/lib/Executions.js +++ b/lib/Executions.js @@ -22,6 +22,7 @@ module.exports = class Executions { await this.db.executions.delete(executionName) } await this.db.executions.put(exec) + await this.load(exec.executionName) } /** From dda24e73139a720e99528f61fb6dc8ccd029f90a Mon Sep 17 00:00:00 2001 From: meenahoda Date: Tue, 1 Mar 2022 13:37:41 +0000 Subject: [PATCH 3/7] feat: introduce websocket to sdk --- lib/Tymly-sdk.js | 3 ++ lib/WebSocket.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 lib/WebSocket.js diff --git a/lib/Tymly-sdk.js b/lib/Tymly-sdk.js index 9e9a4c16..fe7b8b59 100644 --- a/lib/Tymly-sdk.js +++ b/lib/Tymly-sdk.js @@ -14,6 +14,7 @@ const Info = require('./Info') const Route = require('./Route') const Tasks = require('./Tasks') const Downloads = require('./Downloads') +const WebSocket = require('./WebSocket') const database = require('./database') const { v1: uuidv1 } = require('uuid') const shasum = require('shasum') @@ -78,6 +79,7 @@ module.exports = class TymlyClient { this.watching = new Watching(this) this.suggestions = new Suggestions(this) this.download = new Downloads(this) + this.websocket = new WebSocket(this) await Promise.all(USER_QUERY_KEYS.map(k => this[k].load())) @@ -329,6 +331,7 @@ module.exports = class TymlyClient { * Closes down running services. */ async destroy () { + this.websocket.disconnect() this.options.store.commit('app/clear', {}) this.options.store.commit('tracker/clear', {}) this.haveRunUserQuery = false diff --git a/lib/WebSocket.js b/lib/WebSocket.js new file mode 100644 index 00000000..e8454bf0 --- /dev/null +++ b/lib/WebSocket.js @@ -0,0 +1,82 @@ +class _WebSocket { + constructor (client) { + this.userId = client.auth.sub + this.store = client.options.store + + this.ws = this.store.state.app.ws + } + + get url () { + let url = process.env.TYMLY_API_BASE_URL.replace(/^http/, 'ws') + if (url[url.length - 1] !== '/') url += '/' + + const params = [ + `subscriptions=${encodeURIComponent(Object.keys(this.store.state.app.subscriptions))}`, + `userId=${encodeURIComponent(this.userId)}` + ] + + url += `?${params.join('&')}` + return url + } + + subscribe (subs) { + if (!Array.isArray(subs) || subs.length === 0) { + console.log('Nothing to subscribe to') + return + } + + for (const { event } of subs) { + this.store.commit('app/subscribe', event) + } + + this.connect() + } + + unsubscribe (subs) { + if (!Array.isArray(subs) || subs.length === 0) { + console.log('Nothing to unsubscribe from') + return + } + + for (const { event } of subs) { + this.store.commit('app/unsubscribe', event) + } + + this.connect() + } + + connect () { + if (this.ws) return + + console.log('Opening websocket connection', this.url) + this.ws = new WebSocket(this.url) + this.ws.onopen = event => { + this.ws.onmessage = msg => { + // do not update if came from same source - msg.srcElement? + + let event = '' + let message = {} + + try { + const obj = JSON.parse(msg.data) + event = obj.key + message = obj.message + } catch (e) { } + + this.store.commit('app/subscriptionUpdate', { event, message }) + } + } + this.store.commit('app/ws', this.ws) + } + + disconnect () { + if (this.ws === null) return + + console.log('Closing websocket connection') + this.ws.close() + this.ws = null + this.store.commit('app/ws', this.ws) + } +} + +module.exports = _WebSocket From a5c5161b16765583b30f0c5561bc7ea596d63826 Mon Sep 17 00:00:00 2001 From: meenahoda Date: Tue, 1 Mar 2022 13:50:36 +0000 Subject: [PATCH 4/7] feat: change name of ws message mutation --- lib/WebSocket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/WebSocket.js b/lib/WebSocket.js index e8454bf0..160f403c 100644 --- a/lib/WebSocket.js +++ b/lib/WebSocket.js @@ -63,7 +63,7 @@ class _WebSocket { message = obj.message } catch (e) { } - this.store.commit('app/subscriptionUpdate', { event, message }) + this.store.commit('app/wsMessage', { event, message }) } } this.store.commit('app/ws', this.ws) From ef2db017a7be3110bd5f6b841128a7ebf35f725d Mon Sep 17 00:00:00 2001 From: meenahoda Date: Tue, 1 Mar 2022 15:30:37 +0000 Subject: [PATCH 5/7] feat: plugin to handle subscriptions --- lib/WebSocket.js | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/lib/WebSocket.js b/lib/WebSocket.js index 160f403c..ae2ff5d8 100644 --- a/lib/WebSocket.js +++ b/lib/WebSocket.js @@ -2,8 +2,8 @@ class _WebSocket { constructor (client) { this.userId = client.auth.sub this.store = client.options.store - this.ws = this.store.state.app.ws + this.connect() } get url () { @@ -11,7 +11,6 @@ class _WebSocket { if (url[url.length - 1] !== '/') url += '/' const params = [ - `subscriptions=${encodeURIComponent(Object.keys(this.store.state.app.subscriptions))}`, `userId=${encodeURIComponent(this.userId)}` ] @@ -19,30 +18,14 @@ class _WebSocket { return url } - subscribe (subs) { - if (!Array.isArray(subs) || subs.length === 0) { - console.log('Nothing to subscribe to') - return - } - - for (const { event } of subs) { - this.store.commit('app/subscribe', event) - } - + subscribe (subscriptions) { this.connect() + this.ws.send(JSON.stringify({ event: 'subscribe', subscriptions })) } - unsubscribe (subs) { - if (!Array.isArray(subs) || subs.length === 0) { - console.log('Nothing to unsubscribe from') - return - } - - for (const { event } of subs) { - this.store.commit('app/unsubscribe', event) - } - + unsubscribe (subscriptions) { this.connect() + this.ws.send(JSON.stringify({ event: 'unsubscribe', subscriptions })) } connect () { From b7fd3932b7531b879dfa28113c79ba14880441f8 Mon Sep 17 00:00:00 2001 From: meenahoda Date: Wed, 2 Mar 2022 12:56:19 +0000 Subject: [PATCH 6/7] feat: pass token to ws connection --- lib/WebSocket.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/WebSocket.js b/lib/WebSocket.js index ae2ff5d8..4f55f954 100644 --- a/lib/WebSocket.js +++ b/lib/WebSocket.js @@ -1,7 +1,7 @@ class _WebSocket { constructor (client) { - this.userId = client.auth.sub this.store = client.options.store + this.token = client.options.auth.token this.ws = this.store.state.app.ws this.connect() } @@ -10,12 +10,15 @@ class _WebSocket { let url = process.env.TYMLY_API_BASE_URL.replace(/^http/, 'ws') if (url[url.length - 1] !== '/') url += '/' + const headers = { + authorization: 'Bearer ' + this.token + } + const params = [ - `userId=${encodeURIComponent(this.userId)}` + `headers=${JSON.stringify(headers)}` ] - url += `?${params.join('&')}` - return url + return url + `?${params.join('&')}` } subscribe (subscriptions) { @@ -31,7 +34,8 @@ class _WebSocket { connect () { if (this.ws) return - console.log('Opening websocket connection', this.url) + console.log('Opening websocket connection') + this.ws = new WebSocket(this.url) this.ws.onopen = event => { this.ws.onmessage = msg => { From 6ca2464d710f75b7bdd0ac0f9e90ad9cd6ac3183 Mon Sep 17 00:00:00 2001 From: meenahoda Date: Wed, 9 Mar 2022 10:01:39 +0000 Subject: [PATCH 7/7] fix: different token format for ws url --- lib/WebSocket.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/WebSocket.js b/lib/WebSocket.js index 4f55f954..1ced5fd8 100644 --- a/lib/WebSocket.js +++ b/lib/WebSocket.js @@ -10,12 +10,8 @@ class _WebSocket { let url = process.env.TYMLY_API_BASE_URL.replace(/^http/, 'ws') if (url[url.length - 1] !== '/') url += '/' - const headers = { - authorization: 'Bearer ' + this.token - } - const params = [ - `headers=${JSON.stringify(headers)}` + `token=Bearer ${this.token}` ] return url + `?${params.join('&')}`