This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAssignmentsClient.ts
More file actions
105 lines (89 loc) · 2.83 KB
/
AssignmentsClient.ts
File metadata and controls
105 lines (89 loc) · 2.83 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
import IAssignment from '@double-agent/collect-controller/interfaces/IAssignment';
import Config from '@double-agent/config';
import { promises as Fs } from 'fs';
import fetch from 'node-fetch';
import * as qs from 'querystring';
import { Stream } from 'stream';
import * as Tar from 'tar';
export { IAssignment };
export default class AssignmentsClient {
private readonly baseUrl: string;
constructor(readonly userId: string) {
this.baseUrl = Config.runner.assignmentsHost;
}
async downloadAssignmentProfiles(assignmentId: string, filesDir: string): Promise<void> {
const filesStream = await this.get<Stream>(`/download/${assignmentId}`, {
userId: this.userId,
});
await Fs.mkdir(filesDir, { recursive: true }).catch(() => null);
await new Promise((resolve, reject) => {
filesStream
.pipe(
Tar.extract({
cwd: filesDir,
preserveOwner: false,
}),
)
.on('finish', resolve)
.on('error', reject);
});
}
async downloadAll(filesDir: string): Promise<void> {
const filesStream = await this.get<Stream>(`/download`, { userId: this.userId });
await Fs.mkdir(filesDir, { recursive: true }).catch(() => null);
await new Promise((resolve, reject) => {
filesStream
.pipe(
Tar.extract({
cwd: filesDir,
preserveOwner: false,
}),
)
.on('finish', resolve)
.on('error', reject);
});
}
async activate(assignmentId: string): Promise<IAssignment> {
const result = await this.get<{ assignment: IAssignment }>(`/activate/${assignmentId}`, {
userId: this.userId,
});
return result.assignment;
}
/**
* Create and activate a single assignment
*/
async createSingleUserAgentIdAssignment(
userAgentId: string,
dataDir?: string,
): Promise<IAssignment> {
const { assignment } = await this.get<{ assignment: IAssignment }>('/', {
userId: this.userId,
userAgentId,
dataDir,
});
return assignment;
}
async start(params: { dataDir: string; userAgentsToTestPath: string }): Promise<IAssignment[]> {
const result = await this.get<{ assignments: IAssignment[] }>('/create', {
userId: this.userId,
...params,
});
return result.assignments;
}
async finish(): Promise<void> {
await this.get('/finish', { userId: this.userId });
}
private async get<T>(path: string, params: any): Promise<T> {
const paramStrs = qs.stringify(params as any);
const res = await fetch(`${this.baseUrl}${path}?${paramStrs}`);
const contentType = res.headers.get('content-type');
if (contentType === 'application/json') {
const data = await res.json();
if (res.status >= 400) {
throw new Error(data.message);
}
return data;
}
return res.body;
}
}