-
Notifications
You must be signed in to change notification settings - Fork 325
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
html-rewriter: Support streaming content replacements #3211
Conversation
2ceeba1
to
25d56aa
Compare
src/workerd/api/html-rewriter.h
Outdated
@@ -304,6 +307,8 @@ class EndTag final: public HTMLRewriter::Token { | |||
|
|||
private: | |||
kj::Maybe<CType&> impl; | |||
// TODO(npaun): is adding rewriter field like this legit? |
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 would say, not quite legit, because this could become dangling. The reason is that subclasses of Token are the C++-side implementations of JS wrapper objects, meaning JS can arbitrarily extend their lifetimes beyond Rewriter
's lifetime. This is the reason there's the TokenScope guard which nullifies impl
above.
Of course, checkToken()
is called in all of the places we use this rewriter
reference, so the code as written is safe, but it feels like leaving a footgun around.
It occurs to me that an alternative could be to make each Token subclass own their own HashMap of registered replacers. We would expect each Token's replacers to be dropped before its associated TokenScope ends, meaning we could have ~TokenScope()
assert if there are any registered replacers left in the Token.
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.
Thanks 🙏🏻👍🏻
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.
Thanx...
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 dangling reference issue is fixed.
However, I didn't move the registered replacers to Token
yet because it seems like the replacer callback can be invoked after their registering handler has exited. Happy to revisit this if you have any ideas to try later on.
df2e80f
to
9154b3e
Compare
9154b3e
to
64202f3
Compare
62fabff
to
dabc3b6
Compare
dabc3b6
to
da5d326
Compare
da5d326
to
ce80785
Compare
The generated output of |
d9535a3
to
33c3910
Compare
This reverts commit c77df01, which is causing upstream test failures.
lol-html allows you to insert replacement values in various places in a HTML stream. Previously, these replacement values were always strings. However, recently lol-html added support for streaming replacements in cloudflare/lol-html#229. lol-html requires the streams to produce valid UTF-8 data, but it may be written in arbitrary length chunks (even partial UTF-8 characters).
This PR adds support for replacement values that are
ReadableStream
s orResponse
s.