-
Notifications
You must be signed in to change notification settings - Fork 5
🔧 fix undici erroneously grabing previous request Agents by default #480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8d940fa
:wrench: fix undici erroneously grabing previous request Agents by de…
sebastianMindee 2aa8541
fix blank_pdf_url check
sebastianMindee e92409b
add dispatcher for url dl + tests
sebastianMindee 92c2725
make test non-mandatory for non-compatible version
sebastianMindee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { Agent, Dispatcher, getGlobalDispatcher } from "undici"; | ||
| import { logger } from "@/logger.js"; | ||
|
|
||
| /** Library-owned fallback dispatcher, shared across all components. */ | ||
| let fallbackDispatcher: Dispatcher | undefined; | ||
|
|
||
| /** | ||
| * Returns the global undici dispatcher, provided it was created by the same | ||
| * undici copy this library imports. | ||
| */ | ||
| export function resolveDefaultDispatcher(): Dispatcher { | ||
| const globalDispatcher = getGlobalDispatcher(); | ||
| if (globalDispatcher instanceof Dispatcher) { | ||
| return globalDispatcher; | ||
| } | ||
| logger.debug( | ||
| "Global dispatcher belongs to a different undici instance, using a library-owned Agent instead." | ||
| ); | ||
| fallbackDispatcher ??= new Agent(); | ||
| return fallbackDispatcher; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { afterEach, describe, it } from "node:test"; | ||
| import { Agent, Dispatcher, getGlobalDispatcher, setGlobalDispatcher } from "undici"; | ||
| import { BaseSettings } from "@/http/baseSettings.js"; | ||
| import { UrlInput } from "@/input/index.js"; | ||
|
|
||
| class TestSettings extends BaseSettings { | ||
| constructor(dispatcher?: Dispatcher) { | ||
| super("dummy-key", dispatcher); | ||
| } | ||
| protected apiKeyFromEnv(): string { | ||
| return "dummy-key"; | ||
| } | ||
| protected hostnameFromEnv(): string { | ||
| return "test-host"; | ||
| } | ||
| } | ||
|
|
||
| describe("BaseSettings – dispatcher resolution", () => { | ||
| const originalDispatcher = getGlobalDispatcher(); | ||
|
|
||
| afterEach(() => { | ||
| setGlobalDispatcher(originalDispatcher); | ||
| }); | ||
|
|
||
| it("uses an explicitly provided dispatcher as-is", () => { | ||
| const agent = new Agent(); | ||
| const settings = new TestSettings(agent); | ||
| assert.strictEqual(settings.dispatcher, agent); | ||
| }); | ||
|
|
||
| it("uses the global dispatcher when it comes from the same undici copy", () => { | ||
| const agent = new Agent(); | ||
| setGlobalDispatcher(agent); | ||
| const settings = new TestSettings(); | ||
| assert.strictEqual(settings.dispatcher, agent); | ||
| }); | ||
|
|
||
| it("ignores a global dispatcher from a foreign undici instance", () => { | ||
| // Simulates Node's built-in fetch (or another undici copy) having | ||
| // registered its dispatcher on the shared global symbol: such objects are | ||
| // not instances of this library's undici Dispatcher class. | ||
| const foreignDispatcher = { dispatch: () => true } as unknown as Agent; | ||
| setGlobalDispatcher(foreignDispatcher); | ||
| const settings = new TestSettings(); | ||
| assert.notStrictEqual(settings.dispatcher, foreignDispatcher); | ||
| assert.ok(settings.dispatcher instanceof Dispatcher); | ||
| }); | ||
|
|
||
| it("reuses the same fallback dispatcher across instances", () => { | ||
| const foreignDispatcher = { dispatch: () => true } as unknown as Agent; | ||
| setGlobalDispatcher(foreignDispatcher); | ||
| const first = new TestSettings(); | ||
| const second = new TestSettings(); | ||
| assert.strictEqual(first.dispatcher, second.dispatcher); | ||
| }); | ||
|
|
||
| it("UrlInput ignores a foreign global dispatcher as well", () => { | ||
| const foreignDispatcher = { dispatch: () => true } as unknown as Agent; | ||
| setGlobalDispatcher(foreignDispatcher); | ||
| const input = new UrlInput({ url: "https://example.com/file.pdf" }); | ||
| assert.notStrictEqual(input.dispatcher, foreignDispatcher); | ||
| assert.ok(input.dispatcher instanceof Dispatcher); | ||
| }); | ||
|
|
||
| it("UrlInput uses an explicitly provided dispatcher as-is", () => { | ||
| const agent = new Agent(); | ||
| const input = new UrlInput({ url: "https://example.com/file.pdf", dispatcher: agent }); | ||
| assert.strictEqual(input.dispatcher, agent); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { before, describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
|
|
||
| // IMPORTANT: this file must NOT statically import "undici" or the SDK. | ||
| // This test showcases how the library previously behaved and broke when reusing a foreign global dispatcher. | ||
|
|
||
| describe( | ||
| "MindeeV2 – Integration – foreign global dispatcher", | ||
| { timeout: 120000 }, | ||
| () => { | ||
| let apiKey: string; | ||
| let modelId: string; | ||
| let pdfBuffer: Buffer; | ||
| let undici: typeof import("undici"); | ||
| let mindee: typeof import("@/index.js"); | ||
| let mindeeHttp: typeof import("@/v2/http/index.js"); | ||
| let foreignDispatcher: import("undici").Dispatcher; | ||
|
|
||
| before(async () => { | ||
| apiKey = process.env["MINDEE_V2_API_KEY"] ?? ""; | ||
| modelId = process.env["MINDEE_V2_SE_TESTS_FINDOC_MODEL_ID"] ?? ""; | ||
| const pdfUrl = process.env["MINDEE_V2_SE_TESTS_BLANK_PDF_URL"]; | ||
| assert.ok( | ||
| pdfUrl, | ||
| "MINDEE_V2_SE_TESTS_BLANK_PDF_URL must be set to run this integration test", | ||
| ); | ||
|
|
||
| const response = await fetch(pdfUrl); | ||
| assert.ok(response.ok, `Failed to download the test PDF from: ${pdfUrl}`); | ||
| pdfBuffer = Buffer.from(await response.arrayBuffer()); | ||
|
|
||
| undici = await import("undici"); | ||
| mindee = await import("@/index.js"); | ||
| mindeeHttp = await import("@/v2/http/index.js"); | ||
|
|
||
| foreignDispatcher = undici.getGlobalDispatcher(); | ||
| assert.ok( | ||
| !(foreignDispatcher instanceof undici.Dispatcher), | ||
| "Expected the global dispatcher to belong to Node's built-in undici." + | ||
| " Check that nothing loads npm undici before the first fetch call." | ||
| ); | ||
| }); | ||
|
|
||
| // Whether cross-instance dispatch actually breaks depends on the exact | ||
| // pairing between Node's built-in undici and our npm undici copy, so this | ||
| // repro is skipped on Node versions where the two happen to be compatible. | ||
| it("repro: enqueueing through the foreign dispatcher fails with a transport error", async (t) => { | ||
| const client = new mindee.Client({ | ||
| apiKey: apiKey, | ||
| dispatcher: foreignDispatcher, | ||
| }); | ||
| const source = new mindee.BufferInput({ | ||
| buffer: pdfBuffer, | ||
| filename: "blank.pdf", | ||
| }); | ||
| try { | ||
| await client.enqueue( | ||
| mindee.product.Extraction, source, { modelId: modelId } | ||
| ); | ||
| } catch (err: unknown) { | ||
| assert.ok( | ||
| !(err instanceof mindeeHttp.MindeeHttpErrorV2), | ||
| "Expected a transport-level error, not an API error." | ||
| ); | ||
| return; | ||
| } | ||
| t.skip( | ||
| "Cross-instance dispatch is compatible on this Node version;" + | ||
| " the foreign-dispatcher breakage cannot be reproduced here." | ||
| ); | ||
| }); | ||
|
|
||
| it("fix: the default client ignores the foreign dispatcher and succeeds", async () => { | ||
| const client = new mindee.Client({ apiKey: apiKey }); | ||
| const source = new mindee.BufferInput({ | ||
| buffer: pdfBuffer, | ||
| filename: "blank.pdf", | ||
| }); | ||
| const response = await client.enqueue( | ||
| mindee.product.Extraction, source, { modelId: modelId } | ||
| ); | ||
| assert.ok(response.job.id); | ||
| }); | ||
|
|
||
| it("fix: UrlInput ignores the foreign dispatcher and downloads succeed", async () => { | ||
| const source = new mindee.UrlInput({ | ||
| url: "https://github.com/mindee/client-lib-test-data/blob/main/" + | ||
| "file_types/pdf/blank_1.pdf?raw=true", | ||
| }); | ||
| assert.notStrictEqual(source.dispatcher, foreignDispatcher); | ||
| assert.ok(source.dispatcher instanceof undici.Dispatcher); | ||
| await source.init(); | ||
| const localized = await source.asLocalInputSource(); | ||
| await localized.init(); | ||
| assert.ok(localized.fileObject.length > 0); | ||
| }); | ||
| } | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.