Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor logic for fetching files and listing directories #111

Open
flakey5 opened this issue Apr 4, 2024 · 0 comments
Open

Refactor logic for fetching files and listing directories #111

flakey5 opened this issue Apr 4, 2024 · 0 comments

Comments

@flakey5
Copy link
Member

flakey5 commented Apr 4, 2024

Background

As of right now the code behind fetching files, heading files, and listing directories is functional and working fine. However, it isn't the cleanest.

Current Request Flow

image

Notable Points

  • GET & HEAD requests are squished into the same functions. This can get messy, especially when one of them requires certain behaviors that the other one doesn't
  • Many functions are long and hard to follow in some cases
  • R2 and the origin fallback behaviors are baked in

Proposal

I have a few ideas that can make the code nicer to read and add some additional functionality that is/could be useful. I've implemented most of these ideas in my personal fork as POCs, https://github.com/flakey5/release-cloudflare-worker/tree/flakey5/20240330/contributors (term POC being used liberally in some cases)

A Provider

A Provider essentially acts like an API client. It abstracts the calls to a data source (e.g. r2, origin.nodejs.org) and returns the data in a shared type (e.g. GetFileResult for getting a file). It also should handle retry logic if necessary.

export interface Provider {
  head(path: string): Promise<HeadResult>;
  getFile(path: string, options?: GetFileOptions): Promise<GetFileResult>;
  readDirectory(path: string, options?: ReadDirectoryOptions): Promise<ReadDirectoryResult>;
}

So, an example of using a Provider to get a file from R2 would look something like:

const provider = new R2Provider({ ctx });
const result = await provider.getFile('some-file', { /* additional options, e.g. conditional headers */ });
if (!result.exists) {
  // file doesn't exist
} else {
  // do something with result.body
}

Providers will be created for R2 (R2Provider), R2's S3 API (S3Provider), and the origin server (OriginProvider). POC located here.

This API is a bit more complicated than what is currently implemented. However, it gives us a few benefits:

Our current implementation does not give us these benefits. It is hard coded to go to R2 and then the origin server if all retries to R2 get exhausted.

Additional changes

  • Handle GET and HEAD requests separately.
  • Refactor files in src/handlers/strategies to files in src/actions
  • Move code that renders the directory listing HTML into a response like we do with errors (e.g. src/responses/directoryNotFound.ts)

Proposed Request Flow

GET

image

HEAD

image

Wdyt cc @nodejs/web-infra ?

flakey5 added a commit that referenced this issue Apr 14, 2024
Adds the Provider interface and most of the implementation of R2Provider as discussed in #111. Note that as of right now the provider isn't being used, this will happen in future prs.
flakey5 added a commit that referenced this issue Apr 14, 2024
Adds the Provider interface and most of the implementation of R2Provider as discussed in #111. Note that as of right now the provider isn't being used, this will happen in future prs.
flakey5 added a commit that referenced this issue Apr 14, 2024
Implements the `OriginProvider` class as described in #111 and allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue Apr 14, 2024
Adds the Provider interface and most of the implementation of R2Provider as discussed in #111. Note that as of right now the provider isn't being used, this will happen in future prs.
flakey5 added a commit that referenced this issue Apr 14, 2024
Implements the `OriginProvider` class as described in #111 and allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue Apr 14, 2024
Implements the S3Provider class as described in #111.
flakey5 added a commit that referenced this issue Apr 14, 2024
Adds the Provider interface and most of the implementation of R2Provider as discussed in #111. Note that as of right now the provider isn't being used, this will happen in future prs.
flakey5 added a commit that referenced this issue Apr 14, 2024
Adds the Provider interface and most of the implementation of R2Provider as discussed in #111. Note that as of right now the provider isn't being used, this will happen in future prs.
flakey5 added a commit that referenced this issue Apr 14, 2024
Implements the `OriginProvider` class as described in #111 and allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue Apr 14, 2024
Implements the S3Provider class as described in #111.
flakey5 added a commit that referenced this issue Apr 15, 2024
Implements the S3Provider class as described in #111.
flakey5 added a commit that referenced this issue Apr 24, 2024
Implements the `OriginProvider` class as described in #111 and allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue Apr 24, 2024
Implements the `OriginProvider` and `S3Provider` classes as described in #111. Allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue Apr 24, 2024
Implements the `OriginProvider` and `S3Provider` classes as described in #111. Allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue Apr 24, 2024
Implements the `OriginProvider` and `S3Provider` classes as described in #111. Allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue May 31, 2024
Implements the `OriginProvider` and `S3Provider` classes as described in #111. Allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue May 31, 2024
Implements the `OriginProvider` and `S3Provider` classes as described in #111. Allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue May 31, 2024
Implements the `OriginProvider` and `S3Provider` classes as described in #111. Allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
flakey5 added a commit that referenced this issue Jun 2, 2024
Implements the `OriginProvider` and `S3Provider` classes as described in #111. Allows `R2Provider` to take in a fallback provider that will be used if R2 fails.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant