From 1e9af57cb5547885dfddcdec32924eeca86d837b Mon Sep 17 00:00:00 2001 From: Walid Ziouche Date: Fri, 10 Dec 2021 00:17:09 +0100 Subject: [PATCH 1/3] Fix parsing weird formats of the keywords field of some package.json files --- cli/internal/fs/package_json.go | 41 ++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/cli/internal/fs/package_json.go b/cli/internal/fs/package_json.go index 6eb4869b56f1d..d8369eadfc087 100644 --- a/cli/internal/fs/package_json.go +++ b/cli/internal/fs/package_json.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "reflect" + "strings" "sync" "github.com/pascaldekloe/name" @@ -39,7 +40,7 @@ type PackageJSON struct { Name string `json:"name,omitempty"` Version string `json:"version,omitempty"` Description string `json:"description,omitempty"` - Keywords []string `json:"keywords,omitempty"` + Keywords Keywords `json:"keywords,omitempty"` Homepage string `json:"homepage,omitempty"` License string `json:"license,omitempty"` Files []string `json:"files,omitempty"` @@ -65,6 +66,7 @@ type PackageJSON struct { ExternalDepsHash string } +type Keywords []string type Workspaces []string type WorkspacesAlt struct { @@ -85,6 +87,43 @@ func (r *Workspaces) UnmarshalJSON(data []byte) error { return nil } +func (k *Keywords) UnmarshalJSON(data []byte) error { + // First, attempt to get the keywords list expecting it's well defined + // as described by npm spec, an array of strings + // see: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#keywords + var valid = []string{} + if err := json.Unmarshal(data, &valid); err == nil { + *k = Keywords(valid) + return nil + } + // Reaching this code path, the keywords list is defined in a non-standard + // or invalid format. Try to deal with that. + + // Contain everything in a string + var tmpstr string + err := json.Unmarshal(data, &tmpstr) + + // If we fail to unmarshal as string, give up at this point. + if err != nil { + return err + } + + // Try removing extra chars, or chars meant to split the keywords list. + // Replace them with a whitespace instead, so we can split by it. + var removechars = []string{"[", "]", "/", ","} + for _, r := range removechars { + tmpstr = strings.ReplaceAll(tmpstr, r, " ") + } + + // Trim leading/trailing spaces + tmpstr = strings.TrimSpace(tmpstr) + // Split by whitespace + var val = strings.Fields(tmpstr) + // fill the relevant type definition with the split result + *k = Keywords(val) + return nil +} + // Parse parses package.json payload and returns structure. func Parse(payload []byte) (*PackageJSON, error) { var packagejson *PackageJSON From ae41ec2188bf39463859e241f87db4f9daeb477d Mon Sep 17 00:00:00 2001 From: Walid Ziouche Date: Sun, 12 Dec 2021 11:54:31 +0100 Subject: [PATCH 2/3] Revert "Fix parsing weird formats of the keywords field of some package.json files" This reverts commit 1e9af57cb5547885dfddcdec32924eeca86d837b. --- cli/internal/fs/package_json.go | 41 +-------------------------------- 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/cli/internal/fs/package_json.go b/cli/internal/fs/package_json.go index d8369eadfc087..6eb4869b56f1d 100644 --- a/cli/internal/fs/package_json.go +++ b/cli/internal/fs/package_json.go @@ -5,7 +5,6 @@ import ( "fmt" "io/ioutil" "reflect" - "strings" "sync" "github.com/pascaldekloe/name" @@ -40,7 +39,7 @@ type PackageJSON struct { Name string `json:"name,omitempty"` Version string `json:"version,omitempty"` Description string `json:"description,omitempty"` - Keywords Keywords `json:"keywords,omitempty"` + Keywords []string `json:"keywords,omitempty"` Homepage string `json:"homepage,omitempty"` License string `json:"license,omitempty"` Files []string `json:"files,omitempty"` @@ -66,7 +65,6 @@ type PackageJSON struct { ExternalDepsHash string } -type Keywords []string type Workspaces []string type WorkspacesAlt struct { @@ -87,43 +85,6 @@ func (r *Workspaces) UnmarshalJSON(data []byte) error { return nil } -func (k *Keywords) UnmarshalJSON(data []byte) error { - // First, attempt to get the keywords list expecting it's well defined - // as described by npm spec, an array of strings - // see: https://docs.npmjs.com/cli/v8/configuring-npm/package-json#keywords - var valid = []string{} - if err := json.Unmarshal(data, &valid); err == nil { - *k = Keywords(valid) - return nil - } - // Reaching this code path, the keywords list is defined in a non-standard - // or invalid format. Try to deal with that. - - // Contain everything in a string - var tmpstr string - err := json.Unmarshal(data, &tmpstr) - - // If we fail to unmarshal as string, give up at this point. - if err != nil { - return err - } - - // Try removing extra chars, or chars meant to split the keywords list. - // Replace them with a whitespace instead, so we can split by it. - var removechars = []string{"[", "]", "/", ","} - for _, r := range removechars { - tmpstr = strings.ReplaceAll(tmpstr, r, " ") - } - - // Trim leading/trailing spaces - tmpstr = strings.TrimSpace(tmpstr) - // Split by whitespace - var val = strings.Fields(tmpstr) - // fill the relevant type definition with the split result - *k = Keywords(val) - return nil -} - // Parse parses package.json payload and returns structure. func Parse(payload []byte) (*PackageJSON, error) { var packagejson *PackageJSON From 3d6e74c89af5ed1b00a9959cfc262da9aa94096f Mon Sep 17 00:00:00 2001 From: Walid Ziouche Date: Sun, 12 Dec 2021 11:56:41 +0100 Subject: [PATCH 3/3] Remove PackageJSON struct fields we do not use --- cli/internal/fs/package_json.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cli/internal/fs/package_json.go b/cli/internal/fs/package_json.go index 6eb4869b56f1d..325a70d800135 100644 --- a/cli/internal/fs/package_json.go +++ b/cli/internal/fs/package_json.go @@ -38,12 +38,6 @@ type Pipeline struct { type PackageJSON struct { Name string `json:"name,omitempty"` Version string `json:"version,omitempty"` - Description string `json:"description,omitempty"` - Keywords []string `json:"keywords,omitempty"` - Homepage string `json:"homepage,omitempty"` - License string `json:"license,omitempty"` - Files []string `json:"files,omitempty"` - Main string `json:"main,omitempty"` Scripts map[string]string `json:"scripts,omitempty"` Dependencies map[string]string `json:"dependencies,omitempty"` DevDependencies map[string]string `json:"devDependencies,omitempty"`