Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
module.exports = function ajax(url, data) {
var deferred = D();
try {
var x = new (XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
x.open(data ? 'POST' : 'GET', url, 1);
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
x.onreadystatechange = function () {
x.readyState > 3 && deferred.resolve({ 'data': x.responseText, 'xhr': x });
};
x.send(data)
} catch (e) {
console.error(e);
deferred.reject(e);
}
return deferred.promise;
};
}());
this.make = function(epubConfig) {
console.debug('building epub', epubConfig);
var zip = new JSZip();
var deferred = D();
D.all(
addMimetype(zip),
addContainerInfo(zip, epubConfig),
addManifestOpf(zip, epubConfig),
addCover(zip, epubConfig),
addEpub2Nav(zip, epubConfig),
addEpub3Nav(zip, epubConfig),
addStylesheets(zip, epubConfig),
addContent(zip, epubConfig)
).then(function() {
deferred.resolve(zip);
});
return deferred.promise;
};
function addStylesheets(zip, epubConfig) {
var deferred = D();
if(epubConfig.stylesheet.url) {
return ajax(epubConfig.stylesheet.url).then(function(result) {
epubConfig.styles = result.data;
compileAndAddCss();
});
} else {
compileAndAddCss();
}
return deferred.promise;
function compileAndAddCss() {
var styles = {
original: epubConfig.stylesheet.replaceOriginal ? '' : templates.css,
custom: epubConfig.styles
};
zip.folder('EPUB').folder('css').file('main.css', compile('{{{original}}}{{{custom}}}', styles, true));
function addFiles(zip, epubConfig) {
var deferred_list = [];
for(var i = 0; i < epubConfig.additionalFiles.length; i++) {
var file = epubConfig.additionalFiles[i];
var deferred = new D();
JSZipUtils.getBinaryContent(file.url, (function(file, deferred) {
return function(err, data) {
if(!err) {
zip.folder('EPUB').folder(file.folder).file(file.filename, data, { binary: true });
deferred.resolve('');
} else {
deferred.reject(err);
}
};
})(file, deferred));
deferred_list.push(deferred.promise);
}
return D.all(deferred_list);
}
, getAllSince: function(what, since, params){
since.toISOString && (since = since.toISOString());
params || (params = {} );
params.sort = 'updated_on:desc';
params.limit || (params.limit=100);
var self = this
, d = D()
, res = []
, getNext = function getNext(data){
var fulfilled=false;
data[what].forEach(function(v){
if( since >= v.updated_on ){
fulfilled=true;
}
fulfilled || res.push(v);
});
if( (!fulfilled) && (data.total_count > (data.limit+data.offset)) ){
params.offset = data.limit+data.offset;
params.limit = data.limit;
return self.get(what, params).success(getNext).rethrow(d.reject);
}
return d.resolve(res);
}
function addStylesheets(zip, epubConfig) {
var deferred = D();
if (epubConfig.stylesheet.url) {
return ajax(epubConfig.stylesheet.url).then(function(result) {
epubConfig.styles = result.data;
compileAndAddCss();
});
} else {
compileAndAddCss();
}
return deferred.promise;
function compileAndAddCss() {
var styles = {
original: epubConfig.stylesheet.replaceOriginal ? '' : templates.css,
custom: epubConfig.styles
};
zip.folder('EPUB').file(epubConfig.slug + '.css', compile('{{{original}}}{{{custom}}}', styles, true));
, request: function(method, path, params){
params || (params = {});
var self = this
, d = D()
, keyPass = keyPassphrases[this.keyPassId]
, retry = params.retry || {}
, verbose = this.verbose
, impersonation
, options
, req
;
if (params.retry) {
delete params.retry;
}
retry.count || (retry.count = 1);
retry.maxTry || (retry.maxTry = self.maxTry);
retry.maxDelay || (retry.maxDelay = self.maxDelay);
if (params.impersonate) {
impersonation = params.impersonate;
function addCover(zip, epubConfig) {
var deferred = D();
if (epubConfig.coverUrl) {
JSZipUtils.getBinaryContent(epubConfig.coverUrl, function (err, data) {
if (!err) {
var ext = epubConfig.coverUrl.substr(epubConfig.coverUrl.lastIndexOf('.') + 1);
zip.folder('EPUB').file(epubConfig.slug + '-cover.' + ext, data, { binary: true });
deferred.resolve('');
} else {
deferred.reject(err);
}
});
} else {
deferred.resolve(true);
}
return deferred.promise;
}
function addCover(zip, epubConfig) {
var deferred = D();
if (epubConfig.coverUrl) {
JSZipUtils.getBinaryContent(epubConfig.coverUrl, function (err, data) {
if (!err) {
var ext = epubConfig.coverUrl.substr(epubConfig.coverUrl.lastIndexOf('.') + 1);
zip.folder('EPUB').file(epubConfig.slug + '-cover.' + ext, data, { binary: true });
deferred.resolve('');
} else {
deferred.reject(err);
}
});
} else {
deferred.resolve(true);
}
return deferred.promise;
}
function addStylesheets(zip, epubConfig) {
var deferred = D();
if (epubConfig.stylesheet.url) {
return ajax(epubConfig.stylesheet.url).then(function(result) {
epubConfig.styles = result.data;
compileAndAddCss();
});
} else {
compileAndAddCss();
}
return deferred.promise;
function compileAndAddCss() {
var styles = {
original: epubConfig.stylesheet.replaceOriginal ? '' : templates.css,
custom: epubConfig.styles
};
zip.folder('EPUB').file(epubConfig.slug + '.css', compile('{{{original}}}{{{custom}}}', styles, true));