-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lua
417 lines (348 loc) Β· 10.4 KB
/
client.lua
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
-- AO Package Manager for easy installation of packages in ao processes
-------------------------------------------------------------------------
-- ___ .______ .___ ___. __ __ __ ___
-- / \ | _ \ | \/ | | | | | | | / \
-- / ^ \ | |_) | | \ / | | | | | | | / ^ \
-- / /_\ \ | ___/ | |\/| | | | | | | | / /_\ \
-- / _____ \ | | | | | | __| `----.| `--' | / _____ \
-- /__/ \__\ | _| |__| |__| (__)_______| \______/ /__/ \__\
--
---------------------------------------------------------------------------
-- APM Registry source code: https://github.com/betteridea-dev/ao-package-manager
-- CLI tool for managing packages: https://www.npmjs.com/package/apm-tool
-- Web UI for browsing & publishing packages: https://apm.betteridea.dev
-- Built with β€οΈ by BetterIDEa
local apm_id = "DKF8oXtPvh3q8s0fJFIeHFyHNM6oKrwMCUrPxEMroak"
local apm_version = "2.0.5"
json = require("json")
base64 = require(".base64")
function Set(list)
local set = {}
for _, l in ipairs(list) do set[l] = true end
return set
end
function Hexencode(str)
return (str:gsub(".", function(char) return string.format("%02x", char:byte()) end))
end
function Hexdecode(hex)
return (hex:gsub("%x%x", function(digits) return string.char(tonumber(digits, 16)) end))
end
function IsValidVersion(variant)
-- version string or 43 char message_id
return variant:match("^%d+%.%d+%.%d+$") or (variant:match("^[a-zA-Z0-9%-%_]+$") and #variant == 43)
end
function IsValidPackageName(name)
return name:match("^[a-zA-Z0-9%-_]+$")
end
function IsValidVendor(name)
return name and name:match("^@[a-z0-9-]+$")
end
-- can be @vendor/pkgname or pkgname
-- can be @vendor/pkgname@version or pkgname@version
-- can be @vendor/pkgname@message_id or pkgname@message_id
-- message_id length is 43 chars
function SplitPackageName(query)
local vendor, pkgname, version
-- if only vendor is given
if query:find("^@%w+$") then
return query, nil, nil
end
-- check if version is provided
local version_index = query:find("@%d+.%d+.%d+$")
if version_index then
version = query:sub(version_index + 1)
query = query:sub(1, version_index - 1)
else
-- check if length > 43 and last 43 chars are message_id
if #query > 45 then
local message_id = query:sub(-43)
if message_id:match("^[a-zA-Z0-9%-%_]+$") then
version = message_id
query = query:sub(1, -44)
end
end
end
-- check if vendor is provided
vendor, pkgname = query:match("@(%w+)/([%w%-%_]+)")
if not vendor then
pkgname = query
else
vendor = "@" .. vendor
end
return vendor, pkgname, version
end
-- common error handler
function HandleRun(func, msg)
local ok, err = pcall(func, msg)
if not ok then
local clean_err = err:match(":%d+: (.+)") or err
print(msg.Action .. " - " .. err)
-- if not msg.Target == ao.id then
ao.send({
Target = msg.From,
Data = clean_err,
Result = "error"
})
-- end
end
end
function CheckUpdate(msg)
local latest_client_version = msg.LatestClientVersion
if not latest_client_version then
return
end
if latest_client_version and latest_client_version > apm._version then
print("β οΈ APM update available v:" .. latest_client_version .. " run 'apm.update()'")
end
end
-------------------------------------------------------------
function DownloadResponseHandler(msg)
local from = msg.From
if not from == apm.ID then
print("Attempt to download from illegal source")
return
end
if not msg.Result == "success" then
print("Download failed: " .. msg.Name)
return
end
local source = msg.Data
local name = msg.Name
local version = msg.Version
local warnings = msg.Warnings -- {ModifiesGlobalState:boolean, Message:boolean}
local dependencies = msg.Dependencies -- {[name:string] = {version:string}}
if source then
source = Hexdecode(source)
end
if warnings and warnings.ModifiesGlobalState then
print("β οΈ Package modifies global state")
end
if warnings and warnings.Message then
print("β οΈ " .. warnings.Message)
end
-- if vendor is @apm remove it and just keep the name
local loaded_name = name:match("^@apm/(.+)$") or name
local func, err = load(string.format([[
local function _load()
%s
end
_G.package.loaded["%s"] = _load()
]], source, loaded_name))
if not func then
error("Error compiling load function: " .. err)
end
func()
apm.installed[name] = version
if dependencies then
dependencies = json.decode(dependencies) -- "dependencies": {"test-pkg": {"version": "1.0.0"}}
end
-- print(dependencies)
for dep, depi in pairs(dependencies) do
-- print("π¦ Checking dependency " .. dep .. "@" .. depi.version)
-- install dependency and make sure there is no circular install
if not (apm.installed[dep] == depi.version) then
print("βΉοΈ Installing dependency " .. dep .. "@" .. depi.version)
apm.install(dep)
end
end
print("β
Downloaded " .. name .. "@" .. version)
CheckUpdate(msg)
end
Handlers.add(
"APM.DownloadResponse",
Handlers.utils.hasMatchingTag("Action", "APM.DownloadResponse"),
function(msg)
HandleRun(DownloadResponseHandler, msg)
end
)
-------------------------------------------------------------
function SearchResponseHandler(msg)
if msg.From ~= apm.ID then
print("Attempt to search from illegal source")
return
end
local result = msg.Result
if not result == "success" then
print("Search failed: " .. msg.Data)
return
end
local res = json.decode(msg.Data)
if #res == 0 then
print("No packages found")
return
end
local p = "\n"
for _, pkg in ipairs(res) do
p = p .. pkg.Vendor .. "/" .. pkg.Name .. " | " .. pkg.Description .. "\n"
end
print(p)
CheckUpdate(msg)
end
Handlers.add(
"APM.SearchResponse",
Handlers.utils.hasMatchingTag("Action", "APM.SearchResponse"),
function(msg)
HandleRun(SearchResponseHandler, msg)
end
)
-------------------------------------------------------------
function InfoResponseHandler(msg)
if msg.From ~= apm.ID then
print("Attempt to get info from illegal source")
return
end
local result = msg.Result
if not result == "success" then
print("Info failed: " .. msg.Data)
return
end
local res = json.decode(msg.Data)
if not res then
print("No info found")
return
end
print("π¦ " .. Colors.green .. res.Vendor .. "/" .. res.Name .. Colors.reset)
print("π Description : " .. Colors.green .. res.Description .. Colors.reset)
print("π Latest Version : " .. Colors.green .. res.Version .. Colors.reset)
print("π₯ Installs : " .. Colors.green .. res.TotalInstalls .. Colors.reset)
print("π APM Url : " .. Colors.green .. "https://apm.betteridea.dev/pkg?id=" .. res.PkgID .. Colors.reset)
print("π Repository Url : " .. Colors.green .. res.Repository .. Colors.reset)
CheckUpdate(msg)
end
Handlers.add(
"APM.InfoResponse",
Handlers.utils.hasMatchingTag("Action", "APM.InfoResponse"),
function(msg)
HandleRun(InfoResponseHandler, msg)
end
)
-------------------------------------------------------------
function UpdateResponseHandler(msg)
local from = msg.From
if not from == apm.ID then
print("Attempt to update from illegal source")
return
end
if not msg.Result == "success" then
print("Update failed: " .. msg.Data)
return
end
local source = msg.Data
local version = msg.Version
if source then
source = Hexdecode(source)
end
local func, err = load(string.format([[
local function _load()
%s
end
-- apm = _load()
_load()
]], source))
if not func then
error("Error compiling load function: " .. err)
end
func()
apm._version = version
print("β
Updated APM to v:" .. version)
print("Please use 'apm' namespace for all commands")
end
Handlers.add(
"APM.UpdateResponse",
Handlers.utils.hasMatchingTag("Action", "APM.UpdateResponse"),
function(msg)
HandleRun(UpdateResponseHandler, msg)
end
)
-------------------------------------------------------------
apm = apm or {}
apm.ID = apm_id
apm._version = apm._version or apm_version
apm.installed = apm.installed or {}
function apm.install(name)
local vendor, pkgname, version = SplitPackageName(name)
if not vendor then
vendor = "@apm"
end
if not IsValidVendor(vendor) then
return error("Invalid vendor name")
end
if not IsValidPackageName(pkgname) then
return error("Invalid package name")
end
if version and not IsValidVersion(version) then
return error("Invalid version")
end
local pkgnv = vendor .. "/" .. pkgname
local pkg_ver = apm.installed[pkgnv]
if pkg_ver then
-- return error("Package already installed. Use apm.uninstall to remove it")
if version and pkg_ver == version then
return "β
Package " .. pkgnv .. " already installed"
end
end
if version then
pkgnv = pkgnv .. "@" .. version
end
Send({
Target = apm.ID,
Action = "APM.Download",
Data = pkgnv
})
return "π¦ Download requested for " .. pkgnv
end
function apm.search(query)
if not query then
return error("No search query provided")
end
Send({
Target = apm.ID,
Action = "APM.Search",
Data = query
})
return "π Search requested for " .. query
end
function apm.update()
Send({
Target = apm.ID,
Action = "APM.Update"
})
return "π¦ Update requested"
end
function apm.info(query)
if not query then
return error("No info query provided")
end
Send({
Target = apm.ID,
Action = "APM.Info",
Data = query
})
return "π¦ Info requested for " .. query
end
function apm.uninstall(name)
local vendor, pkgname, _ = SplitPackageName(name)
if not vendor then
vendor = "@apm"
end
if not IsValidVendor(vendor) then
return error("Invalid vendor name")
end
if not IsValidPackageName(pkgname) then
return error("Invalid package name")
end
local pkgnv = vendor .. "/" .. pkgname
local pkg = apm.installed[pkgnv]
if not pkg then
return error("Package not installed")
end
apm.installed[pkgnv] = nil
if vendor == "@apm" then
_G.package.loaded[name] = nil
else
_G.package.loaded[pkgnv] = nil
end
return "ποΈ Uninstalled " .. pkgnv
end
print("β
APM client v" .. apm._version .. " loaded")
print("usage: apm.install <package name>")