2020-04-29 19:59:46 +00:00
|
|
|
import nodeLibcurl from 'inlc';
|
2017-11-30 16:43:30 +00:00
|
|
|
|
2020-04-29 19:59:46 +00:00
|
|
|
export class Curl {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor() {
|
2017-11-30 16:43:30 +00:00
|
|
|
this._handle = null;
|
|
|
|
this._options = [];
|
|
|
|
this._features = [];
|
|
|
|
this._handle = new nodeLibcurl.Curl();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
setOpt(option, value) {
|
2017-11-30 16:43:30 +00:00
|
|
|
if (!Object.keys(Curl.option).find(k => Curl.option[k])) {
|
|
|
|
throw new Error(`Cannot setOpt for unknown option ${option}`);
|
|
|
|
}
|
|
|
|
|
2017-12-01 14:18:38 +00:00
|
|
|
// Throw on deprecated options
|
|
|
|
const disabledOpts = {
|
2018-12-12 17:36:11 +00:00
|
|
|
[nodeLibcurl.Curl.option.URL]: this.setUrl,
|
2017-12-01 14:18:38 +00:00
|
|
|
};
|
|
|
|
if (disabledOpts[option]) {
|
|
|
|
const name = Curl.optName(option);
|
|
|
|
const newName = disabledOpts[option].name;
|
2018-10-17 16:42:33 +00:00
|
|
|
throw new Error(`setOpt(${name}) is deprecated. Please use ${newName}() instead`);
|
2017-12-01 14:18:38 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 16:43:30 +00:00
|
|
|
this._handle.setOpt(option, value);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
setUrl(url) {
|
2017-12-01 14:18:38 +00:00
|
|
|
this._handle.setOpt(Curl.option.URL, url);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
static getVersion() {
|
2017-11-30 16:43:30 +00:00
|
|
|
return nodeLibcurl.Curl.getVersion();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
static optName(opt) {
|
2018-10-17 16:42:33 +00:00
|
|
|
const name = Object.keys(Curl.option).find(name => Curl.option[name] === opt);
|
2017-12-01 14:18:38 +00:00
|
|
|
return name || opt;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
getInfo(property) {
|
2017-11-30 16:43:30 +00:00
|
|
|
if (!Object.keys(Curl.info).find(k => Curl.info[k])) {
|
|
|
|
throw new Error(`Cannot getInfo for unknown option ${property}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._handle.getInfo(property);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
enable(feature) {
|
2017-11-30 16:43:30 +00:00
|
|
|
if (!Object.keys(Curl.feature).find(k => Curl.feature[k])) {
|
|
|
|
throw new Error(`Cannot enable unknown feature ${feature}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._handle.enable(feature);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
perform() {
|
2017-11-30 16:43:30 +00:00
|
|
|
this._handle.perform();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
close() {
|
2017-11-30 16:43:30 +00:00
|
|
|
if (this._handle) {
|
|
|
|
try {
|
|
|
|
this._handle.close();
|
|
|
|
} catch (err) {
|
|
|
|
// Handle probably closed already
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
cancel() {
|
2017-11-30 16:43:30 +00:00
|
|
|
// TODO
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
on(name, fn) {
|
2017-11-30 16:43:30 +00:00
|
|
|
this._handle.on(name, fn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Curl.feature = nodeLibcurl.Curl.feature;
|
|
|
|
Curl.option = nodeLibcurl.Curl.option;
|
|
|
|
Curl.auth = nodeLibcurl.Curl.auth;
|
|
|
|
Curl.code = nodeLibcurl.Curl.code;
|
|
|
|
Curl.netrc = nodeLibcurl.Curl.netrc;
|
|
|
|
Curl.info = nodeLibcurl.Curl.info;
|