pax_global_header00006660000000000000000000000064123225566650014526gustar00rootroot0000000000000052 comment=36bab8c04223361eb1f11f1473a77d8f93bbe4a9 blob-0.0.4/000077500000000000000000000000001232255666500124455ustar00rootroot00000000000000blob-0.0.4/.gitignore000066400000000000000000000000251232255666500144320ustar00rootroot00000000000000node_modules blob.js blob-0.0.4/.zuul.yml000066400000000000000000000004061232255666500142450ustar00rootroot00000000000000ui: 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 blob-0.0.4/Makefile000066400000000000000000000003141232255666500141030ustar00rootroot00000000000000REPORTER = 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 blob-0.0.4/README.md000066400000000000000000000004731232255666500137300ustar00rootroot00000000000000Blob ==== 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 blob-0.0.4/index.js000066400000000000000000000042261232255666500141160ustar00rootroot00000000000000/** * 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; } })(); blob-0.0.4/package.json000066400000000000000000000007401232255666500147340ustar00rootroot00000000000000{ "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@github.com:rase-/blob.git" }, "scripts": { "test": "make test" } } blob-0.0.4/test/000077500000000000000000000000001232255666500134245ustar00rootroot00000000000000blob-0.0.4/test/index.js000066400000000000000000000060611232255666500150740ustar00rootroot00000000000000var 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'); }); } });