summaryrefslogtreecommitdiff
path: root/lib/internal/buffer.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/buffer.js')
-rw-r--r--lib/internal/buffer.js30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/internal/buffer.js b/lib/internal/buffer.js
new file mode 100644
index 0000000000..fdb34b9efd
--- /dev/null
+++ b/lib/internal/buffer.js
@@ -0,0 +1,30 @@
+'use strict';
+
+if (!process.binding('config').hasIntl) {
+ return;
+}
+
+const normalizeEncoding = require('internal/util').normalizeEncoding;
+const Buffer = require('buffer').Buffer;
+
+const icu = process.binding('icu');
+
+// Transcodes the Buffer from one encoding to another, returning a new
+// Buffer instance.
+exports.transcode = function transcode(source, fromEncoding, toEncoding) {
+ if (!Buffer.isBuffer(source))
+ throw new TypeError('"source" argument must be a Buffer');
+ if (source.length === 0) return Buffer.alloc(0);
+
+ fromEncoding = normalizeEncoding(fromEncoding) || fromEncoding;
+ toEncoding = normalizeEncoding(toEncoding) || toEncoding;
+ const result = icu.transcode(source, fromEncoding, toEncoding);
+ if (Buffer.isBuffer(result))
+ return result;
+
+ const code = icu.icuErrName(result);
+ const err = new Error(`Unable to transcode Buffer [${code}]`);
+ err.code = code;
+ err.errno = result;
+ throw err;
+};