-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
193 lines (141 loc) · 5.02 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
var inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter;
var path = require("path");
var childProcess = require("child_process");
var phantomjs = require("phantomjs-prebuilt");
var restify = require("restify-clients");
var fs = require("fs");
var async = require("async");
var scriptPath = path.join(__dirname, "script.js"),
chartJsPath = path.resolve(path.dirname(require.resolve("chart.js")), "../dist/Chart.bundle.min.js"),
binPath = phantomjs.path;
function ChartRenderer(options) {
EventEmitter.call(this);
if (!options) {
options = {};
}
this.logger = options.logger;
this.url = options.url;
this.port = options.port || 8083;
// the PhantomJS server is single threaded and rendering (in our case) seems to happening synchronously. so let's queue up requests
// here instead of on the server. the server seems to perform better this way and let's us keep track of any outstanding requests.
var self = this;
this._queue = async.queue(function(request, done) { self._request.post("/", request, done) }, 1);
}
inherits(ChartRenderer, EventEmitter);
ChartRenderer.prototype.renderBase64 = function (config, callback) {
if (config == null) {
throw new Error("Missing required argument 'config'.");
}
if (callback == null) {
throw new Error("Missing required argument 'callback'.");
}
if (this._closing || this._closed) {
return callback(new Error("Renderer is closed."));
}
if (config.chart == null) {
return callback(new Error("Missing chart configuration."));
}
if (config.type && (config.type != "PNG" && config.type != "JPEG" && config.type != "GIF")) {
return callback(new Error("Unsupported image type '" + config.type + "'. Supported types are PNG, GIF, and JPEG."));
}
this._queue.push(safeStringify(config), function(err, req, res) {
if (err) return callback(err);
callback(null, res.body);
});
};
ChartRenderer.prototype.renderBuffer = function (config, callback) {
this.renderBase64(config, function (err, data) {
if (err) return callback(err);
callback(null, new Buffer(data, "base64"));
});
};
ChartRenderer.prototype.close = function (callback) {
if(callback) {
if(this._closed) {
callback();
return;
}
this.on("closed", callback);
}
if (this._closing) return;
this._closing = true;
this.emit("closing");
if (!this._process) {
this._closed = true;
this.emit("closed");
return;
}
var self = this;
this._process.on("exit", function () {
self._closed = true;
self.emit("closed");
});
this._process.kill("SIGTERM");
};
ChartRenderer.prototype.open = function (callback) {
if (this._opening || this._open) {
return callback(new Error("Renderer is already open."));
}
this._opening = true;
// create a HTTP client to connect to the server
this._request = restify.createStringClient({
url: this.url || ("http://localhost:" + this.port),
contentType: "application/json"
});
// start up the PhantomJS server if url is not specified
if (this.url) {
callback(null, this);
return;
}
this._startPhantom(callback);
};
ChartRenderer.prototype._startPhantom = function (callback) {
var child = childProcess.spawn(binPath, [scriptPath, this.port, chartJsPath]),
self = this;
var stdout = "";
child.stdout.on('data', function (data) {
if (data) {
var text = data.toString();
// while we are opening, track stdout and watch for text indicating the server started ok
if (self._opening) {
stdout += text;
if (stdout.indexOf("listening") != -1) {
self._opening = false;
callback(null, self);
}
else if (stdout.indexOf("unable to listen") != -1) {
self._opening = false;
callback(new Error("Unable to start PhantomJS server on port " + self.port));
}
}
// send output to bunyan logger if we have one
if (self.logger) {
self.logger.trace(text);
}
}
});
child.on("exit", function () {
// if we are not in the process of closing then this is an error
if (!self._closing) {
// flag process as closed even if we did not initiate it
self._closed = true;
self.emit("error", new Error("PhantomJS server exited unexpectedly."));
}
});
this._process = child;
};
function createChartRenderer (options, callback) {
var renderer = new ChartRenderer(options);
renderer.open(callback);
}
function safeStringify(value) {
if (value == null) return null;
try {
return JSON.stringify(value);
}
catch (e) {
return null;
}
}
exports.createChartRenderer = createChartRenderer;