@@ -56,47 +56,61 @@ for (var i = 0; i < encodings.length; ++i) |
56 | 56 | // StringDecoder provides an interface for efficiently splitting a series of |
57 | 57 | // buffers into a series of JS strings without breaking apart multi-byte |
58 | 58 | // characters. |
59 | | -class StringDecoder { |
60 | | - constructor(encoding) { |
61 | | - this.encoding = normalizeEncoding(encoding); |
62 | | - this[kNativeDecoder] = Buffer.alloc(kSize); |
63 | | - this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding]; |
64 | | - } |
65 | | - |
66 | | - write(buf) { |
67 | | - if (typeof buf === 'string') |
68 | | - return buf; |
69 | | - if (!ArrayBuffer.isView(buf)) |
70 | | - throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buf', |
71 | | - ['Buffer', 'Uint8Array', 'ArrayBufferView']); |
72 | | - return decode(this[kNativeDecoder], buf); |
73 | | - } |
74 | | - |
75 | | - end(buf) { |
76 | | - let ret = ''; |
77 | | - if (buf !== undefined) |
78 | | - ret = this.write(buf); |
79 | | - if (this[kNativeDecoder][kBufferedBytes] > 0) |
80 | | - ret += flush(this[kNativeDecoder]); |
81 | | - return ret; |
82 | | - } |
| 59 | +function StringDecoder(encoding) { |
| 60 | + this.encoding = normalizeEncoding(encoding); |
| 61 | + this[kNativeDecoder] = Buffer.alloc(kSize); |
| 62 | + this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding]; |
| 63 | +} |
83 | 64 |
|
84 | | - /* Everything below this line is undocumented legacy stuff. */ |
| 65 | +StringDecoder.prototype.write = function write(buf) { |
| 66 | + if (typeof buf === 'string') |
| 67 | + return buf; |
| 68 | + if (!ArrayBuffer.isView(buf)) |
| 69 | + throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buf', |
| 70 | + ['Buffer', 'Uint8Array', 'ArrayBufferView']); |
| 71 | + return decode(this[kNativeDecoder], buf); |
| 72 | +}; |
85 | 73 |
|
86 | | - text(buf, offset) { |
87 | | - this[kNativeDecoder][kMissingBytes] = 0; |
88 | | - this[kNativeDecoder][kBufferedBytes] = 0; |
89 | | - return this.write(buf.slice(offset)); |
90 | | - } |
| 74 | +StringDecoder.prototype.end = function end(buf) { |
| 75 | + let ret = ''; |
| 76 | + if (buf !== undefined) |
| 77 | + ret = this.write(buf); |
| 78 | + if (this[kNativeDecoder][kBufferedBytes] > 0) |
| 79 | + ret += flush(this[kNativeDecoder]); |
| 80 | + return ret; |
| 81 | +}; |
91 | 82 |
|
92 | | - get lastTotal() { |
93 | | - return this[kNativeDecoder][kBufferedBytes] + this.lastNeed; |
94 | | - } |
| 83 | +/* Everything below this line is undocumented legacy stuff. */ |
| 84 | +StringDecoder.prototype.text = function text(buf, offset) { |
| 85 | + this[kNativeDecoder][kMissingBytes] = 0; |
| 86 | + this[kNativeDecoder][kBufferedBytes] = 0; |
| 87 | + return this.write(buf.slice(offset)); |
| 88 | +}; |
95 | 89 |
|
96 | | - get lastChar() { |
97 | | - return this[kNativeDecoder].subarray(kIncompleteCharactersStart, |
98 | | - kIncompleteCharactersEnd); |
| 90 | +Object.defineProperties(StringDecoder.prototype, { |
| 91 | + lastChar: { |
| 92 | + configurable: true, |
| 93 | + enumerable: true, |
| 94 | + get() { |
| 95 | + return this[kNativeDecoder].subarray(kIncompleteCharactersStart, |
| 96 | + kIncompleteCharactersEnd); |
| 97 | + } |
| 98 | + }, |
| 99 | + lastNeed: { |
| 100 | + configurable: true, |
| 101 | + enumerable: true, |
| 102 | + get() { |
| 103 | + return this[kNativeDecoder][kMissingBytes]; |
| 104 | + } |
| 105 | + }, |
| 106 | + lastTotal: { |
| 107 | + configurable: true, |
| 108 | + enumerable: true, |
| 109 | + get() { |
| 110 | + return this[kNativeDecoder][kBufferedBytes] + |
| 111 | + this[kNativeDecoder][kMissingBytes]; |
| 112 | + } |
99 | 113 | } |
100 | | -} |
| 114 | +}); |
101 | 115 |
|
102 | 116 | exports.StringDecoder = StringDecoder; |