Merge pull request #252 from secretrobotron/issue249

[Issue249] Support for ArrayBuffer in Filer.Buffer r=ack
This commit is contained in:
Alan K 2014-08-14 23:04:38 -04:00
commit 2d653d474e
5 changed files with 68 additions and 2 deletions

View File

@ -196,7 +196,7 @@ When in a node.js environment, native `Buffer`s can be used, or Filer.Buffer, wh
to node's `Buffer`. In a browser, you can use also use `Filer.Buffer`. to node's `Buffer`. In a browser, you can use also use `Filer.Buffer`.
NOTE: a `Filer.Buffer` in a browser is really an augmented `Uint8Array` (i.e., the node `Buffer` api NOTE: a `Filer.Buffer` in a browser is really an augmented `Uint8Array` (i.e., the node `Buffer` api
methods are added to the instance). See https://github.com/feross/buffer for more details. methods are added to the instance). See https://github.com/feross/buffer for more details. Additionally, unlike native `Buffer`, `Filer.Buffer`'s constructor can accept `ArrayBuffer` objects, which will be interpreted as `Uint8Array`s.
####Filer.Path<a name="FilerPath"></a> ####Filer.Path<a name="FilerPath"></a>

23
src/buffer.js Normal file
View File

@ -0,0 +1,23 @@
function FilerBuffer (subject, encoding, nonZero) {
// Automatically turn ArrayBuffer into Uint8Array so that underlying
// Buffer code doesn't just throw away and ignore ArrayBuffer data.
if (subject instanceof ArrayBuffer) {
subject = new Uint8Array(subject);
}
return new Buffer(subject, encoding, nonZero);
};
// Inherit prototype from Buffer
FilerBuffer.prototype = Object.create(Buffer.prototype);
FilerBuffer.prototype.constructor = FilerBuffer;
// Also copy static methods onto FilerBuffer ctor
Object.keys(Buffer).forEach(function (p) {
if (Buffer.hasOwnProperty(p)) {
FilerBuffer[p] = Buffer[p];
}
});
module.exports = FilerBuffer;

View File

@ -1,6 +1,6 @@
module.exports = { module.exports = {
FileSystem: require('./filesystem/interface.js'), FileSystem: require('./filesystem/interface.js'),
Buffer: Buffer, Buffer: require('./buffer.js'),
Path: require('./path.js'), Path: require('./path.js'),
Errors: require('./errors.js') Errors: require('./errors.js')
}; };

42
tests/bugs/issue249.js Normal file
View File

@ -0,0 +1,42 @@
var Filer = require('../..');
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;
describe('Filer.Buffer should accept initialized ArrayBuffers, issue 249', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should accept an ArrayBuffer with a specified size', function(done) {
var buffer = new Filer.Buffer(new ArrayBuffer(5));
expect(buffer.length).to.equal(5);
done();
});
});
describe('Filer.Buffer static methods are in tact, issue 249', function() {
beforeEach(util.setup);
afterEach(util.cleanup);
it('should proxy Buffer.isBuffer', function(done) {
expect(Filer.Buffer.isBuffer(new Filer.Buffer([]))).to.equal(true);
expect(Filer.Buffer.isBuffer('')).to.equal(false);
done();
});
it('should proxy Buffer.isEncoding', function(done) {
expect(Filer.Buffer.isEncoding('utf8')).to.equal(true);
expect(Filer.Buffer.isEncoding('smoop')).to.equal(false);
done();
});
it('should proxy Buffer.byteLength', function(done) {
expect(Filer.Buffer.byteLength('01100111', 'binary')).to.equal(8);
done();
});
it('should proxy Buffer.concat', function(done) {
expect(Filer.Buffer.concat([new Filer.Buffer(1), new Filer.Buffer(2)]).length).to.equal(3);
done();
});
});

View File

@ -68,5 +68,6 @@ require("./spec/node-js/simple/test-fs-watch-recursive");
require("./bugs/issue105"); require("./bugs/issue105");
require("./bugs/issue106"); require("./bugs/issue106");
require("./bugs/issue239"); require("./bugs/issue239");
require("./bugs/issue249");
require("./bugs/ls-depth-bug"); require("./bugs/ls-depth-bug");
require("./bugs/issue247.js"); require("./bugs/issue247.js");