Skip to content
Browse files

0.0.5

  • Loading branch information...
1 parent 177ce48 commit f05b7874d4f82bb7d580f144569a1dd3f4de451d @3rd-Eden committed
Showing with 196 additions and 142 deletions.
  1. +90 −69 doc/index.html
  2. +99 −66 lib/server.js
  3. +7 −7 package.json
View
159 doc/index.html
@@ -131,63 +131,64 @@
<ul><li><p><strong>param</strong>: <em>Object</em> options Options to customize the servers functionality.</p></li><li><p><strong>param</strong>: <em>Array</em> origins The origins that are allowed on this server, defaults to <code>*:*</code>.</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="keyword">function</span> <span class="class">Server</span>(<span class="variable">options</span>, <span class="variable">origins</span>){
+<pre><code><span class="keyword">function</span> <span class="class">Server</span> (<span class="variable">options</span>, <span class="variable">origins</span>) {
<span class="keyword">var</span> <span class="variable">me</span> = <span class="this">this</span>;
-
+
<span class="this">this</span>.<span class="variable">origins</span> = <span class="variable">origins</span> || [<span class="string">'*:*'</span>];
<span class="this">this</span>.<span class="variable">port</span> = <span class="number integer">843</span>;
<span class="this">this</span>.<span class="variable">log</span> = <span class="variable">console</span>.<span class="variable">log</span>;
-
+
<span class="comment">// merge `this` with the options</span>
- <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">options</span>).<span class="variable">forEach</span>(<span class="keyword">function</span>(<span class="variable">key</span>){
+ <span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">options</span>).<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">key</span>) {
<span class="variable">me</span>[<span class="variable">key</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; (<span class="variable">me</span>[<span class="variable">key</span>] = <span class="variable">options</span>[<span class="variable">key</span>])
});
-
+
<span class="comment">// create the net server</span>
- <span class="this">this</span>.<span class="variable">socket</span> = <span class="variable">net</span>.<span class="variable">createServer</span>(<span class="keyword">function</span> <span class="variable">createServer</span>(<span class="variable">socket</span>){
- <span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'error'</span>, <span class="keyword">function</span> <span class="variable">socketError</span>(){ <span class="variable">me</span>.<span class="variable">responder</span>.<span class="variable">call</span>(<span class="variable">me</span>, <span class="variable">socket</span>) });
+ <span class="this">this</span>.<span class="variable">socket</span> = <span class="variable">net</span>.<span class="variable">createServer</span>(<span class="keyword">function</span> <span class="variable">createServer</span> (<span class="variable">socket</span>) {
+ <span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'error'</span>, <span class="keyword">function</span> <span class="variable">socketError</span> () {
+ <span class="variable">me</span>.<span class="variable">responder</span>.<span class="variable">call</span>(<span class="variable">me</span>, <span class="variable">socket</span>);
+ });
+
<span class="variable">me</span>.<span class="variable">responder</span>.<span class="variable">call</span>(<span class="variable">me</span>, <span class="variable">socket</span>);
});
-
+
<span class="comment">// Listen for errors as the port might be blocked because we do not have root priv.</span>
- <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'error'</span>, <span class="keyword">function</span> <span class="variable">serverError</span>(<span class="variable">err</span>){
+ <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'error'</span>, <span class="keyword">function</span> <span class="variable">serverError</span> (<span class="variable">err</span>) {
<span class="comment">// Special and common case error handling</span>
- <span class="keyword">if</span> (<span class="variable">err</span>.<span class="variable">errno</span> == <span class="number integer">13</span>){
+ <span class="keyword">if</span> (<span class="variable">err</span>.<span class="variable">errno</span> == <span class="number integer">13</span>) {
<span class="variable">me</span>.<span class="variable">log</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">log</span>(
<span class="string">'Unable to listen to port `'</span> + <span class="variable">me</span>.<span class="variable">port</span> + <span class="string">'` as your Node.js instance does not have root privileges. '</span> +
(
<span class="variable">me</span>.<span class="variable">server</span>
- ? <span class="string">'The Flash Policy file will now be served inline over the supplied HTTP server, Flash Policy files request will suffer.'</span>
- : <span class="string">'No fallback server supplied.'</span>
+ ? <span class="string">'The Flash Policy File requests will only be served inline over the supplied HTTP server. Inline serving is slower than a dedicated server instance.'</span>
+ : <span class="string">'No fallback server supplied, we will be unable to answer Flash Policy File requests.'</span>
)
);
-
- <span class="variable">me</span>.<span class="variable">socket</span>.<span class="variable">removeAllListeners</span>();
- <span class="keyword">delete</span> <span class="variable">me</span>.<span class="variable">socket</span>;
<span class="variable">me</span>.<span class="variable">emit</span>(<span class="string">'connect_failed'</span>, <span class="variable">err</span>);
+ <span class="variable">me</span>.<span class="variable">socket</span>.<span class="variable">removeAllListeners</span>();
+ <span class="keyword">delete</span> <span class="variable">me</span>.<span class="variable">socket</span>;
} <span class="keyword">else</span> {
- <span class="variable">me</span>.<span class="variable">log</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">log</span>(<span class="string">'FlashPolicyFileServer received a error event:\n'</span> + (<span class="variable">err</span>.<span class="variable">message</span> ? <span class="variable">err</span>.<span class="variable">message</span> : <span class="variable">err</span>));
+ <span class="variable">me</span>.<span class="variable">log</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">log</span>(<span class="string">'FlashPolicyFileServer received an error event:\n'</span> + (<span class="variable">err</span>.<span class="variable">message</span> ? <span class="variable">err</span>.<span class="variable">message</span> : <span class="variable">err</span>));
}
});
-
- <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'timeout'</span>, <span class="keyword">function</span> <span class="variable">serverTimeout</span>(){});
- <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'close'</span>, <span class="keyword">function</span> <span class="variable">serverClosed</span>(<span class="variable">err</span>){
+
+ <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'timeout'</span>, <span class="keyword">function</span> <span class="variable">serverTimeout</span> () {});
+ <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">on</span>(<span class="string">'close'</span>, <span class="keyword">function</span> <span class="variable">serverClosed</span> (<span class="variable">err</span>) {
<span class="variable">err</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">log</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">log</span>(<span class="string">'Server closing due to an error: \n'</span> + (<span class="variable">err</span>.<span class="variable">message</span> ? <span class="variable">err</span>.<span class="variable">message</span> : <span class="variable">err</span>));
-
- <span class="keyword">if</span> (<span class="variable">me</span>.<span class="variable">server</span>){
- <span class="comment">// not online anymore</span>
- <span class="keyword">delete</span> <span class="variable">me</span>.<span class="variable">server</span>.<span class="variable">online</span>;
-
+
+ <span class="keyword">if</span> (<span class="variable">me</span>.<span class="variable">server</span>) {
<span class="comment">// Remove the inline policy listener if we close down</span>
<span class="comment">// but only when the server was `online` (see listen prototype)</span>
- <span class="keyword">if</span>( <span class="variable">me</span>.<span class="variable">server</span>[<span class="string">'@'</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">server</span>.<span class="variable">online</span>){
+ <span class="keyword">if</span> (<span class="variable">me</span>.<span class="variable">server</span>[<span class="string">'@'</span>] &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">server</span>.<span class="variable">online</span>) {
<span class="variable">me</span>.<span class="variable">server</span>.<span class="variable">removeListener</span>(<span class="string">'connection'</span>, <span class="variable">me</span>.<span class="variable">server</span>[<span class="string">'@'</span>]);
}
+
+ <span class="comment">// not online anymore</span>
+ <span class="keyword">delete</span> <span class="variable">me</span>.<span class="variable">server</span>.<span class="variable">online</span>;
}
- <span class="variable">me</span>.<span class="variable">log</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">me</span>.<span class="variable">log</span>(<span class="string">'Shutting down FlashPolicyFileServer'</span>);
});
-
+
<span class="comment">// Compile the initial `buffer`</span>
<span class="this">this</span>.<span class="variable">compile</span>();
}</code></pre>
@@ -202,26 +203,26 @@
<ul><li><p><strong>param</strong>: <em>Number</em> port The port number it should be listening to.</p></li><li><p><strong>param</strong>: <em>Server</em> server A HTTP server instance, this will be used to listen for inline requests</p></li><li><p><strong>param</strong>: <em>Function</em> cb The callback needs to be called once server is ready</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listen</span> = <span class="keyword">function</span> <span class="variable">listen</span>(<span class="variable">port</span>, <span class="variable">server</span>, <span class="variable">cb</span>){
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">listen</span> = <span class="keyword">function</span> <span class="variable">listen</span> (<span class="variable">port</span>, <span class="variable">server</span>, <span class="variable">cb</span>){
<span class="keyword">var</span> <span class="variable">me</span> = <span class="this">this</span>
, <span class="variable">args</span> = <span class="variable">slice</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="number integer">0</span>)
, <span class="variable">callback</span>;
-
+
<span class="comment">// assign the correct vars, for flexible arguments</span>
- <span class="variable">args</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> <span class="variable">args</span>(<span class="variable">arg</span>){
+ <span class="variable">args</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> <span class="variable">args</span> (<span class="variable">arg</span>){
<span class="keyword">var</span> <span class="variable">type</span> = <span class="keyword">typeof</span> <span class="variable">arg</span>;
-
+
<span class="keyword">if</span> (<span class="variable">type</span> === <span class="string">'number'</span>) <span class="variable">me</span>.<span class="variable">port</span> = <span class="variable">arg</span>;
<span class="keyword">if</span> (<span class="variable">type</span> === <span class="string">'function'</span>) <span class="variable">callback</span> = <span class="variable">arg</span>;
<span class="keyword">if</span> (<span class="variable">type</span> === <span class="string">'object'</span>) <span class="variable">me</span>.<span class="variable">server</span> = <span class="variable">arg</span>;
});
-
- <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">server</span>){
-
+
+ <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">server</span>) {
+
<span class="comment">// no one in their right mind would ever create a `@` prototype, so Im just gonna store</span>
<span class="comment">// my function on the server, so I can remove it later again once the server(s) closes</span>
- <span class="this">this</span>.<span class="variable">server</span>[<span class="string">'@'</span>] = <span class="keyword">function</span> <span class="variable">connection</span>(<span class="variable">socket</span>){
- <span class="variable">socket</span>.<span class="variable">once</span>(<span class="string">'data'</span>, <span class="keyword">function</span> <span class="variable">requestData</span>(<span class="variable">data</span>){
+ <span class="this">this</span>.<span class="variable">server</span>[<span class="string">'@'</span>] = <span class="keyword">function</span> <span class="variable">connection</span> (<span class="variable">socket</span>) {
+ <span class="variable">socket</span>.<span class="variable">once</span>(<span class="string">'data'</span>, <span class="keyword">function</span> <span class="variable">requestData</span> (<span class="variable">data</span>) {
<span class="comment">// if it's a Flash policy request, and we can write to the </span>
<span class="keyword">if</span> (
<span class="variable">data</span>
@@ -231,24 +232,27 @@
&<span class="variable">amp</span>;&<span class="variable">amp</span>; (<span class="variable">socket</span>.<span class="variable">readyState</span> === <span class="string">'open'</span> || <span class="variable">socket</span>.<span class="variable">readyState</span> === <span class="string">'writeOnly'</span>)
){
<span class="comment">// send the buffer</span>
- <span class="variable">socket</span>.<span class="variable">end</span>(<span class="variable">me</span>.<span class="variable">buffer</span>);
+ <span class="keyword">try</span> { <span class="variable">socket</span>.<span class="variable">end</span>(<span class="variable">me</span>.<span class="variable">buffer</span>); }
+ <span class="keyword">catch</span> (<span class="variable">e</span>) {}
}
});
};
+
<span class="comment">// attach it</span>
<span class="this">this</span>.<span class="variable">server</span>.<span class="variable">on</span>(<span class="string">'connection'</span>, <span class="this">this</span>.<span class="variable">server</span>[<span class="string">'@'</span>]);
}
-
+
<span class="comment">// We add a callback method, so we can set a flag for when the server is `enabled` or `online`.</span>
<span class="comment">// this flag is needed because if a error occurs and the we cannot boot up the server the</span>
<span class="comment">// fallback functionality should not be removed during the `close` event</span>
- <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">listen</span>(<span class="this">this</span>.<span class="variable">port</span>, <span class="keyword">function</span> <span class="variable">serverListening</span>(){
- <span class="variable">me</span>.<span class="variable">socket</span>.<span class="variable">online</span> = <span class="variable">true</span>;
-
- <span class="keyword">if</span> (<span class="variable">callback</span>) <span class="variable">callback</span>(), <span class="variable">callback</span> = <span class="variable">undefined</span>;
-
+ <span class="this">this</span>.<span class="variable">port</span> &<span class="variable">gt</span>;= <span class="number integer">0</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">listen</span>(<span class="this">this</span>.<span class="variable">port</span>, <span class="keyword">function</span> <span class="variable">serverListening</span> () {
+ <span class="variable">me</span>.<span class="variable">socket</span>.<span class="variable">online</span> = <span class="variable">true</span>;
+ <span class="keyword">if</span> (<span class="variable">callback</span>) {
+ <span class="variable">callback</span>.<span class="variable">call</span>(<span class="variable">me</span>);
+ <span class="variable">callback</span> = <span class="variable">undefined</span>;
+ }
});
-
+
<span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
@@ -265,21 +269,23 @@
<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">add</span> = <span class="keyword">function</span> <span class="variable">add</span>(){
<span class="keyword">var</span> <span class="variable">args</span> = <span class="variable">slice</span>.<span class="variable">call</span>(<span class="variable">arguments</span>, <span class="number integer">0</span>)
, <span class="variable">i</span> = <span class="variable">args</span>.<span class="variable">length</span>;
-
+
<span class="comment">// flag duplicates</span>
- <span class="keyword">while</span> (<span class="variable">i</span>--){
+ <span class="keyword">while</span> (<span class="variable">i</span>--) {
<span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">origins</span>.<span class="variable">indexOf</span>(<span class="variable">args</span>[<span class="variable">i</span>]) &<span class="variable">gt</span>;= <span class="number integer">0</span>){
<span class="variable">args</span>[<span class="variable">i</span>] = <span class="keyword">null</span>;
}
}
-
+
<span class="comment">// Add all the arguments to the array</span>
<span class="comment">// but first we want to remove all `falsy` values from the args</span>
<span class="class">Array</span>.<span class="variable">prototype</span>.<span class="variable">push</span>.<span class="variable">apply</span>(
<span class="this">this</span>.<span class="variable">origins</span>
- , <span class="variable">args</span>.<span class="variable">filter</span>(<span class="keyword">function</span>(<span class="variable">value</span>){ <span class="keyword">return</span> !!<span class="variable">value</span> })
+ , <span class="variable">args</span>.<span class="variable">filter</span>(<span class="keyword">function</span> <span class="variable">filter</span> (<span class="variable">value</span>) {
+ <span class="keyword">return</span> !!<span class="variable">value</span>;
+ })
);
-
+
<span class="this">this</span>.<span class="variable">compile</span>();
<span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
@@ -294,15 +300,15 @@
<ul><li><p><strong>param</strong>: <em>String</em> origin The origin that needs to be removed from the server</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> <span class="variable">remove</span>(<span class="variable">origin</span>){
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> <span class="variable">remove</span> (<span class="variable">origin</span>){
<span class="keyword">var</span> <span class="variable">position</span> = <span class="this">this</span>.<span class="variable">origins</span>.<span class="variable">indexOf</span>(<span class="variable">origin</span>);
-
+
<span class="comment">// only remove and recompile if we have a match</span>
- <span class="keyword">if</span> (<span class="variable">position</span> &<span class="variable">gt</span>; <span class="number integer">0</span>){
- <span class="this">this</span>.<span class="variable">origins</span>.<span class="variable">splice</span>(<span class="variable">position</span>,<span class="number integer">1</span>);
+ <span class="keyword">if</span> (<span class="variable">position</span> &<span class="variable">gt</span>; <span class="number integer">0</span>) {
+ <span class="this">this</span>.<span class="variable">origins</span>.<span class="variable">splice</span>(<span class="variable">position</span>, <span class="number integer">1</span>);
<span class="this">this</span>.<span class="variable">compile</span>();
}
-
+
<span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
@@ -314,10 +320,11 @@
<ul><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">close</span> = <span class="keyword">function</span> <span class="variable">close</span>(){
+<pre><code><span class="class">Server</span>.<span class="variable">prototype</span>.<span class="variable">close</span> = <span class="keyword">function</span> <span class="variable">close</span> () {
<span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">removeAllListeners</span>();
- <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">close</span>();
-
+ <span class="keyword">try</span> { <span class="this">this</span>.<span class="variable">socket</span>.<span class="variable">close</span>(); }
+ <span class="keyword">catch</span> (<span class="variable">e</span>) {}
+
<span class="keyword">return</span> <span class="this">this</span>;
};</code></pre>
</td>
@@ -328,9 +335,12 @@
</p>
</td>
<td class="code">
-<pre><code><span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">process</span>.<span class="class">EventEmitter</span>.<span class="variable">prototype</span>).<span class="variable">forEach</span>(<span class="keyword">function</span> <span class="variable">proxy</span>(<span class="variable">key</span>){
- <span class="class">Server</span>.<span class="variable">prototype</span>[<span class="variable">key</span>] = <span class="class">Server</span>.<span class="variable">prototype</span>[<span class="variable">key</span>] || <span class="keyword">function</span> (){
- <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">socket</span>) <span class="this">this</span>.<span class="variable">socket</span>[<span class="variable">key</span>].<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">socket</span>, <span class="variable">arguments</span>);
+<pre><code><span class="class">Object</span>.<span class="variable">keys</span>(<span class="variable">process</span>.<span class="class">EventEmitter</span>.<span class="variable">prototype</span>).<span class="variable">forEach</span>(<span class="keyword">function</span> <span class="variable">proxy</span> (<span class="variable">key</span>){
+ <span class="class">Server</span>.<span class="variable">prototype</span>[<span class="variable">key</span>] = <span class="class">Server</span>.<span class="variable">prototype</span>[<span class="variable">key</span>] || <span class="keyword">function</span> () {
+ <span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">socket</span>) {
+ <span class="this">this</span>.<span class="variable">socket</span>[<span class="variable">key</span>].<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">socket</span>, <span class="variable">arguments</span>);
+ }
+
<span class="keyword">return</span> <span class="this">this</span>;
};
});</code></pre>
@@ -345,18 +355,26 @@
<ul><li><p><strong>param</strong>: <em>Object</em> options A options object to override the default config</p></li><li><p><strong>param</strong>: <em>Array</em> origins The origins that should be allowed by the server</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="variable">exports</span>.<span class="variable">createServer</span> = <span class="keyword">function</span> <span class="variable">createServer</span>(<span class="variable">options</span>, <span class="variable">origins</span>){
- <span class="variable">origins</span> = <span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">origins</span>) ? <span class="variable">origins</span> : (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>) ? <span class="variable">options</span> : <span class="variable">false</span>);
- <span class="variable">options</span> = !<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>) &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">options</span> ? <span class="variable">options</span> : {};
-
+<pre><code><span class="variable">exports</span>.<span class="variable">createServer</span> = <span class="keyword">function</span> <span class="variable">createServer</span> (<span class="variable">options</span>, <span class="variable">origins</span>) {
+ <span class="variable">origins</span> = <span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">origins</span>)
+ ? <span class="variable">origins</span>
+ : (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>) ? <span class="variable">options</span> : <span class="variable">false</span>);
+
+ <span class="variable">options</span> = !<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>) &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">options</span>
+ ? <span class="variable">options</span>
+ : {};
+
<span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Server</span>(<span class="variable">options</span>, <span class="variable">origins</span>);
};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
-<p>Provide a hook to the original server, so it can be extended if needed.
- </p>
+<p>Provide a hook to the original server, so it can be extended if needed.</p>
+
+<h2></h2>
+
+<ul><li><p><strong>type</strong>: <em>Net.Server</em> </p></li></ul>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Server</span> = <span class="class">Server</span>;</code></pre>
@@ -364,11 +382,14 @@
</tr>
<tr class="code">
<td class="docs">
-<p>Module version
- </p>
+<p>Module version</p>
+
+<h2></h2>
+
+<ul><li><p><strong>type</strong>: <em>String</em> </p></li></ul>
</td>
<td class="code">
-<pre><code><span class="variable">exports</span>.<span class="variable">version</span> = <span class="string">'0.0.2'</span>;
+<pre><code><span class="variable">exports</span>.<span class="variable">version</span> = <span class="string">'0.0.5'</span>;
</code></pre>
</td>
</tr> </body>
View
165 lib/server.js
@@ -1,6 +1,7 @@
/**
* Module dependencies and cached references.
*/
+
var slice = Array.prototype.slice
, net = require('net');
@@ -14,28 +15,32 @@ var slice = Array.prototype.slice
* @param {Array} origins The origins that are allowed on this server, defaults to `*:*`.
* @api public
*/
-function Server(options, origins){
+
+function Server (options, origins) {
var me = this;
-
+
this.origins = origins || ['*:*'];
this.port = 843;
this.log = console.log;
-
+
// merge `this` with the options
- Object.keys(options).forEach(function(key){
+ Object.keys(options).forEach(function (key) {
me[key] && (me[key] = options[key])
});
-
+
// create the net server
- this.socket = net.createServer(function createServer(socket){
- socket.on('error', function socketError(){ me.responder.call(me, socket) });
+ this.socket = net.createServer(function createServer (socket) {
+ socket.on('error', function socketError () {
+ me.responder.call(me, socket);
+ });
+
me.responder.call(me, socket);
});
-
+
// Listen for errors as the port might be blocked because we do not have root priv.
- this.socket.on('error', function serverError(err){
+ this.socket.on('error', function serverError (err) {
// Special and common case error handling
- if (err.errno == 13){
+ 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. ' +
(
@@ -44,7 +49,7 @@ function Server(options, origins){
: 'No fallback server supplied, we will be unable to answer Flash Policy File requests.'
)
);
-
+
me.emit('connect_failed', err);
me.socket.removeAllListeners();
delete me.socket;
@@ -52,23 +57,23 @@ function Server(options, origins){
me.log && me.log('FlashPolicyFileServer received an error event:\n' + (err.message ? err.message : err));
}
});
-
- this.socket.on('timeout', function serverTimeout(){});
- this.socket.on('close', function serverClosed(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){
+
+ if (me.server) {
// Remove the inline policy listener if we close down
// but only when the server was `online` (see listen prototype)
- if( me.server['@'] && me.server.online){
+ if (me.server['@'] && me.server.online) {
me.server.removeListener('connection', me.server['@']);
}
-
+
// not online anymore
delete me.server.online;
}
});
-
+
// Compile the initial `buffer`
this.compile();
}
@@ -81,26 +86,27 @@ function Server(options, origins){
* @param {Function} cb The callback needs to be called once server is ready
* @api public
*/
-Server.prototype.listen = function listen(port, server, cb){
+
+Server.prototype.listen = function listen (port, server, cb){
var me = this
, args = slice.call(arguments, 0)
, callback;
-
+
// assign the correct vars, for flexible arguments
- args.forEach(function args(arg){
+ 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){
-
+
+ if (this.server) {
+
// no one in their right mind would ever create a `@` prototype, so Im just gonna store
// my function on the server, so I can remove it later again once the server(s) closes
- this.server['@'] = function connection(socket){
- socket.once('data', function requestData(data){
+ this.server['@'] = function connection (socket) {
+ socket.once('data', function requestData (data) {
// if it's a Flash policy request, and we can write to the
if (
data
@@ -110,23 +116,27 @@ Server.prototype.listen = function listen(port, server, cb){
&& (socket.readyState === 'open' || socket.readyState === 'writeOnly')
){
// send the buffer
- socket.end(me.buffer);
+ try { socket.end(me.buffer); }
+ catch (e) {}
}
});
};
+
// attach it
this.server.on('connection', this.server['@']);
}
-
+
// We add a callback method, so we can set a flag for when the server is `enabled` or `online`.
// this flag is needed because if a error occurs and the we cannot boot up the server the
// fallback functionality should not be removed during the `close` event
- this.port >= 0 && this.socket.listen(this.port, function serverListening(){
- me.socket.online = true;
- if (callback) callback.call(me), callback = undefined;
-
+ this.port >= 0 && this.socket.listen(this.port, function serverListening () {
+ me.socket.online = true;
+ if (callback) {
+ callback.call(me);
+ callback = undefined;
+ }
});
-
+
return this;
};
@@ -136,9 +146,12 @@ Server.prototype.listen = function listen(port, server, cb){
* @param {net.Socket} socket The socket that needs to receive the message
* @api private
*/
-Server.prototype.responder = function responder(socket){
- if (socket && socket.readyState == 'open' && socket.end)
- socket.end(this.buffer);
+
+Server.prototype.responder = function responder (socket){
+ if (socket && socket.readyState == 'open' && socket.end) {
+ try { socket.end(this.buffer); }
+ catch (e) {}
+ }
};
/**
@@ -147,24 +160,25 @@ Server.prototype.responder = function responder(socket){
*
* @api private
*/
-Server.prototype.compile = function compile(){
+
+Server.prototype.compile = function compile (){
var xml = [
'<?xml version="1.0"?>'
, '<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">'
, '<cross-domain-policy>'
];
-
+
// add the allow access element
- this.origins.forEach(function origin(origin){
+ this.origins.forEach(function origin (origin){
var parts = origin.split(':');
xml.push('<allow-access-from domain="' + parts[0] + '" to-ports="'+ parts[1] +'"/>');
});
-
+
xml.push('</cross-domain-policy>');
-
+
// store the result in a buffer so we don't have to re-generate it all the time
this.buffer = new Buffer(xml.join(''), 'utf8');
-
+
return this;
};
@@ -174,24 +188,27 @@ Server.prototype.compile = function compile(){
* @param {Arguments} The origins that need to be added.
* @api public
*/
+
Server.prototype.add = function add(){
var args = slice.call(arguments, 0)
, i = args.length;
-
+
// flag duplicates
- while (i--){
+ while (i--) {
if (this.origins.indexOf(args[i]) >= 0){
args[i] = null;
}
}
-
+
// Add all the arguments to the array
// but first we want to remove all `falsy` values from the args
Array.prototype.push.apply(
this.origins
- , args.filter(function filter(value){ return !!value })
+ , args.filter(function filter (value) {
+ return !!value;
+ })
);
-
+
this.compile();
return this;
};
@@ -202,15 +219,16 @@ Server.prototype.add = function add(){
* @param {String} origin The origin that needs to be removed from the server
* @api public
*/
-Server.prototype.remove = function remove(origin){
+
+Server.prototype.remove = function remove (origin){
var position = this.origins.indexOf(origin);
-
+
// only remove and recompile if we have a match
- if (position > 0){
- this.origins.splice(position,1);
+ if (position > 0) {
+ this.origins.splice(position, 1);
this.compile();
}
-
+
return this;
};
@@ -219,21 +237,25 @@ Server.prototype.remove = function remove(origin){
*
* @api public
*/
-Server.prototype.close = function close(){
+
+Server.prototype.close = function close () {
this.socket.removeAllListeners();
- try {
- this.socket.close();
- }
+ try { this.socket.close(); }
catch (e) {}
+
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);
+
+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;
};
});
@@ -245,19 +267,30 @@ Object.keys(process.EventEmitter.prototype).forEach(function proxy(key){
* @param {Array} origins The origins that should be allowed by the server
* @api public
*/
-exports.createServer = function createServer(options, origins){
- origins = Array.isArray(origins) ? origins : (Array.isArray(options) ? options : false);
- options = !Array.isArray(options) && options ? options : {};
-
+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.
+ *
+ * @type {Net.Server}
*/
+
exports.Server = Server;
/**
* Module version
+ *
+ * @type {String}
*/
-exports.version = '0.0.3';
+
+exports.version = '0.0.5';
View
14 package.json
@@ -1,8 +1,8 @@
{
"name": "policyfile"
-, "version": "0.0.3"
+, "version": "0.0.5"
, "author": "Arnout Kazemier"
-, "description": "Flash Socket Policy File Server. A server to respond to Flash Socket Policy requests, both inline and through a dedicated server instance. "
+, "description": "Flash Socket Policy File Server. A server to respond to Flash Socket Policy requests, both inline and through a dedicated server instance."
, "main": "index"
, "keywords":[
"flash"
@@ -21,12 +21,12 @@
, "email":"info@3rd-Eden.com"
, "web":"http://www.3rd-Eden.com"
}]
-, "licenses": [{
+, "license": {
"type": "MIT"
, "url": "https://github.com/3rd-Eden/FlashPolicyFileServer/blob/master/LICENSE"
- }]
-, "repositories": [{
+ }
+, "repository": {
"type": "git"
, "url" : "https://github.com/3rd-Eden/FlashPolicyFileServer.git"
- }]
-}
+ }
+}

0 comments on commit f05b787

Please sign in to comment.
Something went wrong with that request. Please try again.