diff --git a/src/buffer.js b/src/buffer.js new file mode 100644 index 0000000..1756aee --- /dev/null +++ b/src/buffer.js @@ -0,0 +1,18 @@ +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); +}; + +Object.keys(Buffer).forEach(function (p) { + if (Buffer.hasOwnProperty(p)) { + FilerBuffer[p] = Buffer[p]; + } +}); + +module.exports = FilerBuffer; diff --git a/tests/bugs/issue249.js b/tests/bugs/issue249.js new file mode 100644 index 0000000..28776d4 --- /dev/null +++ b/tests/bugs/issue249.js @@ -0,0 +1,14 @@ +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(); + }); +}); diff --git a/tests/index.js b/tests/index.js index c04b12c..5d9632a 100644 --- a/tests/index.js +++ b/tests/index.js @@ -68,5 +68,6 @@ require("./spec/node-js/simple/test-fs-watch-recursive"); require("./bugs/issue105"); require("./bugs/issue106"); require("./bugs/issue239"); +require("./bugs/issue249"); require("./bugs/ls-depth-bug"); require("./bugs/issue247.js");