mirror of
https://github.com/zebrajr/node.git
synced 2025-12-07 12:20:50 +01:00
Several changes:
* Soft-Deprecate Buffer() constructors
* Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
* Add `--zero-fill-buffers` command line option
* Add byteOffset and length to `new Buffer(arrayBuffer)` constructor
* buffer.fill('') previously had no effect, now zero-fills
* Update the docs
PR-URL: https://github.com/nodejs/node/pull/4682
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
28 lines
850 B
JavaScript
28 lines
850 B
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const path = require('path');
|
|
const Buffer = require('buffer').Buffer;
|
|
const fs = require('fs');
|
|
const filepath = path.join(common.fixturesDir, 'x.txt');
|
|
const fd = fs.openSync(filepath, 'r');
|
|
const expected = 'xyz\n';
|
|
const bufferAsync = Buffer.allocUnsafe(expected.length);
|
|
const bufferSync = Buffer.allocUnsafe(expected.length);
|
|
let readCalled = 0;
|
|
|
|
fs.read(fd, bufferAsync, 0, expected.length, 0, function(err, bytesRead) {
|
|
readCalled++;
|
|
|
|
assert.equal(bytesRead, expected.length);
|
|
assert.deepEqual(bufferAsync, Buffer.from(expected));
|
|
});
|
|
|
|
var r = fs.readSync(fd, bufferSync, 0, expected.length, 0);
|
|
assert.deepEqual(bufferSync, Buffer.from(expected));
|
|
assert.equal(r, expected.length);
|
|
|
|
process.on('exit', function() {
|
|
assert.equal(readCalled, 1);
|
|
});
|