fetchfox-sdk 1.0.19 → 1.0.21

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fetchfox-sdk",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "AI scraper",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
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
- error: [],
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._completed || this._error;
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
- 'name',
47
- 'state',
48
- 'args',
49
- 'metrics',
50
- 'progress',
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
- async get() {
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
- const s = this.#select(data);
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._completed = true;
103
- this.trigger('completed', this);
103
+ this.#completed = true;
104
104
  }
105
- if (this.state == 'error') {
106
- this._error = true;
107
- this.trigger('error', this);
105
+ if (this.state == 'failed') {
106
+ this.#failed = true;
108
107
  }
108
+ }
109
109
 
110
- if (['completed', 'error'].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
- );
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 error() {
160
- return this.waitFor('error');
166
+ async failed() {
167
+ return this.waitFor('failed');
161
168
  }
162
169
 
163
170
  async finished() {
package/src/fetchfox.js CHANGED
@@ -7,7 +7,7 @@ export const FetchFox = class {
7
7
  this.apiKey = apiKey;
8
8
  this.host = host;
9
9
 
10
- const fns = [crawl, extract, scrape];
10
+ const fns = [visit, crawl, extract, scrape];
11
11
 
12
12
  for (const fn of fns) {
13
13
  this[fn.name] = function (args) {
package/src/index.js CHANGED
@@ -1,11 +1,14 @@
1
- export * from './scrape.js';
1
+ export * from './visit.js';
2
2
  export * from './crawl.js';
3
3
  export * from './extract.js';
4
+ export * from './scrape.js';
5
+
4
6
  export * from './jobs.js';
5
7
  export * from './user.js';
6
8
  export * from './credits.js';
7
9
  export * from './proxy.js';
8
10
  export * from './urls.js';
11
+
9
12
  export { Job } from './detach.js';
10
13
  export { call } from './api.js';
11
14
  export { FetchFox } from './fetchfox.js';
package/src/visit.js ADDED
@@ -0,0 +1,11 @@
1
+ import { call } from './api.js';
2
+ import { Job } from './detach.js';
3
+
4
+ export async function visit(args) {
5
+ return call('POST', '/api/visit', args);
6
+ }
7
+
8
+ visit.detach = async (args) => {
9
+ const data = await call('POST', '/api/visit', { ...args, detach: true });
10
+ return new Job(data.jobId, args);
11
+ };
package/src/#index.js# DELETED
@@ -1,13 +0,0 @@
1
- export * from './scrape.js';
2
- export * from './crawl.js';
3
- export * from './extract.js';
4
- export * from './jobs.js';
5
- export * from './user.js';
6
- export * from './credits.js';
7
- export * from './proxy.js';
8
- export * from './urls.js';
9
-
10
- export { Job } from './detach.js';
11
- export { call } from './api.js';
12
- export { FetchFox } from './fetchfox.js';
13
- export { configure, host, ws, apiKey } from './configure.js';