@boy-offi9-inc/tidepull 0.8.1 → 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
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@boy-offi9-inc/tidepull",
3
- "version": "0.8.1",
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",