Skip to content

Commit

Permalink
add typings to archivedb
Browse files Browse the repository at this point in the history
  • Loading branch information
emma-sg committed Sep 4, 2024
1 parent 85759ae commit e1005eb
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 80 deletions.
222 changes: 161 additions & 61 deletions src/archivedb.ts

Large diffs are not rendered by default.

23 changes: 15 additions & 8 deletions src/fuzzymatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type FuzzyRule = {

type FuzzyResEntry = {
url: string;
status: number;
status?: number | undefined;
fuzzyMatchUrl?: string;
};

Expand Down Expand Up @@ -179,7 +179,10 @@ export class FuzzyMatcher {

for (const testRule of this.rules) {
// very large URLs likely do not match any of our existing rules, so just skip
if (matchUrl.length < MAX_ARG_LEN * 4 && matchUrl.match(testRule.match)) {
if (
matchUrl.length < MAX_ARG_LEN * 4 &&
matchUrl.match(testRule.match!)
) {
rule = testRule;
break;
}
Expand All @@ -188,7 +191,7 @@ export class FuzzyMatcher {
let fuzzyCanonUrl = reqUrl;

if (rule?.fuzzyCanonReplace) {
fuzzyCanonUrl = reqUrl.replace(rule.match, rule.fuzzyCanonReplace);
fuzzyCanonUrl = reqUrl.replace(rule.match!, rule.fuzzyCanonReplace);
}

const split = rule?.split || "?";
Expand All @@ -201,6 +204,7 @@ export class FuzzyMatcher {
}

getFuzzyCanonsWithArgs(reqUrl: string) {
// eslint-disable-next-line prefer-const
let { fuzzyCanonUrl, prefix, rule } = this.getRuleFor(reqUrl);

if (fuzzyCanonUrl === reqUrl) {
Expand Down Expand Up @@ -280,7 +284,7 @@ export class FuzzyMatcher {

try {
reqUrl = new URL(reqUrlStr);
} catch (e) {
} catch (_e) {
return null;
}

Expand All @@ -302,7 +306,7 @@ export class FuzzyMatcher {

try {
url = new URL(result.fuzzyMatchUrl || result.url);
} catch (e) {
} catch (_e) {
continue;
}

Expand All @@ -312,8 +316,8 @@ export class FuzzyMatcher {
total /= 2.0;

// lower total score for status >200
if (result.status > 200) {
total *= 10 ** ((200 - result.status) * 0.0001);
if (result.status! > 200) {
total *= 10 ** ((200 - result.status!) * 0.0001);
}

//console.log('total: ' + total + ' ' + url.href + ' <=> ' + reqUrl);
Expand Down Expand Up @@ -341,6 +345,7 @@ export class FuzzyMatcher {

const keySets: KeySets = {};

// eslint-disable-next-line prefer-const
for (let [key, value] of reqQuery) {
let foundValue = foundQuery.get(key);

Expand Down Expand Up @@ -380,6 +385,7 @@ export class FuzzyMatcher {

if (foundValue === value) {
score += weight * value.length;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
} else if (foundValue === null || value === null) {
score += 0.0;
} else if (!isNaN(numValue) && !isNaN(numFoundValue)) {
Expand All @@ -390,7 +396,7 @@ export class FuzzyMatcher {
const fQ = jsonToQueryParams(foundValue);

score += this.getMatch(rQ, fQ) * weight * 2;
} catch (e) {
} catch (_e) {
score += 0.5 * weight * this.levScore(value, foundValue);
}
} else {
Expand Down Expand Up @@ -439,6 +445,7 @@ export class FuzzyMatcher {
const valueNoQ = valueQ > 0 ? value.slice(0, valueQ) : value;
const foundNoQ = foundQ > 0 ? foundValue.slice(0, foundQ) : foundValue;

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!keySets[keyBase]) {
keySets[keyBase] = { value: [], found: new Set() };
}
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export type PageEntry = {

export type DigestRefCount = {
digest: string;
count: number;
count: number | undefined;
size: number;
};

Expand All @@ -64,7 +64,7 @@ export interface DBStore {
getResource: (
request: ArchiveRequest,
prefix: string,
event?: FetchEvent,
event: FetchEvent,
opts?: Record<string, any>,
) => Promise<ArchiveResponse | Response | null>;

Expand All @@ -75,6 +75,6 @@ export interface ArchiveLoader {
load: (
db: DBStore,
progressUpdateCallback?: any,
totalLength?: number,
totalLength?: number | undefined,
) => Promise<void>;
}
6 changes: 5 additions & 1 deletion src/warcloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,11 @@ class WARCLoader extends BaseParser {
return null;
}

async load(db: any, progressUpdate: any, totalSize: number) {
async load(db: any, progressUpdate: any, totalSize?: number | undefined) {
// TODO @ikreymer is this reasonable? unsure how this is used
if (totalSize == null) {
throw new Error("totalSize is required");
}
this.db = db;

const parser = new WARCParser(this.reader);
Expand Down
4 changes: 4 additions & 0 deletions src/wombat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ declare module "*.txt" {
const content: any;
export default content;
}

interface FetchEvent {
replacesClientId?: string;
}
4 changes: 2 additions & 2 deletions test/rewriteCSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const rewriteCSS = test.macro({
t,
content: string,
expected: string,
encoding = "utf8",
expectedContentType = "text/css",
encoding: string | undefined = "utf8",
expectedContentType: string | undefined = "text/css",
) {
const opts = {
content,
Expand Down
10 changes: 6 additions & 4 deletions test/testDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { tsToDate } from "../src/utils.js";
import { ArchiveDB } from "../src/archivedb.js";

import crypto from "node:crypto";
import { type DigestRefCount } from "../src/types.js";

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!global.crypto) {
(global as any).crypto = crypto;
}
Expand All @@ -24,7 +26,7 @@ if (!global.crypto) {

const db = new ArchiveDB("db", { minDedupSize: 0 });

function ts(timestamp) {
function ts(timestamp: string) {
return tsToDate(timestamp).getTime();
}

Expand Down Expand Up @@ -175,10 +177,10 @@ test("Search by pageId", async (t) => {
});

test("Delete with ref counts", async (t) => {
const toDict = (results) => {
const obj = {};
const toDict = (results: (DigestRefCount | null)[]) => {
const obj: Record<string, number | undefined> = {};
for (const res of results) {
obj[res.digest] = res.count;
obj[res!.digest] = res!.count;
}
return obj;
};
Expand Down
2 changes: 1 addition & 1 deletion test/testZip.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//import { fetch } from "node:fetch";

import anyTest, { type TestFn, ExecutionContext } from "ava";
import anyTest, { type TestFn } from "ava";
import http from "http";

import listen from "test-listen";
Expand Down

0 comments on commit e1005eb

Please sign in to comment.