gscdump 4.4.0 → 4.5.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/README.md CHANGED
@@ -92,6 +92,41 @@ See [URL inspection and indexing](../../docs/gscdump-cli/guides/6.url-indexing.m
92
92
 
93
93
  The package root also exports batch and projection helpers such as `fetchSitesWithSitemaps`, `batchInspectUrlsFlatSettled`, `inspectUrlFlat`, and `batchRequestIndexing`.
94
94
 
95
+ ## Send IndexNow change notifications
96
+
97
+ Publish a UTF-8 key file on the exact host you submit.
98
+ The key file contains your key, with no other content.
99
+
100
+ ```ts
101
+ import { indexNow } from 'gscdump/indexnow'
102
+
103
+ const client = indexNow()
104
+ const input = {
105
+ host: 'example.com',
106
+ key: 'your-key-12345',
107
+ keyLocation: 'https://example.com/your-key-12345.txt',
108
+ }
109
+ const verification = await client.verify(input)
110
+ if (verification.ok && verification.value._tag === 'verified') {
111
+ const result = await client.submit({
112
+ ...input,
113
+ urls: ['https://example.com/changed-page'],
114
+ })
115
+ console.log(result)
116
+ }
117
+ ```
118
+
119
+ The client rejects URLs outside the exact host or key directory before sending.
120
+ Verification allows HTTPS, disables redirects, and reads at most 1,024 bytes within ten seconds.
121
+ Inject `fetch`, `clock`, or `timeoutMs` when you need different transport or timing.
122
+ Expected input failures return `Result`; infrastructure failures propagate.
123
+
124
+ Submission Receipts describe delivery. HTTP 200 means accepted; HTTP 202 means key validation remains pending.
125
+ Neither response proves indexing.
126
+ The direct client sends one batch of up to 10,000 URLs without retries.
127
+ Hosted callers use the SDK operations, which cap each batch at 1,000 URLs.
128
+ See the [IndexNow protocol](https://www.indexnow.org/documentation).
129
+
95
130
  ## Read Bing Indexing Evidence
96
131
 
97
132
  Use `gscdump/bing` with an OAuth access token or Bing Webmaster API key. The client returns tagged
@@ -129,6 +164,7 @@ sitemap membership is available through `@gscdump/sdk/v1`.
129
164
 
130
165
  - `gscdump/client`: Google client and authentication types
131
166
  - `gscdump/indexing`: inspection and indexing helpers
167
+ - `gscdump/indexnow`: key verification and change notifications
132
168
  - `gscdump/errors`: typed Google API failures
133
169
  - `gscdump/sites`: Site helpers
134
170
  - `gscdump/sitemap-identity`: sitemap scope and membership hashing
@@ -0,0 +1,53 @@
1
+ import { Result } from "./core/result.mjs";
2
+ export interface IndexNowInput {
3
+ host: string;
4
+ key: string;
5
+ keyLocation: string;
6
+ }
7
+ export type IndexNowDelivery = {
8
+ _tag: 'accepted';
9
+ httpStatus: 200;
10
+ reason: null;
11
+ retryAfterMs: null;
12
+ } | {
13
+ _tag: 'pending';
14
+ httpStatus: 202;
15
+ reason: 'key-validation-pending';
16
+ retryAfterMs: null;
17
+ } | {
18
+ _tag: 'rejected';
19
+ httpStatus: 400 | 403 | 422;
20
+ reason: string;
21
+ retryAfterMs: null;
22
+ } | {
23
+ _tag: 'retrying';
24
+ httpStatus: number;
25
+ reason: string;
26
+ retryAfterMs: number | null;
27
+ } | {
28
+ _tag: 'failed';
29
+ httpStatus: number;
30
+ reason: string;
31
+ retryAfterMs: null;
32
+ };
33
+ export interface IndexNowInvalidRequest {
34
+ _tag: 'InvalidRequest';
35
+ reason: string;
36
+ }
37
+ export type IndexNowVerification = {
38
+ _tag: 'verified';
39
+ } | {
40
+ _tag: 'verification-required';
41
+ reason: string;
42
+ };
43
+ export interface IndexNowClient {
44
+ verify: (input: IndexNowInput) => Promise<Result<IndexNowVerification, IndexNowInvalidRequest>>;
45
+ submit: (input: IndexNowInput & {
46
+ urls: string[];
47
+ }) => Promise<Result<IndexNowDelivery, IndexNowInvalidRequest>>;
48
+ }
49
+ export declare function indexNow(options?: {
50
+ fetch?: typeof globalThis.fetch;
51
+ clock?: () => Date;
52
+ timeoutMs?: number;
53
+ }): IndexNowClient;
@@ -0,0 +1,129 @@
1
+ import { err, ok } from "./core/result.mjs";
2
+ function parseUrl(value) {
3
+ return URL.canParse(value) ? new URL(value) : null;
4
+ }
5
+ function parseInput(input) {
6
+ const location = parseUrl(input.keyLocation);
7
+ if (!/^[a-z0-9-]{8,128}$/i.test(input.key)) return err({
8
+ _tag: "InvalidRequest",
9
+ reason: "invalid-key"
10
+ });
11
+ if (!location || location.protocol !== "https:" || location.host !== input.host || location.username || location.password || location.hash || location.search || location.port) return err({
12
+ _tag: "InvalidRequest",
13
+ reason: "invalid-key-location"
14
+ });
15
+ return ok(location);
16
+ }
17
+ function indexNow(options = {}) {
18
+ const fetch = options.fetch ?? globalThis.fetch;
19
+ const timeoutMs = options.timeoutMs ?? 1e4;
20
+ const clock = options.clock ?? (() => /* @__PURE__ */ new Date());
21
+ return {
22
+ async verify(input) {
23
+ const parsed = parseInput(input);
24
+ if (!parsed.ok) return parsed;
25
+ const response = await fetch(input.keyLocation, {
26
+ redirect: "error",
27
+ signal: AbortSignal.timeout(timeoutMs)
28
+ });
29
+ if (response.status !== 200) return ok({
30
+ _tag: "verification-required",
31
+ reason: "key-file-unavailable"
32
+ });
33
+ const reader = response.body?.getReader();
34
+ if (!reader) return ok({
35
+ _tag: "verification-required",
36
+ reason: "key-file-empty"
37
+ });
38
+ const chunks = [];
39
+ let length = 0;
40
+ for (;;) {
41
+ const next = await reader.read();
42
+ if (next.done) break;
43
+ length += next.value.length;
44
+ if (length > 1024) {
45
+ await reader.cancel();
46
+ return ok({
47
+ _tag: "verification-required",
48
+ reason: "key-file-too-large"
49
+ });
50
+ }
51
+ chunks.push(next.value);
52
+ }
53
+ const bytes = new Uint8Array(length);
54
+ let offset = 0;
55
+ for (const chunk of chunks) {
56
+ bytes.set(chunk, offset);
57
+ offset += chunk.length;
58
+ }
59
+ return ok(new TextDecoder().decode(bytes).trim() === input.key ? { _tag: "verified" } : {
60
+ _tag: "verification-required",
61
+ reason: "key-file-mismatch"
62
+ });
63
+ },
64
+ async submit(input) {
65
+ const parsed = parseInput(input);
66
+ if (!parsed.ok) return parsed;
67
+ if (!input.urls.length || input.urls.length > 1e4) return err({
68
+ _tag: "InvalidRequest",
69
+ reason: "invalid-url-count"
70
+ });
71
+ const directory = parsed.value.pathname.slice(0, parsed.value.pathname.lastIndexOf("/") + 1);
72
+ for (const value of input.urls) {
73
+ const url = parseUrl(value);
74
+ if (!url || !["https:", "http:"].includes(url.protocol) || url.host !== input.host || url.username || url.password || url.port || url.hash || !url.pathname.startsWith(directory)) return err({
75
+ _tag: "InvalidRequest",
76
+ reason: "url-outside-key-scope"
77
+ });
78
+ }
79
+ const response = await fetch("https://api.indexnow.org/indexnow", {
80
+ method: "POST",
81
+ redirect: "error",
82
+ signal: AbortSignal.timeout(timeoutMs),
83
+ headers: { "content-type": "application/json; charset=utf-8" },
84
+ body: JSON.stringify({
85
+ host: input.host,
86
+ key: input.key,
87
+ keyLocation: input.keyLocation,
88
+ urlList: input.urls
89
+ })
90
+ });
91
+ if (response.status === 200) return ok({
92
+ _tag: "accepted",
93
+ httpStatus: 200,
94
+ reason: null,
95
+ retryAfterMs: null
96
+ });
97
+ if (response.status === 202) return ok({
98
+ _tag: "pending",
99
+ httpStatus: 202,
100
+ reason: "key-validation-pending",
101
+ retryAfterMs: null
102
+ });
103
+ if (response.status === 400 || response.status === 403 || response.status === 422) return ok({
104
+ _tag: "rejected",
105
+ httpStatus: response.status,
106
+ reason: response.status === 403 ? "invalid-key" : "invalid-request",
107
+ retryAfterMs: null
108
+ });
109
+ if (response.status === 429 || response.status >= 500) {
110
+ const retry = response.headers.get("retry-after");
111
+ const seconds = retry === null ? NaN : Number(retry);
112
+ const delay = Number.isFinite(seconds) && seconds >= 0 ? seconds * 1e3 : retry === null ? NaN : Date.parse(retry) - clock().getTime();
113
+ return ok({
114
+ _tag: "retrying",
115
+ httpStatus: response.status,
116
+ reason: response.status === 429 ? "rate-limited" : "service-unavailable",
117
+ retryAfterMs: Number.isFinite(delay) ? Math.max(0, delay) : null
118
+ });
119
+ }
120
+ return ok({
121
+ _tag: "failed",
122
+ httpStatus: response.status,
123
+ reason: "unexpected-response",
124
+ retryAfterMs: null
125
+ });
126
+ }
127
+ };
128
+ }
129
+ export { indexNow };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "gscdump",
3
3
  "type": "module",
4
- "version": "4.4.0",
4
+ "version": "4.5.0",
5
5
  "description": "Direct Google Search Console and Bing Webmaster clients with typed queries and Indexing Evidence",
6
6
  "author": {
7
7
  "name": "Harlan Wilton",
@@ -102,6 +102,11 @@
102
102
  "types": "./dist/tenant.d.mts",
103
103
  "import": "./dist/tenant.mjs",
104
104
  "default": "./dist/tenant.mjs"
105
+ },
106
+ "./indexnow": {
107
+ "types": "./dist/indexnow.d.mts",
108
+ "import": "./dist/indexnow.mjs",
109
+ "default": "./dist/indexnow.mjs"
105
110
  }
106
111
  },
107
112
  "main": "./dist/index.mjs",
@@ -113,7 +118,7 @@
113
118
  "node": ">=22.13.0"
114
119
  },
115
120
  "dependencies": {
116
- "@gscdump/contracts": "^4.4.0",
121
+ "@gscdump/contracts": "^4.5.0",
117
122
  "ofetch": "^1.5.1"
118
123
  },
119
124
  "scripts": {