-
Notifications
You must be signed in to change notification settings - Fork 151
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
base: master
Are you sure you want to change the base?
Conversation
14adcfe
to
2dc8543
Compare
src/chat.ts
Outdated
// 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, | ||
}); |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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. |
My pleasure. I have a good time playing in the Workers ecosystem. OK, let's keep this open for the time being. |
Signed-off-by: Sora Morimoto <[email protected]>
Signed-off-by: Sora Morimoto <[email protected]>
Signed-off-by: Sora Morimoto <[email protected]>
Signed-off-by: Sora Morimoto <[email protected]>
81f8643
to
88d42c8
Compare
There was a problem hiding this 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.
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.
any update on this ? |
Are you still interested in merging this? If so, I can update this to make this mergeable. |
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.