Skip to content

Commit

Permalink
fix: try to use urllib4 (#177)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## 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.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
fengmk2 and coderabbitai[bot] authored Dec 12, 2024
1 parent 2f008e7 commit c8551b9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
20 changes: 19 additions & 1 deletion lib/mock_agent.js
Original file line number Diff line number Diff line change
@@ -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');

Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/apps/no-framework/plugin/a/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"eggPlugin": {
"name": "a",
"dependencies": [ "egg-mock" ]
"dependencies": [
"egg-mock"
]
}
}
2 changes: 1 addition & 1 deletion test/fixtures/server/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "server",
"version": "1.0.0"
}
}

0 comments on commit c8551b9

Please sign in to comment.