node/test/parallel/test-stream2-pipe-error-handling.js
James M Snell 85ab4a5f12 buffer: add .from(), .alloc() and .allocUnsafe()
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>
2016-03-16 08:34:02 -07:00

86 lines
1.8 KiB
JavaScript

'use strict';
require('../common');
var assert = require('assert');
var stream = require('stream');
(function testErrorListenerCatches() {
var count = 1000;
var source = new stream.Readable();
source._read = function(n) {
n = Math.min(count, n);
count -= n;
source.push(Buffer.allocUnsafe(n));
};
var unpipedDest;
source.unpipe = function(dest) {
unpipedDest = dest;
stream.Readable.prototype.unpipe.call(this, dest);
};
var dest = new stream.Writable();
dest._write = function(chunk, encoding, cb) {
cb();
};
source.pipe(dest);
var gotErr = null;
dest.on('error', function(err) {
gotErr = err;
});
var unpipedSource;
dest.on('unpipe', function(src) {
unpipedSource = src;
});
var err = new Error('This stream turned into bacon.');
dest.emit('error', err);
assert.strictEqual(gotErr, err);
assert.strictEqual(unpipedSource, source);
assert.strictEqual(unpipedDest, dest);
})();
(function testErrorWithoutListenerThrows() {
var count = 1000;
var source = new stream.Readable();
source._read = function(n) {
n = Math.min(count, n);
count -= n;
source.push(Buffer.allocUnsafe(n));
};
var unpipedDest;
source.unpipe = function(dest) {
unpipedDest = dest;
stream.Readable.prototype.unpipe.call(this, dest);
};
var dest = new stream.Writable();
dest._write = function(chunk, encoding, cb) {
cb();
};
source.pipe(dest);
var unpipedSource;
dest.on('unpipe', function(src) {
unpipedSource = src;
});
var err = new Error('This stream turned into bacon.');
var gotErr = null;
try {
dest.emit('error', err);
} catch (e) {
gotErr = e;
}
assert.strictEqual(gotErr, err);
assert.strictEqual(unpipedSource, source);
assert.strictEqual(unpipedDest, dest);
})();