mirror of
https://github.com/elastic/kibana.git
synced 2025-04-25 18:27:59 -04:00
This creates a response_stream plugin in the Kibana /examples section. The plugin demonstrates API endpoints that can stream data chunks with a single request with gzip/compression support. gzip-streams get decompressed natively by browsers. The plugin demonstrates two use cases to get started: Streaming a raw string as well as a more complex example that streams Redux-like actions to the client which update React state via useReducer().
29 lines
845 B
TypeScript
29 lines
845 B
TypeScript
/*
|
|
* 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.
|
|
*/
|
|
|
|
export async function* parseStream(stream: NodeJS.ReadableStream) {
|
|
let partial = '';
|
|
|
|
try {
|
|
for await (const value of stream) {
|
|
const full = `${partial}${value}`;
|
|
const parts = full.split('\n');
|
|
const last = parts.pop();
|
|
|
|
partial = last ?? '';
|
|
|
|
const actions = parts.map((p) => JSON.parse(p));
|
|
|
|
for (const action of actions) {
|
|
yield action;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
yield { type: 'error', payload: error.toString() };
|
|
}
|
|
}
|