feat: configurable status code behaviours (#340)

This commit is contained in:
Andrew Mackrodt
2020-09-29 14:54:26 +01:00
committed by GitHub
parent 7d8897cd9f
commit 3b7487e97a
7 changed files with 115 additions and 41 deletions
+2 -1
View File
@@ -33,6 +33,7 @@ export const Asus: Store = {
url: 'https://store.asus.com/us/item/202009AM150000003/'
}
],
name: 'asus'
name: 'asus',
successStatusCodes: [[0, 399], 404]
};
+52
View File
@@ -0,0 +1,52 @@
import {Link, Store} from '..';
import {Logger, Print} from '../../../logger';
import {delay, isStatusCodeInRange} from '../../../util';
import {Config} from '../../../config';
type Backoff = {
count: number;
time: number;
};
const stores: Record<string, Backoff> = {};
export async function processBackoffDelay(store: Store, link: Link, statusCode: number): Promise<number> {
/**
* We treat statusCode 0 as successful as some of the puppeteer plugins
* cause side-effects resulting in an empty response object even though
* the page renders fine and its content is accessible.
*/
let backoffStatusCodes = store.backoffStatusCodes;
if (!backoffStatusCodes) {
backoffStatusCodes = [403];
}
const isBackoff = isStatusCodeInRange(statusCode, backoffStatusCodes);
let backoff = stores[store.name];
if (!backoff) {
backoff = {count: 0, time: Config.browser.minBackoff};
stores[store.name] = backoff;
}
if (!isBackoff) {
if (backoff.count > 0) {
backoff.count--;
backoff.time = Math.max(backoff.time / 2, Config.browser.minBackoff);
}
return -1;
}
const backoffTime = backoff.time;
Logger.debug(Print.backoff(link, store, {delay: backoffTime, statusCode}, true));
await delay(backoff.time);
backoff.count++;
backoff.time = Math.min(backoff.time * 2, Config.browser.maxBackoff);
return backoffTime;
}
+15
View File
@@ -27,7 +27,15 @@ export type Labels = {
outOfStock?: LabelQuery;
};
export type StatusCodeRangeArray = Array<(number | [number, number])>;
export type Store = {
/**
* The range of status codes which will trigger backoff, i.e. an increasing
* delay between requests. Setting an empty array will disable the feature.
* If not defined, the default range will be used: 403.
*/
backoffStatusCodes?: StatusCodeRangeArray;
disableAdBlocker?: boolean;
links: Link[];
linksBuilder?: {
@@ -37,5 +45,12 @@ export type Store = {
labels: Labels;
name: string;
setupAction?: (browser: Browser) => void;
/**
* The range of status codes which considered successful, i.e. without error
* allowing request parsing to continue. Setting an empty array will cause
* all requests to fail. If not defined, the default range will be used:
* 0 -> 399 inclusive.
*/
successStatusCodes?: StatusCodeRangeArray;
waitUntil?: LoadEvent;
};
+1
View File
@@ -1,6 +1,7 @@
import {Store} from './store';
export const Zotac: Store = {
backoffStatusCodes: [403, 503],
labels: {
inStock: {
container: '.add-to-cart-wrapper',