XL-Zhao-23 commented on code in PR #252:
URL: https://github.com/apache/skywalking-eyes/pull/252#discussion_r2554061067


##########
pkg/deps/npm.go:
##########
@@ -318,3 +369,67 @@ func (resolver *NpmResolver) ParsePkgFile(pkgFile string) 
(*Package, error) {
        }
        return &packageInfo, nil
 }
+
+// isForCurrentPlatform checks whether the given package name targets
+// the current platform. This method should be called before parsing
+// to determine if a package is cross-platform and not for the current
+// OS/architecture, so it can be safely skipped.
+func (resolver *NpmResolver) isForCurrentPlatform(pkgName string) bool {
+       pkgPlatform, pkgArch := resolver.analyzePackagePlatform(pkgName)
+       if pkgPlatform == "" && pkgArch == "" {
+               return true
+       }
+
+       currentOS := runtime.GOOS
+       currentArch := runtime.GOARCH
+
+       return pkgPlatform == currentOS && resolver.isArchCompatible(pkgArch, 
currentArch)
+}
+
+// analyzePackagePlatform extracts the target operating system and architecture
+// from a package name by matching it against predefined regex patterns
+// (platformPatterns). Returns empty strings if no match is found.
+func (resolver *NpmResolver) analyzePackagePlatform(pkgName string) (string, 
string) {
+       for _, pattern := range platformPatterns {
+               if pattern.regex.MatchString(pkgName) {
+                       return pattern.os, pattern.arch
+               }
+       }
+       return "", ""
+}
+
+// isArchCompatible determines whether two architectures are considered 
compatible.
+// For example, "x64" is compatible with "amd64", and "arm" is compatible with 
"armv7".
+// Returns true if the architectures match or belong to the same compatibility 
group.
+func (resolver *NpmResolver) isArchCompatible(pkgArch, currentArch string) 
bool {
+       archCompatibility := map[string][]string{
+               "x64":    {"x64", "x86_64", "amd64"},
+               "ia32":   {"ia32", "x86", "386", "i386"},
+               "arm64":  {"arm64", "aarch64"},
+               "arm":    {"arm", "armv7", "armhf", "armv7l"},
+               "x86_64": {"x86_64", "x64", "amd64"},
+               "amd64":  {"amd64", "x64", "x86_64"},
+       }
+
+       if pkgArch == currentArch {
+               return true
+       }
+
+       if compatibleArches, exists := archCompatibility[pkgArch]; exists {
+               for _, arch := range compatibleArches {
+                       if arch == currentArch {
+                               return true
+                       }
+               }
+       }
+
+       if compatibleArches, exists := archCompatibility[currentArch]; exists {
+               for _, arch := range compatibleArches {
+                       if arch == pkgArch {
+                               return true
+                       }
+               }
+       }
+
+       return false

Review Comment:
   Adopted a clearer and non-redundant approach to convert the architecture in 
package names into Go's standard CPU architecture naming.
   
   ```go 
   
   
   // normalizeArch converts various architecture aliases into Go's canonical 
naming.
   func normalizeArch(arch string) string {
        // Convert to lowercase to handle case variations (e.g., "AMD64").
        arch = strings.ToLower(arch)
        switch arch {
        // x86-64 family (64-bit Intel/AMD)
        case "x64", "x86_64", "amd64", "x86-64":
                return "amd64"
        // x86 32-bit family (legacy)
        case "ia32", "x86", "386", "i386", "i686":
                return "386"
        // ARM 64-bit
        case "arm64", "aarch64":
                return "arm64"
        // ARM 32-bit
        case "arm", "armv7", "armhf", "armv7l", "armel":
                return "arm"
        // Unknown architecture: return as-is (alternatively, could return 
empty to indicate incompatibility).
        default:
                return arch
        }
   }
   
   // analyzePackagePlatform extracts the target OS and architecture from a 
package name.
   func (resolver *NpmResolver) analyzePackagePlatform(pkgName string) (string, 
string) {
        for _, pattern := range platformPatterns {
                if pattern.regex.MatchString(pkgName) {
                        return pattern.os, pattern.arch
                }
        }
        return "", ""
   }
   
   // isForCurrentPlatform checks whether the package is intended for the 
current OS and architecture.
   func (resolver *NpmResolver) isForCurrentPlatform(pkgName string) bool {
        pkgPlatform, pkgArch := resolver.analyzePackagePlatform(pkgName)
        // If no platform/arch info is embedded in the package name, assume 
it's universal.
        if pkgPlatform == "" && pkgArch == "" {
                return true
        }
   
        currentOS := runtime.GOOS
        currentArch := runtime.GOARCH
   
        // The package matches only if both OS and architecture are compatible.
        return pkgPlatform == currentOS && resolver.isArchCompatible(pkgArch, 
currentArch)
   }
   
   // isArchCompatible determines whether the package's architecture is 
compatible with the current machine's architecture.
   func (resolver *NpmResolver) isArchCompatible(pkgArch, currentArch string) 
bool {
        return normalizeArch(pkgArch) == normalizeArch(currentArch)
   }
   
   
   
   
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to