open-nomad/ui/app/services/sockets.js
Buck Doyle 89136cbf6a Add massaged results of class codemod
Manual interventions:
• decorators on the same line for service and controller
  injections and most computed property macros
• preserving import order when possible, both per-line
  and intra-line
• moving new imports to the bottom
• removal of classic decorator for trivial cases
• conversion of init to constructor when appropriate
2020-06-10 16:18:42 -05:00

39 lines
1.3 KiB
JavaScript

import Service from '@ember/service';
import config from 'nomad-ui/config/environment';
import { getOwner } from '@ember/application';
export default class SocketsService extends Service {
getTaskStateSocket(taskState, command) {
const mirageEnabled =
config.environment !== 'production' &&
config['ember-cli-mirage'] &&
config['ember-cli-mirage'].enabled !== false;
if (mirageEnabled) {
return new Object({
messageDisplayed: false,
send(e) {
if (!this.messageDisplayed) {
this.messageDisplayed = true;
this.onmessage({ data: `{"stdout":{"data":"${btoa('unsupported in Mirage\n\r')}"}}` });
} else {
this.onmessage({ data: e.replace('stdin', 'stdout') });
}
},
});
} else {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const applicationAdapter = getOwner(this).lookup('adapter:application');
const prefix = `${applicationAdapter.host ||
window.location.host}/${applicationAdapter.urlPrefix()}`;
return new WebSocket(
`${protocol}//${prefix}/client/allocation/${taskState.allocation.id}` +
`/exec?task=${taskState.name}&tty=true&ws_handshake=true` +
`&command=${encodeURIComponent(`["${command}"]`)}`
);
}
}
}