Repository: facebook/react
File: fixtures/flight/server/region.js (JavaScript)

Code before:
      res.set('Content-type', 'application/json');
      let requestedFilePath = req.query.name;

      if (requestedFilePath.startsWith('file://')) {
        requestedFilePath = requestedFilePath.slice(7);
      }

      console.log(requestedFilePath);

      const relativePath = path.relative(rootDir, requestedFilePath);
      if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
        // This is outside the root directory of the app. Forbid it to be served.
        res.status = 403;
        res.write('{}');
        res.end();
        return;
      }

      const sourceMap = nodeModule.findSourceMap(requestedFilePath);
      let map;
      // There are two ways to return a source map depending on what we observe in error.stack.
      // A real app will have a similar choice to make for which strategy to pick.
      if (!sourceMap || Error.prepareStackTrace === undefined) {
        // When --enable-source-maps is enabled, the error.stack that we use to track
        // stacks will have had the source map already applied so it's pointing to the
        // original source. We return a blank source map that just maps everything to
        // the original source in this case.
        const sourceContent = await readFile(requestedFilePath, 'utf8');
        const lines = sourceContent.split('\n').length;
        map = {
          version: 3,
          sources: [requestedFilePath],
          sourcesContent: [sourceContent],
          // Note: This approach to mapping each line only lets you jump to each line
          // not jump to a column within a line. To do that, you need a proper source map
          // generated for each parsed segment or add a segment for each column.
          mappings: 'AAAA' + ';AACA'.repeat(lines - 1),
          sourceRoot: '',
        };
      } else {
        // If something has overridden prepareStackTrace it is likely not getting the
        // natively applied source mapping to error.stack and so the line will point to
        // the compiled output similar to how a browser works.
        // E.g. ironically this can happen with the source-map-support library that is
        // auto-invoked by @babel/register if external source maps are generated.
        // In this case we just use the source map that the native source mapping would
        // have used.
        map = sourceMap.payload;
      }
      res.write(JSON.stringify(map));
      res.end();

Review comment (suggestion):
Right now this throws when we request source map for node internal frames e.g. `Error: ENOENT: no such file or directory, open 'node:internal/process/task_queues'`.

Should we handle these on the server gracefully? Otherwise it might be a bit spammy. I guess the client can't handle it since it doesn't necessarily know what runtime internals are (assuming Bun uses other protocols).

Code after:
      res.set('Content-type', 'application/json');
      let requestedFilePath = req.query.name;

      if (requestedFilePath.startsWith('file://')) {
        requestedFilePath = requestedFilePath.slice(7);
      }

      const relativePath = path.relative(rootDir, requestedFilePath);
      if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
        // This is outside the root directory of the app. Forbid it to be served.
        res.status = 403;
        res.write('{}');
        res.end();
        return;
      }

      const sourceMap = nodeModule.findSourceMap(requestedFilePath);
      let map;
      // There are two ways to return a source map depending on what we observe in error.stack.
      // A real app will have a similar choice to make for which strategy to pick.
      if (!sourceMap || Error.prepareStackTrace === undefined) {
        // When --enable-source-maps is enabled, the error.stack that we use to track
        // stacks will have had the source map already applied so it's pointing to the
        // original source. We return a blank source map that just maps everything to
        // the original source in this case.
        const sourceContent = await readFile(requestedFilePath, 'utf8');
        const lines = sourceContent.split('\n').length;
        map = {
          version: 3,
          sources: [requestedFilePath],
          sourcesContent: [sourceContent],
          // Note: This approach to mapping each line only lets you jump to each line
          // not jump to a column within a line. To do that, you need a proper source map
          // generated for each parsed segment or add a segment for each column.
          mappings: 'AAAA' + ';AACA'.repeat(lines - 1),
          sourceRoot: '',
        };
      } else {
        // If something has overridden prepareStackTrace it is likely not getting the
        // natively applied source mapping to error.stack and so the line will point to
        // the compiled output similar to how a browser works.
        // E.g. ironically this can happen with the source-map-support library that is
        // auto-invoked by @babel/register if external source maps are generated.
        // In this case we just use the source map that the native source mapping would
        // have used.
        map = sourceMap.payload;
      }
      res.write(JSON.stringify(map));
      res.end();
    } catch (x) {
      res.status = 500;