debian-mirror-gitlab/app/presenters/packages/npm/package_presenter.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.9 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Packages
module Npm
class PackagePresenter
include API::Helpers::RelatedResourcesHelpers
2021-12-11 22:18:48 +05:30
# Allowed fields are those defined in the abbreviated form
# defined here: https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md#abbreviated-version-object
# except: name, version, dist, dependencies and xDependencies. Those are generated by this presenter.
PACKAGE_JSON_ALLOWED_FIELDS = %w[deprecated bin directories dist engines _hasShrinkwrap].freeze
2020-07-28 23:09:34 +05:30
attr_reader :name, :packages
2022-01-26 12:08:38 +05:30
def initialize(name, packages)
2020-07-28 23:09:34 +05:30
@name = name
@packages = packages
end
def versions
package_versions = {}
2021-11-11 11:23:49 +05:30
packages.each_batch do |relation|
2021-12-11 22:18:48 +05:30
batched_packages = relation.including_dependency_links
.preload_files
2022-01-26 12:08:38 +05:30
.preload_npm_metadatum
2021-12-11 22:18:48 +05:30
batched_packages.each do |package|
2022-04-04 11:22:00 +05:30
package_file = package.installable_package_files.last
2020-07-28 23:09:34 +05:30
2021-12-11 22:18:48 +05:30
next unless package_file
2020-07-28 23:09:34 +05:30
2021-12-11 22:18:48 +05:30
package_versions[package.version] = build_package_version(package, package_file)
end
2020-07-28 23:09:34 +05:30
end
package_versions
end
def dist_tags
build_package_tags.tap { |t| t["latest"] ||= sorted_versions.last }
end
private
def build_package_tags
2021-04-29 21:17:54 +05:30
package_tags.to_h { |tag| [tag.name, tag.package.version] }
2020-07-28 23:09:34 +05:30
end
def build_package_version(package, package_file)
2021-12-11 22:18:48 +05:30
abbreviated_package_json(package).merge(
2020-07-28 23:09:34 +05:30
name: package.name,
version: package.version,
dist: {
shasum: package_file.file_sha1,
tarball: tarball_url(package, package_file)
}
2021-12-11 22:18:48 +05:30
).tap do |package_version|
2020-07-28 23:09:34 +05:30
package_version.merge!(build_package_dependencies(package))
end
end
def tarball_url(package, package_file)
expose_url "#{api_v4_projects_path(id: package.project_id)}" \
"/packages/npm/#{package.name}" \
"/-/#{package_file.file_name}"
end
def build_package_dependencies(package)
dependencies = Hash.new { |h, key| h[key] = {} }
2021-11-11 11:23:49 +05:30
package.dependency_links.each do |dependency_link|
2020-07-28 23:09:34 +05:30
dependency = dependency_link.dependency
dependencies[dependency_link.dependency_type][dependency.name] = dependency.version_pattern
end
dependencies
end
def sorted_versions
2021-11-11 11:23:49 +05:30
versions = packages.pluck_versions.compact
2020-07-28 23:09:34 +05:30
VersionSorter.sort(versions)
end
def package_tags
2023-04-23 21:23:45 +05:30
Packages::Tag.for_package_ids(packages.last_of_each_version_ids)
2021-11-11 11:23:49 +05:30
.preload_package
2020-07-28 23:09:34 +05:30
end
2021-12-11 22:18:48 +05:30
def abbreviated_package_json(package)
json = package.npm_metadatum&.package_json || {}
json.slice(*PACKAGE_JSON_ALLOWED_FIELDS)
end
2020-07-28 23:09:34 +05:30
end
end
end