@sugarcube/plugin-http
Version:
HTTP related plugins for sugarcube.
57 lines (47 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _fp = require("lodash/fp");
var _dashp = require("dashp");
var _path = require("path");
var _core = require("@sugarcube/core");
var _utils = require("../utils");
const wgetTypes = ["url"];
const fetchPage = (envelope, {
cfg,
log
}) => {
const cmd = (0, _fp.get)("http.wget_cmd", cfg);
const dataDir = (0, _fp.get)("http.data_dir", cfg);
return _core.envelope.fmapDataAsync(unit => (0, _dashp.collectP)(media => {
if (!(0, _fp.includes)(media.type, wgetTypes)) return media;
const {
type,
term
} = media;
const idHash = media._sc_id_hash;
const location = (0, _path.join)(dataDir, unit._sc_id_hash, "wget", idHash);
return (0, _dashp.flowP)([(0, _utils.wget)(cmd, location), () => unit._sc_downloads.push({
location,
type,
term
}), (0, _dashp.caughtP)(e => // FIXME: Wget breaks a lot with error code 8.
log.error(`Wget on ${term} failed with ${e}`)), (0, _dashp.tapP)(() => log.info(`Wget'ed ${term} to ${location}.`)), () => media], term);
}, unit._sc_media).then(ms => (0, _fp.merge)(unit, {
_sc_media: ms
})), envelope);
};
const plugin = _core.plugin.liftManyA2([_utils.assertDir, fetchPage]);
plugin.desc = "Fetch whole pages using wget.";
plugin.argv = {
"http.wget_cmd": {
type: "string",
nargs: 1,
default: "wget",
desc: "The path to the wget command."
}
};
var _default = plugin;
exports.default = _default;