-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
* under the License. | ||
*/ | ||
|
||
/* eslint-disable @typescript-eslint/restrict-template-expressions */ | ||
|
||
import { EventEmitter } from 'events' | ||
import Debug from 'debug' | ||
import buffer from 'buffer' | ||
|
@@ -26,7 +28,9 @@ import BaseConnection, { | |
ConnectionOptions, | ||
ConnectionRequestParams, | ||
ConnectionRequestOptions, | ||
ConnectionRequestOptionsAsStream, | ||
ConnectionRequestResponse, | ||
ConnectionRequestResponseAsStream, | ||
getIssuerCertificate | ||
} from './BaseConnection' | ||
import { Pool, buildConnector, Dispatcher } from 'undici' | ||
|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, the |
||
async request (params: ConnectionRequestParams, options: any): Promise<any> { | ||
const maxResponseSize = options.maxResponseSize ?? MAX_STRING_LENGTH | ||
const maxCompressedResponseSize = options.maxCompressedResponseSize ?? MAX_BUFFER_LENGTH | ||
const requestParams = { | ||
|
@@ -135,7 +141,6 @@ export default class Connection extends BaseConnection { | |
timeoutId = setTimeout(() => { | ||
timedout = true | ||
if (options.signal != null) { | ||
// @ts-expect-error | ||
options.signal.dispatchEvent('abort') | ||
} else { | ||
this[kEmitter].emit('abort') | ||
|
@@ -168,6 +173,14 @@ export default class Connection extends BaseConnection { | |
} | ||
} | ||
|
||
if (options.asStream === true) { | ||
return { | ||
statusCode: response.statusCode, | ||
headers: response.headers, | ||
body: response.body | ||
} | ||
} | ||
|
||
const contentEncoding = (response.headers['content-encoding'] ?? '').toLowerCase() | ||
const isCompressed = contentEncoding.includes('gzip') || contentEncoding.includes('deflate') // eslint-disable-line | ||
const isVectorTile = (response.headers['content-type'] ?? '').includes('application/vnd.mapbox-vector-tile') | ||
|
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.
cc @mshustov @pgayvallet now we are exporting the
TransportOptions
as well! 🎉