From 923b999e89977a5bf448be96068a67f5be3a3c45 Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Sat, 1 Dec 2018 19:57:09 -0500 Subject: [PATCH] Fix #547: update Buffer use, docs, tests to match node.js v11.3.0 --- README.md | 37 ++++++++++++++---- src/buffer.js | 23 ----------- src/encoding.js | 13 +------ src/filesystem/implementation.js | 20 ++++------ src/index.js | 2 +- src/providers/indexeddb.js | 3 +- src/providers/websql.js | 3 +- tests/bugs/issue249.js | 6 +-- tests/index.js | 1 + tests/spec/filer.buffer.spec.js | 49 ++++++++++++++++++++++++ tests/spec/fs.appendFile.spec.js | 6 +-- tests/spec/fs.close.spec.js | 3 +- tests/spec/fs.ftruncate.spec.js | 14 +++---- tests/spec/fs.lseek.spec.js | 22 +++++------ tests/spec/fs.read.spec.js | 20 ++++------ tests/spec/fs.truncate.spec.js | 14 +++---- tests/spec/fs.write.spec.js | 5 +-- tests/spec/fs.writeFile-readFile.spec.js | 4 +- tests/spec/providers/providers.base.js | 8 ++-- tests/spec/times.spec.js | 9 ++--- 20 files changed, 139 insertions(+), 123 deletions(-) delete mode 100644 src/buffer.js create mode 100644 tests/spec/filer.buffer.spec.js diff --git a/README.md b/README.md index f9f8758..925a53e 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,28 @@ 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. Additionally, unlike native `Buffer`, `Filer.Buffer`'s constructor can accept `ArrayBuffer` objects, which will be interpreted as `Uint8Array`s. +methods are added to the instance). See https://github.com/feross/buffer for more details. + +NOTE: `Filer.Buffer` currently includes the older, deprecated [constructor functions](https://nodejs.org/api/buffer.html#buffer_new_buffer_array), but these will be removed +at some point. You are encouraged to switch to use the newer class methods `Buffer.from()` +and `Buffer.alloc()`. See the [node.js Buffer docs](https://nodejs.org/api/buffer.html). + +```js +/* Deprecated - see https://nodejs.org/api/buffer.html#buffer_new_buffer_array */ +new Buffer(array) +new Buffer(arrayBuffer[, byteOffset[, length]]) +new Buffer(buffer) +new Buffer(string[, encoding]) +new Buffer(size) + +/* Use Instead */ +Buffer.from(array) +Buffer.from(arrayBuffer[, byteOffset[, length]]) +Buffer.from(buffer) +Buffer.from(string[, encoding]) +Buffer.alloc(size) +Buffer.allocUnsafe(size) +``` #### Filer.Path @@ -357,7 +378,7 @@ Example: ```javascript // Create a file, shrink it, expand it. -var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); +var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(err, fd) { if(err) throw error; @@ -388,7 +409,7 @@ Example: ```javascript // Create a file, shrink it, expand it. -var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); +var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(err, fd) { if(err) throw error; @@ -905,7 +926,7 @@ Example: ```javascript // Create a file with the following bytes. -var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); +var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(err, fd) { if(err) throw error; @@ -950,7 +971,7 @@ fs.open('/myfile', 'r', function(err, fd) { // Create a buffer large enough to hold the file's contents var nbytes = expected = stats.size; - var buffer = new Filer.Buffer(nbytes); + var buffer = Filer.Buffer.alloc(nbytes); var read = 0; function readBytes(offset, position, length) { @@ -1008,7 +1029,7 @@ fs.writeFile('/myfile.txt', "...data...", function (err) { }); // Write binary file -var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); +var buffer = Filer.Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.writeFile('/myfile', buffer, function (err) { if (err) throw err; }); @@ -1031,8 +1052,8 @@ fs.appendFile('/myfile.txt', "Data...", function (err) { // '/myfile.txt' would now read out 'More...Data...' // Append binary file -var data = new Filer.Buffer([1, 2, 3, 4]); -var more = new Filer.Buffer([5, 6, 7, 8]); +var data = Filer.Buffer.from([1, 2, 3, 4]); +var more = Filer.Buffer.from([5, 6, 7, 8]); fs.writeFile('/myfile', data, function (err) { if (err) throw err; diff --git a/src/buffer.js b/src/buffer.js deleted file mode 100644 index 6286d33..0000000 --- a/src/buffer.js +++ /dev/null @@ -1,23 +0,0 @@ -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; diff --git a/src/encoding.js b/src/encoding.js index b3f1031..45feceb 100644 --- a/src/encoding.js +++ b/src/encoding.js @@ -1,13 +1,4 @@ -// Adapt encodings to work with Buffer or Uint8Array, they expect the latter -function decode(buf) { - return buf.toString('utf8'); -} - -function encode(string) { - return new Buffer(string, 'utf8'); -} - module.exports = { - encode: encode, - decode: decode + encode: string => Buffer.from(string), + decode: buffer => buffer.toString('utf8') }; diff --git a/src/filesystem/implementation.js b/src/filesystem/implementation.js index 87b095d..fee85b8 100644 --- a/src/filesystem/implementation.js +++ b/src/filesystem/implementation.js @@ -36,7 +36,6 @@ var OpenFileDescription = require('../open-file-description.js'); var SuperNode = require('../super-node.js'); var Node = require('../node.js'); var Stats = require('../stats.js'); -var Buffer = require('../buffer.js'); /** * Update node times. Only passed times are modified (undefined times are ignored) @@ -672,8 +671,7 @@ function open_file(context, path, flags, mode, callback) { if(error) { callback(error); } else { - fileData = new Buffer(0); - fileData.fill(0); + fileData = Buffer.alloc(0); context.putBuffer(fileNode.data, fileData, update_directory_data); } } @@ -738,8 +736,8 @@ function replace_data(context, ofd, buffer, offset, length, callback) { callback(error); } else { fileNode = result; - var newData = new Buffer(length); - newData.fill(0); + + var newData = Buffer.alloc(length); buffer.copy(newData, 0, offset, offset + length); ofd.position = length; @@ -792,8 +790,7 @@ function write_data(context, ofd, buffer, offset, length, position, callback) { } var _position = (!(undefined === position || null === position)) ? position : ofd.position; var newSize = Math.max(fileData.length, _position + length); - var newData = new Buffer(newSize); - newData.fill(0); + var newData = Buffer.alloc(newSize); if(fileData) { fileData.copy(newData); } @@ -1268,8 +1265,7 @@ function truncate_file(context, path, length, callback) { if(!fileData) { return callback(new Errors.EIO('Expected Buffer')); } - var data = new Buffer(length); - data.fill(0); + var data = Buffer.alloc(length); if(fileData) { fileData.copy(data); } @@ -1328,8 +1324,7 @@ function ftruncate_file(context, ofd, length, callback) { if(fileData) { data = fileData.slice(0, length); } else { - data = new Buffer(length); - data.fill(0); + data = Buffer.alloc(length); } context.putBuffer(fileNode.data, data, update_file_node); } @@ -1802,8 +1797,7 @@ function readFile(fs, context, path, options, callback) { } var size = stats.size; - var buffer = new Buffer(size); - buffer.fill(0); + var buffer = Buffer.alloc(size); read_data(context, ofd, buffer, 0, size, 0, function(err) { cleanup(); diff --git a/src/index.js b/src/index.js index d8e9f31..dc54a5e 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ module.exports = { FileSystem: require('./filesystem/interface.js'), - Buffer: require('./buffer.js'), + Buffer: Buffer, Path: require('./path.js'), Errors: require('./errors.js'), Shell: require('./shell/shell.js') diff --git a/src/providers/indexeddb.js b/src/providers/indexeddb.js index b0fb412..db08eb9 100644 --- a/src/providers/indexeddb.js +++ b/src/providers/indexeddb.js @@ -2,7 +2,6 @@ var FILE_SYSTEM_NAME = require('../constants.js').FILE_SYSTEM_NAME; var FILE_STORE_NAME = require('../constants.js').FILE_STORE_NAME; var IDB_RW = require('../constants.js').IDB_RW; var IDB_RO = require('../constants.js').IDB_RO; -var FilerBuffer = require('../buffer.js'); var indexedDB = global.indexedDB || global.mozIndexedDB || @@ -64,7 +63,7 @@ IndexedDBContext.prototype.getBuffer = function(key, callback) { if(err) { return callback(err); } - callback(null, new FilerBuffer(arrayBuffer)); + callback(null, Buffer.from(arrayBuffer)); }); }; diff --git a/src/providers/websql.js b/src/providers/websql.js index 7219a0a..0a73bdb 100644 --- a/src/providers/websql.js +++ b/src/providers/websql.js @@ -4,7 +4,6 @@ var WSQL_VERSION = require('../constants.js').WSQL_VERSION; var WSQL_SIZE = require('../constants.js').WSQL_SIZE; var WSQL_DESC = require('../constants.js').WSQL_DESC; var Errors = require('../errors.js'); -var FilerBuffer = require('../buffer.js'); var base64ArrayBuffer = require('base64-arraybuffer'); function WebSQLContext(db, isReadOnly) { @@ -75,7 +74,7 @@ WebSQLContext.prototype.getBuffer = function(key, callback) { // Deal with zero-length ArrayBuffers, which will be encoded as '' if(result || result === '') { var arrayBuffer = base64ArrayBuffer.decode(result); - result = new FilerBuffer(arrayBuffer); + result = Buffer.from(arrayBuffer); } callback(null, result); diff --git a/tests/bugs/issue249.js b/tests/bugs/issue249.js index 4dbc901..01c7031 100644 --- a/tests/bugs/issue249.js +++ b/tests/bugs/issue249.js @@ -7,7 +7,7 @@ describe('Filer.Buffer should accept initialized ArrayBuffers, issue 249', funct afterEach(util.cleanup); it('should accept an ArrayBuffer with a specified size', function(done) { - var buffer = new Filer.Buffer(new ArrayBuffer(5)); + var buffer = Buffer.from(new ArrayBuffer(5)); expect(buffer.length).to.equal(5); done(); }); @@ -18,7 +18,7 @@ describe('Filer.Buffer static methods are in tact, issue 249', function() { afterEach(util.cleanup); it('should proxy Buffer.isBuffer', function(done) { - expect(Filer.Buffer.isBuffer(new Filer.Buffer([]))).to.equal(true); + expect(Filer.Buffer.isBuffer(Buffer.from([]))).to.equal(true); expect(Filer.Buffer.isBuffer('')).to.equal(false); done(); }); @@ -35,7 +35,7 @@ describe('Filer.Buffer static methods are in tact, issue 249', function() { }); it('should proxy Buffer.concat', function(done) { - expect(Filer.Buffer.concat([new Filer.Buffer(1), new Filer.Buffer(2)]).length).to.equal(3); + expect(Filer.Buffer.concat([Buffer.alloc(1), Buffer.alloc(2)]).length).to.equal(3); done(); }); }); diff --git a/tests/index.js b/tests/index.js index 3d053d4..a8b86dc 100644 --- a/tests/index.js +++ b/tests/index.js @@ -5,6 +5,7 @@ // Filer require('./spec/filer.spec'); +require('./spec/filer.buffer.spec.js'); // Filer.FileSystem.* require('./spec/filer.filesystem.spec'); diff --git a/tests/spec/filer.buffer.spec.js b/tests/spec/filer.buffer.spec.js new file mode 100644 index 0000000..45e6f85 --- /dev/null +++ b/tests/spec/filer.buffer.spec.js @@ -0,0 +1,49 @@ +var Filer = require('../../src'); +var expect = require('chai').expect; + +describe('Filer.Buffer', function() { + + it('should support .from()', function() { + expect(Filer.Buffer.from).to.be.a('function'); + }); + + it('should support .alloc()', function() { + expect(Filer.Buffer.alloc).to.be.a('function'); + }); + + it('should support .isBuffer()', function() { + var buf = Buffer.alloc(0); + expect(Buffer.isBuffer(buf)).to.be.true; + }); + + describe('Deprecation checks - constructor vs. class method init', function() { + + it('should allow new Buffer(array)', function() { + var arr = [1, 2, 3]; + var buf1 = new Buffer(arr); + var buf2 = new Buffer.from(arr); + expect(buf1).to.deep.equal(buf2); + }); + + it('should allow new Buffer(ArrayBuffer)', function() { + var arrayBuffer = (new Uint8Array([1, 2, 3])).buffer; + var buf1 = new Buffer(arrayBuffer); + var buf2 = Buffer.from(arrayBuffer); + expect(buf1).to.deep.equal(buf2); + }); + + it('should allow new Buffer(ArrayBuffer)', function() { + var buffer = new Buffer.from([1, 2, 3]); + var buf1 = new Buffer(buffer); + var buf2 = Buffer.from(buffer); + expect(buf1).to.deep.equal(buf2); + }); + + it('should allow new Buffer(string)', function() { + var s = 'Hello World'; + var buf1 = new Buffer(s); + var buf2 = Buffer.from(s); + expect(buf1).to.deep.equal(buf2); + }); + }); +}); diff --git a/tests/spec/fs.appendFile.spec.js b/tests/spec/fs.appendFile.spec.js index 7fb3aa9..f153dc3 100644 --- a/tests/spec/fs.appendFile.spec.js +++ b/tests/spec/fs.appendFile.spec.js @@ -70,9 +70,9 @@ describe('fs.appendFile', function() { var fs = util.fs(); // String and utf8 binary encoded versions of the same thing: 'This is a file.' - var binary = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); - var binary2 = new Buffer([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]); - var binary3 = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46, + var binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); + var binary2 = Buffer.from([32, 65, 112, 112, 101, 110, 100, 101, 100, 46]); + var binary3 = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46, 32, 65, 112, 112, 101, 110, 100, 101, 100, 46]); fs.writeFile('/mybinaryfile', binary, function(error) { diff --git a/tests/spec/fs.close.spec.js b/tests/spec/fs.close.spec.js index b050669..0a94992 100644 --- a/tests/spec/fs.close.spec.js +++ b/tests/spec/fs.close.spec.js @@ -1,4 +1,3 @@ -var Filer = require('../../src'); var util = require('../lib/test-utils.js'); var expect = require('chai').expect; @@ -12,7 +11,7 @@ describe('fs.close', function() { }); it('should release the file descriptor', function(done) { - var buffer = new Filer.Buffer(0); + var buffer = Buffer.alloc(0); var fs = util.fs(); fs.open('/myfile', 'w+', function(error, result) { diff --git a/tests/spec/fs.ftruncate.spec.js b/tests/spec/fs.ftruncate.spec.js index 5bef131..9f6726d 100644 --- a/tests/spec/fs.ftruncate.spec.js +++ b/tests/spec/fs.ftruncate.spec.js @@ -44,8 +44,8 @@ describe('fs.ftruncate', function() { it('should truncate a file', function(done) { let fs = util.fs(); - let buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - let truncated = new Buffer([1, 2]); + let buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + let truncated = Buffer.from([1, 2]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -76,7 +76,7 @@ describe('fs.ftruncate', function() { it('should truncate a valid descriptor', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -102,8 +102,8 @@ describe('fs.ftruncate', function() { it('should pad a file with zeros when the length is greater than the file size', function(done) { let fs = util.fs(); - let buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - let truncated = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 0]); + let buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + let truncated = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 0]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -133,7 +133,7 @@ describe('fs.ftruncate', function() { it('should assume a length of 0 if passed undefined', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -163,7 +163,7 @@ describe('fs.ftruncate', function() { it('should update the file size', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; diff --git a/tests/spec/fs.lseek.spec.js b/tests/spec/fs.lseek.spec.js index 84d5c1c..4d8f9ce 100644 --- a/tests/spec/fs.lseek.spec.js +++ b/tests/spec/fs.lseek.spec.js @@ -1,4 +1,3 @@ -var Filer = require('../../src'); var util = require('../lib/test-utils.js'); var expect = require('chai').expect; @@ -50,9 +49,8 @@ describe('fs.lseek', function() { it('should set the current position if whence is SET', function(done) { var fs = util.fs(); var offset = 3; - var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var result_buffer = new Filer.Buffer(buffer.length + offset); - result_buffer.fill(0); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + var result_buffer = Buffer.alloc(buffer.length + offset); fs.open('/myfile', 'w+', function(error, fd) { if(error) throw error; @@ -77,7 +75,7 @@ describe('fs.lseek', function() { if(error) throw error; expect(result.size).to.equal(offset + buffer.length); - var expected = new Filer.Buffer([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]); + var expected = Buffer.from([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]); expect(result_buffer).to.deep.equal(expected); done(); }); @@ -91,9 +89,8 @@ describe('fs.lseek', function() { it('should update the current position if whence is CUR', function(done) { var fs = util.fs(); var offset = -2; - var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var result_buffer = new Filer.Buffer(2 * buffer.length + offset); - result_buffer.fill(0); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + var result_buffer = Buffer.alloc(2 * buffer.length + offset); fs.open('/myfile', 'w+', function(error, fd) { if(error) throw error; @@ -118,7 +115,7 @@ describe('fs.lseek', function() { if(error) throw error; expect(result.size).to.equal(offset + 2 * buffer.length); - var expected = new Filer.Buffer([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]); + var expected = Buffer.from([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]); expect(result_buffer).to.deep.equal(expected); done(); }); @@ -132,7 +129,7 @@ describe('fs.lseek', function() { it('should update the current position if whence is END', function(done) { var fs = util.fs(); var offset = 5; - var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); var result_buffer; fs.open('/myfile', 'w+', function(error, result) { @@ -159,13 +156,12 @@ describe('fs.lseek', function() { if(error) throw error; expect(result.size).to.equal(offset + 2 * buffer.length); - result_buffer = new Filer.Buffer(result.size); - result_buffer.fill(0); + result_buffer = Buffer.alloc(result.size); fs.read(fd2, result_buffer, 0, result_buffer.length, 0, function(error, result) { if(error) throw error; expect(result).to.equal(result_buffer.length); - var expected = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]); + var expected = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]); expect(result_buffer).to.deep.equal(expected); done(); }); diff --git a/tests/spec/fs.read.spec.js b/tests/spec/fs.read.spec.js index 991db44..216c3bc 100644 --- a/tests/spec/fs.read.spec.js +++ b/tests/spec/fs.read.spec.js @@ -1,4 +1,3 @@ -var Filer = require('../../src'); var util = require('../lib/test-utils.js'); var expect = require('chai').expect; @@ -13,9 +12,8 @@ describe('fs.read', function() { it('should read data from a file', function(done) { var fs = util.fs(); - var wbuffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var rbuffer = new Filer.Buffer(wbuffer.length); - rbuffer.fill(0); + var wbuffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + var rbuffer = Buffer.alloc(wbuffer.length); fs.open('/myfile', 'w+', function(error, fd) { if(error) throw error; @@ -35,9 +33,8 @@ describe('fs.read', function() { it('should update the current file position', function(done) { var fs = util.fs(); - var wbuffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var rbuffer = new Filer.Buffer(wbuffer.length); - rbuffer.fill(0); + var wbuffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + var rbuffer = Buffer.alloc(wbuffer.length); var _result = 0; fs.open('/myfile', 'w+', function(error, fd) { @@ -66,10 +63,8 @@ describe('fs.read', function() { it('should fail to read a directory', function(done) { var fs = util.fs(); - var buf = new Filer.Buffer(20); - var buf2 = new Filer.Buffer(20); - buf.fill(0); - buf2.fill(0); + var buf = Buffer.alloc(20); + var buf2 = Buffer.alloc(20); fs.mkdir('/mydir', function(error) { if(error) throw error; @@ -92,8 +87,7 @@ describe('fs.read', function() { var fs = util.fs(); var fd = 0; - var rbuffer = new Filer.Buffer(8); - rbuffer.fill(0); + var rbuffer = Buffer.alloc(8); fs.read(fd, rbuffer, 0, rbuffer.length, 0, function(error, result) { expect(error).to.exist; diff --git a/tests/spec/fs.truncate.spec.js b/tests/spec/fs.truncate.spec.js index 1bc7550..44fda48 100644 --- a/tests/spec/fs.truncate.spec.js +++ b/tests/spec/fs.truncate.spec.js @@ -52,8 +52,8 @@ describe('fs.truncate', function() { it('should truncate a file', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var truncated = new Buffer([1]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + var truncated = Buffer.from([1]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -83,8 +83,8 @@ describe('fs.truncate', function() { it('should pad a file with zeros when the length is greater than the file size', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); - var truncated = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 0]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); + var truncated = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 0]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -114,7 +114,7 @@ describe('fs.truncate', function() { it('should update the file size', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -144,7 +144,7 @@ describe('fs.truncate', function() { it('should assume a length of 0 if passed undefined', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; @@ -175,7 +175,7 @@ describe('fs.truncate', function() { it('should follow symbolic links', function(done) { var fs = util.fs(); - var buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(error, result) { if(error) throw error; diff --git a/tests/spec/fs.write.spec.js b/tests/spec/fs.write.spec.js index 5976995..63658ec 100644 --- a/tests/spec/fs.write.spec.js +++ b/tests/spec/fs.write.spec.js @@ -1,4 +1,3 @@ -var Filer = require('../../src'); var util = require('../lib/test-utils.js'); var expect = require('chai').expect; @@ -13,7 +12,7 @@ describe('fs.write', function() { it('should write data to a file', function(done) { var fs = util.fs(); - var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); fs.open('/myfile', 'w', function(error, fd) { if(error) throw error; @@ -34,7 +33,7 @@ describe('fs.write', function() { it('should update the current file position', function(done) { var fs = util.fs(); - var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); var _result = 0; fs.open('/myfile', 'w', function(error, fd) { diff --git a/tests/spec/fs.writeFile-readFile.spec.js b/tests/spec/fs.writeFile-readFile.spec.js index 4f03fe9..cc48fe1 100644 --- a/tests/spec/fs.writeFile-readFile.spec.js +++ b/tests/spec/fs.writeFile-readFile.spec.js @@ -80,7 +80,7 @@ describe('fs.writeFile, fs.readFile', function() { it('should write, read a binary file', function(done) { var fs = util.fs(); // String and utf8 binary encoded versions of the same thing: 'This is a file.' - var binary = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); + var binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); fs.writeFile('/myfile', binary, function(error) { if(error) throw error; @@ -180,7 +180,7 @@ describe('fsPromises.writeFile, fsPromises.readFile', function() { it('should write, read a binary file', function() { var fsPromises = util.fs().promises; // String and utf8 binary encoded versions of the same thing: 'This is a file.' - var binary = new Buffer([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); + var binary = Buffer.from([84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 102, 105, 108, 101, 46]); return fsPromises.writeFile('/myfile', binary) .then( () => fsPromises.readFile('/myfile')) diff --git a/tests/spec/providers/providers.base.js b/tests/spec/providers/providers.base.js index 91248ce..32e1fef 100644 --- a/tests/spec/providers/providers.base.js +++ b/tests/spec/providers/providers.base.js @@ -1,6 +1,5 @@ /* eslint no-console: 0 */ -var Buffer = require('../../../src').Buffer; var util = require('../../lib/test-utils.js'); var expect = require('chai').expect; @@ -77,8 +76,7 @@ module.exports = function createProviderTestsFor(providerName, testProvider) { if(error) throw error; var context = provider.getReadWriteContext(); - // Filer Buffer - var buf = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); + var buf = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); context.putBuffer('key', buf, function(error) { if(error) throw error; @@ -97,8 +95,8 @@ module.exports = function createProviderTestsFor(providerName, testProvider) { if(error) throw error; var context = provider.getReadWriteContext(); - // Zero-length Filer Buffer - var buf = new Buffer(new ArrayBuffer(0)); + // Zero-length Buffer + var buf = Buffer.alloc(0); context.putBuffer('key', buf, function(error) { if(error) throw error; diff --git a/tests/spec/times.spec.js b/tests/spec/times.spec.js index d25d86a..73bab19 100644 --- a/tests/spec/times.spec.js +++ b/tests/spec/times.spec.js @@ -1,4 +1,3 @@ -var Filer = require('../../src'); var util = require('../lib/test-utils.js'); var expect = require('chai').expect; @@ -355,7 +354,7 @@ describe('node times (atime, mtime, ctime)', function() { it('should update atime, ctime, mtime when calling fs.write()', function(done) { var fs = util.fs(); - var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); createTree(function() { fs.open('/myfile', 'w', function(error, fd) { @@ -384,7 +383,7 @@ describe('node times (atime, mtime, ctime)', function() { it('should make no change when calling fs.read()', function(done) { var fs = util.fs(); - var buffer = new Filer.Buffer([1, 2, 3, 4, 5, 6, 7, 8]); + var buffer = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]); createTree(function() { fs.open('/myfile', 'w', function(err, fd) { @@ -401,8 +400,8 @@ describe('node times (atime, mtime, ctime)', function() { if(error) throw error; stat('/myfile', function(stats1) { - var buffer2 = new Filer.Buffer(buffer.length); - buffer2.fill(0); + var buffer2 = Buffer.alloc(buffer.length); + fs.read(fd, buffer2, 0, buffer2.length, 0, function(err, nbytes) { if(err) throw err; expect(nbytes).to.equal(buffer2.length);