From c8551b98f044c7315a67f579cc2c1b4f5dde73c4 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Thu, 12 Dec 2024 14:40:10 +0800 Subject: [PATCH] fix: try to use urllib4 (#177) ## Summary by CodeRabbit - **New Features** - Introduced a conditional loading mechanism for the MockAgent and related functions, enhancing flexibility with different urllib versions. - **Style** - Reformatted the dependencies array in the package.json for improved readability. - Added a newline at the end of the package.json for the server project. --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- lib/mock_agent.js | 20 ++++++++++++++++++- .../apps/no-framework/plugin/a/package.json | 4 +++- test/fixtures/server/package.json | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/mock_agent.js b/lib/mock_agent.js index 304f849..8e61655 100644 --- a/lib/mock_agent.js +++ b/lib/mock_agent.js @@ -1,5 +1,23 @@ const { debuglog } = require('util'); -const { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('urllib'); +let { MockAgent, setGlobalDispatcher, getGlobalDispatcher } = require('urllib'); +if (typeof getGlobalDispatcher === 'undefined') { + let urllibNext; + // https://github.com/eggjs/egg/blob/3.x/package.json#L59 + try { + // try to use urllib4 + urllibNext = require('urllib4'); + } catch { + // try to use urllib-next + try { + urllibNext = require('urllib-next'); + } catch { + throw new Error('Please install urllib@4'); + } + } + MockAgent = urllibNext.MockAgent; + setGlobalDispatcher = urllibNext.setGlobalDispatcher; + getGlobalDispatcher = urllibNext.getGlobalDispatcher; +} const debug = debuglog('egg-mock:lib:mock_agent'); diff --git a/test/fixtures/apps/no-framework/plugin/a/package.json b/test/fixtures/apps/no-framework/plugin/a/package.json index 7e3daa8..565d744 100644 --- a/test/fixtures/apps/no-framework/plugin/a/package.json +++ b/test/fixtures/apps/no-framework/plugin/a/package.json @@ -1,6 +1,8 @@ { "eggPlugin": { "name": "a", - "dependencies": [ "egg-mock" ] + "dependencies": [ + "egg-mock" + ] } } diff --git a/test/fixtures/server/package.json b/test/fixtures/server/package.json index 69a979e..f9096fc 100644 --- a/test/fixtures/server/package.json +++ b/test/fixtures/server/package.json @@ -1,4 +1,4 @@ { "name": "server", "version": "1.0.0" -} \ No newline at end of file +}