Giter VIP home page Giter VIP logo

Comments (4)

lukeed avatar lukeed commented on July 22, 2024

worktop is meant to be completely self-sufficient. You can/should use it directly and have information be accurate and up to date. In fact, until @cloudflare/workers-types 2.0 release, the worktop types for CF Workers were more accurate than the 1.x types.

worktop can't and won't use the CF types package because:

  1. worktop isn't exclusively a CF framework
  2. the CF types are globals and are meant only for a Cloudflare Workers environment
    This means that the type information included would be inaccurate for Deno, for example, and global types are all-or-nothing.
  3. the CF types are not 1:1 with the shared global names
    The types reflect the CF runtime, which is great, but if there are any spec compliance issues, they'll collide with TS internals and report as errors.
  4. Because of (3) you can't actually use @cloudflare/workers-types alongside the "dom" or "webworker" built-in types.

from worktop.

eugene1g avatar eugene1g commented on July 22, 2024

I understand that one goal for worktop is to be runtime-agnostic. However, the project currently defines types for request.cf this way:

interface Request {
cf: IncomingCloudflareProperties;
}

This cf field is specific to Cloudflare, and if developers access req.cf they expect this property to reflect the behavior of Cloudflare Workers (what fields exist, what is nullable/optional, etc). The issue is that IncomingCloudflareProperties is hardcoded, and has become outdated. It seems that worktop cannot choose not to ship this type for Cloudflare Workers, it can only choose between "duplicate, and maintain regularly" or "reference the official definitions maintained by Cloudflare".

I found two ways around this issue:

  1. Cast the cf property manually in every handler, e.g. const cf = req.cf as unknown as IncomingRequestCfProperties;
  2. Override the type within worktop, e.g. in global.d.ts:
/// <reference types="@cloudflare/workers-types" />

declare module "worktop/cfw" {
  interface IncomingCloudflareProperties extends IncomingRequestCfProperties {}
}

I don't think either solution is what you want developers to do, and since Cloudflare recommends new projects to use @cloudflare/workers-types, these conflicts will become more common. Embracing @cloudflare/workers-types should reduce your long-term burden for worktop as Cloudflare now actively maintains that package.

from worktop.

lukeed avatar lukeed commented on July 22, 2024

What request.cf property is outdated? Let’s just fix that. It changes infrequently.

from worktop.

eugene1g avatar eugene1g commented on July 22, 2024

Cool, I'll do a PR

from worktop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.