2024-04-27 13:51:12 +01:00
|
|
|
import { request } from 'undici';
|
|
|
|
import { Readable } from 'node:stream';
|
2024-06-23 16:37:02 +01:00
|
|
|
import { closeRequest, getHeaders, pipe } from './shared.js';
|
2024-06-07 12:08:20 +01:00
|
|
|
import { handleHlsPlaylist, isHlsRequest } from './internal-hls.js';
|
2024-04-27 12:00:45 +01:00
|
|
|
|
|
|
|
const CHUNK_SIZE = BigInt(8e6); // 8 MB
|
|
|
|
const min = (a, b) => a < b ? a : b;
|
|
|
|
|
|
|
|
async function* readChunks(streamInfo, size) {
|
|
|
|
let read = 0n;
|
|
|
|
while (read < size) {
|
|
|
|
if (streamInfo.controller.signal.aborted) {
|
|
|
|
throw new Error("controller aborted");
|
|
|
|
}
|
|
|
|
|
|
|
|
const chunk = await request(streamInfo.url, {
|
|
|
|
headers: {
|
|
|
|
...getHeaders('youtube'),
|
|
|
|
Range: `bytes=${read}-${read + CHUNK_SIZE}`
|
|
|
|
},
|
2024-05-12 17:35:52 +01:00
|
|
|
dispatcher: streamInfo.dispatcher,
|
2024-04-27 12:00:45 +01:00
|
|
|
signal: streamInfo.controller.signal
|
|
|
|
});
|
|
|
|
|
|
|
|
const expected = min(CHUNK_SIZE, size - read);
|
|
|
|
const received = BigInt(chunk.headers['content-length']);
|
|
|
|
|
|
|
|
if (received < expected / 2n) {
|
2024-06-23 16:37:02 +01:00
|
|
|
closeRequest(streamInfo.controller);
|
2024-04-27 12:00:45 +01:00
|
|
|
}
|
2024-08-03 09:47:13 +01:00
|
|
|
|
2024-04-27 12:00:45 +01:00
|
|
|
for await (const data of chunk.body) {
|
|
|
|
yield data;
|
|
|
|
}
|
|
|
|
|
|
|
|
read += received;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handleYoutubeStream(streamInfo, res) {
|
2024-06-22 11:57:30 +01:00
|
|
|
const { signal } = streamInfo.controller;
|
2024-06-23 16:37:02 +01:00
|
|
|
const cleanup = () => (res.end(), closeRequest(streamInfo.controller));
|
2024-06-22 11:57:30 +01:00
|
|
|
|
2024-04-27 12:00:45 +01:00
|
|
|
try {
|
|
|
|
const req = await fetch(streamInfo.url, {
|
|
|
|
headers: getHeaders('youtube'),
|
|
|
|
method: 'HEAD',
|
2024-05-12 17:35:52 +01:00
|
|
|
dispatcher: streamInfo.dispatcher,
|
2024-06-22 11:57:30 +01:00
|
|
|
signal
|
2024-04-27 12:00:45 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
streamInfo.url = req.url;
|
|
|
|
const size = BigInt(req.headers.get('content-length'));
|
|
|
|
|
2024-05-22 16:08:56 +01:00
|
|
|
if (req.status !== 200 || !size) {
|
2024-06-23 13:51:36 +01:00
|
|
|
return cleanup();
|
2024-05-22 16:08:56 +01:00
|
|
|
}
|
2024-04-27 12:00:45 +01:00
|
|
|
|
2024-06-22 11:57:30 +01:00
|
|
|
const generator = readChunks(streamInfo, size);
|
|
|
|
|
|
|
|
const abortGenerator = () => {
|
|
|
|
generator.return();
|
|
|
|
signal.removeEventListener('abort', abortGenerator);
|
|
|
|
}
|
|
|
|
|
|
|
|
signal.addEventListener('abort', abortGenerator);
|
2024-08-03 09:47:13 +01:00
|
|
|
|
2024-06-22 11:57:30 +01:00
|
|
|
const stream = Readable.from(generator);
|
2024-04-27 12:00:45 +01:00
|
|
|
|
2024-04-27 12:10:26 +01:00
|
|
|
for (const headerName of ['content-type', 'content-length']) {
|
|
|
|
const headerValue = req.headers.get(headerName);
|
|
|
|
if (headerValue) res.setHeader(headerName, headerValue);
|
|
|
|
}
|
|
|
|
|
2024-06-23 13:51:36 +01:00
|
|
|
pipe(stream, res, cleanup);
|
2024-04-27 12:00:45 +01:00
|
|
|
} catch {
|
2024-06-23 13:51:36 +01:00
|
|
|
cleanup();
|
2024-04-27 12:00:45 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-26 12:53:50 +01:00
|
|
|
|
2024-06-23 13:51:36 +01:00
|
|
|
async function handleGenericStream(streamInfo, res) {
|
|
|
|
const { signal } = streamInfo.controller;
|
2024-06-23 16:37:02 +01:00
|
|
|
const cleanup = () => res.end();
|
2024-04-27 12:00:45 +01:00
|
|
|
|
2024-04-26 12:53:50 +01:00
|
|
|
try {
|
|
|
|
const req = await request(streamInfo.url, {
|
2024-04-27 12:30:16 +01:00
|
|
|
headers: {
|
2024-07-06 18:15:15 +01:00
|
|
|
...Object.fromEntries(streamInfo.headers),
|
2024-04-27 12:30:16 +01:00
|
|
|
host: undefined
|
|
|
|
},
|
2024-05-12 17:35:52 +01:00
|
|
|
dispatcher: streamInfo.dispatcher,
|
2024-06-23 13:51:36 +01:00
|
|
|
signal,
|
2024-04-26 12:53:50 +01:00
|
|
|
maxRedirections: 16
|
|
|
|
});
|
|
|
|
|
|
|
|
res.status(req.statusCode);
|
2024-06-23 16:37:02 +01:00
|
|
|
req.body.on('error', () => {});
|
2024-04-26 12:53:50 +01:00
|
|
|
|
|
|
|
for (const [ name, value ] of Object.entries(req.headers))
|
|
|
|
res.setHeader(name, value)
|
|
|
|
|
|
|
|
if (req.statusCode < 200 || req.statusCode > 299)
|
2024-06-23 16:37:02 +01:00
|
|
|
return cleanup();
|
2024-04-26 12:53:50 +01:00
|
|
|
|
2024-06-07 12:08:20 +01:00
|
|
|
if (isHlsRequest(req)) {
|
|
|
|
await handleHlsPlaylist(streamInfo, req, res);
|
|
|
|
} else {
|
2024-06-23 13:51:36 +01:00
|
|
|
pipe(req.body, res, cleanup);
|
2024-06-07 12:08:20 +01:00
|
|
|
}
|
2024-04-26 12:53:50 +01:00
|
|
|
} catch {
|
2024-06-23 16:37:02 +01:00
|
|
|
closeRequest(streamInfo.controller);
|
2024-06-23 13:51:36 +01:00
|
|
|
cleanup();
|
2024-04-26 12:53:50 +01:00
|
|
|
}
|
2024-06-23 13:51:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function internalStream(streamInfo, res) {
|
|
|
|
if (streamInfo.service === 'youtube') {
|
|
|
|
return handleYoutubeStream(streamInfo, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return handleGenericStream(streamInfo, res);
|
2024-08-03 09:47:13 +01:00
|
|
|
}
|