2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 15:59:48 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
2019-11-27 22:03:25 +00:00
|
|
|
ArrayBufferIsView,
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperties,
|
2019-11-30 15:55:29 +00:00
|
|
|
Symbol,
|
2020-11-21 19:23:32 +00:00
|
|
|
TypedArrayPrototypeSubarray,
|
2019-11-22 17:04:46 +00:00
|
|
|
} = primordials;
|
2019-04-09 07:55:53 +00:00
|
|
|
|
2017-10-07 14:50:42 +00:00
|
|
|
const { Buffer } = require('buffer');
|
2018-02-01 01:28:39 +00:00
|
|
|
const {
|
|
|
|
kIncompleteCharactersStart,
|
|
|
|
kIncompleteCharactersEnd,
|
|
|
|
kMissingBytes,
|
|
|
|
kBufferedBytes,
|
|
|
|
kEncodingField,
|
|
|
|
kSize,
|
|
|
|
decode,
|
|
|
|
flush,
|
|
|
|
} = internalBinding('string_decoder');
|
2023-12-04 01:46:11 +00:00
|
|
|
const {
|
|
|
|
encodingsMap,
|
2024-09-23 22:49:53 +00:00
|
|
|
normalizeEncoding,
|
2023-12-04 01:46:11 +00:00
|
|
|
} = require('internal/util');
|
2018-02-27 13:55:32 +00:00
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
2022-03-08 16:36:11 +00:00
|
|
|
ERR_INVALID_THIS,
|
2023-02-12 18:26:21 +00:00
|
|
|
ERR_UNKNOWN_ENCODING,
|
2018-02-27 13:55:32 +00:00
|
|
|
} = require('internal/errors').codes;
|
2015-05-29 17:35:43 +00:00
|
|
|
|
2018-02-01 01:28:39 +00:00
|
|
|
const kNativeDecoder = Symbol('kNativeDecoder');
|
|
|
|
|
2021-12-30 23:44:32 +00:00
|
|
|
/**
|
|
|
|
* StringDecoder provides an interface for efficiently splitting a series of
|
|
|
|
* buffers into a series of JS strings without breaking apart multi-byte
|
|
|
|
* characters.
|
|
|
|
* @param {string} [encoding=utf-8]
|
|
|
|
*/
|
2018-02-12 06:21:19 +00:00
|
|
|
function StringDecoder(encoding) {
|
|
|
|
this.encoding = normalizeEncoding(encoding);
|
2024-09-23 22:49:53 +00:00
|
|
|
if (this.encoding === undefined) {
|
|
|
|
throw new ERR_UNKNOWN_ENCODING(encoding);
|
|
|
|
}
|
2018-02-12 06:21:19 +00:00
|
|
|
this[kNativeDecoder] = Buffer.alloc(kSize);
|
|
|
|
this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
|
|
|
|
}
|
2010-05-18 02:25:51 +00:00
|
|
|
|
2021-12-30 23:44:32 +00:00
|
|
|
/**
|
|
|
|
* Returns a decoded string, omitting any incomplete multi-bytes
|
|
|
|
* characters at the end of the Buffer, or TypedArray, or DataView
|
|
|
|
* @param {string | Buffer | TypedArray | DataView} buf
|
|
|
|
* @returns {string}
|
|
|
|
* @throws {TypeError} Throws when buf is not in one of supported types
|
|
|
|
*/
|
2018-02-12 06:21:19 +00:00
|
|
|
StringDecoder.prototype.write = function write(buf) {
|
|
|
|
if (typeof buf === 'string')
|
|
|
|
return buf;
|
2019-11-27 22:03:25 +00:00
|
|
|
if (!ArrayBufferIsView(buf))
|
2018-02-27 13:55:32 +00:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('buf',
|
2018-08-28 05:51:00 +00:00
|
|
|
['Buffer', 'TypedArray', 'DataView'],
|
2018-03-19 12:33:46 +00:00
|
|
|
buf);
|
2022-03-08 16:36:11 +00:00
|
|
|
if (!this[kNativeDecoder]) {
|
|
|
|
throw new ERR_INVALID_THIS('StringDecoder');
|
|
|
|
}
|
2018-02-12 06:21:19 +00:00
|
|
|
return decode(this[kNativeDecoder], buf);
|
|
|
|
};
|
2015-03-19 21:31:34 +00:00
|
|
|
|
2021-12-30 23:44:32 +00:00
|
|
|
/**
|
|
|
|
* Returns any remaining input stored in the internal buffer as a string.
|
|
|
|
* After end() is called, the stringDecoder object can be reused for new
|
|
|
|
* input.
|
|
|
|
* @param {string | Buffer | TypedArray | DataView} [buf]
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-02-12 06:21:19 +00:00
|
|
|
StringDecoder.prototype.end = function end(buf) {
|
2024-09-23 22:49:53 +00:00
|
|
|
const ret = buf === undefined ? '' : this.write(buf);
|
2018-02-12 06:21:19 +00:00
|
|
|
if (this[kNativeDecoder][kBufferedBytes] > 0)
|
2024-09-23 22:49:53 +00:00
|
|
|
return ret + flush(this[kNativeDecoder]);
|
2018-02-12 06:21:19 +00:00
|
|
|
return ret;
|
|
|
|
};
|
2012-05-05 03:22:01 +00:00
|
|
|
|
2018-02-12 06:21:19 +00:00
|
|
|
/* Everything below this line is undocumented legacy stuff. */
|
2021-12-30 23:44:32 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string | Buffer | TypedArray | DataView} buf
|
|
|
|
* @param {number} offset
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2018-02-12 06:21:19 +00:00
|
|
|
StringDecoder.prototype.text = function text(buf, offset) {
|
|
|
|
this[kNativeDecoder][kMissingBytes] = 0;
|
|
|
|
this[kNativeDecoder][kBufferedBytes] = 0;
|
|
|
|
return this.write(buf.slice(offset));
|
|
|
|
};
|
2012-10-11 22:53:11 +00:00
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
ObjectDefineProperties(StringDecoder.prototype, {
|
2018-02-12 06:21:19 +00:00
|
|
|
lastChar: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2018-02-12 06:21:19 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
2020-11-21 19:23:32 +00:00
|
|
|
return TypedArrayPrototypeSubarray(this[kNativeDecoder],
|
|
|
|
kIncompleteCharactersStart,
|
|
|
|
kIncompleteCharactersEnd);
|
2023-02-12 18:26:21 +00:00
|
|
|
},
|
2018-02-12 06:21:19 +00:00
|
|
|
},
|
|
|
|
lastNeed: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2018-02-12 06:21:19 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
return this[kNativeDecoder][kMissingBytes];
|
2023-02-12 18:26:21 +00:00
|
|
|
},
|
2018-02-12 06:21:19 +00:00
|
|
|
},
|
|
|
|
lastTotal: {
|
2022-06-03 08:23:58 +00:00
|
|
|
__proto__: null,
|
2018-02-12 06:21:19 +00:00
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
return this[kNativeDecoder][kBufferedBytes] +
|
|
|
|
this[kNativeDecoder][kMissingBytes];
|
2023-02-12 18:26:21 +00:00
|
|
|
},
|
|
|
|
},
|
2018-02-12 06:21:19 +00:00
|
|
|
});
|
2010-06-16 01:19:25 +00:00
|
|
|
|
2018-02-01 01:28:39 +00:00
|
|
|
exports.StringDecoder = StringDecoder;
|