FlashPolicyFileServer | |
| lib/server.js |
Module dependencies and cached references.
|
var slice = Array.prototype.slice
, net = require('net');
|
The server that does the Policy File severing
Options
log false or a function that can output log information, defaults to console.log?
param: Object options Options to customize the servers functionality. param: Array origins The origins that are allowed on this server, defaults to *:* . api: public
|
function Server(options, origins){
var me = this;
this.origins = origins || ['*:*'];
this.port = 843;
this.log = console.log;
Object.keys(options).forEach(function(key){
me[key] && (me[key] = options[key])
});
this.socket = net.createServer(function createServer(socket){
socket.on('error', function socketError(){ me.responder.call(me, socket) });
me.responder.call(me, socket);
});
this.socket.on('error', function serverError(err){
if (err.errno == 13){
me.log && me.log(
'Unable to listen to port `' + me.port + '` as your Node.js instance does not have root privileges. ' +
(
me.server
? 'The Flash Policy file will now be served inline over the supplied HTTP server, Flash Policy files request will suffer.'
: 'No fallback server supplied.'
)
);
me.socket.removeAllListeners();
delete me.socket;
me.emit('connect_failed', err);
} else {
me.log && me.log('FlashPolicyFileServer received a error event:\n' + (err.message ? err.message : err));
}
});
this.socket.on('timeout', function serverTimeout(){});
this.socket.on('close', function serverClosed(err){
err && me.log && me.log('Server closing due to an error: \n' + (err.message ? err.message : err));
if (me.server){
delete me.server.online;
if( me.server['@'] && me.server.online){
me.server.removeListener('connection', me.server['@']);
}
}
me.log && me.log('Shutting down FlashPolicyFileServer');
});
this.compile();
}
|
Start listening for requests
param: Number port The port number it should be listening to. param: Server server A HTTP server instance, this will be used to listen for inline requests param: Function cb The callback needs to be called once server is ready api: public
|
Server.prototype.listen = function listen(port, server, cb){
var me = this
, args = slice.call(arguments, 0)
, callback;
args.forEach(function args(arg){
var type = typeof arg;
if (type === 'number') me.port = arg;
if (type === 'function') callback = arg;
if (type === 'object') me.server = arg;
});
if (this.server){
this.server['@'] = function connection(socket){
socket.once('data', function requestData(data){
if (
data
&& data[0] === 60
&& data.toString() === '<policy-file-request/>\0'
&& socket
&& (socket.readyState === 'open' || socket.readyState === 'writeOnly')
){
socket.end(me.buffer);
}
});
};
this.server.on('connection', this.server['@']);
}
this.socket.listen(this.port, function serverListening(){
me.socket.online = true;
if (callback) callback(), callback = undefined;
});
return this;
};
|
Adds a new origin to the Flash Policy File.
|
Server.prototype.add = function add(){
var args = slice.call(arguments, 0)
, i = args.length;
while (i--){
if (this.origins.indexOf(args[i]) >= 0){
args[i] = null;
}
}
Array.prototype.push.apply(
this.origins
, args.filter(function(value){ return !!value })
);
this.compile();
return this;
};
|
Removes a origin from the Flash Policy File.
|
Server.prototype.remove = function remove(origin){
var position = this.origins.indexOf(origin);
if (position > 0){
this.origins.splice(position,1);
this.compile();
}
return this;
};
|
Closes and cleans up the server
|
Server.prototype.close = function close(){
this.socket.removeAllListeners();
this.socket.close();
return this;
};
|
Proxy the event listener requests to the created Net server
|
Object.keys(process.EventEmitter.prototype).forEach(function proxy(key){
Server.prototype[key] = Server.prototype[key] || function (){
if (this.socket) this.socket[key].apply(this.socket, arguments);
return this;
};
});
|
Creates a new server instance.
|
exports.createServer = function createServer(options, origins){
origins = Array.isArray(origins) ? origins : (Array.isArray(options) ? options : false);
options = !Array.isArray(options) && options ? options : {};
return new Server(options, origins);
};
|
Provide a hook to the original server, so it can be extended if needed.
|
exports.Server = Server;
|
Module version
|
exports.version = '0.0.2';
|