fetchfox-sdk 1.0.26 → 1.0.28
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 -4
- package/src/detach.js +20 -84
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fetchfox-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.28",
|
|
4
4
|
"description": "AI scraper",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -33,8 +33,5 @@
|
|
|
33
33
|
},
|
|
34
34
|
"lint-staged": {
|
|
35
35
|
"*.{js,jsx,ts,tsx,css,md}": "prettier --write"
|
|
36
|
-
},
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"socket.io-client": "^4.8.1"
|
|
39
36
|
}
|
|
40
37
|
}
|
package/src/detach.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { io } from 'socket.io-client';
|
|
2
1
|
import { ws, appHost } from './configure.js';
|
|
3
2
|
import { jobs } from './jobs.js';
|
|
4
3
|
|
|
@@ -10,9 +9,8 @@ export function getSocket() {}
|
|
|
10
9
|
|
|
11
10
|
export const Job = class {
|
|
12
11
|
#callbacks;
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
#socket3;
|
|
12
|
+
#wsUpdates;
|
|
13
|
+
#wsChildren;
|
|
16
14
|
#seen;
|
|
17
15
|
#completed;
|
|
18
16
|
#failed;
|
|
@@ -33,25 +31,33 @@ export const Job = class {
|
|
|
33
31
|
};
|
|
34
32
|
|
|
35
33
|
this.#seen = {};
|
|
36
|
-
this.#
|
|
37
|
-
this.#
|
|
38
|
-
this.handleProgress(data);
|
|
39
|
-
});
|
|
40
|
-
this.#socket.emit('sub', this.id);
|
|
41
|
-
|
|
42
|
-
this.#socket2 = new WebSocket(ws(options) + '/ws/jobs/' + id);
|
|
43
|
-
this.#socket2.onmessage = (data) => {
|
|
34
|
+
this.#wsUpdates = new WebSocket(ws(options) + '/ws/jobs/' + id);
|
|
35
|
+
this.#wsUpdates.onmessage = (data) => {
|
|
44
36
|
let parsed;
|
|
45
37
|
try {
|
|
46
38
|
parsed = JSON.parse(data.data);
|
|
47
39
|
} catch {
|
|
48
40
|
return;
|
|
49
41
|
}
|
|
42
|
+
|
|
43
|
+
for (const key of Object.keys(parsed)) {
|
|
44
|
+
this[key] = parsed[key];
|
|
45
|
+
}
|
|
46
|
+
|
|
50
47
|
this.trigger('update', parsed);
|
|
48
|
+
|
|
49
|
+
if (!this.#completed && parsed.state == 'completed') {
|
|
50
|
+
this.#completed = true;
|
|
51
|
+
this.trigger('finished', parsed);
|
|
52
|
+
}
|
|
53
|
+
if (!this.#failed && parsed.state == 'failed') {
|
|
54
|
+
this.#failed = true;
|
|
55
|
+
this.trigger('finished', parsed);
|
|
56
|
+
}
|
|
51
57
|
};
|
|
52
58
|
|
|
53
|
-
this.#
|
|
54
|
-
this.#
|
|
59
|
+
this.#wsChildren = new WebSocket(ws(options) + '/ws/children/' + id);
|
|
60
|
+
this.#wsChildren.onmessage = (data) => {
|
|
55
61
|
let parsed;
|
|
56
62
|
try {
|
|
57
63
|
parsed = JSON.parse(data.data);
|
|
@@ -91,76 +97,6 @@ export const Job = class {
|
|
|
91
97
|
return s;
|
|
92
98
|
}
|
|
93
99
|
|
|
94
|
-
updateFromData(data) {
|
|
95
|
-
const s = this.#select(data);
|
|
96
|
-
for (const key of Object.keys(s)) {
|
|
97
|
-
this[key] = s[key];
|
|
98
|
-
}
|
|
99
|
-
if (['completed', 'failed'].includes(this.state)) {
|
|
100
|
-
if (this.progress?.children?.jobs) {
|
|
101
|
-
this.progress.children.jobs = this.progress.children.jobs.filter(
|
|
102
|
-
(it) => it.state != 'active'
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async get() {
|
|
109
|
-
const data = await jobs.get(this.id);
|
|
110
|
-
this.updateFromData(data);
|
|
111
|
-
return this;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
handleProgress(data) {
|
|
115
|
-
const last = JSON.stringify(this);
|
|
116
|
-
|
|
117
|
-
this.updateFromData(data);
|
|
118
|
-
|
|
119
|
-
const didUpdate = JSON.stringify(this) != last;
|
|
120
|
-
if (didUpdate) {
|
|
121
|
-
this.trigger('progress', this);
|
|
122
|
-
|
|
123
|
-
for (const item of this.results?.items || []) {
|
|
124
|
-
const ser = JSON.stringify(item);
|
|
125
|
-
if (this.#seen[ser]) {
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
this.#seen[ser] = true;
|
|
129
|
-
this.trigger('item', item);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
if (this.state == 'completed') {
|
|
133
|
-
this.#completed = true;
|
|
134
|
-
}
|
|
135
|
-
if (this.state == 'failed') {
|
|
136
|
-
this.#failed = true;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (['completed', 'failed'].includes(this.state)) {
|
|
141
|
-
console.log('Got state:', this.state);
|
|
142
|
-
|
|
143
|
-
if (this.progress?.children?.jobs) {
|
|
144
|
-
this.progress.children.jobs = this.progress.children.jobs.filter(
|
|
145
|
-
(it) => it.state != 'active'
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
this.#socket.disconnect();
|
|
150
|
-
|
|
151
|
-
console.log('Set timeout before triggering completion');
|
|
152
|
-
setTimeout(() => {
|
|
153
|
-
this.get()
|
|
154
|
-
.then(() => {
|
|
155
|
-
this.trigger('progress', this);
|
|
156
|
-
this.trigger(this.state, this);
|
|
157
|
-
this.trigger('finished', this);
|
|
158
|
-
})
|
|
159
|
-
.catch((e) => console.error(e));
|
|
160
|
-
}, 500);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
100
|
checkEvent(event) {
|
|
165
101
|
if (!this.#callbacks[event]) {
|
|
166
102
|
throw new FetchFoxError(`Invalid event: ${event}`);
|