<commit_before>fs = require("fs")
path = require("path")
http = require("http")
spawn = require("child_process").spawn
EventEmitter = require("events").EventEmitter
temp = require("temp")
platform = require("./platform")

PHANTOMJS_PLATFORM_PATHS =
  macosx: path.resolve(__dirname, "../ext/macosx/bin/phantomjs")
  windows: path.resolve(__dirname, "../ext/windows/phantomjs.exe")
  linux32: path.resolve(__dirname, "../ext/linux32/bin/phantomjs")
  linux64: path.resolve(__dirname, "../ext/linux64/bin/phantomjs")
PHANTOMJS_PATH = PHANTOMJS_PLATFORM_PATHS[platform.get()]
PHANTOMJS_SERVER_PORT = 9999

getPhantomScript = (options) ->
  func = (options) ->

    # Create a stream that uses stdout for outbound data, and the builtin
    # phantomjs webserver for inbound data.
    listeners = {}
    emit = (event) ->
      args = Array.prototype.slice.call(arguments, 1)
      (listeners[event] or []).forEach (listener) ->
        listener.apply(listener, args)
    stream =
      write: (data) ->
        console.info("#{options.stdoutDataPrefix}#{JSON.stringify(data)}")
      on: (event, callback) ->
        (listeners[event] or= []).push(callback)
    server = require("webserver").create()
    server.listen options.port, (req, res) ->
      emit("data", req.post)
      res.writeHead(200, {"Content-Length": 0})
      res.write("")
      res.close()

    # Pass the stream to the bootstrap script.
    # TODO: shadow all globals to force explicit use of window object
    bootstrap = new Function("(#{options.bootstrap}).apply({}, arguments)")
    bootstrap(stream, phantom, require, window)

    # Indicate to node that the phantomjs process is ready by writing
    # some data to it.
    stream.write("READY")

  # Serialize the options for `func`, which will be stringified and opened<commit_msg>make sure that listeners run in their own execution context<commit_after>fs = require("fs")
path = require("path")
http = require("http")
spawn = require("child_process").spawn
EventEmitter = require("events").EventEmitter
temp = require("temp")
platform = require("./platform")

PHANTOMJS_PLATFORM_PATHS =
  macosx: path.resolve(__dirname, "../ext/macosx/bin/phantomjs")
  windows: path.resolve(__dirname, "../ext/windows/phantomjs.exe")
  linux32: path.resolve(__dirname, "../ext/linux32/bin/phantomjs")
  linux64: path.resolve(__dirname, "../ext/linux64/bin/phantomjs")
PHANTOMJS_PATH = PHANTOMJS_PLATFORM_PATHS[platform.get()]
PHANTOMJS_SERVER_PORT = 9999

getPhantomScript = (options) ->
  func = (options) ->

    # Create a stream that uses stdout for outbound data, and the builtin
    # phantomjs webserver for inbound data.
    listeners = {}
    emit = (event) ->
      args = Array.prototype.slice.call(arguments, 1)
      (listeners[event] or []).forEach (listener) ->
        setTimeout ->
          listener.apply(listener, args)
    stream =
      write: (data) ->
        console.info("#{options.stdoutDataPrefix}#{JSON.stringify(data)}")
      on: (event, callback) ->
        (listeners[event] or= []).push(callback)
    server = require("webserver").create()
    server.listen options.port, (req, res) ->
      emit("data", req.post)
      res.writeHead(200, {"Content-Length": 0})
      res.write("")
      res.close()

    # Pass the stream to the bootstrap script.
    # TODO: shadow all globals to force explicit use of window object
    bootstrap = new Function("(#{options.bootstrap}).apply({}, arguments)")
    bootstrap(stream, phantom, require, window)

    # Indicate to node that the phantomjs process is ready by writing
    # some data to it.
    stream.write("READY")

  # Serialize the options for `func`, which will be stringified and opened
{'fraction_non_alphanumeric': 0.14223669923995658, 'fraction_numerical': 0.009229098805646038, 'mean_word_length': 3.694267515923567, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 18, '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': '3695099', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1106, 'n_words': 289}