Skip to content

Commit 368ba1c

Browse files
authored
fix(wayback): collapse none (#243)
1 parent 01131da commit 368ba1c

8 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎docs/content/2.providers/01.wayback.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Or import `WaybackProvider` from `@agntn/archives` and construct it yourself.
2323

2424
`snapshots("example.com")` asks `web.archive.org/cdx/search/cdx` for `example.com/*`, so a domain lists everything under it. A full URL is looked up as is. Rows come back as `original`, `timestamp`, `statuscode`, `digest`, `mimetype` and `length`. `_meta` keeps the raw stamp of fourteen digits, the status, and `digest`, `mime` and `length` whenever the index knows them. The digest is the base32 SHA-1 of the archived body, so `filter: "digest:<value>"` finds every capture of those exact bytes. `length` is the size of the compressed WARC record, not of the page. The tool prints all four under each capture.
2525

26-
`collapse` defaults to `timestamp:4`, one capture per year, which keeps a domain with millions of captures answerable. Pass `collapse: "timestamp:8"` for one per day, or `collapse: "digest"` for one per distinct body. `filter` is passed through untouched (`statuscode:200`, `mimetype:text/html`, `!statuscode:3..`). Reads take it too, so `getContent` picks from the filtered rows.
26+
`collapse` defaults to `timestamp:4`, one capture per year, which keeps a domain with millions of captures answerable. Pass `collapse: "timestamp:8"` for one per day, or `collapse: "digest"` for one per distinct body. Pass `collapse: "none"` to get every capture. `filter` is passed through untouched (`statuscode:200`, `mimetype:text/html`, `!statuscode:3..`). Reads take it too, so `getContent` picks from the filtered rows.
2727

2828
`from` and `to` narrow the CDX query itself, so a window is cheap here.
2929

@@ -40,7 +40,7 @@ Bodies are read under the `id_` modifier, which replays the original response wi
4040

4141
- The CDX index answers oldest first. A merged `providers.all()` listing sorted newest first can cut every Wayback row when other archives hold newer captures. Ask Wayback alone for early history. Want this year's captures of one page instead? `order: "newest"` turns `limit` negative, and CDX sends the last rows.
4242
- CDX queries for busy domains take seconds, sometimes close to a minute. That's why Wayback waits 60 seconds unless you name a `timeout` yourself, and why caching pays off here.
43-
- `collapse: "timestamp:4"` is a default, not a fact about the archive. Drop it when you count captures.
43+
- `collapse: "timestamp:4"` is a default, not a fact about the archive. Counting captures? Pass `collapse: "none"` and get them all.
4444
- `collapse` applies across pages too. A finished listing with the default isn't a complete raw history. The cursor bookmarks the collapsed result, not every capture hiding behind it.
4545

4646
## Where it lives

‎src/_providers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ArchiveOptions } from "./types.ts";
22

33
export interface WaybackOptions extends ArchiveOptions {
4+
/** CDX `collapse`, `timestamp:4` unless set; `none` turns it off. */
45
collapse?: string;
56
filter?: string;
67
}
@@ -17,6 +18,7 @@ export type NlnzOptions = ArchiveOptions;
1718

1819
export interface ArchiveItOptions extends ArchiveOptions {
1920
collection: number | string;
21+
/** CDX `collapse`, off unless set; `none` keeps it off. */
2022
collapse?: string;
2123
filter?: string;
2224
}

‎src/providers/archive-it.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
normalizeDomain,
1212
preferSameUrl,
1313
readPlaybackCapture,
14+
collapseParam,
1415
resolveRequestedTimestamp,
1516
selectCapture,
1617
timestampUpperBound,
@@ -68,9 +69,9 @@ export class ArchiveItProvider extends BaseProvider<ArchiveItOptions> {
6869
url: urlPattern,
6970
fl: "original,timestamp,statuscode",
7071
limit: String(options.limit ?? 1000),
72+
...collapseParam(options.collapse),
7173
};
7274

73-
if (options.collapse !== undefined) params.collapse = options.collapse;
7475
if (options.filter !== undefined) params.filter = options.filter;
7576
const from = resolveRequestedTimestamp(options.from, "from");
7677
const to = resolveRequestedTimestamp(options.to, "to");

‎src/providers/wayback.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
preferSameUrl,
1818
readPlaybackCapture,
1919
requestedWindow,
20+
collapseParam,
2021
resolveRequestedTimestamp,
2122
selectCapture,
2223
unwrapSnapshotUrl,
@@ -50,7 +51,7 @@ function waybackQuery(domain: string, options: Readonly<WaybackOptions>): Record
5051
url: listingPattern(domain, options.order),
5152
output: "json",
5253
fl: "original,timestamp,statuscode,digest,mimetype,length,urlkey",
53-
collapse: options.collapse ?? "timestamp:4",
54+
...collapseParam(options.collapse, "timestamp:4"),
5455
};
5556
if (options.filter !== undefined) params.filter = options.filter;
5657
return { ...params, ...requestedWindow(options) };

‎src/tools.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ export const snapshotsTool = defineTool({
156156
user,
157157
collapse: Type.Optional(
158158
Type.String({
159-
description: "Wayback CDX collapse parameter, for example timestamp:4.",
159+
description:
160+
"Wayback CDX collapse parameter. Wayback defaults to timestamp:4; none lists every capture.",
160161
minLength: 1,
161162
maxLength: MAX_PARAMETER_LENGTH,
162163
}),

‎src/utils/_cdx.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ export function requestedWindow(
2626
return result;
2727
}
2828

29+
/**
30+
* The CDX `collapse` parameter, or nothing for `none`.
31+
* @param collapse - The caller's value
32+
* @param fallback - What applies when the caller says nothing
33+
* @returns {Record<string, string>} The parameter, or nothing
34+
*/
35+
export function collapseParam(
36+
collapse: string | undefined,
37+
fallback?: string,
38+
): Record<string, string> {
39+
const value = collapse ?? fallback;
40+
return value === undefined || value === "none" ? {} : { collapse: value };
41+
}
42+
2943
/**
3044
* Reads one CDX field. pywb writes "-" for one it doesn't know, and that's no value.
3145
* @param value - The field as parsed from the row

‎test/archive-it.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ describe("Archive-It", () => {
117117
);
118118
});
119119

120+
it("sends no collapse for none", async () => {
121+
vi.mocked(fetchData).mockResolvedValueOnce("");
122+
123+
await createArchive(createArchiveIt({ collection: "4399", collapse: "none" })).snapshots(
124+
"example.com",
125+
);
126+
127+
const [, request] = vi.mocked(fetchData).mock.calls[0] ?? [];
128+
expect(request?.params).not.toHaveProperty("collapse");
129+
});
130+
120131
it("returns an empty success response for an empty CDX result", async () => {
121132
vi.mocked(fetchData).mockResolvedValueOnce("");
122133

‎test/wayback.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ describe("wayback machine", () => {
149149
);
150150
});
151151

152+
it("leaves collapse out of the CDX query when it is none", async () => {
153+
vi.mocked(fetchData).mockResolvedValueOnce([["original", "timestamp", "statuscode"]]);
154+
155+
const result = await createArchive(createWayback()).snapshots("example.com", {
156+
collapse: "none",
157+
});
158+
159+
expect(result.success).toBe(true);
160+
const [, request] = vi.mocked(fetchData).mock.calls[0] ?? [];
161+
expect(request?.params).not.toHaveProperty("collapse");
162+
expect(result._meta?.queryParams).not.toHaveProperty("collapse");
163+
});
164+
152165
it("narrows the CDX query to the requested window", async () => {
153166
vi.mocked(fetchData).mockResolvedValueOnce([["original", "timestamp", "statuscode"]]);
154167

0 commit comments

Comments
 (0)