feat(es): replace @elastic/transport with native fetch EsClient#281
Open
MattDevy wants to merge 7 commits into
Open
feat(es): replace @elastic/transport with native fetch EsClient#281MattDevy wants to merge 7 commits into
MattDevy wants to merge 7 commits into
Conversation
fbff93c to
2e749dc
Compare
✅MegaLinter analysis: Success
See detailed reports in MegaLinter artifacts MegaLinter is graciously provided by OX Security |
Remove @elastic/transport and replace ES HTTP calls with a lightweight EsClient backed by globalThis.fetch. The CLI always exits after one request, so connection pooling, node sniffing, and dead-node resurrection provide no value. Extend x-elastic-client-meta coverage to all outgoing HTTP requests (Elasticsearch, Kibana, Cloud). Previously only ES requests carried the header via @elastic/transport's constructor-time injection. - src/lib/es-client.ts: new EsClient class with ApiKey/Basic auth, NDJSON bulkBody support, EsResponseError, EsConnectionError, and singleton getEsClient() - src/lib/meta.ts: update clientHeaders() -- drop t=<transportVersion> and un=<undiciVersion>; per spec, t= equals the CLI version when there is no separate transport library; memoize at module load - src/lib/kibana-client.ts, cloud-client.ts: spread clientHeaders() into every request - src/es/errors.ts, handler.ts, request-builder.ts: swap Transport types/imports for EsClient equivalents - src/es/helpers/*: same swap; rename getTransport dep to getEsClient - package.json: remove @elastic/transport from dependencies - NOTICE.txt: regenerated
getEsClient() and EsClient now handle auth being absent from the config (ES clusters with security disabled). The old @elastic/transport code guarded auth != null before accessing it; the replacement code did not, causing a TypeError that surfaced as a misleading missing_config error in all functional tests.
… POST Two bugs caused functional test failures: 1. handler.ts was injecting `content-type: application/json` via reqOpts for string bodies, which EsClient also sets as `Content-Type`. Object.assign kept both (different case), and fetch joined them as a comma-separated duplicate value — rejected by ES with 406 Not Acceptable. 2. Native fetch() enforces the Fetch spec: GET/HEAD requests cannot have a body. @elastic/transport bypassed this via undici directly. Several ES APIs (mget, msearch, scroll, analyze, etc.) send a body on GET requests. EsClient now auto-upgrades GET→POST when a body is present; ES REST APIs accept POST for all such endpoints.
The native fetch EsClient threw EsResponseError on 404 for HEAD endpoints, breaking indices.exists and similar APIs that rely on @elastic/transport's convention of returning true for 2xx and false for 404. Non-404 error statuses still throw.
The watch helper merged in from main still imported Transport from @elastic/transport and getTransport from lib/transport.ts, both of which were removed in this branch. Swap to EsClient and getEsClient to match the rest of the codebase, and update the test mocks. Also regen NOTICE.txt and reconcile package-lock.
46e2bda to
f0f89b5
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #273.
@elastic/transportentirely. The CLI always exits after one HTTP request, so connection pooling, node sniffing, and dead-node resurrection are wasted overhead. NativeglobalThis.fetch(Node 18+, backed by undici) is sufficient.x-elastic-client-metato all outgoing HTTP requests. Previously only Elasticsearch requests carried the header (via@elastic/transport's constructor injection). Kibana and Cloud requests now include it as well.t=equals the CLI version. Drops thet=<transportVersion>andun=<undiciVersion>keys.Changes
New
src/lib/es-client.ts—EsClientclass: thinfetchwrapper with ApiKey/Basic auth, JSON/NDJSON body handling,EsResponseError,EsConnectionError, and agetEsClient()singletontest/lib/es-client.test.ts— full coverage ofEsClient(auth, URL composition, body types, error mapping, meta headers)test/lib/kibana-client.test.ts— new tests verifyingx-elastic-client-metaanduser-agenton every Kibana requestModified
src/lib/meta.ts— updated header format; result memoized at module load (was recomputed on every request)src/lib/kibana-client.ts,src/lib/cloud-client.ts— spreadclientHeaders()into every requestsrc/es/errors.ts—transportError()now matchesEsResponseError/EsConnectionErrorinstead of@elastic/transporterror classessrc/es/handler.ts,src/es/helpers/*— swapTransport→EsClient; renamegetTransportdep togetEsClientsrc/es/request-builder.ts— replaceTransportRequestParamswith localEsRequestParamstest/lib/cloud-client.test.ts,test/lib/meta.test.ts— updated expectationsNOTICE.txt— regenerated (removes@elastic/transportandundicientries)Deleted
src/lib/transport.ts,test/lib/transport.test.tsTest plan
npm run build— no TypeScript errorsnpm test— 1046/1046 passelastic es cat healthsendsx-elastic-client-metawith correctet=,js=,t=keysgrep -r '@elastic/transport' src/returns no matches