       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, require("webpage"))
+    bootstrap(stream, phantom, require, window)
 
-    # Indicate to node that the phantomjs process is ready.
+    # 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

          performWrite()
  return stream

if module is require.main
  stream = create {logger: console}, (stream, webpage) ->

    # Right now, just echo back any data received to show that this is working.
    # TODO: use dnode?

      return value
  return "(#{func})(JSON.parse(#{JSON.stringify(optionsString)}))"

open = (options, bootstrap) ->
  options or= {}
  if typeof options is "function" and not bootstrap
    bootstrap = options
    options = {}
  logger = options.logger or {}
  # TODO: just find an open port instead
  phantomServerPort = PHANTOMJS_SERVER_PORT
  stdoutDataPrefix = "DATA#{Math.random().toString().slice(2)}:"
  phantomOptions =
    port: phantomServerPort
    stdoutDataPrefix: stdoutDataPrefix
    bootstrap: bootstrap

  # Build a tempfile that contains our phantomjs script.
  temp.open {prefix: "phantomstream", suffix: ".js"}, (err, info) ->
