title: Resolve filesystem paths (possibly with symlinks) with Node.js
body: <p>I need a way to verify whether or not a file is under a specific path.  If the path is <code>/var/www</code> and the file is <code>/var/www/something/something-else/file</code>, I need to return <code>true</code>.  If the file is <code>/var/www/../../etc/passwd</code>, I need to return <code>false</code>.</p>
  
  <p>In PHP land, I usually use a function called <a href="http://php.net/manual/en/function.realpath.php" rel="noreferrer"><code>realpath()</code></a>:</p>
  
  <blockquote>
    <p>realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and returns the canonicalized absolute pathname.</p>
  </blockquote>
  
  <p>I run <code>realpath()</code> on both the desired path and the full file path, then determine if the desired path is a substring of the result of the fully resolved file path.  If it is, I return <code>true</code>.</p>
  
  <p>Is there a function for Node.js that achieves the same goal?  <a href="http://nodejs.org/api/path.html#path_path_resolve_from_to" rel="noreferrer"><code>path.resolve()</code></a> gets me half way, but doesn't handle any symlinks that are actually in the filesystem.  <a href="http://nodejs.org/api/fs.html#fs_fs_readlink_path_callback" rel="noreferrer"><code>fs.readlink()</code></a> has absolutely no documentation at all, and may not directly apply since I won't always have symlinks.</p>
  
  <p>Must I loop through each part of the path, resolving symlinks as I go, or is there a more canonical method?</p>