insomnia/packages/insomnia-app/app/node-libcurl/curl.js

88 lines
1.9 KiB
JavaScript
Raw Normal View History

import nodeLibcurl from 'inlc';
export class Curl {
2018-06-25 17:42:50 +00:00
constructor() {
this._handle = null;
this._options = [];
this._features = [];
this._handle = new nodeLibcurl.Curl();
}
2018-06-25 17:42:50 +00:00
setOpt(option, value) {
if (!Object.keys(Curl.option).find(k => Curl.option[k])) {
throw new Error(`Cannot setOpt for unknown option ${option}`);
}
// Throw on deprecated options
const disabledOpts = {
[nodeLibcurl.Curl.option.URL]: this.setUrl,
};
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`);
}
this._handle.setOpt(option, value);
}
2018-06-25 17:42:50 +00:00
setUrl(url) {
this._handle.setOpt(Curl.option.URL, url);
}
2018-06-25 17:42:50 +00:00
static getVersion() {
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);
return name || opt;
}
2018-06-25 17:42:50 +00:00
getInfo(property) {
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) {
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() {
this._handle.perform();
}
2018-06-25 17:42:50 +00:00
close() {
if (this._handle) {
try {
this._handle.close();
} catch (err) {
// Handle probably closed already
}
}
}
2018-06-25 17:42:50 +00:00
cancel() {
// TODO
this.close();
}
2018-06-25 17:42:50 +00:00
on(name, fn) {
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;