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",
"url": "https://github.com/js-platform/filer.git"
},
"browser": {
"request": "browser-request"
},
"dependencies": {
"bower": "~1.0.0",
"request": "^2.36.0"
"request": "^2.36.0",
"browser-request": "^0.3.1"
},
"devDependencies": {
"chai": "~1.9.1",

View File

@ -1,59 +1,22 @@
function browserDownload(uri, callback) {
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);
var request = require('request');
callback(err, data);
};
query.open("GET", uri);
if("withCredentials" in query) {
query.withCredentials = true;
}
query.responseType = "arraybuffer";
query.send();
}
function nodeDownload(uri, callback) {
require('request')({
module.exports.download = function(uri, callback) {
request({
url: uri,
method: "GET",
encoding: null
}, function(err, msg, body) {
var data = null,
arrayBuffer,
statusCode,
arrayLength = body && body.length,
error;
var statusCode;
var error;
msg = msg || null;
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) {
return callback(error, null);
callback(error, null);
return;
}
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);
callback(null, body);
});
}
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
var relpath = path.replace(/^\//, '');
zip.addFile(data, { filename: encode(relpath) });
zip.addFile(data, { filename: Encoding.encode(relpath) });
callback();
});
}