[Pkg-javascript-commits] [node-stream-browserify] 12/44: upgrade ArrayBuffers and Uint8arrays into Buffers

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 14:18:41 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-stream-browserify.

commit 5dbf0764979a74eb6aea6d2712b9a378742d1533
Author: Max Ogden <max at maxogden.com>
Date:   Thu Dec 19 14:36:07 2013 -0800

    upgrade ArrayBuffers and Uint8arrays into Buffers
---
 package.json |  8 +++++++-
 test.js      | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 writable.js  |  5 +++++
 3 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/package.json b/package.json
index 8afc472..3b2ae3a 100644
--- a/package.json
+++ b/package.json
@@ -7,6 +7,9 @@
     "inherits": "~2.0.1",
     "process": "~0.5.1"
   },
+  "scripts": {
+    "test": "node test.js"
+  },
   "repository": {
     "type": "git",
     "url": "git://github.com/substack/stream-browserify.git"
@@ -22,5 +25,8 @@
     "email": "mail at substack.net",
     "url": "http://substack.net"
   },
-  "license": "MIT"
+  "license": "MIT",
+  "devDependencies": {
+    "tape": "~2.3.2"
+  }
 }
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..a91e2b2
--- /dev/null
+++ b/test.js
@@ -0,0 +1,46 @@
+var util = require('util');
+var path = require('path');
+var test = require('tape');
+
+var Writable = require(path.join(__dirname, 'writable'));
+
+util.inherits(TestWritable, Writable);
+
+function TestWritable(opt) {
+    if (!(this instanceof TestWritable))
+        return new TestWritable(opt);
+    Writable.call(this, opt);
+    this._written = [];
+}
+
+TestWritable.prototype._write = function(chunk, encoding, cb) {
+    this._written.push(chunk);
+    cb();
+};
+
+var typedArray = new Uint8Array(1);
+typedArray[0] = 88;
+
+test('.writable writing ArrayBuffer', function(t) {
+    var writable = new TestWritable();
+    
+    writable.write(typedArray.buffer);
+    writable.end();
+    
+    t.equal(writable._written.length, 1);
+    t.equal(writable._written[0].toString(), 'X')
+    t.end()
+});
+
+
+test('.writable writing Uint8array', function(t) {
+    var writable = new TestWritable();
+    
+    writable.write(typedArray);
+    writable.end();
+    
+    t.equal(writable._written.length, 1);
+    t.equal(writable._written[0].toString(), 'X')
+    t.end()
+});
+
diff --git a/writable.js b/writable.js
index 7c58f2e..cece221 100644
--- a/writable.js
+++ b/writable.js
@@ -167,6 +167,11 @@ Writable.prototype.write = function(chunk, encoding, cb) {
     encoding = null;
   }
 
+  if (chunk instanceof Uint8Array)
+    chunk = new Buffer(chunk);
+  if (chunk instanceof ArrayBuffer)
+    chunk = new Buffer(new Uint8Array(chunk));
+  
   if (Buffer.isBuffer(chunk))
     encoding = 'buffer';
   else if (!encoding)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-browserify.git



More information about the Pkg-javascript-commits mailing list