You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
Provide way to allow for new buffer encodings. #1772
Buffer.registerStringEncoding(name, function stringify(buffer) => String, function parse(string) => Buffer) throws on duplicate name
The performance issue could be worked around by never encoding to a buffer until a toString or property related to the buffer value is requested (and then not even doing that if the encodings match on toString), and then cacheing the result.
This is a feature request to allow extension to the "built-in" Buffer
object, not change any existing behavior/signature beyond new
encodings; regardless of how it should be done now I feel that having
a multitude of wrappers may introduce a difficulty in the encoding /
decoding of formats as the wrappers may have many signatures and
encourage tight coupling to systems.
In order to detect if an encoding is present, one would need to
manually add the encoding to ensure it is registered, or have a means
to detect if the encoding is present when the module needing the
encoding is required.
Simply requiring 'encodingType.js' seems simplistic and being at setup
time would most likely be fine in order to install these items in a
similar manner that requiring coffeescript acts on the '.coffee'
files. This is less than elegant than it could be, but far more
elegant than providing multiple wrappers for buffers that would
perform the same task of implementing a new encoding in potentially
many different ways.
how could I know in any module that encoding is registered?
Very good point. Node.js probably needs boolean .stringEncodingRegistered(encodingName) in addition to .registerStringEncoding(name, decoder, encoder).
However, the node_buffer.cc file does not look like it was ever written with freely extensible list of encodings in mind.
There are only a few pre-defined encodings, they appear in many elseifs, they are hardcoded in constructor names, etc.
It won't be easy to write a function that adds yet another encoding to that list, because there was never a list as a real data structure to begin with, and you cannot alter a pile of elseif lines programmaticaly. You'll have to rewrite a good deal of the existing code before you start writing such a function.
I also think that JavaScript functions stringify(buffer) and parse(string) are going to be slow.
For many use cases that is inevitable; however, for simple encodings (such as CP 866, KOI8-R, Windows-1251) the node_buffer.cc could contain some function like PHP's strtr() that accepts a simple array of replace pairs and does most of the work on its own (C++, not JavaScript) level, which is faster.
The module node-iconv (by @bnoordhuis) already does something similar, but supports only some predefined encodings, and is non-Windows, and requires compilation (while Node.js core binaries for Windows are pre-built and available for download).
The module node-iconv (by @bnoordhuis) already does something similar, but supports only some predefined encodings, and is non-Windows, and requires compilation (while Node.js core binaries for Windows are pre-built and available for download).
Iconv-lite seems to have the .decode(buffer, encoding) signature, while Node.js Buffer's .toString does also have start and end optional parameters.
I worked around it by creating my own node-singlebyte module.
Of course, I still look forward for the day when pluggable encodings become natively supported (just because the native code would likely run faster than JavaScript), but now I can live without it.
I'm not sure what you mean by "pluggable encodings" or by "single byte".
v8's internal API is called OneByte, that is essentially latin1. v8 also
natively supports TwoByte and UTF8.
Hmm. I don't really get why there'd be a request for this in core. We
support all the encodings we require, and as shown others are easily
handled in a module. If done properly there won't be any performance
difference.
But, I do forget people want core to be more than just a "core".
The idea is not about adding more encoding definitions to the core.
The idea is about adding an API that would allow userland modules to define more encodings for their own purposes in a way that would allow the core to run new Buffer(sting, encoding) and buf.toString(encoding, start, end) for that encodings, reusing most of the current core code of Buffers.
The typical userland code would then look like the following:
If done properly there won't be any performance difference.
If done in JavaScript, it would run slower than node_buffer.cc code (C/C++ code runs ≈5x faster than JavaScript).
If done in some C/C++ addon, it would face the general inavailability of build tools on end-user Windows systems.
There's no good way to solve this problem, there's only a (not obvious) choice of the lesser ot the three evils:
If you add this feature to the core, the core will be bloated.
If you make a JavaScript userland module, it will loose 80% of the possible speed.
If you make a C/C++ addon in a userland module, then a simple npm install is no longer possible on a typical Windows machine, it has to meet node-gyp's requirements beforehand (and, for example, Windows 7 64-bit SDK .ISO file has a size of 570.9 MB).
The proposed solution is trying to “split the baby” by suggesting to put the encoder and decoder to the core but to let the encoding definitions come from the userland. The userland code may then remain in JavaScript (definitions are data structures, such as an array in the above example, and 80% speed drop won't affect them), the addition to the core is minimal.
That sounds like an extremely bad idea, especially if two or more modules decide to implement different versions of the same encoding with the same name.
Activity
bobrik commentedon Sep 26, 2011
Extending built-in object isn't a good solution.
bmeck commentedon Sep 26, 2011
bobrik, Buffer is not a built-in for javascript, it is created by Node's core.
bobrik commentedon Sep 26, 2011
It's built-in for node. My own opinion: create helper object to do that task.
In other case, how could I know in any module that encoding is registered?
bmeck commentedon Sep 26, 2011
This is a feature request to allow extension to the "built-in" Buffer
object, not change any existing behavior/signature beyond new
encodings; regardless of how it should be done now I feel that having
a multitude of wrappers may introduce a difficulty in the encoding /
decoding of formats as the wrappers may have many signatures and
encourage tight coupling to systems.
In order to detect if an encoding is present, one would need to
manually add the encoding to ensure it is registered, or have a means
to detect if the encoding is present when the module needing the
encoding is required.
Simply requiring 'encodingType.js' seems simplistic and being at setup
time would most likely be fine in order to install these items in a
similar manner that requiring coffeescript acts on the '.coffee'
files. This is less than elegant than it could be, but far more
elegant than providing multiple wrappers for buffers that would
perform the same task of implementing a new encoding in potentially
many different ways.
On Mon, Sep 26, 2011 at 1:02 PM, Ian Babrou
reply@reply.github.com
wrote:
Mithgol commentedon Nov 28, 2011
@bobrik
Very good point. Node.js probably needs boolean
.stringEncodingRegistered(encodingName)in addition to.registerStringEncoding(name, decoder, encoder).Mithgol commentedon Mar 26, 2012
However, the
node_buffer.ccfile does not look like it was ever written with freely extensible list of encodings in mind.There are only a few pre-defined encodings, they appear in many
elseifs, they are hardcoded in constructor names, etc.It won't be easy to write a function that adds yet another encoding to that list, because there was never a list as a real data structure to begin with, and you cannot alter a pile of
elseiflines programmaticaly. You'll have to rewrite a good deal of the existing code before you start writing such a function.Mithgol commentedon May 11, 2012
I also think that JavaScript functions
stringify(buffer)andparse(string)are going to be slow.For many use cases that is inevitable; however, for simple encodings (such as CP 866, KOI8-R, Windows-1251) the
node_buffer.cccould contain some function like PHP'sstrtr()that accepts a simple array of replace pairs and does most of the work on its own (C++, not JavaScript) level, which is faster.The module node-iconv (by @bnoordhuis) already does something similar, but supports only some predefined encodings, and is non-Windows, and requires compilation (while Node.js core binaries for Windows are pre-built and available for download).
bnoordhuis commentedon May 17, 2012
node-iconv should now compile on Windows: bnoordhuis/node-iconv#27
As to 'only some predefined encodings', we're talking about 120 supported encodings. :-)
bnoordhuis commentedon Oct 10, 2013
Closing, no activity in over a year and node-iconv and iconv-lite have the market pretty much cornered.
We may someday allow pluggable encodings but that's ultra-low priority.
Mithgol commentedon Oct 12, 2013
Iconv-lite seems to have the
.decode(buffer, encoding)signature, while Node.js Buffer's.toStringdoes also havestartandendoptional parameters.I worked around it by creating my own node-singlebyte module.
Of course, I still look forward for the day when pluggable encodings become natively supported (just because the native code would likely run faster than JavaScript), but now I can live without it.
trevnorris commentedon Oct 12, 2013
I'm not sure what you mean by "pluggable encodings" or by "single byte".
v8's internal API is called OneByte, that is essentially latin1. v8 also
natively supports TwoByte and UTF8.
bnoordhuis commentedon Oct 13, 2013
Pluggable decoders for StringDecoder.
trevnorris commentedon Oct 13, 2013
Hmm. I don't really get why there'd be a request for this in core. We
support all the encodings we require, and as shown others are easily
handled in a module. If done properly there won't be any performance
difference.
But, I do forget people want core to be more than just a "core".
Mithgol commentedon Oct 17, 2013
The idea is not about adding more encoding definitions to the core.
The idea is about adding an API that would allow userland modules to define more encodings for their own purposes in a way that would allow the core to run
new Buffer(sting, encoding)andbuf.toString(encoding, start, end)for that encodings, reusing most of the current core code of Buffers.The typical userland code would then look like the following:
If done in JavaScript, it would run slower than
node_buffer.cccode (C/C++ code runs ≈5x faster than JavaScript).If done in some C/C++ addon, it would face the general inavailability of build tools on end-user Windows systems.
There's no good way to solve this problem, there's only a (not obvious) choice of the lesser ot the three evils:
npm installis no longer possible on a typical Windows machine, it has to meet node-gyp's requirements beforehand (and, for example, Windows 7 64-bit SDK .ISO file has a size of 570.9 MB).The proposed solution is trying to “split the baby” by suggesting to put the encoder and decoder to the core but to let the encoding definitions come from the userland. The userland code may then remain in JavaScript (definitions are data structures, such as an array in the above example, and 80% speed drop won't affect them), the addition to the core is minimal.
rlidwka commentedon Oct 20, 2013
That sounds like an extremely bad idea, especially if two or more modules decide to implement different versions of the same encoding with the same name.