aboutsummaryrefslogtreecommitdiff
path: root/node_modules/typedarray-to-buffer/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/typedarray-to-buffer/index.js')
-rw-r--r--node_modules/typedarray-to-buffer/index.js25
1 files changed, 25 insertions, 0 deletions
diff --git a/node_modules/typedarray-to-buffer/index.js b/node_modules/typedarray-to-buffer/index.js
new file mode 100644
index 0000000..5fa394d
--- /dev/null
+++ b/node_modules/typedarray-to-buffer/index.js
@@ -0,0 +1,25 @@
+/**
+ * Convert a typed array to a Buffer without a copy
+ *
+ * Author: Feross Aboukhadijeh <https://feross.org>
+ * License: MIT
+ *
+ * `npm install typedarray-to-buffer`
+ */
+
+var isTypedArray = require('is-typedarray').strict
+
+module.exports = function typedarrayToBuffer (arr) {
+ if (isTypedArray(arr)) {
+ // To avoid a copy, use the typed array's underlying ArrayBuffer to back new Buffer
+ var buf = Buffer.from(arr.buffer)
+ if (arr.byteLength !== arr.buffer.byteLength) {
+ // Respect the "view", i.e. byteOffset and byteLength, without doing a copy
+ buf = buf.slice(arr.byteOffset, arr.byteOffset + arr.byteLength)
+ }
+ return buf
+ } else {
+ // Pass through all other types to `Buffer.from`
+ return Buffer.from(arr)
+ }
+}