@aizigao/pi-fetch-pipeline
v1.0.1
Published
Shared fetch middleware pipeline for Pi extensions.
Maintainers
Readme
@aizigao/pi-fetch-pipeline
A tiny shared fetch middleware pipeline for Pi extensions.
Why
Multiple Pi extensions sometimes patch globalThis.fetch independently:
- proxy routing
- header rewriting
- cookie injection
- URL rewriting
- provider compatibility shims
If each extension patches fetch on its own, the last one loaded usually wins, or some requests bypass other extensions entirely.
@aizigao/pi-fetch-pipeline solves this by providing a shared global pipeline keyed by:
Symbol.for("aizigao.fetch.pipeline.v1")Extensions can be installed independently and still compose safely.
Install
npm install @aizigao/pi-fetch-pipelineUsage
import { registerFetchMiddleware } from "@aizigao/pi-fetch-pipeline";
registerFetchMiddleware({
name: "headers-compat",
priority: 10,
middleware: async ({ input, init, next }) => {
const headers = new Headers(init?.headers);
headers.set("x-demo", "1");
return next(input, { ...init, headers });
},
});Ordering
Middlewares are executed by ascending priority.
Suggested convention:
10: request/header compatibility rewrites20: provider-specific host tweaks50: proxy / transport routing
API
registerFetchMiddleware(registration)
Registers or replaces a named middleware and ensures the shared pipeline is installed.
ensureFetchPipeline()
Creates the shared pipeline state if missing and returns it.
installFetchPipeline()
Installs the patched globalThis.fetch if not already installed.
License
MIT
