doc/download.html
<!DOCTYPE html><html lang="en"><head><title>download</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="download"><meta name="groc-project-path" content="lib/download.js"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">lib/download.js</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="kd">var</span> <span class="nx">request</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'request'</span><span class="p">),</span>
<span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'fs'</span><span class="p">),</span>
<span class="nx">url</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'./url.js'</span><span class="p">),</span>
<span class="nx">nodeurl</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'url'</span><span class="p">),</span>
<span class="nx">path</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'path'</span><span class="p">),</span>
<span class="nx">events</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'events'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">Downloader</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">events</span><span class="p">.</span><span class="nx">EventEmitter</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
<span class="p">}</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Downloader inherits from EventEmitter</p></div></div><div class="code"><div class="wrapper"><span class="nx">Downloader</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">__proto__</span> <span class="o">=</span> <span class="nx">events</span><span class="p">.</span><span class="nx">EventEmitter</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span></div></div></div><div class="segment"><div class="comments doc-section"><div class="wrapper"><p>Download a resource to disk. If the URL is relative,
it will be converted to an absolute URL first using the
page URL.</p>
<p>Parameters:</p>
<ul>
<li><p><strong>resUrl must be a String.</strong><br/>(the URL of the resource to download)</p></li>
<li><p><strong>pageUrl must be a String.</strong><br/>(the URL of the page on which the link occured)</p></li>
</ul>
<p><strong>Returns an EventEmitter</strong><br/>(an event emitter)</p></div></div><div class="code"><div class="wrapper"><span class="nx">Downloader</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">downloadResource</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">resUrl</span><span class="p">,</span> <span class="nx">pageUrl</span><span class="p">,</span> <span class="nx">rename</span><span class="p">,</span> <span class="nx">cookiejar</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">dl</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">resUrl</span> <span class="o">||</span> <span class="o">!</span><span class="nx">pageUrl</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">dl</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'downloadResource was passed a NULL URL'</span><span class="p">));</span>
<span class="p">}</span>
<span class="nx">resUrl</span> <span class="o">=</span> <span class="nx">url</span><span class="p">.</span><span class="nx">cleanResourcePath</span><span class="p">(</span><span class="nx">resUrl</span><span class="p">,</span> <span class="nx">pageUrl</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">resUrl</span> <span class="o">&&</span> <span class="o">!</span><span class="sr">/^(f|ht)tps?:\/\//i</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">resUrl</span><span class="p">))</span> <span class="p">{</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>relative URL</p></div></div><div class="code"><div class="wrapper"> <span class="nx">resUrl</span> <span class="o">=</span> <span class="nx">url</span><span class="p">.</span><span class="nx">relativeToAbsolute</span><span class="p">(</span><span class="nx">resUrl</span><span class="p">,</span> <span class="nx">pageUrl</span><span class="p">);</span>
<span class="p">}</span>
<span class="nx">dl</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'downloadStarted'</span><span class="p">,</span> <span class="nx">resUrl</span><span class="p">);</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>renaming the file?</p></div></div><div class="code"><div class="wrapper"> <span class="kd">var</span> <span class="nx">name</span> <span class="o">=</span> <span class="nx">rename</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">rename</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">pathname</span> <span class="o">=</span> <span class="nx">nodeurl</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">resUrl</span><span class="p">).</span><span class="nx">pathname</span><span class="p">;</span>
<span class="nx">name</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">basename</span><span class="p">(</span><span class="nx">pathname</span><span class="p">);</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">config</span> <span class="o">=</span> <span class="p">{</span>
<span class="nx">url</span><span class="o">:</span> <span class="nx">resUrl</span>
<span class="p">}</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">cookiejar</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">config</span><span class="p">.</span><span class="nx">jar</span> <span class="o">=</span> <span class="nx">cookiejar</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">request</span><span class="p">(</span><span class="nx">config</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">response</span><span class="p">,</span> <span class="nx">body</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">err</span> <span class="o">&&</span> <span class="nx">response</span><span class="p">.</span><span class="nx">statusCode</span> <span class="o">==</span> <span class="mi">200</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">dl</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'dataReceived'</span><span class="p">,</span> <span class="nx">body</span><span class="p">);</span>
<span class="nx">fs</span><span class="p">.</span><span class="nx">writeFile</span><span class="p">(</span><span class="nx">name</span><span class="p">,</span> <span class="nx">body</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">dl</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">dl</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'downloadComplete'</span><span class="p">,</span> <span class="nx">name</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">dl</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="nx">err</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">dl</span><span class="p">.</span><span class="nx">emit</span><span class="p">(</span><span class="s1">'error'</span><span class="p">,</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'bad HTTP response code: '</span> <span class="o">+</span>
<span class="nx">response</span><span class="p">.</span><span class="nx">statusCode</span><span class="p">));</span>
<span class="p">}</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">Downloader</span><span class="p">;</span></div></div></div></div></body></html>