Switch to build-time check vs run-time for request module in browserify

This commit is contained in:
David Humphrey (:humph) david.humphrey@senecacollege.ca 2014-06-06 11:41:26 -04:00
parent fa65a34905
commit c8dcd2d14a
3 changed files with 16 additions and 49 deletions

View File

@ -24,9 +24,13 @@
"type": "git", "type": "git",
"url": "https://github.com/js-platform/filer.git" "url": "https://github.com/js-platform/filer.git"
}, },
"browser": {
"request": "browser-request"
},
"dependencies": { "dependencies": {
"bower": "~1.0.0", "bower": "~1.0.0",
"request": "^2.36.0" "request": "^2.36.0",
"browser-request": "^0.3.1"
}, },
"devDependencies": { "devDependencies": {
"chai": "~1.9.1", "chai": "~1.9.1",

View File

@ -1,59 +1,22 @@
function browserDownload(uri, callback) { var request = require('request');
var query = new XMLHttpRequest();
query.onload = function() {
var err = query.status != 200 ? { message: query.statusText, code: query.status } : null,
data = err ? null : new Uint8Array(query.response);
callback(err, data); module.exports.download = function(uri, callback) {
}; request({
query.open("GET", uri);
if("withCredentials" in query) {
query.withCredentials = true;
}
query.responseType = "arraybuffer";
query.send();
}
function nodeDownload(uri, callback) {
require('request')({
url: uri, url: uri,
method: "GET", method: "GET",
encoding: null encoding: null
}, function(err, msg, body) { }, function(err, msg, body) {
var data = null, var statusCode;
arrayBuffer, var error;
statusCode,
arrayLength = body && body.length,
error;
msg = msg || null; msg = msg || null;
statusCode = msg && msg.statusCode; statusCode = msg && msg.statusCode;
error = statusCode !== 200 ? { message: err || 'Not found!', code: statusCode } : null;
error = statusCode != 200 ? { message: err || 'Not found!', code: statusCode } : null;
if (error) { if (error) {
return callback(error, null); callback(error, null);
return;
} }
callback(null, body);
arrayBuffer = arrayLength && new ArrayBuffer(arrayLength);
// Convert buffer to Uint8Array
if (arrayBuffer && (statusCode == 200)) {
data = new Uint8Array(arrayBuffer);
for (var i = 0; i < body.length; ++i) {
data[i] = body[i];
}
}
callback(null, data);
}); });
} };
module.exports.download = (function() {
if (typeof XMLHttpRequest === 'undefined') {
return nodeDownload;
} else {
return browserDownload;
}
}());

View File

@ -543,7 +543,7 @@ Shell.prototype.zip = function(zipfile, paths, options, callback) {
// Make path relative within the zip // Make path relative within the zip
var relpath = path.replace(/^\//, ''); var relpath = path.replace(/^\//, '');
zip.addFile(data, { filename: encode(relpath) }); zip.addFile(data, { filename: Encoding.encode(relpath) });
callback(); callback();
}); });
} }