Using scopes in Effect to manage resources like temporary directories is a good approach. Your cu...

Is there a better way to do this?

export const DownloadHandler = Effect.gen(function*() {
  const params = yield* HttpServerRequest.schemaSearchParams(UrlParams);
  const scope = yield* Scope.make();

  yield* Effect.addFinalizer(() => Scope.close(scope, Exit.succeed(void 0)));

  const { id, outputPath } = yield* hlsToMp4(params.link).pipe(
    Scope.extend(scope),
  );
  yield* Effect.logInfo("Sending file", { outputPath });

  return yield* HttpServerResponse.file(outputPath, {
    headers: {
      "Content-Disposition": `attachment; filename="${id}.mp4"`,
    },
  });
})


Basically the hlsToMp4 has a yield* fs.makeTempDirectoryScoped(); where the output file is saved, and I need that to be there until I send the response back. So in a way need to inherit the scope from the parent.

The snippet I shared above seems to work, though not sure if there is a better way. My assumption was to .pipe(Effect.scoped) on the handler, but that didn't do the trick
Was this page helpful?