Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/api/crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,9 @@ export class CrawlerHost extends RPCHost {
if (opts.markdown) {
this.threadLocal.set('turndownOpts', opts.markdown);
}
if (opts.freshness) {
this.threadLocal.set('x-brave-freshness', opts.freshness);
}

const crawlOpts: ExtraScrappingOptions = {
proxyUrl: opts.proxyUrl,
Expand Down
8 changes: 8 additions & 0 deletions src/dto/crawler-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,9 @@ export class CrawlerOptions extends AutoCastable {
})
respondTiming?: RESPOND_TIMING;

@Prop()
freshness?: string;

_hintIps?: string[];

static override from(input: any) {
Expand Down Expand Up @@ -589,6 +592,11 @@ export class CrawlerOptions extends AutoCastable {
instance.respondTiming ??= respondTiming as RESPOND_TIMING;
}

const freshness = ctx?.get('x-brave-freshness');
if (freshness && /^(pd|pw|pm|py|\d{4}-\d{2}-\d{2}to\d{4}-\d{2}-\d{2})$/.test(freshness)) {
instance.freshness ??= freshness;
}

if (instance.cacheTolerance) {
instance.cacheTolerance = instance.cacheTolerance * 1000;
}
Expand Down
9 changes: 7 additions & 2 deletions src/services/brave-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class BraveSearchService extends AsyncService {
this.braveSearchHTTP = new BraveSearchHTTP(this.secretExposer.BRAVE_SEARCH_API_KEY);
}

async webSearch(query: WebSearchQueryParams) {
async webSearch(query: WebSearchQueryParams & { freshness?: string }) {
const ip = this.threadLocal.get('ip');
const extraHeaders: WebSearchOptionalHeaderOptions = {};
if (ip) {
Expand Down Expand Up @@ -61,11 +61,16 @@ export class BraveSearchService extends AsyncService {
extraHeaders['User-Agent'] = this.threadLocal.get('userAgent');
}

const encoded = { ...query };
const encoded: Record<string, any> = { ...query };
if (encoded.q) {
encoded.q = (Buffer.from(encoded.q).toString('ascii') === encoded.q) ? encoded.q : encodeURIComponent(encoded.q);
}

const freshness = this.threadLocal.get('x-brave-freshness') || query.freshness;
if (freshness) {
encoded.freshness = freshness;
}

let maxTries = 11;

while (maxTries--) {
Expand Down