-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
Description
While investigating the Buffer
usage, I noticed one thing — in many (really, many) cases, Buffer
s are constructed just for the sake of base64-encoding a string.
base64-encodinng is used in many places, like auth headers, data uris, messages, etc.
With the new API, that would look like Buffer.from(from).toString(encoding)
.
With the old API, that would look like new Buffer(from).toString(encoding)
(note that this lacks checks).
So I propose to add a small utility function, e.g.
Buffer.convert = function(value, targetEncoding, sourceEncoding) {
if (!Buffer.isEncoding(targetEncoding)) {
throw new TypeError('"targetEncoding" must be a valid string encoding');
}
return Buffer.from(value, sourceEncoding).toString(targetEncoding);
}
Note that the impl does not default to utf-8
and forces an encoding to be specified, this way it would be more clear what the userspace code does.
Sure, that could be done on userside, but this doesn't deserve a separate package, and re-implementing that in all the packages that do conversion also doesn't look very good to me.
Note that if we have had such method earlier, the usage of Buffer
without new
(and other Buffer
-related issues) would have been measurably lower.
If everyone thinks that this is a good idea, I am willing to file a PR for it (impl/docs/tests).