github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/app/services/sockets.js (about)

     1  import Service from '@ember/service';
     2  import config from 'nomad-ui/config/environment';
     3  import { getOwner } from '@ember/application';
     4  import { inject as service } from '@ember/service';
     5  
     6  export default class SocketsService extends Service {
     7    @service system;
     8  
     9    getTaskStateSocket(taskState, command) {
    10      const mirageEnabled =
    11        config.environment !== 'production' &&
    12        config['ember-cli-mirage'] &&
    13        config['ember-cli-mirage'].enabled !== false;
    14  
    15      if (mirageEnabled) {
    16        return new Object({
    17          messageDisplayed: false,
    18  
    19          send(e) {
    20            if (!this.messageDisplayed) {
    21              this.messageDisplayed = true;
    22              this.onmessage({
    23                data: `{"stdout":{"data":"${btoa(
    24                  'unsupported in Mirage\n\r'
    25                )}"}}`,
    26              });
    27            } else {
    28              this.onmessage({ data: e.replace('stdin', 'stdout') });
    29            }
    30          },
    31        });
    32      } else {
    33        const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
    34        const applicationAdapter = getOwner(this).lookup('adapter:application');
    35        const prefix = `${
    36          applicationAdapter.host || window.location.host
    37        }/${applicationAdapter.urlPrefix()}`;
    38        const region = this.system.activeRegion;
    39  
    40        return new WebSocket(
    41          `${protocol}//${prefix}/client/allocation/${taskState.allocation.id}` +
    42            `/exec?task=${taskState.name}&tty=true&ws_handshake=true` +
    43            (region ? `&region=${region}` : '') +
    44            `&command=${encodeURIComponent(`["${command}"]`)}`
    45        );
    46      }
    47    }
    48  }