Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit 4bf1d10

Browse files
isaacsindutny
authored andcommitted
crypto: LazyTransform on properties, not methods
It needs to apply the Transform class when the _readableState, _writableState, or _transformState properties are accessed, otherwise things like setEncoding and on('data') don't work properly. Also, the methods wrappers are no longer needed, since they're only problematic because they access the undefined properties.
1 parent c4379a5 commit 4bf1d10

File tree

2 files changed

+63
-13
lines changed

2 files changed

+63
-13
lines changed

lib/crypto.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -155,19 +155,27 @@ function LazyTransform(options) {
155155
}
156156
util.inherits(LazyTransform, stream.Transform);
157157

158-
var transformMethods = ['read', 'write', 'end', 'pipe', 'unpipe',
159-
'setEncoding', 'pause', 'resume'];
160-
161-
transformMethods.forEach(function(action, i, actions) {
162-
LazyTransform.prototype[action] = function() {
163-
stream.Transform.call(this, this._options);
164-
165-
actions.forEach(function(action) {
166-
this[action] = stream.Transform.prototype[action];
167-
}, this);
168-
169-
return this[action].apply(this, arguments);
170-
};
158+
[
159+
'_readableState',
160+
'_writableState',
161+
'_transformState'
162+
].forEach(function(prop, i, props) {
163+
Object.defineProperty(LazyTransform.prototype, prop, {
164+
get: function() {
165+
stream.Transform.call(this, this._options);
166+
return this[prop];
167+
},
168+
set: function(val) {
169+
Object.defineProperty(this, prop, {
170+
value: val,
171+
enumerable: true,
172+
configurable: true,
173+
writable: true
174+
});
175+
},
176+
configurable: true,
177+
enumerable: true
178+
});
171179
});
172180

173181

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
var crypto = require('crypto');
26+
var stream = require('stream')
27+
var s = new stream.PassThrough();
28+
var h = crypto.createHash('sha1');
29+
var expect = '15987e60950cf22655b9323bc1e281f9c4aff47e';
30+
var gotData = false;
31+
32+
process.on('exit', function() {
33+
assert(gotData);
34+
console.log('ok');
35+
});
36+
37+
s.pipe(h).on('data', function(c) {
38+
assert.equal(c, expect);
39+
gotData = true;
40+
}).setEncoding('hex');
41+
42+
s.end('aoeu');

0 commit comments

Comments
 (0)