Skip to content

Commit

Permalink
fix: save adblock hosts at compile time (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylantientcheu authored Jun 28, 2024
1 parent 8f41892 commit 7ec6f28
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"description": "A custom JavaScript rendering engine based on Playwright",
"main": "dist/index.js",
"scripts": {
"build": "yarn clean && yarn tsc",
"build": "yarn clean && yarn tsc && yarn browser:adblocks",
"ci:start": "ALLOW_LOCALHOST=true yarn start",
"clean": "rm -rf dist/",
"dev": "nodemon",
"dev:run": "yarn build && NODE_ENV=development node -r dotenv/config dist/index.js",
"docker:build": "./scripts/build.sh",
"browser:adblocks": "./scripts/update_adblock_hosts.sh",
"lint": "eslint --ext=jsx,ts,tsx,js .",
"start": "UV_THREADPOOL_SIZE=100 node dist/index.js",
"semantic-release": "semantic-release",
Expand Down
14 changes: 14 additions & 0 deletions scripts/update_adblock_hosts.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

# adblock hosts file URL
URL="https://raw.githubusercontent.com/badmojr/1Hosts/master/Pro/domains.txt"

TARGET_DIR="./dist/lib/browser"
TARGET_FILE="adblock_hosts.txt"

if curl -o "${TARGET_DIR}/${TARGET_FILE}" "$URL" -s; then
echo "✅ adblock hosts download successful."
else
echo "❌ adblock hosts download failed."
exit 1
fi
18 changes: 3 additions & 15 deletions src/lib/browser/Adblocker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { request } from 'undici';
import { promises as fs } from 'fs';

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

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

const log = mainLog.child({ svc: 'adbk' });

/**
Expand All @@ -17,15 +13,8 @@ export class Adblocker {

async load(): Promise<void> {
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]+/);
const data = await fs.readFile(`${__dirname}/adblock_hosts.txt`, 'utf8');
const lines = data.split(/[\r\n]+/);

for (const line of lines) {
if (!line.startsWith('#')) {
Expand All @@ -35,7 +24,6 @@ export class Adblocker {

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

0 comments on commit 7ec6f28

Please sign in to comment.