@rankcli/agent-runtime 0.0.1

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.
Files changed (178) hide show
  1. package/README.md +242 -0
  2. package/dist/analyzer-2CSWIQGD.mjs +6 -0
  3. package/dist/chunk-YNZYHEYM.mjs +774 -0
  4. package/dist/index.d.mts +4012 -0
  5. package/dist/index.d.ts +4012 -0
  6. package/dist/index.js +29672 -0
  7. package/dist/index.mjs +28602 -0
  8. package/package.json +53 -0
  9. package/scripts/build-deno.ts +134 -0
  10. package/src/audit/ai/analyzer.ts +347 -0
  11. package/src/audit/ai/index.ts +29 -0
  12. package/src/audit/ai/prompts/content-analysis.ts +271 -0
  13. package/src/audit/ai/types.ts +179 -0
  14. package/src/audit/checks/additional-checks.ts +439 -0
  15. package/src/audit/checks/ai-citation-worthiness.ts +399 -0
  16. package/src/audit/checks/ai-content-structure.ts +325 -0
  17. package/src/audit/checks/ai-readiness.ts +339 -0
  18. package/src/audit/checks/anchor-text.ts +179 -0
  19. package/src/audit/checks/answer-conciseness.ts +322 -0
  20. package/src/audit/checks/asset-minification.ts +270 -0
  21. package/src/audit/checks/bing-optimization.ts +206 -0
  22. package/src/audit/checks/brand-mention-optimization.ts +349 -0
  23. package/src/audit/checks/caching-headers.ts +305 -0
  24. package/src/audit/checks/canonical-advanced.ts +150 -0
  25. package/src/audit/checks/canonical-domain.ts +196 -0
  26. package/src/audit/checks/citation-quality.ts +358 -0
  27. package/src/audit/checks/client-rendering.ts +542 -0
  28. package/src/audit/checks/color-contrast.ts +342 -0
  29. package/src/audit/checks/content-freshness.ts +170 -0
  30. package/src/audit/checks/content-science.ts +589 -0
  31. package/src/audit/checks/conversion-elements.ts +526 -0
  32. package/src/audit/checks/crawlability.ts +220 -0
  33. package/src/audit/checks/directory-listing.ts +172 -0
  34. package/src/audit/checks/dom-analysis.ts +191 -0
  35. package/src/audit/checks/dom-size.ts +246 -0
  36. package/src/audit/checks/duplicate-content.ts +194 -0
  37. package/src/audit/checks/eeat-signals.ts +990 -0
  38. package/src/audit/checks/entity-seo.ts +396 -0
  39. package/src/audit/checks/featured-snippet.ts +473 -0
  40. package/src/audit/checks/freshness-signals.ts +443 -0
  41. package/src/audit/checks/funnel-intent.ts +463 -0
  42. package/src/audit/checks/hreflang.ts +174 -0
  43. package/src/audit/checks/html-compliance.ts +302 -0
  44. package/src/audit/checks/image-dimensions.ts +167 -0
  45. package/src/audit/checks/images.ts +160 -0
  46. package/src/audit/checks/indexnow.ts +275 -0
  47. package/src/audit/checks/interactive-tools.ts +475 -0
  48. package/src/audit/checks/internal-link-graph.ts +436 -0
  49. package/src/audit/checks/keyword-analysis.ts +239 -0
  50. package/src/audit/checks/keyword-cannibalization.ts +385 -0
  51. package/src/audit/checks/keyword-placement.ts +471 -0
  52. package/src/audit/checks/links.ts +203 -0
  53. package/src/audit/checks/llms-txt.ts +224 -0
  54. package/src/audit/checks/local-seo.ts +296 -0
  55. package/src/audit/checks/mobile.ts +167 -0
  56. package/src/audit/checks/modern-images.ts +226 -0
  57. package/src/audit/checks/navboost-signals.ts +395 -0
  58. package/src/audit/checks/on-page.ts +209 -0
  59. package/src/audit/checks/page-resources.ts +285 -0
  60. package/src/audit/checks/pagination.ts +180 -0
  61. package/src/audit/checks/performance.ts +153 -0
  62. package/src/audit/checks/platform-presence.ts +580 -0
  63. package/src/audit/checks/redirect-analysis.ts +153 -0
  64. package/src/audit/checks/redirect-chain.ts +389 -0
  65. package/src/audit/checks/resource-hints.ts +420 -0
  66. package/src/audit/checks/responsive-css.ts +247 -0
  67. package/src/audit/checks/responsive-images.ts +396 -0
  68. package/src/audit/checks/review-ecosystem.ts +415 -0
  69. package/src/audit/checks/robots-validation.ts +373 -0
  70. package/src/audit/checks/security-headers.ts +172 -0
  71. package/src/audit/checks/security.ts +144 -0
  72. package/src/audit/checks/serp-preview.ts +251 -0
  73. package/src/audit/checks/site-maturity.ts +444 -0
  74. package/src/audit/checks/social-meta.test.ts +275 -0
  75. package/src/audit/checks/social-meta.ts +134 -0
  76. package/src/audit/checks/soft-404.ts +151 -0
  77. package/src/audit/checks/structured-data.ts +238 -0
  78. package/src/audit/checks/tech-detection.ts +496 -0
  79. package/src/audit/checks/topical-clusters.ts +435 -0
  80. package/src/audit/checks/tracker-bloat.ts +462 -0
  81. package/src/audit/checks/tracking-verification.test.ts +371 -0
  82. package/src/audit/checks/tracking-verification.ts +636 -0
  83. package/src/audit/checks/url-safety.ts +682 -0
  84. package/src/audit/deno-entry.ts +66 -0
  85. package/src/audit/discovery/index.ts +15 -0
  86. package/src/audit/discovery/link-crawler.ts +232 -0
  87. package/src/audit/discovery/repo-routes.ts +347 -0
  88. package/src/audit/engine.ts +620 -0
  89. package/src/audit/fixes/index.ts +209 -0
  90. package/src/audit/fixes/social-meta-fixes.test.ts +329 -0
  91. package/src/audit/fixes/social-meta-fixes.ts +463 -0
  92. package/src/audit/index.ts +74 -0
  93. package/src/audit/runner.test.ts +299 -0
  94. package/src/audit/runner.ts +130 -0
  95. package/src/audit/types.ts +1953 -0
  96. package/src/content/featured-snippet.ts +367 -0
  97. package/src/content/generator.test.ts +534 -0
  98. package/src/content/generator.ts +501 -0
  99. package/src/content/headline.ts +317 -0
  100. package/src/content/index.ts +62 -0
  101. package/src/content/intent.ts +258 -0
  102. package/src/content/keyword-density.ts +349 -0
  103. package/src/content/readability.ts +262 -0
  104. package/src/executor.ts +336 -0
  105. package/src/fixer.ts +416 -0
  106. package/src/frameworks/detector.test.ts +248 -0
  107. package/src/frameworks/detector.ts +371 -0
  108. package/src/frameworks/index.ts +68 -0
  109. package/src/frameworks/recipes/angular.yaml +171 -0
  110. package/src/frameworks/recipes/astro.yaml +206 -0
  111. package/src/frameworks/recipes/django.yaml +180 -0
  112. package/src/frameworks/recipes/laravel.yaml +137 -0
  113. package/src/frameworks/recipes/nextjs.yaml +268 -0
  114. package/src/frameworks/recipes/nuxt.yaml +175 -0
  115. package/src/frameworks/recipes/rails.yaml +188 -0
  116. package/src/frameworks/recipes/react.yaml +202 -0
  117. package/src/frameworks/recipes/sveltekit.yaml +154 -0
  118. package/src/frameworks/recipes/vue.yaml +137 -0
  119. package/src/frameworks/recipes/wordpress.yaml +209 -0
  120. package/src/frameworks/suggestion-engine.ts +320 -0
  121. package/src/geo/geo-content.test.ts +305 -0
  122. package/src/geo/geo-content.ts +266 -0
  123. package/src/geo/geo-history.test.ts +473 -0
  124. package/src/geo/geo-history.ts +433 -0
  125. package/src/geo/geo-tracker.test.ts +359 -0
  126. package/src/geo/geo-tracker.ts +411 -0
  127. package/src/geo/index.ts +10 -0
  128. package/src/git/commit-helper.test.ts +261 -0
  129. package/src/git/commit-helper.ts +329 -0
  130. package/src/git/index.ts +12 -0
  131. package/src/git/pr-helper.test.ts +284 -0
  132. package/src/git/pr-helper.ts +307 -0
  133. package/src/index.ts +66 -0
  134. package/src/keywords/ai-keyword-engine.ts +1062 -0
  135. package/src/keywords/ai-summarizer.ts +387 -0
  136. package/src/keywords/ci-mode.ts +555 -0
  137. package/src/keywords/engine.ts +359 -0
  138. package/src/keywords/index.ts +151 -0
  139. package/src/keywords/llm-judge.ts +357 -0
  140. package/src/keywords/nlp-analysis.ts +706 -0
  141. package/src/keywords/prioritizer.ts +295 -0
  142. package/src/keywords/site-crawler.ts +342 -0
  143. package/src/keywords/sources/autocomplete.ts +139 -0
  144. package/src/keywords/sources/competitive-search.ts +450 -0
  145. package/src/keywords/sources/competitor-analysis.ts +374 -0
  146. package/src/keywords/sources/dataforseo.ts +206 -0
  147. package/src/keywords/sources/free-sources.ts +294 -0
  148. package/src/keywords/sources/gsc.ts +123 -0
  149. package/src/keywords/topic-grouping.ts +327 -0
  150. package/src/keywords/types.ts +144 -0
  151. package/src/keywords/wizard.ts +457 -0
  152. package/src/loader.ts +40 -0
  153. package/src/reports/index.ts +7 -0
  154. package/src/reports/report-generator.test.ts +293 -0
  155. package/src/reports/report-generator.ts +713 -0
  156. package/src/scheduler/alerts.test.ts +458 -0
  157. package/src/scheduler/alerts.ts +328 -0
  158. package/src/scheduler/index.ts +8 -0
  159. package/src/scheduler/scheduled-audit.test.ts +377 -0
  160. package/src/scheduler/scheduled-audit.ts +149 -0
  161. package/src/test/integration-test.ts +325 -0
  162. package/src/tools/analyzer.ts +373 -0
  163. package/src/tools/crawl.ts +293 -0
  164. package/src/tools/files.ts +301 -0
  165. package/src/tools/h1-fixer.ts +249 -0
  166. package/src/tools/index.ts +67 -0
  167. package/src/tracking/github-action.ts +326 -0
  168. package/src/tracking/google-analytics.ts +265 -0
  169. package/src/tracking/index.ts +45 -0
  170. package/src/tracking/report-generator.ts +386 -0
  171. package/src/tracking/search-console.ts +335 -0
  172. package/src/types.ts +134 -0
  173. package/src/utils/http.ts +302 -0
  174. package/src/wasm-adapter.ts +297 -0
  175. package/src/wasm-entry.ts +14 -0
  176. package/tsconfig.json +17 -0
  177. package/tsup.wasm.config.ts +26 -0
  178. package/vitest.config.ts +15 -0
@@ -0,0 +1,774 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
3
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
4
+ }) : x)(function(x) {
5
+ if (typeof require !== "undefined") return require.apply(this, arguments);
6
+ throw Error('Dynamic require of "' + x + '" is not supported');
7
+ });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+
13
+ // src/utils/http.ts
14
+ var DEFAULT_TIMEOUT = 3e4;
15
+ var DEFAULT_USER_AGENT = "RankCLI/1.0 (+https://rankcli.dev)";
16
+ function createTimeoutController(timeout) {
17
+ const controller = new AbortController();
18
+ const timeoutId = setTimeout(() => controller.abort(), timeout);
19
+ return { controller, timeoutId };
20
+ }
21
+ function headersToObject(headers) {
22
+ const obj = {};
23
+ headers.forEach((value, key) => {
24
+ obj[key.toLowerCase()] = value;
25
+ });
26
+ return obj;
27
+ }
28
+ async function httpGet(url, config = {}) {
29
+ const {
30
+ headers = {},
31
+ timeout = DEFAULT_TIMEOUT,
32
+ validateStatus = (status) => status >= 200 && status < 300,
33
+ maxRedirects = 5,
34
+ params
35
+ } = config;
36
+ let finalUrl = url;
37
+ if (params) {
38
+ const searchParams = new URLSearchParams();
39
+ for (const [key, value] of Object.entries(params)) {
40
+ searchParams.append(key, String(value));
41
+ }
42
+ const separator = url.includes("?") ? "&" : "?";
43
+ finalUrl = `${url}${separator}${searchParams.toString()}`;
44
+ }
45
+ const { controller, timeoutId } = createTimeoutController(timeout);
46
+ try {
47
+ const response = await fetch(finalUrl, {
48
+ method: "GET",
49
+ headers: {
50
+ "User-Agent": DEFAULT_USER_AGENT,
51
+ ...headers
52
+ },
53
+ signal: controller.signal,
54
+ redirect: maxRedirects > 0 ? "follow" : "manual"
55
+ });
56
+ clearTimeout(timeoutId);
57
+ const contentType = response.headers.get("content-type") || "";
58
+ let data;
59
+ if (contentType.includes("application/json")) {
60
+ data = await response.json();
61
+ } else {
62
+ data = await response.text();
63
+ }
64
+ if (!validateStatus(response.status)) {
65
+ const error = new Error(`Request failed with status ${response.status}`);
66
+ error.response = {
67
+ data,
68
+ status: response.status,
69
+ statusText: response.statusText,
70
+ headers: headersToObject(response.headers)
71
+ };
72
+ throw error;
73
+ }
74
+ return {
75
+ data,
76
+ status: response.status,
77
+ statusText: response.statusText,
78
+ headers: headersToObject(response.headers)
79
+ };
80
+ } catch (error) {
81
+ clearTimeout(timeoutId);
82
+ if (error instanceof Error && error.name === "AbortError") {
83
+ throw new Error(`Request timeout after ${timeout}ms`);
84
+ }
85
+ throw error;
86
+ }
87
+ }
88
+ async function httpHead(url, config = {}) {
89
+ const {
90
+ headers = {},
91
+ timeout = DEFAULT_TIMEOUT,
92
+ validateStatus = (status) => status >= 200 && status < 300,
93
+ maxRedirects = 5
94
+ } = config;
95
+ const { controller, timeoutId } = createTimeoutController(timeout);
96
+ try {
97
+ const response = await fetch(url, {
98
+ method: "HEAD",
99
+ headers: {
100
+ "User-Agent": DEFAULT_USER_AGENT,
101
+ ...headers
102
+ },
103
+ signal: controller.signal,
104
+ redirect: maxRedirects > 0 ? "follow" : "manual"
105
+ });
106
+ clearTimeout(timeoutId);
107
+ if (!validateStatus(response.status)) {
108
+ const error = new Error(`Request failed with status ${response.status}`);
109
+ error.response = {
110
+ data: null,
111
+ status: response.status,
112
+ statusText: response.statusText,
113
+ headers: headersToObject(response.headers)
114
+ };
115
+ throw error;
116
+ }
117
+ return {
118
+ data: null,
119
+ status: response.status,
120
+ statusText: response.statusText,
121
+ headers: headersToObject(response.headers)
122
+ };
123
+ } catch (error) {
124
+ clearTimeout(timeoutId);
125
+ if (error instanceof Error && error.name === "AbortError") {
126
+ throw new Error(`Request timeout after ${timeout}ms`);
127
+ }
128
+ throw error;
129
+ }
130
+ }
131
+ async function httpPost(url, data, config = {}) {
132
+ const {
133
+ headers = {},
134
+ timeout = DEFAULT_TIMEOUT,
135
+ validateStatus = (status) => status >= 200 && status < 300,
136
+ auth
137
+ } = config;
138
+ const { controller, timeoutId } = createTimeoutController(timeout);
139
+ const requestHeaders = {
140
+ "User-Agent": DEFAULT_USER_AGENT,
141
+ "Content-Type": "application/json",
142
+ ...headers
143
+ };
144
+ if (auth) {
145
+ const credentials = btoa(`${auth.username}:${auth.password}`);
146
+ requestHeaders["Authorization"] = `Basic ${credentials}`;
147
+ }
148
+ try {
149
+ const response = await fetch(url, {
150
+ method: "POST",
151
+ headers: requestHeaders,
152
+ body: data ? JSON.stringify(data) : void 0,
153
+ signal: controller.signal
154
+ });
155
+ clearTimeout(timeoutId);
156
+ const contentType = response.headers.get("content-type") || "";
157
+ let responseData;
158
+ if (contentType.includes("application/json")) {
159
+ responseData = await response.json();
160
+ } else {
161
+ responseData = await response.text();
162
+ }
163
+ if (!validateStatus(response.status)) {
164
+ const error = new Error(`Request failed with status ${response.status}`);
165
+ error.response = {
166
+ data: responseData,
167
+ status: response.status,
168
+ statusText: response.statusText,
169
+ headers: headersToObject(response.headers)
170
+ };
171
+ throw error;
172
+ }
173
+ return {
174
+ data: responseData,
175
+ status: response.status,
176
+ statusText: response.statusText,
177
+ headers: headersToObject(response.headers)
178
+ };
179
+ } catch (error) {
180
+ clearTimeout(timeoutId);
181
+ if (error instanceof Error && error.name === "AbortError") {
182
+ throw new Error(`Request timeout after ${timeout}ms`);
183
+ }
184
+ throw error;
185
+ }
186
+ }
187
+
188
+ // src/tools/crawl.ts
189
+ import * as cheerio from "cheerio";
190
+ async function crawlUrl(params) {
191
+ const { url } = params;
192
+ try {
193
+ const start = Date.now();
194
+ const response = await httpGet(url, {
195
+ timeout: 3e4,
196
+ validateStatus: () => true
197
+ });
198
+ const loadTime = Date.now() - start;
199
+ const result = {
200
+ url,
201
+ html: response.data,
202
+ statusCode: response.status,
203
+ headers: response.headers,
204
+ loadTime
205
+ };
206
+ return { success: true, data: result };
207
+ } catch (error) {
208
+ return {
209
+ success: false,
210
+ error: error instanceof Error ? error.message : "Unknown error crawling URL"
211
+ };
212
+ }
213
+ }
214
+ async function extractMeta(params) {
215
+ const { html, url } = params;
216
+ try {
217
+ const $ = cheerio.load(html);
218
+ const meta = {
219
+ title: $("title").text().trim() || void 0,
220
+ description: $('meta[name="description"]').attr("content")?.trim(),
221
+ canonical: $('link[rel="canonical"]').attr("href"),
222
+ robots: $('meta[name="robots"]').attr("content"),
223
+ viewport: $('meta[name="viewport"]').attr("content"),
224
+ charset: $("meta[charset]").attr("charset") || $('meta[http-equiv="Content-Type"]').attr("content"),
225
+ openGraph: {
226
+ title: $('meta[property="og:title"]').attr("content"),
227
+ description: $('meta[property="og:description"]').attr("content"),
228
+ image: $('meta[property="og:image"]').attr("content"),
229
+ url: $('meta[property="og:url"]').attr("content"),
230
+ type: $('meta[property="og:type"]').attr("content"),
231
+ siteName: $('meta[property="og:site_name"]').attr("content")
232
+ },
233
+ twitter: {
234
+ card: $('meta[name="twitter:card"]').attr("content"),
235
+ title: $('meta[name="twitter:title"]').attr("content"),
236
+ description: $('meta[name="twitter:description"]').attr("content"),
237
+ image: $('meta[name="twitter:image"]').attr("content"),
238
+ site: $('meta[name="twitter:site"]').attr("content")
239
+ },
240
+ other: {}
241
+ };
242
+ $("meta").each((_, el) => {
243
+ const name = $(el).attr("name") || $(el).attr("property");
244
+ const content = $(el).attr("content");
245
+ if (name && content && !name.startsWith("og:") && !name.startsWith("twitter:")) {
246
+ if (!["description", "robots", "viewport"].includes(name)) {
247
+ meta.other[name] = content;
248
+ }
249
+ }
250
+ });
251
+ return { success: true, data: meta };
252
+ } catch (error) {
253
+ return {
254
+ success: false,
255
+ error: error instanceof Error ? error.message : "Error extracting meta"
256
+ };
257
+ }
258
+ }
259
+ async function analyzeHeadings(params) {
260
+ const { html } = params;
261
+ try {
262
+ const $ = cheerio.load(html);
263
+ const headings = [];
264
+ $("h1, h2, h3, h4, h5, h6").each((_, el) => {
265
+ const tag = el.tagName.toLowerCase();
266
+ headings.push({
267
+ tag,
268
+ text: $(el).text().trim(),
269
+ level: parseInt(tag.charAt(1), 10)
270
+ });
271
+ });
272
+ return { success: true, data: headings };
273
+ } catch (error) {
274
+ return {
275
+ success: false,
276
+ error: error instanceof Error ? error.message : "Error analyzing headings"
277
+ };
278
+ }
279
+ }
280
+ async function extractImages(params) {
281
+ const { html } = params;
282
+ try {
283
+ const $ = cheerio.load(html);
284
+ const images = [];
285
+ $("img").each((_, el) => {
286
+ images.push({
287
+ src: $(el).attr("src") || "",
288
+ alt: $(el).attr("alt") || null,
289
+ width: $(el).attr("width"),
290
+ height: $(el).attr("height"),
291
+ loading: $(el).attr("loading")
292
+ });
293
+ });
294
+ return { success: true, data: images };
295
+ } catch (error) {
296
+ return {
297
+ success: false,
298
+ error: error instanceof Error ? error.message : "Error extracting images"
299
+ };
300
+ }
301
+ }
302
+ async function extractLinks(params) {
303
+ const { html, baseUrl } = params;
304
+ try {
305
+ const $ = cheerio.load(html);
306
+ const links = [];
307
+ const baseHostname = new URL(baseUrl).hostname;
308
+ $("a[href]").each((_, el) => {
309
+ const href = $(el).attr("href") || "";
310
+ const rel = $(el).attr("rel") || "";
311
+ let isInternal = false;
312
+ try {
313
+ if (href.startsWith("/") || href.startsWith("#")) {
314
+ isInternal = true;
315
+ } else {
316
+ const linkHostname = new URL(href).hostname;
317
+ isInternal = linkHostname === baseHostname;
318
+ }
319
+ } catch {
320
+ isInternal = true;
321
+ }
322
+ links.push({
323
+ href,
324
+ text: $(el).text().trim(),
325
+ isInternal,
326
+ isNofollow: rel.includes("nofollow")
327
+ });
328
+ });
329
+ return { success: true, data: links };
330
+ } catch (error) {
331
+ return {
332
+ success: false,
333
+ error: error instanceof Error ? error.message : "Error extracting links"
334
+ };
335
+ }
336
+ }
337
+ async function extractSchema(params) {
338
+ const { html } = params;
339
+ try {
340
+ const $ = cheerio.load(html);
341
+ const schemas = [];
342
+ $('script[type="application/ld+json"]').each((_, el) => {
343
+ try {
344
+ const content = $(el).html();
345
+ if (content) {
346
+ const data = JSON.parse(content);
347
+ schemas.push({
348
+ type: data["@type"] || "Unknown",
349
+ data
350
+ });
351
+ }
352
+ } catch {
353
+ }
354
+ });
355
+ return { success: true, data: schemas };
356
+ } catch (error) {
357
+ return {
358
+ success: false,
359
+ error: error instanceof Error ? error.message : "Error extracting schema"
360
+ };
361
+ }
362
+ }
363
+ async function checkRobots(params) {
364
+ try {
365
+ const baseUrl = new URL(params.url);
366
+ const robotsUrl = `${baseUrl.protocol}//${baseUrl.host}/robots.txt`;
367
+ const response = await httpGet(robotsUrl, {
368
+ timeout: 1e4,
369
+ validateStatus: () => true
370
+ });
371
+ if (response.status === 200) {
372
+ return {
373
+ success: true,
374
+ data: {
375
+ exists: true,
376
+ content: response.data,
377
+ url: robotsUrl
378
+ }
379
+ };
380
+ }
381
+ return {
382
+ success: true,
383
+ data: {
384
+ exists: false,
385
+ url: robotsUrl
386
+ }
387
+ };
388
+ } catch (error) {
389
+ return {
390
+ success: false,
391
+ error: error instanceof Error ? error.message : "Error checking robots.txt"
392
+ };
393
+ }
394
+ }
395
+ async function checkSitemap(params) {
396
+ try {
397
+ const baseUrl = new URL(params.url);
398
+ const sitemapUrls = [
399
+ `${baseUrl.protocol}//${baseUrl.host}/sitemap.xml`,
400
+ `${baseUrl.protocol}//${baseUrl.host}/sitemap_index.xml`,
401
+ `${baseUrl.protocol}//${baseUrl.host}/sitemap/sitemap.xml`
402
+ ];
403
+ for (const sitemapUrl of sitemapUrls) {
404
+ try {
405
+ const response = await httpGet(sitemapUrl, {
406
+ timeout: 1e4
407
+ });
408
+ if (response.status === 200 && response.data.includes("<?xml")) {
409
+ const $ = cheerio.load(response.data, { xmlMode: true });
410
+ const urlCount = $("url").length || $("sitemap").length;
411
+ return {
412
+ success: true,
413
+ data: {
414
+ exists: true,
415
+ url: sitemapUrl,
416
+ urlCount,
417
+ content: response.data.substring(0, 2e3)
418
+ // First 2KB
419
+ }
420
+ };
421
+ }
422
+ } catch {
423
+ }
424
+ }
425
+ return {
426
+ success: true,
427
+ data: {
428
+ exists: false
429
+ }
430
+ };
431
+ } catch (error) {
432
+ return {
433
+ success: false,
434
+ error: error instanceof Error ? error.message : "Error checking sitemap"
435
+ };
436
+ }
437
+ }
438
+
439
+ // src/tools/analyzer.ts
440
+ async function analyzeUrl(params) {
441
+ const { url } = params;
442
+ const issues = [];
443
+ try {
444
+ const crawlResult = await crawlUrl({ url });
445
+ if (!crawlResult.success || !crawlResult.data) {
446
+ return { success: false, error: `Failed to crawl URL: ${crawlResult.error}` };
447
+ }
448
+ const { html, statusCode, loadTime } = crawlResult.data;
449
+ if (statusCode !== 200) {
450
+ issues.push({
451
+ severity: "critical",
452
+ category: "technical",
453
+ code: "HTTP_ERROR",
454
+ message: `Page returned HTTP ${statusCode}`,
455
+ impact: "Search engines cannot index pages that return error codes"
456
+ });
457
+ }
458
+ const metaResult = await extractMeta({ html, url });
459
+ if (metaResult.success && metaResult.data) {
460
+ const meta = metaResult.data;
461
+ analyzeMeta(meta, url, issues);
462
+ }
463
+ const headingsResult = await analyzeHeadings({ html });
464
+ if (headingsResult.success && headingsResult.data) {
465
+ const headings = headingsResult.data;
466
+ analyzeHeadingStructure(headings, issues);
467
+ }
468
+ const imagesResult = await extractImages({ html });
469
+ if (imagesResult.success && imagesResult.data) {
470
+ const images = imagesResult.data;
471
+ analyzeImages(images, issues);
472
+ }
473
+ const schemaResult = await extractSchema({ html });
474
+ if (schemaResult.success) {
475
+ const schemas = schemaResult.data;
476
+ if (!schemas || schemas.length === 0) {
477
+ issues.push({
478
+ severity: "warning",
479
+ category: "schema",
480
+ code: "MISSING_SCHEMA",
481
+ message: "No JSON-LD structured data found",
482
+ impact: "Missing rich snippets in search results. Add Schema.org markup for your content type."
483
+ });
484
+ }
485
+ }
486
+ const robotsResult = await checkRobots({ url });
487
+ if (robotsResult.success) {
488
+ const robots = robotsResult.data;
489
+ if (!robots.exists) {
490
+ issues.push({
491
+ severity: "warning",
492
+ category: "technical",
493
+ code: "MISSING_ROBOTS",
494
+ message: "No robots.txt file found",
495
+ impact: "Provide crawling instructions to search engines with robots.txt"
496
+ });
497
+ }
498
+ }
499
+ const sitemapResult = await checkSitemap({ url });
500
+ if (sitemapResult.success) {
501
+ const sitemap = sitemapResult.data;
502
+ if (!sitemap.exists) {
503
+ issues.push({
504
+ severity: "warning",
505
+ category: "technical",
506
+ code: "MISSING_SITEMAP",
507
+ message: "No sitemap.xml found",
508
+ impact: "Sitemap helps search engines discover and index all your pages"
509
+ });
510
+ }
511
+ }
512
+ if (loadTime > 3e3) {
513
+ issues.push({
514
+ severity: loadTime > 5e3 ? "critical" : "warning",
515
+ category: "performance",
516
+ code: "SLOW_LOAD_TIME",
517
+ message: `Page load time is ${(loadTime / 1e3).toFixed(1)}s`,
518
+ impact: "Slow pages hurt user experience and search rankings. Target <2.5s LCP."
519
+ });
520
+ }
521
+ const score = calculateScore(issues);
522
+ const result = {
523
+ url,
524
+ score,
525
+ issues,
526
+ recommendations: generateRecommendations(issues)
527
+ };
528
+ return { success: true, data: result };
529
+ } catch (error) {
530
+ return {
531
+ success: false,
532
+ error: error instanceof Error ? error.message : "Analysis failed"
533
+ };
534
+ }
535
+ }
536
+ function analyzeMeta(meta, url, issues) {
537
+ if (!meta.title) {
538
+ issues.push({
539
+ severity: "critical",
540
+ category: "meta",
541
+ code: "MISSING_TITLE",
542
+ message: "Missing title tag",
543
+ impact: "Title is the most important on-page SEO element. Every page needs a unique title.",
544
+ fix: {
545
+ file: "index.html",
546
+ before: null,
547
+ after: "<title>Your Page Title - Brand Name</title>"
548
+ }
549
+ });
550
+ } else if (meta.title.length < 30) {
551
+ issues.push({
552
+ severity: "warning",
553
+ category: "meta",
554
+ code: "TITLE_TOO_SHORT",
555
+ message: `Title too short (${meta.title.length} chars)`,
556
+ impact: "Longer titles provide more context. Aim for 50-60 characters.",
557
+ element: meta.title
558
+ });
559
+ } else if (meta.title.length > 60) {
560
+ issues.push({
561
+ severity: "warning",
562
+ category: "meta",
563
+ code: "TITLE_TOO_LONG",
564
+ message: `Title too long (${meta.title.length} chars)`,
565
+ impact: "Titles over 60 characters get truncated in search results.",
566
+ element: meta.title.substring(0, 60) + "..."
567
+ });
568
+ }
569
+ if (!meta.description) {
570
+ issues.push({
571
+ severity: "critical",
572
+ category: "meta",
573
+ code: "MISSING_META_DESC",
574
+ message: "Missing meta description",
575
+ impact: "Meta description appears in search results and affects click-through rate.",
576
+ fix: {
577
+ file: "index.html",
578
+ before: null,
579
+ after: '<meta name="description" content="A compelling description of your page in 150-160 characters." />'
580
+ }
581
+ });
582
+ } else if (meta.description.length < 120) {
583
+ issues.push({
584
+ severity: "warning",
585
+ category: "meta",
586
+ code: "META_DESC_TOO_SHORT",
587
+ message: `Meta description too short (${meta.description.length} chars)`,
588
+ impact: "Aim for 150-160 characters for optimal display in search results."
589
+ });
590
+ } else if (meta.description.length > 160) {
591
+ issues.push({
592
+ severity: "info",
593
+ category: "meta",
594
+ code: "META_DESC_TOO_LONG",
595
+ message: `Meta description may be truncated (${meta.description.length} chars)`,
596
+ impact: "Descriptions over 160 characters may be cut off in search results."
597
+ });
598
+ }
599
+ if (!meta.canonical) {
600
+ issues.push({
601
+ severity: "warning",
602
+ category: "technical",
603
+ code: "MISSING_CANONICAL",
604
+ message: "No canonical URL specified",
605
+ impact: "Canonical tags prevent duplicate content issues.",
606
+ fix: {
607
+ file: "index.html",
608
+ before: null,
609
+ after: `<link rel="canonical" href="${url}" />`
610
+ }
611
+ });
612
+ }
613
+ if (!meta.viewport) {
614
+ issues.push({
615
+ severity: "critical",
616
+ category: "technical",
617
+ code: "MISSING_VIEWPORT",
618
+ message: "Missing viewport meta tag",
619
+ impact: "Required for mobile-friendly pages. Google uses mobile-first indexing.",
620
+ fix: {
621
+ file: "index.html",
622
+ before: null,
623
+ after: '<meta name="viewport" content="width=device-width, initial-scale=1" />'
624
+ }
625
+ });
626
+ }
627
+ const missingOG = [];
628
+ if (!meta.openGraph.title) missingOG.push("og:title");
629
+ if (!meta.openGraph.description) missingOG.push("og:description");
630
+ if (!meta.openGraph.image) missingOG.push("og:image");
631
+ if (missingOG.length > 0) {
632
+ issues.push({
633
+ severity: "warning",
634
+ category: "social",
635
+ code: "MISSING_OG_TAGS",
636
+ message: `Missing Open Graph tags: ${missingOG.join(", ")}`,
637
+ impact: "Open Graph tags control how your page appears when shared on social media."
638
+ });
639
+ }
640
+ if (!meta.twitter.card) {
641
+ issues.push({
642
+ severity: "info",
643
+ category: "social",
644
+ code: "MISSING_TWITTER_CARD",
645
+ message: "Missing Twitter Card meta tags",
646
+ impact: "Twitter/X cards improve visibility when your content is shared."
647
+ });
648
+ }
649
+ }
650
+ function analyzeHeadingStructure(headings, issues) {
651
+ const h1s = headings.filter((h) => h.level === 1);
652
+ if (h1s.length === 0) {
653
+ issues.push({
654
+ severity: "critical",
655
+ category: "content",
656
+ code: "MISSING_H1",
657
+ message: "No H1 heading found",
658
+ impact: "Every page should have exactly one H1 that describes the main content."
659
+ });
660
+ } else if (h1s.length > 1) {
661
+ issues.push({
662
+ severity: "warning",
663
+ category: "content",
664
+ code: "MULTIPLE_H1",
665
+ message: `Multiple H1 headings found (${h1s.length})`,
666
+ impact: "Best practice is one H1 per page. Use H2-H6 for subsections.",
667
+ element: h1s.map((h) => h.text).join(", ")
668
+ });
669
+ }
670
+ let prevLevel = 0;
671
+ for (const h of headings) {
672
+ if (h.level > prevLevel + 1 && prevLevel > 0) {
673
+ issues.push({
674
+ severity: "info",
675
+ category: "content",
676
+ code: "SKIPPED_HEADING_LEVEL",
677
+ message: `Skipped heading level: H${prevLevel} to H${h.level}`,
678
+ impact: "Maintain proper heading hierarchy for accessibility and SEO."
679
+ });
680
+ break;
681
+ }
682
+ prevLevel = h.level;
683
+ }
684
+ }
685
+ function analyzeImages(images, issues) {
686
+ const missingAlt = images.filter((img) => !img.alt);
687
+ if (missingAlt.length > 0) {
688
+ issues.push({
689
+ severity: "warning",
690
+ category: "accessibility",
691
+ code: "IMAGES_MISSING_ALT",
692
+ message: `${missingAlt.length} image(s) missing alt text`,
693
+ impact: "Alt text is essential for accessibility and helps search engines understand images.",
694
+ element: missingAlt.slice(0, 3).map((i) => i.src).join(", ")
695
+ });
696
+ }
697
+ const missingDimensions = images.filter((img) => !img.width || !img.height);
698
+ if (missingDimensions.length > 0) {
699
+ issues.push({
700
+ severity: "info",
701
+ category: "performance",
702
+ code: "IMAGES_MISSING_DIMENSIONS",
703
+ message: `${missingDimensions.length} image(s) missing width/height`,
704
+ impact: "Specifying dimensions prevents layout shift (CLS)."
705
+ });
706
+ }
707
+ }
708
+ function calculateScore(issues) {
709
+ const weights = {
710
+ critical: 15,
711
+ warning: 5,
712
+ info: 1
713
+ };
714
+ let totalDeduction = 0;
715
+ for (const issue of issues) {
716
+ totalDeduction += weights[issue.severity];
717
+ }
718
+ return Math.max(0, Math.min(100, 100 - totalDeduction));
719
+ }
720
+ function generateRecommendations(issues) {
721
+ const recommendations = [];
722
+ const criticalCount = issues.filter((i) => i.severity === "critical").length;
723
+ const warningCount = issues.filter((i) => i.severity === "warning").length;
724
+ if (criticalCount > 0) {
725
+ recommendations.push({
726
+ priority: "high",
727
+ category: "immediate",
728
+ message: `Fix ${criticalCount} critical issue(s) first`,
729
+ impact: "Critical issues directly impact search visibility"
730
+ });
731
+ }
732
+ if (issues.some((i) => i.code === "MISSING_SCHEMA")) {
733
+ recommendations.push({
734
+ priority: "high",
735
+ category: "schema",
736
+ message: "Add JSON-LD structured data",
737
+ impact: "Enable rich snippets in search results"
738
+ });
739
+ }
740
+ if (issues.some((i) => i.code.includes("OG") || i.code.includes("TWITTER"))) {
741
+ recommendations.push({
742
+ priority: "medium",
743
+ category: "social",
744
+ message: "Complete social media meta tags",
745
+ impact: "Improve appearance when shared on social platforms"
746
+ });
747
+ }
748
+ if (warningCount > 3) {
749
+ recommendations.push({
750
+ priority: "medium",
751
+ category: "optimization",
752
+ message: `Address ${warningCount} warnings to improve score`,
753
+ impact: "Each fix improves overall SEO health"
754
+ });
755
+ }
756
+ return recommendations;
757
+ }
758
+
759
+ export {
760
+ __require,
761
+ __export,
762
+ httpGet,
763
+ httpHead,
764
+ httpPost,
765
+ crawlUrl,
766
+ extractMeta,
767
+ analyzeHeadings,
768
+ extractImages,
769
+ extractLinks,
770
+ extractSchema,
771
+ checkRobots,
772
+ checkSitemap,
773
+ analyzeUrl
774
+ };