fetchfox-sdk 1.0.20 → 1.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/agent.js +11 -0
- package/src/api.js +0 -1
- package/src/detach.js +45 -38
- package/src/index.js +1 -0
package/package.json
CHANGED
package/src/agent.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { call } from './api.js';
|
|
2
|
+
import { Job } from './detach.js';
|
|
3
|
+
|
|
4
|
+
export async function agent(args) {
|
|
5
|
+
return call('POST', '/api/agent', args);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
agent.detach = async (args) => {
|
|
9
|
+
const data = await call('POST', '/api/agent', { ...args, detach: true });
|
|
10
|
+
return new Job(data.jobId, args);
|
|
11
|
+
};
|
package/src/api.js
CHANGED
package/src/detach.js
CHANGED
|
@@ -12,13 +12,15 @@ export const Job = class {
|
|
|
12
12
|
#callbacks;
|
|
13
13
|
#socket;
|
|
14
14
|
#seen;
|
|
15
|
+
#completed;
|
|
16
|
+
#failed;
|
|
15
17
|
|
|
16
18
|
constructor(id, options) {
|
|
17
19
|
this.id = id;
|
|
18
20
|
this.#callbacks = {
|
|
19
21
|
item: [],
|
|
20
22
|
completed: [],
|
|
21
|
-
|
|
23
|
+
failed: [],
|
|
22
24
|
finished: [],
|
|
23
25
|
progress: [],
|
|
24
26
|
};
|
|
@@ -33,7 +35,7 @@ export const Job = class {
|
|
|
33
35
|
}
|
|
34
36
|
|
|
35
37
|
get _finished() {
|
|
36
|
-
return this
|
|
38
|
+
return this.#completed || this.#failed;
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
get appUrl() {
|
|
@@ -42,22 +44,15 @@ export const Job = class {
|
|
|
42
44
|
|
|
43
45
|
#select(data) {
|
|
44
46
|
const s = {};
|
|
45
|
-
for (const key of
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
'results',
|
|
52
|
-
'artifacts',
|
|
53
|
-
'timer',
|
|
54
|
-
]) {
|
|
55
|
-
s[key] = data[key] || this[key];
|
|
47
|
+
for (const key of Object.keys(data)) {
|
|
48
|
+
const val = data[key] ?? this[key];
|
|
49
|
+
if (val === undefined) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
s[key] = val;
|
|
56
53
|
}
|
|
57
54
|
|
|
58
55
|
if (s.progress?.children?.jobs) {
|
|
59
|
-
// const late = this.progress.children.jobs.filter(it => it.late);
|
|
60
|
-
// console.log('late jobs:', late);
|
|
61
56
|
s.progress.children.jobs = s.progress.children.jobs.filter(
|
|
62
57
|
(it) => !it.late
|
|
63
58
|
);
|
|
@@ -66,24 +61,30 @@ export const Job = class {
|
|
|
66
61
|
return s;
|
|
67
62
|
}
|
|
68
63
|
|
|
69
|
-
|
|
70
|
-
const data = await jobs.get(this.id);
|
|
64
|
+
updateFromData(data) {
|
|
71
65
|
const s = this.#select(data);
|
|
72
66
|
for (const key of Object.keys(s)) {
|
|
73
67
|
this[key] = s[key];
|
|
74
68
|
}
|
|
69
|
+
if (['completed', 'failed'].includes(this.state)) {
|
|
70
|
+
if (this.progress?.children?.jobs) {
|
|
71
|
+
this.progress.children.jobs = this.progress.children.jobs.filter(
|
|
72
|
+
(it) => it.state != 'active'
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async get() {
|
|
79
|
+
const data = await jobs.get(this.id);
|
|
80
|
+
this.updateFromData(data);
|
|
75
81
|
return this;
|
|
76
82
|
}
|
|
77
83
|
|
|
78
84
|
handleProgress(data) {
|
|
79
|
-
console.log('handleProgress', data);
|
|
80
|
-
|
|
81
85
|
const last = JSON.stringify(this);
|
|
82
86
|
|
|
83
|
-
|
|
84
|
-
for (const key of Object.keys(s)) {
|
|
85
|
-
this[key] = s[key];
|
|
86
|
-
}
|
|
87
|
+
this.updateFromData(data);
|
|
87
88
|
|
|
88
89
|
const didUpdate = JSON.stringify(this) != last;
|
|
89
90
|
if (didUpdate) {
|
|
@@ -99,23 +100,29 @@ export const Job = class {
|
|
|
99
100
|
}
|
|
100
101
|
|
|
101
102
|
if (this.state == 'completed') {
|
|
102
|
-
this
|
|
103
|
-
this.trigger('completed', this);
|
|
103
|
+
this.#completed = true;
|
|
104
104
|
}
|
|
105
|
-
if (this.state == '
|
|
106
|
-
this
|
|
107
|
-
this.trigger('error', this);
|
|
105
|
+
if (this.state == 'failed') {
|
|
106
|
+
this.#failed = true;
|
|
108
107
|
}
|
|
108
|
+
}
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
this.trigger('finished', this);
|
|
117
|
-
this.#socket.disconnect();
|
|
110
|
+
if (['completed', 'failed'].includes(this.state)) {
|
|
111
|
+
if (this.progress?.children?.jobs) {
|
|
112
|
+
this.progress.children.jobs = this.progress.children.jobs.filter(
|
|
113
|
+
(it) => it.state != 'active'
|
|
114
|
+
);
|
|
118
115
|
}
|
|
116
|
+
|
|
117
|
+
this.#socket.disconnect();
|
|
118
|
+
|
|
119
|
+
this.get()
|
|
120
|
+
.then(() => {
|
|
121
|
+
this.trigger('progress', this);
|
|
122
|
+
this.trigger(this.state, this);
|
|
123
|
+
this.trigger('finished', this);
|
|
124
|
+
})
|
|
125
|
+
.catch((e) => console.error(e));
|
|
119
126
|
}
|
|
120
127
|
}
|
|
121
128
|
|
|
@@ -156,8 +163,8 @@ export const Job = class {
|
|
|
156
163
|
return this.waitFor('completed');
|
|
157
164
|
}
|
|
158
165
|
|
|
159
|
-
async
|
|
160
|
-
return this.waitFor('
|
|
166
|
+
async failed() {
|
|
167
|
+
return this.waitFor('failed');
|
|
161
168
|
}
|
|
162
169
|
|
|
163
170
|
async finished() {
|