Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/content/2.providers/01.wayback.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Or import `WaybackProvider` from `@agntn/archives` and construct it yourself.

`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.

`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.
`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.

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

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

- 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.
- 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.
- `collapse: "timestamp:4"` is a default, not a fact about the archive. Drop it when you count captures.
- `collapse: "timestamp:4"` is a default, not a fact about the archive. Counting captures? Pass `collapse: "none"` and get them all.
- `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.

## Where it lives
Expand Down
2 changes: 2 additions & 0 deletions src/_providers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ArchiveOptions } from "./types.ts";

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

export interface ArchiveItOptions extends ArchiveOptions {
collection: number | string;
/** CDX `collapse`, off unless set; `none` keeps it off. */
collapse?: string;
filter?: string;
}
Expand Down
3 changes: 2 additions & 1 deletion src/providers/archive-it.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
normalizeDomain,
preferSameUrl,
readPlaybackCapture,
collapseParam,
resolveRequestedTimestamp,
selectCapture,
timestampUpperBound,
Expand Down Expand Up @@ -68,9 +69,9 @@ export class ArchiveItProvider extends BaseProvider<ArchiveItOptions> {
url: urlPattern,
fl: "original,timestamp,statuscode",
limit: String(options.limit ?? 1000),
...collapseParam(options.collapse),
};

if (options.collapse !== undefined) params.collapse = options.collapse;
if (options.filter !== undefined) params.filter = options.filter;
const from = resolveRequestedTimestamp(options.from, "from");
const to = resolveRequestedTimestamp(options.to, "to");
Expand Down
3 changes: 2 additions & 1 deletion src/providers/wayback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
preferSameUrl,
readPlaybackCapture,
requestedWindow,
collapseParam,
resolveRequestedTimestamp,
selectCapture,
unwrapSnapshotUrl,
Expand Down Expand Up @@ -50,7 +51,7 @@ function waybackQuery(domain: string, options: Readonly<WaybackOptions>): Record
url: listingPattern(domain, options.order),
output: "json",
fl: "original,timestamp,statuscode,digest,mimetype,length,urlkey",
collapse: options.collapse ?? "timestamp:4",
...collapseParam(options.collapse, "timestamp:4"),
};
if (options.filter !== undefined) params.filter = options.filter;
return { ...params, ...requestedWindow(options) };
Expand Down
3 changes: 2 additions & 1 deletion src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ export const snapshotsTool = defineTool({
user,
collapse: Type.Optional(
Type.String({
description: "Wayback CDX collapse parameter, for example timestamp:4.",
description:
"Wayback CDX collapse parameter. Wayback defaults to timestamp:4; none lists every capture.",
minLength: 1,
maxLength: MAX_PARAMETER_LENGTH,
}),
Expand Down
14 changes: 14 additions & 0 deletions src/utils/_cdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ export function requestedWindow(
return result;
}

/**
* The CDX `collapse` parameter, or nothing for `none`.
* @param collapse - The caller's value
* @param fallback - What applies when the caller says nothing
* @returns {Record<string, string>} The parameter, or nothing
*/
export function collapseParam(
collapse: string | undefined,
fallback?: string,
): Record<string, string> {
const value = collapse ?? fallback;
return value === undefined || value === "none" ? {} : { collapse: value };
}

/**
* Reads one CDX field. pywb writes "-" for one it doesn't know, and that's no value.
* @param value - The field as parsed from the row
Expand Down
11 changes: 11 additions & 0 deletions test/archive-it.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ describe("Archive-It", () => {
);
});

it("sends no collapse for none", async () => {
vi.mocked(fetchData).mockResolvedValueOnce("");

await createArchive(createArchiveIt({ collection: "4399", collapse: "none" })).snapshots(
"example.com",
);

const [, request] = vi.mocked(fetchData).mock.calls[0] ?? [];
expect(request?.params).not.toHaveProperty("collapse");
});

it("returns an empty success response for an empty CDX result", async () => {
vi.mocked(fetchData).mockResolvedValueOnce("");

Expand Down
13 changes: 13 additions & 0 deletions test/wayback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ describe("wayback machine", () => {
);
});

it("leaves collapse out of the CDX query when it is none", async () => {
vi.mocked(fetchData).mockResolvedValueOnce([["original", "timestamp", "statuscode"]]);

const result = await createArchive(createWayback()).snapshots("example.com", {
collapse: "none",
});

expect(result.success).toBe(true);
const [, request] = vi.mocked(fetchData).mock.calls[0] ?? [];
expect(request?.params).not.toHaveProperty("collapse");
expect(result._meta?.queryParams).not.toHaveProperty("collapse");
});

it("narrows the CDX query to the requested window", async () => {
vi.mocked(fetchData).mockResolvedValueOnce([["original", "timestamp", "statuscode"]]);

Expand Down
Loading