async run()

in src/task/ReceiveTURNCredentialsTask.ts [39:116]


  async run(): Promise<void> {
    if (this.context.turnCredentials) {
      this.context.logger.info('TURN credentials available, skipping credentials fetch');
      return;
    }

    this.context.logger.error('missing TURN credentials - falling back to fetch');

    if (!this.url) {
      this.context.logger.info('TURN control url not supplied, skipping credentials fetch');
      return;
    }

    const options: RequestInit = {
      method: 'POST',
      mode: 'cors',
      cache: 'no-cache',
      credentials: 'omit',
      headers: {
        'Content-Type': 'application/json',
        'X-Chime-Auth-Token': '_aws_wt_session=' + new DefaultModality(this.joinToken).base(),
      },
      redirect: 'follow',
      referrer: 'no-referrer',
      body: JSON.stringify({ meetingId: this.meetingId }),
    };

    this.context.logger.info(`requesting TURN credentials from ${this.url}`);

    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const responseBodyJson = await new Promise<any>(async (resolve, reject) => {
      this.cancelPromise = (error: Error) => {
        reject(error);
      };

      try {
        const responseBody = await fetch(Versioning.urlWithVersion(this.url), options);
        this.context.logger.info(`received TURN credentials`);
        if (responseBody.status && responseBody.status === 403) {
          reject(
            new Error(
              `canceling ${this.name()} due to the meeting status code: ${
                MeetingSessionStatusCode.TURNCredentialsForbidden
              }`
            )
          );
          return;
        }
        if (responseBody.status && responseBody.status === 404) {
          reject(
            new Error(
              `canceling ${this.name()} due to the meeting status code: ${
                MeetingSessionStatusCode.MeetingEnded
              }`
            )
          );
          return;
        }
        resolve(await responseBody.json());
      } catch (error) {
        reject(error);
      } finally {
        delete this.cancelPromise;
      }
    });

    this.context.turnCredentials = new MeetingSessionTURNCredentials();
    this.context.turnCredentials.password = responseBodyJson.password;
    this.context.turnCredentials.ttl = responseBodyJson.ttl;
    this.context.turnCredentials.uris = responseBodyJson.uris
      .map((uri: string): string => {
        return this.context.meetingSessionConfiguration.urls.urlRewriter(uri);
      })
      .filter((uri: string) => {
        return !!uri;
      });
    this.context.turnCredentials.username = responseBodyJson.username;
  }