-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathstreaming.ts
More file actions
102 lines (86 loc) · 3.33 KB
/
streaming.ts
File metadata and controls
102 lines (86 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {parseMultipart} from '@mjackson/multipart-parser';
import qs from 'qs';
import {
isQueryResponseChunk,
isSessionChunk,
isStreamDataChunk,
} from '../../store/reducers/query/utils';
import type {Actions, StreamQueryParams} from '../../types/api/query';
import type {QueryResponseChunk, SessionChunk, StreamDataChunk} from '../../types/store/streaming';
import {
BINARY_DATA_IN_PLAIN_TEXT_DISPLAY,
DEV_ENABLE_TRACING_FOR_ALL_REQUESTS,
} from '../../utils/constants';
import {isRedirectToAuth} from '../../utils/response';
import {settingsManager} from '../settings';
import {BaseYdbAPI} from './base';
const BOUNDARY = 'boundary';
export interface StreamQueryOptions {
signal?: AbortSignal;
onStreamDataChunk: (chunk: StreamDataChunk) => void;
onQueryResponseChunk: (chunk: QueryResponseChunk) => void;
onSessionChunk: (chunk: SessionChunk) => void;
}
export class StreamingAPI extends BaseYdbAPI {
async streamQuery<Action extends Actions>(
params: StreamQueryParams<Action>,
options: StreamQueryOptions,
) {
const base64 = !settingsManager.readUserSettingsValue(
BINARY_DATA_IN_PLAIN_TEXT_DISPLAY,
true,
);
const queryParams = qs.stringify(
{timeout: params.timeout, base64, schema: 'multipart'},
{encoder: encodeURIComponent},
);
const body = {...params, base64, schema: 'multipart'};
const headers = new Headers({
Accept: 'multipart/form-data',
'Content-Type': 'application/json',
});
if (params.tracingLevel) {
headers.set('X-Trace-Verbosity', String(params.tracingLevel));
}
const enableTracing = settingsManager.readUserSettingsValue(
DEV_ENABLE_TRACING_FOR_ALL_REQUESTS,
);
if (enableTracing) {
headers.set('X-Want-Trace', '1');
}
const response = await fetch(`${this.getPath('/viewer/query')}?${queryParams}`, {
method: 'POST',
signal: options.signal,
headers,
body: JSON.stringify(body),
});
if (!response.ok) {
const responseData = await response.json().catch(() => ({}));
if (isRedirectToAuth({status: response.status, data: responseData})) {
window.location.assign(responseData.authUrl);
return;
}
throw new Error(`${response.status}`);
}
if (!response.body) {
throw new Error('Empty response body');
}
const traceId = response.headers.get('traceresponse')?.split('-')[1];
await parseMultipart(response.body, {boundary: BOUNDARY}, async (part) => {
try {
const chunk = JSON.parse(await part.text());
if (isSessionChunk(chunk)) {
const sessionChunk = chunk;
sessionChunk.meta.trace_id = traceId;
options.onSessionChunk(chunk);
} else if (isStreamDataChunk(chunk)) {
options.onStreamDataChunk(chunk);
} else if (isQueryResponseChunk(chunk)) {
options.onQueryResponseChunk(chunk);
}
} catch (e) {
throw new Error(`Error parsing chunk: ${e}`);
}
});
}
}