Skip to content

Commit

Permalink
feat(): adding WS traces
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucharz committed Dec 18, 2017
1 parent a076ec1 commit cde14fe
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 38 deletions.
4 changes: 0 additions & 4 deletions packages/cli/bin/syncano-cli

This file was deleted.

31 changes: 19 additions & 12 deletions packages/cli/src/commands/socket-trace.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import format from 'chalk'
import _ from 'lodash'
import stackTrace from 'stack-trace'
import WebSocket from 'ws'

import { GlobalSpinner, SimpleSpinner } from './helpers/spinner'
import logger from '../utils/debug'
Expand Down Expand Up @@ -54,23 +55,29 @@ export default class SocketTrace {
}

async startCollectingTraces (socket) {
debug('startCollectingTraces')
this.mainSpinner.start()

try {
const response = await socket.getTraces(this.lastId[socket.name])
const ws = socket.getTraces()
ws.on('error', (err) => {
debug('ws error', err)
this.mainSpinner.stop()
const trace = response.data
this.lastId[socket.name] = response.data.id
await this.printTrace(socket, trace)
ws.terminate()
error(err.message)
this.startCollectingTraces(socket)
} catch (err) {
})

ws.on('close', () => {
debug('ws closed, starting again')
this.startCollectingTraces(socket)
})

ws.on('message', async (data) => {
debug('ws message')
this.mainSpinner.stop()
if (err.code === 'ECONNABORTED') {
this.startCollectingTraces(socket)
} else {
error(4)(err.message)
}
}
await this.printTrace(socket, JSON.parse(data))
this.mainSpinner.start()
})
}

// Decide about how to print trace and which
Expand Down
23 changes: 9 additions & 14 deletions packages/cli/src/utils/sockets/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Promise from 'bluebird'
import template from 'es6-template-strings'
import _ from 'lodash'
import SourceMap from 'source-map'
import WebSocket from 'ws'

import logger from '../debug'
import session from '../session'
Expand Down Expand Up @@ -635,20 +636,14 @@ class Socket {
}

getTraces (lastId) {
const params = { room: `socket:${this.name}` }
if (lastId) {
params.last_id = lastId
}

return axios.request({
url: `https://${session.getHost()}/v2/instances/${session.project.instance}/channels/eventlog/poll/`,
method: 'GET',
timeout: 50000,
params,
headers: {
'X-Api-Key': session.settings.account.getAuthKey()
}
})
const url = [
`https://${session.getHost()}/v2/instances/${session.project.instance}/channels/eventlog/poll/`,
'?transport=websocket',
`&api_key=${session.settings.account.getAuthKey()}`,
`&room=${`socket:${this.name}`}`
].join('')

return new WebSocket(url)
}

static async getEndpointTraceByUrl (url) {
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/tests/e2e/cli.test-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,24 @@ describe('[E2E] CLI User', function () {
.unlink(syncanoYmlPath)
.end(done)
})

it('can logout from cli', function (done) {
nixt()
.cwd(testsLocation)
.run(`${cliLocation} logout`)
.stdout(/You have been logged out/)
.unlink(syncanoYmlPath)
.end(done)
})

it('can log in', function (done) {
nixt()
.cwd(testsLocation)
.run(`${cliLocation} login`)
.on(/Your e-mail/)
.respond(`${tempEmail}\n`)
.on(/Password/)
.respond(`${tempPass}\n`)
.end(done)
})
})
31 changes: 23 additions & 8 deletions packages/lib-js-client/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global URLSearchParams */
import upperFirst from 'lodash.upperfirst'
import camelCase from 'lodash.camelcase'
import querystring from 'querystring'
Expand Down Expand Up @@ -249,14 +250,28 @@ function SyncanoClient (instanceName = required('instanceName'), options = {}) {
}
]

return fetch({
method: 'POST',
url,
data,
headers,
transformRequest,
...options
}).then(response => response.data)
if (data._method === 'GET') {
const urlParams = new URLSearchParams(Object.entries(data))
return fetch({
method: 'GET',
url: url + '?' + urlParams,
qs: data,
// headers,
transformRequest,
...options
})
.then(response => response.data)
} else {
return fetch({
method: 'POST',
url,
data,
headers,
transformRequest,
...options
})
.then(response => response.data)
}
}
}

Expand Down

0 comments on commit cde14fe

Please sign in to comment.