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

Native promises #26

Merged
merged 8 commits into from
Oct 11, 2018
Merged
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
language: node_js
node_js:
- "6"
- "8"
- "9"
- "10"

script:
- npm test
43 changes: 26 additions & 17 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"use strict";

const doRequest = require("request");
const nodefn = require("when/node");
const _ = require("lodash");
const version = require("../package.json").version;

/**
Expand All @@ -14,37 +12,48 @@ const version = require("../package.json").version;
* @returns {Promise}
*/
function request(baseConfig, method, depth, xml) {
const config = _.assign({}, baseConfig);

config.headers = _.assign({}, baseConfig.headers);
const config = {
headers: baseConfig.headers || {},
body: xml,
method,
'headers["Content-length"]': xml.length,
'headers["Depth"]': depth,
antosan marked this conversation as resolved.
Show resolved Hide resolved
...baseConfig
};

if (!baseConfig.headers || "User-Agent" in baseConfig.headers === false) {
config.headers["User-Agent"] = "scrapegoat/" + version;
}

if (baseConfig.auth) {
config.auth = _.assign({}, baseConfig.auth);
config.auth.sendImmediately = "sendImmediately" in baseConfig.auth ? baseConfig.auth.sendImmediately : false;
config.auth = {
sendImmediately: false,
...baseConfig.auth
tannerbaum marked this conversation as resolved.
Show resolved Hide resolved
};
}

config.body = xml;
config.method = method;
config.headers["Content-length"] = xml.length;
config.headers.Depth = depth;
config.timeout = baseConfig.timeout ? baseConfig.timeout : 10000;

return nodefn.call(doRequest, config)
.spread((res, body) => {
let err;
return new Promise((resolve, reject) => {
doRequest(config, (error, res, body) => {
if (error) {
reject(error);
return;
}

if (res.statusCode >= 300) {
err = new Error("Response with status code: " + res.statusCode);
const err = new Error(
"Response with status code: " + res.statusCode
);

err.statusCode = res.statusCode;
throw err;
reject(err);
return;
}

return body;
resolve(body);
});
});
antosan marked this conversation as resolved.
Show resolved Hide resolved
}

module.exports = request;
11 changes: 6 additions & 5 deletions lib/xml/parser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"use strict";

const parseXMLString = require("xml2js").parseString;
const nodefn = require("when/node");
const { promisify } = require("util");
const { parseString } = require("xml2js");
const parseXMLString = promisify(parseString);
const ICAL = require("ical.js");
const moment = require("moment");

// parse calendar object
function parseCalendarMultistatus(xml) {
return nodefn.call(parseXMLString, xml).then((result) => {
return parseXMLString(xml).then((result) => {
const parsed = {};

if (!result["d:multistatus"] || !result["d:multistatus"]["d:response"]) {
Expand All @@ -27,7 +28,7 @@ function parseEventsMultistatus(xml) {
let parsed;
const formatted = [];

return nodefn.call(parseXMLString, xml).then((result) => {
return parseXMLString(xml).then((result) => {
if (!result["d:multistatus"] || !result["d:multistatus"]["d:response"]) {
return formatted;
}
Expand Down Expand Up @@ -134,7 +135,7 @@ function parseEvents(xml) {
let parsed;
const formatted = [];

return nodefn.call(parseXMLString, xml).then((result) => {
return parseXMLString(xml).then((result) => {
if (!result["d:multistatus"] || !result["d:multistatus"]["d:response"]) {
return formatted;
}
Expand Down
Loading