Skip to content

Commit

Permalink
step
Browse files Browse the repository at this point in the history
  • Loading branch information
Miniast committed May 8, 2024
1 parent 375fe81 commit 0134766
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/crawler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventEmitter } from "events";
import { Cluster } from "./rateLimiter/index.js";
import { isBoolean, isFunction, setDefaults, flattenDeep } from "./lib/utils.js";
import { isBoolean, isFunction, setDefaults, flattenDeep, lowerObjectKeys } from "./lib/utils.js";
import { getValidOptions, alignOptions, getCharset } from "./options.js";
import { logOptions } from "./logger.js";
import type { crawlerOptions, requestOptions } from "./types/crawler.js";
Expand All @@ -13,7 +13,7 @@ import { Logger } from "tslog";
process.env.NODE_ENV = process.env.NODE_ENV ?? process.argv[2];
// test
import fs from "fs";
// process.env.NODE_ENV = "debug";
process.env.NODE_ENV = "debug";
//
logOptions.minLevel = process.env.NODE_ENV === "debug" ? 0 : 3;
const log = new Logger(logOptions);
Expand Down Expand Up @@ -117,6 +117,7 @@ class Crawler extends EventEmitter {
else if (options.proxies) log.debug(`Using proxies: ${options.proxies}`);

options.headers = options.headers ?? {};
options.headers = lowerObjectKeys(options.headers);

if (options.forceUTF8 || options.json) options.encoding = "utf8";

Expand All @@ -125,7 +126,7 @@ class Crawler extends EventEmitter {
options.headers["user-agent"] = options.userAgent[this._rotatingUAIndex];
this._rotatingUAIndex++;
} else {
options.headers["user-agent"] = options.userAgent;
options.headers["user-agent"] = options.headers["user-agent"] ?? options.userAgent;
}

if (options.proxies) {
Expand Down
23 changes: 22 additions & 1 deletion src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ export function pick<T extends Object, K extends keyof T>(target: T, keys: (keyo
});
return result;
}

/**
*
* @param obj
* @returns a cleaned object
* @description
* Removes all undefined and null values from an object, this will be done recursively.
* But it will not remove empty objects. (i.e. {})
*/
export const cleanObject = (obj: Record<string, unknown>): Record<string, unknown> => {
Object.keys(obj).forEach(key => {
if (getType(obj[key]) === "object") {
Expand All @@ -76,4 +83,18 @@ export const cleanObject = (obj: Record<string, unknown>): Record<string, unknow
}
});
return obj;
}
/**
*
* @param obj
* @returns an object with all keys in lowercase
* @description
* Converts all keys of an object to lowercase.
*/
export const lowerObjectKeys = (obj: Record<string, unknown>): Record<string, unknown> => {
const result: Record<string, unknown> = {};
Object.keys(obj).forEach(key => {
result[key.toLowerCase()] = obj[key];
});
return result;
}
3 changes: 3 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ export const alignOptions = (options: any): any => {
delete gotOptions[key];
}
});

const headers = gotOptions.headers;
cleanObject(gotOptions);
gotOptions.headers = headers;

gotOptions.retry = { limit: 0 };
return gotOptions;
Expand Down

0 comments on commit 0134766

Please sign in to comment.