Commit c987ffd
committed
fix(document): fix encoding cache poisoning and skip redundant re-detection
Two follow-up correctness fixes for the encoding auto-detection added in the
previous commit, plus the perf optimization that surfaced them:
- Skip re-running EncodingDetector when a file's encoding is already known
(File instances are cached/reused per path for the session), so reopening a
tab doesn't re-read + re-detect every time.
- That skip-check needed to be more careful than a plain truthiness check:
several unrelated features (ProjectManager's "Download" command,
AIChatPanel's image-attach, MediaViewer, etc.) read a File instance with
{encoding: fs.BYTE_ARRAY_ENCODING} and no doNotCache for their own non-text
purposes - which, per File.read()'s own caching, leaves that sentinel value
cached in file._encoding even though the file was never opened as a
document. Added EncodingDetector.isKnownTextEncoding() to tell a real text
codec apart from that sentinel (notably still present in
fs.SUPPORTED_ENCODINGS, so that list alone can't distinguish them).
- Found a deeper, pre-existing bug while writing the regression test for the
above: File.read()'s cache-hit check only compares options.encoding to
this._encoding - it doesn't invalidate _contents/_stat when something
bare-reassigns file._encoding (which _doOpen has always done for the
explicit-option and stored-preference branches too, not just detection).
So: a prior raw-byte read followed by reassigning _encoding to the correct
detected value could make the next real open cache-hit on stale raw bytes
instead of re-reading as text. Fixed via a _setFileEncoding() helper that
clears the stale cache whenever the encoding actually changes.
Extends EncodingDetector-test.js and file-encoding-integ-test.js with
regression coverage for both.1 parent 80328b7 commit c987ffd
4 files changed
Lines changed: 129 additions & 8 deletions
File tree
- src/document
- test/spec
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
522 | 522 | | |
523 | 523 | | |
524 | 524 | | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
525 | 540 | | |
526 | | - | |
| 541 | + | |
527 | 542 | | |
528 | 543 | | |
529 | 544 | | |
530 | 545 | | |
531 | | - | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
532 | 559 | | |
533 | 560 | | |
534 | | - | |
535 | | - | |
536 | | - | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
537 | 565 | | |
538 | | - | |
539 | | - | |
540 | | - | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
541 | 569 | | |
542 | 570 | | |
543 | 571 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
244 | 244 | | |
245 | 245 | | |
246 | 246 | | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
247 | 264 | | |
248 | 265 | | |
249 | 266 | | |
| 267 | + | |
250 | 268 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
274 | 302 | | |
275 | 303 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
50 | 51 | | |
51 | 52 | | |
52 | 53 | | |
| 54 | + | |
53 | 55 | | |
54 | 56 | | |
55 | 57 | | |
| |||
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| 65 | + | |
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
| |||
202 | 205 | | |
203 | 206 | | |
204 | 207 | | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
205 | 252 | | |
206 | 253 | | |
0 commit comments