| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 85f9b27 commit 63eca7f
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,6 +65,7 @@ const { | |||
| 65 | 65 | ERR_OUT_OF_RANGE, | |
| 66 | 66 | ERR_STREAM_PUSH_AFTER_EOF, | |
| 67 | 67 | ERR_STREAM_UNSHIFT_AFTER_END_EVENT, | |
| 68 | + ERR_UNKNOWN_ENCODING | ||
| 68 | 69 | } | |
| 69 | 70 | } = require('internal/errors'); | |
| 70 | 71 | const { validateObject } = require('internal/validators'); | |
@@ -162,7 +163,14 @@ function ReadableState(options, stream, isDuplex) { | |||
| 162 | 163 | // Crypto is kind of old and crusty. Historically, its default string | |
| 163 | 164 | // encoding is 'binary' so we have to make this configurable. | |
| 164 | 165 | // Everything else in the universe uses 'utf8', though. | |
| 165 | - this.defaultEncoding = (options && options.defaultEncoding) || 'utf8'; | ||
| 166 | + const defaultEncoding = options?.defaultEncoding; | ||
| 167 | + if (defaultEncoding == null) { | ||
| 168 | + this.defaultEncoding = 'utf8'; | ||
| 169 | + } else if (Buffer.isEncoding(defaultEncoding)) { | ||
| 170 | + this.defaultEncoding = defaultEncoding; | ||
| 171 | + } else { | ||
| 172 | + throw new ERR_UNKNOWN_ENCODING(defaultEncoding); | ||
| 173 | + } | ||
| 166 | 174 | ||
| 167 | 175 | // Ref the piped dest which we need a drain event on it | |
| 168 | 176 | // type: null | Writable | Set<Writable>. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,37 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const { Readable } = require('stream'); | ||
| 5 | + | ||
| 6 | + { | ||
| 7 | + assert.throws(() => { | ||
| 8 | + new Readable({ | ||
| 9 | + read: () => {}, | ||
| 10 | + defaultEncoding: 'my invalid encoding', | ||
| 11 | + }); | ||
| 12 | + }, { | ||
| 13 | + code: 'ERR_UNKNOWN_ENCODING', | ||
| 14 | + }); | ||
| 15 | + } | ||
| 16 | + | ||
| 17 | + { | ||
| 18 | + const r = new Readable({ | ||
| 19 | + read() {}, | ||
| 20 | + defaultEncoding: 'hex' | ||
| 21 | + }); | ||
| 22 | + | ||
| 23 | + r.push('ab'); | ||
| 24 | + | ||
| 25 | + r.on('data', common.mustCall((chunk) => assert.strictEqual(chunk.toString('hex'), 'ab')), 1); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + { | ||
| 29 | + const r = new Readable({ | ||
| 30 | + read() {}, | ||
| 31 | + defaultEncoding: 'hex', | ||
| 32 | + }); | ||
| 33 | + | ||
| 34 | + r.push('xy', 'utf-8'); | ||
| 35 | + | ||
| 36 | + r.on('data', common.mustCall((chunk) => assert.strictEqual(chunk.toString('utf-8'), 'xy')), 1); | ||
| 37 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments