resourceloader: Factor out encodeJsonForScript
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 15 Sep 2018 20:51:43 +0000 (21:51 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 17 Sep 2018 23:53:42 +0000 (00:53 +0100)
commitb7f4b55f2b63f56ecee6992d8226148682237fc9
treef82f14c945798f7b867af5232f0d429e84e9a34b
parentbcd98b9cd4fea907418cb83a847e8b1dad7c3a67
resourceloader: Factor out encodeJsonForScript

Follows-up dc3fc6cf81, which documented the reasoning for the
specific json flags in StartupModule. In wanting to re-use them
in a different module it became apparant that perhaps it was a
bit too conservative in only allowing the flags to be used in
a script HTTP response.

Lax the contract a little bit (that is, do more escaping) to also
allow safe re-use in HTML context.

I considered making these separate methods (e.g. forScriptResponse,
and forInlineScript) but decided not to because the vast majority
of callers would have to be forInlineScript eventhough the code
in question had no responsiblity or knowledge of it becoming an
inline script, because ResourceLoader allows most modules to
become embedded if they support a private or preview mode.

Those modes are implemented by calling makeModuleResponse, and
wrapping it an inline script. It seems appealing and simplifying
to the API to require that script output is always safe for
embedding rather than complicating the API for winning back
a literal handful of bytes in the edge case that a user-generated
string contains a '<' and was not embedded. I estimate that with
gzip, it will literally save only a single byte, even if used
multiple times. Let's focus optimisation efforts elsewhere :)

Change-Id: I7742dabba6750deecf6fbf51cf9a77ee8cbfc727
includes/resourceloader/ResourceLoader.php
includes/resourceloader/ResourceLoaderStartUpModule.php