Skip to content

Commit

Permalink
feat: add cursor to utils (#22)
Browse files Browse the repository at this point in the history
* feat: add cursor support

* clean up
  • Loading branch information
metreniuk authored Apr 23, 2024
1 parent 143fd08 commit 64cc392
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "@blowfishxyz/blocklist",
"version": "0.0.7",
"version": "0.0.8",
"description": "Fetch and execute lookups on Blowfish blocklists",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type DomainBlocklist = {
bloomFilter: { url: string; hash: string };
recentlyAdded: string[];
recentlyRemoved: string[];
nextCursor: string;
};

export type BloomFilter = {
Expand Down
2 changes: 2 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export async function fetchDomainBlocklist(
},
priorityBlockLists: string[] | null = null,
priorityAllowLists: string[] | null = null,
cursor: string | null = null,
reportError: ErrorCallback | undefined = undefined
): Promise<DomainBlocklist | null> {
const headers = {
Expand All @@ -49,6 +50,7 @@ export async function fetchDomainBlocklist(
body: JSON.stringify({
priorityBlockLists,
priorityAllowLists,
cursor,
}),
...apiKeyConfig,
});
Expand Down
41 changes: 38 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const EMPTY_BLOOM_FILTER: BloomFilter = {
};

describe("fetchDomainBlocklist", () => {
it("should return a not-null blocklist fetched from API with required fields", async () => {
it("should return a non-null blocklist fetched from API with required fields", async () => {
const apiConfig = {
domainBlocklistUrl: DEFAULT_BLOCKLIST_URL,
apiKey: process.env.BLOWFISH_API_KEY,
Expand All @@ -32,6 +32,29 @@ describe("fetchDomainBlocklist", () => {
expect(blocklist!.bloomFilter.hash).not.toBe("");
});

it("should return a cursor that can be used to re-fetch the blocklist", async () => {
const apiConfig = {
domainBlocklistUrl: DEFAULT_BLOCKLIST_URL,
apiKey: process.env.BLOWFISH_API_KEY,
};
const blocklist = await fetchDomainBlocklist(apiConfig);
expect(blocklist).not.toBeNull();
expect(blocklist).toHaveProperty("bloomFilter");
expect(blocklist).toHaveProperty("recentlyAdded");
expect(blocklist).toHaveProperty("recentlyRemoved");

const { nextCursor } = blocklist!;
const nextBlocklist = await fetchDomainBlocklist(
apiConfig,
undefined,
undefined,
nextCursor
);
expect(nextBlocklist).not.toBeNull();
expect(nextBlocklist!.recentlyAdded.length).toEqual(0);
expect(nextBlocklist!.recentlyRemoved.length).toEqual(0);
});

it("tracks thrown errors using a passed function", async () => {
// eslint-disable-next-line prefer-const
let errors: unknown[] = [];
Expand All @@ -41,7 +64,13 @@ describe("fetchDomainBlocklist", () => {
const apiConfig = {
domainBlocklistUrl: "http://2CeaMJtzCTdx8ht2.com/", // this domain does not exist
};
await fetchDomainBlocklist(apiConfig, undefined, undefined, reportError);
await fetchDomainBlocklist(
apiConfig,
undefined,
undefined,
undefined,
reportError
);
expect(errors.length).toBe(1);
expect((errors[0] as Error).message).toBe(
"request to http://2ceamjtzctdx8ht2.com/ failed, reason: getaddrinfo ENOTFOUND 2ceamjtzctdx8ht2.com"
Expand All @@ -57,7 +86,13 @@ describe("fetchDomainBlocklist", () => {
const apiConfig = {
domainBlocklistUrl: "https://google.com/fdjfkdkdkfdkdf/", // this should return 404
};
await fetchDomainBlocklist(apiConfig, undefined, undefined, reportError);
await fetchDomainBlocklist(
apiConfig,
undefined,
undefined,
undefined,
reportError
);
expect(errors.length).toBe(1);
expect(errors[0] as string).toContain("Error 404 (Not Found)");
});
Expand Down

0 comments on commit 64cc392

Please sign in to comment.