Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9e7093f

Browse files
stream: validate writable defaultEncoding
PR-URL:#46322Fixes:#46301Reviewed-By: Robert Nagy <ronagy@icloud.com>Reviewed-By: Matteo Collina <matteo.collina@gmail.com>Reviewed-By: Paolo Insogna <paolo@cowtech.it>Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parenta5fd53f commit9e7093f

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

‎lib/internal/streams/writable.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,14 @@ function WritableState(options, stream, isDuplex) {
122122
// Crypto is kind of old and crusty. Historically, its default string
123123
// encoding is 'binary' so we have to make this configurable.
124124
// Everything else in the universe uses 'utf8', though.
125-
this.defaultEncoding=(options&&options.defaultEncoding)||'utf8';
125+
constdefaultEncoding=options?.defaultEncoding;
126+
if(defaultEncoding==null){
127+
this.defaultEncoding='utf8';
128+
}elseif(Buffer.isEncoding(defaultEncoding)){
129+
this.defaultEncoding=defaultEncoding;
130+
}else{
131+
thrownewERR_UNKNOWN_ENCODING(defaultEncoding);
132+
}
126133

127134
// Not an actual buffer we keep track of, but a measurement
128135
// of how much we're waiting to get pushed to some underlying

‎test/parallel/test-stream-writable-decoded-encoding.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,50 @@ class MyWritable extends stream.Writable {
5656
m.write('some-text','utf8');
5757
m.end();
5858
}
59+
60+
{
61+
assert.throws(()=>{
62+
constm=newMyWritable(null,{
63+
defaultEncoding:'my invalid encoding',
64+
});
65+
m.end();
66+
},{
67+
code:'ERR_UNKNOWN_ENCODING',
68+
});
69+
}
70+
71+
{
72+
constw=newMyWritable(function(isBuffer,type,enc){
73+
assert(!isBuffer);
74+
assert.strictEqual(type,'string');
75+
assert.strictEqual(enc,'hex');
76+
},{
77+
defaultEncoding:'hex',
78+
decodeStrings:false
79+
});
80+
w.write('asd');
81+
w.end();
82+
}
83+
84+
{
85+
constw=newMyWritable(function(isBuffer,type,enc){
86+
assert(!isBuffer);
87+
assert.strictEqual(type,'string');
88+
assert.strictEqual(enc,'utf8');
89+
},{
90+
defaultEncoding:null,
91+
decodeStrings:false
92+
});
93+
w.write('asd');
94+
w.end();
95+
}
96+
97+
{
98+
constm=newMyWritable(function(isBuffer,type,enc){
99+
assert.strictEqual(type,'object');
100+
assert.strictEqual(enc,'utf8');
101+
},{defaultEncoding:'hex',
102+
objectMode:true});
103+
m.write({foo:'bar'},'utf8');
104+
m.end();
105+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp