Skip to content

Commit

Permalink
fix: make the service load independent of adblocker (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylantientcheu authored Jun 26, 2024
1 parent 93fad22 commit 8d6512b
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/lib/browser/Adblocker.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { request } from 'undici';

import { report } from '../../helpers/errorReporting';
import { log as mainLog } from '../../helpers/logger';

// TO DO: copy this at compile time.
// TO DO: Cronjob to update this list on our servers
const list =
'https://raw.githubusercontent.com/badmojr/1Hosts/master/Pro/domains.txt';

Expand All @@ -15,26 +16,30 @@ export class Adblocker {
#hostnames: Set<string> = new Set();

async load(): Promise<void> {
const res = await request(list, {
method: 'GET',
});

let body = '';
for await (const chunk of res.body) {
body += chunk.toString();
}
const lines = body.split(/[\r\n]+/);
try {
const res = await request(list, {
method: 'GET',
});

let body = '';
for await (const chunk of res.body) {
body += chunk.toString();
}
const lines = body.split(/[\r\n]+/);

for (const line of lines) {
if (!line.startsWith('#')) {
this.#hostnames.add(line);
for (const line of lines) {
if (!line.startsWith('#')) {
this.#hostnames.add(line);
}
}
}

log.info('Ready', {
entries: this.#hostnames.size,
lastMod: res.headers['last-modified'],
});
log.info('Ready', {
entries: this.#hostnames.size,
lastMod: res.headers['last-modified'],
});
} catch (err: any) {
report(new Error('Error while setting up adblocker'), { err });
}
}

match(url: URL): boolean {
Expand Down

0 comments on commit 8d6512b

Please sign in to comment.