Skip to content

Commit

Permalink
WIP! feat: real chaos
Browse files Browse the repository at this point in the history
  • Loading branch information
bwallberg committed Aug 30, 2024
1 parent e3c81b4 commit 2b829d0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/manifests/handlers/dash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default async function dashHandler(event: ALBEvent): Promise<ALBResult> {
* #2 - const originalManifest = await fetch(originalUrl);
* #3 - create proxy manifest and return response from it
*/
event.queryStringParameters.url = decodeURIComponent(event.queryStringParameters.url);
const { url } = event.queryStringParameters;

if (!url || !isValidUrl(url)) {
Expand All @@ -19,22 +20,29 @@ export default async function dashHandler(event: ALBEvent): Promise<ALBResult> {
}

try {
const originalDashManifestResponse = await fetch(url);
const responseCopy = originalDashManifestResponse.clone();
console.log(decodeURIComponent(url));
const originalDashManifestResponse = await fetch(decodeURIComponent(url));
console.log('bwallberg org🙌');
if (!originalDashManifestResponse.ok) {
return generateErrorResponse({
status: originalDashManifestResponse.status,
message: 'Unsuccessful Source Manifest fetch'
});
}
const reqQueryParams = new URLSearchParams(event.queryStringParameters);
const text = await responseCopy.text();

console.log("bwallberg get text")
const text = await originalDashManifestResponse.text();
console.log("bwallberg got text")
const dashUtils = dashManifestUtils();
console.log("bwallberg got utils")
const proxyManifest = dashUtils.createProxyDASHManifest(
text,
reqQueryParams
);

console.log("bwallberg wat")

return {
statusCode: 200,
headers: {
Expand All @@ -45,6 +53,7 @@ export default async function dashHandler(event: ALBEvent): Promise<ALBResult> {
body: proxyManifest
};
} catch (err) {
console.error(err);
// for unexpected errors
return generateErrorResponse({
status: 500,
Expand Down
6 changes: 5 additions & 1 deletion src/manifests/utils/dashManifestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ export default function (): DASHManifestTools {
if (!baseUrl) {
baseUrl = originalUrlQuery.get('url');
} else if (!baseUrl.match(/^http/)) {
baseUrl = new URL(baseUrl, originalUrlQuery.get('url')).href;
baseUrl = new URL(
baseUrl,
decodeURIComponent(originalUrlQuery.get('url'))
).href;
console.log('new base', baseUrl);
}
const absoluteInitUrl = new URL(initUrl, baseUrl).href;
segmentTemplate.$.initialization = absoluteInitUrl;
Expand Down
26 changes: 22 additions & 4 deletions src/segments/handlers/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,30 @@ export default async function segmentHandler(
console.log(`Timing out ${query.url}`);
return;
}
// apply Delay

if (allSegmentCorr.get('delay')) {
const delay = Number(allSegmentCorr.get('delay').fields?.ms);
console.log(`Applying ${delay}ms delay to ${query.url}`);
await sleep(delay);
console.log(`Corrupt ${query.url}`);
const request = await fetch(query.url);
const response = await request.arrayBuffer();
return {
statusCode: 200,
headers: {
'Content-Type': request.headers.get('Content-Type'),
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type, Origin'
},
body: Buffer.from(
response.slice(0, response.byteLength / 2)
) as unknown as string
};
}

// apply Delay
// if (allSegmentCorr.get('delay')) {
// const delay = Number(allSegmentCorr.get('delay').fields?.ms);
// console.log(`Applying ${delay}ms delay to ${query.url}`);
// await sleep(delay);
// }
// apply Status Code
if (
allSegmentCorr.get('statusCode') &&
Expand Down
4 changes: 4 additions & 0 deletions src/segments/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { composeALBEvent, handleOptionsRequest } from '../../shared/utils';
import { SEGMENTS_PROXY_SEGMENT } from '../constants';

export default async function segmentRoutes(fastify: FastifyInstance) {
fastify.head('/*', async (req, res) => {
res.code(200).send();
});
fastify.get(SEGMENTS_PROXY_SEGMENT, async (req, res) => {
const event = await composeALBEvent(req.method, req.url, req.headers);
const response = await segmentHandler(event);
Expand All @@ -18,6 +21,7 @@ export default async function segmentRoutes(fastify: FastifyInstance) {
res.redirect(302, response.headers.Location as string);
return;
}
response.headers['Accept-Ranges'] = 'bytes';
res.code(response.statusCode).headers(response.headers).send(response.body);
});
fastify.options('/*', async (req, res) => {
Expand Down

0 comments on commit 2b829d0

Please sign in to comment.