Repository navigation
fix(proxy): add worker and runtime export conditions #170
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
Open
Fire162
wants to merge
1
commit into
unjs:main
Choose a base branch
from
Fire162:fix/proxy-export-conditions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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,82 @@ | ||
| import { createRequire } from "node:module"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import pkg from "../package.json"; | ||
| import * as proxyStub from "../src/proxy-stub"; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
| const proxyNode = require("../dist/proxy.cjs"); | ||
|
|
||
| describe("node-fetch-native/proxy:exports", () => { | ||
| const runtimeConditions = [ | ||
| "browser", | ||
| "bun", | ||
| "deno", | ||
| "edge-light", | ||
| "edge-routine", | ||
| "lagon", | ||
| "netlify", | ||
| "react-native", | ||
| "wintercg", | ||
| "worker", | ||
| "workerd", | ||
| ]; | ||
|
|
||
| it("exports map defines non-node runtime conditions pointing to proxy-stub", () => { | ||
| const proxyExport = pkg.exports["./proxy"]; | ||
| expect(proxyExport).toBeDefined(); | ||
|
|
||
| for (const condition of runtimeConditions) { | ||
| expect( | ||
| proxyExport[condition as keyof typeof proxyExport], | ||
| `condition ${condition} should point to proxy stub`, | ||
| ).toBe("./dist/proxy-stub.mjs"); | ||
| } | ||
| }); | ||
|
|
||
| it("exports map defines node condition pointing to proxy.cjs", () => { | ||
| const proxyExport = pkg.exports["./proxy"]; | ||
| expect(proxyExport.node).toEqual({ | ||
| types: "./lib/proxy.d.ts", | ||
| default: "./dist/proxy.cjs", | ||
| }); | ||
| }); | ||
|
|
||
| it("exports map defines fallback conditions", () => { | ||
| const proxyExport = pkg.exports["./proxy"]; | ||
| expect(proxyExport.import).toBe("./dist/proxy-stub.mjs"); | ||
| expect(proxyExport.require).toBe("./dist/proxy-stub.cjs"); | ||
| expect(proxyExport.default).toBe("./dist/proxy-stub.mjs"); | ||
| expect(proxyExport.types).toBe("./lib/proxy.d.ts"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("node-fetch-native/proxy:stub", () => { | ||
| it("exports createProxy returning undefined agent and dispatcher", () => { | ||
| const proxy = proxyStub.createProxy(); | ||
| expect(proxy).toEqual({ | ||
| agent: undefined, | ||
| dispatcher: undefined, | ||
| }); | ||
| }); | ||
|
|
||
| it("exports createFetch returning global fetch", () => { | ||
| expect(proxyStub.createFetch()).toBe(globalThis.fetch); | ||
| }); | ||
|
|
||
| it("exports fetch matching global fetch", () => { | ||
| expect(proxyStub.fetch).toBe(globalThis.fetch); | ||
| }); | ||
| }); | ||
|
|
||
| describe("node-fetch-native/proxy:node", () => { | ||
| it("exports expected symbols in node proxy cjs", () => { | ||
| expect(typeof proxyNode.createProxy).toBe("function"); | ||
| expect(typeof proxyNode.createFetch).toBe("function"); | ||
| expect(typeof proxyNode.fetch).toBe("function"); | ||
| }); | ||
|
|
||
| it("creates agent when no options provided", () => { | ||
| const proxy = proxyNode.createProxy(); | ||
| expect(proxy).toBeDefined(); | ||
| }); | ||
| }); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Put the
typescondition before runtime conditions.If a TypeScript consumer enables
customConditions: ["workerd"], TypeScript matchesworkerdbefore it reachestypes. It resolves./dist/proxy-stub.mjs, which has no generated adjacent declaration, instead of./lib/proxy.d.ts. The import then loses its published types and can fail a strict type-check. Move the top-leveltypescondition ahead of the runtime conditions; Node will ignore that condition. TypeScript and Node both use export-key order when selecting a matching condition. (typescriptlang.org)As per retrieved learnings, keep the package-level and export-map type declarations for consumer compatibility.
🤖 Prompt for AI Agents
Source: Learnings