fase(3): crawling module domain and application

This commit is contained in:
debian
2026-03-04 16:32:09 -05:00
parent 4a58749048
commit 39c5313ba5
40 changed files with 1117 additions and 13 deletions

View File

@@ -0,0 +1,80 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CrawlSession = void 0;
const AggregateRoot_1 = require("../../../../shared/domain/AggregateRoot");
const Result_1 = require("../../../../shared/domain/Result");
const Url_1 = require("../value-objects/Url");
const CrawlStarted_1 = require("../events/CrawlStarted");
const CrawlCompleted_1 = require("../events/CrawlCompleted");
const CrawlFailed_1 = require("../events/CrawlFailed");
class CrawlSession extends AggregateRoot_1.AggregateRoot {
constructor(props, id) {
super(props, id);
}
static create(request) {
const urlResult = Url_1.Url.create(request.url);
if (!urlResult.ok) {
return (0, Result_1.Err)(urlResult.error);
}
const props = {
url: request.url,
status: 'running',
seed: request.seed,
maxStates: request.maxStates,
statesVisited: 0,
config: request.config ?? {},
};
const session = new CrawlSession(props);
session.addDomainEvent(new CrawlStarted_1.CrawlStarted(session.id.toString(), {
url: request.url,
seed: request.seed,
maxStates: request.maxStates,
}));
return (0, Result_1.Ok)(session);
}
get url() {
return this.props.url;
}
get status() {
return this.props.status;
}
get seed() {
return this.props.seed;
}
get maxStates() {
return this.props.maxStates;
}
get statesVisited() {
return this.props.statesVisited;
}
get config() {
return this.props.config;
}
incrementStatesVisited() {
this.props = { ...this.props, statesVisited: this.props.statesVisited + 1 };
}
complete() {
this.props = { ...this.props, status: 'completed' };
this.addDomainEvent(new CrawlCompleted_1.CrawlCompleted(this.id.toString(), {
url: this.props.url,
statesVisited: this.props.statesVisited,
}));
}
fail(reason) {
this.props = { ...this.props, status: 'failed' };
this.addDomainEvent(new CrawlFailed_1.CrawlFailed(this.id.toString(), {
url: this.props.url,
reason,
statesVisited: this.props.statesVisited,
}));
}
stop() {
this.props = { ...this.props, status: 'stopped' };
this.addDomainEvent(new CrawlCompleted_1.CrawlCompleted(this.id.toString(), {
url: this.props.url,
statesVisited: this.props.statesVisited,
stopped: true,
}));
}
}
exports.CrawlSession = CrawlSession;