nodejs / release-cloudflare-worker

Infra for serving Node.js downloads and documentation.
https://nodejs.org/dist
MIT License
21 stars 5 forks source link

We should have default strategies for caching #73

Closed ovflowd closed 7 months ago

ovflowd commented 8 months ago

As pointed out by @MattIPv4 (https://openjs-foundation.slack.com/archives/C05TRU8PZL6/p1699990976027789) we should be explicit about caching strategies.

And we should also:

flakey5 commented 8 months ago

We should extract many other implicit constants ... to proper constant files

Do we want to just move the object to a constants file like this

// src/constants/r2Prefixes.ts
export function getUrlToBucketPathMap(url) {
  return {
    dist: DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/'),
    download:
      DOWNLOAD_PATH_PREFIX +
      (url.pathname.substring('/download'.length) || '/'),
    docs: DOCS_PATH_PREFIX + (url.pathname.substring('/docs'.length) || '/'),
    api: API_PATH_PREFIX + (url.pathname.substring('/api'.length) || '/'),
    metrics: url.pathname.substring(1), // substring to cut off the /
  }
}

// src/util.ts
const urlToBucketPathMap = getUrlToBucketPathMap(url);
// ...
} else if (basePath in urlToBucketPathMap) {
  bucketPath = urlToBucketPathMap[basePath];
}

or can we do something like this, where we only construct the url we need rather than all of them

// src/constants/r2Prefixes.ts
export function constructDistPath(url) {
  return DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/');
}
// constructDownloadPath, constructDocsPath, ...

// src/util.ts
} else {
  switch (basePath) {
    case 'dist': {
      bucketPath = constructDistPath(url)
      break;
    }
    // download, docs, ...
    default: {
      if (env.DIRECTORY_LISTING === 'restricted') {
        return undefined;
      }
      bucketPath = url.pathname.substring(1);
    }
  }
}
...

or was there something else you were thinking about?

ovflowd commented 7 months ago

// src/constants/r2Prefixes.ts
export function getUrlToBucketPathMap(url) {
  return {
    dist: () => `${DIST_PATH_PREFIX}${url.pathname.substring('/dist'.length) || '/'}`,
    download: () => `${DOWNLOAD_PATH_PREFIX}${url.pathname.substring('/download'.length) || '/'}`,
    ...
  }
}

// src/util.ts
const urlToBucketPathMap = getUrlToBucketPathMap(url);
// ...
} else if (basePath in urlToBucketPathMap) {
  bucketPath = urlToBucketPathMap[basePath];
}
ovflowd commented 7 months ago

I think it's important to mention that we have numerous constant files and it would make sense to have similar constants grouped together (ie. assume we have multiple constants for dist related things or download/ related things, it makes sense to have all these paths and logic to be close together

flakey5 commented 7 months ago

Is there a reason behind why dist: () => `${DIST_PATH_PREFIX}${url.pathname.substring('/dist'.length) || '/'}`, instead of dist: DIST_PATH_PREFIX + (url.pathname.substring('/dist'.length) || '/'),?

ovflowd commented 7 months ago

// src/constants/r2Prefixes.ts
const BUCKET_TO_PATHNAME_MAP = {
  dist: ({ pathname }) => `${DIST_PATH_PREFIX}${pathname.substring('/dist'.length) || '/'}`,
  download: ({ pathname }) => `${DOWNLOAD_PATH_PREFIX}${pathname.substring('/download'.length) || '/'}`,
  ...
};

// ...
} else if (basePath in BUCKET_TO_PATHNAME_MAP) {
  bucketPath = BUCKET_TO_PATHNAME_MAP[basePath](url);
}
ovflowd commented 7 months ago

The main reasoning behind is: You don't calculate and do all these string interpolation at once...

flakey5 commented 7 months ago

Do we want to tell clients to locally cache HEAD requests? It's what we're doing now, but given we don't cache them on the worker's end I wanted to double check https://github.com/nodejs/release-cloudflare-worker/blob/830d72a0c9096da3fb46280fbaa7a493d5d65db4/src/handlers/strategies/serveFile.ts#L159