<commit_before>/**
 * NOTE:
 * the leap Y component is vertical : stratum Y is horizontal
 * the leap Z component is horizontal : stratum Z is vertical
 * SEE: https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Coordinate_Mapping.html
 */

'use strict'

const path = require('path')
const { args, paths, log } = require(path.join(__dirname, '..', 'main.config.js'))

const MockHand = require(path.join(paths.utils, 'leap-mockhand'))
const leapjs = require('leapjs')
const controller = new leapjs.Controller()

if (args.leap) {
  controller.on('connect', () => log.info('Successfully connected.'))
  controller.on('streamingStarted', () => log.info('A Leap device has been connected.'))
  controller.on('streamingStopped', () => log.info('A Leap device has been disconnected.'))

  controller.connect()
}

function getEnsuredHandProperty (key, frame) {
  frame = frame || controller.frame(0)
  if (frame.hands[0]) return frame.hands[0][key]
}

const mockHand = new MockHand([0.5, 0.5, 0.5])

module.exports = {
  controller,
  timeVisible: () => getEnsuredHandProperty('timeVisible'),
  hand (box = [1, 1, 1]) {
    const frame = controller.frame(0)
    const interactionBox = frame.interactionBox
    const handPosition = getEnsuredHandProperty('palmPosition', frame)
    if (interactionBox && handPosition) {
      const normalizedPosition = interactionBox.normalizePoint(handPosition, true)
      mockHand.normalizedPosition = normalizedPosition
      return {
        x: normalizedPosition[0] * box[0],
        y: normalizedPosition[2] * box[1],
        z: normalizedPosition[1] * box[2]
      }
    }

    return mockHand.update(box)
  },
}
<commit_msg>[leap] Catch malformed packet<commit_after>/**
 * NOTE:
 * the leap Y component is vertical : stratum Y is horizontal
 * the leap Z component is horizontal : stratum Z is vertical
 * SEE: https://developer.leapmotion.com/documentation/csharp/devguide/Leap_Coordinate_Mapping.html
 */

'use strict'

const path = require('path')
const { args, paths, log } = require(path.join(__dirname, '..', 'main.config.js'))

const MockHand = require(path.join(paths.utils, 'leap-mockhand'))
const leapjs = require('leapjs')
const controller = new leapjs.Controller()

if (args.leap) {
  controller.on('streamingStarted', () => log.info('A Leap device has been connected.'))
  controller.on('streamingStopped', () => log.info('A Leap device has been disconnected.'))
  controller.on('connect', function () {
    log.info('Successfully connected.')
    // NOTE: this block fixes the lame leapmotion's handling of malformed packets
    // https://github.com/leapmotion/leapjs/issues/219
    const originalHandleData = this.connection.handleData
    this.connection.handleData = function (data) {
      try {
        return originalHandleData.call(this, data)
      } catch (e) {
        log.warning(e)
      }
    }
  })
  controller.connect()
}

function getEnsuredHandProperty (key, frame) {
  frame = frame || controller.frame(0)
  if (frame.hands[0]) return frame.hands[0][key]
}

const mockHand = new MockHand([0.5, 0.5, 0.5])

module.exports = {
  controller,
  timeVisible: () => getEnsuredHandProperty('timeVisible'),
  hand (box = [1, 1, 1]) {
    const frame = controller.frame(0)
    const interactionBox = frame.interactionBox
    const handPosition = getEnsuredHandProperty('palmPosition', frame)
    if (interactionBox && handPosition) {
      const normalizedPosition = interactionBox.normalizePoint(handPosition, true)
      mockHand.normalizedPosition = normalizedPosition
      return {
        x: normalizedPosition[0] * box[0],
        y: normalizedPosition[2] * box[1],
        z: normalizedPosition[1] * box[2]
      }
    }
    return mockHand.update(box)
  }
}

{'fraction_non_alphanumeric': 0.13572001068661502, 'fraction_numerical': 0.010953780390061448, 'mean_word_length': 3.9393139841688654, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4955473', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1143, 'n_words': 275}