rentgen/request-cluster.ts

95 lines
2.2 KiB
TypeScript
Raw Normal View History

2021-10-03 16:33:23 +02:00
import { EventEmitter } from "events";
import ExtendedRequest from "./extended-request";
2021-10-04 18:51:51 +02:00
export type Sources = "cookie" | "pathname" | "queryparams" | "header";
import { TCString, TCModel } from "@iabtcf/core";
2021-10-03 16:33:23 +02:00
2021-10-03 20:13:36 +02:00
export class StolenDataEntry {
public isIAB = false;
public iab: TCModel | null = null;
2021-10-04 18:51:51 +02:00
constructor(
public request: ExtendedRequest,
public source: Sources,
public name: string,
public value: string
) {
try {
this.iab = TCString.decode(value);
console.log(this.iab);
this.isIAB = true;
} catch (e) {}
}
2021-10-04 18:51:51 +02:00
getPriority() {
let priority = 0;
priority += this.value.length;
const url = new URL(this.request.getOrigin());
if (this.value.includes(url.host) || this.value.includes(url.pathname)) {
priority += 100;
}
return priority;
}
2021-10-03 20:13:36 +02:00
}
2021-10-03 16:33:23 +02:00
export class RequestCluster extends EventEmitter {
public requests: ExtendedRequest[] = [];
constructor(public id: string) {
super();
}
add(request: ExtendedRequest) {
this.requests.push(request);
this.emit("change");
}
hasCookies() {
for (const request of this.requests) {
if (request.hasCookie()) {
return true;
}
}
return false;
}
2021-10-04 18:51:51 +02:00
getStolenData(filter: { minValueLength: number }): StolenDataEntry[] {
return this.requests
.map((request) => request.getAllStolenData())
2021-10-03 16:33:23 +02:00
.reduce((a, b) => a.concat(b), [])
2021-10-04 18:51:51 +02:00
.filter((entry) => {
return entry.value.length >= filter.minValueLength;
})
.sort((entry1, entry2) =>
entry1.getPriority() > entry2.getPriority() ? -1 : 1
)
.filter((element, index, array) => {
// remove duplicate neighbours
if (index == 0) {
return true;
}
if (
element.name != array[index - 1].name ||
element.value != array[index - 1].value
) {
return true;
}
});
2021-10-03 16:33:23 +02:00
}
static sortCompare(a: RequestCluster, b: RequestCluster) {
if (a.hasCookies() == b.hasCookies()) {
if (a.id < b.id) {
return -1;
} else {
return 1;
}
} else {
if (a.hasCookies()) {
return -1;
} else {
return 1;
}
}
}
}