-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstreaming.ts
More file actions
126 lines (104 loc) · 4.05 KB
/
streaming.ts
File metadata and controls
126 lines (104 loc) · 4.05 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {parseMultipart} from '@mjackson/multipart-parser';
import qs from 'qs';
import {
isErrorChunk,
isKeepAliveChunk,
isQueryResponseChunk,
isSessionChunk,
isStreamDataChunk,
} from '../../store/reducers/query/utils';
import {readSettingValueFromLS} from '../../store/reducers/settings/utils';
import type {Actions, StreamQueryParams} from '../../types/api/query';
import type {
QueryResponseChunk,
SessionChunk,
StreamDataChunk,
StreamingChunk,
} from '../../types/store/streaming';
import {DEV_ENABLE_TRACING_FOR_ALL_REQUESTS} from '../../utils/constants';
import {isRedirectToAuth} from '../../utils/response';
import {BaseYdbAPI} from './base';
import {readPartText} from './streamingPartReader';
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 {
private csrfToken?: string;
setCSRFToken = (token: string) => {
this.csrfToken = token;
};
async streamQuery<Action extends Actions>(
params: StreamQueryParams<Action>,
options: StreamQueryOptions,
) {
const base64 = params.base64;
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 (this.csrfToken) {
headers.set('X-CSRF-Token', this.csrfToken);
}
if (params.tracingLevel) {
headers.set('X-Trace-Verbosity', String(params.tracingLevel));
}
const enableTracing = readSettingValueFromLS(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,
credentials: this._axios.defaults.withCredentials ? 'include' : 'same-origin',
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) => {
const text = await readPartText(part);
let chunk: unknown;
try {
chunk = JSON.parse(text);
} catch (e) {
throw new Error(`Error parsing chunk: ${e}`);
}
if (isErrorChunk(chunk)) {
await response.body?.cancel().catch(() => {});
throw chunk;
}
const streamingChunk = chunk as StreamingChunk;
if (isSessionChunk(streamingChunk)) {
const sessionChunk = streamingChunk;
sessionChunk.meta.trace_id = traceId;
options.onSessionChunk(streamingChunk);
} else if (isStreamDataChunk(streamingChunk)) {
options.onStreamDataChunk(streamingChunk);
} else if (isQueryResponseChunk(streamingChunk)) {
options.onQueryResponseChunk(streamingChunk);
} else if (isKeepAliveChunk(streamingChunk)) {
// Logging for debug purposes
console.info('Received keep alive chunk');
}
});
}
}