Repository navigation
Commit e8bb0fd
authored
feat(runtime): HTTP module cache controls (#2173)
### Description
The previous PR in the stack gives the module loader its own HTTP cache
when the app ships OkHttp. This PR adds the controls that dev tooling
and apps need over that cache. The names and semantics are a
cross-platform contract shared with the iOS runtime, because the CLI and
the Vite plugin hard-code them.
The controls act only on the cache the module loader keeps for `http(s)`
modules. They never touch an HTTP cache the app uses for its own
requests, and never V8's code cache. The cache is **on** unless one of
these turns it off:
| control | effect |
|---|---|
| app config `"moduleHttpCache": false` (top level, next to `security`)
| the process starts with the cache off |
| launch option `--nativescript-no-module-cache` | this launch starts
with the cache off |
| launch option `--nativescript-clear-module-cache` | wipes the cache
before the launch's first fetch |
| `ns:module` `setHttpCacheEnabled(enabled)` | turns it on or off from
then on, for the whole process |
| `ns:module` `clearHttpCache()` | wipes it before returning |
| inspector `Network.setCacheDisabled {cacheDisabled: true}` | turns it
off while that client stays connected |
| inspector `Network.clearBrowserCache` | wipes it |
| `Clear-Site-Data: "cache"` (or `"*"`) on a module response | clears
that response's origin |
#### Switch and clear (`ns:module`, app config)
- `setHttpCacheEnabled` and `clearHttpCache` exist in debug and release
builds. `setHttpCacheEnabled` throws `TypeError` unless it gets a
boolean.
- The app config and the launch options are read once per process, at
startup. A number counts as a boolean, as on iOS (`0` is off). Any other
non-boolean `moduleHttpCache` logs a warning and leaves the cache on.
The process starts with the cache off when either source says so, and
`setHttpCacheEnabled` overrides both from then on.
- **With the cache off,** a fetch neither reads from nor writes to the
cache, on both transports. Its request carries `Cache-Control: no-cache`
and `Pragma: no-cache`, which is what Fetch's `no-store` mode sends, so
an intermediate cache revalidates too. OkHttp fetches with the cache off
go through a cache-less client that shares the connection pool. A
request-side `no-cache` alone would still store the response, and
`no-store` would put `no-store` on the wire.
- **The switch is process-wide.** The main isolate and every worker
share it, and so do the graph walk and the synchronous fallback. Each
fetch reads it when it is issued; fetches already in flight keep the
value they started with.
- **`clearHttpCache()` clears durably.** With an OkHttp client open, it
evicts through the live cache (`evictAll` + `flush`). Otherwise it
deletes the cache directory's contents, which also clears what an
earlier OkHttp run stored. Either way the next fetch reaches the origin,
including the first fetch after the process is killed right after the
call. It doesn't touch the module registry: a loaded module stays loaded
until `invalidateModules` evicts it.
#### Launch options on Android
A launch option is an empty file that the CLI creates before `am start`,
the same convention `--debug-brk` uses with `<appId>-debugbreak`:
```sh
adb shell "cat /dev/null > /data/local/tmp/<appId>-nativescript-no-module-cache"
adb shell "cat /dev/null > /data/local/tmp/<appId>-nativescript-clear-module-cache"
adb shell am start -n <appId>/<activity>
```
- **Only an empty file is in effect.** An app can't delete a file in
`/data/local/tmp`, so the runtime reads the options at startup and
consumes each one by writing `1` to it. An option therefore applies to
one launch, and the CLI recreates the file for the next one. If the
write fails, the option still applies, and keeps applying until the file
is removed.
- **Only the app's default process reads them,** in debug builds and in
release builds that set `security.allowRemoteModules`. Other release
builds load no HTTP module and never look.
- A file the CLI created is world-writable, so another app on the device
can empty a consumed file and re-arm it for the next launch. The worst
case is a launch with the cache off or wiped.
#### Inspector
- `Network.setCacheDisabled` and `Network.clearBrowserCache` are applied
to the cache on the inspector socket thread before anything else sees
them, from any session and even while paused.
- The disable is per client, keyed by the websocket that sent it. The
cache is in effect when the setting is on **and** no connected client
has disabled it, so DevTools' "Disable cache" doesn't change the
setting. It ends when that client's socket closes, released on the
socket thread, so it also ends while the main isolate is paused or busy.
- **Exactly one response per command.** A main-session command then goes
on to the JS `Network` domain handler registered with
`__registerDomainDispatcher` (`@nativescript/core` registers one), which
answers it. The runtime answers `{}` itself when:
- no handler answers (none is registered, or it returned no response);
- the main thread is paused;
- the command came from a worker session.
A handler that throws gets the iOS answer instead: a `-32000` error
carrying the thrown message. A command for an unknown worker session
applies nothing and gets `-32001` "Session not found", like any other
command for that session.
- A `setCacheDisabled` without a boolean `cacheDisabled` changes
nothing. Where the runtime answers it, the answer is `-32602`.
- On Android the inspector serves one client at a time and exists only
in debuggable apps.
#### `Clear-Site-Data`
- A module response whose `Clear-Site-Data` names `"cache"` or `"*"`
clears the cache's entries for the response's final origin (after
redirects) before the loader receives the response. Plain `http` origins
count, since dev servers use them. Unquoted types are ignored, as in
browsers.
- The clear runs once the body has been read and closed, so the
carrier's own entry, committed at that point, goes with the rest. A
cacheable carrier therefore doesn't wipe the cache again on every hit.
- Origins are compared by scheme, host and port as OkHttp parses the
cached URLs, so entries whose URLs `java.net.URI` rejects (raw `|` in a
query, `[id]` in a path) are cleared too.
- Two limits: only the final response's header is read, so a header on a
redirect hop is ignored; and an entry another fetch is still writing
when the clear runs is committed afterwards and survives.
- A failure inside the clear is logged and never fails the module that
carried the header.
- Clearing one origin goes through the open OkHttp cache. Without one,
the header clears the whole cache directory instead. That happens with
no OkHttp, or when nothing has been fetched through OkHttp yet.
#### On the platform transport (no OkHttp)
There is no cache, but the controls still behave consistently:
- the switch still controls the two `no-cache` request headers;
- `clearHttpCache()` and `Clear-Site-Data` delete any cache directory an
earlier OkHttp run left on disk;
- `clearHttpCache()` doesn't throw.
#### Notes for review
- **Open question for the CLI.** Should the CLI create the launch-option
files for debug runs only? The runtime currently also honours them in
release builds that allow remote modules.
- **Pre-existing inspector reconnect bug, narrowed.** The old
connection's disconnect is posted to the main thread and could run after
a newer client connected, tearing it down. It now skips when a different
socket is connected (commit `1dbd5cb54`, separable). A connect that
lands between its two checks can still lose that client's streams and
worker sessions; closing that fully needs the teardown reordered. The
cache disable no longer depends on it.
- **Open question: a replaced client.** A frontend that was replaced
while its socket stays open keeps the cache disabled until that socket
closes (`connect` releases nothing). Every socket gets its close
callback, so nothing leaks. Releasing the other sockets' holds in
`connect` is the alternative, since Android serves one client at a time.
- **Not yet checked:**
- a release build;
- a `304` response carrying `Clear-Site-Data`;
- a `Clear-Site-Data` response fetched by the synchronous fallback. iOS
covers the last two with specs; Android doesn't yet.
- **No spec covers the inspector commands or the launch-option files.**
The suite has no inspector client, and the option files are read only at
process start. The inspector paths were checked once by hand with a
websocket script against the test app: unknown session → `-32001` and
nothing applied; `setCacheDisabled true` → `{}` and requests carry
`no-cache`; a throwing handler → one `-32000` reply; a second client
stays connected after the first one's socket closed, and the first one's
disable is released. That check ran with the main thread idle, so
release while paused rests on reading the code.
### Does your commit message include the wording below to reference a
specific issue in this repo?
No issue.
### Related Pull Requests
Cross-platform contract with the matching iOS change, which is still in
progress. The names are shared with the iOS runtime: the `ns:module`
members, the launch flags, `moduleHttpCache`, the inspector commands and
`Clear-Site-Data` handling. Android-specific:
- the launch options are files in `/data/local/tmp`, not process
arguments;
- per-origin clearing needs the OkHttp cache to be open;
- the inspector is single-client.
Part of a stack of six; each PR targets the branch below it:
1. #2168 `fix/http-importer-specifiers`: specifier resolution from HTTP
importers
2. #2169 `refactor/module-url-parse`: one URL parse for every http(s)
entry point
3. #2170 `perf/http-module-streaming-compile`: compile large HTTP
modules off the JS thread
4. #2171 `fix/http-module-fetch-budget`: bound fetches by the graph
walk's deadline
5. #2172 `perf/http-module-transport`: persistent connections,
per-host-class caps, OkHttp transport
6. #2173 `feat/http-module-cache-controls`: HTTP module cache controls
(**this PR**)
### Does your pull request have [unit
tests](https://github.com/NativeScript/NativeScript/blob/master/running-tests.md)?
Yes. A new "HTTP cache controls" group in `testEsmHttpLoader.js` covers:
- the switch rejecting a non-boolean;
- no cache directives being sent while the cache is on;
- the next import reaching the origin after `clearHttpCache()`;
- with the cache off: over OkHttp the origin is reached every time and
nothing is stored; over the platform transport the two `no-cache`
headers are sent.
**`Clear-Site-Data`:**
- `"cache"` and `"*"` each clear the origin's entries and the carrier's
own, including an entry whose URL has a raw `|` in its query;
- an unquoted type is ignored;
- the header is honoured on a platform-transport response.
**Workers** share the main isolate's cache (the worker reports its
import's success or error) and see the switch the main isolate turned
off; a worker turning the switch off is seen by the main isolate.
`ModuleTestServer` gains a `/esm/clear-site-data.mjs` route.
**By hand, on an emulator,** with a temporary boot script and a
fixed-port module origin on the host, every check behaved as the
contract above specifies. Each check is a relaunch:
- `moduleHttpCache: false`;
- each launch-option file: it applies to one launch, and the runtime
consumes the file;
- `clearHttpCache()` followed immediately by a process kill: the next
launch reaches the origin.
The full suite on this branch, before the last rebase onto `main`:
- with OkHttp: 2051 specs, 0 failures;
- without OkHttp: 0 failures, with the 11 OkHttp-only specs skipped.
With the review fixes (`9ba565bbd`): 2075 specs, 0 failed, 6 skipped,
read from the app's results (the harness step itself failed when the
host ran out of disk and paused the emulator mid-run).
After the rebase, before the review fixes (`./gradlew
runtestsAndVerifyResults`): 2074 specs, 2068 passed, 0 failed, 6 skipped
(the usual GC, native-module and environment skips), on an API 36 arm64
emulator with OkHttp in the test app, at
`feat/http-module-cache-controls` 21f87a8.
This stays a draft until the stack has been run against a real Vite dev
session on Android, which has not happened yet.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **New Features**
* Added runtime controls to enable or disable HTTP module caching and
clear cached data.
* HTTP module caching is enabled by default, with startup configuration
and launch options available to change or clear it.
* Cache settings apply to requests across the app and workers. Responses
can clear cached data with `Clear-Site-Data`.
* Added inspector commands to disable caching or clear the browser
cache.
* **Documentation**
* Documented cache defaults, configuration options, runtime controls,
and cache-clearing behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent 840f23a commit e8bb0fd
18 files changed
Lines changed: 1223 additions & 96 deletions
File tree
- docs
- test-app
- app/src/main
- assets/app/tests
- java/com/tns
- tests
- runtime/src/main
- cpp
- js
- java/com/tns
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
429 | 429 | | |
430 | 430 | | |
431 | 431 | | |
| 432 | + | |
| 433 | + | |
432 | 434 | | |
433 | 435 | | |
434 | 436 | | |
| |||
453 | 455 | | |
454 | 456 | | |
455 | 457 | | |
| 458 | + | |
456 | 459 | | |
457 | 460 | | |
458 | 461 | | |
| |||
990 | 993 | | |
991 | 994 | | |
992 | 995 | | |
993 | | - | |
994 | | - | |
995 | | - | |
996 | | - | |
997 | | - | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
998 | 1001 | | |
999 | 1002 | | |
1000 | 1003 | | |
| |||
1033 | 1036 | | |
1034 | 1037 | | |
1035 | 1038 | | |
| 1039 | + | |
1036 | 1040 | | |
1037 | 1041 | | |
1038 | 1042 | | |
1039 | 1043 | | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
| 1049 | + | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + | |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
| 1083 | + | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
| 1100 | + | |
| 1101 | + | |
| 1102 | + | |
| 1103 | + | |
| 1104 | + | |
| 1105 | + | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
| 1112 | + | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
| 1116 | + | |
| 1117 | + | |
| 1118 | + | |
| 1119 | + | |
| 1120 | + | |
| 1121 | + | |
| 1122 | + | |
| 1123 | + | |
| 1124 | + | |
| 1125 | + | |
| 1126 | + | |
| 1127 | + | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
1040 | 1135 | | |
1041 | 1136 | | |
1042 | 1137 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
0 commit comments