[Pkg-javascript-commits] [node-blob] 01/02: Imported Upstream version 0.0.4
Sebastiaan Couwenberg
sebastic at moszumanska.debian.org
Sun Mar 29 20:13:53 UTC 2015
This is an automated email from the git hooks/post-receive script.
sebastic pushed a commit to branch master
in repository node-blob.
commit a61e6319de4cc0108ff1bd51e2dc97ce1ea96fea
Author: Bas Couwenberg <sebastic at xs4all.nl>
Date: Sun Mar 29 21:10:22 2015 +0200
Imported Upstream version 0.0.4
---
.gitignore | 2 ++
.zuul.yml | 14 +++++++++
Makefile | 14 +++++++++
README.md | 14 +++++++++
index.js | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 20 +++++++++++++
test/index.js | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 254 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..548a368
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+node_modules
+blob.js
diff --git a/.zuul.yml b/.zuul.yml
new file mode 100644
index 0000000..380c395
--- /dev/null
+++ b/.zuul.yml
@@ -0,0 +1,14 @@
+ui: mocha-bdd
+browsers:
+ - name: chrome
+ version: 8..latest
+ - name: firefox
+ version: 7..latest
+ - name: safari
+ version: 6..latest
+ - name: opera
+ version: 12.1..latest
+ - name: ie
+ version: 10..latest
+ - name: android
+ version: latest
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..7d9601a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+REPORTER = dot
+
+build: blob.js
+
+blob.js:
+ @./node_modules/.bin/browserify --standalone blob index.js > blob.js
+
+test:
+ @./node_modules/.bin/zuul -- test/index.js
+
+clean:
+ rm blob.js
+
+.PHONY: test blob.js
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..6915955
--- /dev/null
+++ b/README.md
@@ -0,0 +1,14 @@
+Blob
+====
+
+A module that exports a constructor that uses window.Blob when available, and a BlobBuilder with any vendor prefix in other cases. If neither is available, it exports undefined.
+
+Usage:
+
+```javascript
+var Blob = require('blob');
+var b = new Blob(['hi', 'constructing', 'a', 'blob']);
+```
+
+## Licence
+MIT
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..cad3f84
--- /dev/null
+++ b/index.js
@@ -0,0 +1,96 @@
+/**
+ * Create a blob builder even when vendor prefixes exist
+ */
+
+var BlobBuilder = global.BlobBuilder
+ || global.WebKitBlobBuilder
+ || global.MSBlobBuilder
+ || global.MozBlobBuilder;
+
+/**
+ * Check if Blob constructor is supported
+ */
+
+var blobSupported = (function() {
+ try {
+ var a = new Blob(['hi']);
+ return a.size === 2;
+ } catch(e) {
+ return false;
+ }
+})();
+
+/**
+ * Check if Blob constructor supports ArrayBufferViews
+ * Fails in Safari 6, so we need to map to ArrayBuffers there.
+ */
+
+var blobSupportsArrayBufferView = blobSupported && (function() {
+ try {
+ var b = new Blob([new Uint8Array([1,2])]);
+ return b.size === 2;
+ } catch(e) {
+ return false;
+ }
+})();
+
+/**
+ * Check if BlobBuilder is supported
+ */
+
+var blobBuilderSupported = BlobBuilder
+ && BlobBuilder.prototype.append
+ && BlobBuilder.prototype.getBlob;
+
+/**
+ * Helper function that maps ArrayBufferViews to ArrayBuffers
+ * Used by BlobBuilder constructor and old browsers that didn't
+ * support it in the Blob constructor.
+ */
+
+function mapArrayBufferViews(ary) {
+ for (var i = 0; i < ary.length; i++) {
+ var chunk = ary[i];
+ if (chunk.buffer instanceof ArrayBuffer) {
+ var buf = chunk.buffer;
+
+ // if this is a subarray, make a copy so we only
+ // include the subarray region from the underlying buffer
+ if (chunk.byteLength !== buf.byteLength) {
+ var copy = new Uint8Array(chunk.byteLength);
+ copy.set(new Uint8Array(buf, chunk.byteOffset, chunk.byteLength));
+ buf = copy.buffer;
+ }
+
+ ary[i] = buf;
+ }
+ }
+}
+
+function BlobBuilderConstructor(ary, options) {
+ options = options || {};
+
+ var bb = new BlobBuilder();
+ mapArrayBufferViews(ary);
+
+ for (var i = 0; i < ary.length; i++) {
+ bb.append(ary[i]);
+ }
+
+ return (options.type) ? bb.getBlob(options.type) : bb.getBlob();
+};
+
+function BlobConstructor(ary, options) {
+ mapArrayBufferViews(ary);
+ return new Blob(ary, options || {});
+};
+
+module.exports = (function() {
+ if (blobSupported) {
+ return blobSupportsArrayBufferView ? global.Blob : BlobConstructor;
+ } else if (blobBuilderSupported) {
+ return BlobBuilderConstructor;
+ } else {
+ return undefined;
+ }
+})();
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..222dedd
--- /dev/null
+++ b/package.json
@@ -0,0 +1,20 @@
+{
+ "name": "blob",
+ "description": "Abstracts out Blob and uses BlobBulder in cases where it is supported with any vendor prefix.",
+ "version": "0.0.4",
+ "homepage": "https://github.com/rase-/blob",
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "1.17.1",
+ "expect.js": "0.2.0",
+ "zuul": "1.5.4",
+ "browserify": "3.30.1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git at github.com:rase-/blob.git"
+ },
+ "scripts": {
+ "test": "make test"
+ }
+}
diff --git a/test/index.js b/test/index.js
new file mode 100644
index 0000000..df9303f
--- /dev/null
+++ b/test/index.js
@@ -0,0 +1,94 @@
+var Blob = require('../');
+var expect = require('expect.js');
+
+describe('blob', function() {
+ if (!Blob) {
+ it('should not have a blob or a blob builder in the global namespace, or blob should not be a constructor function if the module exports false', function() {
+ try {
+ var ab = (new Uint8Array(5)).buffer;
+ global.Blob([ab]);
+ expect().fail('Blob shouldn\'t be constructable');
+ } catch (e) {}
+
+ var BlobBuilder = global.BlobBuilder
+ || global.WebKitBlobBuilder
+ || global.MSBlobBuilder
+ || global.MozBlobBuilder;
+ expect(BlobBuilder).to.be(undefined);
+ });
+ } else {
+ it('should encode a proper sized blob when given a string argument', function() {
+ var b = new Blob(['hi']);
+ expect(b.size).to.be(2);
+ });
+
+ it('should encode a blob with proper size when given two strings as arguments', function() {
+ var b = new Blob(['hi', 'hello']);
+ expect(b.size).to.be(7);
+ });
+
+ it('should encode arraybuffers with right content', function(done) {
+ var ary = new Uint8Array(5);
+ for (var i = 0; i < 5; i++) ary[i] = i;
+ var b = new Blob([ary.buffer]);
+ var fr = new FileReader();
+ fr.onload = function() {
+ var newAry = new Uint8Array(this.result);
+ for (var i = 0; i < 5; i++) expect(newAry[i]).to.be(i);
+ done();
+ };
+ fr.readAsArrayBuffer(b);
+ });
+
+ it('should encode typed arrays with right content', function(done) {
+ var ary = new Uint8Array(5);
+ for (var i = 0; i < 5; i++) ary[i] = i;
+ var b = new Blob([ary]);
+ var fr = new FileReader();
+ fr.onload = function() {
+ var newAry = new Uint8Array(this.result);
+ for (var i = 0; i < 5; i++) expect(newAry[i]).to.be(i);
+ done();
+ };
+ fr.readAsArrayBuffer(b);
+ });
+
+ it('should encode sliced typed arrays with right content', function(done) {
+ var ary = new Uint8Array(5);
+ for (var i = 0; i < 5; i++) ary[i] = i;
+ var b = new Blob([ary.subarray(2)]);
+ var fr = new FileReader();
+ fr.onload = function() {
+ var newAry = new Uint8Array(this.result);
+ for (var i = 0; i < 3; i++) expect(newAry[i]).to.be(i + 2);
+ done();
+ };
+ fr.readAsArrayBuffer(b);
+ });
+
+ it('should encode with blobs', function(done) {
+ var ary = new Uint8Array(5);
+ for (var i = 0; i < 5; i++) ary[i] = i;
+ var b = new Blob([new Blob([ary.buffer])]);
+ var fr = new FileReader();
+ fr.onload = function() {
+ var newAry = new Uint8Array(this.result);
+ for (var i = 0; i < 5; i++) expect(newAry[i]).to.be(i);
+ done();
+ };
+ fr.readAsArrayBuffer(b);
+ });
+
+ it('should enode mixed contents to right size', function() {
+ var ary = new Uint8Array(5);
+ for (var i = 0; i < 5; i++) ary[i] = i;
+ var b = new Blob([ary.buffer, 'hello']);
+ expect(b.size).to.be(10);
+ });
+
+ it('should accept mime type', function() {
+ var b = new Blob(['hi', 'hello'], { type: 'text/html' });
+ expect(b.type).to.be('text/html');
+ });
+ }
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-blob.git
More information about the Pkg-javascript-commits
mailing list