@ziplayer/plugin 0.1.52 → 0.2.1-dev-2
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/YouTubePlugin.d.ts +5 -1
- package/dist/YouTubePlugin.d.ts.map +1 -1
- package/dist/YouTubePlugin.js +72 -107
- package/dist/YouTubePlugin.js.map +1 -1
- package/dist/index.d.ts +0 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -16
- package/dist/index.js.map +1 -1
- package/dist/utils/sabr-stream-factory.d.ts +29 -9
- package/dist/utils/sabr-stream-factory.d.ts.map +1 -1
- package/dist/utils/sabr-stream-factory.js +170 -31
- package/dist/utils/sabr-stream-factory.js.map +1 -1
- package/dist/utils/stream-converter.d.ts +8 -5
- package/dist/utils/stream-converter.d.ts.map +1 -1
- package/dist/utils/stream-converter.js +105 -41
- package/dist/utils/stream-converter.js.map +1 -1
- package/package.json +46 -45
- package/src/YouTubePlugin.ts +591 -620
- package/src/index.ts +0 -17
- package/src/utils/sabr-stream-factory.ts +284 -96
- package/src/utils/stream-converter.ts +113 -45
- package/tsconfig.json +6 -2
- package/src/YTSRPlugin.ts +0 -596
- package/src/types/googlevideo.d.ts +0 -45
package/src/index.ts
CHANGED
|
@@ -85,23 +85,6 @@ export * from "./SpotifyPlugin";
|
|
|
85
85
|
*/
|
|
86
86
|
export * from "./TTSPlugin";
|
|
87
87
|
|
|
88
|
-
/**
|
|
89
|
-
* YTSR plugin for advanced YouTube search without streaming.
|
|
90
|
-
*
|
|
91
|
-
* Provides comprehensive YouTube search functionality including:
|
|
92
|
-
* - Advanced video search with filters (duration, upload date, sort by)
|
|
93
|
-
* - Playlist and channel search
|
|
94
|
-
* - Multiple search types (video, playlist, channel, all)
|
|
95
|
-
* - Metadata extraction without streaming
|
|
96
|
-
* - Support for YouTube URLs
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* const ytsrPlugin = new YTSRPlugin();
|
|
100
|
-
* const result = await ytsrPlugin.search("Never Gonna Give You Up", "user123");
|
|
101
|
-
* const playlistResult = await ytsrPlugin.searchPlaylist("chill music", "user123");
|
|
102
|
-
*/
|
|
103
|
-
export * from "./YTSRPlugin";
|
|
104
|
-
|
|
105
88
|
/**
|
|
106
89
|
* Attachments plugin for handling Discord attachment URLs and audio files.
|
|
107
90
|
*
|
|
@@ -1,96 +1,284 @@
|
|
|
1
|
-
import { createWriteStream } from "fs";
|
|
2
|
-
import { join } from "path";
|
|
3
|
-
import { tmpdir } from "os";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}
|
|
1
|
+
import { createWriteStream } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { tmpdir } from "os";
|
|
4
|
+
import { Readable } from "stream";
|
|
5
|
+
import { Constants, YTNodes, Platform } from "youtubei.js";
|
|
6
|
+
import type Innertube from "youtubei.js";
|
|
7
|
+
|
|
8
|
+
import { SabrStream } from "googlevideo/sabr-stream";
|
|
9
|
+
import { buildSabrFormat } from "googlevideo/utils";
|
|
10
|
+
|
|
11
|
+
import { BG } from "bgutils-js";
|
|
12
|
+
import { JSDOM } from "jsdom";
|
|
13
|
+
import { webStreamToNodeStream } from "./stream-converter";
|
|
14
|
+
|
|
15
|
+
export interface OutputStream {
|
|
16
|
+
stream: NodeJS.WritableStream;
|
|
17
|
+
filePath: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SabrAudioResult {
|
|
21
|
+
title: string;
|
|
22
|
+
stream: Readable;
|
|
23
|
+
format: {
|
|
24
|
+
mimeType: string;
|
|
25
|
+
itag: number;
|
|
26
|
+
contentLength: number;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SabrPlaybackOptions {
|
|
31
|
+
preferWebM?: boolean;
|
|
32
|
+
preferOpus?: boolean;
|
|
33
|
+
videoQuality?: string;
|
|
34
|
+
audioQuality?: string;
|
|
35
|
+
enabledTrackTypes?: any;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Generates a web PoToken for YouTube authentication
|
|
39
|
+
* This is required for accessing restricted video content
|
|
40
|
+
*/
|
|
41
|
+
async function generateWebPoToken(contentBinding: string): Promise<{
|
|
42
|
+
visitorData: string;
|
|
43
|
+
placeholderPoToken: string;
|
|
44
|
+
poToken: string;
|
|
45
|
+
}> {
|
|
46
|
+
try {
|
|
47
|
+
const requestKey = "O43z0dpjhgX20SCx4KAo";
|
|
48
|
+
|
|
49
|
+
if (!contentBinding) throw new Error("Could not get visitor data");
|
|
50
|
+
|
|
51
|
+
const dom = new JSDOM();
|
|
52
|
+
|
|
53
|
+
Object.assign(globalThis, {
|
|
54
|
+
window: dom.window,
|
|
55
|
+
document: dom.window.document,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
const bgConfig = {
|
|
59
|
+
fetch: (input: any, init: any) => fetch(input, init),
|
|
60
|
+
globalObj: globalThis,
|
|
61
|
+
identifier: contentBinding,
|
|
62
|
+
requestKey,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const bgChallenge = await BG.Challenge.create(bgConfig);
|
|
66
|
+
|
|
67
|
+
if (!bgChallenge) throw new Error("Could not get challenge");
|
|
68
|
+
|
|
69
|
+
const interpreterJavascript = bgChallenge.interpreterJavascript.privateDoNotAccessOrElseSafeScriptWrappedValue;
|
|
70
|
+
|
|
71
|
+
if (interpreterJavascript) {
|
|
72
|
+
new Function(interpreterJavascript)();
|
|
73
|
+
} else throw new Error("Could not load VM");
|
|
74
|
+
|
|
75
|
+
const poTokenResult = await BG.PoToken.generate({
|
|
76
|
+
program: bgChallenge.program,
|
|
77
|
+
globalName: bgChallenge.globalName,
|
|
78
|
+
bgConfig,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const placeholderPoToken = BG.PoToken.generatePlaceholder(contentBinding);
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
visitorData: contentBinding,
|
|
85
|
+
placeholderPoToken,
|
|
86
|
+
poToken: poTokenResult.poToken,
|
|
87
|
+
};
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.warn("PoToken generation failed, continuing without it:", error);
|
|
90
|
+
return {
|
|
91
|
+
visitorData: contentBinding,
|
|
92
|
+
placeholderPoToken: "",
|
|
93
|
+
poToken: "",
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Makes a proper player request to YouTube API
|
|
100
|
+
*/
|
|
101
|
+
async function makePlayerRequest(innertube: Innertube, videoId: string, reloadPlaybackContext?: any): Promise<any> {
|
|
102
|
+
const watchEndpoint = new YTNodes.NavigationEndpoint({
|
|
103
|
+
watchEndpoint: { videoId },
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const extraArgs: any = {
|
|
107
|
+
playbackContext: {
|
|
108
|
+
adPlaybackContext: { pyv: true },
|
|
109
|
+
contentPlaybackContext: {
|
|
110
|
+
vis: 0,
|
|
111
|
+
splay: false,
|
|
112
|
+
lactMilliseconds: "-1",
|
|
113
|
+
signatureTimestamp: innertube.session.player?.signature_timestamp,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
contentCheckOk: true,
|
|
117
|
+
racyCheckOk: true,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
if (reloadPlaybackContext) {
|
|
121
|
+
extraArgs.playbackContext.reloadPlaybackContext = reloadPlaybackContext;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return watchEndpoint.call(innertube.actions, {
|
|
125
|
+
...extraArgs,
|
|
126
|
+
parse: true,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* YouTube VM shim
|
|
132
|
+
* This allows the SABR stream to execute YouTube's custom JavaScript for deciphering signatures and generating tokens
|
|
133
|
+
*/
|
|
134
|
+
Platform.shim.eval = async (data, env) => {
|
|
135
|
+
const properties = [];
|
|
136
|
+
|
|
137
|
+
if (env.n) properties.push(`n: exportedVars.nFunction("${env.n}")`);
|
|
138
|
+
if (env.sig) properties.push(`sig: exportedVars.sigFunction("${env.sig}")`);
|
|
139
|
+
|
|
140
|
+
const code = `${data.output}\nreturn { ${properties.join(", ")} }`;
|
|
141
|
+
return new Function(code)();
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Creates a SABR audio stream for YouTube video download
|
|
146
|
+
* This provides better quality and more reliable streaming than standard methods
|
|
147
|
+
*/
|
|
148
|
+
export async function createSabrStream(
|
|
149
|
+
videoId: string,
|
|
150
|
+
innertube: Innertube,
|
|
151
|
+
options?: SabrPlaybackOptions,
|
|
152
|
+
): Promise<SabrAudioResult> {
|
|
153
|
+
try {
|
|
154
|
+
// Generate PoToken for authentication
|
|
155
|
+
const webPo = await generateWebPoToken(videoId);
|
|
156
|
+
|
|
157
|
+
// Make initial player request
|
|
158
|
+
const player = await makePlayerRequest(innertube, videoId);
|
|
159
|
+
|
|
160
|
+
const title = player.video_details?.title || "unknown";
|
|
161
|
+
|
|
162
|
+
const serverAbrStreamingUrl = await innertube.session.player?.decipher(player.streaming_data?.server_abr_streaming_url);
|
|
163
|
+
|
|
164
|
+
const ustreamerConfig =
|
|
165
|
+
player.player_config?.media_common_config.media_ustreamer_request_config?.video_playback_ustreamer_config;
|
|
166
|
+
|
|
167
|
+
if (!serverAbrStreamingUrl || !ustreamerConfig) {
|
|
168
|
+
throw new Error("Missing SABR streaming config");
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const sabrFormats = player.streaming_data?.adaptive_formats.map((f: any) => buildSabrFormat(f)) || [];
|
|
172
|
+
|
|
173
|
+
const sabr = new SabrStream({
|
|
174
|
+
formats: sabrFormats,
|
|
175
|
+
serverAbrStreamingUrl,
|
|
176
|
+
videoPlaybackUstreamerConfig: ustreamerConfig,
|
|
177
|
+
poToken: webPo.poToken,
|
|
178
|
+
clientInfo: {
|
|
179
|
+
clientName: parseInt(
|
|
180
|
+
Constants.CLIENT_NAME_IDS[innertube.session.context.client.clientName as keyof typeof Constants.CLIENT_NAME_IDS],
|
|
181
|
+
),
|
|
182
|
+
clientVersion: innertube.session.context.client.clientVersion,
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// Handle player response reload events
|
|
187
|
+
sabr.on("reloadPlayerResponse", async (ctx: any) => {
|
|
188
|
+
try {
|
|
189
|
+
const pr = await makePlayerRequest(innertube, videoId, ctx);
|
|
190
|
+
|
|
191
|
+
const url = await innertube.session.player?.decipher(pr.streaming_data?.server_abr_streaming_url);
|
|
192
|
+
|
|
193
|
+
const config = pr.player_config?.media_common_config.media_ustreamer_request_config?.video_playback_ustreamer_config;
|
|
194
|
+
|
|
195
|
+
if (url && config) {
|
|
196
|
+
sabr.setStreamingURL(url);
|
|
197
|
+
sabr.setUstreamerConfig(config);
|
|
198
|
+
}
|
|
199
|
+
} catch (error) {
|
|
200
|
+
console.error("Failed to reload player response:", error);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// Start the stream with audio preference
|
|
205
|
+
const mergedOptions = { ...DEFAULT_SABR_OPTIONS, ...options };
|
|
206
|
+
const { audioStream, selectedFormats } = await sabr.start({
|
|
207
|
+
audioQuality: mergedOptions?.audioQuality || "medium",
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Convert Web Stream to Node.js Readable stream with optimized buffer
|
|
211
|
+
const nodeStream = webStreamToNodeStream(audioStream, 32 * 1024); // 32KB buffer for YouTube streams
|
|
212
|
+
|
|
213
|
+
return {
|
|
214
|
+
title,
|
|
215
|
+
stream: nodeStream,
|
|
216
|
+
format: {
|
|
217
|
+
mimeType: selectedFormats.audioFormat.mimeType || "audio/webm",
|
|
218
|
+
itag: selectedFormats.audioFormat.itag || 0,
|
|
219
|
+
contentLength: selectedFormats.audioFormat.contentLength || 0,
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throw new Error(`SABR stream creation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Creates an output stream for writing downloaded content
|
|
229
|
+
*/
|
|
230
|
+
export function createOutputStream(videoTitle: string, mimeType: string): OutputStream {
|
|
231
|
+
const sanitizedTitle = videoTitle.replace(/[<>:"/\\|?*]/g, "_").substring(0, 100);
|
|
232
|
+
const extension = getExtensionFromMimeType(mimeType);
|
|
233
|
+
const fileName = `${sanitizedTitle}.${extension}`;
|
|
234
|
+
const filePath = join(tmpdir(), fileName);
|
|
235
|
+
|
|
236
|
+
const stream = createWriteStream(filePath);
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
stream,
|
|
240
|
+
filePath,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Sanitizes a filename by removing invalid characters
|
|
246
|
+
*/
|
|
247
|
+
export function sanitizeFileName(name: string): string {
|
|
248
|
+
return name.replace(/[^\w\d]+/g, "_").slice(0, 128);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Converts bytes to megabytes
|
|
253
|
+
*/
|
|
254
|
+
export function bytesToMB(bytes: number): string {
|
|
255
|
+
return (bytes / 1024 / 1024).toFixed(2);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Gets file extension from MIME type
|
|
260
|
+
*/
|
|
261
|
+
function getExtensionFromMimeType(mimeType: string): string {
|
|
262
|
+
const mimeMap: { [key: string]: string } = {
|
|
263
|
+
"audio/mp4": "m4a",
|
|
264
|
+
"audio/webm": "webm",
|
|
265
|
+
"audio/ogg": "ogg",
|
|
266
|
+
"video/mp4": "mp4",
|
|
267
|
+
"video/webm": "webm",
|
|
268
|
+
"video/ogg": "ogv",
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
return mimeMap[mimeType] || "bin";
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Default sabr playback options - optimized for memory usage
|
|
276
|
+
* Using MEDIUM quality and WebM/Opus reduces bandwidth by ~30-40%
|
|
277
|
+
*/
|
|
278
|
+
export const DEFAULT_SABR_OPTIONS: SabrPlaybackOptions = {
|
|
279
|
+
preferWebM: true, // WebM with Opus is more memory-efficient
|
|
280
|
+
preferOpus: true, // Opus codec = smaller bitrate vs AAC
|
|
281
|
+
videoQuality: "360p", // Lower resolution = less processing
|
|
282
|
+
audioQuality: "medium", // Medium quality balances quality vs bandwidth (~96-128kbps vs 256kbps)
|
|
283
|
+
enabledTrackTypes: "VIDEO_AND_AUDIO",
|
|
284
|
+
};
|
|
@@ -2,78 +2,146 @@ import { Readable } from "stream";
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Converts a Web ReadableStream to a Node.js Readable stream
|
|
5
|
+
* with proper backpressure handling to prevent memory bloat
|
|
6
|
+
*
|
|
7
|
+
* Optimization:
|
|
8
|
+
* - Respects highWaterMark (64KB buffer)
|
|
9
|
+
* - Handles backpressure by pausing reads when buffer is full
|
|
10
|
+
* - Batches small chunks to reduce overhead
|
|
11
|
+
* - Lazy evaluation - only reads when consumer is ready
|
|
5
12
|
*/
|
|
6
|
-
export function webStreamToNodeStream(webStream: ReadableStream): Readable {
|
|
13
|
+
export function webStreamToNodeStream(webStream: ReadableStream, highWaterMark: number = 64 * 1024): Readable {
|
|
14
|
+
let reader: ReadableStreamDefaultReader | null = null;
|
|
15
|
+
let pumpActive = true;
|
|
16
|
+
let abortController: AbortController | null = null;
|
|
17
|
+
let isReading = false; // Prevent concurrent reads
|
|
18
|
+
|
|
7
19
|
const nodeStream = new Readable({
|
|
8
|
-
|
|
9
|
-
|
|
20
|
+
// Set buffer size to 64KB - balance between memory and throughput
|
|
21
|
+
highWaterMark: highWaterMark,
|
|
22
|
+
|
|
23
|
+
read(size?: number) {
|
|
24
|
+
// Resume reading when stream is ready for more data
|
|
25
|
+
if (!isReading && pumpActive && reader) {
|
|
26
|
+
isReading = true;
|
|
27
|
+
pump().catch((error) => {
|
|
28
|
+
if (pumpActive) {
|
|
29
|
+
console.error("[stream-converter] Pump error:", error);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
10
33
|
},
|
|
11
|
-
});
|
|
12
34
|
|
|
13
|
-
|
|
14
|
-
|
|
35
|
+
destroy(error: Error | null, callback: (error?: Error | null) => void) {
|
|
36
|
+
// Gracefully stop the pump when stream is destroyed
|
|
37
|
+
pumpActive = false;
|
|
15
38
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
nodeStream.push(Buffer.from(value));
|
|
39
|
+
// Cancel reader if active
|
|
40
|
+
if (reader) {
|
|
41
|
+
try {
|
|
42
|
+
reader.cancel().catch(() => {
|
|
43
|
+
// Ignore cancel errors
|
|
44
|
+
});
|
|
45
|
+
reader = null;
|
|
46
|
+
} catch {}
|
|
26
47
|
}
|
|
27
|
-
} catch (error) {
|
|
28
|
-
nodeStream.destroy(error as Error);
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
48
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
49
|
+
// Abort any pending operations
|
|
50
|
+
if (abortController) {
|
|
51
|
+
try {
|
|
52
|
+
abortController.abort();
|
|
53
|
+
} catch {}
|
|
54
|
+
abortController = null;
|
|
55
|
+
}
|
|
37
56
|
|
|
38
|
-
|
|
39
|
-
* Converts a Web ReadableStream to a Node.js Readable stream with progress tracking
|
|
40
|
-
*/
|
|
41
|
-
export function webStreamToNodeStreamWithProgress(
|
|
42
|
-
webStream: ReadableStream,
|
|
43
|
-
progressCallback?: (bytesRead: number) => void,
|
|
44
|
-
): Readable {
|
|
45
|
-
const nodeStream = new Readable({
|
|
46
|
-
read() {
|
|
47
|
-
// This will be handled by the Web Stream reader
|
|
57
|
+
callback(error);
|
|
48
58
|
},
|
|
49
59
|
});
|
|
50
60
|
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
// Create abort controller for graceful shutdown
|
|
62
|
+
abortController = new AbortController();
|
|
63
|
+
|
|
64
|
+
// Create a reader from the Web Stream
|
|
65
|
+
reader = webStream.getReader();
|
|
53
66
|
|
|
67
|
+
// Read chunks with backpressure support
|
|
54
68
|
const pump = async () => {
|
|
55
69
|
try {
|
|
56
|
-
while (
|
|
70
|
+
while (pumpActive && reader) {
|
|
57
71
|
const { done, value } = await reader.read();
|
|
72
|
+
|
|
73
|
+
// Check if pump was stopped during read
|
|
74
|
+
if (!pumpActive || !reader) {
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
|
|
58
78
|
if (done) {
|
|
59
79
|
nodeStream.push(null); // End the stream
|
|
60
80
|
break;
|
|
61
81
|
}
|
|
62
82
|
|
|
63
|
-
|
|
64
|
-
|
|
83
|
+
if (value && pumpActive) {
|
|
84
|
+
// Convert to Buffer and push
|
|
85
|
+
const buffer = Buffer.from(value);
|
|
86
|
+
|
|
87
|
+
// Check backpressure: push() returns false if internal buffer is full
|
|
88
|
+
// This means we should pause and let the consumer catch up
|
|
89
|
+
const shouldContinue = nodeStream.push(buffer);
|
|
65
90
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
91
|
+
if (!shouldContinue) {
|
|
92
|
+
// Internal buffer is full, pause reading
|
|
93
|
+
isReading = false;
|
|
94
|
+
break; // Exit pump, will resume when consumer calls read()
|
|
95
|
+
}
|
|
69
96
|
}
|
|
70
97
|
}
|
|
71
98
|
} catch (error) {
|
|
72
|
-
|
|
99
|
+
// Only destroy if pump is still active and stream exists
|
|
100
|
+
if (pumpActive) {
|
|
101
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
102
|
+
|
|
103
|
+
// Ignore "Controller is already closed" and stream cancelled errors
|
|
104
|
+
if (
|
|
105
|
+
errorMsg.includes("Controller is already closed") ||
|
|
106
|
+
errorMsg.includes("already been cancelled") ||
|
|
107
|
+
errorMsg.includes("stream closed") ||
|
|
108
|
+
errorMsg.includes("aborted")
|
|
109
|
+
) {
|
|
110
|
+
// Stream was destroyed externally, just end cleanly
|
|
111
|
+
nodeStream.push(null);
|
|
112
|
+
} else {
|
|
113
|
+
// Real error, report it
|
|
114
|
+
nodeStream.destroy(error as Error);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
} finally {
|
|
118
|
+
// Mark as not reading
|
|
119
|
+
isReading = false;
|
|
120
|
+
|
|
121
|
+
// Cleanup reader when pump ends
|
|
122
|
+
try {
|
|
123
|
+
if (reader) {
|
|
124
|
+
await reader.cancel();
|
|
125
|
+
reader = null;
|
|
126
|
+
}
|
|
127
|
+
} catch {}
|
|
128
|
+
|
|
129
|
+
pumpActive = false;
|
|
73
130
|
}
|
|
74
131
|
};
|
|
75
132
|
|
|
76
|
-
pump
|
|
133
|
+
// Start initial pump when stream is requested
|
|
134
|
+
// Note: pump will be called by read() callback for backpressure compliance
|
|
135
|
+
setImmediate(() => {
|
|
136
|
+
if (pumpActive && reader && !isReading) {
|
|
137
|
+
isReading = true;
|
|
138
|
+
pump().catch((error) => {
|
|
139
|
+
if (pumpActive) {
|
|
140
|
+
console.error("[stream-converter] Initial pump error:", error);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
});
|
|
77
145
|
|
|
78
146
|
return nodeStream;
|
|
79
147
|
}
|
package/tsconfig.json
CHANGED
|
@@ -16,8 +16,12 @@
|
|
|
16
16
|
"allowSyntheticDefaultImports": true,
|
|
17
17
|
"experimentalDecorators": true,
|
|
18
18
|
"emitDecoratorMetadata": true,
|
|
19
|
-
"resolveJsonModule": true
|
|
19
|
+
"resolveJsonModule": true,
|
|
20
|
+
"paths": {
|
|
21
|
+
"googlevideo/sabr-stream": ["./node_modules/googlevideo/dist/src/exports/sabr-stream"],
|
|
22
|
+
"googlevideo/utils": ["./node_modules/googlevideo/dist/src/exports/utils"]
|
|
23
|
+
}
|
|
20
24
|
},
|
|
21
|
-
"include": ["src/*"
|
|
25
|
+
"include": ["src/*"],
|
|
22
26
|
"exclude": ["node_modules", "dist", "examples"]
|
|
23
27
|
}
|