-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Dev tools] Fix performance issue with autocomplete suggestions (#143428
) (#143813) * Fix performance issue with autocomplete suggestions * Add unit tests for streamToString function * Address CR change Co-authored-by: Muhammad Ibragimov <[email protected]> Co-authored-by: Kibana Machine <[email protected]> (cherry picked from commit 220f867) Co-authored-by: Muhammad Ibragimov <[email protected]>
- Loading branch information
1 parent
af28812
commit 3db77af
Showing
5 changed files
with
139 additions
and
33 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
35 changes: 35 additions & 0 deletions
35
src/plugins/console/server/lib/utils/stream_to_json.test.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { Readable } from 'stream'; | ||
import { streamToJSON } from './stream_to_json'; | ||
import type { IncomingMessage } from 'http'; | ||
|
||
describe('streamToString', () => { | ||
it('should limit the response size', async () => { | ||
const stream = new Readable({ | ||
read() { | ||
this.push('a'.repeat(1000)); | ||
}, | ||
}); | ||
await expect( | ||
streamToJSON(stream as IncomingMessage, 500) | ||
).rejects.toThrowErrorMatchingInlineSnapshot(`"Response size limit exceeded"`); | ||
}); | ||
|
||
it('should parse the response', async () => { | ||
const stream = new Readable({ | ||
read() { | ||
this.push('{"test": "test"}'); | ||
this.push(null); | ||
}, | ||
}); | ||
const result = await streamToJSON(stream as IncomingMessage, 5000); | ||
expect(result).toEqual({ test: 'test' }); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { IncomingMessage } from 'http'; | ||
|
||
export function streamToJSON(stream: IncomingMessage, limit: number) { | ||
return new Promise<string>((resolve, reject) => { | ||
const chunks: Buffer[] = []; | ||
stream.on('data', (chunk) => { | ||
chunks.push(chunk); | ||
if (Buffer.byteLength(Buffer.concat(chunks)) > limit) { | ||
stream.destroy(); | ||
reject(new Error('Response size limit exceeded')); | ||
} | ||
}); | ||
stream.on('end', () => { | ||
const response = Buffer.concat(chunks).toString('utf8'); | ||
resolve(JSON.parse(response)); | ||
}); | ||
stream.on('error', reject); | ||
}); | ||
} |
This file contains 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