Skip to content

Commit

Permalink
feat(acot-runner-sitemap): add timeout options
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed Apr 18, 2021
1 parent 41b06f9 commit 1a27081
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
28 changes: 23 additions & 5 deletions packages/acot-runner-sitemap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The URL of `sitemap.xml`
}
```

## `include`
### `include`

**Type:** `string[]`
**Required:** `false`
Expand All @@ -70,7 +70,7 @@ Page path pattern to include in audit target. See the [micromatch][mm] documenta
}
```

## `exclude`
### `exclude`

**Type:** `string[]`
**Required:** `false`
Expand All @@ -89,7 +89,7 @@ Page path pattern to exclude in audit target. See the [micromatch][mm] documenta
}
```

## `limit`
### `limit`

**Type:** `number`
**Required:** `false`
Expand All @@ -108,7 +108,7 @@ Maximum number of pages to include in the audit target. If no value is specified
}
```

## `random`
### `random`

**Type:** `{ pattern: string; limit: number }[]`
**Required:** `false`
Expand Down Expand Up @@ -136,7 +136,7 @@ Randomly include the number of `limit`s in the audit target from the page list t
}
```

## `headers`
### `headers`

**Type:** `Record<string, string>`
**Required:** `false`
Expand All @@ -157,4 +157,22 @@ The key-value of the header used when fetching the `sitemap.xml` specified in [s
}
```

### `timeout`

**Type:** `number`
**Default:** `60000`
**Required:** `false`

Maximum time in milliseconds to wait for collecting sitemaps.

```json
{
"runner": "@acot/sitemap",
"with": {
"source": "https://acot.example/sitemap.xml",
"timeout": 120000
}
}
```

[mm]: https://github.com/micromatch/micromatch
22 changes: 20 additions & 2 deletions packages/acot-runner-sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { transform } from 'camaro';
import micromatch from 'micromatch';
const debug = require('debug')('acot:runner:sitemap');

const DEFAULT_TIMEOUT = 60 * 1000;

type Options = {
source: string;
include?: string[];
Expand All @@ -21,6 +23,7 @@ type Options = {
limit: number;
}[];
headers?: Record<string, string>;
timeout?: number;
};

const schema: Schema = {
Expand Down Expand Up @@ -65,6 +68,10 @@ const schema: Schema = {
headers: {
type: 'object',
},
timeout: {
type: 'number',
minimum: 0,
},
},
required: ['source'],
additionalProperties: false,
Expand Down Expand Up @@ -109,8 +116,10 @@ const filterUrls = (urls: string[], options: Options) => {
};

const fetchSitemap = async (url: string, options: Options) => {
const now = Date.now();
const res = await fetch(url, {
headers: options.headers,
timeout: options.timeout!,
});

if (res.ok === false) {
Expand All @@ -128,7 +137,12 @@ const fetchSitemap = async (url: string, options: Options) => {

if (transformed.maps.length > 0) {
const children = await Promise.all(
transformed.maps.map((child) => fetchSitemap(child, options)),
transformed.maps.map((child) =>
fetchSitemap(child, {
...options,
timeout: Math.max(1, options.timeout! - (Date.now() - now)),
}),
),
);

urls = urls.concat(...children);
Expand All @@ -141,7 +155,11 @@ export class SitemapRunner extends AcotRunner<Options> {
protected async collect(): AcotRunnerCollectResult {
const router = new ConfigRouter(this.config);
const sources = new Map();
let entries = await fetchSitemap(this.options.source, this.options);

let entries = await fetchSitemap(this.options.source, {
timeout: DEFAULT_TIMEOUT,
...this.options,
});

entries =
this.options.limit != null
Expand Down

0 comments on commit 1a27081

Please sign in to comment.