Skip to content

Commit

Permalink
Add range header support.
Browse files Browse the repository at this point in the history
  • Loading branch information
sammacbeth committed May 16, 2018
1 parent 795b150 commit fab2160
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const joinPaths = require('path').join;
const pump = require('pump')
const parseDatURL = require('parse-dat-url');
const pda = require('pauls-dat-api');
const DatArchive = require('node-dat-archive')
const DatArchive = require('node-dat-archive');
const parseRange = require('range-parser');

class DatGateway {
constructor(library) {
Expand All @@ -31,7 +32,7 @@ class DatGateway {
async handleRequest(req, res) {
// mimic beakerbrowser's dat protocol handling for the web.
// Cribbed from https://github.com/beakerbrowser/beaker/blob/master/app/background-process/protocols/dat.js
// minor minor alterations
// with minor alterations
const errorResponse = (code, message) => {
res.statusCode = code;
res.end(message);
Expand Down Expand Up @@ -121,12 +122,23 @@ class DatGateway {
return;
}
}
// handle range
res.setHeader('Accept-Ranges', 'bytes');
let range = req.headers.Range || req.headers.range;
if (range) range = parseRange(entry.size, range);
if (range && range.type === 'bytes') {
const sendRange = range[0];
res.statusCode = 206;
res.setHeader('Content-Range', `bytes ${sendRange.start}-${sendRange.end}/${entry.size}`);
res.setHeader('Content-Length', sendRange.end - sendRange.start + 1);
} else {
res.setHeader('Content-Length', entry.size);
res.statusCode = 200;
}

res.setHeader('Content-Length', entry.size);
res.setHeader('Content-Type', mime.getType(entry.path));
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Cache-Control', 'public, max-age: 60');
res.statusCode = 200;
if (req.method === 'HEAD') {
res.end();
}
Expand Down

0 comments on commit fab2160

Please sign in to comment.