Skip to content

Commit

Permalink
Support node 10 (#42)
Browse files Browse the repository at this point in the history
* Support node 10 + swap request with needle

* Remove reqheaders from one test causing it to fail

* Update docs

* Support https for agent

* Buffer -> Buffer.from

* fix custom client settings in one of the tests to use response_timeout

* bump

* cleanup // remove deprecated agent option

* Update changelog
  • Loading branch information
Rebecca Rice authored Dec 19, 2019
1 parent 908c20b commit 4988209
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 103 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- 6
- 8
- 10

sudo: false

Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
## changelog

## 3.0.0

- Node 10 support
- Swaps request with needle (may cause breaking changes if you use a custom client to customize request options)
- Uses agentkeepalive for all requests

## 2.1.1

- Update agentkeepalive dependency to v4.0.2. This will prevent this._evictSession error
- Update agentkeepalive dependency to v4.0.2. This will prevent this. _evictSession error_

## 2.1.0

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ Transform GeoJSON into Mapnik XML.

### url markers

If your GeoJSON object has one or more features with a `marker-url` property, `mapnikify()` will write the images found at the url into a file in a temporary directory and use that path in the Mapnik XML. This uses the [request library](https://www.npmjs.com/package/request) to handle the http file fetching.
If your GeoJSON object has one or more features with a `marker-url` property, `mapnikify()` will write the images found at the url into a file in a temporary directory and use that path in the Mapnik XML. This uses the [needle library](https://www.npmjs.com/package/needle) to handle the http file fetching.

By default the request will attempt to fetch binary data from the specified url. If the url is `http` and not `https` , Mapnikify will use [agentkeepalive](https://www.npmjs.com/package/agentkeepalive) to speed up requesting multiple images. There is also a default timeout of 5 seconds.
By default the request will attempt to fetch binary data from the specified url. Mapnikify will use [agentkeepalive](https://www.npmjs.com/package/agentkeepalive) to speed up requesting multiple images. There is also a default timeout of 5 seconds.

You can customize the defaults passed to `request()` . Simply set a custom wrapper defined with `request.defaults` . See [request's documentation on defaults](https://www.npmjs.com/package/request#requestdefaultsoptions) for more information. For a quick example, this will set a longer timeout:
You can customize the defaults passed to `needle()` . Simply set a custom wrapper defined with `needle.defaults` . See [needle's documentation on defaults](https://www.npmjs.com/package/needle#overriding-defaults) for more information. For a quick example, this will set a longer timeout:

```javascript
var mapnikify = require('mapnikify');
var myRequest = require('request').defaults({
var myRequest = require('needle').defaults({
timeout: 10000,
followRedirect: false
});
Expand Down
29 changes: 12 additions & 17 deletions lib/get.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var AgentKeepAlive = require('agentkeepalive'),
var AgentKeepAlive = require('agentkeepalive').HttpsAgent,
xtend = require('xtend'),
requestLib = require('request');
needle = require('needle');

// Use a single agent for all requests so that requests to the same
// host can use keep-alive for performance.
var agent = new AgentKeepAlive({
maxSockets: 128,
maxKeepAliveRequests: 0,
freeSocketTimeout: 30000
});

Expand All @@ -18,13 +17,13 @@ var agent = new AgentKeepAlive({
* @param {function} callback
*/
module.exports = function get(options, callback) {
var request = getClient();
var request = getClientSettings();
var params = normalizeParams(options);
if(!params) { callback(new Error('Invalid parameters: ' + JSON.stringify(options))); }

// This might load the same file multiple times, but the overhead should
// be very little.
request(params, function(err, resp, data) {
request.get(options, { agent: agent }, function(err, resp, data) {
// Use err.status of 400 as it's not an unexpected application error,
// but likely due to a bad request. Catches ECONNREFUSED,
// getaddrinfo ENOENT, etc.
Expand All @@ -33,9 +32,7 @@ module.exports = function get(options, callback) {
reqErr.originalError = err;
return callback(reqErr);
}
// request 2.2.x *always* returns the response body as a string.
// @TODO remove this once request is upgraded.
if (!(data instanceof Buffer)) data = new Buffer(data, 'binary');
if (!(data instanceof Buffer)) data = new Buffer.from(data, 'binary');
// Restrict data length.
if (data.length > 32768) return callback(new Error('Marker loaded from URL is too large.'));
callback(null, data);
Expand All @@ -62,14 +59,12 @@ function normalizeParams(options) {
return params;
}

// allows passing in a custom request handler
function getClient() {
if (module.exports.requestClient) { return module.exports.requestClient; }
return requestLib.defaults({
timeout: 5000,
encoding: 'binary',
headers: {
'accept-encoding': 'binary'
}
// allows passing in a custom request handler for needle
function getClientSettings() {
module.exports.requestClient ?
module.exports.requestClient :
needle.defaults({
response_timeout: 4000
});
return needle;
}
Loading

0 comments on commit 4988209

Please sign in to comment.