From af1943b5c94002bddb404610a2d4ed6704b16f33 Mon Sep 17 00:00:00 2001 From: secretrobotron Date: Fri, 8 Aug 2014 15:18:09 -0400 Subject: [PATCH 1/4] [Issue249] Support for ArrayBuffer in Filer.Buffer 1. Added proxy Buffer constructor for Filer.Buffer. 2. Added tests for new Filer.Buffer. --- src/buffer.js | 18 ++++++++++++++++++ tests/bugs/issue249.js | 14 ++++++++++++++ tests/index.js | 1 + 3 files changed, 33 insertions(+) create mode 100644 src/buffer.js create mode 100644 tests/bugs/issue249.js 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"); From eb79a956ea80cd329c0a0f3bf8fbf2385db4be7c Mon Sep 17 00:00:00 2001 From: secretrobotron Date: Fri, 8 Aug 2014 15:31:23 -0400 Subject: [PATCH 2/4] [issue249] forgot the glue Need to expose FilerBuffer in place of Buffer on Filer namespace. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 35e55b4..f911644 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ module.exports = { FileSystem: require('./filesystem/interface.js'), - Buffer: Buffer, + Buffer: require('./buffer.js'), Path: require('./path.js'), Errors: require('./errors.js') }; From 77e0ee7117064c81dfe54d3eb46c0bec38d74073 Mon Sep 17 00:00:00 2001 From: secretrobotron Date: Sat, 9 Aug 2014 13:05:15 -0400 Subject: [PATCH 3/4] [issue249] fixed Buffer prototype & tests --- src/buffer.js | 5 +++++ tests/bugs/issue249.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/buffer.js b/src/buffer.js index 1756aee..bf8745e 100644 --- a/src/buffer.js +++ b/src/buffer.js @@ -9,6 +9,11 @@ function FilerBuffer (subject, encoding, nonZero) { 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]; diff --git a/tests/bugs/issue249.js b/tests/bugs/issue249.js index 28776d4..cdb2d3a 100644 --- a/tests/bugs/issue249.js +++ b/tests/bugs/issue249.js @@ -12,3 +12,31 @@ describe('Filer.Buffer should accept initialized ArrayBuffers, issue 249', funct 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(); + }); +}); + From 65ddff6ae774618c3c8299284f0cc5d35d68157d Mon Sep 17 00:00:00 2001 From: Bobby Richter Date: Thu, 14 Aug 2014 15:43:22 -0700 Subject: [PATCH 4/4] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 92595b9..c5929b7 100644 --- a/README.md +++ b/README.md @@ -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`. 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