diff --git a/src/manifests/handlers/dash/index.ts b/src/manifests/handlers/dash/index.ts index 0ec8784..8bbdf1f 100644 --- a/src/manifests/handlers/dash/index.ts +++ b/src/manifests/handlers/dash/index.ts @@ -9,6 +9,7 @@ export default async function dashHandler(event: ALBEvent): Promise { * #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)) { @@ -19,8 +20,9 @@ export default async function dashHandler(event: ALBEvent): Promise { } 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, @@ -28,13 +30,19 @@ export default async function dashHandler(event: ALBEvent): Promise { }); } 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: { @@ -45,6 +53,7 @@ export default async function dashHandler(event: ALBEvent): Promise { body: proxyManifest }; } catch (err) { + console.error(err); // for unexpected errors return generateErrorResponse({ status: 500, diff --git a/src/manifests/utils/dashManifestUtils.ts b/src/manifests/utils/dashManifestUtils.ts index 8dee572..a4c0a63 100644 --- a/src/manifests/utils/dashManifestUtils.ts +++ b/src/manifests/utils/dashManifestUtils.ts @@ -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; diff --git a/src/segments/handlers/segment.ts b/src/segments/handlers/segment.ts index 13cace6..04fba46 100644 --- a/src/segments/handlers/segment.ts +++ b/src/segments/handlers/segment.ts @@ -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') && diff --git a/src/segments/routes/index.ts b/src/segments/routes/index.ts index 40206d9..15d86ce 100644 --- a/src/segments/routes/index.ts +++ b/src/segments/routes/index.ts @@ -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); @@ -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) => {