From c8dcd2d14a0b3bb051ef749a9008353aec6e1f07 Mon Sep 17 00:00:00 2001 From: "David Humphrey (:humph) david.humphrey@senecacollege.ca" Date: Fri, 6 Jun 2014 11:41:26 -0400 Subject: [PATCH] Switch to build-time check vs run-time for request module in browserify --- package.json | 6 ++++- src/shell/network.js | 57 ++++++++------------------------------------ src/shell/shell.js | 2 +- 3 files changed, 16 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index e1c2e8c..0766c34 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/shell/network.js b/src/shell/network.js index 3c68fb8..c018423 100644 --- a/src/shell/network.js +++ b/src/shell/network.js @@ -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; - } -}()); +}; diff --git a/src/shell/shell.js b/src/shell/shell.js index d49f4fc..15bf5f8 100644 --- a/src/shell/shell.js +++ b/src/shell/shell.js @@ -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(); }); }