2017-04-12 21:17:40 +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 {
|
|
|
|
constructor () {
|
|
|
|
super();
|
|
|
|
this._options = {};
|
2017-10-11 21:43:21 +00:00
|
|
|
this._meta = {};
|
2017-04-12 21:17:40 +00:00
|
|
|
}
|
2017-10-28 22:16:34 +00:00
|
|
|
|
2017-04-12 21:17:40 +00:00
|
|
|
setOpt (name, value) {
|
|
|
|
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++) {
|
|
|
|
const buffer = new Buffer(23);
|
|
|
|
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) {
|
|
|
|
const {size} = fs.fstatSync(value);
|
|
|
|
const buffer = new Buffer(size);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getInfo (name) {
|
|
|
|
switch (name) {
|
|
|
|
case Curl.info.COOKIELIST:
|
|
|
|
return [`#HttpOnly_.insomnia.rest\tTRUE\t/url/path\tTRUE\t${Date.now() / 1000}\tfoo\tbar`];
|
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
perform () {
|
|
|
|
process.nextTick(() => {
|
|
|
|
const data = Buffer.from(JSON.stringify({
|
2017-10-11 21:43:21 +00:00
|
|
|
options: this._options,
|
|
|
|
meta: this._meta
|
2017-04-12 21:17:40 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
this.emit('data', data);
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
this.emit('end', 'NOT_USED', 'NOT_USED', [{
|
|
|
|
'Content-Length': `${data.length}`,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
result: {code: 200, reason: 'OK'}
|
|
|
|
}]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
close () {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
TEXT: 'TEXT'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Curl.auth = {
|
|
|
|
ANY: 'ANY'
|
|
|
|
};
|
|
|
|
|
2017-07-31 17:29:36 +00:00
|
|
|
Curl.netrc = {
|
|
|
|
IGNORED: 0,
|
|
|
|
OPTIONAL: 1,
|
|
|
|
REQUIRED: 2
|
|
|
|
};
|
|
|
|
|
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',
|
|
|
|
XFERINFOFUNCTION: 'XFERINFOFUNCTION'
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Curl: Curl
|
|
|
|
};
|