2017-01-03 22:16:48 +01: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 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
2019-11-27 23:03:25 +01:00
|
|
|
ArrayBufferIsView,
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperties,
|
2019-11-30 16:55:29 +01:00
|
|
|
Symbol,
|
2019-11-22 18:04:46 +01:00
|
|
|
} = primordials;
|
2019-04-09 09:55:53 +02:00
|
|
|
|
2017-10-07 16:50:42 +02:00
|
|
|
const { Buffer } = require('buffer');
|
2018-02-01 02:28:39 +01:00
|
|
|
const {
|
|
|
|
kIncompleteCharactersStart,
|
|
|
|
kIncompleteCharactersEnd,
|
|
|
|
kMissingBytes,
|
|
|
|
kBufferedBytes,
|
|
|
|
kEncodingField,
|
|
|
|
kSize,
|
|
|
|
decode,
|
|
|
|
flush,
|
|
|
|
encodings
|
|
|
|
} = internalBinding('string_decoder');
|
2016-06-08 01:03:27 +02:00
|
|
|
const internalUtil = require('internal/util');
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_UNKNOWN_ENCODING
|
|
|
|
} = require('internal/errors').codes;
|
2016-06-08 01:03:27 +02:00
|
|
|
const isEncoding = Buffer[internalUtil.kIsEncodingSymbol];
|
2015-05-29 19:35:43 +02:00
|
|
|
|
2018-02-01 02:28:39 +01:00
|
|
|
const kNativeDecoder = Symbol('kNativeDecoder');
|
|
|
|
|
2016-05-15 20:53:08 +02:00
|
|
|
// Do not cache `Buffer.isEncoding` when checking encoding names as some
|
|
|
|
// modules monkey-patch it to support additional encodings
|
|
|
|
function normalizeEncoding(enc) {
|
2016-06-08 01:03:27 +02:00
|
|
|
const nenc = internalUtil.normalizeEncoding(enc);
|
2018-02-14 23:48:35 +01:00
|
|
|
if (nenc === undefined) {
|
|
|
|
if (Buffer.isEncoding === isEncoding || !Buffer.isEncoding(enc))
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_UNKNOWN_ENCODING(enc);
|
2018-02-14 23:48:35 +01:00
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
return nenc;
|
2012-10-05 02:43:15 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 02:28:39 +01:00
|
|
|
const encodingsMap = {};
|
2019-11-12 16:17:43 +01:00
|
|
|
for (let i = 0; i < encodings.length; ++i)
|
2018-02-01 02:28:39 +01:00
|
|
|
encodingsMap[encodings[i]] = i;
|
|
|
|
|
2014-05-13 17:42:48 +02:00
|
|
|
// StringDecoder provides an interface for efficiently splitting a series of
|
|
|
|
// buffers into a series of JS strings without breaking apart multi-byte
|
2016-05-15 20:53:08 +02:00
|
|
|
// characters.
|
2018-02-12 07:21:19 +01:00
|
|
|
function StringDecoder(encoding) {
|
|
|
|
this.encoding = normalizeEncoding(encoding);
|
|
|
|
this[kNativeDecoder] = Buffer.alloc(kSize);
|
|
|
|
this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
|
|
|
|
}
|
2010-05-18 04:25:51 +02:00
|
|
|
|
2018-02-12 07:21:19 +01:00
|
|
|
StringDecoder.prototype.write = function write(buf) {
|
|
|
|
if (typeof buf === 'string')
|
|
|
|
return buf;
|
2019-11-27 23:03:25 +01:00
|
|
|
if (!ArrayBufferIsView(buf))
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('buf',
|
2018-08-28 07:51:00 +02:00
|
|
|
['Buffer', 'TypedArray', 'DataView'],
|
2018-03-19 13:33:46 +01:00
|
|
|
buf);
|
2018-02-12 07:21:19 +01:00
|
|
|
return decode(this[kNativeDecoder], buf);
|
|
|
|
};
|
2015-03-19 22:31:34 +01:00
|
|
|
|
2018-02-12 07:21:19 +01:00
|
|
|
StringDecoder.prototype.end = function end(buf) {
|
|
|
|
let ret = '';
|
|
|
|
if (buf !== undefined)
|
|
|
|
ret = this.write(buf);
|
|
|
|
if (this[kNativeDecoder][kBufferedBytes] > 0)
|
|
|
|
ret += flush(this[kNativeDecoder]);
|
|
|
|
return ret;
|
|
|
|
};
|
2012-05-05 05:22:01 +02:00
|
|
|
|
2018-02-12 07:21:19 +01:00
|
|
|
/* Everything below this line is undocumented legacy stuff. */
|
|
|
|
StringDecoder.prototype.text = function text(buf, offset) {
|
|
|
|
this[kNativeDecoder][kMissingBytes] = 0;
|
|
|
|
this[kNativeDecoder][kBufferedBytes] = 0;
|
|
|
|
return this.write(buf.slice(offset));
|
|
|
|
};
|
2012-10-12 00:53:11 +02:00
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
ObjectDefineProperties(StringDecoder.prototype, {
|
2018-02-12 07:21:19 +01:00
|
|
|
lastChar: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
return this[kNativeDecoder].subarray(kIncompleteCharactersStart,
|
|
|
|
kIncompleteCharactersEnd);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lastNeed: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
return this[kNativeDecoder][kMissingBytes];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lastTotal: {
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
get() {
|
|
|
|
return this[kNativeDecoder][kBufferedBytes] +
|
|
|
|
this[kNativeDecoder][kMissingBytes];
|
|
|
|
}
|
2018-02-01 07:12:05 +01:00
|
|
|
}
|
2018-02-12 07:21:19 +01:00
|
|
|
});
|
2010-06-16 03:19:25 +02:00
|
|
|
|
2018-02-01 02:28:39 +01:00
|
|
|
exports.StringDecoder = StringDecoder;
|