github.com/google/osv-scalibr@v0.4.1/clients/datasource/npm_registry_cache.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package datasource 16 17 import ( 18 "maps" 19 "strings" 20 "time" 21 ) 22 23 type npmRegistryCache struct { 24 Timestamp *time.Time // Timestamp of when this cache was made 25 Details map[string]npmRegistryPackageDetails // For a package name, the versions & their dependencies, and the list of tags 26 ScopeURLs map[string]string // The URL of the registry used for a given package @scope. Used to invalidate cache if registry has changed. 27 } 28 29 // GobEncode encodes the cache to bytes. 30 func (c *NPMRegistryAPIClient) GobEncode() ([]byte, error) { 31 c.mu.Lock() 32 defer c.mu.Unlock() 33 34 if c.cacheTimestamp == nil { 35 now := time.Now().UTC() 36 c.cacheTimestamp = &now 37 } 38 39 cache := npmRegistryCache{ 40 Timestamp: c.cacheTimestamp, 41 Details: c.details.GetMap(), 42 ScopeURLs: make(map[string]string), 43 } 44 45 // store the registry URL for each scope (but not the auth info) 46 cache.ScopeURLs = c.registries.ScopeURLs 47 48 return gobMarshal(&cache) 49 } 50 51 // GobDecode decodes the cache from bytes. 52 func (c *NPMRegistryAPIClient) GobDecode(b []byte) error { 53 // decode the cached data 54 var cache npmRegistryCache 55 if err := gobUnmarshal(b, &cache); err != nil { 56 return err 57 } 58 59 if cache.Timestamp != nil && time.Since(*cache.Timestamp) >= cacheExpiry { 60 // Cache expired 61 return nil 62 } 63 64 c.mu.Lock() 65 defer c.mu.Unlock() 66 67 // remove any cache entries whose registry has changed 68 maps.DeleteFunc(cache.Details, func(pkg string, _ npmRegistryPackageDetails) bool { 69 scope := "" 70 if strings.HasPrefix(pkg, "@") { 71 scope, _, _ = strings.Cut(pkg, "/") 72 } 73 74 return cache.ScopeURLs[scope] != c.registries.ScopeURLs[scope] 75 }) 76 77 c.cacheTimestamp = cache.Timestamp 78 c.details.SetMap(cache.Details) 79 80 return nil 81 }