Skip to content

Commit

Permalink
fix: return buffer rather than string
Browse files Browse the repository at this point in the history
  • Loading branch information
popomore committed Sep 23, 2015
1 parent 54224a2 commit 87ab9bc
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 26 deletions.
8 changes: 5 additions & 3 deletions index.js
@@ -1,3 +1,5 @@
'use strict';

var path = require('path');
var debug = require('debug')('connect-combo');
var async = require('async');
Expand Down Expand Up @@ -33,8 +35,8 @@ function combo(options) {
options = extend(defaults, options);

var directory = options.directory;
var getDir = typeof options.directory === 'string'?
function() {return directory;} : options.directory;
var getDir = typeof options.directory === 'string' ?
function() { return directory; } : options.directory;

return function(req, res, next) {
options.directory = getDir(req);
Expand Down Expand Up @@ -65,7 +67,7 @@ function combo(options) {
'Content-Type': mime.lookup(exts[0]),
'Date': new Date().toUTCString()
});
res.end(results.join('\n'));
res.end(Buffer.concat(results));
}
});
}
Expand Down
24 changes: 13 additions & 11 deletions lib/file.js
@@ -1,3 +1,5 @@
'use strict';

var fs = require('fs');
var path = require('path');
var url = require('url');
Expand All @@ -23,7 +25,7 @@ File.prototype.end = function(cb) {
if (!err) {
// find file from directory
that.emit('found', that.localPath);
cb(null, data.toString());
cb(null, data);
} else {
that.emit('not found', that.localPath);
if (that.options.beforeProxy) {
Expand All @@ -32,15 +34,15 @@ File.prototype.end = function(cb) {
} else {
requestProxy();
}
}

function requestProxy() {
if (that.options.proxy) {
// find file from remote server
that.getRemote(cb);
} else {
// not found
cb(err);
}
function requestProxy() {
if (that.options.proxy) {
// find file from remote server
that.getRemote(cb);
} else {
// not found
cb(err);
}
}
});
Expand Down Expand Up @@ -75,10 +77,10 @@ File.prototype.getRemote = function(cb) {
mkdirp(that.localPath);
fs.writeFile(that.localPath, data, function(err) {
!err && that.emit('cached', that.localPath);
cb(err, data.toString());
cb(err, data);
});
} else {
cb(null, data.toString());
cb(null, data);
}
});

Expand Down
37 changes: 29 additions & 8 deletions test/file.js
@@ -1,17 +1,20 @@
'use strict';

require('should');
var fs = require('fs');
var join = require('path').join;
var url = require('url');
var sinon = require('sinon');
var File = require('..').File;
var fixture = join(__dirname, 'fixture');

describe('File', function() {

var options;

beforeEach(function() {
options= {
directory: join(__dirname, 'fixture'),
options = {
directory: fixture,
proxy: 'http://static.alipayobjects.com',
cache: false
};
Expand All @@ -31,6 +34,7 @@ describe('File', function() {
new File('beforeproxy.js', options)
.on('found', spy)
.end(function(err, data) {
if (err) return done(err);
data.should.be.eql('beforeproxy');
spy.calledOnce.should.be.true;
done();
Expand All @@ -42,7 +46,8 @@ describe('File', function() {
new File('a.js', options)
.on('found', spy)
.end(function(err, data) {
data.should.be.eql('define("a", function(){});');
if (err) return done(err);
data.toString().should.be.eql('define("a", function(){});');
spy.calledOnce.should.be.true;
spy.calledWith(join(options.directory, 'a.js')).should.be.true;
done();
Expand All @@ -54,7 +59,7 @@ describe('File', function() {
var spy = sinon.spy();
new File('c.js', options)
.on('not found', spy)
.end(function(err, data) {
.end(function(err) {
err.should.be.an.instanceof(Error);
spy.calledOnce.should.be.true;
spy.calledWith(join(options.directory, 'c.js')).should.be.true;
Expand All @@ -70,7 +75,8 @@ describe('File', function() {
.on('not found', spy1)
.on('found', spy2)
.end(function(err, data) {
data.should.be.eql(seajs);
if (err) return done(err);
data.toString().should.be.eql(seajs);
spy1.calledOnce.should.be.true;
spy2.calledOnce.should.be.true;
spy1.calledWith(join(options.directory, 'seajs/seajs/2.1.1/sea.js')).should.be.true;
Expand All @@ -83,7 +89,7 @@ describe('File', function() {
var spy = sinon.spy();
new File('seajs/seajs/2.1.1/not-exist.js', options)
.on('not found', spy)
.end(function(err, data) {
.end(function(err) {
err.should.be.an.instanceof(Error);
spy.calledTwice.should.be.true;
spy.calledWith(join(options.directory, 'seajs/seajs/2.1.1/not-exist.js')).should.be.true;
Expand All @@ -95,7 +101,6 @@ describe('File', function() {
it('should response from remote and cached', function(done) {
options.cache = true;
var file = 'seajs/1.3.1/sea.js';
var seajs = fs.readFileSync(join(__dirname, './fixture/sea.js')).toString();
var cached = join(__dirname, 'fixture', file);
fs.existsSync(cached) && fs.unlinkSync(cached);

Expand All @@ -106,7 +111,8 @@ describe('File', function() {
.on('not found', spy1)
.on('found', spy2)
.on('cached', spy3)
.end(function(err, data) {
.end(function(err) {
if (err) return done(err);
spy1.calledOnce.should.be.true;
spy2.calledOnce.should.be.true;
spy3.calledOnce.should.be.true;
Expand All @@ -118,4 +124,19 @@ describe('File', function() {
done();
});
});

it('should get ttf', function(done) {
var len = fs.readFileSync(join(fixture, 'a.ttf')).length;
console.log(len);
var spy = sinon.spy();
new File('a.ttf', options)
.on('found', spy)
.end(function(err, data) {
if (err) return done(err);
spy.calledOnce.should.be.true;
new Buffer(data).length.should.eql(len);
spy.calledWith(join(options.directory, 'a.ttf')).should.be.true;
done();
});
});
});
Binary file added test/fixture/a.ttf
Binary file not shown.
8 changes: 4 additions & 4 deletions test/index.js
Expand Up @@ -19,7 +19,7 @@ describe('Combo', function() {
request(app)
.get('/??a.js,b.js,c/d.js')
.expect('Content-Type', 'application/javascript')
.expect('define("a", function(){});\ndefine("b", function(){});\ndefine("d", function(){});')
.expect('define("a", function(){});define("b", function(){});define("d", function(){});')
.end(done);
});

Expand All @@ -28,7 +28,7 @@ describe('Combo', function() {
request(app)
.get('/c??d.js,e.js')
.expect('Content-Type', 'application/javascript')
.expect('define("d", function(){});\ndefine("e", function(){});')
.expect('define("d", function(){});define("e", function(){});')
.end(done);
});

Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Combo', function() {
var widget = fs.readFileSync(path.join(__dirname, './fixture/widget.js')).toString();
request(app)
.get('/??a.js,arale/widget/1.0.0/widget.js')
.expect('define("a", function(){});\n' + widget)
.expect('define("a", function(){});' + widget)
.end(done);
});

Expand Down Expand Up @@ -144,7 +144,7 @@ describe('Combo', function() {
var app = createServer(options);
request(app)
.get('/??a.js?123,b.js?456&input_encoding=utf-8')
.expect('define("a", function(){});\ndefine("b", function(){});', done);
.expect('define("a", function(){});define("b", function(){});', done);
});

it('should show log', function(done) {
Expand Down

0 comments on commit 87ab9bc

Please sign in to comment.