func()

in internal/sourcemap/sourcemap_fetcher.go [47:95]


func (s *SourcemapFetcher) Fetch(ctx context.Context, name, version, path string) (*sourcemap.Consumer, error) {
	original := identifier{name: name, version: version, path: path}

	select {
	case <-s.metadata.ready():
		if err := s.metadata.err(); err != nil {
			return nil, err
		}
	case <-ctx.Done():
		return nil, fmt.Errorf("error waiting for metadata fetcher to be ready: %w", ctx.Err())
	default:
		return nil, fmt.Errorf("metadata fetcher is not ready: %w", errFetcherUnvailable)
	}

	if i, ok := s.metadata.getID(original); ok {
		// Only fetch from ES if the sourcemap id exists
		return s.fetch(ctx, i)
	}

	if urlPath, err := url.Parse(path); err == nil {
		// The sourcemap coule be stored in ES with a relative
		// bundle filepath but the request came in with an
		// absolute path
		original.path = urlPath.Path
		if urlPath.Path != path {
			// The sourcemap could be stored on ES under a certain host
			// but a request came in from a different host.
			// Look for an alias to the url path to retrieve the correct
			// host and fetch the sourcemap
			if i, ok := s.metadata.getID(original); ok {
				return s.fetch(ctx, i)
			}
		}

		// Clean the url and try again if the result is different from
		// the original bundle filepath
		urlPath.RawQuery = ""
		urlPath.Fragment = ""
		urlPath = urlPath.JoinPath()
		cleanPath := urlPath.String()

		if cleanPath != path {
			s.logger.Debugf("original filepath %s converted to %s", path, cleanPath)
			return s.Fetch(ctx, name, version, cleanPath)
		}
	}

	return nil, fmt.Errorf("unable to find sourcemap.url for service.name=%s service.version=%s bundle.path=%s", name, version, path)
}