<commit_before>'use strict'

var util = require('util')
var stream = require('readable-stream')
var EventEmitter = require('events').EventEmitter
var uuid = require('node-uuid')
var SequenceStream = require('./sequence-stream')
var debug = require('./debug')

var uris = {} // TODO: Figure out the right connection between URI's and sessions ;)
var pool = {}

// Reverse lookup session based on the URI. Useful when dealing with iOS, which
// doesn't seem to use the session headers.
var sessionMap = {}

var emitter = module.exports = new EventEmitter()

emitter.uri = function (uri, val) {
  if (!(uri in uris)) uris[uri] = val || {}
  else if (val) uris[uri] = val
  return uris[uri]
}

emitter.init = function (req) {
  var uri = req.uri
  var id = uuid.v4()
  var session = pool[id] = new Session(id)
  session.uris[uri] = uris[uri]
  sessionMap[uri] = id
  debug('created new session', id)
  this.emit('new', session)
  return session
}

emitter.get = function (req) {
  var uri = req.uri
  var id = req.headers['session'] || sessionMap[uri]
  if (!id) return null
  var session = pool[id]
  if (!session) return null
  if (!(uri in session.uris)) session.uris[uri] = uris[uri]
  return session
}

emitter.end = function (req) {
  var id = req.headers['session']
  debug('clearing session', id)
  delete pool[id]
}

var Session = function (id, opts) {
  if (!(this instanceof Session)) return new Session(id, opts)

  stream.PassThrough.call(this, opts)

  this.id = id<commit_msg>Clean up sessions module<commit_after>'use strict'

var util = require('util')
var stream = require('readable-stream')
var EventEmitter = require('events').EventEmitter
var uuid = require('node-uuid')
var SequenceStream = require('./sequence-stream')
var debug = require('./debug')

var uris = {} // TODO: Figure out the right connection between URI's and sessions ;)
var pool = {}

// Reverse lookup session based on the URI. Useful when dealing with iOS, which
// doesn't seem to use the session headers.
var sessionMap = {}

var sessions = module.exports = new EventEmitter()

sessions.uri = function (uri, val) {
  if (!(uri in uris)) uris[uri] = val || {}
  else if (val) uris[uri] = val
  return uris[uri]
}

sessions.init = function (req) {
  var uri = req.uri
  var id = uuid.v4()
  var session = pool[id] = new Session(id)
  session.uris[uri] = uris[uri]
  sessionMap[uri] = id
  debug('created new session', id)
  this.emit('new', session)
  return session
}

sessions.get = function (req) {
  var uri = req.uri
  var id = req.headers['session'] || sessionMap[uri]
  if (!id) return null
  var session = pool[id]
  if (!session) return null
  if (!(uri in session.uris)) session.uris[uri] = uris[uri]
  return session
}

sessions.end = function (req) {
  var id = req.headers['session']
  debug('clearing session', id)
  delete pool[id]
}

var Session = function (id, opts) {
  if (!(this instanceof Session)) return new Session(id, opts)

  stream.PassThrough.call(this, opts)

  this.id = id
{'fraction_non_alphanumeric': 0.14247491638795987, 'fraction_numerical': 0.0006688963210702341, 'mean_word_length': 3.9356435643564356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '3986794', 'n_tokens_mistral': 979, 'n_tokens_neox': 965, 'n_words': 324}