-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11268 from dependabot/add-bun-file-parser
Add BunLock FileParser
- Loading branch information
Showing
15 changed files
with
406 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
npm_and_yarn/lib/dependabot/npm_and_yarn/file_parser/bun_lock.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "yaml" | ||
require "dependabot/errors" | ||
require "dependabot/npm_and_yarn/helpers" | ||
require "sorbet-runtime" | ||
|
||
module Dependabot | ||
module NpmAndYarn | ||
class FileParser < Dependabot::FileParsers::Base | ||
class BunLock | ||
extend T::Sig | ||
|
||
sig { params(dependency_file: DependencyFile).void } | ||
def initialize(dependency_file) | ||
@dependency_file = dependency_file | ||
end | ||
|
||
sig { returns(T::Hash[String, T.untyped]) } | ||
def parsed | ||
@parsed ||= begin | ||
content = begin | ||
# Since bun.lock is a JSONC file, which is a subset of YAML, we can use YAML to parse it | ||
YAML.load(T.must(@dependency_file.content)) | ||
rescue Psych::SyntaxError => e | ||
raise_invalid!("malformed JSONC at line #{e.line}, column #{e.column}") | ||
end | ||
raise_invalid!("expected to be an object") unless content.is_a?(Hash) | ||
|
||
version = content["lockfileVersion"] | ||
raise_invalid!("expected 'lockfileVersion' to be an integer") unless version.is_a?(Integer) | ||
raise_invalid!("expected 'lockfileVersion' to be >= 0") unless version >= 0 | ||
raise_invalid!("unsupported 'lockfileVersion' = #{version}") unless version.zero? | ||
|
||
T.let(content, T.untyped) | ||
end | ||
end | ||
|
||
sig { returns(Dependabot::FileParsers::Base::DependencySet) } | ||
def dependencies | ||
dependency_set = Dependabot::FileParsers::Base::DependencySet.new | ||
|
||
# bun.lock v0 format: | ||
# https://github.com/oven-sh/bun/blob/c130df6c589fdf28f9f3c7f23ed9901140bc9349/src/install/bun.lock.zig#L595-L605 | ||
|
||
packages = parsed["packages"] | ||
raise_invalid!("expected 'packages' to be an object") unless packages.is_a?(Hash) | ||
|
||
packages.each do |key, details| | ||
raise_invalid!("expected 'packages.#{key}' to be an array") unless details.is_a?(Array) | ||
|
||
resolution = details.first | ||
raise_invalid!("expected 'packages.#{key}[0]' to be a string") unless resolution.is_a?(String) | ||
|
||
name, version = resolution.split(/(?<=\w)\@/) | ||
next if name.empty? | ||
|
||
semver = Version.semver_for(version) | ||
next unless semver | ||
|
||
dependency_set << Dependency.new( | ||
name: name, | ||
version: semver.to_s, | ||
package_manager: "npm_and_yarn", | ||
requirements: [] | ||
) | ||
end | ||
|
||
dependency_set | ||
end | ||
|
||
sig do | ||
params(dependency_name: String, requirement: T.untyped, _manifest_name: String) | ||
.returns(T.nilable(T::Hash[String, T.untyped])) | ||
end | ||
def details(dependency_name, requirement, _manifest_name) | ||
packages = parsed["packages"] | ||
return unless packages.is_a?(Hash) | ||
|
||
candidates = | ||
packages | ||
.select { |name, _| name == dependency_name } | ||
.values | ||
|
||
# If there's only one entry for this dependency, use it, even if | ||
# the requirement in the lockfile doesn't match | ||
if candidates.one? | ||
parse_details(candidates.first) | ||
else | ||
candidate = candidates.find do |label, _| | ||
label.scan(/(?<=\w)\@(?:npm:)?([^\s,]+)/).flatten.include?(requirement) | ||
end&.last | ||
parse_details(candidate) | ||
end | ||
end | ||
|
||
private | ||
|
||
sig { params(message: String).void } | ||
def raise_invalid!(message) | ||
raise Dependabot::DependencyFileNotParseable.new(@dependency_file.path, "Invalid bun.lock file: #{message}") | ||
end | ||
|
||
sig do | ||
params(entry: T.nilable(T::Array[T.untyped])).returns(T.nilable(T::Hash[String, T.untyped])) | ||
end | ||
def parse_details(entry) | ||
return unless entry.is_a?(Array) | ||
|
||
# Either: | ||
# - "{name}@{version}", registry, details, integrity | ||
# - "{name}@{resolution}", details | ||
resolution = entry.first | ||
return unless resolution.is_a?(String) | ||
|
||
name, version = resolution.split(/(?<=\w)\@/) | ||
semver = Version.semver_for(version) | ||
|
||
if semver | ||
registry, details, integrity = entry[1..3] | ||
{ | ||
"name" => name, | ||
"version" => semver.to_s, | ||
"registry" => registry, | ||
"details" => details, | ||
"integrity" => integrity | ||
} | ||
else | ||
details = entry[1] | ||
{ | ||
"name" => name, | ||
"resolution" => version, | ||
"details" => details | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
npm_and_yarn/spec/fixtures/projects/bun/invalid_lockfile/bun.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# This is an invalid bun.lock file! | ||
[ |
1 change: 1 addition & 0 deletions
1
npm_and_yarn/spec/fixtures/projects/bun/invalid_lockfile/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
7 changes: 7 additions & 0 deletions
7
npm_and_yarn/spec/fixtures/projects/bun/invalid_lockfile_version/bun.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"lockfileVersion": -1, | ||
"workspaces": { | ||
"": {}, | ||
}, | ||
"dependencies": {}, | ||
} |
1 change: 1 addition & 0 deletions
1
npm_and_yarn/spec/fixtures/projects/bun/invalid_lockfile_version/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
36 changes: 36 additions & 0 deletions
36
npm_and_yarn/spec/fixtures/projects/bun/simple_v0/bun.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"lockfileVersion": 0, | ||
"workspaces": { | ||
"": { | ||
"dependencies": { | ||
"fetch-factory": "^0.0.1", | ||
}, | ||
"devDependencies": { | ||
"etag": "^1.0.0", | ||
}, | ||
}, | ||
}, | ||
"packages": { | ||
"encoding": ["encoding@0.1.13", "", { "dependencies": { "iconv-lite": "^0.6.2" } }, "sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A=="], | ||
|
||
"es6-promise": ["es6-promise@3.3.1", "", {}, "sha512-SOp9Phqvqn7jtEUxPWdWfWoLmyt2VaJ6MpvP9Comy1MceMXqE6bxvaTu4iaxpYYPzhny28Lc+M87/c2cPK6lDg=="], | ||
|
||
"etag": ["etag@1.8.1", "", {}, "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg=="], | ||
|
||
"fetch-factory": ["fetch-factory@0.0.1", "", { "dependencies": { "es6-promise": "^3.0.2", "isomorphic-fetch": "^2.1.1", "lodash": "^3.10.1" } }, "sha512-gexRwqIhwzDJ2pJvL0UYfiZwW06/bdYWxAmswFFts7C87CF8i6liApihTk7TZFYMDcQjvvDIvyHv0q379z0aWA=="], | ||
|
||
"iconv-lite": ["iconv-lite@0.6.3", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw=="], | ||
|
||
"is-stream": ["is-stream@1.1.0", "", {}, "sha512-uQPm8kcs47jx38atAcWTVxyltQYoPT68y9aWYdV6yWXSyW8mzSat0TL6CiWdZeCdF3KrAvpVtnHbTv4RN+rqdQ=="], | ||
|
||
"isomorphic-fetch": ["isomorphic-fetch@2.2.1", "", { "dependencies": { "node-fetch": "^1.0.1", "whatwg-fetch": ">=0.10.0" } }, "sha512-9c4TNAKYXM5PRyVcwUZrF3W09nQ+sO7+jydgs4ZGW9dhsLG2VOlISJABombdQqQRXCwuYG3sYV/puGf5rp0qmA=="], | ||
|
||
"lodash": ["lodash@3.10.1", "", {}, "sha512-9mDDwqVIma6OZX79ZlDACZl8sBm0TEnkf99zV3iMA4GzkIT/9hiqP5mY0HoT1iNLCrKc/R1HByV+yJfRWVJryQ=="], | ||
|
||
"node-fetch": ["node-fetch@1.7.3", "", { "dependencies": { "encoding": "^0.1.11", "is-stream": "^1.0.1" } }, "sha512-NhZ4CsKx7cYm2vSrBAr2PvFOe6sWDf0UYLRqA6svUYg7+/TSfVAu49jYC4BvQ4Sms9SZgdqGBgroqfDhJdTyKQ=="], | ||
|
||
"safer-buffer": ["safer-buffer@2.1.2", "", {}, "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="], | ||
|
||
"whatwg-fetch": ["whatwg-fetch@3.6.20", "", {}, "sha512-EqhiFU6daOA8kpjOWTL0olhVOF3i7OrFzSYiGsEMB8GcXS+RrzauAERX65xMeNWVqxA6HXH2m69Z9LaKKdisfg=="], | ||
} | ||
} |
Oops, something went wrong.