@boy-offi9-inc/tidepull 0.8.0 → 0.9.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/README.md CHANGED
@@ -62,7 +62,7 @@ const files = await pullAll(g, "https://example.com/podcast.rss", { items: "1-3,
62
62
  // "01 - Episode title.mp3", "02 - ...": the number and the title come from the playlist
63
63
  ```
64
64
 
65
- `g.resolve(url)` returns an `Info` or a `Playlist` (`isPlaylist(x)` tells you which). `g.extract(url)` still returns a single `Info` and refuses a playlist with a clear error. A failing item is thrown by default; pass `onError` to report it and continue. Template fields for playlist items: `%(playlist)s`, `%(playlist_index)02d`, `%(playlist_count)s`, `%(playlist_id)s`.
65
+ `g.resolve(url)` returns an `Info` or a `Playlist` (`isPlaylist(x)` tells you which). `g.extract(url)` still returns a single `Info` and refuses a playlist with a clear error. A failing item is thrown by default; pass `onError` to report it and continue. Template fields for playlist items: `%(playlist)s`, `%(playlist_index)02d`, `%(playlist_count)s`, `%(playlist_id)s`. To download several items at once, pass `concurrency` (an integer ≥ 1, default 1); only the downloads run in parallel — items are still extracted in order and the returned files keep item order.
66
66
 
67
67
  ## Example
68
68
  ```console
package/dist/main.js CHANGED
@@ -107,6 +107,16 @@ function listEntries(p) {
107
107
  p.entries.forEach((e, i) => rows.push([String(i + 1), e.title ?? "", e.url]));
108
108
  return table(rows);
109
109
  }
110
+ function renderHelp(theme, color) {
111
+ const plain = !color || !!process.env.NO_COLOR || theme.name === "mono";
112
+ const accent = (s) => (plain ? s : theme.accent + s + theme.reset);
113
+ const lines = HELP.split("\n");
114
+ return [
115
+ renderBanner(VERSION, theme, color),
116
+ "",
117
+ ...lines.map((l) => (l.startsWith("Usage:") ? accent(l) : l)),
118
+ ].join("\n");
119
+ }
110
120
  export async function main(argv) {
111
121
  let values;
112
122
  let positionals;
@@ -173,7 +183,7 @@ export async function main(argv) {
173
183
  return 2;
174
184
  }
175
185
  if (values.help)
176
- return void process.stdout.write(HELP + "\n"), 0;
186
+ return void process.stdout.write(renderHelp(pickTheme(values.theme), !values["no-color"] && shouldUseColor()) + "\n"), 0;
177
187
  if (values.version)
178
188
  return void process.stdout.write(VERSION + "\n"), 0;
179
189
  let theme;
@@ -185,8 +195,8 @@ export async function main(argv) {
185
195
  return 2;
186
196
  }
187
197
  const machineMode = values.quiet || values["dump-json"] || values["get-url"] || values.simulate || values["progress-json"];
188
- const interactiveStdout = process.stdout.isTTY && !machineMode && !values["list-extractors"];
189
- let showBanner = false;
198
+ const interactiveStdout = process.stdout.isTTY && !machineMode;
199
+ let showBanner = interactiveStdout;
190
200
  const log = new Logger({
191
201
  quiet: values.quiet,
192
202
  verbose: values.verbose,
@@ -241,7 +251,7 @@ export async function main(argv) {
241
251
  log.error(errMsg(e));
242
252
  return e instanceof TidepullError ? e.exitCode : 1;
243
253
  }
244
- showBanner = interactiveStdout && urls.length > 0;
254
+ showBanner = interactiveStdout && !values["list-extractors"];
245
255
  if (showBanner) {
246
256
  const pluginNames = plugins.map((p) => p.name);
247
257
  process.stderr.write(renderBanner(VERSION, theme, !values["no-color"] && shouldUseColor(), pluginNames) + "\n");
@@ -18,5 +18,6 @@ export declare function resolveItems(tp: Tidepull, source: string | Info | Playl
18
18
  export interface PullAllOptions extends Partial<DownloadOptions>, CallOptions {
19
19
  items?: string;
20
20
  onError?: (error: Error, item: PlaylistItem) => void;
21
+ concurrency?: number;
21
22
  }
22
23
  export declare function pullAll(tp: Tidepull, source: string, o?: PullAllOptions): Promise<string[]>;
package/dist/playlist.js CHANGED
@@ -58,19 +58,85 @@ export async function* resolveItems(tp, source, o = {}) {
58
58
  }
59
59
  }
60
60
  export async function pullAll(tp, source, o = {}) {
61
- const { items, onError, ...rest } = o;
62
- const files = [];
63
- for await (const item of resolveItems(tp, source, { items, signal: o.signal, trust: o.trust })) {
64
- try {
65
- if (item.error)
66
- throw item.error;
67
- files.push(await tp.download(item.info, { ...rest, fields: { ...rest.fields, ...playlistFields(item) } }));
68
- }
69
- catch (e) {
70
- if (o.signal?.aborted || !onError)
71
- throw e;
72
- onError(e, item);
61
+ const { items, onError, concurrency = 1, ...rest } = o;
62
+ const limit = concurrency === undefined ? 1 : Math.floor(concurrency);
63
+ if (limit !== concurrency || limit < 1) {
64
+ throw new TidepullError(`Invalid playlist concurrency ${concurrency} (an integer >= 1)`, 2);
65
+ }
66
+ if (limit === 1) {
67
+ const files = [];
68
+ for await (const item of resolveItems(tp, source, { items, signal: o.signal, trust: o.trust })) {
69
+ try {
70
+ if (item.error)
71
+ throw item.error;
72
+ files.push(await tp.download(item.info, { ...rest, fields: { ...rest.fields, ...playlistFields(item) } }));
73
+ }
74
+ catch (e) {
75
+ if (o.signal?.aborted || !onError)
76
+ throw e;
77
+ onError(e, item);
78
+ }
73
79
  }
80
+ return files;
74
81
  }
75
- return files;
82
+ const iterator = resolveItems(tp, source, { items, signal: o.signal, trust: o.trust });
83
+ const slots = [];
84
+ let nextSlot = 0;
85
+ let firstFailure;
86
+ let inFlight = 0;
87
+ let exhausted = false;
88
+ await new Promise((resolve, reject) => {
89
+ let settled = false;
90
+ const finish = () => {
91
+ if (settled)
92
+ return;
93
+ settled = true;
94
+ if (firstFailure)
95
+ reject(firstFailure);
96
+ else
97
+ resolve();
98
+ };
99
+ const launch = (item) => {
100
+ const slot = nextSlot++;
101
+ inFlight++;
102
+ void (async () => {
103
+ try {
104
+ if (item.error)
105
+ throw item.error;
106
+ slots[slot] = await tp.download(item.info, { ...rest, fields: { ...rest.fields, ...playlistFields(item) } });
107
+ }
108
+ catch (e) {
109
+ if (o.signal?.aborted || !onError)
110
+ firstFailure ??= e;
111
+ else
112
+ onError(e, item);
113
+ }
114
+ finally {
115
+ inFlight--;
116
+ pump();
117
+ }
118
+ })();
119
+ };
120
+ const pump = () => {
121
+ void (async () => {
122
+ try {
123
+ while (!exhausted && !firstFailure && !o.signal?.aborted && inFlight < limit) {
124
+ const step = await iterator.next();
125
+ if (step.done) {
126
+ exhausted = true;
127
+ break;
128
+ }
129
+ launch(step.value);
130
+ }
131
+ }
132
+ catch (e) {
133
+ firstFailure ??= e;
134
+ }
135
+ if (inFlight === 0 && (exhausted || firstFailure || o.signal?.aborted))
136
+ finish();
137
+ })();
138
+ };
139
+ pump();
140
+ });
141
+ return slots.filter((f) => f !== undefined);
76
142
  }
package/dist/ui/banner.js CHANGED
@@ -3,15 +3,37 @@ const visible = (s) => s.replace(/\x1b\[[0-9;]*m/g, "").length;
3
3
  function termWidth() {
4
4
  return process.stdout.columns ?? 80;
5
5
  }
6
- function infoRows(version, theme, plain, plugins) {
6
+ function infoRows(version, theme, plain, plugins, valueRoom) {
7
7
  const dim = (s) => (plain ? s : theme.dim + s + theme.reset);
8
8
  const ok = (s) => (plain ? s : theme.success + s + theme.reset);
9
9
  const tag = (s) => (plain ? s : theme.tag + s + theme.reset);
10
+ const fit = (s, plain_s) => {
11
+ if (visible(plain_s) <= valueRoom)
12
+ return s;
13
+ if (plugins.length > 1 && plain_s === plugins.join(", ")) {
14
+ let names = plugins;
15
+ do {
16
+ names = names.slice(0, -1);
17
+ const body = names.join(", ") + ` +${plugins.length - names.length}`;
18
+ if (visible(body) <= valueRoom)
19
+ return tag(body);
20
+ } while (names.length > 1);
21
+ const one = plugins[0].slice(0, Math.max(1, valueRoom - 1)) + "\u2026";
22
+ return tag(one);
23
+ }
24
+ return s;
25
+ };
10
26
  const rows = [
11
27
  ["media downloader", ok(`v${version}`)],
12
28
  ["stdout", ok("data only")],
13
29
  ];
14
- rows.push(["plugins", plugins.length ? tag(plugins.join(", ")) : dim("none")]);
30
+ const pluginValue = plugins.length ? plugins.join(", ") : "";
31
+ if (plugins.length) {
32
+ rows.push(["plugins", fit(tag(pluginValue), pluginValue)]);
33
+ }
34
+ else {
35
+ rows.push(["plugins", dim("none")]);
36
+ }
15
37
  return rows;
16
38
  }
17
39
  export function renderBanner(version, theme, color = true, plugins = [], width) {
@@ -19,10 +41,22 @@ export function renderBanner(version, theme, color = true, plugins = [], width)
19
41
  const paint = (c, s) => (plain ? s : c + s + theme.reset);
20
42
  const accent = (s) => paint(theme.accent, s);
21
43
  const dim = (s) => paint(theme.dim, s);
22
- const w = Math.max(24, Math.min(width ?? termWidth(), 60));
23
- const rows = infoRows(version, theme, plain, plugins);
24
- const LABEL = Math.max(...rows.map(([k]) => k.length)) + 2;
44
+ const w = Math.min(width ?? termWidth(), 60);
25
45
  const inner = w - 2;
46
+ const LABEL = 17;
47
+ if (inner - 2 - LABEL < 10) {
48
+ const full = `${GLYPH} tidepull ${version}`;
49
+ const title = visible(full) > inner - 5
50
+ ? full.slice(0, Math.max(1, inner - 6)) + "\u2026"
51
+ : full;
52
+ const dash = "\u2500";
53
+ const fill = Math.max(1, inner - visible(title) - 4);
54
+ return [
55
+ accent("\u250c\u2500" + " ") + title + " " + accent(" " + dash.repeat(fill) + "\u2510"),
56
+ accent("\u2514" + dash.repeat(inner) + "\u2518"),
57
+ ].join("\n");
58
+ }
59
+ const rows = infoRows(version, theme, plain, plugins, inner - 2 - LABEL);
26
60
  const lines = [];
27
61
  const title = `${GLYPH} tidepull` + (plain ? ` v${version}` : ` ${paint(theme.dim, `v${version}`)}`);
28
62
  const shownTitle = visible(title) > inner - 4 ? `${GLYPH} tidepull` : title;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boy-offi9-inc/tidepull",
3
- "version": "0.8.0",
3
+ "version": "0.9.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",