node/test/parallel/test-stream-transform-constructor-set-methods.js
Calvin Metcalf 07c7f198db stream: add final method
Adds the ability to for write streams to have an _final method which acts
similarly to the _flush method that transform streams have but is called before
the finish event is emitted and if asynchronous delays the stream from
finishing.  The `final` option may also be passed in order to set it.

PR-URL: https://github.com/nodejs/node/pull/12828
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
2017-05-28 07:00:40 -07:00

40 lines
767 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const Transform = require('stream').Transform;
const _transform = common.mustCall(function _transform(d, e, n) {
n();
});
const _final = common.mustCall(function _final(n) {
n();
});
const _flush = common.mustCall(function _flush(n) {
n();
});
const t = new Transform({
transform: _transform,
flush: _flush,
final: _final
});
const t2 = new Transform({});
t.end(Buffer.from('blerg'));
t.resume();
assert.throws(() => {
t2.end(Buffer.from('blerg'));
}, /^Error: _transform\(\) is not implemented$/);
process.on('exit', () => {
assert.strictEqual(t._transform, _transform);
assert.strictEqual(t._flush, _flush);
assert.strictEqual(t._final, _final);
});