68 lines
3.0 KiB
TypeScript
68 lines
3.0 KiB
TypeScript
import type { QualifiedAttribute, QualifiedTag } from 'sax';
|
|
import { Readable, Transform, type TransformOptions, type TransformCallback } from 'node:stream';
|
|
import type { SitemapItem, XMLToSitemapItemStreamOptions } from './types.js';
|
|
import { SaxParserStream } from './sax-parser-stream.js';
|
|
export type { XMLToSitemapItemStreamOptions };
|
|
/**
|
|
* Takes a stream of xml and transforms it into a stream of SitemapItems
|
|
* Use this to parse existing sitemaps into config options compatible with this library
|
|
*
|
|
* Emits `'maxEntriesExceeded'` (with the cap) once, when the first `<url>` past
|
|
* `maxEntries` is seen. If nothing listens for that event, the stream logs the
|
|
* overflow through its logger instead.
|
|
*/
|
|
export declare class XMLToSitemapItemStream extends SaxParserStream<SitemapItem> {
|
|
private currentItem;
|
|
private currentVideo;
|
|
private currentImage;
|
|
private currentLink;
|
|
private dontpushCurrentLink;
|
|
constructor(opts?: XMLToSitemapItemStreamOptions);
|
|
/**
|
|
* Number of `<url>` entries seen, including any past `maxEntries`
|
|
* @deprecated Use entryCount
|
|
*/
|
|
get urlCount(): number;
|
|
protected onOpenTag(tag: QualifiedTag): void;
|
|
protected onCharData(tag: string, text: string): void;
|
|
/** Check a yes/no value, reporting it if it is invalid */
|
|
private isYesNo;
|
|
protected onAttribute(currentTag: string, attr: QualifiedAttribute): void;
|
|
protected onCloseTag(tag: string): void;
|
|
}
|
|
/**
|
|
Read xml and resolve with the configuration that would produce it or reject with
|
|
an error
|
|
```
|
|
import { createReadStream } from 'fs'
|
|
import { Readable } from 'stream'
|
|
import { parseSitemap, SitemapStream, streamToPromise } from 'sitemap'
|
|
const items = await parseSitemap(createReadStream('./example.xml'))
|
|
// produces the same xml
|
|
// you can, of course, more practically modify it or store it
|
|
const sms = new SitemapStream()
|
|
Readable.from(items).pipe(sms)
|
|
console.log((await streamToPromise(sms)).toString())
|
|
```
|
|
Once the sitemap passes 50,000 `<url>` entries, parsing stops: `xml` is
|
|
destroyed and the promise resolves with the first 50,000 items.
|
|
@param {Readable} xml what to parse
|
|
@return {Promise<SitemapItem[]>} resolves with list of sitemap items that can be fed into a SitemapStream. Rejects with an Error object if `xml` or the parser fails, including on truncated XML.
|
|
*/
|
|
export declare function parseSitemap(xml: Readable): Promise<SitemapItem[]>;
|
|
export interface ObjectStreamToJSONOptions extends TransformOptions {
|
|
lineSeparated: boolean;
|
|
}
|
|
/**
|
|
* A Transform that converts a stream of objects into a JSON Array or a line
|
|
* separated stringified JSON
|
|
* @param [lineSeparated=false] whether to separate entries by a new line or comma
|
|
*/
|
|
export declare class ObjectStreamToJSON extends Transform {
|
|
lineSeparated: boolean;
|
|
firstWritten: boolean;
|
|
constructor(opts?: ObjectStreamToJSONOptions);
|
|
_transform(chunk: SitemapItem, encoding: string, cb: TransformCallback): void;
|
|
_flush(cb: TransformCallback): void;
|
|
}
|