edge-sitemap
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -7,4 +7,4 @@ /** | ||
* @typedef {Object} SitemapItem | ||
* @property {SitemapChangeFreq} changefreq | ||
* @property {`${number}-${number}-${number}`} lastmod | ||
* @property {SitemapChangeFreq=} changefreq | ||
* @property {`${number}-${number}-${number}`=} lastmod | ||
* @property {(URL|string)} loc | ||
@@ -15,3 +15,3 @@ * @property {number=} priority | ||
/** | ||
* @typedef {Object} SitemapStreamOptions | ||
* @typedef {Object} SitemapTransformerOptions | ||
* @property {(URL|string)=} baseURL | ||
@@ -22,59 +22,106 @@ * @property {boolean=} pretty | ||
/** | ||
* @extends {TransformStream<SitemapItem, string>} | ||
* @implements {Transformer<SitemapItem, string>} | ||
*/ | ||
export class SitemapStream extends TransformStream { | ||
export class SitemapTransformer { | ||
/** @type {(URL|string)=} */ | ||
#baseURL | ||
/** @type {string} */ | ||
#lf | ||
/** @type {string} */ | ||
#indent | ||
/** | ||
* @param {SitemapStreamOptions=} options | ||
* @param {SitemapTransformerOptions=} options | ||
*/ | ||
constructor({ baseURL, pretty = false } = {}) { | ||
const lf = pretty ? '\n' : '' | ||
constructor( | ||
/** @todo https://github.com/microsoft/TypeScript/pull/52880 */ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
{ baseURL, pretty = false } = {} | ||
) { | ||
this.#baseURL = baseURL | ||
this.#lf = pretty ? '\n' : '' | ||
this.#indent = pretty ? ' ' : '' | ||
} | ||
super({ | ||
flush(controller) { | ||
controller.enqueue(`</urlset>${lf}`) | ||
}, | ||
start(controller) { | ||
controller.enqueue('<?xml version="1.0" encoding="UTF-8"?>\n') | ||
controller.enqueue( | ||
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${lf}` | ||
) | ||
}, | ||
transform(chunk, controller) { | ||
const indent = pretty ? ' ' : '' | ||
/** @type {TransformerFlushCallback<string>} */ | ||
flush(controller) { | ||
controller.enqueue(`</urlset>${this.#lf}`) | ||
} | ||
controller.enqueue(`${indent}<url>${lf}`) | ||
/** @type {TransformerStartCallback<string>} */ | ||
start(controller) { | ||
controller.enqueue('<?xml version="1.0" encoding="UTF-8"?>\n') | ||
controller.enqueue( | ||
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${this.#lf}` | ||
) | ||
} | ||
if (chunk.changefreq) { | ||
controller.enqueue( | ||
`${indent.repeat(2)}<changefreq>${ | ||
chunk.changefreq | ||
}</changefreq>${lf}` | ||
) | ||
} | ||
/** @type {TransformerTransformCallback<SitemapItem, string>} */ | ||
transform(chunk, controller) { | ||
/** @type {URL} */ | ||
let url | ||
try { | ||
url = new URL(chunk.loc, this.#baseURL) | ||
} catch (error) { | ||
controller.error(error) | ||
if (chunk.lastmod) { | ||
controller.enqueue( | ||
`${indent.repeat(2)}<lastmod>${chunk.lastmod}</lastmod>${lf}` | ||
) | ||
} | ||
return | ||
} | ||
controller.enqueue( | ||
`${indent.repeat(2)}<loc>${new URL( | ||
chunk.loc, | ||
baseURL | ||
).toString()}</loc>${lf}` | ||
) | ||
controller.enqueue(`${this.#indent}<url>${this.#lf}`) | ||
if (chunk.priority) { | ||
controller.enqueue( | ||
`${indent.repeat(2)}<priority>${chunk.priority.toFixed( | ||
1 | ||
)}</priority>${lf}` | ||
) | ||
} | ||
if (chunk.changefreq) { | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<changefreq>${chunk.changefreq}</changefreq>${ | ||
this.#lf | ||
}` | ||
) | ||
} | ||
controller.enqueue(`${indent}</url>${lf}`) | ||
}, | ||
}) | ||
if (chunk.lastmod) { | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<lastmod>${chunk.lastmod}</lastmod>${ | ||
this.#lf | ||
}` | ||
) | ||
} | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<loc>${url.toString()}</loc>${this.#lf}` | ||
) | ||
if (chunk.priority) { | ||
const priority = chunk.priority.toFixed(1) | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<priority>${priority}</priority>${this.#lf}` | ||
) | ||
} | ||
controller.enqueue(`${this.#indent}</url>${this.#lf}`) | ||
} | ||
} | ||
/** | ||
* @typedef {Object} SitemapStreamOptions | ||
* @property {(URL|string)=} baseURL | ||
* @property {boolean=} pretty | ||
*/ | ||
/** | ||
* @extends {TransformStream<SitemapItem, string>} | ||
*/ | ||
export class SitemapStream extends TransformStream { | ||
/** | ||
* @param {SitemapStreamOptions=} options | ||
*/ | ||
constructor( | ||
/** @todo https://github.com/microsoft/TypeScript/pull/52880 */ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
{ baseURL, pretty = false } = {} | ||
) { | ||
super(new SitemapTransformer({ baseURL, pretty })) | ||
} | ||
} |
@@ -42,4 +42,5 @@ { | ||
"types": "./src/index.d.ts", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"scripts": { | ||
"build": "tsc", | ||
"format": "prettier -w .", | ||
@@ -46,0 +47,0 @@ "lint": "eslint src", |
@@ -1,24 +0,57 @@ | ||
export type SitemapChangeFreq = | ||
| 'always' | ||
| 'daily' | ||
| 'hourly' | ||
| 'monthly' | ||
| 'never' | ||
| 'weekly' | ||
| 'yearly' | ||
export interface SitemapItem { | ||
changefreq?: SitemapChangeFreq | ||
lastmod?: `${number}-${number}-${number}` | ||
loc: URL | string | ||
priority?: number | ||
/** | ||
* @typedef {('always'|'daily'|'hourly'|'monthly'|'never'|'weekly'|'yearly')} SitemapChangeFreq | ||
*/ | ||
/** | ||
* @typedef {Object} SitemapItem | ||
* @property {SitemapChangeFreq=} changefreq | ||
* @property {`${number}-${number}-${number}`=} lastmod | ||
* @property {(URL|string)} loc | ||
* @property {number=} priority | ||
*/ | ||
/** | ||
* @typedef {Object} SitemapTransformerOptions | ||
* @property {(URL|string)=} baseURL | ||
* @property {boolean=} pretty | ||
*/ | ||
/** | ||
* @implements {Transformer<SitemapItem, string>} | ||
*/ | ||
export class SitemapTransformer implements Transformer<SitemapItem, string> { | ||
/** | ||
* @param {SitemapTransformerOptions=} options | ||
*/ | ||
constructor({ baseURL, pretty }?: SitemapTransformerOptions | undefined); | ||
flush(controller: TransformStreamDefaultController<string>): void | PromiseLike<void>; | ||
start(controller: TransformStreamDefaultController<string>): any; | ||
transform(chunk: SitemapItem, controller: TransformStreamDefaultController<string>): void | PromiseLike<void>; | ||
#private; | ||
} | ||
export interface SitemapStreamOptions { | ||
baseURL?: URL | string | ||
pretty?: boolean | ||
} | ||
/** | ||
* @typedef {Object} SitemapStreamOptions | ||
* @property {(URL|string)=} baseURL | ||
* @property {boolean=} pretty | ||
*/ | ||
/** | ||
* @extends {TransformStream<SitemapItem, string>} | ||
*/ | ||
export class SitemapStream extends TransformStream<SitemapItem, string> { | ||
constructor(options?: SitemapStreamOptions) | ||
/** | ||
* @param {SitemapStreamOptions=} options | ||
*/ | ||
constructor({ baseURL, pretty }?: SitemapStreamOptions | undefined); | ||
} | ||
export type SitemapChangeFreq = ('always' | 'daily' | 'hourly' | 'monthly' | 'never' | 'weekly' | 'yearly'); | ||
export type SitemapItem = { | ||
changefreq?: SitemapChangeFreq | undefined; | ||
lastmod?: `${number}-${number}-${number}` | undefined; | ||
loc: (URL | string); | ||
priority?: number | undefined; | ||
}; | ||
export type SitemapTransformerOptions = { | ||
baseURL?: (URL | string) | undefined; | ||
pretty?: boolean | undefined; | ||
}; | ||
export type SitemapStreamOptions = { | ||
baseURL?: (URL | string) | undefined; | ||
pretty?: boolean | undefined; | ||
}; |
145
src/index.js
@@ -7,4 +7,4 @@ /** | ||
* @typedef {Object} SitemapItem | ||
* @property {SitemapChangeFreq} changefreq | ||
* @property {`${number}-${number}-${number}`} lastmod | ||
* @property {SitemapChangeFreq=} changefreq | ||
* @property {`${number}-${number}-${number}`=} lastmod | ||
* @property {(URL|string)} loc | ||
@@ -15,3 +15,3 @@ * @property {number=} priority | ||
/** | ||
* @typedef {Object} SitemapStreamOptions | ||
* @typedef {Object} SitemapTransformerOptions | ||
* @property {(URL|string)=} baseURL | ||
@@ -22,59 +22,106 @@ * @property {boolean=} pretty | ||
/** | ||
* @extends {TransformStream<SitemapItem, string>} | ||
* @implements {Transformer<SitemapItem, string>} | ||
*/ | ||
export class SitemapStream extends TransformStream { | ||
export class SitemapTransformer { | ||
/** @type {(URL|string)=} */ | ||
#baseURL | ||
/** @type {string} */ | ||
#lf | ||
/** @type {string} */ | ||
#indent | ||
/** | ||
* @param {SitemapStreamOptions=} options | ||
* @param {SitemapTransformerOptions=} options | ||
*/ | ||
constructor({ baseURL, pretty = false } = {}) { | ||
const lf = pretty ? '\n' : '' | ||
constructor( | ||
/** @todo https://github.com/microsoft/TypeScript/pull/52880 */ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
{ baseURL, pretty = false } = {} | ||
) { | ||
this.#baseURL = baseURL | ||
this.#lf = pretty ? '\n' : '' | ||
this.#indent = pretty ? ' ' : '' | ||
} | ||
super({ | ||
flush(controller) { | ||
controller.enqueue(`</urlset>${lf}`) | ||
}, | ||
start(controller) { | ||
controller.enqueue('<?xml version="1.0" encoding="UTF-8"?>\n') | ||
controller.enqueue( | ||
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${lf}` | ||
) | ||
}, | ||
transform(chunk, controller) { | ||
const indent = pretty ? ' ' : '' | ||
/** @type {TransformerFlushCallback<string>} */ | ||
flush(controller) { | ||
controller.enqueue(`</urlset>${this.#lf}`) | ||
} | ||
controller.enqueue(`${indent}<url>${lf}`) | ||
/** @type {TransformerStartCallback<string>} */ | ||
start(controller) { | ||
controller.enqueue('<?xml version="1.0" encoding="UTF-8"?>\n') | ||
controller.enqueue( | ||
`<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${this.#lf}` | ||
) | ||
} | ||
if (chunk.changefreq) { | ||
controller.enqueue( | ||
`${indent.repeat(2)}<changefreq>${ | ||
chunk.changefreq | ||
}</changefreq>${lf}` | ||
) | ||
} | ||
/** @type {TransformerTransformCallback<SitemapItem, string>} */ | ||
transform(chunk, controller) { | ||
/** @type {URL} */ | ||
let url | ||
try { | ||
url = new URL(chunk.loc, this.#baseURL) | ||
} catch (error) { | ||
controller.error(error) | ||
if (chunk.lastmod) { | ||
controller.enqueue( | ||
`${indent.repeat(2)}<lastmod>${chunk.lastmod}</lastmod>${lf}` | ||
) | ||
} | ||
return | ||
} | ||
controller.enqueue( | ||
`${indent.repeat(2)}<loc>${new URL( | ||
chunk.loc, | ||
baseURL | ||
).toString()}</loc>${lf}` | ||
) | ||
controller.enqueue(`${this.#indent}<url>${this.#lf}`) | ||
if (chunk.priority) { | ||
controller.enqueue( | ||
`${indent.repeat(2)}<priority>${chunk.priority.toFixed( | ||
1 | ||
)}</priority>${lf}` | ||
) | ||
} | ||
if (chunk.changefreq) { | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<changefreq>${chunk.changefreq}</changefreq>${ | ||
this.#lf | ||
}` | ||
) | ||
} | ||
controller.enqueue(`${indent}</url>${lf}`) | ||
}, | ||
}) | ||
if (chunk.lastmod) { | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<lastmod>${chunk.lastmod}</lastmod>${ | ||
this.#lf | ||
}` | ||
) | ||
} | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<loc>${url.toString()}</loc>${this.#lf}` | ||
) | ||
if (chunk.priority) { | ||
const priority = chunk.priority.toFixed(1) | ||
controller.enqueue( | ||
`${this.#indent.repeat(2)}<priority>${priority}</priority>${this.#lf}` | ||
) | ||
} | ||
controller.enqueue(`${this.#indent}</url>${this.#lf}`) | ||
} | ||
} | ||
/** | ||
* @typedef {Object} SitemapStreamOptions | ||
* @property {(URL|string)=} baseURL | ||
* @property {boolean=} pretty | ||
*/ | ||
/** | ||
* @extends {TransformStream<SitemapItem, string>} | ||
*/ | ||
export class SitemapStream extends TransformStream { | ||
/** | ||
* @param {SitemapStreamOptions=} options | ||
*/ | ||
constructor( | ||
/** @todo https://github.com/microsoft/TypeScript/pull/52880 */ | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
{ baseURL, pretty = false } = {} | ||
) { | ||
super(new SitemapTransformer({ baseURL, pretty })) | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11050
271
1