@boy-offi9-inc/tidepull 0.19.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/errors.d.ts +1 -0
- package/dist/core/errors.js +9 -0
- package/dist/core/http.js +18 -3
- package/dist/core/pipeline.js +33 -12
- package/dist/extractors/direct.d.ts +2 -2
- package/dist/extractors/direct.js +2 -10
- package/package.json +2 -2
package/dist/core/errors.d.ts
CHANGED
package/dist/core/errors.js
CHANGED
|
@@ -35,3 +35,12 @@ export class RedirectError extends TidepullError {
|
|
|
35
35
|
this.name = "RedirectError";
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
+
export function httpErrorCode(status) {
|
|
39
|
+
if (status === 429)
|
|
40
|
+
return "RATE_LIMITED";
|
|
41
|
+
if (status === 401 || status === 403)
|
|
42
|
+
return "AUTH_REQUIRED";
|
|
43
|
+
if (status === 451)
|
|
44
|
+
return "GEO_BLOCKED";
|
|
45
|
+
return "UPSTREAM_FAILED";
|
|
46
|
+
}
|
package/dist/core/http.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TidepullError, RedirectError, errMsg } from "./errors.js";
|
|
1
|
+
import { TidepullError, RedirectError, errMsg, codedError, httpErrorCode } from "./errors.js";
|
|
2
2
|
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
3
3
|
const REDIRECT = new Set([301, 302, 303, 307, 308]);
|
|
4
4
|
const OPEN_HEADERS = new Set(["user-agent", "accept", "accept-language", "accept-encoding", "range", "referer", "origin"]);
|
|
@@ -58,7 +58,10 @@ export class Http {
|
|
|
58
58
|
const res = await this.follow(url, init);
|
|
59
59
|
if ((res.status >= 500 || res.status === 429) && attempt < this.retries) {
|
|
60
60
|
await res.body?.cancel();
|
|
61
|
-
|
|
61
|
+
const ra = Number(res.headers.get("retry-after"));
|
|
62
|
+
if (Number.isFinite(ra) && ra > 0)
|
|
63
|
+
await sleep(Math.min(ra * 1000, 15_000));
|
|
64
|
+
lastErr = codedError(httpErrorCode(res.status), `HTTP ${res.status} from ${url}`);
|
|
62
65
|
continue;
|
|
63
66
|
}
|
|
64
67
|
return res;
|
|
@@ -69,7 +72,11 @@ export class Http {
|
|
|
69
72
|
lastErr = e;
|
|
70
73
|
}
|
|
71
74
|
}
|
|
72
|
-
|
|
75
|
+
const status = lastStatus(lastErr);
|
|
76
|
+
if (status !== undefined) {
|
|
77
|
+
throw codedError(httpErrorCode(status), `Unable to reach ${url} after ${this.retries + 1} attempts: HTTP ${status}`);
|
|
78
|
+
}
|
|
79
|
+
throw codedError("UPSTREAM_FAILED", `Unable to reach ${url}: ${errMsg(lastErr)}`);
|
|
73
80
|
}
|
|
74
81
|
async follow(url, init) {
|
|
75
82
|
const start = new URL(url);
|
|
@@ -120,3 +127,11 @@ export class Http {
|
|
|
120
127
|
};
|
|
121
128
|
}
|
|
122
129
|
}
|
|
130
|
+
function lastStatus(e) {
|
|
131
|
+
if (e instanceof TidepullError) {
|
|
132
|
+
const m = /HTTP (\d{3})/.exec(e.message);
|
|
133
|
+
if (m)
|
|
134
|
+
return Number(m[1]);
|
|
135
|
+
}
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
package/dist/core/pipeline.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { mkdir, rename, rm, stat } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { downloadDash, downloadHls, downloadHttp, RateLimiter } from "./downloader.js";
|
|
4
|
-
import { TidepullError } from "./errors.js";
|
|
4
|
+
import { TidepullError, codedError, errMsg } from "./errors.js";
|
|
5
5
|
import { findFfmpeg, mergeStreams, remux, extractAudio } from "./ffmpeg.js";
|
|
6
6
|
import { Meter } from "./meter.js";
|
|
7
7
|
import { selectFormats } from "./selector.js";
|
|
@@ -18,18 +18,39 @@ export async function resolveUrl(rawUrl, ctx) {
|
|
|
18
18
|
}
|
|
19
19
|
if (url.protocol !== "http:" && url.protocol !== "https:")
|
|
20
20
|
throw new TidepullError(`Unsupported URL scheme: ${url.protocol}`, 2);
|
|
21
|
-
const
|
|
22
|
-
if (
|
|
23
|
-
throw
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
const candidates = ctx.extractors.filter((e) => e.suitable(url));
|
|
22
|
+
if (candidates.length === 0)
|
|
23
|
+
throw codedError("UNSUPPORTED", `No extractor can handle ${url.href}`, "Check the URL, or install a plugin for this site (tidepull plugins)");
|
|
24
|
+
let primaryErr;
|
|
25
|
+
let lastErr;
|
|
26
|
+
for (const ex of candidates) {
|
|
27
|
+
ctx.emit({ type: "extract", extractor: ex.name, url: url.href });
|
|
28
|
+
let result;
|
|
29
|
+
try {
|
|
30
|
+
result = await ex.extract(url, ctx);
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
if (ctx.signal?.aborted)
|
|
34
|
+
throw e;
|
|
35
|
+
if (primaryErr === undefined)
|
|
36
|
+
primaryErr = e;
|
|
37
|
+
lastErr = e;
|
|
38
|
+
ctx.log.debug(`extractor "${ex.name}" failed: ${e instanceof Error ? e.message : String(e)} — trying next candidate`);
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (isPlaylist(result)) {
|
|
42
|
+
if (!result.entries?.length)
|
|
43
|
+
throw new TidepullError(`Extractor "${ex.name}" returned an empty playlist`);
|
|
44
|
+
}
|
|
45
|
+
else if (!result.formats?.length)
|
|
46
|
+
throw new TidepullError(`Extractor "${ex.name}" returned no formats`);
|
|
47
|
+
return result;
|
|
29
48
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
49
|
+
const codedOf = (e) => e instanceof TidepullError && typeof e.code === "string" ? e : undefined;
|
|
50
|
+
const chosen = codedOf(primaryErr) ?? codedOf(lastErr) ?? primaryErr ?? lastErr;
|
|
51
|
+
if (chosen instanceof TidepullError)
|
|
52
|
+
throw chosen;
|
|
53
|
+
throw new TidepullError(`All ${candidates.length} matching extractor(s) failed for ${url.href}: ${errMsg(lastErr)}`);
|
|
33
54
|
}
|
|
34
55
|
export async function extractInfo(rawUrl, ctx) {
|
|
35
56
|
const result = await resolveUrl(rawUrl, ctx);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { httpErrorCode } from "../core/errors.js";
|
|
2
|
+
export { httpErrorCode };
|
|
2
3
|
import type { Ctx, Info } from "../core/types.js";
|
|
3
|
-
export declare function httpErrorCode(status: number): TidepullErrorCode;
|
|
4
4
|
export declare const directExtractor: {
|
|
5
5
|
name: string;
|
|
6
6
|
suitable: (url: URL) => boolean;
|
|
@@ -1,14 +1,6 @@
|
|
|
1
|
-
import { TidepullError, codedError } from "../core/errors.js";
|
|
1
|
+
import { TidepullError, codedError, httpErrorCode } from "../core/errors.js";
|
|
2
|
+
export { httpErrorCode };
|
|
2
3
|
import { extOf, stem } from "./util.js";
|
|
3
|
-
export function httpErrorCode(status) {
|
|
4
|
-
if (status === 429)
|
|
5
|
-
return "RATE_LIMITED";
|
|
6
|
-
if (status === 401 || status === 403)
|
|
7
|
-
return "AUTH_REQUIRED";
|
|
8
|
-
if (status === 451)
|
|
9
|
-
return "GEO_BLOCKED";
|
|
10
|
-
return "UPSTREAM_FAILED";
|
|
11
|
-
}
|
|
12
4
|
const VIDEO = new Set(["mp4", "m4v", "webm", "mkv", "mov", "flv", "avi", "ts"]);
|
|
13
5
|
const AUDIO = new Set(["mp3", "m4a", "aac", "ogg", "oga", "opus", "wav", "flac"]);
|
|
14
6
|
export const directExtractor = {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boy-offi9-inc/tidepull",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "A minimal, from-scratch media downloader library: extractor plugins, yt-dlp-style format selection, resumable downloads.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"build": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json && node scripts/fix-dts.mjs",
|
|
26
26
|
"prepack": "npm run build",
|
|
27
27
|
"prepublishOnly": "npm run typecheck && npm test",
|
|
28
|
-
"size": "node scripts/size.mjs dist/index.js --budget=
|
|
28
|
+
"size": "node scripts/size.mjs dist/index.js --budget=58",
|
|
29
29
|
"lint:imports": "node scripts/lint-imports.mjs"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|