From 25e6e690afcb673a47c1ecc3db6ce2075bb2bbd2 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Tue, 11 Feb 2025 13:53:36 -0800 Subject: [PATCH] dynamic pages query: support pages 'q' and 'size' params for querying pages dynamically bump version to 2.21.0 --- package.json | 2 +- src/api.ts | 10 ++++++++++ src/wacz/multiwacz.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 9005d9e..86dcd2f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@webrecorder/wabac", - "version": "2.20.9.1", + "version": "2.21.0-beta.0", "main": "index.js", "type": "module", "exports": { diff --git a/src/api.ts b/src/api.ts index d70db5a..53db323 100644 --- a/src/api.ts +++ b/src/api.ts @@ -297,6 +297,16 @@ class API { if (!coll) { return { error: "collection_not_found" }; } + if (coll.store instanceof MultiWACZ) { + // @ts-expect-error [TODO] - TS4111 - Property '_query' comes from an index signature, so it must be accessed with ['_query']. + const q = params._query.get("q"); + // @ts-expect-error [TODO] - TS4111 - Property '_query' comes from an index signature, so it must be accessed with ['_query']. + const limit = Number(params._query.get("limit")) || 25; + if (q) { + const pages = await coll.store.queryPages(q, limit); + return { pages }; + } + } const pages = await coll.store.getAllPages(); return { pages }; } diff --git a/src/wacz/multiwacz.ts b/src/wacz/multiwacz.ts index 3be251d..a847b7b 100644 --- a/src/wacz/multiwacz.ts +++ b/src/wacz/multiwacz.ts @@ -1240,6 +1240,40 @@ export class MultiWACZ } } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + async queryPages(urlPrefix: string, limit = 25) : Promise[]> { + const params = new URLSearchParams(); + params.set("urlPrefix", urlPrefix); + params.set("pageSize", limit + ""); + const res = await fetch(this.pagesQuery + "?" + params.toString(), { + headers: this.sourceLoader?.headers, + }); + if (res.status !== 200) { + return []; + } + const json = await res.json(); + if (!json) { + return []; + } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const pages = json.items.map((x: any) => { + x.wacz = x.filename; + const file = this.waczfiles[x.filename]; + if (file) { + x.waczhash = file.hash; + } + if (typeof(x.ts) === "string") { + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + x.ts = new Date(x.ts).getTime(); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return x; + }); + + // eslint-disable-next-line @typescript-eslint/no-unsafe-return + return pages; + } + async getWACZFilesForPagesQuery(requestUrl: string) { const params = new URLSearchParams(); const url = new URL(requestUrl);