This is an automated email from the ASF dual-hosted git repository. glynnbird pushed a commit to branch fetch in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git
commit 6d800c350fbc1ec3669b26a7faa40b103f58d0cf Author: Glynn Bird <[email protected]> AuthorDate: Wed Dec 14 16:00:27 2022 +0000 custom agent and agentOptions with tests --- README.md | 7 +++++++ lib/nano.js | 20 ++++++++++++++------ test/nano.agent.test.js | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 1c29471..8a170d9 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,13 @@ const nano = Nano({ url: 'http://127.0.0.1:5984', agentOptions }) The meanings of the agentOptions attributes is described [here](https://undici.nodejs.org/#/docs/api/Agent?id=new-undiciagentoptions), [here](https://undici.nodejs.org/#/docs/api/Pool?id=parameter-pooloptions) and [here](https://undici.nodejs.org/#/docs/api/Client?id=parameter-clientoptions) +You may also supply a pre-existing `undici.Agent` e.g. + +```js +const agent = new undici.Agent({bodyTimeout: 30000 }) +const nano = Nano({ url: 'http://127.0.0.1:5984', agentOptions: agent }) +`` + > Note `requestDefaults` is no longer supported. ## Custom headers diff --git a/lib/nano.js b/lib/nano.js index 8c2e598..79bce3e 100644 --- a/lib/nano.js +++ b/lib/nano.js @@ -15,6 +15,7 @@ const assert = require('assert') const stream = require('stream') const Readable = stream.Readable const undici = require('undici') +const fetch = global.fetch || undici.fetch const ChangesReader = require('./changesreader.js') const CookieJar = require('./cookie.js') const MultiPartFactory = require('./multipart.js') @@ -79,8 +80,15 @@ module.exports = exports = function dbScope (cfg) { } // look for agentOptions - if (cfg.agentOptions && typeof cfg.agentOptions === 'object') { - cfg.agent = new undici.Agent(cfg.agentOptions) + if (cfg.agentOptions) { + // if we've been passed a undici.Agent or undici.MockAgent, + // basically anything inherited from undici.Dispatcher, we + // can use it + if (cfg.agentOptions instanceof undici.Dispatcher) { + cfg.agent = cfg.agentOptions + } else if (typeof cfg.agentOptions === 'object') { + cfg.agent = new undici.Agent(cfg.agentOptions) + } } // create cookieJar for this Nano @@ -239,7 +247,7 @@ module.exports = exports = function dbScope (cfg) { const responseHeaders = Object.assign({ uri: req.url, statusCode - }, response.headers ? Object.fromEntries(response.headers): {}) + }, response.headers ? Object.fromEntries(response.headers) : {}) const error = new Error(message) error.scope = 'couch' @@ -430,7 +438,7 @@ module.exports = exports = function dbScope (cfg) { if (opts.stream) { // return the Request object for streaming const outStream = new stream.PassThrough() - undici.fetch(fetchOptions.url, fetchOptions).then((response) => { + fetch(fetchOptions.url, fetchOptions).then((response) => { const readableWebStream = response.body const readableNodeStream = Readable.fromWeb ? Readable.fromWeb(readableWebStream) : Readable.from(readableWebStream) if (response.status > 300) { @@ -444,14 +452,14 @@ module.exports = exports = function dbScope (cfg) { return outStream } else { if (typeof callback === 'function') { - undici.fetch(fetchOptions.url, fetchOptions).then((response) => { + fetch(fetchOptions.url, fetchOptions).then((response) => { responseHandler(response, fetchOptions, opts, null, null, callback) }).catch((e) => { responseHandler(e, fetchOptions, opts, null, null, callback) }) } else { return new Promise((resolve, reject) => { - undici.fetch(fetchOptions.url, fetchOptions).then((response) => { + fetch(fetchOptions.url, fetchOptions).then((response) => { responseHandler(response, fetchOptions, opts, resolve, reject) }).catch((e) => { responseHandler(e, fetchOptions, opts, resolve, reject) diff --git a/test/nano.agent.test.js b/test/nano.agent.test.js new file mode 100644 index 0000000..4397af0 --- /dev/null +++ b/test/nano.agent.test.js @@ -0,0 +1,18 @@ +const test = require('node:test') +const assert = require('node:assert/strict') +const { COUCH_URL, mockAgent } = require('./mock.js') +const Nano = require('../lib/nano') +const undici = require('undici') + +test('should be able to supply a custom agent parameters', async () => { + const agentOptions = { + bodyTimeout: 10000 + } + const nano = Nano({ url: COUCH_URL, agentOptions }) + assert(nano.config.agent instanceof undici.Agent) +}) + +test('should be able to supply a custom agent', async () => { + const nano = Nano({ url: COUCH_URL, agentOptions: mockAgent }) + assert.equal(nano.config.agent, mockAgent) +})
