@nitpicker/report-google-sheets 0.19.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/data/create-discrepancies.d.ts +10 -12
- package/lib/data/create-discrepancies.js +27 -31
- package/lib/data/create-image-list.d.ts +26 -6
- package/lib/data/create-image-list.js +65 -39
- package/lib/data/create-links.d.ts +18 -1
- package/lib/data/create-links.js +91 -55
- package/lib/data/create-page-list.d.ts +24 -24
- package/lib/data/create-page-list.js +177 -225
- package/lib/data/create-referrers-relational-table.d.ts +13 -7
- package/lib/data/create-referrers-relational-table.js +57 -47
- package/lib/data/create-resources-relational-table.d.ts +7 -0
- package/lib/data/create-resources-relational-table.js +41 -33
- package/lib/data/create-resources.d.ts +24 -124
- package/lib/data/create-resources.js +100 -264
- package/lib/data/create-violations.d.ts +7 -6
- package/lib/data/create-violations.js +34 -19
- package/lib/dedupe-progress-message.d.ts +17 -0
- package/lib/dedupe-progress-message.js +26 -0
- package/lib/format-progress-count.d.ts +19 -0
- package/lib/format-progress-count.js +22 -0
- package/lib/open-report-archive.d.ts +54 -0
- package/lib/open-report-archive.js +76 -0
- package/lib/report.d.ts +16 -12
- package/lib/report.js +101 -94
- package/lib/reports/get-plugin-reports.d.ts +14 -6
- package/lib/reports/get-plugin-reports.js +14 -34
- package/lib/sheets/create-sheets.d.ts +66 -36
- package/lib/sheets/create-sheets.js +228 -362
- package/lib/sheets/estimate-cell-budget.d.ts +55 -0
- package/lib/sheets/estimate-cell-budget.js +56 -0
- package/lib/sheets/types.d.ts +111 -91
- package/lib/test-helpers/assert-no-lazy-cells.d.ts +16 -0
- package/lib/test-helpers/assert-no-lazy-cells.js +23 -0
- package/lib/test-helpers/cell-inspection.d.ts +20 -0
- package/lib/test-helpers/cell-inspection.js +21 -0
- package/lib/test-helpers/create-mock-sheet.d.ts +30 -0
- package/lib/test-helpers/create-mock-sheet.js +71 -0
- package/lib/test-helpers/one-chunk.d.ts +8 -0
- package/lib/test-helpers/one-chunk.js +11 -0
- package/lib/utils/join-urls-for-note.d.ts +27 -0
- package/lib/utils/join-urls-for-note.js +48 -0
- package/lib/utils/truncate-note-text.d.ts +15 -0
- package/lib/utils/truncate-note-text.js +23 -0
- package/package.json +8 -10
- package/lib/archive.d.ts +0 -29
- package/lib/archive.js +0 -49
- package/lib/sheets/run-finalize-resources.d.ts +0 -40
- package/lib/sheets/run-finalize-resources.js +0 -44
- package/lib/types.d.ts +0 -11
- package/lib/types.js +0 -1
- package/lib/utils/canonicalize-url.d.ts +0 -53
- package/lib/utils/canonicalize-url.js +0 -105
- package/lib/utils/has-prop-filter.d.ts +0 -18
- package/lib/utils/has-prop-filter.js +0 -19
- package/lib/utils/sort-resources-by-url.d.ts +0 -69
- package/lib/utils/sort-resources-by-url.js +0 -223
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { NOTE_MAX_LENGTH } from './join-urls-for-note.js';
|
|
2
|
+
/**
|
|
3
|
+
* Truncates an arbitrary, already-built note string to `maxLength`,
|
|
4
|
+
* appending a truncation marker when truncation actually happens. Sibling
|
|
5
|
+
* to {@link joinUrlsForNote} for note content that isn't a list of URLs (a
|
|
6
|
+
* page title, a JSON blob, a plugin-supplied free-text note) — both share
|
|
7
|
+
* the same {@link NOTE_MAX_LENGTH} ceiling so every note this package
|
|
8
|
+
* writes stays under `Cell.provide()`'s hard cut (see `NOTE_MAX_LENGTH`'s
|
|
9
|
+
* docs).
|
|
10
|
+
* @param text - The full note text.
|
|
11
|
+
* @param maxLength - Optional override (defaults to {@link NOTE_MAX_LENGTH}).
|
|
12
|
+
* @example
|
|
13
|
+
* truncateNoteText('a'.repeat(10_000), 100);
|
|
14
|
+
* // 'aaaa...aaaa\n\n...(truncated: showing first 100 of 10000 characters)'
|
|
15
|
+
*/
|
|
16
|
+
export function truncateNoteText(text, maxLength = NOTE_MAX_LENGTH) {
|
|
17
|
+
if (text.length <= maxLength) {
|
|
18
|
+
return text;
|
|
19
|
+
}
|
|
20
|
+
const suffix = `\n\n...(truncated: showing first ${maxLength} of ${text.length} characters)`;
|
|
21
|
+
const keepLength = Math.max(0, maxLength - suffix.length);
|
|
22
|
+
return text.slice(0, keepLength) + suffix;
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nitpicker/report-google-sheets",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Nitpicker reporter that outputs crawl and analysis results to Google Sheets",
|
|
5
5
|
"author": "D-ZERO",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -30,23 +30,21 @@
|
|
|
30
30
|
"clean": "tsc --build --clean"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@d-zero/dealer": "1.
|
|
33
|
+
"@d-zero/dealer": "1.13.1",
|
|
34
34
|
"@d-zero/google-auth": "0.7.6",
|
|
35
35
|
"@d-zero/google-sheets": "0.10.0",
|
|
36
36
|
"@d-zero/shared": "0.23.0",
|
|
37
|
-
"@nitpicker/crawler": "0.
|
|
38
|
-
"@nitpicker/query": "0.
|
|
39
|
-
"@nitpicker/types": "0.
|
|
37
|
+
"@nitpicker/crawler": "0.21.0",
|
|
38
|
+
"@nitpicker/query": "0.21.0",
|
|
39
|
+
"@nitpicker/types": "0.21.0",
|
|
40
40
|
"ansi-colors": "4.1.3",
|
|
41
41
|
"debug": "4.4.3",
|
|
42
|
-
"enquirer": "2.4.1"
|
|
43
|
-
"jsdom": "29.1.1"
|
|
42
|
+
"enquirer": "2.4.1"
|
|
44
43
|
},
|
|
45
44
|
"devDependencies": {
|
|
46
45
|
"@types/debug": "4.1.13",
|
|
47
|
-
"@types/jsdom": "28.0.3",
|
|
48
46
|
"googleapis": "173.0.0",
|
|
49
|
-
"vitest": "4.1.
|
|
47
|
+
"vitest": "4.1.11"
|
|
50
48
|
},
|
|
51
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "233d081009aeeeb2d2622536ca29b22693ba65ca"
|
|
52
50
|
}
|
package/lib/archive.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Archive } from '@nitpicker/crawler';
|
|
2
|
-
/**
|
|
3
|
-
* Result of opening an archive with signal handlers registered.
|
|
4
|
-
*
|
|
5
|
-
* Implements `Symbol.asyncDispose` so callers can obtain one via
|
|
6
|
-
* `await using` instead of remembering to call both
|
|
7
|
-
* `removeSignalHandlers()` and `archive.close()` by hand.
|
|
8
|
-
*/
|
|
9
|
-
export interface ArchiveHandle extends AsyncDisposable {
|
|
10
|
-
/** The opened archive instance. */
|
|
11
|
-
readonly archive: Archive;
|
|
12
|
-
/** Removes the registered signal handlers. Call this when the archive is no longer in use. */
|
|
13
|
-
readonly removeSignalHandlers: () => void;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Opens a `.nitpicker` archive file and registers process signal handlers
|
|
17
|
-
* to ensure the archive is closed gracefully on unexpected termination.
|
|
18
|
-
*
|
|
19
|
-
* The returned handle's `Symbol.asyncDispose` removes the signal handlers
|
|
20
|
-
* and closes the archive together — prefer `await using` over calling
|
|
21
|
-
* `removeSignalHandlers()` / `archive.close()` separately.
|
|
22
|
-
* @param filePath - Path to the `.nitpicker` archive file
|
|
23
|
-
* @param onExtractProgress - Forwarded to `Archive.open` — called during the
|
|
24
|
-
* untar step with bytes read so far and the archive's total size (issue
|
|
25
|
-
* #294: a large archive's extraction can take tens of seconds with no
|
|
26
|
-
* other signal it isn't hung).
|
|
27
|
-
* @returns An {@link ArchiveHandle} containing the archive and a cleanup function.
|
|
28
|
-
*/
|
|
29
|
-
export declare function getArchive(filePath: string, onExtractProgress?: (readBytes: number, totalBytes: number) => void): Promise<ArchiveHandle>;
|
package/lib/archive.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { Archive } from '@nitpicker/crawler';
|
|
2
|
-
import { archiveLog } from './debug.js';
|
|
3
|
-
/**
|
|
4
|
-
* Opens a `.nitpicker` archive file and registers process signal handlers
|
|
5
|
-
* to ensure the archive is closed gracefully on unexpected termination.
|
|
6
|
-
*
|
|
7
|
-
* The returned handle's `Symbol.asyncDispose` removes the signal handlers
|
|
8
|
-
* and closes the archive together — prefer `await using` over calling
|
|
9
|
-
* `removeSignalHandlers()` / `archive.close()` separately.
|
|
10
|
-
* @param filePath - Path to the `.nitpicker` archive file
|
|
11
|
-
* @param onExtractProgress - Forwarded to `Archive.open` — called during the
|
|
12
|
-
* untar step with bytes read so far and the archive's total size (issue
|
|
13
|
-
* #294: a large archive's extraction can take tens of seconds with no
|
|
14
|
-
* other signal it isn't hung).
|
|
15
|
-
* @returns An {@link ArchiveHandle} containing the archive and a cleanup function.
|
|
16
|
-
*/
|
|
17
|
-
export async function getArchive(filePath, onExtractProgress) {
|
|
18
|
-
archiveLog('Open file: %s', filePath);
|
|
19
|
-
const archive = await Archive.open({
|
|
20
|
-
filePath,
|
|
21
|
-
openPluginData: true,
|
|
22
|
-
onExtractProgress,
|
|
23
|
-
});
|
|
24
|
-
archiveLog('File open succeeded');
|
|
25
|
-
const signals = ['SIGINT', 'SIGBREAK', 'SIGHUP', 'SIGABRT'];
|
|
26
|
-
const close = async () => {
|
|
27
|
-
await archive.close();
|
|
28
|
-
process.exit();
|
|
29
|
-
};
|
|
30
|
-
archiveLog('Bind close method to SIGINT, SIGBREAK, SIGHUP, SIGABRT events');
|
|
31
|
-
for (const signal of signals) {
|
|
32
|
-
process.on(signal, close);
|
|
33
|
-
}
|
|
34
|
-
const removeSignalHandlers = () => {
|
|
35
|
-
for (const signal of signals) {
|
|
36
|
-
process.removeListener(signal, close);
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
return {
|
|
40
|
-
archive,
|
|
41
|
-
removeSignalHandlers,
|
|
42
|
-
async [Symbol.asyncDispose]() {
|
|
43
|
-
archiveLog('Closes file');
|
|
44
|
-
removeSignalHandlers();
|
|
45
|
-
await archive.close();
|
|
46
|
-
archiveLog('Closed');
|
|
47
|
-
},
|
|
48
|
-
};
|
|
49
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import type { CreateSheetSetting } from './types.js';
|
|
2
|
-
import type { Lanes } from '@d-zero/dealer';
|
|
3
|
-
import type { Sheet } from '@d-zero/google-sheets';
|
|
4
|
-
/**
|
|
5
|
-
* Parameters for {@link runFinalizeResources}.
|
|
6
|
-
*/
|
|
7
|
-
export interface RunFinalizeResourcesParams {
|
|
8
|
-
/** Sheet setting that may declare a `finalizeResources` hook. */
|
|
9
|
-
readonly setting: CreateSheetSetting;
|
|
10
|
-
/** Google Sheets wrapper that buffers and sends rows. */
|
|
11
|
-
readonly sheet: Sheet;
|
|
12
|
-
/** Optional Lanes instance for terminal progress display. */
|
|
13
|
-
readonly lanes?: Lanes;
|
|
14
|
-
/** Lane ID assigned to this sheet. */
|
|
15
|
-
readonly laneId: number;
|
|
16
|
-
/** Display name of the sheet (used in progress messages). */
|
|
17
|
-
readonly sheetName: string;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Calls the `finalizeResources` hook (Phase 3 terminator) and streams the
|
|
21
|
-
* aggregated rows through `sheet.appendRow(...rows)`, subscribing to
|
|
22
|
-
* `sheet.onProgress` so chunk-flush progress is mirrored to Lanes.
|
|
23
|
-
*
|
|
24
|
-
* - No-op when the setting has no `finalizeResources` hook.
|
|
25
|
-
* - No-op when the hook returns `null` / empty array.
|
|
26
|
-
* - Always resets `sheet.onProgress` in a `finally` block, even when
|
|
27
|
-
* `appendRow` throws — preventing a stale handler from leaking into
|
|
28
|
-
* a different sheet's lane.
|
|
29
|
-
*
|
|
30
|
-
* Known limitation (V8 argument cap): the aggregated rows are spread into
|
|
31
|
-
* `sheet.appendRow(...finalRows)`, so an array longer than V8's practical
|
|
32
|
-
* argument limit (~65k) throws `RangeError: Maximum call stack size
|
|
33
|
-
* exceeded`. Verified working at 1.6M raw resources → 63k aggregated rows.
|
|
34
|
-
* If aggregates ever exceed ~60k, switch to chunked calls (e.g. 10k per
|
|
35
|
-
* `appendRow`) — the Sheet's internal 2500-row buffer is call-count
|
|
36
|
-
* agnostic, so outer chunking preserves ordering and total send count.
|
|
37
|
-
* The spec pins behaviour at 100 rows to stay clear of the V8 limit.
|
|
38
|
-
* @param params - Hook invocation context (see {@link RunFinalizeResourcesParams}).
|
|
39
|
-
*/
|
|
40
|
-
export declare function runFinalizeResources(params: RunFinalizeResourcesParams): Promise<void>;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { sheetLog } from '../debug.js';
|
|
2
|
-
/**
|
|
3
|
-
* Calls the `finalizeResources` hook (Phase 3 terminator) and streams the
|
|
4
|
-
* aggregated rows through `sheet.appendRow(...rows)`, subscribing to
|
|
5
|
-
* `sheet.onProgress` so chunk-flush progress is mirrored to Lanes.
|
|
6
|
-
*
|
|
7
|
-
* - No-op when the setting has no `finalizeResources` hook.
|
|
8
|
-
* - No-op when the hook returns `null` / empty array.
|
|
9
|
-
* - Always resets `sheet.onProgress` in a `finally` block, even when
|
|
10
|
-
* `appendRow` throws — preventing a stale handler from leaking into
|
|
11
|
-
* a different sheet's lane.
|
|
12
|
-
*
|
|
13
|
-
* Known limitation (V8 argument cap): the aggregated rows are spread into
|
|
14
|
-
* `sheet.appendRow(...finalRows)`, so an array longer than V8's practical
|
|
15
|
-
* argument limit (~65k) throws `RangeError: Maximum call stack size
|
|
16
|
-
* exceeded`. Verified working at 1.6M raw resources → 63k aggregated rows.
|
|
17
|
-
* If aggregates ever exceed ~60k, switch to chunked calls (e.g. 10k per
|
|
18
|
-
* `appendRow`) — the Sheet's internal 2500-row buffer is call-count
|
|
19
|
-
* agnostic, so outer chunking preserves ordering and total send count.
|
|
20
|
-
* The spec pins behaviour at 100 rows to stay clear of the V8 limit.
|
|
21
|
-
* @param params - Hook invocation context (see {@link RunFinalizeResourcesParams}).
|
|
22
|
-
*/
|
|
23
|
-
export async function runFinalizeResources(params) {
|
|
24
|
-
const { setting, sheet, lanes, laneId, sheetName } = params;
|
|
25
|
-
if (!setting.finalizeResources)
|
|
26
|
-
return;
|
|
27
|
-
lanes?.update(laneId, `${sheetName}: Finalizing aggregated rows%dots%`);
|
|
28
|
-
sheetLog('[%s] finalizeResources start', sheetName);
|
|
29
|
-
const finalRows = await setting.finalizeResources();
|
|
30
|
-
if (!finalRows || finalRows.length === 0)
|
|
31
|
-
return;
|
|
32
|
-
const total = finalRows.length;
|
|
33
|
-
lanes?.update(laneId, `${sheetName}: Sending 0/${total} aggregated rows%dots%`);
|
|
34
|
-
sheet.onProgress = (sent) => {
|
|
35
|
-
lanes?.update(laneId, `${sheetName}: Sending ${sent}/${total} aggregated rows%dots%`);
|
|
36
|
-
};
|
|
37
|
-
try {
|
|
38
|
-
await sheet.appendRow(...finalRows);
|
|
39
|
-
}
|
|
40
|
-
finally {
|
|
41
|
-
sheet.onProgress = undefined;
|
|
42
|
-
}
|
|
43
|
-
sheetLog('[%s] finalizeResources emitted %d rows', sheetName, total);
|
|
44
|
-
}
|
package/lib/types.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Makes the specified properties of `T` required while keeping
|
|
3
|
-
* the rest unchanged. Used by `hasPropFilter` to narrow types
|
|
4
|
-
* where an optional callback property is known to be defined.
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* type WithEachPage = RequiredProp<CreateSheetSetting, 'eachPage'>;
|
|
8
|
-
* // eachPage is now non-optional
|
|
9
|
-
* ```
|
|
10
|
-
*/
|
|
11
|
-
export type RequiredProp<T, P extends keyof T> = T & Required<Pick<T, P>>;
|
package/lib/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Canonicalize a resource URL by stripping query parameter *values*
|
|
3
|
-
* while preserving every other part of the URL.
|
|
4
|
-
*
|
|
5
|
-
* - Scheme / host / port / path are returned verbatim.
|
|
6
|
-
* - The query string is rewritten to a sorted, deduplicated list of
|
|
7
|
-
* parameter keys joined by `&`, with all values discarded.
|
|
8
|
-
* - URLs without a query string are returned unchanged.
|
|
9
|
-
*
|
|
10
|
-
* This is intentionally simpler than {@link URL}/`URLSearchParams`: it
|
|
11
|
-
* neither percent-decodes nor reorders anything outside the query
|
|
12
|
-
* string, so two raw URLs that differ only in per-request query values
|
|
13
|
-
* collapse to the same canonical form without changing path-embedded
|
|
14
|
-
* identifiers (e.g. tracking IDs in `/pagead/viewthroughconversion/<id>/`).
|
|
15
|
-
* @param url - The raw resource URL as stored in the archive. The
|
|
16
|
-
* archive stores URLs in `withoutHash` form (no `#fragment`); if
|
|
17
|
-
* you pass a URL with a fragment, the fragment will leak into the
|
|
18
|
-
* last value but the canonical key list is unaffected.
|
|
19
|
-
* @returns The canonicalized URL.
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* canonicalizeUrl('https://x.com/p/123?b=Y&a=X&a=Z');
|
|
23
|
-
* // → 'https://x.com/p/123?a&b'
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
export declare function canonicalizeUrl(url: string): string;
|
|
27
|
-
/**
|
|
28
|
-
* Parses the query string of a URL and returns its `(key, value)` pairs
|
|
29
|
-
* in source order. Keys are kept as written (no percent-decoding, no
|
|
30
|
-
* sort, no dedupe) so callers can distinguish e.g. `a=1&a=2` as two
|
|
31
|
-
* separate pairs. Values are the raw substring after the first `=`;
|
|
32
|
-
* pairs with no `=` get an empty-string value, and pairs with no key
|
|
33
|
-
* are skipped.
|
|
34
|
-
*
|
|
35
|
-
* Returns an empty array for URLs without a query string or with an
|
|
36
|
-
* empty query string.
|
|
37
|
-
*
|
|
38
|
-
* **Fragment caveat:** This function does not strip `#fragment` from
|
|
39
|
-
* the URL. The archive's `resources.url` column already stores URLs
|
|
40
|
-
* in `withoutHash` form so this is normally a no-op, but callers
|
|
41
|
-
* passing in arbitrary URLs should strip the fragment first if they
|
|
42
|
-
* care about clean values.
|
|
43
|
-
* @param url - The raw resource URL as stored in the archive.
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* extractQueryPairs('https://x.com/p?a=1&b=2&a=3');
|
|
47
|
-
* // → [{ key: 'a', value: '1' }, { key: 'b', value: '2' }, { key: 'a', value: '3' }]
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export declare function extractQueryPairs(url: string): Array<{
|
|
51
|
-
readonly key: string;
|
|
52
|
-
readonly value: string;
|
|
53
|
-
}>;
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Splits a raw query string body (the substring **after** the leading
|
|
3
|
-
* `?`, with no `#fragment` attached) into `(key, value)` pairs in
|
|
4
|
-
* source order. Internal helper shared between {@link canonicalizeUrl}
|
|
5
|
-
* and {@link extractQueryPairs} so the two stay consistent.
|
|
6
|
-
*
|
|
7
|
-
* - No percent-decoding, no sort, no dedupe — callers handle those.
|
|
8
|
-
* - Pairs with an empty key are skipped (matches URL semantics: a
|
|
9
|
-
* leading `&` or `=value` is ignored).
|
|
10
|
-
* - Pairs without `=` get an empty-string value.
|
|
11
|
-
*
|
|
12
|
-
* Returns an empty array for the empty query string.
|
|
13
|
-
* @param query - The query string body without the leading `?`.
|
|
14
|
-
*/
|
|
15
|
-
function parseQueryPairs(query) {
|
|
16
|
-
if (query.length === 0) {
|
|
17
|
-
return [];
|
|
18
|
-
}
|
|
19
|
-
const pairs = [];
|
|
20
|
-
for (const pair of query.split('&')) {
|
|
21
|
-
const eq = pair.indexOf('=');
|
|
22
|
-
const key = eq === -1 ? pair : pair.slice(0, eq);
|
|
23
|
-
if (key.length === 0) {
|
|
24
|
-
continue;
|
|
25
|
-
}
|
|
26
|
-
const value = eq === -1 ? '' : pair.slice(eq + 1);
|
|
27
|
-
pairs.push({ key, value });
|
|
28
|
-
}
|
|
29
|
-
return pairs;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Canonicalize a resource URL by stripping query parameter *values*
|
|
33
|
-
* while preserving every other part of the URL.
|
|
34
|
-
*
|
|
35
|
-
* - Scheme / host / port / path are returned verbatim.
|
|
36
|
-
* - The query string is rewritten to a sorted, deduplicated list of
|
|
37
|
-
* parameter keys joined by `&`, with all values discarded.
|
|
38
|
-
* - URLs without a query string are returned unchanged.
|
|
39
|
-
*
|
|
40
|
-
* This is intentionally simpler than {@link URL}/`URLSearchParams`: it
|
|
41
|
-
* neither percent-decodes nor reorders anything outside the query
|
|
42
|
-
* string, so two raw URLs that differ only in per-request query values
|
|
43
|
-
* collapse to the same canonical form without changing path-embedded
|
|
44
|
-
* identifiers (e.g. tracking IDs in `/pagead/viewthroughconversion/<id>/`).
|
|
45
|
-
* @param url - The raw resource URL as stored in the archive. The
|
|
46
|
-
* archive stores URLs in `withoutHash` form (no `#fragment`); if
|
|
47
|
-
* you pass a URL with a fragment, the fragment will leak into the
|
|
48
|
-
* last value but the canonical key list is unaffected.
|
|
49
|
-
* @returns The canonicalized URL.
|
|
50
|
-
* @example
|
|
51
|
-
* ```ts
|
|
52
|
-
* canonicalizeUrl('https://x.com/p/123?b=Y&a=X&a=Z');
|
|
53
|
-
* // → 'https://x.com/p/123?a&b'
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
export function canonicalizeUrl(url) {
|
|
57
|
-
const qIndex = url.indexOf('?');
|
|
58
|
-
if (qIndex === -1) {
|
|
59
|
-
return url;
|
|
60
|
-
}
|
|
61
|
-
const head = url.slice(0, qIndex);
|
|
62
|
-
const query = url.slice(qIndex + 1);
|
|
63
|
-
if (query.length === 0) {
|
|
64
|
-
return head + '?';
|
|
65
|
-
}
|
|
66
|
-
const pairs = parseQueryPairs(query);
|
|
67
|
-
const keys = pairs.map((pair) => pair.key).toSorted();
|
|
68
|
-
const unique = [];
|
|
69
|
-
for (let i = 0; i < keys.length; i++) {
|
|
70
|
-
if (i === 0 || keys[i] !== keys[i - 1]) {
|
|
71
|
-
unique.push(keys[i]);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return head + '?' + unique.join('&');
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Parses the query string of a URL and returns its `(key, value)` pairs
|
|
78
|
-
* in source order. Keys are kept as written (no percent-decoding, no
|
|
79
|
-
* sort, no dedupe) so callers can distinguish e.g. `a=1&a=2` as two
|
|
80
|
-
* separate pairs. Values are the raw substring after the first `=`;
|
|
81
|
-
* pairs with no `=` get an empty-string value, and pairs with no key
|
|
82
|
-
* are skipped.
|
|
83
|
-
*
|
|
84
|
-
* Returns an empty array for URLs without a query string or with an
|
|
85
|
-
* empty query string.
|
|
86
|
-
*
|
|
87
|
-
* **Fragment caveat:** This function does not strip `#fragment` from
|
|
88
|
-
* the URL. The archive's `resources.url` column already stores URLs
|
|
89
|
-
* in `withoutHash` form so this is normally a no-op, but callers
|
|
90
|
-
* passing in arbitrary URLs should strip the fragment first if they
|
|
91
|
-
* care about clean values.
|
|
92
|
-
* @param url - The raw resource URL as stored in the archive.
|
|
93
|
-
* @example
|
|
94
|
-
* ```ts
|
|
95
|
-
* extractQueryPairs('https://x.com/p?a=1&b=2&a=3');
|
|
96
|
-
* // → [{ key: 'a', value: '1' }, { key: 'b', value: '2' }, { key: 'a', value: '3' }]
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
|
-
export function extractQueryPairs(url) {
|
|
100
|
-
const qIndex = url.indexOf('?');
|
|
101
|
-
if (qIndex === -1) {
|
|
102
|
-
return [];
|
|
103
|
-
}
|
|
104
|
-
return parseQueryPairs(url.slice(qIndex + 1));
|
|
105
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import type { RequiredProp } from '../types.js';
|
|
2
|
-
/**
|
|
3
|
-
* Returns a type guard that checks whether a specific property
|
|
4
|
-
* is defined (truthy) on an object. The returned guard narrows
|
|
5
|
-
* the type to `RequiredProp<T, P>`, making the property non-optional.
|
|
6
|
-
*
|
|
7
|
-
* Used to split `CreateSheetSetting[]` into subsets that definitely
|
|
8
|
-
* have a particular callback (e.g. `eachPage`, `eachResource`),
|
|
9
|
-
* eliminating the need for null-checks in the processing loops.
|
|
10
|
-
* @param prop - The property name to check.
|
|
11
|
-
* @returns A type-narrowing predicate function.
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* const withEachPage = settings.filter(hasPropFilter('eachPage'));
|
|
15
|
-
* // withEachPage[0].eachPage is now non-optional
|
|
16
|
-
* ```
|
|
17
|
-
*/
|
|
18
|
-
export declare function hasPropFilter<T, P extends keyof T>(prop: P): (object: T) => object is RequiredProp<T, P>;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Returns a type guard that checks whether a specific property
|
|
3
|
-
* is defined (truthy) on an object. The returned guard narrows
|
|
4
|
-
* the type to `RequiredProp<T, P>`, making the property non-optional.
|
|
5
|
-
*
|
|
6
|
-
* Used to split `CreateSheetSetting[]` into subsets that definitely
|
|
7
|
-
* have a particular callback (e.g. `eachPage`, `eachResource`),
|
|
8
|
-
* eliminating the need for null-checks in the processing loops.
|
|
9
|
-
* @param prop - The property name to check.
|
|
10
|
-
* @returns A type-narrowing predicate function.
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* const withEachPage = settings.filter(hasPropFilter('eachPage'));
|
|
14
|
-
* // withEachPage[0].eachPage is now non-optional
|
|
15
|
-
* ```
|
|
16
|
-
*/
|
|
17
|
-
export function hasPropFilter(prop) {
|
|
18
|
-
return (object) => !!object[prop];
|
|
19
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Natural-order string comparator, ported from Martin Pool's
|
|
3
|
-
* `strnatcmp.c` (sourcefrog/natsort, originally derived from Stuart
|
|
4
|
-
* Cheshire's 1996 Macintosh natural sort). Faithful to the C
|
|
5
|
-
* reference's dispatch:
|
|
6
|
-
*
|
|
7
|
-
* - Skip leading ASCII whitespace on both sides.
|
|
8
|
-
* - When both sides have a digit run:
|
|
9
|
-
* - If **either** run starts with `'0'`, treat them as fractional
|
|
10
|
-
* and use {@link compareLeft} (left-aligned digit-by-digit).
|
|
11
|
-
* - Otherwise use {@link compareRight} (length-first, with bias for
|
|
12
|
-
* ties).
|
|
13
|
-
* - End-of-string on both sides means equal.
|
|
14
|
-
* - Otherwise fold ASCII letters to lower case and compare one
|
|
15
|
-
* UTF-16 code unit.
|
|
16
|
-
*
|
|
17
|
-
* Allocates no intermediate strings: only `charCodeAt` and integer
|
|
18
|
-
* arithmetic. That makes the function safe to invoke O(N log N)
|
|
19
|
-
* times during a 1M+ element sort without inflating the V8 heap with
|
|
20
|
-
* derived `SeqString` allocations.
|
|
21
|
-
*
|
|
22
|
-
* **Benchmark reference (Node 24, M-class CPU):** 100K-entry sort
|
|
23
|
-
* completes in ~650 ms with median heap delta ratio
|
|
24
|
-
* `heapDelta(100K) / heapDelta(10K) ≈ 5–15×`. If either measurement
|
|
25
|
-
* jumps by an order of magnitude (multi-second elapsed, ratio > 100),
|
|
26
|
-
* suspect a regression that reintroduced per-compare string
|
|
27
|
-
* allocation or `Intl.Collator` dispatch.
|
|
28
|
-
*
|
|
29
|
-
* **UTF-16 semantics:** index moves one code unit per step. ASCII
|
|
30
|
-
* URLs (the only kind the archive currently stores) are entirely
|
|
31
|
-
* BMP, so this is equivalent to per-character iteration. URLs that
|
|
32
|
-
* contain surrogate pairs (e.g. raw emoji in path segments) compare
|
|
33
|
-
* as UTF-16 code unit sequences, which is deterministic but not
|
|
34
|
-
* equivalent to Unicode code-point order. That matches `strnatcmp.c`
|
|
35
|
-
* — it also operates on bytes, not Unicode scalars.
|
|
36
|
-
* @param a - First URL.
|
|
37
|
-
* @param b - Second URL.
|
|
38
|
-
* @returns Negative if `a < b`, positive if `a > b`, zero if equal.
|
|
39
|
-
*/
|
|
40
|
-
export declare function naturalCompare(a: string, b: string): number;
|
|
41
|
-
/**
|
|
42
|
-
* Sorts archive resources by their URL in natural order, returning a
|
|
43
|
-
* new array (the input is left untouched). Uses
|
|
44
|
-
* {@link naturalCompare} as the comparator, so embedded integers
|
|
45
|
-
* compare by value (`image-2.jpg` < `image-10.jpg`) and ASCII case
|
|
46
|
-
* is folded.
|
|
47
|
-
*
|
|
48
|
-
* The sort is stable (V8 TimSort). Each comparison allocates no
|
|
49
|
-
* intermediate strings, so the only extra memory is V8's TimSort
|
|
50
|
-
* auxiliary buffer — O(N) pointer-sized entries, not O(N × URL length).
|
|
51
|
-
* @param resources - The list of resources to sort. Only the `url`
|
|
52
|
-
* field is read. A `null` url (a blob-routed resource whose identity
|
|
53
|
-
* is a large `data:` URI — see `create-entity-tables.ts`'s
|
|
54
|
-
* `resource_items url / blob mutual-exclusion CHECK`) sorts as if it
|
|
55
|
-
* were the empty string, placing it first.
|
|
56
|
-
* @returns A new array sorted by URL in natural order.
|
|
57
|
-
* @example
|
|
58
|
-
* ```ts
|
|
59
|
-
* sortResourcesByUrl([
|
|
60
|
-
* { url: 'https://x/img-10.jpg' },
|
|
61
|
-
* { url: 'https://x/img-2.jpg' },
|
|
62
|
-
* { url: 'https://x/img-1.jpg' },
|
|
63
|
-
* ]);
|
|
64
|
-
* // → [{ url: 'https://x/img-1.jpg' }, { url: 'https://x/img-2.jpg' }, { url: 'https://x/img-10.jpg' }]
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export declare function sortResourcesByUrl<T extends {
|
|
68
|
-
readonly url: string | null;
|
|
69
|
-
}>(resources: readonly T[]): T[];
|