-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathgrpc-progressible.ts
278 lines (266 loc) · 9.33 KB
/
grpc-progressible.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import { v4 } from 'uuid';
import {
ProgressMessage,
ResponseService,
} from '../common/protocol/response-service';
import {
UpdateCoreLibrariesIndexResponse,
UpdateIndexResponse,
UpdateLibrariesIndexResponse,
} from './cli-protocol/cc/arduino/cli/commands/v1/commands_pb';
import {
DownloadProgress,
TaskProgress,
} from './cli-protocol/cc/arduino/cli/commands/v1/common_pb';
import {
PlatformInstallResponse,
PlatformUninstallResponse,
} from './cli-protocol/cc/arduino/cli/commands/v1/core_pb';
import {
LibraryInstallResponse,
LibraryUninstallResponse,
ZipLibraryInstallResponse,
} from './cli-protocol/cc/arduino/cli/commands/v1/lib_pb';
type LibraryProgressResponse =
| LibraryInstallResponse
| LibraryUninstallResponse
| ZipLibraryInstallResponse;
namespace LibraryProgressResponse {
export function is(response: unknown): response is LibraryProgressResponse {
return (
response instanceof LibraryInstallResponse ||
response instanceof LibraryUninstallResponse ||
response instanceof ZipLibraryInstallResponse
);
}
export function workUnit(response: LibraryProgressResponse): UnitOfWork {
return {
task: response.getTaskProgress(),
...(response instanceof LibraryInstallResponse && {
download: response.getProgress(),
}),
};
}
}
type PlatformProgressResponse =
| PlatformInstallResponse
| PlatformUninstallResponse;
namespace PlatformProgressResponse {
export function is(response: unknown): response is PlatformProgressResponse {
return (
response instanceof PlatformInstallResponse ||
response instanceof PlatformUninstallResponse
);
}
export function workUnit(response: PlatformProgressResponse): UnitOfWork {
return {
task: response.getTaskProgress(),
...(response instanceof PlatformInstallResponse && {
download: response.getProgress(),
}),
};
}
}
type IndexProgressResponse =
| UpdateIndexResponse
| UpdateLibrariesIndexResponse
| UpdateCoreLibrariesIndexResponse;
namespace IndexProgressResponse {
export function is(response: unknown): response is IndexProgressResponse {
return (
response instanceof UpdateIndexResponse ||
response instanceof UpdateLibrariesIndexResponse ||
response instanceof UpdateCoreLibrariesIndexResponse // not used by the IDE2 but available for full typings compatibility
);
}
export function workUnit(response: IndexProgressResponse): UnitOfWork {
return { download: response.getDownloadProgress() };
}
}
export type ProgressResponse =
| LibraryProgressResponse
| PlatformProgressResponse
| IndexProgressResponse;
interface UnitOfWork {
task?: TaskProgress;
download?: DownloadProgress;
}
/**
* It's solely a dev thing. Flip it to `true` if you want to debug the progress from the CLI responses.
*/
const DEBUG = false;
export namespace ExecuteWithProgress {
export interface Options {
/**
* _unknown_ progress if falsy.
*/
readonly progressId?: string;
readonly responseService: Partial<ResponseService>;
}
export function createDataCallback<R extends ProgressResponse>({
responseService,
progressId,
}: ExecuteWithProgress.Options): (response: R) => void {
const uuid = v4();
let localFile = '';
let localTotalSize = Number.NaN;
return (response: R) => {
if (DEBUG) {
const json = toJson(response);
if (json) {
console.log(`Progress response [${uuid}]: ${json}`);
}
}
const { task, download } = resolve(response);
if (!download && !task) {
console.warn(
"Implementation error. Neither 'download' nor 'task' is available."
);
// This is still an API error from the CLI, but IDE2 ignores it.
// Technically, it does not cause an error, but could mess up the progress reporting.
// See an example of an empty object `{}` repose here: https://github.com/arduino/arduino-ide/issues/906#issuecomment-1171145630.
return;
}
if (task && download) {
throw new Error(
"Implementation error. Both 'download' and 'task' are available."
);
}
if (task) {
const message = task.getName() || task.getMessage();
if (message) {
if (progressId) {
responseService.reportProgress?.({
progressId,
message,
work: { done: Number.NaN, total: Number.NaN },
});
}
responseService.appendToOutput?.({ chunk: `${message}\n` });
}
} else if (download) {
if (download.getFile() && !localFile) {
localFile = download.getFile();
}
if (download.getTotalSize() > 0 && Number.isNaN(localTotalSize)) {
localTotalSize = download.getTotalSize();
}
// This happens only once per file download.
if (download.getTotalSize() && localFile) {
responseService.appendToOutput?.({ chunk: `${localFile}\n` });
}
if (progressId && localFile) {
let work: ProgressMessage.Work | undefined = undefined;
if (download.getDownloaded() > 0 && !Number.isNaN(localTotalSize)) {
work = {
total: localTotalSize,
done: download.getDownloaded(),
};
}
responseService.reportProgress?.({
progressId,
message: `Downloading ${localFile}`,
work,
});
}
if (download.getCompleted()) {
// Discard local state.
if (progressId && !Number.isNaN(localTotalSize)) {
responseService.reportProgress?.({
progressId,
message: '',
work: { done: Number.NaN, total: Number.NaN },
});
}
localFile = '';
localTotalSize = Number.NaN;
}
}
};
}
function resolve(response: unknown): Readonly<Partial<UnitOfWork>> {
if (LibraryProgressResponse.is(response)) {
return LibraryProgressResponse.workUnit(response);
} else if (PlatformProgressResponse.is(response)) {
return PlatformProgressResponse.workUnit(response);
} else if (IndexProgressResponse.is(response)) {
return IndexProgressResponse.workUnit(response);
}
console.warn('Unhandled gRPC response', response);
return {};
}
function toJson(response: ProgressResponse): string | undefined {
if (response instanceof LibraryInstallResponse) {
return JSON.stringify(LibraryInstallResponse.toObject(false, response));
} else if (response instanceof LibraryUninstallResponse) {
return JSON.stringify(LibraryUninstallResponse.toObject(false, response));
} else if (response instanceof ZipLibraryInstallResponse) {
return JSON.stringify(
ZipLibraryInstallResponse.toObject(false, response)
);
} else if (response instanceof PlatformInstallResponse) {
return JSON.stringify(PlatformInstallResponse.toObject(false, response));
} else if (response instanceof PlatformUninstallResponse) {
return JSON.stringify(
PlatformUninstallResponse.toObject(false, response)
);
} else if (response instanceof UpdateIndexResponse) {
return JSON.stringify(UpdateIndexResponse.toObject(false, response));
} else if (response instanceof UpdateLibrariesIndexResponse) {
return JSON.stringify(
UpdateLibrariesIndexResponse.toObject(false, response)
);
} else if (response instanceof UpdateCoreLibrariesIndexResponse) {
return JSON.stringify(
UpdateCoreLibrariesIndexResponse.toObject(false, response)
);
}
console.warn('Unhandled gRPC response', response);
return undefined;
}
}
export class IndexesUpdateProgressHandler {
private done = 0;
private readonly total: number;
readonly progressId: string;
constructor(
additionalUrlsCount: number,
private readonly onProgress: (progressMessage: ProgressMessage) => void,
private readonly onError?: ({
progressId,
message,
}: {
progressId: string;
message: string;
}) => void,
private readonly onStart?: (progressId: string) => void,
private readonly onEnd?: (progressId: string) => void
) {
this.progressId = v4();
this.total = IndexesUpdateProgressHandler.total(additionalUrlsCount);
// Note: at this point, the IDE2 backend might not have any connected clients, so this notification is not delivered to anywhere
// Hence, clients must handle gracefully when no `willUpdate` is received before any `didProgress`.
this.onStart?.(this.progressId);
}
reportEnd(): void {
this.onEnd?.(this.progressId);
}
reportProgress(message: string): void {
this.onProgress({
message,
progressId: this.progressId,
work: { total: this.total, done: ++this.done },
});
}
reportError(message: string): void {
this.onError?.({ progressId: this.progressId, message });
}
private static total(additionalUrlsCount: number): number {
// +1 for the `package_index.tar.bz2` when updating the platform index.
const totalPlatformIndexCount = additionalUrlsCount + 1;
// The `library_index.json.gz` and `library_index.json.sig` when running the library index update.
const totalLibraryIndexCount = 2;
// +1 for the `initInstance` call after the index update (`reportEnd`)
return totalPlatformIndexCount + totalLibraryIndexCount + 1;
}
}