Skip to content

Commit

Permalink
feat: improve typings, refactor range creation
Browse files Browse the repository at this point in the history
* improve typings & update ts+eslint config for a better dev experience
* refactor range (start, end) creation -- using map function
  • Loading branch information
vinnyA3 committed Feb 18, 2022
1 parent 3d8dde9 commit 4b0d813
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
],
"env": {
"browser": false
},
"rules": {
"@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/ban-ts-comment": "off"
}
}
3 changes: 1 addition & 2 deletions lib/anime-dl/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fsP from "fs/promises";
// eslint-disable-next-line
const path = require("path"); // ignoring import lint, path only imported as common
import path from "path";
import cheerio from "cheerio";

import utils from "../utils";
Expand Down
12 changes: 5 additions & 7 deletions lib/anime-dl/processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const extractVideoMetadataFromDetailsPage = (
): VideoMetadata => {
const $ = cheerio.load(pageHTML);
const videoInfo = $(".anime_info_body");
let videoMetadata: VideoMetadata = {};
const videoMetadata: VideoMetadata = {};

videoMetadata.title = videoInfo.find("h1").text();

Expand All @@ -76,14 +76,12 @@ const getEpisodeRangesFromDetailsPage = (pageHTML: string) => {
const $ = cheerio.load(pageHTML);
const episodePage = $("#episode_page");
const episodeRanges = episodePage.find("li");
let extractedRanges: EpisodeRange[] = [];
const extractedRanges: EpisodeRange[] = [];

episodeRanges.each(function () {
const range = $(this).find("a").text();
const bounds = range.split("-");
const [lowerBound, upperBound] = bounds;
const normalizedLowerBound = stripNewlinesAndSpacesToNum(lowerBound);
const normalizedUpperBound = stripNewlinesAndSpacesToNum(upperBound);
const [start, end] = bounds.map(stripNewlinesAndSpacesToNum);

extractedRanges.push(
bounds.length === 1
Expand All @@ -92,8 +90,8 @@ const getEpisodeRangesFromDetailsPage = (pageHTML: string) => {
end: 1,
}
: {
start: normalizedLowerBound === 0 ? 1 : normalizedLowerBound,
end: normalizedUpperBound,
start: start === 0 ? 1 : start,
end,
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/check-executable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const {
const sanitizeCommand = (command: string): string => {
let result = "";

if (/[^A-Za-z0-9_\/:=-]/.test(command)) {
if (/[^A-Za-z0-9_/:=-]/.test(command)) {
result = "'" + command.replace(/'/g, "'\\''") + "'";
result = command
.replace(/^(?:'')+/g, "") // dedupe single-quote at the beginning
Expand Down
32 changes: 18 additions & 14 deletions lib/utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@ interface ResponseHeadersWithLocation extends http.IncomingHttpHeaders {
location: string;
}

type ClientProtocols = "http" | "https";
type ClientTypeOf = typeof http | typeof https;
type Protocols = "http" | "https";
type ClientType = typeof http | typeof https;
type EmptyOptions = Record<string, unknown>;

const Clients: { [k in ClientProtocols]: ClientTypeOf } = {
const Clients: { [k in Protocols]: ClientType } = {
http,
https,
};

const RedirectCodesMap: { [k in string]: number } = {
"301": 301,
"302": 302,
};

const noop = () => {};

const chooseClient = (url: string): ClientTypeOf => {
const chooseClient = (url: string): ClientType => {
const reProtocolMatch = /(https?):\/\//gi;
const matchResult = url.match(reProtocolMatch) || [];
let protocolScheme: ClientProtocols = "https";
let protocolScheme: Protocols = "https";

if (matchResult.length) {
protocolScheme = (matchResult[0].split(":")[0] ||
protocolScheme) as ClientProtocols;
protocolScheme) as Protocols;
}

return Clients[protocolScheme];
Expand All @@ -36,15 +42,14 @@ const getOriginHeadersWithLocation = (
| PromiseLike<ResponseHeadersWithLocation>
) => void,
reject: (reason?: string | undefined) => void,
options: http.RequestOptions | {}
options: http.RequestOptions | EmptyOptions
) => {
const client = chooseClient(url) as unknown as typeof http;
const client = chooseClient(url) as unknown as ClientType;

client
.get(url, options, (res) => {
const redirectCode =
res.statusCode != null &&
(res.statusCode === 301 || res.statusCode === 302);
res.statusCode != null && RedirectCodesMap[res.statusCode];

if (redirectCode) {
return getOriginHeadersWithLocation(
Expand All @@ -65,15 +70,14 @@ const get = (
url: string,
resolve: (value: string | PromiseLike<string>) => void,
reject: (reason?: string | undefined) => void,
options: http.RequestOptions | {}
options: http.RequestOptions | EmptyOptions
) => {
const client = chooseClient(url) as unknown as typeof http;
const client = chooseClient(url) as unknown as ClientType;

client
.get(url, options, (res) => {
const redirectCode =
res.statusCode != null &&
(res.statusCode === 301 || res.statusCode === 302);
res.statusCode != null && RedirectCodesMap[res.statusCode];

if (redirectCode) {
return get(res.headers.location || "", resolve, reject, options);
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": false,
"noUnusedLocals": true,
"esModuleInterop": true,
"strict": true,
"baseUrl": ".",
"paths": {
Expand Down

0 comments on commit 4b0d813

Please # to comment.