diff --git a/.gitignore b/.gitignore index c2d28d0..c7a2ae9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules dist -sources/src/#generated-*.ts \ No newline at end of file +sources/src/#generated-*.ts +.buildcache \ No newline at end of file diff --git a/builder/package.json b/builder/package.json new file mode 100644 index 0000000..30d80af --- /dev/null +++ b/builder/package.json @@ -0,0 +1,32 @@ +{ + "name": "@filteringdev/tinyshield-builder", + "private": true, + "type": "module", + "scripts": { + "lint": "tsc --noEmit && eslint **/*.ts", + "build": "tsx source/build.ts", + "debug": "tsx source/debug.ts", + "cache": "tsx source/cache.ts", + "clean": "rm -rf dist && rm -rf .buildcache" + }, + "dependencies": { + "@types/node": "^24.10.8" + }, + "devDependencies": { + "@adguard/agtree": "^3.4.3", + "@npmcli/package-json": "^7.0.4", + "@types/npmcli__package-json": "^4.0.4", + "@types/semver": "^7.7.1", + "@typescriptprime/parsing": "^1.0.4", + "@typescriptprime/securereq": "^1.0.1", + "chokidar": "^5.0.0", + "esbuild": "^0.27.2", + "eslint": "^9.39.2", + "semver": "^7.7.3", + "tldts": "^7.0.19", + "tsx": "^4.21.0", + "typescript": "^5.9.3", + "typescript-eslint": "^8.53.0", + "zod": "^4.3.5" + } +} diff --git a/sources/banner.txt b/builder/source/banner.txt similarity index 100% rename from sources/banner.txt rename to builder/source/banner.txt diff --git a/builder.ts b/builder/source/builder.ts similarity index 99% rename from builder.ts rename to builder/source/builder.ts index 478665b..326276f 100644 --- a/builder.ts +++ b/builder/source/builder.ts @@ -99,7 +99,7 @@ let AttachHeaderPath = `/tmp/${crypto.randomUUID()}` Fs.writeFileSync(AttachHeaderPath, ConvertedHeader, { encoding: 'utf-8', mode: 0o700 }) console.log('Written temporary header file to:', AttachHeaderPath) await ESBuild.build({ - entryPoints: ['./sources/src/index.ts'], + entryPoints: ['./sources/index.ts'], bundle: true, minify: BuildType === 'production', define: { diff --git a/builder/source/references/iabsellers.ts b/builder/source/references/iabsellers.ts new file mode 100644 index 0000000..cbfab0e --- /dev/null +++ b/builder/source/references/iabsellers.ts @@ -0,0 +1,38 @@ +import * as Zod from 'zod' +import { HTTPSRequest } from '@typescriptprime/securereq' + + +const IABSellersJsonURL = 'https://info.ad-shield.io/sellers.json' + +export async function FetchIABSellersJsonData(): Promise { + const IABSellersJsonResponse: { StatusCode: number, Headers: Record, Body: unknown } = await HTTPSRequest(new URL(IABSellersJsonURL), { ExpectedAs: 'JSON' }) + let IABSellersJsonData =IABSellersJsonResponse.Body as { + // eslint-disable-next-line @typescript-eslint/naming-convention + sellers: Array<{ + // eslint-disable-next-line @typescript-eslint/naming-convention + seller_id: number, + // eslint-disable-next-line @typescript-eslint/naming-convention + seller_type: string, + // eslint-disable-next-line @typescript-eslint/naming-convention + name: string, + // eslint-disable-next-line @typescript-eslint/naming-convention + domain: string + }> + } + IABSellersJsonData = await Zod.object({ + sellers: Zod.array(Zod.object({ + seller_id: Zod.number(), + seller_type: Zod.string(), + name: Zod.string(), + domain: Zod.string().refine(D => { + try { + new URL(`https://${D}`) + } catch { + return false + } + return true + }) + })) + }).parseAsync(IABSellersJsonData) + return [...new Set(IABSellersJsonData.sellers.map(S => S.domain))] +} \ No newline at end of file diff --git a/builder/source/utils/http-server.ts b/builder/source/utils/http-server.ts new file mode 100644 index 0000000..7de809f --- /dev/null +++ b/builder/source/utils/http-server.ts @@ -0,0 +1,33 @@ +import * as HTTP from 'node:http' +import * as Fs from 'node:fs' + +function IsLoopBack(IP: string) { + return IP === '127.0.0.1' || IP === '::1' || IP === '::ffff:127.0.0.1' +} + +export function RunDebugServer(Port: number, FileName: string[], ShouldPreventHTTPResponse: boolean) { + const HTTPServer = HTTP.createServer((Req, Res) => { + if (!FileName.includes(Req.url?.substring(1) || '')) { + Res.writeHead(404) + Res.end() + return + } else if (!IsLoopBack(Req.socket.remoteAddress)) { + Res.writeHead(403) + Res.end() + return + } else if (ShouldPreventHTTPResponse || !Fs.existsSync(`${process.cwd()}/dist/${Req.url?.substring(1)}`)) { + Res.writeHead(503) + Res.end('File not built yet.') + return + } + + const Content = Fs.readFileSync(`${process.cwd()}/dist/${Req.url?.substring(1)}`, 'utf-8') + Res.writeHead(200, { + 'content-type': 'application/javascript; charset=utf-8', + 'content-length': new TextEncoder().encode(Content).byteLength.toString() + }) + Res.end(Content) + }) + + HTTPServer.listen(Port) +} \ No newline at end of file diff --git a/builder/tsconfig.json b/builder/tsconfig.json new file mode 100644 index 0000000..bcb6707 --- /dev/null +++ b/builder/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../tsconfig.json", + "include": [ + "source/**/*.ts", + "test/**/*.ts" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index 170f47a..31b569f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "build:userscript": "tsx builder.ts -- production", "build": "npm run build:interface && npm run build:userscript", "debug": "tsx builder.ts -- development", - "lint": "tsc --noEmit && eslint sources/**/*.ts" + "lint": "npm run lint -w builder && npm run lint -w userscript" }, "keywords": [ "Ad-Shield" @@ -28,20 +28,9 @@ "url": "git+https://github.com/FilteringDev/tinyShield.git" }, "license": "MPL-2.0", - "dependencies": { - "@types/node": "^24.9.2" - }, - "devDependencies": { - "@npmcli/package-json": "^7.0.4", - "@types/npmcli__package-json": "^4.0.4", - "@types/semver": "^7.7.1", - "esbuild": "^0.27.0", - "eslint": "^9.38.0", - "semver": "^7.7.3", - "tsx": "^4.21.0", - "typescript": "^5.9.3", - "typescript-eslint": "^8.46.2", - "zod": "^4.3.5" - }, - "packageManager": "npm@11.5.1+" + "packageManager": "npm@11.5.1+", + "workspaces": [ + "userscript", + "builder" + ] } diff --git a/sources/esbuild.inject.ts b/sources/esbuild.inject.ts deleted file mode 100644 index 02b2d13..0000000 --- a/sources/esbuild.inject.ts +++ /dev/null @@ -1,9 +0,0 @@ -/*! - * @license MPL-2.0 - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - * - * Contributors: - * - See Git history at https://github.com/FilteringDev/tinyShield for detailed authorship information. - */ \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 398aab3..ac06520 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,12 @@ "moduleResolution": "NodeNext", "removeComments": false, "alwaysStrict": false, - "skipLibCheck": true + "skipLibCheck": true, + "paths": { + "@builder/*": ["./builder/source/*"], + "@userscript/*": ["./userscript/source/*"], + "@root/*": ["./source/*"], + "@reporoot/*": ["./*"] + } } } \ No newline at end of file diff --git a/userscript/package.json b/userscript/package.json new file mode 100644 index 0000000..92dd5e1 --- /dev/null +++ b/userscript/package.json @@ -0,0 +1,11 @@ +{ + "name": "@filteringdev/tinyshield-userscript", + "private": true, + "type": "module", + "scripts": { + "lint": "tsc --noEmit && eslint **/*.ts" + }, + "devDependencies": { + "@types/web": "^0.0.317" + } +} diff --git a/sources/src/as-weakmap.ts b/userscript/source/as-weakmap.ts similarity index 100% rename from sources/src/as-weakmap.ts rename to userscript/source/as-weakmap.ts diff --git a/sources/src/index.ts b/userscript/source/index.ts similarity index 100% rename from sources/src/index.ts rename to userscript/source/index.ts diff --git a/sources/interface.ts b/userscript/source/interface.ts similarity index 86% rename from sources/interface.ts rename to userscript/source/interface.ts index 5621353..659cced 100644 --- a/sources/interface.ts +++ b/userscript/source/interface.ts @@ -8,4 +8,4 @@ * - See Git history at https://github.com/FilteringDev/tinyShield for detailed authorship information. */ -export { RunTinyShieldUserscript } from './src/index.js' \ No newline at end of file +export { RunTinyShieldUserscript } from './index.js' \ No newline at end of file diff --git a/sources/src/utils.ts b/userscript/source/utils.ts similarity index 100% rename from sources/src/utils.ts rename to userscript/source/utils.ts diff --git a/userscript/tsconfig.json b/userscript/tsconfig.json new file mode 100644 index 0000000..ed149fb --- /dev/null +++ b/userscript/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "include": [ + "source/**/*.ts" + ] +} \ No newline at end of file