First Commit of new way of building the Platform
This commit is contained in:
1 parent
e48cf0dae5
commit
40e60b42cc
4616 files changed
+713402
No files matched your search
+108
@@ -0,0 +1,108 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
// eslint-disable-next-line node/no-deprecated-api
|
||||
var _require = require('url'),
|
||||
parse = _require.parse;
|
||||
|
||||
var _require2 = require('cookiejar'),
|
||||
CookieJar = _require2.CookieJar;
|
||||
|
||||
var _require3 = require('cookiejar'),
|
||||
CookieAccessInfo = _require3.CookieAccessInfo;
|
||||
|
||||
var methods = require('methods');
|
||||
|
||||
var request = require('../..');
|
||||
|
||||
var AgentBase = require('../agent-base');
|
||||
/**
|
||||
* Expose `Agent`.
|
||||
*/
|
||||
|
||||
|
||||
module.exports = Agent;
|
||||
/**
|
||||
* Initialize a new `Agent`.
|
||||
*
|
||||
* @api public
|
||||
*/
|
||||
|
||||
function Agent(options) {
|
||||
if (!(this instanceof Agent)) {
|
||||
return new Agent(options);
|
||||
}
|
||||
|
||||
AgentBase.call(this);
|
||||
this.jar = new CookieJar();
|
||||
|
||||
if (options) {
|
||||
if (options.ca) {
|
||||
this.ca(options.ca);
|
||||
}
|
||||
|
||||
if (options.key) {
|
||||
this.key(options.key);
|
||||
}
|
||||
|
||||
if (options.pfx) {
|
||||
this.pfx(options.pfx);
|
||||
}
|
||||
|
||||
if (options.cert) {
|
||||
this.cert(options.cert);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Agent.prototype = Object.create(AgentBase.prototype);
|
||||
/**
|
||||
* Save the cookies in the given `res` to
|
||||
* the agent's cookie jar for persistence.
|
||||
*
|
||||
* @param {Response} res
|
||||
* @api private
|
||||
*/
|
||||
|
||||
Agent.prototype._saveCookies = function (res) {
|
||||
var cookies = res.headers['set-cookie'];
|
||||
if (cookies) this.jar.setCookies(cookies);
|
||||
};
|
||||
/**
|
||||
* Attach cookies when available to the given `req`.
|
||||
*
|
||||
* @param {Request} req
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
Agent.prototype._attachCookies = function (req) {
|
||||
var url = parse(req.url);
|
||||
var access = new CookieAccessInfo(url.hostname, url.pathname, url.protocol === 'https:');
|
||||
var cookies = this.jar.getCookies(access).toValueString();
|
||||
req.cookies = cookies;
|
||||
};
|
||||
|
||||
methods.forEach(function (name) {
|
||||
var method = name.toUpperCase();
|
||||
|
||||
Agent.prototype[name] = function (url, fn) {
|
||||
var req = new request.Request(method, url);
|
||||
req.on('response', this._saveCookies.bind(this));
|
||||
req.on('redirect', this._saveCookies.bind(this));
|
||||
req.on('redirect', this._attachCookies.bind(this, req));
|
||||
|
||||
this._attachCookies(req);
|
||||
|
||||
this._setDefaults(req);
|
||||
|
||||
if (fn) {
|
||||
req.end(fn);
|
||||
}
|
||||
|
||||
return req;
|
||||
};
|
||||
});
|
||||
Agent.prototype.del = Agent.prototype.delete;
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
"use strict";
|
||||
|
||||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
|
||||
|
||||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||||
|
||||
var Stream = require('stream');
|
||||
|
||||
var util = require('util');
|
||||
|
||||
var net = require('net');
|
||||
|
||||
var tls = require('tls'); // eslint-disable-next-line node/no-deprecated-api
|
||||
|
||||
|
||||
var _require = require('url'),
|
||||
parse = _require.parse;
|
||||
|
||||
var semver = require('semver');
|
||||
|
||||
var http2;
|
||||
if (semver.gte(process.version, 'v10.10.0')) http2 = require('http2');else throw new Error('superagent: this version of Node.js does not support http2');
|
||||
var _http2$constants = http2.constants,
|
||||
HTTP2_HEADER_PATH = _http2$constants.HTTP2_HEADER_PATH,
|
||||
HTTP2_HEADER_STATUS = _http2$constants.HTTP2_HEADER_STATUS,
|
||||
HTTP2_HEADER_METHOD = _http2$constants.HTTP2_HEADER_METHOD,
|
||||
HTTP2_HEADER_AUTHORITY = _http2$constants.HTTP2_HEADER_AUTHORITY,
|
||||
HTTP2_HEADER_HOST = _http2$constants.HTTP2_HEADER_HOST,
|
||||
HTTP2_HEADER_SET_COOKIE = _http2$constants.HTTP2_HEADER_SET_COOKIE,
|
||||
NGHTTP2_CANCEL = _http2$constants.NGHTTP2_CANCEL;
|
||||
|
||||
function setProtocol(protocol) {
|
||||
return {
|
||||
request: function request(options) {
|
||||
return new Request(protocol, options);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function Request(protocol, options) {
|
||||
var _this = this;
|
||||
|
||||
Stream.call(this);
|
||||
var defaultPort = protocol === 'https:' ? 443 : 80;
|
||||
var defaultHost = 'localhost';
|
||||
var port = options.port || defaultPort;
|
||||
var host = options.host || defaultHost;
|
||||
delete options.port;
|
||||
delete options.host;
|
||||
this.method = options.method;
|
||||
this.path = options.path;
|
||||
this.protocol = protocol;
|
||||
this.host = host;
|
||||
delete options.method;
|
||||
delete options.path;
|
||||
|
||||
var sessionOptions = _objectSpread({}, options);
|
||||
|
||||
if (options.socketPath) {
|
||||
sessionOptions.socketPath = options.socketPath;
|
||||
sessionOptions.createConnection = this.createUnixConnection.bind(this);
|
||||
}
|
||||
|
||||
this._headers = {};
|
||||
var session = http2.connect("".concat(protocol, "//").concat(host, ":").concat(port), sessionOptions);
|
||||
this.setHeader('host', "".concat(host, ":").concat(port));
|
||||
session.on('error', function (err) {
|
||||
return _this.emit('error', err);
|
||||
});
|
||||
this.session = session;
|
||||
}
|
||||
/**
|
||||
* Inherit from `Stream` (which inherits from `EventEmitter`).
|
||||
*/
|
||||
|
||||
|
||||
util.inherits(Request, Stream);
|
||||
|
||||
Request.prototype.createUnixConnection = function (authority, options) {
|
||||
switch (this.protocol) {
|
||||
case 'http:':
|
||||
return net.connect(options.socketPath);
|
||||
|
||||
case 'https:':
|
||||
options.ALPNProtocols = ['h2'];
|
||||
options.servername = this.host;
|
||||
options.allowHalfOpen = true;
|
||||
return tls.connect(options.socketPath, options);
|
||||
|
||||
default:
|
||||
throw new Error('Unsupported protocol', this.protocol);
|
||||
}
|
||||
}; // eslint-disable-next-line no-unused-vars
|
||||
|
||||
|
||||
Request.prototype.setNoDelay = function (bool) {// We can not use setNoDelay with HTTP/2.
|
||||
// Node 10 limits http2session.socket methods to ones safe to use with HTTP/2.
|
||||
// See also https://nodejs.org/api/http2.html#http2_http2session_socket
|
||||
};
|
||||
|
||||
Request.prototype.getFrame = function () {
|
||||
var _method,
|
||||
_this2 = this;
|
||||
|
||||
if (this.frame) {
|
||||
return this.frame;
|
||||
}
|
||||
|
||||
var method = (_method = {}, _defineProperty(_method, HTTP2_HEADER_PATH, this.path), _defineProperty(_method, HTTP2_HEADER_METHOD, this.method), _method);
|
||||
var headers = this.mapToHttp2Header(this._headers);
|
||||
headers = Object.assign(headers, method);
|
||||
var frame = this.session.request(headers); // eslint-disable-next-line no-unused-vars
|
||||
|
||||
frame.once('response', function (headers, flags) {
|
||||
headers = _this2.mapToHttpHeader(headers);
|
||||
frame.headers = headers;
|
||||
frame.statusCode = headers[HTTP2_HEADER_STATUS];
|
||||
frame.status = frame.statusCode;
|
||||
|
||||
_this2.emit('response', frame);
|
||||
});
|
||||
this._headerSent = true;
|
||||
frame.once('drain', function () {
|
||||
return _this2.emit('drain');
|
||||
});
|
||||
frame.on('error', function (err) {
|
||||
return _this2.emit('error', err);
|
||||
});
|
||||
frame.on('close', function () {
|
||||
return _this2.session.close();
|
||||
});
|
||||
this.frame = frame;
|
||||
return frame;
|
||||
};
|
||||
|
||||
Request.prototype.mapToHttpHeader = function (headers) {
|
||||
var keys = Object.keys(headers);
|
||||
var http2Headers = {};
|
||||
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
var value = headers[key];
|
||||
key = key.toLowerCase();
|
||||
|
||||
switch (key) {
|
||||
case HTTP2_HEADER_SET_COOKIE:
|
||||
value = Array.isArray(value) ? value : [value];
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
http2Headers[key] = value;
|
||||
}
|
||||
|
||||
return http2Headers;
|
||||
};
|
||||
|
||||
Request.prototype.mapToHttp2Header = function (headers) {
|
||||
var keys = Object.keys(headers);
|
||||
var http2Headers = {};
|
||||
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
var value = headers[key];
|
||||
key = key.toLowerCase();
|
||||
|
||||
switch (key) {
|
||||
case HTTP2_HEADER_HOST:
|
||||
key = HTTP2_HEADER_AUTHORITY;
|
||||
value = /^http:\/\/|^https:\/\//.test(value) ? parse(value).host : value;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
http2Headers[key] = value;
|
||||
}
|
||||
|
||||
return http2Headers;
|
||||
};
|
||||
|
||||
Request.prototype.setHeader = function (name, value) {
|
||||
this._headers[name.toLowerCase()] = value;
|
||||
};
|
||||
|
||||
Request.prototype.getHeader = function (name) {
|
||||
return this._headers[name.toLowerCase()];
|
||||
};
|
||||
|
||||
Request.prototype.write = function (data, encoding) {
|
||||
var frame = this.getFrame();
|
||||
return frame.write(data, encoding);
|
||||
};
|
||||
|
||||
Request.prototype.pipe = function (stream, options) {
|
||||
var frame = this.getFrame();
|
||||
return frame.pipe(stream, options);
|
||||
};
|
||||
|
||||
Request.prototype.end = function (data) {
|
||||
var frame = this.getFrame();
|
||||
frame.end(data);
|
||||
}; // eslint-disable-next-line no-unused-vars
|
||||
|
||||
|
||||
Request.prototype.abort = function (data) {
|
||||
var frame = this.getFrame();
|
||||
frame.close(NGHTTP2_CANCEL);
|
||||
this.session.destroy();
|
||||
};
|
||||
|
||||
exports.setProtocol = setProtocol;
|
||||
+1350
File diff suppressed because it is too large.
Load diff
+12
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function (res, fn) {
|
||||
var data = []; // Binary data needs binary storage
|
||||
|
||||
res.on('data', function (chunk) {
|
||||
data.push(chunk);
|
||||
});
|
||||
res.on('end', function () {
|
||||
fn(null, Buffer.concat(data));
|
||||
});
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
"use strict";
|
||||
|
||||
exports['application/x-www-form-urlencoded'] = require('./urlencoded');
|
||||
exports['application/json'] = require('./json');
|
||||
exports.text = require('./text');
|
||||
|
||||
var binary = require('./image');
|
||||
|
||||
exports['application/octet-stream'] = binary;
|
||||
exports['application/pdf'] = binary;
|
||||
exports.image = binary;
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function (res, fn) {
|
||||
res.text = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', function (chunk) {
|
||||
res.text += chunk;
|
||||
});
|
||||
res.on('end', function () {
|
||||
var body;
|
||||
var err;
|
||||
|
||||
try {
|
||||
body = res.text && JSON.parse(res.text);
|
||||
} catch (err2) {
|
||||
err = err2; // issue #675: return the raw response if the response parsing fails
|
||||
|
||||
err.rawResponse = res.text || null; // issue #876: return the http status code if the response parsing fails
|
||||
|
||||
err.statusCode = res.statusCode;
|
||||
} finally {
|
||||
fn(err, body);
|
||||
}
|
||||
});
|
||||
};
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = function (res, fn) {
|
||||
res.text = '';
|
||||
res.setEncoding('utf8');
|
||||
res.on('data', function (chunk) {
|
||||
res.text += chunk;
|
||||
});
|
||||
res.on('end', fn);
|
||||
};
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
var qs = require('qs');
|
||||
|
||||
module.exports = function (res, fn) {
|
||||
res.text = '';
|
||||
res.setEncoding('ascii');
|
||||
res.on('data', function (chunk) {
|
||||
res.text += chunk;
|
||||
});
|
||||
res.on('end', function () {
|
||||
try {
|
||||
fn(null, qs.parse(res.text));
|
||||
} catch (err) {
|
||||
fn(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
var util = require('util');
|
||||
|
||||
var Stream = require('stream');
|
||||
|
||||
var ResponseBase = require('../response-base');
|
||||
/**
|
||||
* Expose `Response`.
|
||||
*/
|
||||
|
||||
|
||||
module.exports = Response;
|
||||
/**
|
||||
* Initialize a new `Response` with the given `xhr`.
|
||||
*
|
||||
* - set flags (.ok, .error, etc)
|
||||
* - parse header
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Object} options
|
||||
* @constructor
|
||||
* @extends {Stream}
|
||||
* @implements {ReadableStream}
|
||||
* @api private
|
||||
*/
|
||||
|
||||
function Response(req) {
|
||||
Stream.call(this);
|
||||
this.res = req.res;
|
||||
var res = this.res;
|
||||
this.request = req;
|
||||
this.req = req.req;
|
||||
this.text = res.text;
|
||||
this.body = res.body === undefined ? {} : res.body;
|
||||
this.files = res.files || {};
|
||||
this.buffered = req._resBuffered;
|
||||
this.headers = res.headers;
|
||||
this.header = this.headers;
|
||||
|
||||
this._setStatusProperties(res.statusCode);
|
||||
|
||||
this._setHeaderProperties(this.header);
|
||||
|
||||
this.setEncoding = res.setEncoding.bind(res);
|
||||
res.on('data', this.emit.bind(this, 'data'));
|
||||
res.on('end', this.emit.bind(this, 'end'));
|
||||
res.on('close', this.emit.bind(this, 'close'));
|
||||
res.on('error', this.emit.bind(this, 'error'));
|
||||
}
|
||||
/**
|
||||
* Inherit from `Stream`.
|
||||
*/
|
||||
|
||||
|
||||
util.inherits(Response, Stream); // eslint-disable-next-line new-cap
|
||||
|
||||
ResponseBase(Response.prototype);
|
||||
/**
|
||||
* Implements methods of a `ReadableStream`
|
||||
*/
|
||||
|
||||
Response.prototype.destroy = function (err) {
|
||||
this.res.destroy(err);
|
||||
};
|
||||
/**
|
||||
* Pause.
|
||||
*/
|
||||
|
||||
|
||||
Response.prototype.pause = function () {
|
||||
this.res.pause();
|
||||
};
|
||||
/**
|
||||
* Resume.
|
||||
*/
|
||||
|
||||
|
||||
Response.prototype.resume = function () {
|
||||
this.res.resume();
|
||||
};
|
||||
/**
|
||||
* Return an `Error` representative of this response.
|
||||
*
|
||||
* @return {Error}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
Response.prototype.toError = function () {
|
||||
var req = this.req;
|
||||
var method = req.method;
|
||||
var path = req.path;
|
||||
var msg = "cannot ".concat(method, " ").concat(path, " (").concat(this.status, ")");
|
||||
var err = new Error(msg);
|
||||
err.status = this.status;
|
||||
err.text = this.text;
|
||||
err.method = method;
|
||||
err.path = path;
|
||||
return err;
|
||||
};
|
||||
|
||||
Response.prototype.setStatusProperties = function (status) {
|
||||
console.warn('In superagent 2.x setStatusProperties is a private method');
|
||||
return this._setStatusProperties(status);
|
||||
};
|
||||
/**
|
||||
* To json.
|
||||
*
|
||||
* @return {Object}
|
||||
* @api public
|
||||
*/
|
||||
|
||||
|
||||
Response.prototype.toJSON = function () {
|
||||
return {
|
||||
req: this.request.toJSON(),
|
||||
header: this.header,
|
||||
status: this.status,
|
||||
text: this.text
|
||||
};
|
||||
};
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
"use strict";
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
var _require = require('string_decoder'),
|
||||
StringDecoder = _require.StringDecoder;
|
||||
|
||||
var Stream = require('stream');
|
||||
|
||||
var zlib = require('zlib');
|
||||
/**
|
||||
* Buffers response data events and re-emits when they're unzipped.
|
||||
*
|
||||
* @param {Request} req
|
||||
* @param {Response} res
|
||||
* @api private
|
||||
*/
|
||||
|
||||
|
||||
exports.unzip = function (req, res) {
|
||||
var unzip = zlib.createUnzip();
|
||||
var stream = new Stream();
|
||||
var decoder; // make node responseOnEnd() happy
|
||||
|
||||
stream.req = req;
|
||||
unzip.on('error', function (err) {
|
||||
if (err && err.code === 'Z_BUF_ERROR') {
|
||||
// unexpected end of file is ignored by browsers and curl
|
||||
stream.emit('end');
|
||||
return;
|
||||
}
|
||||
|
||||
stream.emit('error', err);
|
||||
}); // pipe to unzip
|
||||
|
||||
res.pipe(unzip); // override `setEncoding` to capture encoding
|
||||
|
||||
res.setEncoding = function (type) {
|
||||
decoder = new StringDecoder(type);
|
||||
}; // decode upon decompressing with captured encoding
|
||||
|
||||
|
||||
unzip.on('data', function (buf) {
|
||||
if (decoder) {
|
||||
var str = decoder.write(buf);
|
||||
if (str.length > 0) stream.emit('data', str);
|
||||
} else {
|
||||
stream.emit('data', buf);
|
||||
}
|
||||
});
|
||||
unzip.on('end', function () {
|
||||
stream.emit('end');
|
||||
}); // override `on` to capture data listeners
|
||||
|
||||
var _on = res.on;
|
||||
|
||||
res.on = function (type, fn) {
|
||||
if (type === 'data' || type === 'end') {
|
||||
stream.on(type, fn.bind(res));
|
||||
} else if (type === 'error') {
|
||||
stream.on(type, fn.bind(res));
|
||||
|
||||
_on.call(res, type, fn);
|
||||
} else {
|
||||
_on.call(res, type, fn);
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user