-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
208 lines (157 loc) · 4.75 KB
/
client.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
var fs = require('fs');
var path = require('path');
var uglify = require('uglify-js');
var _ = require('highland');
var debug = require('diagnostics')('bc:client');
var pageTitle = 'browser-console';
var pageHeader = [
'<!DOCTYPE html>',
'<html>',
'<head>',
' <title>' + pageTitle + '</title>',
'</head>',
'<body>',
'<script>'
].join('\n');
var pageFooter = [
'</script>',
'</body>',
'</html>'
].join('\n');
var connectCode = 'var primus = Primus.connect();';
function Client(config, primus) {
this._config = config.client || {};
this._primus = primus;
this._path = this._config.path || 'public';
this._useCache = !!this._config.useCache;
this._mainHtml = this._config.mainHtml || 'index.html';
this._cacheHtml = this._config.cacheHtml || 'cache.html';
this._mainHtmlPath = this.getFilePath(this._mainHtml);
this._cacheHtmlPath = this.getFilePath(this._cacheHtml);
this._clientJs = this._config.clientJs || 'client.js';
this._utilJs = this._config.utilJs || 'util.js';
this._cacheExists = false;
this._clientStream = null;
this._dynamicCode = [];
}
Client.prototype.getFilePath = function getFilePath(f) {
return path.join(__dirname, this._path, f);
};
Client.prototype.build = function build(callback) {
var that = this;
var readFile = _.wrapCallback(fs.readFile);
// None of these actions are actually executed
// until something comes along to 'thunk' it
this._clientStream = _(function (push) {
push(null, that._primus.library());
push(null, _.nil);
})
.concat(_(readFile(this.getFilePath(this._utilJs))))
.concat(_(function (push) {
// we create a stream here so that dynamic code could be modified later
var dynCode = that._dynamicCode;
dynCode = (!dynCode || !dynCode.length) ? [ connectCode ] : dynCode;
dynCode.forEach(function (c) {
push(null, c);
});
push(null, _.nil);
}))
.concat(_(readFile(this.getFilePath(this._clientJs))))
.reduce1(function (a, b) { return a.concat(b); })
.flatMap(this.uglifyStream)
.map(function (js) {
var head = that._config.pageTitle ?
pageHeader.replace(pageTitle, that._config.pageTitle) : pageHeader;
return head + js + pageFooter;
});
process.nextTick(function () {
callback(null, that);
});
};
Client.prototype.exportFile = function exportFile(tunnelUrl, callback) {
var that = this;
debug('Exporting client html.');
var useCache = !!this._config.useCache;
if (!useCache) {
debug('Cache disabled. Exporting to mainHtml.');
return this._export(this._mainHtmlPath, tunnelUrl, callback);
}
this.cacheExists(function (err, cacheExists) {
if (cacheExists) {
debug('Cache exists.');
return callback(err, that);
}
that._export(that._cacheHtmlPath, null, function (err) {
if (!err) {
debug('Cache replenished.');
}
return callback(err, that);
});
});
};
Client.prototype.cacheExists = function (callback) {
fs.stat(this._cacheHtmlPath, function (err, stats) {
return callback(err, (stats && stats.isFile()));
});
};
Client.prototype._copyCacheToMain = function (callback) {
callback = this._callbackOnce(callback);
var rd = fs.createReadStream(this._cacheHtmlPath);
rd.on('error', callback);
var wr = fs.createWriteStream(this._mainHtmlPath);
wr.on('error', callback);
wr.on('finish', callback);
rd.pipe(wr);
};
Client.prototype._callbackOnce = function (callback) {
var that = this;
return function (err) {
if (callback) {
var cb = callback;
callback = null;
cb(err, that);
}
};
};
Client.prototype._export = function _export(path, tunnelUrl, callback) {
if (!this._clientStream) {
return callback(new Error('client.build()?'));
}
var that = this;
var callbackOnce = this._callbackOnce(function (err, client) {
if (err) {
try {
fs.unlinkSync(path);
} catch (e) {
// best-effort cleanup :P
}
}
callback(err, client);
});
var destStream = fs.createWriteStream(path);
destStream.on('error', function (err) {
callbackOnce(err);
});
var connCode = !tunnelUrl ? connectCode : connectCode.replace('connect()', 'connect("' + tunnelUrl + '")');
this._dynamicCode.unshift(connCode);
this._clientStream
.errors(function (err, push) {
callbackOnce(err);
})
.pipe(destStream)
.on('finish', function () {
callbackOnce(null);
});
};
Client.prototype.uglifyStream = function uglifyStream(code) {
return _(function (push) {
var res = uglify.minify(code, { fromString: true });
if (!res || !res.code) {
return push(new Error('Failed to uglify client.'));
}
push(null, res.code);
push(null, _.nil);
});
};
module.exports = Client;