title: Node.js Serve and Modify Static File On-the-Fly
body: <p>I have many <code>.m3u8</code> and <code>.ts</code> files for HLS Streaming,
  and all of these are generated by another streaming software on-the-fly.
  I want to add secure tokens per client to prevent unauthorized access.</p>
  
  <p>NginX is used to serve HLS files, so I choose <code>secure_link</code> module to provide a hashed token generated from a combination of pathname, client IP, expire time and a salt.</p>
  
  <p>The problem is, the <code>.m3u8</code> playlist must be modified to add tokens to each <code>.ts</code> file. I think NginX won't be able to modify the file, so it is the job my backend server should do.</p>
  
  <p>In general, <code>m3u8</code> files are not very large, but I'm not 100% sure. Caches are not very useful because the original <code>.m3u8</code> will be generated and modified by another process on-the-fly, and for every distinct clients the token varies.</p>
  
  <p>I'm currently using <a href="http://koajs.com/" rel="nofollow noreferrer">Koa</a>. I've wrote a <code>Transform</code> stream that can read a <code>.m3u8</code> file as stream and pipe out. Should I use <code>fs.createReadStream</code> and transform and pass the stream to <code>ctx.res</code>, or use <code>fs.readFile</code> to load and modify the whole file and send? Or any other better way to do it?</p>