stripe-loaders-core 0.0.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 ADDED
@@ -0,0 +1,167 @@
1
+ # Stripe Docs Sitemap Processor
2
+
3
+ A utility library for processing Stripe documentation sitemaps. This library helps you fetch, parse, and process sitemaps from Stripe's documentation site.
4
+
5
+ ## Features
6
+
7
+ - Extract URLs from Stripe documentation sitemaps
8
+ - Process sitemap index files to get all URLs from multiple sitemaps
9
+ - Find newly added URLs by comparing current and previous URL lists
10
+ - Configurable logging with custom logger support
11
+ - Debug mode for detailed logging
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ npm install stripe-loader-core
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Basic Usage
22
+
23
+ ```typescript
24
+ import { SitemapProcessor } from 'stripe-loader-core';
25
+
26
+ // Create a processor instance
27
+ const processor = new SitemapProcessor();
28
+
29
+ // Fetch and process a sitemap index
30
+ async function fetchAllDocs() {
31
+ try {
32
+ const urls = await processor.fetchAndProcessSitemapIndex('https://docs.stripe.com/sitemap.xml');
33
+ console.log(`Found ${urls.length} URLs in the Stripe documentation`);
34
+ return urls;
35
+ } catch (error) {
36
+ console.error('Error processing sitemap:', error);
37
+ return [];
38
+ }
39
+ }
40
+
41
+ // Find new URLs
42
+ function findNewContent(currentUrls, previousUrls) {
43
+ const newUrls = processor.findNewUrls(currentUrls, previousUrls);
44
+ console.log(`Found ${newUrls.length} new URLs`);
45
+ return newUrls;
46
+ }
47
+ ```
48
+
49
+ ### With Debug Mode
50
+
51
+ Enable debug mode to see detailed logs during processing:
52
+
53
+ ```typescript
54
+ // Create a processor with debug mode enabled
55
+ const processor = new SitemapProcessor({ debug: true });
56
+
57
+ // Now all processing will output detailed logs
58
+ const urls = await processor.fetchAndProcessSitemapIndex('https://docs.stripe.com/sitemap.xml');
59
+ ```
60
+
61
+ ### Custom Logger
62
+
63
+ You can provide your own logger implementation:
64
+
65
+ ```typescript
66
+ import { SitemapProcessor, Logger } from '@stripe-loaders/core';
67
+
68
+ // Implement your own logger
69
+ class MyCustomLogger implements Logger {
70
+ log(message: string): void {
71
+ // Custom log implementation
72
+ myLoggingService.info(message);
73
+ }
74
+
75
+ error(message: string): void {
76
+ // Custom error implementation
77
+ myLoggingService.error(message);
78
+ }
79
+
80
+ warn(message: string): void {
81
+ // Custom warning implementation
82
+ myLoggingService.warn(message);
83
+ }
84
+ }
85
+
86
+ // Create a processor with custom logger
87
+ const processor = new SitemapProcessor({
88
+ logger: new MyCustomLogger(),
89
+ });
90
+ ```
91
+
92
+ ### Processing Individual Sitemaps
93
+
94
+ If you need to process a specific sitemap file:
95
+
96
+ ```typescript
97
+ // Process a single sitemap file
98
+ const urls = await processor.fetchAndProcessSitemap('https://docs.stripe.com/sitemap-api.xml');
99
+ console.log(`Found ${urls.length} API documentation URLs`);
100
+ ```
101
+
102
+ ### Extracting URLs from Sitemap Content
103
+
104
+ If you already have the sitemap content:
105
+
106
+ ```typescript
107
+ // Extract URLs from sitemap content
108
+ const sitemapContent = '<?xml version="1.0" encoding="UTF-8"?>...'; // XML content
109
+ const urls = processor.extractUrlsFromSitemap(sitemapContent);
110
+
111
+ // Extract sitemap URLs from a sitemap index
112
+ const indexContent = '<?xml version="1.0" encoding="UTF-8"?>...'; // XML content
113
+ const sitemapUrls = processor.extractSitemapUrlsFromIndex(indexContent);
114
+ ```
115
+
116
+ ## API Reference
117
+
118
+ ### SitemapProcessor
119
+
120
+ The main class for processing sitemaps.
121
+
122
+ #### Constructor
123
+
124
+ ```typescript
125
+ constructor(options: { debug?: boolean; logger?: Logger } = {})
126
+ ```
127
+
128
+ - `options.debug`: Enable debug mode (default: false)
129
+ - `options.logger`: Custom logger implementation (default: ConsoleLogger)
130
+
131
+ #### Methods
132
+
133
+ - `extractUrlsFromSitemap(content: string, baseUrl?: string): string[]`
134
+
135
+ - Extract URLs from sitemap XML content
136
+ - `baseUrl` defaults to 'https://docs.stripe.com/'
137
+
138
+ - `extractSitemapUrlsFromIndex(content: string): string[]`
139
+
140
+ - Extract sitemap URLs from a sitemap index XML content
141
+
142
+ - `findNewUrls(currentUrls: string[], previousUrls: string[]): string[]`
143
+
144
+ - Find URLs that exist in currentUrls but not in previousUrls
145
+
146
+ - `fetchAndProcessSitemap(url: string, baseUrl?: string): Promise<string[]>`
147
+
148
+ - Fetch a sitemap from the given URL and extract all URLs
149
+
150
+ - `fetchAndProcessSitemapIndex(indexUrl: string, baseUrl?: string): Promise<string[]>`
151
+ - Fetch a sitemap index and process all referenced sitemaps
152
+
153
+ ### Logger Interface
154
+
155
+ Interface for custom loggers.
156
+
157
+ ```typescript
158
+ interface Logger {
159
+ log(message: string): void;
160
+ error(message: string): void;
161
+ warn(message: string): void;
162
+ }
163
+ ```
164
+
165
+ ## License
166
+
167
+ MIT
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Utility functions for processing Stripe docs sitemaps
3
+ */
4
+ /**
5
+ * Logger interface for SitemapProcessor
6
+ */
7
+ export interface Logger {
8
+ log(message: string): void;
9
+ error(message: string): void;
10
+ warn(message: string): void;
11
+ }
12
+ /**
13
+ * Default console logger implementation
14
+ */
15
+ export declare class ConsoleLogger implements Logger {
16
+ private debug;
17
+ constructor(debug?: boolean);
18
+ log(message: string): void;
19
+ error(message: string): void;
20
+ warn(message: string): void;
21
+ }
22
+ /**
23
+ * Utility class for processing Stripe docs sitemaps
24
+ */
25
+ export declare class SitemapProcessor {
26
+ private debug;
27
+ private logger;
28
+ /**
29
+ * Constructor for SitemapProcessor
30
+ * @param options Configuration options
31
+ */
32
+ constructor(options?: {
33
+ debug?: boolean;
34
+ logger?: Logger;
35
+ });
36
+ /**
37
+ * Log a message if debug mode is enabled
38
+ * @param message Message to log
39
+ */
40
+ private log;
41
+ /**
42
+ * Log an error message
43
+ * @param message Error message to log
44
+ */
45
+ private logError;
46
+ /**
47
+ * Log a warning message
48
+ * @param message Warning message to log
49
+ */
50
+ private logWarning;
51
+ /**
52
+ * Extract URLs from a sitemap
53
+ * @param content Sitemap XML content
54
+ * @param baseUrl Base URL part to extract (e.g., https://docs.stripe.com/)
55
+ * @returns Array of extracted URLs
56
+ */
57
+ extractUrlsFromSitemap(content: string, baseUrl?: string): string[];
58
+ /**
59
+ * Extract sitemap URLs from a sitemap index
60
+ * @param content Sitemap index XML content
61
+ * @returns Array of extracted sitemap URLs
62
+ */
63
+ extractSitemapUrlsFromIndex(content: string): string[];
64
+ /**
65
+ * Find newly added URLs
66
+ * @param currentUrls Current URL list
67
+ * @param previousUrls Previous URL list
68
+ * @returns Array of newly added URLs
69
+ */
70
+ findNewUrls(currentUrls: string[], previousUrls: string[]): string[];
71
+ /**
72
+ * Fetch and process a sitemap
73
+ * @param url Sitemap URL
74
+ * @param baseUrl Base URL part to extract
75
+ * @returns List of retrieved URLs
76
+ */
77
+ fetchAndProcessSitemap(url: string, baseUrl?: string): Promise<string[]>;
78
+ /**
79
+ * Fetch and process a sitemap index
80
+ * @param indexUrl Sitemap index URL
81
+ * @param baseUrl Base URL part to extract
82
+ * @returns List of retrieved URLs
83
+ */
84
+ fetchAndProcessSitemapIndex(indexUrl: string, baseUrl?: string): Promise<string[]>;
85
+ }
86
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,MAAM,WAAW,MAAM;IACrB,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CAC7B;AAED;;GAEG;AACH,qBAAa,aAAc,YAAW,MAAM;IAC9B,OAAO,CAAC,KAAK;gBAAL,KAAK,GAAE,OAAe;IAE1C,GAAG,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAM1B,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;IAI5B,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI;CAK5B;AAED;;GAEG;AACH,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,KAAK,CAAU;IACvB,OAAO,CAAC,MAAM,CAAS;IAEvB;;;OAGG;gBACS,OAAO,GAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAO;IAK9D;;;OAGG;IACH,OAAO,CAAC,GAAG;IAIX;;;OAGG;IACH,OAAO,CAAC,QAAQ;IAIhB;;;OAGG;IACH,OAAO,CAAC,UAAU;IAIlB;;;;;OAKG;IACI,sBAAsB,CAC3B,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,MAAmC,GAC3C,MAAM,EAAE;IAYX;;;;OAIG;IACI,2BAA2B,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE;IAyC7D;;;;;OAKG;IACI,WAAW,CAAC,WAAW,EAAE,MAAM,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE;IAK3E;;;;;OAKG;IACU,sBAAsB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAWrF;;;;;OAKG;IACU,2BAA2B,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;CAyBhG"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=index.spec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.spec.d.ts","sourceRoot":"","sources":["../src/index.spec.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class c{constructor(t=!1){this.debug=t}log(t){this.debug&&console.log(t)}error(t){console.error(t)}warn(t){this.debug&&console.warn(t)}}class h{constructor(t={}){this.debug=t.debug??!1,this.logger=t.logger??new c(this.debug)}log(t){this.logger.log(t)}logError(t){this.logger.error(t)}logWarning(t){this.logger.warn(t)}extractUrlsFromSitemap(t,r="https://docs.stripe.com/"){const e=new RegExp(`<loc>(${r}[^<]+)</loc>`,"g"),s=[];let o;for(;(o=e.exec(t))!==null;)s.push(o[1]);return s}extractSitemapUrlsFromIndex(t){if(!t||typeof t!="string")return this.logError("Invalid input: content must be a string"),[];if(!t.includes("<?xml")||!t.includes("<sitemapindex"))return this.logError("Invalid sitemap index XML: Missing <?xml or <sitemapindex> elements."),[];try{const r=/<loc>([^<]+)<\/loc>/g,e=[];let s;for(;(s=r.exec(t))!==null;){const o=s[1];try{new URL(o),e.push(o)}catch{this.logWarning(`Skipped invalid URL format: ${o}`)}}return e.length===0&&this.logWarning("No valid URLs found in sitemap index"),e}catch(r){return this.logError(`Error extracting URLs from sitemap index: ${r}`),[]}}findNewUrls(t,r){const e=new Set(r);return t.filter(s=>!e.has(s))}async fetchAndProcessSitemap(t,r){const e=await fetch(t);if(!e.ok)throw new Error(`Failed to fetch sitemap: ${e.status} ${e.statusText}`);const s=await e.text();return this.extractUrlsFromSitemap(s,r)}async fetchAndProcessSitemapIndex(t,r){this.log(`Fetching sitemap index: ${t}`);const e=await fetch(t);if(!e.ok)throw new Error(`Failed to fetch sitemap index: ${e.status} ${e.statusText}`);const s=await e.text(),o=this.extractSitemapUrlsFromIndex(s);this.log(`Number of sitemap partitions: ${o.length}`);const n=[];for(let i=0;i<o.length;i++){const l=o[i];this.log(`Processing partition ${i+1}/${o.length}: ${l}`);const a=await this.fetchAndProcessSitemap(l,r);this.log(`Retrieved ${a.length} URLs from partition ${i+1}`),n.push(...a)}return n}}exports.ConsoleLogger=c;exports.SitemapProcessor=h;
2
+ //# sourceMappingURL=stripe-loaders-core.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stripe-loaders-core.cjs","sources":["../src/index.ts"],"sourcesContent":["/**\n * Utility functions for processing Stripe docs sitemaps\n */\n\n/**\n * Logger interface for SitemapProcessor\n */\nexport interface Logger {\n log(message: string): void;\n error(message: string): void;\n warn(message: string): void;\n}\n\n/**\n * Default console logger implementation\n */\nexport class ConsoleLogger implements Logger {\n constructor(private debug: boolean = false) {}\n\n log(message: string): void {\n if (this.debug) {\n console.log(message);\n }\n }\n\n error(message: string): void {\n console.error(message);\n }\n\n warn(message: string): void {\n if (this.debug) {\n console.warn(message);\n }\n }\n}\n\n/**\n * Utility class for processing Stripe docs sitemaps\n */\nexport class SitemapProcessor {\n private debug: boolean;\n private logger: Logger;\n\n /**\n * Constructor for SitemapProcessor\n * @param options Configuration options\n */\n constructor(options: { debug?: boolean; logger?: Logger } = {}) {\n this.debug = options.debug ?? false;\n this.logger = options.logger ?? new ConsoleLogger(this.debug);\n }\n\n /**\n * Log a message if debug mode is enabled\n * @param message Message to log\n */\n private log(message: string): void {\n this.logger.log(message);\n }\n\n /**\n * Log an error message\n * @param message Error message to log\n */\n private logError(message: string): void {\n this.logger.error(message);\n }\n\n /**\n * Log a warning message\n * @param message Warning message to log\n */\n private logWarning(message: string): void {\n this.logger.warn(message);\n }\n\n /**\n * Extract URLs from a sitemap\n * @param content Sitemap XML content\n * @param baseUrl Base URL part to extract (e.g., https://docs.stripe.com/)\n * @returns Array of extracted URLs\n */\n public extractUrlsFromSitemap(\n content: string,\n baseUrl: string = 'https://docs.stripe.com/'\n ): string[] {\n const urlRegex = new RegExp(`<loc>(${baseUrl}[^<]+)<\\/loc>`, 'g');\n const urls: string[] = [];\n let match;\n\n while ((match = urlRegex.exec(content)) !== null) {\n urls.push(match[1]);\n }\n\n return urls;\n }\n\n /**\n * Extract sitemap URLs from a sitemap index\n * @param content Sitemap index XML content\n * @returns Array of extracted sitemap URLs\n */\n public extractSitemapUrlsFromIndex(content: string): string[] {\n // Input validation\n if (!content || typeof content !== 'string') {\n this.logError('Invalid input: content must be a string');\n return [];\n }\n\n // Basic XML structure validation\n if (!content.includes('<?xml') || !content.includes('<sitemapindex')) {\n this.logError('Invalid sitemap index XML: Missing <?xml or <sitemapindex> elements.');\n return [];\n }\n\n try {\n const sitemapRegex = /<loc>([^<]+)<\\/loc>/g;\n const sitemapUrls: string[] = [];\n let match;\n\n while ((match = sitemapRegex.exec(content)) !== null) {\n // URL validation (optional)\n const url = match[1];\n try {\n new URL(url); // Check if URL is valid\n sitemapUrls.push(url);\n } catch (urlError) {\n this.logWarning(`Skipped invalid URL format: ${url}`);\n // Skip invalid URLs\n }\n }\n\n if (sitemapUrls.length === 0) {\n this.logWarning('No valid URLs found in sitemap index');\n }\n\n return sitemapUrls;\n } catch (error) {\n this.logError(`Error extracting URLs from sitemap index: ${error}`);\n return []; // Return empty array on error\n }\n }\n\n /**\n * Find newly added URLs\n * @param currentUrls Current URL list\n * @param previousUrls Previous URL list\n * @returns Array of newly added URLs\n */\n public findNewUrls(currentUrls: string[], previousUrls: string[]): string[] {\n const previousUrlSet = new Set(previousUrls);\n return currentUrls.filter(url => !previousUrlSet.has(url));\n }\n\n /**\n * Fetch and process a sitemap\n * @param url Sitemap URL\n * @param baseUrl Base URL part to extract\n * @returns List of retrieved URLs\n */\n public async fetchAndProcessSitemap(url: string, baseUrl?: string): Promise<string[]> {\n const response = await fetch(url);\n\n if (!response.ok) {\n throw new Error(`Failed to fetch sitemap: ${response.status} ${response.statusText}`);\n }\n\n const sitemapContent = await response.text();\n return this.extractUrlsFromSitemap(sitemapContent, baseUrl);\n }\n\n /**\n * Fetch and process a sitemap index\n * @param indexUrl Sitemap index URL\n * @param baseUrl Base URL part to extract\n * @returns List of retrieved URLs\n */\n public async fetchAndProcessSitemapIndex(indexUrl: string, baseUrl?: string): Promise<string[]> {\n this.log(`Fetching sitemap index: ${indexUrl}`);\n const response = await fetch(indexUrl);\n\n if (!response.ok) {\n throw new Error(`Failed to fetch sitemap index: ${response.status} ${response.statusText}`);\n }\n\n const indexContent = await response.text();\n const sitemapUrls = this.extractSitemapUrlsFromIndex(indexContent);\n this.log(`Number of sitemap partitions: ${sitemapUrls.length}`);\n\n // Combine URLs from each sitemap file\n const allUrls: string[] = [];\n\n for (let i = 0; i < sitemapUrls.length; i++) {\n const sitemapUrl = sitemapUrls[i];\n this.log(`Processing partition ${i + 1}/${sitemapUrls.length}: ${sitemapUrl}`);\n const urls = await this.fetchAndProcessSitemap(sitemapUrl, baseUrl);\n this.log(`Retrieved ${urls.length} URLs from partition ${i + 1}`);\n allUrls.push(...urls);\n }\n\n return allUrls;\n }\n}\n"],"names":["ConsoleLogger","debug","message","SitemapProcessor","options","content","baseUrl","urlRegex","urls","match","sitemapRegex","sitemapUrls","url","error","currentUrls","previousUrls","previousUrlSet","response","sitemapContent","indexUrl","indexContent","allUrls","sitemapUrl"],"mappings":"gFAgBO,MAAMA,CAAgC,CAC3C,YAAoBC,EAAiB,GAAO,CAAxB,KAAA,MAAAA,CAAA,CAEpB,IAAIC,EAAuB,CACrB,KAAK,OACP,QAAQ,IAAIA,CAAO,CACrB,CAGF,MAAMA,EAAuB,CAC3B,QAAQ,MAAMA,CAAO,CAAA,CAGvB,KAAKA,EAAuB,CACtB,KAAK,OACP,QAAQ,KAAKA,CAAO,CACtB,CAEJ,CAKO,MAAMC,CAAiB,CAQ5B,YAAYC,EAAgD,GAAI,CACzD,KAAA,MAAQA,EAAQ,OAAS,GAC9B,KAAK,OAASA,EAAQ,QAAU,IAAIJ,EAAc,KAAK,KAAK,CAAA,CAOtD,IAAIE,EAAuB,CAC5B,KAAA,OAAO,IAAIA,CAAO,CAAA,CAOjB,SAASA,EAAuB,CACjC,KAAA,OAAO,MAAMA,CAAO,CAAA,CAOnB,WAAWA,EAAuB,CACnC,KAAA,OAAO,KAAKA,CAAO,CAAA,CASnB,uBACLG,EACAC,EAAkB,2BACR,CACV,MAAMC,EAAW,IAAI,OAAO,SAASD,CAAO,eAAiB,GAAG,EAC1DE,EAAiB,CAAC,EACpB,IAAAC,EAEJ,MAAQA,EAAQF,EAAS,KAAK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N,MAAA,IAAI,MAAM,kCAAkCA,EAAS,MAAM,IAAIA,EAAS,UAAU,EAAE,EAGtF,MAAAG,EAAe,MAAMH,EAAS,KAAK,EACnCN,EAAc,KAAK,4BAA4BS,CAAY,EACjE,KAAK,IAAI,iCAAiCT,EAAY,MAAM,EAAE,EAG9D,MAAMU,EAAoB,CAAC,EAE3B,QAAS,EAAI,EAAG,EAAIV,EAAY,OAAQ,IAAK,CACrC,MAAAW,EAAaX,EAAY,CAAC,EAC3B,KAAA,IAAI,wBAAwB,EAAI,CAAC,IAAIA,EAAY,MAAM,KAAKW,CAAU,EAAE,EAC7E,MAAMd,EAAO,MAAM,KAAK,uBAAuBc,EAAYhB,CAAO,EAClE,KAAK,IAAI,aAAaE,EAAK,MAAM,wBAAwB,EAAI,CAAC,EAAE,EACxDa,EAAA,KAAK,GAAGb,CAAI,CAAA,CAGf,OAAAa,CAAA,CAEX"}
@@ -0,0 +1,133 @@
1
+ class g {
2
+ constructor(t = !1) {
3
+ this.debug = t;
4
+ }
5
+ log(t) {
6
+ this.debug && console.log(t);
7
+ }
8
+ error(t) {
9
+ console.error(t);
10
+ }
11
+ warn(t) {
12
+ this.debug && console.warn(t);
13
+ }
14
+ }
15
+ class h {
16
+ /**
17
+ * Constructor for SitemapProcessor
18
+ * @param options Configuration options
19
+ */
20
+ constructor(t = {}) {
21
+ this.debug = t.debug ?? !1, this.logger = t.logger ?? new g(this.debug);
22
+ }
23
+ /**
24
+ * Log a message if debug mode is enabled
25
+ * @param message Message to log
26
+ */
27
+ log(t) {
28
+ this.logger.log(t);
29
+ }
30
+ /**
31
+ * Log an error message
32
+ * @param message Error message to log
33
+ */
34
+ logError(t) {
35
+ this.logger.error(t);
36
+ }
37
+ /**
38
+ * Log a warning message
39
+ * @param message Warning message to log
40
+ */
41
+ logWarning(t) {
42
+ this.logger.warn(t);
43
+ }
44
+ /**
45
+ * Extract URLs from a sitemap
46
+ * @param content Sitemap XML content
47
+ * @param baseUrl Base URL part to extract (e.g., https://docs.stripe.com/)
48
+ * @returns Array of extracted URLs
49
+ */
50
+ extractUrlsFromSitemap(t, r = "https://docs.stripe.com/") {
51
+ const e = new RegExp(`<loc>(${r}[^<]+)</loc>`, "g"), s = [];
52
+ let i;
53
+ for (; (i = e.exec(t)) !== null; )
54
+ s.push(i[1]);
55
+ return s;
56
+ }
57
+ /**
58
+ * Extract sitemap URLs from a sitemap index
59
+ * @param content Sitemap index XML content
60
+ * @returns Array of extracted sitemap URLs
61
+ */
62
+ extractSitemapUrlsFromIndex(t) {
63
+ if (!t || typeof t != "string")
64
+ return this.logError("Invalid input: content must be a string"), [];
65
+ if (!t.includes("<?xml") || !t.includes("<sitemapindex"))
66
+ return this.logError("Invalid sitemap index XML: Missing <?xml or <sitemapindex> elements."), [];
67
+ try {
68
+ const r = /<loc>([^<]+)<\/loc>/g, e = [];
69
+ let s;
70
+ for (; (s = r.exec(t)) !== null; ) {
71
+ const i = s[1];
72
+ try {
73
+ new URL(i), e.push(i);
74
+ } catch {
75
+ this.logWarning(`Skipped invalid URL format: ${i}`);
76
+ }
77
+ }
78
+ return e.length === 0 && this.logWarning("No valid URLs found in sitemap index"), e;
79
+ } catch (r) {
80
+ return this.logError(`Error extracting URLs from sitemap index: ${r}`), [];
81
+ }
82
+ }
83
+ /**
84
+ * Find newly added URLs
85
+ * @param currentUrls Current URL list
86
+ * @param previousUrls Previous URL list
87
+ * @returns Array of newly added URLs
88
+ */
89
+ findNewUrls(t, r) {
90
+ const e = new Set(r);
91
+ return t.filter((s) => !e.has(s));
92
+ }
93
+ /**
94
+ * Fetch and process a sitemap
95
+ * @param url Sitemap URL
96
+ * @param baseUrl Base URL part to extract
97
+ * @returns List of retrieved URLs
98
+ */
99
+ async fetchAndProcessSitemap(t, r) {
100
+ const e = await fetch(t);
101
+ if (!e.ok)
102
+ throw new Error(`Failed to fetch sitemap: ${e.status} ${e.statusText}`);
103
+ const s = await e.text();
104
+ return this.extractUrlsFromSitemap(s, r);
105
+ }
106
+ /**
107
+ * Fetch and process a sitemap index
108
+ * @param indexUrl Sitemap index URL
109
+ * @param baseUrl Base URL part to extract
110
+ * @returns List of retrieved URLs
111
+ */
112
+ async fetchAndProcessSitemapIndex(t, r) {
113
+ this.log(`Fetching sitemap index: ${t}`);
114
+ const e = await fetch(t);
115
+ if (!e.ok)
116
+ throw new Error(`Failed to fetch sitemap index: ${e.status} ${e.statusText}`);
117
+ const s = await e.text(), i = this.extractSitemapUrlsFromIndex(s);
118
+ this.log(`Number of sitemap partitions: ${i.length}`);
119
+ const n = [];
120
+ for (let o = 0; o < i.length; o++) {
121
+ const l = i[o];
122
+ this.log(`Processing partition ${o + 1}/${i.length}: ${l}`);
123
+ const a = await this.fetchAndProcessSitemap(l, r);
124
+ this.log(`Retrieved ${a.length} URLs from partition ${o + 1}`), n.push(...a);
125
+ }
126
+ return n;
127
+ }
128
+ }
129
+ export {
130
+ g as ConsoleLogger,
131
+ h as SitemapProcessor
132
+ };
133
+ //# sourceMappingURL=stripe-loaders-core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stripe-loaders-core.js","sources":["../src/index.ts"],"sourcesContent":["/**\n * Utility functions for processing Stripe docs sitemaps\n */\n\n/**\n * Logger interface for SitemapProcessor\n */\nexport interface Logger {\n log(message: string): void;\n error(message: string): void;\n warn(message: string): void;\n}\n\n/**\n * Default console logger implementation\n */\nexport class ConsoleLogger implements Logger {\n constructor(private debug: boolean = false) {}\n\n log(message: string): void {\n if (this.debug) {\n console.log(message);\n }\n }\n\n error(message: string): void {\n console.error(message);\n }\n\n warn(message: string): void {\n if (this.debug) {\n console.warn(message);\n }\n }\n}\n\n/**\n * Utility class for processing Stripe docs sitemaps\n */\nexport class SitemapProcessor {\n private debug: boolean;\n private logger: Logger;\n\n /**\n * Constructor for SitemapProcessor\n * @param options Configuration options\n */\n constructor(options: { debug?: boolean; logger?: Logger } = {}) {\n this.debug = options.debug ?? false;\n this.logger = options.logger ?? new ConsoleLogger(this.debug);\n }\n\n /**\n * Log a message if debug mode is enabled\n * @param message Message to log\n */\n private log(message: string): void {\n this.logger.log(message);\n }\n\n /**\n * Log an error message\n * @param message Error message to log\n */\n private logError(message: string): void {\n this.logger.error(message);\n }\n\n /**\n * Log a warning message\n * @param message Warning message to log\n */\n private logWarning(message: string): void {\n this.logger.warn(message);\n }\n\n /**\n * Extract URLs from a sitemap\n * @param content Sitemap XML content\n * @param baseUrl Base URL part to extract (e.g., https://docs.stripe.com/)\n * @returns Array of extracted URLs\n */\n public extractUrlsFromSitemap(\n content: string,\n baseUrl: string = 'https://docs.stripe.com/'\n ): string[] {\n const urlRegex = new RegExp(`<loc>(${baseUrl}[^<]+)<\\/loc>`, 'g');\n const urls: string[] = [];\n let match;\n\n while ((match = urlRegex.exec(content)) !== null) {\n urls.push(match[1]);\n }\n\n return urls;\n }\n\n /**\n * Extract sitemap URLs from a sitemap index\n * @param content Sitemap index XML content\n * @returns Array of extracted sitemap URLs\n */\n public extractSitemapUrlsFromIndex(content: string): string[] {\n // Input validation\n if (!content || typeof content !== 'string') {\n this.logError('Invalid input: content must be a string');\n return [];\n }\n\n // Basic XML structure validation\n if (!content.includes('<?xml') || !content.includes('<sitemapindex')) {\n this.logError('Invalid sitemap index XML: Missing <?xml or <sitemapindex> elements.');\n return [];\n }\n\n try {\n const sitemapRegex = /<loc>([^<]+)<\\/loc>/g;\n const sitemapUrls: string[] = [];\n let match;\n\n while ((match = sitemapRegex.exec(content)) !== null) {\n // URL validation (optional)\n const url = match[1];\n try {\n new URL(url); // Check if URL is valid\n sitemapUrls.push(url);\n } catch (urlError) {\n this.logWarning(`Skipped invalid URL format: ${url}`);\n // Skip invalid URLs\n }\n }\n\n if (sitemapUrls.length === 0) {\n this.logWarning('No valid URLs found in sitemap index');\n }\n\n return sitemapUrls;\n } catch (error) {\n this.logError(`Error extracting URLs from sitemap index: ${error}`);\n return []; // Return empty array on error\n }\n }\n\n /**\n * Find newly added URLs\n * @param currentUrls Current URL list\n * @param previousUrls Previous URL list\n * @returns Array of newly added URLs\n */\n public findNewUrls(currentUrls: string[], previousUrls: string[]): string[] {\n const previousUrlSet = new Set(previousUrls);\n return currentUrls.filter(url => !previousUrlSet.has(url));\n }\n\n /**\n * Fetch and process a sitemap\n * @param url Sitemap URL\n * @param baseUrl Base URL part to extract\n * @returns List of retrieved URLs\n */\n public async fetchAndProcessSitemap(url: string, baseUrl?: string): Promise<string[]> {\n const response = await fetch(url);\n\n if (!response.ok) {\n throw new Error(`Failed to fetch sitemap: ${response.status} ${response.statusText}`);\n }\n\n const sitemapContent = await response.text();\n return this.extractUrlsFromSitemap(sitemapContent, baseUrl);\n }\n\n /**\n * Fetch and process a sitemap index\n * @param indexUrl Sitemap index URL\n * @param baseUrl Base URL part to extract\n * @returns List of retrieved URLs\n */\n public async fetchAndProcessSitemapIndex(indexUrl: string, baseUrl?: string): Promise<string[]> {\n this.log(`Fetching sitemap index: ${indexUrl}`);\n const response = await fetch(indexUrl);\n\n if (!response.ok) {\n throw new Error(`Failed to fetch sitemap index: ${response.status} ${response.statusText}`);\n }\n\n const indexContent = await response.text();\n const sitemapUrls = this.extractSitemapUrlsFromIndex(indexContent);\n this.log(`Number of sitemap partitions: ${sitemapUrls.length}`);\n\n // Combine URLs from each sitemap file\n const allUrls: string[] = [];\n\n for (let i = 0; i < sitemapUrls.length; i++) {\n const sitemapUrl = sitemapUrls[i];\n this.log(`Processing partition ${i + 1}/${sitemapUrls.length}: ${sitemapUrl}`);\n const urls = await this.fetchAndProcessSitemap(sitemapUrl, baseUrl);\n this.log(`Retrieved ${urls.length} URLs from partition ${i + 1}`);\n allUrls.push(...urls);\n }\n\n return allUrls;\n }\n}\n"],"names":["ConsoleLogger","debug","message","SitemapProcessor","options","content","baseUrl","urlRegex","urls","match","sitemapRegex","sitemapUrls","url","error","currentUrls","previousUrls","previousUrlSet","response","sitemapContent","indexUrl","indexContent","allUrls","i","sitemapUrl"],"mappings":"AAgBO,MAAMA,EAAgC;AAAA,EAC3C,YAAoBC,IAAiB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AS,UAAU,EAAE;AAGtF,UAAAG,IAAe,MAAMH,EAAS,KAAK,GACnCN,IAAc,KAAK,4BAA4BS,CAAY;AACjE,SAAK,IAAI,iCAAiCT,EAAY,MAAM,EAAE;AAG9D,UAAMU,IAAoB,CAAC;AAE3B,aAASC,IAAI,GAAGA,IAAIX,EAAY,QAAQW,KAAK;AACrC,YAAAC,IAAaZ,EAAYW,CAAC;AAC3B,WAAA,IAAI,wBAAwBA,IAAI,CAAC,IAAIX,EAAY,MAAM,KAAKY,CAAU,EAAE;AAC7E,YAAMf,IAAO,MAAM,KAAK,uBAAuBe,GAAYjB,CAAO;AAClE,WAAK,IAAI,aAAaE,EAAK,MAAM,wBAAwBc,IAAI,CAAC,EAAE,GACxDD,EAAA,KAAK,GAAGb,CAAI;AAAA,IAAA;AAGf,WAAAa;AAAA,EAAA;AAEX;"}
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "stripe-loaders-core",
3
+ "version": "0.0.0",
4
+ "type": "module",
5
+ "main": "./dist/stripe-loaders-core.cjs",
6
+ "module": "./dist/stripe-loaders-core.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/stripe-loaders-core.js",
12
+ "require": "./dist/stripe-loaders-core.cjs"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "dev": "vite build --watch",
20
+ "build": "vite build && tsc --emitDeclarationOnly",
21
+ "test": "vitest run",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "keywords": [
25
+ "stripe",
26
+ "documentation",
27
+ "sitemap",
28
+ "processor",
29
+ "utility"
30
+ ],
31
+ "author": "hideokamoto",
32
+ "license": "MIT",
33
+ "description": "Core library for Stripe loaders",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "https://github.com/wpkyoto/stripe-docs-loader",
37
+ "directory": "packages/core"
38
+ },
39
+ "bugs": {
40
+ "url": "https://github.com/wpkyoto/stripe-docs-loader/issues"
41
+ },
42
+ "homepage": "https://github.com/wpkyoto/stripe-docs-loader/tree/main/packages/core#readme",
43
+ "publishConfig": {
44
+ "access": "public"
45
+ }
46
+ }