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

Port to TypeScript #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

smorimoto
Copy link

@smorimoto smorimoto commented Jan 22, 2023

This PR ports the main module to TypeScript. There are some mismatches in the original code in fairly strict terms, and I believe TypeScript can make things more explicit.
We use the latest yarn to manage dev dependencies, but I'm open to switching to any package manager.

*Use the "hide whitespace changes" option for easier viewing.

@smorimoto smorimoto force-pushed the typescript-port branch 3 times, most recently from 14adcfe to 2dc8543 Compare January 22, 2023 22:17
src/chat.ts Outdated
Comment on lines 197 to 203
// Send the request to the object. The `fetch()` method of a Durable Object stub has the
// same signature as the global `fetch()` function, but the request is always sent to the
// object, regardless of the request's URL.
return roomObject.fetch(newUrl, request);
return roomObject.fetch(newUrl, {
method: request.method,
headers: request.headers,
});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sceptical that the original code and comments here are correct. (But I often make silly mistakes.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code was correct. The idea is that we want to send a request that is identical to request except with a modified URL. Since the Request type's properties have the same names as the options object that is passed as the second parameter to fetch(), we can simply pass the request object itself and have it be treated as options.

The new code you've written here only copies over the method and headers from the original request; everything else is lost. In particular, this loses body, as well as some more obscure stuff like referrer.

This is a pretty common pattern in Workers code. Does TypeScript flag this as an error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestInit allows either IncomingRequestCfProperties or RequestInitCfProperties as the cf field, whereas fetch only allows RequestInitCfProperties. To fix this mismatch, chopping the cf field makes it relatively easy on the user side to let TypeScript take the flag down, but I'm still unsure if this is the right way with Workers.

const { cf, ...requestInit } = request;
return roomObject.fetch(newUrl, requestInit);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, this seems like a bug in the type definitions. The Request constructor and fetch should have the same argument signature.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, it seems worth raising the issue in the workerd repository and fixing the thing cleanly there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kentonv I think cloudflare/workerd#324 is relevant to this topic.

src/chat.ts Show resolved Hide resolved
@kentonv
Copy link
Member

kentonv commented Jan 24, 2023

Hi Sora,

I appreciate your effort making this example better by porting it to TypeScript!

However, this is a very big change, and in order to accept it, we would need to carefully review and test it, which will take some work. I am not sure when I or someone else on the team will be able to get to this. In the meantime though, we can at least leave the PR open so people who want to see a TypeScript version can find it here.

@smorimoto
Copy link
Author

My pleasure. I have a good time playing in the Workers ecosystem.

OK, let's keep this open for the time being.

Copy link
Member

@kentonv kentonv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came up in #39: The changes here to checkLimit() are incorrect -- the original version was working as intended, callLimiter() is intentionally running asynchronously.

src/chat.ts Outdated Show resolved Hide resolved
src/chat.ts Outdated Show resolved Hide resolved
Revert changes to checkLimit(). This function was written as intended: the call to `callLimiter()` is intended to run asynchronously, while `checkLimit()` is intended to return synchronously.
@zhihengGet
Copy link

any update on this ?

@smorimoto
Copy link
Author

Are you still interested in merging this? If so, I can update this to make this mergeable.

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

Successfully merging this pull request may close these issues.

3 participants