@boy-offi9-inc/tidepull 0.19.0 → 0.20.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.
@@ -15,3 +15,4 @@ export declare function abortError(): TidepullError;
15
15
  export declare class RedirectError extends TidepullError {
16
16
  constructor(message: string);
17
17
  }
18
+ export declare function httpErrorCode(status: number): TidepullErrorCode;
@@ -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
- lastErr = new TidepullError(`HTTP ${res.status}`);
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
- throw new TidepullError(`Unable to reach ${url}: ${errMsg(lastErr)}`);
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
+ }
@@ -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,34 @@ 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 ex = ctx.extractors.find((e) => e.suitable(url));
22
- if (!ex)
23
- throw new TidepullError(`No extractor can handle ${url.href}`);
24
- ctx.emit({ type: "extract", extractor: ex.name, url: url.href });
25
- const result = await ex.extract(url, ctx);
26
- if (isPlaylist(result)) {
27
- if (!result.entries?.length)
28
- throw new TidepullError(`Extractor "${ex.name}" returned an empty playlist`);
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 lastErr;
25
+ for (const ex of candidates) {
26
+ ctx.emit({ type: "extract", extractor: ex.name, url: url.href });
27
+ let result;
28
+ try {
29
+ result = await ex.extract(url, ctx);
30
+ }
31
+ catch (e) {
32
+ if (ctx.signal?.aborted)
33
+ throw e;
34
+ lastErr = e;
35
+ ctx.log.debug(`extractor "${ex.name}" failed: ${e instanceof Error ? e.message : String(e)} — trying next candidate`);
36
+ continue;
37
+ }
38
+ if (isPlaylist(result)) {
39
+ if (!result.entries?.length)
40
+ throw new TidepullError(`Extractor "${ex.name}" returned an empty playlist`);
41
+ }
42
+ else if (!result.formats?.length)
43
+ throw new TidepullError(`Extractor "${ex.name}" returned no formats`);
44
+ return result;
29
45
  }
30
- else if (!result.formats?.length)
31
- throw new TidepullError(`Extractor "${ex.name}" returned no formats`);
32
- return result;
46
+ if (lastErr instanceof TidepullError)
47
+ throw lastErr;
48
+ throw new TidepullError(`All ${candidates.length} matching extractor(s) failed for ${url.href}: ${errMsg(lastErr)}`);
33
49
  }
34
50
  export async function extractInfo(rawUrl, ctx) {
35
51
  const result = await resolveUrl(rawUrl, ctx);
@@ -1,6 +1,6 @@
1
- import { type TidepullErrorCode } from "../core/errors.js";
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.19.0",
3
+ "version": "0.20.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",