-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
141 lines (122 loc) · 3.44 KB
/
index.js
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var fs = require('fs')
var path = require('path')
var http = require('http')
var util = require('util')
var childProcess = require('child_process')
var EventEmitter = require('events').EventEmitter
var async = require('async')
var temp = require('temp')
var phantomjs = require('phantomjs-prebuilt')
var WebSocket = require('faye-websocket')
var noop = function () {}
// PhantomJS script template.
var template = async.memoize(function (path, cb) {
fs.readFile(path, 'utf-8', cb)
}).bind(null, path.join(__dirname, 'tmpl', 'script.js'))
// A NoFace is a handle to a PhantomJS child process.
function NoFace () {
this.child = null
this.channel = null
}
util.inherits(NoFace, EventEmitter)
// Send a message to PhantomJS.
NoFace.prototype.send = function (message) {
if (!this.channel) {
throw new Error('Channel not established')
}
this.channel.send(message)
}
// Close the connection to PhantomJS.
NoFace.prototype.close = function (message) {
if (!this.channel) {
throw new Error('Channel not established')
}
this.channel.close()
this.channel = null
}
// Create a NoFace. Takes a function to execute in PhantomJS, and
// optionally any extra options to `child_process.spawn`.
module.exports = function (src, options) {
if (typeof src === 'function') {
src = src.toString()
}
if (!src.match(/^\s*function/)) {
src = 'function (channel) {' + src + '}'
}
// Create a handle object.
var ph = new NoFace()
// Name of the temporary file containing the PhantomJS script.
var tmpFile = temp.path({
prefix: 'node-noface-',
suffix: '.js'
})
// Build phantomjs arguments.
var args = (options && options.args) || []
args = args.concat(tmpFile)
// Temporary HTTP server used to establish the channel.
var server = http.createServer(function (req, res) {
res.writeHead(400)
res.end()
})
async.series([
function (cb) {
// Grab a random port, start listening.
server.listen(0, 'localhost', cb)
},
function (cb) {
// Write a script based on the template.
template(function (err, script) {
if (err) return cb(err)
script = script
.replace('PORT', server.address().port)
.replace('SRC', src)
fs.writeFile(tmpFile, script, cb)
})
},
function (cb) {
// Wait for a WebSocket connection.
server.on('upgrade', function (req, sock, head) {
// Callback only once.
if (!cb) return
ph.channel = new WebSocket(req, sock, head)
ph.channel.onmessage = function (event) {
ph.emit('message', event.data)
}
ph.channel.onclose = function () {
ph.channel = null
ph.emit('close')
}
cb()
cb = null
})
// Spawn PhantomJS.
ph.child = childProcess.spawn(phantomjs.path, args, options)
ph.child.on('exit', function (code) {
if (cb) {
cb(new Error('PhantomJS startup failed, code ' + code))
cb = null
} else if (code !== 0) {
ph.emit('error', new Error(
'PhantomJS exited with code ' + code
))
}
})
}
], function (err) {
// No longer need the HTTP server.
if (server) {
server.close()
server = null
}
// Clean up the temporary script file.
fs.unlink(tmpFile, noop)
// Emit result.
if (err) {
ph.emit('error', err)
} else {
ph.emit('open')
}
})
// Return the handle.
return ph
}