refactor: introduce category, record, and export packages (#651)

Later layers speak categories and records, and export needs nothing else.
Ordering rows once in the exporter fixes Firefox history, written least visited first.
This commit is contained in:
Roger authored and GitHub committed 2026-10-26 19:55:19 +08:00
1 parent 9937dbf9fb
commit b04c436a73
97 files changed
+1993 -1646

No files matched your search

+11 -10
View File
@@ -44,18 +44,19 @@ Every record starts with two flat source fields — `Browser` and `Profile` ([RF
## Deterministic ordering
Output order is defined once per category, in the exporter, for every browser. Files list extractions in plan order — the browser table's order (one merged list whose per-OS projections equal today's `platformBrowsers()` orders), then user data dir ID, then profiles in discovery order (directory order for Chromium and Firefox, the default profile first for Safari) — which matches today's order. Within an extraction, records are sorted by today's key, with tie-breakers added and a stable sort (`sort.SliceStable`; Go 1.20 has no `slices`), so two runs over the same snapshot produce byte-identical files:
Output order is defined once per category, in the exporter, for every browser. Files list extractions in plan order — the browser table's order (one merged list whose per-OS projections equal today's `platformBrowsers()` orders), then user data dir ID, then profiles in discovery order (directory order for Chromium and Firefox, the default profile first for Safari) — which matches today's order. Within an extraction, records are sorted by today's key with a stable sort (`sort.SliceStable`; Go 1.20 has no `slices`), and ties keep the order the extractor emitted them in — the source order, which a snapshot fixes — so two runs over the same snapshot produce byte-identical files:
| Category | Order |
| --------------------------------------------------- | -------------------------------------------------------------------------------------- |
| password | `created_at` descending, then `store` (local before account), `url`, `username` |
| cookie | `created_at` descending, then `host`, `name`, `path` |
| history | `visit_count` descending, then `last_visit` descending, `url` |
| download | `start_time` descending, then `url` |
| bookmark | `created_at` descending, then `id` ascending |
| creditcard, extension, localstorage, sessionstorage | source order, made explicit (`ORDER BY rowid`, LevelDB key order, JSON document order) |
| Category | Order |
| --------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| password | `created_at` descending; on a tie, local before account, the order the Chromium extractor reads its two stores in |
| cookie, bookmark | `created_at` descending |
| history | `visit_count` descending |
| download | `start_time` descending |
| creditcard, extension, localstorage, sessionstorage | source order (row order, LevelDB key order, JSON document order) |
Today the order is decided in each extractor with an unstable `sort.Slice`, so ties come out in arbitrary order — and Firefox history sorts _ascending_ by visit count (`browser/firefox/extract_history.go`) while Chromium and Safari sort descending. The table above keeps today's keys and fixes Firefox to match the others.
Ties are not broken by a second key, because some sources lack the first: Safari bookmarks carry no creation time and Safari downloads no start time, so a tie-breaker such as `id` or `url` would replace their file order with an order nobody chose. A stable sort leaves them as the browser stored them.
Today the order is decided in each extractor with an unstable `sort.Slice`, so ties come out in an order nobody chose — and Firefox history sorts _ascending_ by visit count (`browser/firefox/extract_history.go`) while Chromium and Safari sort descending. The table keeps today's keys and fixes Firefox to match the others.
## Streaming vs. buffering
+1 -1
View File
@@ -67,7 +67,7 @@ Each bug designed out by this rewrite gets a test that would have caught it, so
## Import direction
The table in [RFC-002](002-package-layout.md) is kept by review, not by a linter or a test: every new import edge is either in the table or added to it in the same pull request. The compiler still rejects cycles.
The table in [RFC-002](002-package-layout.md) is kept by review, not by a linter or a test: every new import edge is either in the table or added to it in the same pull request. The compiler still rejects cycles. Until the switch-over, today's packages — and `cmd`, which still drives them — also import the new packages they are moving onto; those edges leave with the old code ([RFC-012](012-migration-roadmap.md)).
## Go 1.20 pitfalls
+3 -2
View File
@@ -30,7 +30,8 @@ Leaves first, bottom-up along the dependency arrows ([RFC-002](002-package-layou
- Introduce `internal/category` — `Category` with bare values numbered from 1, today's `String()` values, `All` and `Parse` — and `internal/record`: `Login`, `Cookie`, and the other record types with flat `Browser` + `Profile` fields followed by today's field names and order, set through `record.Stamp` ([RFC-003](003-core-types.md)).
- Apply the naming in [RFC-004](004-naming-decisions.md) verbatim: `types.Category` → `category.Category`, `types.LoginEntry` → `record.Login` and every other `…Entry` → its bare name, no `Category` prefix, no `Owner`/`Origin` umbrella.
- Move `output` to `internal/export` — `Writer` and the `csv`, `json`, and `cookie-editor` encoders — on the new record types ([RFC-008](008-output-and-export.md)). It depends only on `category`, `record`, and `log`.
- No output change: `tagliatelle` and the parity suite must stay green.
- Move the row order out of the extractors into the exporter ([RFC-008](008-output-and-export.md)), so an extractor emits rows in source order. This lands the Firefox history order fix with its regression test.
- No other output change: `tagliatelle` and the parity suite must stay green.
### Phase 3 — `crypto`, `masterkey`, `keychain`, `winapi`
@@ -52,7 +53,7 @@ Leaves first, bottom-up along the dependency arrows ([RFC-002](002-package-layou
### Phase 6 — Widen: Firefox and Safari
- Port the Firefox and Safari bases one category at a time, each with a `testdata/` fixture and golden.
- Land the listed output fixes here — Firefox history order and bookmark fields, Safari bookmark IDs — each with its regression test and as an expected diff in the parity suite.
- Land the remaining listed output fixes here — Firefox bookmark fields and Safari bookmark IDs — each with its regression test and as an expected diff in the parity suite.
### Phase 7 — Switch over