Skip to content

Commit

Permalink
get something working with calculating integrity on pin.
Browse files Browse the repository at this point in the history
  • Loading branch information
botandrose-machine committed Oct 22, 2023
1 parent 87a664b commit 38751af
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/importmap/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ def self.exit_on_failure?
option :env, type: :string, aliases: :e, default: "production"
option :from, type: :string, aliases: :f, default: "jspm"
option :download, type: :boolean, aliases: :d, default: false
option :integrity, type: :boolean, aliases: :i, default: false
def pin(*packages)
if imports = packager.import(*packages, env: options[:env], from: options[:from])
imports.each do |package, url|
if options[:download]
puts %(Pinning "#{package}" to #{packager.vendor_path}/#{package}.js via download from #{url})
packager.download(package, url)
pin = packager.vendored_pin_for(package, url)
integrity = packager.calculate_integrity(package: package) if options[:integrity]
pin = packager.vendored_pin_for(package, url, integrity: integrity)
else
puts %(Pinning "#{package}" to #{url})
pin = packager.pin_for(package, url)
integrity = packager.calculate_integrity(url: url) if options[:integrity]
pin = packager.pin_for(package, url, integrity: integrity)
end

if packager.packaged?(package)
Expand Down
22 changes: 17 additions & 5 deletions lib/importmap/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ def import(*packages, env: "production", from: "jspm")
end
end

def pin_for(package, url)
%(pin "#{package}", to: "#{url}")
def pin_for(package, url, integrity: false)
if integrity
%(pin "#{package}", to: "#{url}", integrity: "#{integrity}")
else
%(pin "#{package}", to: "#{url}")
end
end

def vendored_pin_for(package, url)
def vendored_pin_for(package, url, integrity: false)
filename = package_filename(package)
version = extract_package_version_from(url)
integrity_suffix = %(, integrity: "#{integrity}") if integrity

if "#{package}.js" == filename
%(pin "#{package}" # #{version})
%(pin "#{package}"#{integrity_suffix} # #{version})
else
%(pin "#{package}", to: "#{filename}" # #{version})
%(pin "#{package}", to: "#{filename}"#{integrity_suffix} # #{version})
end
end

Expand All @@ -62,6 +67,13 @@ def remove(package)
remove_package_from_importmap(package)
end

def calculate_integrity(package: nil, url: nil)
contents = File.read(vendored_package_path(package)) if package
contents = Net::HTTP.get_response(URI(url)).body if url
integrity = Digest::SHA384.base64digest(contents)
"sha384-#{integrity}"
end

private
def post_json(body)
Net::HTTP.post(self.class.endpoint, body.to_json, "Content-Type" => "application/json")
Expand Down

0 comments on commit 38751af

Please sign in to comment.