{"action":"closed","issue":{"url":"https://api.github.com/repos/nodejs/node/issues/2835","repository_url":"https://api.github.com/repos/nodejs/node","labels_url":"https://api.github.com/repos/nodejs/node/issues/2835/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/node/issues/2835/comments","events_url":"https://api.github.com/repos/nodejs/node/issues/2835/events","html_url":"https://github.com/nodejs/node/issues/2835","id":106170389,"number":2835,"title":"provide a way to introduce new encodings for `new Buffer('someString', 'someEncoding')`","user":{"login":"Mithgol","id":1088720,"avatar_url":"https://avatars.githubusercontent.com/u/1088720?v=3","gravatar_id":"","url":"https://api.github.com/users/Mithgol","html_url":"https://github.com/Mithgol","followers_url":"https://api.github.com/users/Mithgol/followers","following_url":"https://api.github.com/users/Mithgol/following{/other_user}","gists_url":"https://api.github.com/users/Mithgol/gists{/gist_id}","starred_url":"https://api.github.com/users/Mithgol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Mithgol/subscriptions","organizations_url":"https://api.github.com/users/Mithgol/orgs","repos_url":"https://api.github.com/users/Mithgol/repos","events_url":"https://api.github.com/users/Mithgol/events{/privacy}","received_events_url":"https://api.github.com/users/Mithgol/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/nodejs/node/labels/buffer","name":"buffer","color":"f7c6c7"},{"url":"https://api.github.com/repos/nodejs/node/labels/feature%20request","name":"feature request","color":"0D7BDE"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":17,"created_at":"2015-09-12T16:28:08Z","updated_at":"2016-03-16T09:02:32Z","closed_at":"2016-03-16T09:02:32Z","body":"In the constructor `new Buffer('someString', 'someEncoding')` Node.js v4.0.0 itself [supports](https://nodejs.org/docs/v4.0.0/api/buffer.html#buffer_buffer) a limited number of encodings: `'ascii'`, `'utf8'`, `'utf16le'` (aka `'ucs2'`), `'base64'`, `'hex'`, `'binary'`. That's a half of a dozen. That's not plenty.\r\n\r\nAnd that's why a widely used package [`iconv-lite`](https://www.npmjs.com/package/iconv-lite) (450+ direct dependents, ≈240+ thousands of daily downloads) provides a method ([`.extendNodeEncodings()`](https://github.com/ashtuchkin/iconv-lite/tree/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f#extend-nodejs-own-encodings)) that adds a support of many other known encodings to the `Buffer` API.\r\n\r\nHowever, `iconv-lite` does not seem to work in Node v4.0.0 well enough. Any use of an iconv-lite-provided encoding in an attempt of `new Buffer('someLatinString', 'encodingName')` results in some random output such as the following:\r\n\r\n![(screenshot)](https://cloud.githubusercontent.com/assets/1088720/9831362/56911214-595c-11e5-9f9a-f00bc0c17d21.gif)\r\n\r\nIt seems to me that either 0fa6c4a6fc7ed4a2dfe821f1d3a46d5f6ff43e69 was not enough to fix #1547 or a separate deeper issue exists.\r\n\r\nCurrently iconv-lite's [`extend-node.js`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js) changes the behaviour of the following methods:\r\n\r\nin `SlowBuffer` | in `Buffer`\r\n:-------------: | :-----------:\r\n[`SlowBuffer.prototype.toString`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js#L25) | [`Buffer.prototype.toString`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js#L100)\r\n[`SlowBuffer.prototype.write`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js#L39) | [`Buffer.prototype.write`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js#L114)\r\n[`SlowBuffer.byteLength`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js#L88) | [`Buffer.byteLength`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js#L88)\r\n  | [`Buffer.isEncoding`](https://github.com/ashtuchkin/iconv-lite/blob/e285b7c31eb0406cf5a8e3e09bc16fbd2786360f/lib/extend-node.js#L83)\r\n\r\nWhy those changes were enough in Node v0.10 and v0.12 but aren't in Node v4.0.0?\r\n\r\nI may be wrong, but… it seems to me that in Node v0.12 the Buffer's constructor [have used](https://github.com/nodejs/node/blob/archived-io.js-v0.12/lib/buffer.js#L99) `this.write(subject, encoding)` internally but in the current Node v4.0.0 neither the Buffer's [constructor](https://github.com/nodejs/node/blob/de051757e28e2fd256eb9a64d44bf22a0e0bbf00/lib/buffer.js#L33-L49) nor its [`fromString`](https://github.com/nodejs/node/blob/de051757e28e2fd256eb9a64d44bf22a0e0bbf00/lib/buffer.js#L86-L101) helper do that. They seem to use [`binding.createFromString(string, encoding)`](https://github.com/nodejs/node/blob/de051757e28e2fd256eb9a64d44bf22a0e0bbf00/lib/buffer.js#L92) (where necessary) or [`allocPool.write(string, poolOffset, encoding)`](https://github.com/nodejs/node/blob/de051757e28e2fd256eb9a64d44bf22a0e0bbf00/lib/buffer.js#L96) and both of these come from [`process.binding('buffer')`](https://github.com/nodejs/node/blob/de051757e28e2fd256eb9a64d44bf22a0e0bbf00/lib/buffer.js#L4) and aren't replaced by `iconv-lite`. And it won't be easy to replace them from userland, I suppose.\r\n\r\nIs my assumption correct?\r\n\r\nWhat should be done in `iconv-lite` (or in Node.js, or in both) for a multitude of encodings to work in the `new Buffer('someString', 'encodingName')` constructor correctly?"}}