Repository: expo/expo
File: packages/expo-server/src/runtime/index.ts (TypeScript)

Code before:
    const scope = {
      ...setup,
      origin: setup.origin,
      environment: setup.environment,
      requestHeaders: setup.requestHeaders,
      waitUntil,
      deferTask: setup.deferTask,
      setResponseHeaders(updateHeaders) {
        responseHeadersUpdates.push(updateHeaders);
      },
    } satisfies RequestAPI;

    if (!scope.deferTask) {
      scope.deferTask = function deferTask(fn) {
        deferredTasks.push(fn);
      };
    }

    let result: Response;
    try {
      const response =
        scopeRef.current != null
          ? await scopeRef.current.run(scope, () => run(...args))
          : await run(...args);
      // Recreate the response with mutable headers, since the original response
      // may have an immutable headers guard (like from `Response.redirect()`)
      result = new Response(response.body, {
        ...response,
        status: response.status,
        statusText: response.statusText,
        headers: response.headers,
        // Cloudflare-specific response properties
        cf: (response as any).cf,
        webSocket: (response as any).webSocket,
      } as ResponseInit);
    } catch (error) {
      if (error != null && error instanceof Response && !error.bodyUsed) {
        result = error;
      } else if (error != null && error instanceof Error && 'status' in error) {
        return errorToResponse(error);
      } else {
        throw error;
      }
    }

    deferredTasks.forEach((fn) => {
      const maybePromise = fn();
      if (maybePromise != null) waitUntil(maybePromise);
    });

    for (const updateHeaders of responseHeadersUpdates) {

Review comment (suggestion):
Technically, a user can throw responses too, so we might want to do this lazily or after this block, but it's a small detail

Code after:
    const scope = {
      ...setup,
      origin: setup.origin,
      environment: setup.environment,
      requestHeaders: setup.requestHeaders,
      waitUntil,
      deferTask: setup.deferTask,
      setResponseHeaders(updateHeaders) {
        responseHeadersUpdates.push(updateHeaders);
      },
    } satisfies RequestAPI;

    if (!scope.deferTask) {
      scope.deferTask = function deferTask(fn) {
        deferredTasks.push(fn);
      };
    }

    let result: Response;
    try {
      result =
        scopeRef.current != null
          ? await scopeRef.current.run(scope, () => run(...args))
          : await run(...args);
    } catch (error) {
      if (error != null && error instanceof Response && !error.bodyUsed) {
        result = error;
      } else if (error != null && error instanceof Error && 'status' in error) {
        return errorToResponse(error);
      } else {
        throw error;
      }
    }

    // Recreate the response with mutable headers, since the original response
    // may have an immutable headers guard (like from `Response.redirect()`)
    result = new Response(result.body, {
      ...result,
      status: result.status,
      statusText: result.statusText,
      headers: result.headers,
      // Cloudflare-specific response properties
      cf: (result as any).cf,
      webSocket: (result as any).webSocket,
    } as ResponseInit);

    deferredTasks.forEach((fn) => {
      const maybePromise = fn();
      if (maybePromise != null) waitUntil(maybePromise);
    });