2018-06-25 17:42:50 +00:00
|
|
|
import { EventEmitter } from 'events';
|
2017-10-28 22:16:34 +00:00
|
|
|
import fs from 'fs';
|
2017-04-12 21:17:40 +00:00
|
|
|
|
|
|
|
class Curl extends EventEmitter {
|
2018-06-25 17:42:50 +00:00
|
|
|
constructor() {
|
2017-04-12 21:17:40 +00:00
|
|
|
super();
|
|
|
|
this._options = {};
|
2017-10-11 21:43:21 +00:00
|
|
|
this._meta = {};
|
2017-11-13 23:10:53 +00:00
|
|
|
this._features = {};
|
2017-04-12 21:17:40 +00:00
|
|
|
}
|
2017-10-28 22:16:34 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
static getVersion() {
|
2017-11-03 19:35:13 +00:00
|
|
|
return 'libcurl/7.54.0 LibreSSL/2.0.20 zlib/1.2.11 nghttp2/1.24.0';
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
enable(name) {
|
2017-11-13 23:10:53 +00:00
|
|
|
this._features[name] = true;
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
setUrl(url) {
|
2017-12-01 14:36:30 +00:00
|
|
|
this.setOpt(Curl.option.URL, url);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
setOpt(name, value) {
|
2017-04-12 21:17:40 +00:00
|
|
|
if (!name) {
|
|
|
|
throw new Error(`Invalid option ${name} ${value}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name === Curl.option.CAINFO) {
|
|
|
|
// Just ignore this because it's platform-specific
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-11 21:43:21 +00:00
|
|
|
if (name === Curl.option.READFUNCTION) {
|
2017-10-28 22:16:34 +00:00
|
|
|
let body = '';
|
|
|
|
// Only limiting this to prevent infinite loops
|
|
|
|
for (let i = 0; i < 1000; i++) {
|
2017-11-20 16:07:36 +00:00
|
|
|
const buffer = Buffer.alloc(23);
|
2017-10-28 22:16:34 +00:00
|
|
|
const bytes = value(buffer);
|
|
|
|
if (bytes === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
body += buffer.slice(0, bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._meta[`${name}_VALUE`] = body;
|
2017-10-11 21:43:21 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 21:17:40 +00:00
|
|
|
if (name === Curl.option.COOKIELIST) {
|
|
|
|
// This can be set multiple times
|
|
|
|
this._options[name] = this._options[name] || [];
|
|
|
|
this._options[name].push(value);
|
2017-10-28 22:16:34 +00:00
|
|
|
} else if (name === Curl.option.READDATA) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { size } = fs.fstatSync(value);
|
2017-11-20 16:07:36 +00:00
|
|
|
const buffer = Buffer.alloc(size);
|
2017-10-28 22:16:34 +00:00
|
|
|
fs.readSync(value, buffer, 0, size, 0);
|
|
|
|
this._options[name] = buffer.toString();
|
2017-04-12 21:17:40 +00:00
|
|
|
} else {
|
|
|
|
this._options[name] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
getInfo(name) {
|
2017-04-12 21:17:40 +00:00
|
|
|
switch (name) {
|
|
|
|
case Curl.info.COOKIELIST:
|
2018-10-17 16:42:33 +00:00
|
|
|
return [`#HttpOnly_.insomnia.rest\tTRUE\t/url/path\tTRUE\t${Date.now() / 1000}\tfoo\tbar`];
|
2017-04-12 21:17:40 +00:00
|
|
|
case Curl.info.EFFECTIVE_URL:
|
|
|
|
return this._options[Curl.option.URL];
|
|
|
|
case Curl.info.TOTAL_TIME:
|
|
|
|
return 700;
|
|
|
|
case Curl.info.SIZE_DOWNLOAD:
|
|
|
|
return 800;
|
|
|
|
default:
|
|
|
|
throw new Error(`Invalid info ${name}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
perform() {
|
2017-04-12 21:17:40 +00:00
|
|
|
process.nextTick(() => {
|
2018-06-25 17:42:50 +00:00
|
|
|
const data = Buffer.from(
|
|
|
|
JSON.stringify({
|
|
|
|
options: this._options,
|
|
|
|
meta: this._meta,
|
2018-12-12 17:36:11 +00:00
|
|
|
features: this._features,
|
|
|
|
}),
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-04-12 21:17:40 +00:00
|
|
|
|
|
|
|
this.emit('data', data);
|
2017-11-21 17:49:17 +00:00
|
|
|
this._options.WRITEFUNCTION(data);
|
2017-04-12 21:17:40 +00:00
|
|
|
|
|
|
|
process.nextTick(() => {
|
2018-06-25 17:42:50 +00:00
|
|
|
this.emit(
|
|
|
|
'end',
|
|
|
|
'NOT_USED',
|
|
|
|
'NOT_USED',
|
|
|
|
[
|
|
|
|
'HTTP/1.1 200 OK',
|
|
|
|
`Content-Length: ${data.length}`,
|
|
|
|
'Content-Type: application/json',
|
2018-12-12 17:36:11 +00:00
|
|
|
'',
|
|
|
|
].join('\n'),
|
2018-06-25 17:42:50 +00:00
|
|
|
);
|
2017-04-12 21:17:40 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
close() {}
|
2017-04-12 21:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Curl.info = {
|
|
|
|
COOKIELIST: 'COOKIELIST',
|
|
|
|
EFFECTIVE_URL: 'EFFECTIVE_URL',
|
|
|
|
SIZE_DOWNLOAD: 'SIZE_DOWNLOAD',
|
|
|
|
TOTAL_TIME: 'TOTAL_TIME',
|
|
|
|
debug: {
|
|
|
|
DATA_IN: 'DATA_IN',
|
|
|
|
DATA_OUT: 'DATA_OUT',
|
|
|
|
SSL_DATA_IN: 'SSL_DATA_IN',
|
|
|
|
SSL_DATA_OUT: 'SSL_DATA_OUT',
|
2018-12-12 17:36:11 +00:00
|
|
|
TEXT: 'TEXT',
|
|
|
|
},
|
2017-04-12 21:17:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Curl.auth = {
|
2018-12-12 17:36:11 +00:00
|
|
|
ANY: 'ANY',
|
2017-04-12 21:17:40 +00:00
|
|
|
};
|
|
|
|
|
2017-07-31 17:29:36 +00:00
|
|
|
Curl.netrc = {
|
|
|
|
IGNORED: 0,
|
|
|
|
OPTIONAL: 1,
|
2018-12-12 17:36:11 +00:00
|
|
|
REQUIRED: 2,
|
2017-07-31 17:29:36 +00:00
|
|
|
};
|
|
|
|
|
2017-11-13 23:10:53 +00:00
|
|
|
Curl.feature = {
|
2017-11-21 17:49:17 +00:00
|
|
|
NO_HEADER_PARSING: 'NO_HEADER_PARSING',
|
2018-12-12 17:36:11 +00:00
|
|
|
NO_DATA_PARSING: 'NO_DATA_PARSING',
|
2017-11-13 23:10:53 +00:00
|
|
|
};
|
|
|
|
|
2017-04-12 21:17:40 +00:00
|
|
|
Curl.option = {
|
2017-07-26 00:49:22 +00:00
|
|
|
ACCEPT_ENCODING: 'ACCEPT_ENCODING',
|
2017-04-12 21:17:40 +00:00
|
|
|
CAINFO: 'CAINFO',
|
|
|
|
COOKIEFILE: 'COOKIEFILE',
|
|
|
|
COOKIELIST: 'COOKIELIST',
|
|
|
|
CUSTOMREQUEST: 'CUSTOMREQUEST',
|
|
|
|
DEBUGFUNCTION: 'DEBUGFUNCTION',
|
|
|
|
FOLLOWLOCATION: 'FOLLOWLOCATION',
|
|
|
|
HTTPAUTH: 'HTTPAUTH',
|
2017-07-26 00:49:22 +00:00
|
|
|
HTTPGET: 'HTTPGET',
|
2017-04-12 21:17:40 +00:00
|
|
|
HTTPHEADER: 'HTTPHEADER',
|
|
|
|
HTTPPOST: 'HTTPPOST',
|
2017-09-09 12:48:27 +00:00
|
|
|
INFILESIZE_LARGE: 'INFILESIZE_LARGE',
|
2017-04-12 21:17:40 +00:00
|
|
|
KEYPASSWD: 'KEYPASSWD',
|
2017-08-10 15:44:24 +00:00
|
|
|
MAXREDIRS: 'MAXREDIRS',
|
2017-07-31 17:29:36 +00:00
|
|
|
NETRC: 'NETRC',
|
2017-07-26 00:49:22 +00:00
|
|
|
NOBODY: 'NOBODY',
|
2017-04-12 21:17:40 +00:00
|
|
|
NOPROGRESS: 'NOPROGRESS',
|
2017-06-06 20:21:59 +00:00
|
|
|
NOPROXY: 'NOPROXY',
|
2017-04-12 21:17:40 +00:00
|
|
|
PASSWORD: 'PASSWORD',
|
2017-07-26 00:49:22 +00:00
|
|
|
POST: 'POST',
|
2017-04-12 21:17:40 +00:00
|
|
|
POSTFIELDS: 'POSTFIELDS',
|
|
|
|
PROXY: 'PROXY',
|
|
|
|
PROXYAUTH: 'PROXYAUTH',
|
|
|
|
READDATA: 'READDATA',
|
2017-10-11 21:43:21 +00:00
|
|
|
READFUNCTION: 'READFUNCTION',
|
2017-04-12 21:17:40 +00:00
|
|
|
SSLCERT: 'SSLCERT',
|
|
|
|
SSLCERTTYPE: 'SSLCERTTYPE',
|
|
|
|
SSLKEY: 'SSLKEY',
|
|
|
|
SSL_VERIFYHOST: 'SSL_VERIFYHOST',
|
|
|
|
SSL_VERIFYPEER: 'SSL_VERIFYPEER',
|
|
|
|
TIMEOUT_MS: 'TIMEOUT_MS',
|
2017-07-26 00:49:22 +00:00
|
|
|
UNIX_SOCKET_PATH: 'UNIX_SOCKET_PATH',
|
2017-04-12 21:17:40 +00:00
|
|
|
UPLOAD: 'UPLOAD',
|
|
|
|
URL: 'URL',
|
|
|
|
USERAGENT: 'USERAGENT',
|
|
|
|
USERNAME: 'USERNAME',
|
|
|
|
VERBOSE: 'VERBOSE',
|
2017-11-21 17:49:17 +00:00
|
|
|
WRITEFUNCTION: 'WRITEFUNCTION',
|
2018-12-12 17:36:11 +00:00
|
|
|
XFERINFOFUNCTION: 'XFERINFOFUNCTION',
|
2017-04-12 21:17:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2018-12-12 17:36:11 +00:00
|
|
|
Curl: Curl,
|
2017-04-12 21:17:40 +00:00
|
|
|
};
|