Skip to content

Add asStream support#34

Merged
delvedor merged 3 commits intomainfrom
as-stream
Dec 16, 2021
Merged

Add asStream support#34
delvedor merged 3 commits intomainfrom
as-stream

Conversation

@delvedor
Copy link
Copy Markdown
Member

@delvedor delvedor commented Dec 16, 2021

With this option, the transport will return a readable stream directly from the HTTP layer, it won't try to parse it or decompress it.
The response size check won't be executed.

import { Readable } from 'stream'
import {
  Transport,
  WeightedConnectionPool,
  UndiciConnection
} from '@elastic/transport'

const pool = new WeightedConnectionPool({ Connection: UndiciConnection })
pool.addConnection('http://localhost:9200')
const transport = new Transport({ connectionPool: pool })

const body = await transport.request<Readable>({
  method: 'GET',
  path: '/'
}, {
  asStream: true
})

body.setEncoding('utf8')
let payload = ''
for await (const chunk of body) {
  payload += chunk
}
console.log(payload)

Closes: elastic/elasticsearch-js#1597

@delvedor delvedor added the enhancement New feature or request label Dec 16, 2021
Comment thread index.d.ts
} from './lib/pool'

export type {
TransportOptions,
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @mshustov @pgayvallet now we are exporting the TransportOptions as well! 🎉


async request (params: ConnectionRequestParams, options: ConnectionRequestOptions): Promise<ConnectionRequestResponse> {
async request (params: ConnectionRequestParams, options: ConnectionRequestOptions): Promise<ConnectionRequestResponse>
async request (params: ConnectionRequestParams, options: ConnectionRequestOptionsAsStream): Promise<ConnectionRequestResponseAsStream>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it mean that we need to re-generate all the API type definitions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, the connection.request is only used by the transport, and its signature hasn't changed.
In the client you might need to use directly client.transport.request<Readable>, as the surface API assumes you are using JSON.

@delvedor delvedor merged commit 694a9c0 into main Dec 16, 2021
@delvedor delvedor deleted the as-stream branch December 16, 2021 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support asStream to bypass decompression of gzipped responses

2 participants