Skip to content
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

Add asStream support #34

Merged
merged 3 commits into from
Dec 16, 2021
Merged

Add asStream support #34

merged 3 commits into from
Dec 16, 2021

Conversation

delvedor
Copy link
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
@@ -47,6 +49,7 @@ export type {
} from './lib/pool'

export type {
TransportOptions,
Copy link
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! 🎉

@@ -109,7 +113,9 @@ export default class Connection extends BaseConnection {
this.pool = new Pool(this.url.toString(), undiciOptions)
}

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
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
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