Browse Source

support range requests

master
Alan Faubert 2 years ago
parent
commit
de8c6d90b7
  1. 24
      sfs.js

24
sfs.js

@ -66,11 +66,25 @@ http
) {
type += '; charset=utf-8';
}
res.writeHead(200, {
'content-type': type,
'content-length': stats.size,
});
fs.createReadStream(path)
let range = /^bytes=(\d+)-(\d*)$/.exec(req.headers.range);
if (range) {
const start = +range[1];
const end = range[2] ? +range[2] : stats.size - 1;
range = start <= end && end < stats.size ? { start, end } : null;
}
if (range) {
res.writeHead(206, {
'content-type': type,
'content-range': `bytes ${range.start}-${range.end}/${stats.size}`,
'content-length': range.end - range.start + 1,
});
} else {
res.writeHead(200, {
'content-type': type,
'content-length': stats.size,
});
}
fs.createReadStream(path, range)
.on('error', () => sendError(res, 500, 'Internal Server Error'))
.pipe(res);
return;

Loading…
Cancel
Save