Skip to content

Commit

Permalink
Bye bye superagent
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed May 4, 2024
1 parent 4177d2d commit 84024f9
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 442 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 20.0.0
- Replaced superagent with native node http client as per https://github.com/onebeyond/rascal/issues/234

## 19.0.0
- I am not aware of any breaking changes in this release, but emitting error events asynchronously could have subtle side effects, hence the major release
- Deprecate session 'cancelled' event in favour of 'cancel' (both will work)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ The AMQP protocol doesn't support assertion or checking of vhosts, so Rascal use
}
```

Rascal uses [superagent](https://github.com/visionmedia/superagent) under the hood. URL configuration is also supported.
Rascal uses [http.request](https://nodejs.org/api/http.html#httprequesturl-options-callback) under the hood. URL configuration is also supported.

```json
{
Expand All @@ -489,11 +489,11 @@ Rascal uses [superagent](https://github.com/visionmedia/superagent) under the ho
}
```

You can also supply your own agent via the broker components. Use this when you need to set [TLS options](https://visionmedia.github.io/superagent/#tls-options).
You can also supply your own agent via the broker components. Use this when you need to set TLS options](https://nodejs.org/api/https.html#httpsrequesturl-options-callback).

```js
const superagent = require('superagent-defaults');
const agent = superagent().on('request', (req) => console.log(req.url));
const https = require('https');
const agent = new https.Agent(options);
const components = { agent };
const broker = await Broker.create(config, components);
```
Expand Down
41 changes: 21 additions & 20 deletions lib/management/Client.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
const http = require('http');
const debug = require('debug')('rascal:management:client');
const format = require('util').format;
const _ = require('lodash');
const defaultAgent = require('superagent');

function Client(suppliedAgent) {
const agent = suppliedAgent || defaultAgent;
function Client(agent) {
const self = this;

this.assertVhost = function (name, config, next) {
debug('Asserting vhost: %s', name);
const options = getVhostOptions(name, config);
self._request('put', options.url, options.timeout, (err) => {
const url = getUrl(name, config);
self._request('PUT', url, config.options, (err) => {
if (!err) return next();
const _err = err.status ? new Error(format('Failed to assert vhost: %s. %s returned status %d', name, config.loggableUrl, err.status)) : err;
return next(_err);
Expand All @@ -19,8 +17,8 @@ function Client(suppliedAgent) {

this.checkVhost = function (name, config, next) {
debug('Checking vhost: %s', name);
const options = getVhostOptions(name, config);
self._request('get', options.url, options.timeout, (err) => {
const url = getUrl(name, config);
self._request('GET', url, config.options, (err) => {
if (!err) return next();
const _err = err.status ? new Error(format('Failed to check vhost: %s. %s returned status %d', name, config.loggableUrl, err.status)) : err;
return next(_err);
Expand All @@ -29,26 +27,29 @@ function Client(suppliedAgent) {

this.deleteVhost = function (name, config, next) {
debug('Deleting vhost: %s', name);
const options = getVhostOptions(name, config);
self._request('delete', options.url, options.timeout, (err) => {
const url = getUrl(name, config);
self._request('DELETE', url, config.options, (err) => {
if (!err) return next();
const _err = err.status ? new Error(format('Failed to delete vhost: %s. %s returned status %d', name, config.loggableUrl, err.status)) : err;
return next(_err);
});
};

this._request = function (method, url, timeout, next) {
agent[method](url)
.timeout({ deadline: timeout })
.then(() => {
next();
})
.catch(next);
this._request = function (method, url, options, next) {
const req = http.request(url, { ...options, method, agent }, (res) => {
if (res.statusCode >= 300) {
const err = Object.assign(new Error('HTTP Error'), { status: res.statusCode });
return next(err);
}
res.on('data', () => {});
res.on('end', () => next());
});
req.on('error', next);
req.end();
};

function getVhostOptions(name, config) {
const url = format('%s/%s/%s', config.url, 'api/vhosts', name);
return _.defaultsDeep({ url }, config.options);
function getUrl(name, config) {
return format('%s/%s/%s', config.url, 'api/vhosts', name);
}
}

Expand Down
Loading

0 comments on commit 84024f9

Please sign in to comment.