Skip to content
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
6 changes: 4 additions & 2 deletions src/lib/server/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,28 @@ const client =
* If the range is not satisfiable, a {@link RangeNotSatisfiableError} is thrown.
*
* @param key - The key of the object to get.
* @param opts.signal - Aborts the underlying S3 request when the consumer goes away, releasing the socket back to the pool.
* @param opts.range - An optional range to retrieve a portion of the object.
* @returns The object from S3, or `null` if the object does not exist.
*/
export async function getPodcastObject(
key: string,
opts: { range?: string | null } = {},
opts: { signal: AbortSignal; range?: string | null },
): Promise<{
stream: ReadableStream;
headers: Record<string, string>;
} | null> {
try {
const cmd = new GetObjectCommand({ Bucket: BUCKET, Key: key, Range: opts.range ?? undefined });
const res = await client.send(cmd);
const res = await client.send(cmd, { abortSignal: opts.signal });

if (!res.Body || !(res.Body instanceof Readable)) {
return null;
}

const stream = Readable.toWeb(res.Body);
if (!(stream instanceof ReadableStream)) {
res.Body.destroy();
return null;
}

Expand Down
5 changes: 4 additions & 1 deletion src/routes/(main)/(protected)/podcasts/[...key]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export const GET: RequestHandler = async (event) => {

let podcast: Awaited<ReturnType<typeof getPodcastObject>> | null = null;
try {
podcast = await getPodcastObject(`podcasts/${event.params.key}`, { range });
podcast = await getPodcastObject(`podcasts/${event.params.key}`, {
range,
signal: event.request.signal,
});
if (!podcast) {
return text(INVALID_REQUEST_BODY, { status: 400 });
}
Expand Down
Loading