Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSZipUtils.xhr, support node.js #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var JSZipUtils = {};
let JSZipUtils = {};
// just use the responseText with xhr1, response with xhr2.
// The transformation doesn't throw away high-order byte (with responseText)
// because JSZip handles that case. If not used with JSZip, you may need to
Expand All @@ -23,21 +23,16 @@ function createActiveXHR() {
} catch( e ) {}
}

// Create the request object
var createXHR = (typeof window !== "undefined" && window.ActiveXObject) ?
/* Microsoft failed to properly
* implement the XMLHttpRequest in IE7 (can't request local files),
* so we use the ActiveXObject when it is available
* Additionally XMLHttpRequest can be disabled in IE7/IE8 so
* we need a fallback.
*/
function() {
return createStandardXHR() || createActiveXHR();
} :
// For all other browsers, use the standard XMLHttpRequest object
createStandardXHR;
JSZipUtils.xhr = null;

function createXHR()
{
if (JSZipUtils.xhr || window.XMLHttpRequest) {
return new (JSZipUtils.xhr || window.XMLHttpRequest);
}

return createActiveXHR();
}

JSZipUtils.getBinaryContent = function(path, callback) {
/*
Expand All @@ -56,7 +51,7 @@ JSZipUtils.getBinaryContent = function(path, callback) {
*/
try {

var xhr = createXHR();
let xhr = createXHR();

xhr.open('GET', path, true);

Expand All @@ -71,7 +66,7 @@ JSZipUtils.getBinaryContent = function(path, callback) {
}

xhr.onreadystatechange = function(evt) {
var file, err;
let file, err;
// use `xhr` and not `this`... thanks IE
if (xhr.readyState === 4) {
if (xhr.status === 200 || xhr.status === 0) {
Expand All @@ -80,7 +75,7 @@ JSZipUtils.getBinaryContent = function(path, callback) {
try {
file = JSZipUtils._getBinaryFromXHR(xhr);
} catch(e) {
err = new Error(e);
err = e;
}
callback(err, file);
} else {
Expand All @@ -92,7 +87,7 @@ JSZipUtils.getBinaryContent = function(path, callback) {
xhr.send();

} catch (e) {
callback(new Error(e), null);
callback(e, null);
}
};

Expand Down