Skip to content

Commit

Permalink
serve the client js files.
Browse files Browse the repository at this point in the history
  • Loading branch information
prgsmall committed Nov 12, 2011
1 parent 08f9e26 commit 7351c5c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 37 deletions.
3 changes: 1 addition & 2 deletions examples/acequiaClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
<title>Acequia Client Test Page</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css" type="text/css" />

<script src='msg.js'></script>
<script src='acequiaClient.js'></script>
<script src='http://localhost:9091/acequia/acequia.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>

Expand Down
50 changes: 42 additions & 8 deletions lib/acequia.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@

// TODO: Listen with http server and serve up the acequia client js file.

// TODO: At startup compress the acequia client js file, using uglify.


// Imports and globals.
var http = require("http"),
url = require("url"),
net = require("net"),
io = require('socket.io'),
log4js = require('log4js-node'),
dgram = require("dgram"),
osc = require("./osc.js"),
ac = require("./client.js"),
msg = require("./msg.js"),
querystring = require("querystring"),
Buffer = require('buffer').Buffer;
Buffer = require('buffer').Buffer,
fs = require("fs"),
URL = require("url"),
genclient = require("./genclient");


// Generate the client code
genclient.generateClientCode();

var INTERNAL_IP = "0.0.0.0",
OSC_PORT = 9090,
Expand All @@ -27,7 +32,7 @@ var INTERNAL_IP = "0.0.0.0",
// The list of clients
var clients = new ac.AcequiaClients(TIMEOUT * 1000);

var oscServer, wsServer, tcpServer;
var oscServer, wsServer, tcpServer, httpServer;

var logger = log4js.getLogger("acequia");

Expand Down Expand Up @@ -99,7 +104,36 @@ function startServers() {
oscServer.bind(OSC_PORT, INTERNAL_IP);

// Websocket server:
wsServer = require('socket.io').listen(WS_PORT);

// TODO: Read into memory in genclient
var serveClientCode = function (res, min) {
var file, fd, stats;
min = min ? ".min" : "";
file = process.cwd() + "/lib/dist/acequia" + min + ".js";
stats = fs.statSync(file);
buffer = new Buffer(stats.size);

fd = fs.openSync(file, "r");
fs.readSync(fd, buffer, 0, stats.size, null);
fs.close(fd);

res.writeHead(200,{ 'Content-Type': 'text/javascript' });
res.end(buffer.toString("utf8"));
};

httpServer = http.createServer(function (req, res){
var pathName = URL.parse(req.url, true).pathname;
console.log("HTTP server received " + pathName);

if (pathName == "/acequia/acequia.js") {
serveClientCode(res, false);
} else if (pathName == "/acequia/acequia.min.js") {
serveClientCode(res, true);
}
});
httpServer.listen(WS_PORT);

wsServer = io.listen(httpServer);

wsServer.enable('browser client minification'); // send minified client
wsServer.enable('browser client etag'); // apply etag caching logic based on version number
Expand Down Expand Up @@ -164,7 +198,7 @@ function startServers() {
logger.debug("TCP: %s:%s connect", socket.remoteAddress, socket.remotePort);
});

socket.addListener("data", function(data) {
socket.addListener("data", function (data) {
var index = 0, msgs = [], size, message,
buffer = new Buffer(data);

Expand Down Expand Up @@ -231,7 +265,7 @@ function startServers() {
});
});

tcpServer.on("listening", function() {
tcpServer.on("listening", function () {
logger.debug("TCP Server is listening on %j", tcpServer.address());
});

Expand Down
65 changes: 45 additions & 20 deletions lib/genclient.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,74 @@
/*globals require exports process*/

var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var fs = require("fs");
var util = require("util");
var Buffer = require('buffer').Buffer;

var concatenateClientFiles = function (distDir) {
var buffers = [], uber, fd, stats, i, size = 0, file, files = ["msg.js", "client/acequiaClient.js"];
var fullCode = "";
var minifiedCode = "";

/**
* Reads the client-side files and concatenate them into a single file
* @returns {String} the concatenated file
*/
var concatenateClientFiles = function () {
var buffer, code = "", fd, stats, i, size = 0, file, files = ["msg.js", "client/acequiaClient.js"];

for (i in files) {
file = process.cwd() + "/lib/" + files[i];
stats = fs.statSync(file);
buffers.push(new Buffer(stats.size));
buffer = new Buffer(stats.size);

fd = fs.openSync(file, "r");
size += fs.readSync(fd, buffers[i], 0, stats.size, null);
size += fs.readSync(fd, buffer, 0, stats.size, null);
fs.close(fd);

code += buffer.toString("utf8");
}

uber = "";
for (i in buffers) {
uber += buffers[i].toString("utf8");
}
return code;
};

/**
* Write the code generated in concatenateClientFiles to two files:
* one as is, the other minified.
* @param {String} code The code to write out.
* @param {String} distDir The directories where the files should be written.
*/
var writeClientFiles = function (code, distDir) {
var fd, ast;

// Write the non-minified version
fd = fs.openSync(distDir + "acequia.js", "w");
fs.writeSync(fd, uber, 0);
fs.writeSync(fd, code, 0);
fs.close(fd);

return uber;
};

var minifyClientFiles = function (code, distDir) {
var ast = jsp.parse(code);
// Minify the code
ast = jsp.parse(code);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
var final_code = pro.gen_code(ast);
minifiedCode = pro.gen_code(ast);

// Write the minified version
fd = fs.openSync(distDir + "acequia.min.js", "w");
fs.writeSync(fd, final_code, 0);
fs.writeSync(fd, minifiedCode, 0);
fs.close(fd);
};

/**
* Generates the client code for the server to serve up.
*/
exports.generateClientCode = function () {
var distDir = process.cwd() + "/lib/dist/";
var distDir;

distDir = process.cwd() + "/lib/dist/";
fs.mkdir(distDir, 0777);

var code = concatenateClientFiles(distDir);
minifyClientFiles(code, distDir);
};
fullCode = concatenateClientFiles();
writeClientFiles(fullCode, distDir);
};

exports.code = fullCode;
exports.minifiedCode = minifiedCode;
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{ "description":"Message router for node.js supporting multiple protocols"
, "directories":{"doc":"./doc","lib":"./lib/ws"}
, "engines":{"node":">=0.2.0-0"}
, "name" : "Acequia"
, "repository":{"type":"git","url":"http://github.com/miksago/node-websocket-server.git"}
, "scripts": { "start": "node ./acequia.js" }
, "version" : "0.5.0"
{
"name" : "Acequia"
,"description":"Message router for node.js supporting multiple protocols"
, "main": "index"
, "directories": {"lib": "./lib"}
, "engines":{"node":">=0.6.0-0"}
, "repository":{"type":"git","url":"https://prgsmall@github.com/prgsmall/acequia.git"}
, "scripts": { "start": "node ." }
, "version" : "0.1.0"
}

0 comments on commit 7351c5c

Please sign in to comment.