Skip to content

Commit

Permalink
refactor as middleware function and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
mroswald committed Jul 7, 2016
1 parent f67c0b6 commit 5d540fa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
13 changes: 13 additions & 0 deletions packager/react-packager/src/Server/__tests__/Server-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,19 @@ describe('processRequest', () => {
expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios');
expect(res.end).toBeCalledWith('i am image');
});

it('should serve range request', () => {
const req = {url: '/assets/imgs/a.png?platform=ios', headers: {range: 'bytes=0-3'}};
const res = {end: jest.fn(), writeHead: jest.fn()};
const mockData = new Buffer('i am image');

AssetServer.prototype.get.mockImpl(() => Promise.resolve(mockData));

server.processRequest(req, res);
jest.runAllTimers();
expect(AssetServer.prototype.get).toBeCalledWith('imgs/a.png', 'ios');
expect(res.end).toBeCalledWith(mockData.slice(0, 3));
});
});

describe('buildbundle(options)', () => {
Expand Down
45 changes: 21 additions & 24 deletions packager/react-packager/src/Server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ const dependencyOpts = declareOpts({
},
});

const isRangeRequest = (req) => req.headers && req.headers.range;

class Server {
constructor(options) {
const opts = validateOpts(options);
Expand Down Expand Up @@ -399,34 +397,33 @@ class Server {
});
}

_rangeRequestMiddleware(req, res, data, assetPath) {
if (req.headers && req.headers.range) {
let [rangeStart, rangeEnd] = req.headers.range.replace(/bytes=/, '').split('-');
let dataStart = parseInt(rangeStart, 10);
let dataEnd = rangeEnd ? parseInt(rangeEnd, 10) : data.length - 1;
let chunksize = (dataEnd - dataStart) + 1;

res.writeHead(206, {
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Range': `bytes ${dataStart}-${dataEnd}/${data.length}`,
'Content-Type': mime.lookup(path.basename(assetPath[1]))
});

return data.slice(dataStart, dataEnd);
}

return data;
}

_processAssetsRequest(req, res) {
const urlObj = url.parse(req.url, true);
const assetPath = urlObj.pathname.match(/^\/assets\/(.+)$/);
const assetEvent = Activity.startEvent(`processing asset request ${assetPath[1]}`);
this._assetServer.get(assetPath[1], urlObj.query.platform)
.then(
data => {
if (isRangeRequest(req)) {
let [rangeStart, rangeEnd] = req.headers.range.replace(/bytes=/, '').split('-');
let dataStart = parseInt(rangeStart, 10);
let dataEnd = rangeEnd ? parseInt(rangeEnd, 10) : data.length - 1;
let chunksize = (dataEnd - dataStart) + 1;

res.writeHead(206, {
'Accept-Ranges': 'bytes',
'Content-Length': chunksize,
'Content-Range': `bytes ${dataStart}-${dataEnd}/${data.length}`,
'Content-Type': mime.lookup(path.basename(assetPath[1]))
});

let output = new Buffer(chunksize);
data.copy(output, 0, dataStart, dataEnd);

res.end(output);
} else {
res.end(data);
}
},
data => res.end(this._rangeRequestMiddleware(req, res, data, assetPath)),
error => {
console.error(error.stack);
res.writeHead('404');
Expand Down

0 comments on commit 5d540fa

Please sign in to comment.