getDatabase returns the models.dev database. When allowFetch is false the catalog is served from memory or the on-disk cache only and the network is never touched; a cache miss yields an empty database rather than an error.
(ctx context.Context, allowFetch bool)
| 153 | // catalog is served from memory or the on-disk cache only and the network is |
| 154 | // never touched; a cache miss yields an empty database rather than an error. |
| 155 | func (s *Store) getDatabase(ctx context.Context, allowFetch bool) (*Database, error) { |
| 156 | s.mu.Lock() |
| 157 | defer s.mu.Unlock() |
| 158 | |
| 159 | // The authoritative catalog, once loaded, serves every lookup — including |
| 160 | // fetch-disallowed ones, which then benefit from the fresher data. |
| 161 | if s.db != nil { |
| 162 | return s.db, nil |
| 163 | } |
| 164 | |
| 165 | if !allowFetch { |
| 166 | // Serve (and memoize) a cache-only snapshot without touching the |
| 167 | // network. Kept out of s.db so it can never satisfy a later |
| 168 | // fetch-eligible lookup for a known provider. |
| 169 | if s.cacheDB == nil { |
| 170 | s.cacheDB, _ = loadDatabase(ctx, s.cacheFile, false, s.fetcher()) |
| 171 | } |
| 172 | return s.cacheDB, nil |
| 173 | } |
| 174 | |
| 175 | db, authoritative := loadDatabase(ctx, s.cacheFile, true, s.fetcher()) |
| 176 | // Only memoize a result that came from the on-disk cache or a live fetch. |
| 177 | // The embedded fallback snapshot is deliberately NOT pinned, so a later |
| 178 | // lookup retries the fetch once the network (or cache) recovers instead of |
| 179 | // serving the build-time snapshot for the Store's entire lifetime. |
| 180 | if authoritative { |
| 181 | s.db = db |
| 182 | } |
| 183 | return db, nil |
| 184 | } |
| 185 | |
| 186 | // fetcher returns the Store's catalog fetcher, defaulting to fetchFromAPI when |
| 187 | // unset. The constructors always populate s.fetch, but defaulting here guards |
no test coverage detected