Skip to content

Commit

Permalink
Test all supported undici versions: v4, v5, v6 and v7
Browse files Browse the repository at this point in the history
  • Loading branch information
hansott committed Nov 28, 2024
1 parent d643665 commit a44c859
Show file tree
Hide file tree
Showing 15 changed files with 476 additions and 389 deletions.
6 changes: 5 additions & 1 deletion library/agent/hooks/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type PackageName = string;
export class Package {
private versions: VersionedPackage[] = [];

constructor(private readonly packageName: PackageName) {
constructor(private packageName: PackageName) {
if (!this.packageName) {
throw new Error("Package name is required");
}
Expand All @@ -22,6 +22,10 @@ export class Package {
return this.packageName;
}

setName(name: string) {
this.packageName = name;
}

withVersion(range: string): VersionedPackage {
const pkg = new VersionedPackage(range);
this.versions.push(pkg);
Expand Down
23 changes: 23 additions & 0 deletions library/agent/hooks/wrapRequire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,26 @@ function executeInterceptors(
export function getOriginalRequire() {
return originalRequire;
}

export function __internalRewritePackageName(
packageName: string,
aliasForTesting: string
) {
if (!isRequireWrapped) {
throw new Error(
"Start the agent before calling __internalRewritePackageName(..)"
);
}

Check warning on line 317 in library/agent/hooks/wrapRequire.ts

View check run for this annotation

Codecov / codecov/patch

library/agent/hooks/wrapRequire.ts#L314-L317

Added lines #L314 - L317 were not covered by tests

if (packages.length === 0) {
throw new Error("No packages to patch");
}

Check warning on line 321 in library/agent/hooks/wrapRequire.ts

View check run for this annotation

Codecov / codecov/patch

library/agent/hooks/wrapRequire.ts#L320-L321

Added lines #L320 - L321 were not covered by tests

const pkg = packages.find((pkg) => pkg.getName() === packageName);

if (!pkg) {
throw new Error(`Could not find package ${packageName}`);
}

Check warning on line 327 in library/agent/hooks/wrapRequire.ts

View check run for this annotation

Codecov / codecov/patch

library/agent/hooks/wrapRequire.ts#L326-L327

Added lines #L326 - L327 were not covered by tests

pkg.setName(aliasForTesting);
}
63 changes: 54 additions & 9 deletions library/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@
"tap": "^18.6.1",
"type-fest": "^4.24.0",
"typescript": "^5.3.3",
"undici": "^6.12.0",
"xml-js": "^1.6.11",
"xml2js": "^0.6.2"
"xml2js": "^0.6.2",
"undici-v4": "npm:undici@^4.0.0",
"undici-v5": "npm:undici@^5.0.0",
"undici-v6": "npm:undici@^6.0.0",
"undici-v7": "npm:undici@^7.0.0"
},
"scripts": {
"test": "node ../scripts/run-tap.js",
Expand Down
4 changes: 3 additions & 1 deletion library/sinks/Undici.custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as dns from "dns";
import * as t from "tap";
import { Token } from "../agent/api/Token";
import { Context, runWithContext } from "../agent/Context";
import { __internalRewritePackageName } from "../agent/hooks/wrapRequire";
import { wrap } from "../helpers/wrap";
import { getMajorNodeVersion } from "../helpers/getNodeVersion";
import { Undici } from "./Undici";
Expand Down Expand Up @@ -46,9 +47,10 @@ t.test(
token: new Token("123"),
});
agent.start([new Undici()]);
__internalRewritePackageName("undici", "undici-v6");

const { request, Dispatcher, setGlobalDispatcher, getGlobalDispatcher } =
require("undici") as typeof import("undici");
require("undici-v6") as typeof import("undici-v6");

// See https://www.npmjs.com/package/@n8n_io/license-sdk
// They set a custom dispatcher to proxy certain requests
Expand Down
9 changes: 4 additions & 5 deletions library/sinks/Undici.localhost.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/* eslint-disable prefer-rest-params */
import * as t from "tap";
import { Agent } from "../agent/Agent";
import { createServer, Server } from "http";
import { ReportingAPIForTesting } from "../agent/api/ReportingAPIForTesting";
import { Token } from "../agent/api/Token";
import { Context, runWithContext } from "../agent/Context";
import { LoggerNoop } from "../agent/logger/LoggerNoop";
import { __internalRewritePackageName } from "../agent/hooks/wrapRequire";
import { createTestAgent } from "../helpers/createTestAgent";
import { getMajorNodeVersion } from "../helpers/getNodeVersion";
import { Undici } from "./Undici";
Expand Down Expand Up @@ -58,6 +56,7 @@ const agent = createTestAgent({
});

agent.start([new Undici()]);
__internalRewritePackageName("undici", "undici-v6");

const port = 1346;
const serverUrl = `http://localhost:${port}`;
Expand All @@ -83,7 +82,7 @@ t.test(
getMajorNodeVersion() <= 16 ? "ReadableStream is not available" : false,
},
async (t) => {
const { request } = require("undici");
const { request } = require("undici-v6") as typeof import("undici-v6");

await runWithContext(
createContext({
Expand All @@ -108,7 +107,7 @@ t.test(
getMajorNodeVersion() <= 16 ? "ReadableStream is not available" : false,
},
async (t) => {
const { request } = require("undici");
const { request } = require("undici-v6") as typeof import("undici-v6");

const error = await t.rejects(async () => {
await runWithContext(
Expand Down
Loading

0 comments on commit a44c859

Please sign in to comment.