summaryrefslogtreecommitdiffstats
path: root/lua/mason-core/purl.lua
blob: 65acbdd168b7a3906cb3c75ff93744e3478155d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
local Optional = require "mason-core.optional"
local Result = require "mason-core.result"
local _ = require "mason-core.functional"

local M = {}

-- Fully spec-compliant parser for purls (https://github.com/package-url/purl-spec)

---@param str string
local function parse_hex(str)
    return tonumber(str, 16)
end

---@param char string
local function percent_encode(char)
    return ("%%%x"):format(string.byte(char, 1, 1))
end

local decode_percent_encoding = _.gsub("%%([A-Fa-f0-9][A-Fa-f0-9])", _.compose(string.char, parse_hex))
local encode_percent_encoding = _.gsub("[!#$&'%(%)%*%+;=%?@%[%] ]", percent_encode)

local function validate_conan(purl)
    if purl.namespace and not _.path({ "qualifiers", "channel" }, purl) then
        return Result.failure "Missing channel qualifier."
    elseif not purl.namespace and _.path({ "qualifiers", "channel" }, purl) then
        return Result.failure "Missing namespace."
    end
    return Result.success(purl)
end

local function validate_cran(purl)
    if not purl.version then
        return Result.failure "Missing version."
    end
    return Result.success(purl)
end

local function validate_swift(purl)
    if not purl.namespace then
        return Result.failure "Missing namespace."
    end
    if not purl.version then
        return Result.failure "Missing version."
    end
    return Result.success(purl)
end

---@class Purl
---@field scheme '"pkg"'
---@field type string
---@field namespace string?
---@field name string
---@field version string?
---@field qualifiers table<string, string>?
---@field subpath string?

---@param str string
local function split_once_right(str, char)
    for i = #str, 1, -1 do
        if str:sub(i, i) == char then
            local segment = str:sub(i + 1, #str)
            return str:sub(1, i - 1), segment
        end
    end
    return str
end

---@param str string
local function split_once_left(str, char)
    for i = 1, #str do
        if str:sub(i, i) == char then
            local segment = str:sub(1, i - 1)
            return segment, str:sub(i + 1)
        end
    end
    return str
end

local function left_trim(char, str)
    for i = 1, #str do
        if str:sub(i, i) ~= char then
            return i
        end
    end
    return #str + 1
end

local function right_trim(char, str)
    for i = #str, 1, -1 do
        if str:sub(i, i) ~= char then
            return i
        end
    end
    return #str + 1
end

---@param char string
---@param str string
local function trim(char, str)
    return str:sub(left_trim(char, str), right_trim(char, str))
end

local parse_subpath = _.compose(
    _.join "/",
    _.filter_map(function(segment)
        if segment == "." or segment == ".." or segment == "" then
            return Optional.empty()
        end
        return Optional.of(decode_percent_encoding(segment))
    end),
    _.split "/",
    _.partial(trim, "/")
)

local parse_qualifiers = _.compose(
    _.evolve {
        checksum = _.split ",",
    },
    _.from_pairs,
    _.filter_map(function(pair)
        local key, value = split_once_left(pair, "=")
        if value ~= nil and value ~= "" then
            return Optional.of { _.to_lower(key), decode_percent_encoding(value) }
        else
            return Optional.empty()
        end
    end),
    _.split "&"
)

local parse_namespace = _.compose(
    _.join "/",
    _.filter_map(function(segment)
        if segment == "" then
            return Optional.empty()
        end
        return Optional.of(decode_percent_encoding(segment))
    end),
    _.split "/"
)

local pypi = _.evolve {
    name = _.compose(_.to_lower, _.gsub("_", "-")),
}

local huggingface = _.evolve {
    version = _.to_lower,
}

local azuredatabricks = _.evolve {
    name = _.to_lower,
    namespace = _.to_lower,
}

local bitbucket = _.evolve {
    name = _.to_lower,
    namespace = _.to_lower,
}

local github = _.evolve {
    name = _.to_lower,
    namespace = _.to_lower,
}

local composer = _.evolve {
    name = _.to_lower,
    namespace = _.to_lower,
}

local is_mlflow_azuredatabricks = _.all_pass {
    _.prop_eq("type", "mlflow"),
    _.path_satisfies(_.matches "^https?://.*azuredatabricks%.net", { "qualifiers", "repository_url" }),
}

local type_validations = _.cond {
    { _.prop_eq("type", "conan"), validate_conan },
    { _.prop_eq("type", "cran"), validate_cran },
    { _.prop_eq("type", "swift"), validate_swift },
    { _.T, Result.success },
}

local type_transforms = _.cond {
    { _.prop_eq("type", "bitbucket"), bitbucket },
    { _.prop_eq("type", "composer"), composer },
    { _.prop_eq("type", "github"), github },
    { _.prop_eq("type", "pypi"), pypi },
    { _.prop_eq("type", "huggingface"), huggingface },
    { is_mlflow_azuredatabricks, azuredatabricks },
    { _.T, _.identity },
}

local type_specific_transforms = _.compose(type_validations, type_transforms)

---@param raw_purl string
---@return Result # Result<Purl>
function M.parse(raw_purl)
    -- Implementation of recommended parsing algo
    -- https://github.com/package-url/purl-spec/blob/master/PURL-SPECIFICATION.rst#how-to-parse-a-purl-string-in-its-components
    local remainder, subpath = split_once_right(raw_purl, "#")
    if subpath then
        subpath = parse_subpath(subpath)
    end

    local remainder, qualifiers = split_once_right(remainder, "?")
    if qualifiers then
        qualifiers = parse_qualifiers(qualifiers)
        if not _.all(_.matches "^[a-zA-Z%-_%.][0-9a-zA-Z%-_%.]*$", _.keys(qualifiers)) then
            return Result.failure "Malformed purl (invalid qualifier names)."
        end
    end

    local scheme, remainder = split_once_left(remainder, ":")
    if not remainder then
        return Result.failure "Malformed purl (missing type, namespace, name, version components)."
    end
    if scheme ~= "pkg" then
        return Result.failure "Malformed purl (invalid scheme)."
    end
    remainder = trim("/", remainder)

    local type, remainder = split_once_left(remainder, "/")
    if not remainder then
        return Result.failure "Malformed purl (missing namespace, name, version components)"
    end
    type = _.to_lower(type)

    local remainder, version = split_once_right(remainder, "@")
    if version then
        version = decode_percent_encoding(version)
    end

    local remainder, name = split_once_right(remainder, "/")
    if not name then
        name = remainder
        remainder = nil
    end
    if name == "" then
        return Result.failure "Malformed purl (missing name)."
    end
    name = decode_percent_encoding(name)

    local namespace = remainder
    if namespace then
        namespace = parse_namespace(namespace)
    end

    return type_specific_transforms {
        scheme = scheme,
        type = type,
        namespace = namespace,
        name = name,
        version = version,
        qualifiers = qualifiers,
        subpath = subpath,
    }
end

local stringify_qualifiers = _.compose(
    _.join "&",
    _.sort_by(_.identity),
    _.map(_.compose(_.join "=", _.evolve { _.identity, encode_percent_encoding })),
    _.to_pairs,
    _.evolve {
        checksum = _.join ",",
    }
)

---@param purl Purl
---@return string
function M.compile(purl)
    local str = "pkg:"
    str = str .. purl.type .. "/"
    if purl.namespace then
        str = str .. encode_percent_encoding(purl.namespace) .. "/"
    end
    str = str .. purl.name
    if purl.version then
        str = str .. "@" .. encode_percent_encoding(purl.version)
    end
    if purl.qualifiers then
        str = str .. "?" .. stringify_qualifiers(purl.qualifiers)
    end
    if purl.subpath then
        str = str .. "#" .. purl.subpath
    end
    return str
end

return M