Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transcode uri #6

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
function handler(event) {
var EXPIRED_REDIRECT_PREFIX = '<EXPIRED_REDIRECT_PREFIX>';
var request = event.request || {};
var querystring = request.querystring || {};
var uri = request.uri || '';

// pass-through root/favicon
Expand All @@ -16,14 +14,25 @@ function handler(event) {
parts.shift();
}

if (parts[0] === 't') {
return handleTranscode(request, parts);
} else {
return handleDigest(request, parts);
}
}

function handleDigest(request, parts) {
var EXPIRED_REDIRECT_PREFIX = '<EXPIRED_REDIRECT_PREFIX>';

// just kick out invalid looking paths
// either: /podcast_id/episode_guid/digest/filename.mp3
// or: /podcast_id/feed_id/episode_guid/digest/filename.mp3
// /podcast_id/episode_guid/digest/filename.mp3
// /podcast_id/feed_id/episode_guid/digest/filename.mp3
if (parts.length !== 4 && parts.length !== 5) {
return { statusCode: 404, statusDescription: 'Not found. Like, ever.' };
}

// kick back expired redirects (TODO: require ?exp later on)
var querystring = request.querystring || {};
if (querystring.exp) {
var now = Math.round(Date.now() / 1000);
if (now > parseInt(querystring.exp.value, 10)) {
Expand All @@ -44,7 +53,7 @@ function handler(event) {
// 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.' };
return { statusCode: 404, statusDescription: 'Not found. Like, what?' };
}

// normalize stitch requests to /<podcast_id>/<episode_guid>/<digest>
Expand All @@ -56,9 +65,21 @@ function handler(event) {

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

return request;
}

function handleTranscode(request, parts) {
// just kick out invalid looking paths
// /t/a/path/<file>/<format>.<extension>
// /t/<host>/[<path>/]<file>/<format>.<extension>
if (parts.length < 4) {
return { statusCode: 404, statusDescription: 'Not found. Nope.' };
}

return request;
}
22 changes: 21 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('handler', () => {
expect(handler(event1)).toEqual(event1.request);
});

it('allows non-expiring links through', async () => {
it('allows non-expiring digest links through', async () => {
const event1 = event('/1234/some-guid/some-digest/file.mp3');
expect(handler(event1)).toEqual(event1.request);

Expand Down Expand Up @@ -139,4 +139,24 @@ describe('handler', () => {
expect(result1.uri).toMatch('/1234/some-guid/some-digest/force-');
expect(result1.uri).toMatch(/force-[0-9]+/);
});

it('404s on bad looking transcode paths', async () => {
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('allows transcode links through', async () => {
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);
});
});