chalk#yellow TypeScript Examples

The following examples show how to use chalk#yellow. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: reportPreviewer.ts    From cli with Apache License 2.0 6 votes vote down vote up
private getMissingEntriesHandler(): (
    this: SnapshotReporter
  ) => void | Promise<void> {
    const missingVaultEntryPrinter = (entries: string[]) =>
      ReportViewer.printAbridgedMessages(
        entries,
        ReportViewer.entryPlurable,
        yellow
      );
    return function (this: SnapshotReporter) {
      const entries = Array.from(this.missingVaultEntries).map(
        ({vaultEntryId}) => vaultEntryId
      );
      CliUx.ux.log(
        yellow(
          `Missing vault ${pluralizeIfNeeded(
            ReportViewer.entryPlurable,
            entries.length
          )} in destination organization:`
        )
      );
      missingVaultEntryPrinter(entries);
    };
  }
Example #2
Source File: log.ts    From yfm-transform with MIT License 6 votes vote down vote up
function createLogger(type: LogLevels) {
    const formatter: Record<string, (v: string) => string> = {
        [LogLevels.INFO]: (msg) => `${green('INFO')} ${msg}`,
        [LogLevels.WARN]: (msg) => `${yellow('WARN')} ${msg}`,
        [LogLevels.ERROR]: (msg) => `${red('ERR ')} ${msg}`,
    };

    return function log(msg: string) {
        const problem = formatter[type](msg);

        if (!problems[type].includes(problem)) {
            problems[type].push(problem);
        }
    };
}
Example #3
Source File: logger.ts    From anchorcli with Apache License 2.0 5 votes vote down vote up
export function warn(text: string) {
  console.log(bold(white(bgYellow(' WARNING '))) + ' ' + yellow(text));
}
Example #4
Source File: handshake.ts    From hoprnet with GNU General Public License v3.0 5 votes vote down vote up
/**
   * Tries to establish a relayed connection to the given destination
   * @param relay relay to use
   * @param destination destination to connect to trough relay
   * @returns a relayed connection to `destination`
   */
  async initiate(relay: PeerId, destination: PeerId): Promise<Response> {
    this.shaker.write(destination.pubKey.marshal())

    let chunk: StreamType | undefined
    try {
      chunk = await this.shaker.read()
    } catch (err: any) {
      error(`Error while reading answer from ${green(relay.toB58String())}.`, err.message)
    }

    if (chunk == null || chunk.length == 0) {
      verbose(`Received empty message. Discarding`)
      this.shaker.rest()
      return {
        success: false,
        code: 'FAIL'
      }
    }

    const answer = chunk.slice(0, 1)[0]

    this.shaker.rest()

    // Anything can happen
    switch (answer as RelayHandshakeMessage) {
      case RelayHandshakeMessage.OK:
        log(
          `Successfully established outbound relayed connection with ${green(
            destination.toB58String()
          )} over relay ${green(relay.toB58String())}`
        )
        return {
          success: true,
          stream: this.shaker.stream
        }
      default:
        error(
          `Could not establish relayed connection to ${green(destination.toB58String())} over relay ${green(
            relay.toB58String()
          )}. Answer was: <${yellow(handshakeMessageToString(answer))}>`
        )

        return {
          success: false,
          code: 'FAIL'
        }
    }
  }
Example #5
Source File: handshake.ts    From hoprnet with GNU General Public License v3.0 4 votes vote down vote up
/**
   * Negotiates between initiator and destination whether they can establish
   * a relayed connection.
   * @param source peerId of the initiator
   * @param getStreamToCounterparty used to connect to counterparty
   * @param exists to check if relay state exists
   * @param isActive to check if existing relay state can be used
   * @param updateExisting to update existing connection with new stream if not active
   * @param createNew to establish a whole-new instance
   */
  async negotiate(
    source: PeerId,
    getStreamToCounterparty: InstanceType<typeof Relay>['dialNodeDirectly'],
    state: Pick<RelayState, 'exists' | 'isActive' | 'updateExisting' | 'createNew'>,
    __relayFreeTimeout?: number
  ): Promise<void> {
    log(`handling relay request`)

    let chunk: StreamType | undefined

    try {
      chunk = await this.shaker.read()
    } catch (err) {
      error(err)
    }

    if (chunk == null || chunk.length == 0) {
      this.shaker.write(Uint8Array.of(RelayHandshakeMessage.FAIL_INVALID_PUBLIC_KEY))
      this.shaker.rest()
      error(`Received empty message from peer ${yellow(source)}. Ending stream because unable to identify counterparty`)
      return
    }

    let destination: PeerId | undefined

    try {
      destination = pubKeyToPeerId(chunk.slice())
    } catch (err) {
      error(err)
    }

    if (destination == null) {
      error(`Cannot decode public key of destination.`)
      this.shaker.write(Uint8Array.of(RelayHandshakeMessage.FAIL_INVALID_PUBLIC_KEY))
      this.shaker.rest()
      return
    }

    log(`counterparty identified as ${destination.toB58String()}`)

    if (source.equals(destination)) {
      error(`Peer ${source.toB58String()} is trying to loopback to itself. Dropping connection.`)
      this.shaker.write(Uint8Array.of(RelayHandshakeMessage.FAIL_LOOPBACKS_ARE_NOT_ALLOWED))
      this.shaker.rest()
      return
    }

    const relayedConnectionExists = state.exists(source, destination)

    if (relayedConnectionExists) {
      // Relay could exist but connection is dead
      const connectionIsActive = await state.isActive(source, destination)

      if (connectionIsActive) {
        this.shaker.write(Uint8Array.of(RelayHandshakeMessage.OK))
        this.shaker.rest()

        state.updateExisting(source, destination, this.shaker.stream)

        return
      }
    }

    let toDestinationStruct: Awaited<ReturnType<typeof getStreamToCounterparty>>
    try {
      toDestinationStruct = await getStreamToCounterparty(destination, DELIVERY_PROTOCOL(this.options.environment))
    } catch (err) {
      error(err)
    }

    // Anything can happen while attempting to connect
    if (toDestinationStruct == null) {
      error(
        `Failed to create circuit from ${source.toB58String()} to ${destination.toB58String()} because destination is not reachable`
      )
      this.shaker.write(Uint8Array.of(RelayHandshakeMessage.FAIL_COULD_NOT_REACH_COUNTERPARTY))
      this.shaker.rest()
      return
    }

    const destinationShaker = handshake(toDestinationStruct.stream as Stream)

    destinationShaker.write(source.pubKey.marshal())

    let destinationChunk: StreamType | undefined

    try {
      destinationChunk = await destinationShaker.read()
    } catch (err) {
      error(err)
    }

    if (destinationChunk == null || destinationChunk.length == 0) {
      this.shaker.write(Uint8Array.of(RelayHandshakeMessage.FAIL_COULD_NOT_REACH_COUNTERPARTY))
      this.shaker.rest()

      destinationShaker.rest()
      try {
        await toDestinationStruct.conn.close()
      } catch (err) {
        error(`Error while closing connection to destination ${destination.toB58String()}.`, err)
      }
      return
    }

    const destinationAnswer = destinationChunk.slice(0, 1)[0]

    switch (destinationAnswer as RelayHandshakeMessage) {
      case RelayHandshakeMessage.OK:
        this.shaker.write(Uint8Array.of(RelayHandshakeMessage.OK))
        this.shaker.rest()
        destinationShaker.rest()

        try {
          // NOTE: This returns only when the relay connection is terminated
          await state.createNew(
            source,
            destination,
            this.shaker.stream,
            destinationShaker.stream,
            this.options.relayFreeTimeout
          )
        } catch (err) {
          error(
            `Cannot establish relayed connection between ${destination.toB58String()} and ${source.toB58String()}`,
            err
          )
          // @TODO find a way how to forward the error to source and destination
          return
        }
        break
      default:
        this.shaker.write(Uint8Array.of(RelayHandshakeMessage.FAIL_COULD_NOT_REACH_COUNTERPARTY))
        this.shaker.rest()

        destinationShaker.rest()
        return
    }
  }