Skip to content
This repository was archived by the owner on Aug 10, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
reporter: spec
recursive: true
5 changes: 3 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* Modules from the community: package.json
*/
var defaultRequest = require('request');
var waitress = require('waitress');
var check = require('check-types');

Expand All @@ -14,6 +13,8 @@ var _jsonParser = require('./utils/jsonParser');
var _encodePolyline = require('./utils/encodePolylines');
var _decodePolyline = require('./utils/decodePolylines');
var _getDefaultConfig = require('./config/getDefault');
var _httpRequest = require('./utils/httpRequest');

var _constants = require('./config/constants');


Expand Down Expand Up @@ -69,7 +70,7 @@ var GoogleMapsAPI = function(config, request) {
}

if (typeof request !== 'function') {
request = defaultRequest;
request = _httpRequest;
}

this.request = request;
Expand Down
67 changes: 67 additions & 0 deletions lib/utils/httpRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Minimal drop-in replacement for the `request` module using native fetch.
*
* Supports the subset of the request API used by this library:
* request(options, callback)
*
* options:
* - uri: the full URL to request
* - encoding: response encoding (null = Buffer, string = that encoding,
* undefined = utf8 string)
* - proxy: optional proxy URL (HTTP proxy only)
*
* callback(err, res, body) where res has statusCode and headers.
*/
module.exports = function(options, callback) {

var fetchOptions = { method: 'GET' };

if (options.proxy) {
var dispatcher = createProxyDispatcher(options.proxy);
if (dispatcher) fetchOptions.dispatcher = dispatcher;
}

fetch(options.uri, fetchOptions)
.then(function(res) {
var statusCode = res.status;
var headers = {};
res.headers.forEach(function(value, key) {
headers[key] = value;
});

var bodyPromise;
if (options.encoding === null) {
bodyPromise = res.arrayBuffer().then(function(buf) {
return Buffer.from(buf);
});
} else if (options.encoding === 'binary') {
bodyPromise = res.arrayBuffer().then(function(buf) {
return Buffer.from(buf).toString('latin1');
});
} else {
bodyPromise = res.text();
}

return bodyPromise.then(function(body) {
callback(null, { statusCode: statusCode, headers: headers }, body);
});
})
.catch(function(err) {
// fetch wraps connection errors: the real error (with code) is in
// err.cause. Surface it so callers see ECONNREFUSED etc.
if (err && err.cause && err.cause.code && !err.code) {
err.code = err.cause.code;
}
callback(err);
});

};

function createProxyDispatcher(proxyUrl) {
try {
var ProxyAgent = require('undici').ProxyAgent;
return new ProxyAgent(proxyUrl);
} catch (e) {
return null;
}
}
Loading