Skip to content

Commit 14657e5

Browse files
committed
api/stream: split types.js into proxy.js and ffmpeg.js
1 parent aa376d7 commit 14657e5

File tree

3 files changed

+50
-46
lines changed

3 files changed

+50
-46
lines changed
Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import ffmpeg from "ffmpeg-static";
22
import { spawn } from "child_process";
3-
import { Agent, request } from "undici";
43
import { create as contentDisposition } from "content-disposition-header";
54

65
import { env } from "../config.js";
76
import { destroyInternalStream } from "./manage.js";
87
import { hlsExceptions } from "../processing/service-config.js";
9-
import { getHeaders, closeRequest, closeResponse, pipe, estimateTunnelLength, estimateAudioMultiplier } from "./shared.js";
8+
import { closeResponse, pipe, estimateTunnelLength, estimateAudioMultiplier } from "./shared.js";
109

1110
const metadataTags = [
1211
"album",
@@ -55,44 +54,6 @@ const getCommand = (args) => {
5554
return [ffmpeg, args]
5655
}
5756

58-
const defaultAgent = new Agent();
59-
60-
const proxy = async (streamInfo, res) => {
61-
const abortController = new AbortController();
62-
const shutdown = () => (
63-
closeRequest(abortController),
64-
closeResponse(res),
65-
destroyInternalStream(streamInfo.urls)
66-
);
67-
68-
try {
69-
res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
70-
res.setHeader('Content-disposition', contentDisposition(streamInfo.filename));
71-
72-
const { body: stream, headers, statusCode } = await request(streamInfo.urls, {
73-
headers: {
74-
...getHeaders(streamInfo.service),
75-
Range: streamInfo.range
76-
},
77-
signal: abortController.signal,
78-
maxRedirections: 16,
79-
dispatcher: defaultAgent,
80-
});
81-
82-
res.status(statusCode);
83-
84-
for (const headerName of ['accept-ranges', 'content-type', 'content-length']) {
85-
if (headers[headerName]) {
86-
res.setHeader(headerName, headers[headerName]);
87-
}
88-
}
89-
90-
pipe(stream, res, shutdown);
91-
} catch {
92-
shutdown();
93-
}
94-
}
95-
9657
const render = async (res, streamInfo, ffargs, multiplier) => {
9758
let process;
9859
const urls = Array.isArray(streamInfo.urls) ? streamInfo.urls : [streamInfo.urls];
@@ -245,7 +206,6 @@ const convertGif = async (streamInfo, res) => {
245206
}
246207

247208
export default {
248-
proxy,
249209
remux,
250210
convertAudio,
251211
convertGif,

api/src/stream/proxy.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Agent, request } from "undici";
2+
import { create as contentDisposition } from "content-disposition-header";
3+
4+
import { destroyInternalStream } from "./manage.js";
5+
import { getHeaders, closeRequest, closeResponse, pipe } from "./shared.js";
6+
7+
const defaultAgent = new Agent();
8+
9+
export default async function (streamInfo, res) {
10+
const abortController = new AbortController();
11+
const shutdown = () => (
12+
closeRequest(abortController),
13+
closeResponse(res),
14+
destroyInternalStream(streamInfo.urls)
15+
);
16+
17+
try {
18+
res.setHeader('Cross-Origin-Resource-Policy', 'cross-origin');
19+
res.setHeader('Content-disposition', contentDisposition(streamInfo.filename));
20+
21+
const { body: stream, headers, statusCode } = await request(streamInfo.urls, {
22+
headers: {
23+
...getHeaders(streamInfo.service),
24+
Range: streamInfo.range
25+
},
26+
signal: abortController.signal,
27+
maxRedirections: 16,
28+
dispatcher: defaultAgent,
29+
});
30+
31+
res.status(statusCode);
32+
33+
for (const headerName of ['accept-ranges', 'content-type', 'content-length']) {
34+
if (headers[headerName]) {
35+
res.setHeader(headerName, headers[headerName]);
36+
}
37+
}
38+
39+
pipe(stream, res, shutdown);
40+
} catch {
41+
shutdown();
42+
}
43+
}

api/src/stream/stream.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import stream from "./types.js";
1+
import proxy from "./proxy.js";
2+
import ffmpeg from "./ffmpeg.js";
23

34
import { closeResponse } from "./shared.js";
45
import { internalStream } from "./internal.js";
@@ -7,21 +8,21 @@ export default async function(res, streamInfo) {
78
try {
89
switch (streamInfo.type) {
910
case "proxy":
10-
return await stream.proxy(streamInfo, res);
11+
return await proxy(streamInfo, res);
1112

1213
case "internal":
1314
return await internalStream(streamInfo.data, res);
1415

1516
case "merge":
1617
case "remux":
1718
case "mute":
18-
return await stream.remux(streamInfo, res);
19+
return await ffmpeg.remux(streamInfo, res);
1920

2021
case "audio":
21-
return await stream.convertAudio(streamInfo, res);
22+
return await ffmpeg.convertAudio(streamInfo, res);
2223

2324
case "gif":
24-
return await stream.convertGif(streamInfo, res);
25+
return await ffmpeg.convertGif(streamInfo, res);
2526
}
2627

2728
closeResponse(res);

0 commit comments

Comments
 (0)