Skip to content

Commit

Permalink
[ML] Fix types.
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed May 23, 2022
1 parent d5b8c5c commit eebf7fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 5 additions & 2 deletions packages/kbn-aiops-utils/src/lib/accept_compression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
* Side Public License, v 1.
*/

import { IncomingHttpHeaders } from 'http';
// TODO: Replace these with kbn packaged versions once we have those available to us.
// At the moment imports from runtime plugins into packages are not supported.
// import type { Headers } from '@kbn/core/server';
type Headers = Record<string, string | string[] | undefined>;

/**
* Returns whether request headers accept a response using gzip compression.
*
* @param headers - Request headers.
* @returns boolean
*/
export function acceptCompression(headers: IncomingHttpHeaders) {
export function acceptCompression(headers: Headers) {
let compressed = false;

Object.keys(headers).forEach((key) => {
Expand Down
17 changes: 11 additions & 6 deletions packages/kbn-aiops-utils/src/lib/stream_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@

import { Stream } from 'stream';
import zlib from 'zlib';
import { IncomingHttpHeaders } from 'http';

// TODO: Replace these with kbn packaged versions once we have those available to us.
// At the moment imports from runtime plugins into packages are not supported.
// import type { Headers } from '@kbn/core/server';

import { acceptCompression } from './accept_compression';

type Headers = Record<string, string | string[] | undefined>;

// We need this otherwise Kibana server will crash with a 'ERR_METHOD_NOT_IMPLEMENTED' error.
class ResponseStream extends Stream.PassThrough {
flush() {}
Expand All @@ -29,7 +34,9 @@ interface StreamFactoryReturnType<T = unknown> {
push: (d: T) => void;
responseWithHeaders: {
body: zlib.Gzip | ResponseStream;
headers?: IncomingHttpHeaders;
// TODO: Replace these with kbn packaged versions once we have those available to us.
// At the moment imports from runtime plugins into packages are not supported.
headers?: any;
};
}

Expand All @@ -40,17 +47,15 @@ interface StreamFactoryReturnType<T = unknown> {
* @param headers - Request headers.
* @returns An object with stream attributes and methods.
*/
export function streamFactory<T = string>(headers: IncomingHttpHeaders): StreamFactoryReturnType<T>;
export function streamFactory<T = string>(headers: Headers): StreamFactoryReturnType<T>;
/**
* Sets up a response stream with support for gzip compression depending on provided
* request headers. Any non-string data pushed to the stream will be stream as NDJSON.
*
* @param headers - Request headers.
* @returns An object with stream attributes and methods.
*/
export function streamFactory<T = unknown>(
headers: IncomingHttpHeaders
): StreamFactoryReturnType<T> {
export function streamFactory<T = unknown>(headers: Headers): StreamFactoryReturnType<T> {
let streamType: StreamType;
const isCompressed = acceptCompression(headers);

Expand Down

0 comments on commit eebf7fb

Please sign in to comment.