initial version

This commit is contained in:
Nolan Lawson 2014-10-24 15:01:49 -04:00
parent 4ff09939b0
commit 09b307c535
5 changed files with 168 additions and 132 deletions

View File

@ -1,8 +1,5 @@
language: node_js
services:
- couchdb
node_js:
- "0.10"
script: npm run $COMMAND
@ -14,20 +11,10 @@ before_script:
# Workaround for Selenium #3280 issue
- "sudo sed -i 's/^127\\.0\\.0\\.1.*$/127.0.0.1 localhost/' /etc/hosts"
# Lets know what CouchDB we are playing with
# and make sure its logging debug
- "curl -X GET http://127.0.0.1:5984/"
- "curl -X PUT http://127.0.0.1:5984/_config/log/level -d '\"debug\"'"
after_failure:
- "curl -X GET http://127.0.0.1:5984/_log?bytes=1000000"
env:
matrix:
- COMMAND=test
- CLIENT=selenium:firefox COMMAND=test
- CLIENT=selenium:phantomjs COMMAND=test
- COMMAND=coverage
branches:
only:

164
index.js
View File

@ -1,19 +1,153 @@
'use strict';
var utils = require('./pouch-utils');
var utils = require('./utils');
var blob = require('blob');
var Promise = utils.Promise;
exports.sayHello = utils.toPromise(function (callback) {
//
// You can use the following code to
// get the pouch or PouchDB objects
//
// var pouch = this;
// var PouchDB = pouch.constructor;
callback(null, 'hello');
});
/* istanbul ignore next */
if (typeof window !== 'undefined' && window.PouchDB) {
window.PouchDB.plugin(exports);
function createBlob(parts, opts) {
return blob(parts, opts);
}
// From http://stackoverflow.com/questions/14967647/ (continues on next line)
// encode-decode-image-with-base64-breaks-image (2013-04-21)
function binaryStringToArrayBuffer(bin) {
var length = bin.length;
var buf = new ArrayBuffer(length);
var arr = new Uint8Array(buf);
var i = -1;
while (++i < length) {
arr[i] = bin.charCodeAt(i);
}
return buf;
}
// Can't find original post, but this is close
// http://stackoverflow.com/questions/6965107/ (continues on next line)
// converting-between-strings-and-arraybuffers
function arrayBufferToBinaryString(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var length = bytes.byteLength;
var i = -1;
while (++i < length) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
}
// shim for browsers that don't support it
function blobToBinaryString(blob) {
return new Promise(function (resolve, reject) {
var reader = new FileReader();
var hasBinaryString = typeof reader.readAsBinaryString === 'function';
reader.onloadend = function (e) {
var result = e.target.result || '';
if (hasBinaryString) {
return resolve(result);
}
resolve(arrayBufferToBinaryString(result));
};
reader.onerror = reject;
if (hasBinaryString) {
reader.readAsBinaryString(blob);
} else {
reader.readAsArrayBuffer(blob);
}
});
}
function base64StringToBlob(base64, type) {
return Promise.resolve().then(function () {
var parts = [binaryStringToArrayBuffer(atob(base64))];
return type ? createBlob(parts, {type: type}) : createBlob(parts);
});
}
function blobToBase64String(blob) {
return blobToBinaryString(blob).then(function (binary) {
return btoa(binary);
});
}
function loadImage(src) {
return new Promise(function (resolve, reject) {
var img = new Image();
img.onload = function () {
resolve(img);
};
img.onerror = reject;
img.src = src;
});
}
function dataURLToBlob(dataURL) {
var type = dataURL.match(/data:([^;]+)/)[1];
var base64 = dataURL.replace(/^[^,]+,/, '');
return createBlob([binaryStringToArrayBuffer(atob(base64))], {type: type});
}
function createObjectURL(blob) {
var compatURL = window.URL || window.webkitURL;
return compatURL.createObjectURL(blob);
}
function revokeObjectURL(url) {
var compatURL = window.URL || window.webkitURL;
return compatURL.revokeObjectURL(url);
}
function imgToCanvas(img) {
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
// copy the image contents to the canvas
var context = canvas.getContext('2d');
context.drawImage(
img,
0, 0,
img.width, img.height,
0, 0,
img.width, img.height);
return canvas;
}
function imgSrcToDataURL(src, type) {
type = type || 'image/jpeg';
return loadImage(src).then(function (img) {
return imgToCanvas(img);
}).then(function (canvas) {
return canvas.toDataURL(type);
});
}
function imgSrcToBlob(src, type) {
type = type || 'image/jpeg';
return loadImage(src).then(function (img) {
return imgToCanvas(img);
}).then(function (canvas) {
if (typeof canvas.toBlob === 'function') {
return new Promise(function (resolve) {
canvas.toBlob(type, resolve);
});
}
return dataURLToBlob(canvas.toDataURL(type));
});
}
module.exports = {
createBlob : createBlob,
createObjectURL : createObjectURL,
revokeObjectURL : revokeObjectURL,
imgSrcToBlob : imgSrcToBlob,
imgSrcToDataURL : imgSrcToDataURL,
dataURLToBlob : dataURLToBlob,
blobToBase64String : blobToBase64String,
base64StringToBlob : base64StringToBlob
};

View File

@ -1,40 +1,37 @@
{
"name": "pouchdb-plugin-seed",
"version": "0.1.0",
"description": "PouchDB Plugin Seed project",
"name": "blob-util",
"version": "0.0.1",
"description": "Utilities for working with Blob objects in the browser",
"main": "index.js",
"repository": {
"type": "git",
"url": "git://github.com/pouchdb/plugin-seed.git"
"url": "git://github.com/nolanlawson/blob-util.git"
},
"keywords": [
"pouch",
"pouchdb",
"plugin",
"seed",
"couch",
"couchdb"
"blob",
"blobs",
"binary",
"util",
"utils"
],
"author": "",
"author": "Nolan Lawson <nolan.lawson@gmail.com>",
"license": "Apache",
"bugs": {
"url": "https://github.com/pouchdb/plugin-seed/issues"
"url": "https://github.com/nolanlawson/blob-util/issues"
},
"scripts": {
"test-node": "TEST_DB=testdb,http://localhost:5984/testdb istanbul test ./node_modules/mocha/bin/_mocha test/test.js",
"test-node": "istanbul test ./node_modules/mocha/bin/_mocha test/test.js",
"test-browser": "./bin/test-browser.js",
"jshint": "jshint -c .jshintrc *.js test/test.js",
"test": "npm run jshint && ./bin/run-test.sh",
"build": "mkdir -p dist && browserify index.js -o dist/pouchdb.mypluginname.js && npm run min",
"min": "uglifyjs dist/pouchdb.mypluginname.js -mc > dist/pouchdb.mypluginname.min.js",
"build": "mkdir -p dist && browserify index.js -o dist/blob-util.js && npm run min",
"min": "uglifyjs dist/blob-util.js -mc > dist/blob-util.min.js",
"dev": "browserify test/test.js > test/test-bundle.js && npm run dev-server",
"dev-server": "./bin/dev-server.js",
"coverage": "npm test --coverage && istanbul check-coverage --lines 100 --function 100 --statements 100 --branches 100"
},
"dependencies": {
"lie": "^2.6.0",
"inherits": "~2.0.1",
"argsarray": "0.0.1",
"es3ify": "^0.1.3"
},
"devDependencies": {
@ -47,16 +44,12 @@
"jshint": "~2.3.0",
"mocha": "~1.18",
"phantomjs": "^1.9.7-5",
"pouchdb": "pouchdb/pouchdb",
"request": "^2.36.0",
"sauce-connect-launcher": "^0.4.2",
"uglify-js": "^2.4.13",
"watchify": "~0.4.1",
"wd": "^0.2.21"
},
"browser": {
"crypto": false
},
"browserify": {
"transform": [
"es3ify"

View File

@ -1,83 +0,0 @@
'use strict';
var Promise;
/* istanbul ignore next */
if (typeof window !== 'undefined' && window.PouchDB) {
Promise = window.PouchDB.utils.Promise;
} else {
Promise = typeof global.Promise === 'function' ? global.Promise : require('lie');
}
/* istanbul ignore next */
exports.once = function (fun) {
var called = false;
return exports.getArguments(function (args) {
if (called) {
console.trace();
throw new Error('once called more than once');
} else {
called = true;
fun.apply(this, args);
}
});
};
/* istanbul ignore next */
exports.getArguments = function (fun) {
return function () {
var len = arguments.length;
var args = new Array(len);
var i = -1;
while (++i < len) {
args[i] = arguments[i];
}
return fun.call(this, args);
};
};
/* istanbul ignore next */
exports.toPromise = function (func) {
//create the function we will be returning
return exports.getArguments(function (args) {
var self = this;
var tempCB = (typeof args[args.length - 1] === 'function') ? args.pop() : false;
// if the last argument is a function, assume its a callback
var usedCB;
if (tempCB) {
// if it was a callback, create a new callback which calls it,
// but do so async so we don't trap any errors
usedCB = function (err, resp) {
process.nextTick(function () {
tempCB(err, resp);
});
};
}
var promise = new Promise(function (fulfill, reject) {
try {
var callback = exports.once(function (err, mesg) {
if (err) {
reject(err);
} else {
fulfill(mesg);
}
});
// create a callback for this invocation
// apply the function in the orig context
args.push(callback);
func.apply(self, args);
} catch (e) {
reject(e);
}
});
// if there is a callback, call it back
if (usedCB) {
promise.then(function (result) {
usedCB(null, result);
}, usedCB);
}
promise.cancel = function () {
return this;
};
return promise;
});
};
exports.inherits = require('inherits');
exports.Promise = Promise;

5
utils.js Normal file
View File

@ -0,0 +1,5 @@
'use strict';
var Promise = typeof global.Promise === 'function' ? global.Promise : require('lie');
exports.Promise = Promise;