forked from amir20/phantomjs-node
/
phantom.coffee
99 lines (79 loc) · 3.13 KB
/
phantom.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
dnode = require 'dnode'
http = require 'http'
shoe = require 'shoe'
spawn = require 'win-spawn'
# the list of phantomjs RPC wrapper
phanta = []
# @Description: starts and returns a child process running phantomjs
# @param: port:int
# @args: args:object
# @return: ps:object
startPhantomProcess = (binary, port, args) ->
spawn binary, args.concat [__dirname+'/shim.js', port]
# @Description: kills off all phantom processes within spawned by this parent process when it is exits
onSignal = ->
phantom.exit() for phantom in phanta
process.exit()
process.on 'exit', ->
phantom.exit() for phantom in phanta
process.on 'SIGINT', onSignal
process.on 'SIGTERM', onSignal
# @Description: We need this because dnode does magic clever stuff with functions, but we want the function to make it intact to phantom
wrap = (ph) ->
ph._createPage = ph.createPage
ph.createPage = (cb) ->
ph._createPage (page) ->
page._evaluate = page.evaluate
page.evaluate = (fn, cb, args...) -> page._evaluate.apply(page, [fn.toString(), cb].concat(args))
page._onResourceRequested = page.onResourceRequested
page.onResourceRequested = (fn, cb) -> page._onResourceRequested.apply(page, [fn.toString(), cb])
cb page
module.exports =
create: ->
args = []
options = {}
for arg in arguments
switch typeof arg
when 'function' then cb = arg
when 'string' then args.push arg
when 'object' then options = arg
options.binary ?= 'phantomjs'
options.port ?= 0
options.dnodeOpts ?= {}
phantom = null
httpServer = http.createServer()
httpServer.listen options.port
httpServer.on 'listening', () ->
port = httpServer.address().port
ps = startPhantomProcess options.binary, port, args
ps.stdout.on 'data', options.onStdout || (data) -> console.log "phantom stdout: #{data}"
ps.stderr.on 'data', options.onStderr || (data) -> module.exports.stderrHandler(data.toString('utf8'))
ps.on 'error', (err) ->
if err?.code is 'ENOENT'
console.error "phantomjs-node: You don't have 'phantomjs' installed"
else
throw err
# @Description: when the background phantomjs child process exits or crashes
# removes the current dNode phantomjs RPC wrapper from the list of phantomjs RPC wrapper
ps.on 'exit', (code, signal) ->
httpServer.close()
if phantom
phantom.onExit?()
phanta = (p for p in phanta when p isnt phantom)
if options.onExit
options.onExit code, signal
else
console.assert not signal?, "signal killed phantomjs: #{signal}"
console.assert code is 0, "abnormal phantomjs exit code: #{code}"
sock = shoe (stream) ->
d = dnode({}, options.dnodeOpts)
d.on 'remote', (phantom) ->
wrap phantom
phanta.push phantom
cb? phantom
d.pipe stream
stream.pipe d
sock.install httpServer, '/dnode'
stderrHandler: (message) ->
return if message.match /(No such method.*socketSentData)|(CoreText performance note)/ #Stupid, stupid QTWebKit
console.warn "phantom stderr: #{message}"