Skip to content

Commit c1fdb3f

Browse files
committed
Source exclusion and serializer fix
1 parent 12cd3cc commit c1fdb3f

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@unistudents/saffron",
3-
"version": "6.1.2",
3+
"version": "6.1.3",
44
"description": "A fairly intuitive & powerful framework that enables you to collect & save articles and news from all over the web. ",
55
"license": "MIT",
66
"homepage": "https://github.com/unistudents/saffron#readme",
@@ -19,7 +19,7 @@
1919
"build": "tsc -d"
2020
},
2121
"dependencies": {
22-
"axios": "^1.5.1",
22+
"axios": "1.6.0",
2323
"chalk": "^4.1.2",
2424
"cheerio": "^1.0.0-rc.12",
2525
"fast-xml-parser": "^4.3.2",

src/modules/scheduler.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ export class Scheduler {
148148
}
149149

150150
// Exclude sources
151-
excluded.forEach((ex_source: any) => {
152-
const index = this.sources.findIndex((source: Source) => source.name === ex_source);
151+
excluded.forEach((ex_source: string) => {
152+
const index = parsedSources.findIndex((source: Source) => source.name === ex_source);
153153
if (index !== -1)
154154
parsedSources.splice(index, 1);
155155
});

src/utils/serializer.util.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {Source} from "../components/source";
33
import {Job} from "../components/job";
44
import {Instructions} from "../components/instructions";
55

6-
const AsyncFunction = async function () {}.constructor;
6+
const AsyncFunction = (async function () {}).constructor;
77
const types = [Article, Source, Job, Instructions, TextDecoder, Object, Array, Function, AsyncFunction, Error];
88

99
export class Serializer {
@@ -24,6 +24,14 @@ export class Serializer {
2424
_serialize(object: any): object {
2525
if (!(object instanceof Object)) return object;
2626

27+
if(object instanceof AsyncFunction || object instanceof Function) {
28+
const index = this.types.findIndex(e => e.name == object.constructor.name);
29+
return {
30+
index,
31+
entries: object.toString(),
32+
};
33+
}
34+
2735
const index = this.types.findIndex(e => e.name == object.constructor.name);
2836
if (index == -1)
2937
throw new Error(`SerializerException Type '${object.constructor.name}' is not supported for serialization`);
@@ -37,6 +45,10 @@ export class Serializer {
3745
_deserialize(data: any): any {
3846
if (data !== Object(data)) return data;
3947

48+
if(this.types[data.index] === AsyncFunction || this.types[data.index] === Function) {
49+
return eval(data.entries);
50+
}
51+
4052
const obj = new this.types[data.index]();
4153
data.entries.map((entry: any) => obj[entry[0]] = this._deserialize(entry[1]));
4254
return obj;

test/other.test.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {hashCode} from "../src/utils/hashCode.util";
2-
import {pack, Saffron, Source, unpack, Utils} from "../src";
2+
import {Instructions, pack, Saffron, Source, unpack, Utils} from "../src";
33
import {Job, JobStatus} from "../src/components/job";
44
import {expect} from "chai";
55
import {ParserType} from "../src/components/Parser";
@@ -50,7 +50,7 @@ describe('Other', function () {
5050
],
5151
type: 'wordpress-v2',
5252
scrape: {articles: {}}
53-
}, null).then(jobSource => {
53+
}, null).then(async jobSource => {
5454
const job = new Job(jobSource, 'worker-id', 25000, null);
5555

5656
const packed = pack(job);
@@ -82,6 +82,18 @@ describe('Other', function () {
8282
}
8383

8484
expect(source.instructions.parserType).to.equal(ParserType.WORDPRESS_V2);
85+
86+
// Async function test
87+
const instructions = new Instructions();
88+
instructions.axios = async (source) => {
89+
return {
90+
timeout: 2000
91+
};
92+
};
93+
94+
const packed2 = pack(instructions);
95+
const unpacked2: Instructions = unpack(packed2);
96+
expect((await (unpacked2.axios as any)()).timeout).to.equal(2000);
8597
});
8698
});
8799

test/scheduler.test.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
import {expect} from "chai";
22
import {Job, Saffron, Source} from "../src/index";
33
import {JobStatus} from "../src/components/job";
4-
import {Config} from "../src/components/config";
54
import {Dynamic2} from "./abc_dynamics";
65

76
describe('Scheduler', function () {
87
const SOURCES_SIZE = 12;
98

9+
it('Include only / Exclude', function () {
10+
return new Promise(async (resolve) => {
11+
const saffron = new Saffron();
12+
saffron.initialize({
13+
mode: 'main',
14+
sources: {
15+
path: './test/sources',
16+
exclude: ['html1-source']
17+
},
18+
workers: {nodes: ['worker1']},
19+
misc: {log: 'none'}
20+
});
21+
22+
await saffron.scheduler.resetSources();
23+
expect(saffron.scheduler.sources.length).to.equal(SOURCES_SIZE - 1);
24+
25+
resolve(undefined);
26+
});
27+
});
28+
1029
const saffron = new Saffron();
1130
it('Initialization', function () {
1231
saffron.initialize({

0 commit comments

Comments
 (0)