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

WIP: rewrite to typescript #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,15 @@ module.exports = function(grunt) {
browserify: {
"utils": {
files: {
'dist/jszip-utils.js': ['lib/index.js']
'dist/jszip-utils.js': ['lib/index.ts']
},
options: {
standalone: 'JSZipUtils',
configure: function (bundler) {
bundler.plugin(require('tsify'));
},
browserifyOptions: {
standalone: 'JSZipUtils'
},
postBundleCB: postBundleWithLicense
}
},
Expand Down Expand Up @@ -95,6 +100,11 @@ module.exports = function(grunt) {
src: 'dist/jszip-utils-ie.js',
dest: 'dist/jszip-utils-ie.min.js'
}
},
shell: {
"ts-declaration": {
command: "tsc lib/index.ts --declaration --emitDeclarationOnly --outDir './dist'"
}
}
});

Expand All @@ -103,6 +113,7 @@ module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-shell');

// A task to cause Grunt to sit and wait, keeping the test server running
grunt.registerTask("wait", function() {
Expand All @@ -118,6 +129,6 @@ module.exports = function(grunt) {
grunt.registerTask("test", ["jshint", "test-local"]);
}

grunt.registerTask("build", ["browserify", "uglify"]);
grunt.registerTask("build", ["browserify", "uglify", "shell:ts-declaration"]);
grunt.registerTask("default", ["jshint", "build"]);
};
30 changes: 30 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
declare function _getBinaryFromXHR(xhr: XMLHttpRequest): any;
declare type CallbackData = string | ArrayBuffer;
interface GetBinaryContentCallback {
(err: null, data: CallbackData): void;
(err: Error, data: null): void;
}
interface GetBinaryContentProgressData {
path: string;
originalEvent: ProgressEvent;
percent: number;
loaded: ProgressEvent['loaded'];
total: ProgressEvent['total'];
}
declare type GetBinaryContentProgress = (data: GetBinaryContentProgressData) => void;
declare type GetBinaryContentOptions = {
callback: GetBinaryContentCallback;
progress?: GetBinaryContentProgress;
};
/**
* @param {string} path The path to the resource to GET.
* @param {function|{callback: function, progress: function}} options
* @return {Promise|undefined} If no callback is passed then a promise is returned
*/
declare function getBinaryContent(path: string, options?: never): Promise<CallbackData>;
declare function getBinaryContent(path: string, options: GetBinaryContentCallback | GetBinaryContentOptions): void;
declare const JSZipUtils: {
_getBinaryFromXHR: typeof _getBinaryFromXHR;
getBinaryContent: typeof getBinaryContent;
};
export default JSZipUtils;
9 changes: 5 additions & 4 deletions dist/jszip-utils-ie.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ JSZipUtils - A collection of cross-browser utilities to go along with JSZip.
Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip-utils/master/LICENSE.markdown.

*/
;(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var global=typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {};/* jshint evil: true, newcap: false */
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){(function (){
/* jshint evil: true, newcap: false */
/* global IEBinaryToArray_ByteStr, IEBinaryToArray_ByteStr_Last */
"use strict";

Expand Down Expand Up @@ -52,5 +53,5 @@ global.JSZipUtils._getBinaryFromXHR = function (xhr) {
// enforcing Stuk's coding style
// vim: set shiftwidth=4 softtabstop=4:

},{}]},{},[1])
;
}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}]},{},[1]);
2 changes: 1 addition & 1 deletion dist/jszip-utils-ie.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 54 additions & 80 deletions dist/jszip-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,31 @@ JSZipUtils - A collection of cross-browser utilities to go along with JSZip.
Dual licenced under the MIT license or GPLv3. See https://raw.github.com/Stuk/jszip-utils/master/LICENSE.markdown.

*/
!function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.JSZipUtils=e():"undefined"!=typeof global?global.JSZipUtils=e():"undefined"!=typeof self&&(self.JSZipUtils=e())}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.JSZipUtils = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
/*globals Promise */

var JSZipUtils = {};
exports.__esModule = true;
// 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
// do it, see https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data
JSZipUtils._getBinaryFromXHR = function (xhr) {
function _getBinaryFromXHR(xhr) {
// for xhr.responseText, the 0xFF mask is applied by JSZip
return xhr.response || xhr.responseText;
};

}
// taken from jQuery
function createStandardXHR() {
try {
return new window.XMLHttpRequest();
} catch( e ) {}
}
catch (e) { }
}

function createActiveXHR() {
try {
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch( e ) {}
}
catch (e) { }
}

// Create the request object
var createXHR = (typeof window !== "undefined" && window.ActiveXObject) ?
/* Microsoft failed to properly
Expand All @@ -42,45 +40,27 @@ var createXHR = (typeof window !== "undefined" && window.ActiveXObject) ?
* Additionally XMLHttpRequest can be disabled in IE7/IE8 so
* we need a fallback.
*/
function() {
return createStandardXHR() || createActiveXHR();
} :
function () {
return createStandardXHR() || createActiveXHR();
} :
// For all other browsers, use the standard XMLHttpRequest object
createStandardXHR;


/**
* @param {string} path The path to the resource to GET.
* @param {function|{callback: function, progress: function}} options
* @return {Promise|undefined} If no callback is passed then a promise is returned
*/
JSZipUtils.getBinaryContent = function (path, options) {
var promise, resolve, reject;
var callback;

if (!options) {
options = {};
}

// backward compatible callback
if (typeof options === "function") {
callback = options;
options = {};
} else if (typeof options.callback === 'function') {
function getBinaryContent(path, options) {
var callback = typeof options === 'object'
// callback inside options object
callback = options.callback;
}

? options.callback
// backward compatible callback or undefined callback (when call async)
: options;
var progress = typeof options === 'object' ? options.progress : undefined;
var promise;
var resolve = function (data) { callback === null || callback === void 0 ? void 0 : callback(null, data); };
var reject = function (err) { callback === null || callback === void 0 ? void 0 : callback(err, null); };
if (!callback && typeof Promise !== "undefined") {
promise = new Promise(function (_resolve, _reject) {
resolve = _resolve;
reject = _reject;
});
} else {
resolve = function (data) { callback(null, data); };
reject = function (err) { callback(err, null); };
}

/*
* Here is the tricky part : getting the data.
* In firefox/chrome/opera/... setting the mimeType to 'text/plain; charset=x-user-defined'
Expand All @@ -96,65 +76,59 @@ JSZipUtils.getBinaryContent = function (path, options) {
* the responseType attribute : http://bugs.jquery.com/ticket/11461
*/
try {
var xhr = createXHR();

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

var xhr_1 = createXHR();
xhr_1.open('GET', path, true);
// recent browsers
if ("responseType" in xhr) {
xhr.responseType = "arraybuffer";
if ("responseType" in xhr_1) {
xhr_1.responseType = "arraybuffer";
}

// older browser
if(xhr.overrideMimeType) {
xhr.overrideMimeType("text/plain; charset=x-user-defined");
if (xhr_1.overrideMimeType) {
xhr_1.overrideMimeType("text/plain; charset=x-user-defined");
}

xhr.onreadystatechange = function (event) {
xhr_1.onreadystatechange = function () {
// use `xhr` and not `this`... thanks IE
if (xhr.readyState === 4) {
if (xhr.status === 200 || xhr.status === 0) {
if (xhr_1.readyState === 4) {
if (xhr_1.status === 200 || xhr_1.status === 0) {
try {
resolve(JSZipUtils._getBinaryFromXHR(xhr));
} catch(err) {
resolve(JSZipUtils._getBinaryFromXHR(xhr_1));
}
catch (err) {
reject(new Error(err));
}
} else {
}
else {
reject(new Error("Ajax error for " + path + " : " + this.status + " " + this.statusText));
}
}
};

if(options.progress) {
xhr.onprogress = function(e) {
options.progress({
path: path,
originalEvent: e,
percent: e.loaded / e.total * 100,
loaded: e.loaded,
total: e.total
});
};
}

xhr.send();

} catch (e) {
reject(new Error(e), null);
xhr_1.onprogress = function (event) {
progress === null || progress === void 0 ? void 0 : progress({
path: path,
originalEvent: event,
percent: event.loaded / event.total * 100,
loaded: event.loaded,
total: event.total
});
};
xhr_1.send();
}
catch (e) {
reject(new Error(e));
}

// returns a promise or undefined depending on whether a callback was
// provided
return promise;
}
var JSZipUtils = {
_getBinaryFromXHR: _getBinaryFromXHR,
getBinaryContent: getBinaryContent
};

// export
module.exports = JSZipUtils;

exports["default"] = JSZipUtils;
// enforcing Stuk's coding style
// vim: set shiftwidth=4 softtabstop=4:

},{}]},{},[1])
(1)
},{}]},{},[1])(1)
});
;
2 changes: 1 addition & 1 deletion dist/jszip-utils.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading