Skip to content

Commit

Permalink
Add support for transcode uri
Browse files Browse the repository at this point in the history
  • Loading branch information
kookster committed Apr 20, 2024
1 parent 453f05c commit 5f1eb08
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
43 changes: 26 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ function handler(event) {
}

// just kick out invalid looking paths
// either: /podcast_id/episode_guid/digest/filename.mp3
// or: /podcast_id/feed_id/episode_guid/digest/filename.mp3
if (parts.length !== 4 && parts.length !== 5) {

// /podcast_id/episode_guid/digest/filename.mp3
// /podcast_id/feed_id/episode_guid/digest/filename.mp3
// /t/a/path/<file>/<format>.<extension>
// /t/<host>/[<path>/]<file>/<format>.<extension>
if (parts[0] === 't') {
if (parts.length < 4) {
return { statusCode: 404, statusDescription: 'Not found. Nope.' };
}
} else if (parts.length !== 4 && parts.length !== 5) {
return { statusCode: 404, statusDescription: 'Not found. Like, ever.' };
}

Expand All @@ -42,22 +49,24 @@ function handler(event) {

// TODO: check/require a signature query param (signing your path/exp/le/force)
// TEMPORARY: just kick out short/fake looking digests (2nd to last)
var digest = parts[parts.length - 2];
if (digest.length < 20 && digest !== 'some-digest') {
return { statusCode: 404, statusDescription: 'Not found. Like, ever.' };
}
if (parts[0] !== 't') {
var digest = parts[parts.length - 2];
if (digest.length < 20 && digest !== 'some-digest') {
return { statusCode: 404, statusDescription: 'Not found. Like, what?' };
}

// normalize stitch requests to /<podcast_id>/<episode_guid>/<digest>
if (parts.length === 5) {
parts.splice(1, 1);
}
parts.splice(-1, 1);
request.uri = '/' + parts.join('/');
// normalize stitch requests to /<podcast_id>/<episode_guid>/<digest>
if (parts.length === 5) {
parts.splice(1, 1);
}
parts.splice(-1, 1);
request.uri = '/' + parts.join('/');

// force restitching the arrangement by adding a random string to the url
// TODO: REAAAALLY need to sign/secret this one somehow
if (querystring.force) {
request.uri += `/force-${Date.now()}`;
// force restitching the arrangement by adding a random string to the url
// TODO: REAAAALLY need to sign/secret this one somehow
if (querystring.force) {
request.uri += `/force-${Date.now()}`;
}
}

return request;
Expand Down
15 changes: 15 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ describe('handler', () => {
expect(handler(event('/some')).statusCode).toEqual(404);
expect(handler(event('/some/path')).statusCode).toEqual(404);
expect(handler(event('/some/path/here')).statusCode).toEqual(404);
expect(handler(event('/t')).statusCode).toEqual(404);
expect(handler(event('/t/path')).statusCode).toEqual(404);
expect(handler(event('/t/some/path')).statusCode).toEqual(404);
});

it('404s on short/fake looking digests', async () => {
Expand All @@ -53,6 +56,18 @@ describe('handler', () => {

const event4 = event('/use1-whatev/1234/adfree/some-guid/some-digest/file.mp3');
expect(handler(event4)).toEqual(event4.request);

const event5 = event('/t/some.domain.com/test.wav/72.1.16000.m4a');
expect(handler(event5)).toEqual(event5.request);

const event6 = event('/t/a/up/guid1/test.wav/72.1.16000.m4a');
expect(handler(event6)).toEqual(event6.request);

const event7 = event('/t/a/up/p1/p2/guid1/test.wav/72.1.16000.m4a');
expect(handler(event7)).toEqual(event7.request);

const event8 = event('/t/some.com/p1/p2/p3/p4/test.wav/72.1.16000.m4a');
expect(handler(event8)).toEqual(event8.request);
});

it('allows non-expired links through', async () => {
Expand Down

0 comments on commit 5f1eb08

Please sign in to comment.