@crossberryweb/mozhi 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -1
- package/bin/mozhi-linux-arm64 +0 -0
- package/bin/mozhi-linux-x86_64 +0 -0
- package/bin/mozhi.js +65 -21
- package/checksums-sha256.txt +6 -6
- package/lib/cli.js +185 -0
- package/lib/gh.js +109 -0
- package/lib/pkgman.js +433 -0
- package/lib/registry.js +112 -0
- package/lib/scaffold.js +199 -0
- package/lib/zip.js +239 -0
- package/package.json +8 -4
- package/registry.json +256 -0
package/README.md
CHANGED
|
@@ -13,7 +13,49 @@ mz run app.mz # short alias — same binary, identical behavior
|
|
|
13
13
|
Both are installed by npm from the same launcher, so `mz` and `mozhi` are
|
|
14
14
|
always in sync: same version, same flags, same exit codes.
|
|
15
15
|
|
|
16
|
-
##
|
|
16
|
+
## Requirements
|
|
17
|
+
|
|
18
|
+
- **Node.js ≥ 22** — any currently supported release line: 22.x (Maintenance
|
|
19
|
+
LTS), 24.x (LTS) or 26.x (Current). Node.js 14, 16, 18 and 20 have reached
|
|
20
|
+
end of life and are no longer supported; on those lines the launcher still
|
|
21
|
+
works (it only uses `child_process`, `fs` and `path`) but prints a one-time
|
|
22
|
+
end-of-life notice. The interpreter itself is a fully static native binary —
|
|
23
|
+
Node.js only runs the tiny launcher shim.
|
|
24
|
+
|
|
25
|
+
## Package manager (v3.3.0)
|
|
26
|
+
|
|
27
|
+
The launcher now includes a package manager — no interpreter required:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
mz install strings # from the Mozhi registry (version-controlled JSON)
|
|
31
|
+
mz install datetime@v1.0.0 # a specific release tag
|
|
32
|
+
mz install @user/repo # from any Git repo's tagged releases
|
|
33
|
+
mz install @user/repo@v0.2.0 # ... pinned to a tag
|
|
34
|
+
mz install ./tool.mz.pkg.zip # manual/offline install from a package zip
|
|
35
|
+
mz install ./mytool # from a local package directory
|
|
36
|
+
mz list # installed packages + origins
|
|
37
|
+
mz search web # search the registry
|
|
38
|
+
mz uninstall strings
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Packages are verified before every install (`package.toml` manifest + entry
|
|
42
|
+
module, traversal-safe extraction) and resolve automatically at `import`:
|
|
43
|
+
|
|
44
|
+
```mozhi
|
|
45
|
+
import strings
|
|
46
|
+
echo(strings.reverse("Mozhi"))
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Create your own package — complete with a GitHub Actions workflow that
|
|
50
|
+
attaches `<name>.mz.pkg.zip` to the release whenever you push a `v*` tag:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
mz pkg new mytool
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Full guide: https://crossberry-in.github.io/mozhi-doc/packages.html
|
|
57
|
+
|
|
58
|
+
## What you get (v3.3.0)
|
|
17
59
|
|
|
18
60
|
- **Precise error diagnostics**: every error carries a stable code
|
|
19
61
|
(`LEX001`, `PARSE002`, `NAME001`, ...), a caret span under the exact
|
package/bin/mozhi-linux-arm64
CHANGED
|
Binary file
|
package/bin/mozhi-linux-x86_64
CHANGED
|
Binary file
|
package/bin/mozhi.js
CHANGED
|
@@ -12,16 +12,26 @@
|
|
|
12
12
|
* mozhi — the canonical command
|
|
13
13
|
* mz — the short alias (identical behavior, same binary)
|
|
14
14
|
*
|
|
15
|
-
*
|
|
15
|
+
* Package manager: `mz install|uninstall|list|search|pkg ...` is handled
|
|
16
|
+
* natively by this launcher (lib/) without starting the interpreter —
|
|
17
|
+
* see `mz help pkg`.
|
|
18
|
+
*
|
|
19
|
+
* Bundled official release binaries (v3.3.0 line, static builds with
|
|
20
|
+
* SQLite vendored):
|
|
16
21
|
* bin/mozhi-linux-x86_64
|
|
17
22
|
* bin/mozhi-linux-arm64
|
|
18
23
|
*
|
|
24
|
+
* Node.js requirement (engines: ">=22"): the launcher only needs
|
|
25
|
+
* child_process, fs, path, zlib and fetch, but the supported Node.js
|
|
26
|
+
* release lines are 22.x, 24.x and 26.x — older lines are end-of-life
|
|
27
|
+
* and print a notice.
|
|
28
|
+
*
|
|
19
29
|
* macOS / Windows / other platforms:
|
|
20
30
|
* see https://github.com/crossberry-in/mozhi-doc#installation
|
|
21
31
|
*/
|
|
22
32
|
'use strict';
|
|
23
33
|
|
|
24
|
-
const { spawn } = require('child_process');
|
|
34
|
+
const { spawn, spawnSync } = require('child_process');
|
|
25
35
|
const fs = require('fs');
|
|
26
36
|
const path = require('path');
|
|
27
37
|
|
|
@@ -29,27 +39,61 @@ const path = require('path');
|
|
|
29
39
|
const arch = process.arch === 'x64' ? 'x86_64' : process.arch;
|
|
30
40
|
const bin = path.join(__dirname, 'mozhi-' + process.platform + '-' + arch);
|
|
31
41
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
process.
|
|
42
|
+
// Node.js support policy (see engines in package.json): 22.x, 24.x and 26.x
|
|
43
|
+
// are the release lines that still receive security updates as of 2026-10.
|
|
44
|
+
// End-of-life lines still work — this launcher uses nothing newer than
|
|
45
|
+
// child_process/fs/path/zlib/fetch — but users get a one-time notice.
|
|
46
|
+
const nodeMajor = parseInt(String(process.version).slice(1), 10);
|
|
47
|
+
if (nodeMajor < 22) {
|
|
48
|
+
console.error('[mozhi] Notice: Node.js ' + process.version + ' has reached end of life.');
|
|
49
|
+
console.error('[mozhi] Supported Node.js release lines: 22.x, 24.x, 26.x (https://nodejs.org).');
|
|
50
|
+
console.error('[mozhi] Continuing anyway — the launcher needs nothing newer.');
|
|
39
51
|
}
|
|
40
52
|
|
|
41
|
-
|
|
53
|
+
/* ---------------- package manager commands (no interpreter) --------- */
|
|
54
|
+
|
|
55
|
+
const PKG_COMMANDS = new Set(['install', 'add', 'uninstall', 'remove', 'list', 'ls', 'search', 'pkg', 'package']);
|
|
42
56
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
57
|
+
const argv = process.argv.slice(2);
|
|
58
|
+
|
|
59
|
+
if (argv.length && PKG_COMMANDS.has(argv[0])) {
|
|
60
|
+
const cli = require('../lib/cli.js');
|
|
61
|
+
cli.run(argv).then((code) => process.exit(code === undefined ? 0 : code));
|
|
62
|
+
} else if (argv.length && (argv[0] === 'help' || argv[0] === '--help' || argv[0] === '-h')) {
|
|
63
|
+
// Interpreter usage + package manager section appended.
|
|
64
|
+
if (fs.existsSync(bin)) {
|
|
65
|
+
const r = spawnSync(bin, ['--help'], { stdio: ['inherit', 'pipe', 'inherit'] });
|
|
66
|
+
if (r.status === 0 && r.stdout) process.stdout.write(r.stdout);
|
|
67
|
+
}
|
|
68
|
+
const cli = require('../lib/cli.js');
|
|
69
|
+
console.log('\npackage manager (handled by this launcher):');
|
|
70
|
+
cli.usage();
|
|
71
|
+
process.exit(0);
|
|
72
|
+
} else {
|
|
73
|
+
/* ---------------- interpreter passthrough ------------------------- */
|
|
47
74
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
75
|
+
if (!fs.existsSync(bin)) {
|
|
76
|
+
console.error('[mozhi] No bundled interpreter for ' + process.platform + '-' + process.arch + '.');
|
|
77
|
+
console.error('[mozhi] This package ships official v3.3.0 binaries for:');
|
|
78
|
+
console.error('[mozhi] linux-x86_64, linux-arm64 (fully static — works on glibc, Alpine, Termux)');
|
|
79
|
+
console.error('[mozhi] For macOS, Windows and other platforms use the installer:');
|
|
80
|
+
console.error('[mozhi] curl -fsSL https://github.com/crossberry-in/mozhi-doc/raw/main/install.sh | bash');
|
|
81
|
+
process.exit(1);
|
|
54
82
|
}
|
|
55
|
-
|
|
83
|
+
|
|
84
|
+
const child = spawn(bin, argv, { stdio: 'inherit' });
|
|
85
|
+
|
|
86
|
+
child.on('error', (err) => {
|
|
87
|
+
console.error('[mozhi] Failed to start interpreter: ' + err.message);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
child.on('exit', (code, signal) => {
|
|
92
|
+
if (signal) {
|
|
93
|
+
// propagate fatal signals (SIGINT/SIGTERM...) with the correct exit status
|
|
94
|
+
process.kill(process.pid, signal);
|
|
95
|
+
} else {
|
|
96
|
+
process.exit(code === null ? 0 : code);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}
|
package/checksums-sha256.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
0bcd18f3dd5dec453ad3ccd804fe92c8be81c3b97649f79f1e0db7d26f433ed7 mozhi-interpreter-alpine-arm64
|
|
2
|
+
fd0a2776c77bf0be97a985081c45642f019148a958bd1f5577762cc229bcf561 mozhi-interpreter-alpine-x86_64
|
|
3
|
+
8d7918d3ca952fcbdbc0c1e3631e180ac32b69bf71920bf37173acb0dae123c4 mozhi-interpreter-linux-arm64
|
|
4
|
+
5df211b4dfddd70e84ec7cb5a61131342395536a610c34a688a23a4d3bde9c75 mozhi-interpreter-linux-x86_64
|
|
5
|
+
ccb515d143081e78ac69aa5c11ef7773987b4da6f3d768c5c36ef8414159fa56 mozhi-interpreter-macos-arm64
|
|
6
|
+
ecb9f937ccfa1a3f45492ead38a85a776e13e4b4700541a2da4e9987b05c083e mozhi-interpreter-macos-x86_64
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*
|
|
3
|
+
* Mozhi package manager CLI — `mz install|uninstall|list|search|pkg`.
|
|
4
|
+
*
|
|
5
|
+
* Invoked from bin/mozhi.js before any interpreter passthrough, so these
|
|
6
|
+
* subcommands work for both the `mozhi` and `mz` commands.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
const pkgman = require('./pkgman');
|
|
12
|
+
const scaffold = require('./scaffold');
|
|
13
|
+
const reg = require('./registry');
|
|
14
|
+
|
|
15
|
+
function out(msg) { process.stdout.write(msg + '\n'); }
|
|
16
|
+
function err(msg) { process.stderr.write(msg + '\n'); }
|
|
17
|
+
|
|
18
|
+
function pad(s, n) {
|
|
19
|
+
s = String(s == null ? '' : s);
|
|
20
|
+
return s.length >= n ? s : s + ' '.repeat(n - s.length);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function usage() {
|
|
24
|
+
out('usage:');
|
|
25
|
+
out(' mz install <name> install a package from the Mozhi registry');
|
|
26
|
+
out(' mz install <name>@<tag> install a specific release tag');
|
|
27
|
+
out(' mz install @user/repo install from any Git repo release');
|
|
28
|
+
out(' mz install @user/repo@tag install a specific repo release tag');
|
|
29
|
+
out(' mz install ./pkg.mz.pkg.zip install from a local package zip');
|
|
30
|
+
out(' mz install ./pkgdir install from a local package directory');
|
|
31
|
+
out(' mz uninstall <name> remove an installed package');
|
|
32
|
+
out(' mz list list installed packages');
|
|
33
|
+
out(' mz search [query] search the package registry');
|
|
34
|
+
out(' mz pkg new <name> scaffold a new package (with release workflow)');
|
|
35
|
+
out(' mz pkg check [dir] verify a package directory structure');
|
|
36
|
+
out(' mz pkg zip [dir] build <name>.mz.pkg.zip from a package dir');
|
|
37
|
+
out('');
|
|
38
|
+
out('options:');
|
|
39
|
+
out(' --local install into ./.mozhi/pkg instead of ~/.mozhi/pkg');
|
|
40
|
+
out('');
|
|
41
|
+
out('package layout: package.toml + src/mod.mz; releases carry <name>.mz.pkg.zip');
|
|
42
|
+
out('registry: ' + reg.DEFAULT_REGISTRY_URL);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function shortInstallHelp() {
|
|
46
|
+
err("mz: 'install' needs a package argument");
|
|
47
|
+
err(' mz install <name> # from the Mozhi registry');
|
|
48
|
+
err(' mz install @user/repo # from any Git repo');
|
|
49
|
+
err(' mz install ./pkg.mz.pkg.zip # from a local zip');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function cmdInstall(args, opts) {
|
|
53
|
+
if (!args.length) { shortInstallHelp(); return 1; }
|
|
54
|
+
const spec = args[0];
|
|
55
|
+
out('installing ' + spec + ' ...');
|
|
56
|
+
const res = await pkgman.install(spec, opts);
|
|
57
|
+
const m = res.manifest || {};
|
|
58
|
+
out('');
|
|
59
|
+
out(' installed: ' + (m.name || spec) + (m.version ? ' v' + m.version : ''));
|
|
60
|
+
if (m.description) out(' ' + m.description);
|
|
61
|
+
out(' location: ' + res.dest);
|
|
62
|
+
if (res.release) out(' release: ' + res.release.tag_name + ' (' + res.asset.name + ')');
|
|
63
|
+
out('');
|
|
64
|
+
out(" import it: import " + (m.name || spec));
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function cmdUninstall(args, opts) {
|
|
69
|
+
if (!args.length) { err("mz: 'uninstall' needs a package name"); return 1; }
|
|
70
|
+
const removed = pkgman.uninstall(args[0], opts);
|
|
71
|
+
for (const r of removed) out('removed: ' + r);
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function trunc(s, n) {
|
|
76
|
+
s = String(s == null ? '' : s);
|
|
77
|
+
return s.length > n ? s.slice(0, n - 1) + '…' : s;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function cmdList() {
|
|
81
|
+
const rows = pkgman.listInstalled();
|
|
82
|
+
if (!rows.length) {
|
|
83
|
+
out('no packages installed — try: mz search or mz install <name>');
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
out(pad('NAME', 20) + pad('VERSION', 12) + pad('SOURCE', 34) + 'PATH');
|
|
87
|
+
for (const r of rows) {
|
|
88
|
+
out(pad(trunc(r.name, 19), 20) + pad(r.version || '-', 12) + pad(trunc(r.source, 33), 34) + r.path);
|
|
89
|
+
}
|
|
90
|
+
return 0;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function cmdSearch(args) {
|
|
94
|
+
const { registry, source } = await reg.fetchRegistry();
|
|
95
|
+
const q = args[0] || '';
|
|
96
|
+
const hits = reg.searchPackages(registry, q);
|
|
97
|
+
out('registry packages (' + (q ? 'matching "' + q + '"' : 'all') + ') — source: ' + source);
|
|
98
|
+
out('');
|
|
99
|
+
for (const p of hits) {
|
|
100
|
+
const dist = p.repo ? p.repo : 'mozhi-doc libs';
|
|
101
|
+
out(' ' + pad(p.name, 20) + pad('v' + (p.version || '?'), 10) + pad(dist, 34));
|
|
102
|
+
if (p.description) out(' ' + pad('', 20) + p.description);
|
|
103
|
+
}
|
|
104
|
+
if (!hits.length) out(' (no matches)');
|
|
105
|
+
return 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function cmdPkg(args) {
|
|
109
|
+
const sub = args[0] || 'help';
|
|
110
|
+
if (sub === 'new') {
|
|
111
|
+
const name = args[1];
|
|
112
|
+
if (!name) { err("mz: 'pkg new' needs a package name — usage: mz pkg new <name> [dir]"); return 1; }
|
|
113
|
+
const res = scaffold.scaffold(name, args[2]);
|
|
114
|
+
out('created package ' + name + ' in ' + res.root + ' (' + res.files + ' files)');
|
|
115
|
+
out('');
|
|
116
|
+
out(' next steps:');
|
|
117
|
+
out(' cd ' + name);
|
|
118
|
+
out(' mozhi test # run the sample test');
|
|
119
|
+
out(' mz pkg check # verify structure');
|
|
120
|
+
out(' git init && git add -A && git commit -m "initial import"');
|
|
121
|
+
out(' git remote add origin git@github.com:<you>/' + name + '.git && git push -u origin main');
|
|
122
|
+
out(' git tag v0.1.0 && git push origin v0.1.0 # auto-release workflow builds the zip');
|
|
123
|
+
out('');
|
|
124
|
+
out(' others install it with: mz install @<you>/' + name);
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
if (sub === 'check') {
|
|
128
|
+
const info = pkgman.checkDir(args[1] || '.');
|
|
129
|
+
out('package structure OK: ' + info.manifest.name +
|
|
130
|
+
(info.manifest.version ? ' v' + info.manifest.version : '') +
|
|
131
|
+
' — entry: ' + info.manifest.entry + ' (' + info.files + ' files)');
|
|
132
|
+
if (!info.manifest.version) {
|
|
133
|
+
err('mz: warning: package.toml has no version field');
|
|
134
|
+
}
|
|
135
|
+
return 0;
|
|
136
|
+
}
|
|
137
|
+
if (sub === 'zip') {
|
|
138
|
+
const res = pkgman.zipDir(args[1] || '.');
|
|
139
|
+
out('built ' + res.out + ' (' + res.name + (res.version ? ' v' + res.version : '') + ', ' + res.count + ' files)');
|
|
140
|
+
out('attach it to a GitHub release, or let the release workflow do it on tag push.');
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
usage();
|
|
144
|
+
return sub === 'help' || sub === '--help' ? 0 : 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/* Entry point. Returns process exit code. */
|
|
148
|
+
async function run(argv) {
|
|
149
|
+
const cmd = argv[0];
|
|
150
|
+
const args = argv.slice(1);
|
|
151
|
+
const opts = { local: args.includes('--local') };
|
|
152
|
+
const rest = args.filter((a) => a !== '--local');
|
|
153
|
+
|
|
154
|
+
try {
|
|
155
|
+
switch (cmd) {
|
|
156
|
+
case 'install':
|
|
157
|
+
case 'add': // legacy alias kept for older docs
|
|
158
|
+
return await cmdInstall(rest, opts);
|
|
159
|
+
case 'uninstall':
|
|
160
|
+
case 'remove':
|
|
161
|
+
return await cmdUninstall(rest, opts);
|
|
162
|
+
case 'list':
|
|
163
|
+
case 'ls':
|
|
164
|
+
return cmdList();
|
|
165
|
+
case 'search':
|
|
166
|
+
return await cmdSearch(rest);
|
|
167
|
+
case 'pkg':
|
|
168
|
+
case 'package':
|
|
169
|
+
return cmdPkg(rest);
|
|
170
|
+
case 'help':
|
|
171
|
+
case '--help':
|
|
172
|
+
case '-h':
|
|
173
|
+
usage();
|
|
174
|
+
return 0;
|
|
175
|
+
default:
|
|
176
|
+
usage();
|
|
177
|
+
return 1;
|
|
178
|
+
}
|
|
179
|
+
} catch (e) {
|
|
180
|
+
err('mz: ' + (e && e.message ? e.message : String(e)));
|
|
181
|
+
return 1;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
module.exports = { run, usage };
|
package/lib/gh.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/*
|
|
3
|
+
* Mozhi package manager — GitHub releases client.
|
|
4
|
+
*
|
|
5
|
+
* Packages are distributed as tagged releases carrying a single
|
|
6
|
+
* `<pkg_name>.mz.pkg.zip` asset (see docs/packages). Downloads only ever
|
|
7
|
+
* come from release tags — never from arbitrary branches — so what gets
|
|
8
|
+
* installed is exactly what the author released.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const DEFAULT_API = 'https://api.github.com';
|
|
12
|
+
|
|
13
|
+
function apiBase() {
|
|
14
|
+
return (process.env.MOZHI_GH_API || DEFAULT_API).replace(/\/+$/, '');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const UA = 'mozhi-pkg/3.3 (+https://github.com/crossberry-in/mozhi)';
|
|
18
|
+
|
|
19
|
+
function validRepo(repo) {
|
|
20
|
+
return /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(repo);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function ghFetch(path, opts = {}) {
|
|
24
|
+
const url = apiBase() + path;
|
|
25
|
+
let res;
|
|
26
|
+
try {
|
|
27
|
+
res = await fetch(url, {
|
|
28
|
+
signal: AbortSignal.timeout(opts.timeoutMs || 15000),
|
|
29
|
+
headers: {
|
|
30
|
+
'user-agent': UA,
|
|
31
|
+
accept: 'application/vnd.github+json',
|
|
32
|
+
...(process.env.GITHUB_TOKEN && !process.env.MOZHI_GH_API
|
|
33
|
+
? { authorization: 'Bearer ' + process.env.GITHUB_TOKEN }
|
|
34
|
+
: {}),
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
} catch (e) {
|
|
38
|
+
throw new Error('GitHub API unreachable (' + url + '): ' + e.message);
|
|
39
|
+
}
|
|
40
|
+
if (res.status === 404) {
|
|
41
|
+
const err = new Error('not found: ' + url);
|
|
42
|
+
err.status = 404;
|
|
43
|
+
throw err;
|
|
44
|
+
}
|
|
45
|
+
if (res.status === 403 || res.status === 429) {
|
|
46
|
+
const remaining = res.headers.get('x-ratelimit-remaining');
|
|
47
|
+
throw new Error(
|
|
48
|
+
'GitHub API rate limit' + (remaining === '0' ? ' exceeded' : ' (HTTP ' + res.status + ')') +
|
|
49
|
+
' — try again later or set GITHUB_TOKEN'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (!res.ok) {
|
|
53
|
+
const err = new Error('GitHub API HTTP ' + res.status + ' for ' + url);
|
|
54
|
+
err.status = res.status;
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
return res.json();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/* Latest published release of a repo (null if the repo has no releases). */
|
|
61
|
+
async function latestRelease(repo) {
|
|
62
|
+
if (!validRepo(repo)) throw new Error('invalid repository: ' + repo + ' (expected owner/name)');
|
|
63
|
+
try {
|
|
64
|
+
return await ghFetch('/repos/' + repo + '/releases/latest');
|
|
65
|
+
} catch (e) {
|
|
66
|
+
if (e.status === 404) return null;
|
|
67
|
+
throw e;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* Release for an exact tag (null when the tag has no release). */
|
|
72
|
+
async function releaseByTag(repo, tag) {
|
|
73
|
+
if (!validRepo(repo)) throw new Error('invalid repository: ' + repo + ' (expected owner/name)');
|
|
74
|
+
try {
|
|
75
|
+
return await ghFetch('/repos/' + repo + '/releases/tags/' + encodeURIComponent(tag));
|
|
76
|
+
} catch (e) {
|
|
77
|
+
if (e.status === 404) return null;
|
|
78
|
+
throw e;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/* Pick the package asset from a release: exact <name>.mz.pkg.zip first,
|
|
83
|
+
* otherwise a single *.mz.pkg.zip asset. Returns null when none match. */
|
|
84
|
+
function pickAsset(release, pkgName) {
|
|
85
|
+
const assets = (release && release.assets) || [];
|
|
86
|
+
if (pkgName) {
|
|
87
|
+
const exact = assets.find((a) => a.name === pkgName + '.mz.pkg.zip');
|
|
88
|
+
if (exact) return exact;
|
|
89
|
+
}
|
|
90
|
+
const zips = assets.filter((a) => /\.mz\.pkg\.zip$/.test(a.name));
|
|
91
|
+
if (zips.length === 1) return zips[0];
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* Download a release asset (follows redirects; 128MB cap). */
|
|
96
|
+
async function downloadAsset(asset) {
|
|
97
|
+
const res = await fetch(asset.browser_download_url, {
|
|
98
|
+
signal: AbortSignal.timeout(60000),
|
|
99
|
+
headers: { 'user-agent': UA },
|
|
100
|
+
});
|
|
101
|
+
if (!res.ok) throw new Error('asset download failed (HTTP ' + res.status + '): ' + asset.browser_download_url);
|
|
102
|
+
const len = Number(res.headers.get('content-length') || 0);
|
|
103
|
+
if (len > 128 * 1024 * 1024) throw new Error('asset too large: ' + asset.name);
|
|
104
|
+
const buf = Buffer.from(await res.arrayBuffer());
|
|
105
|
+
if (buf.length > 128 * 1024 * 1024) throw new Error('asset too large: ' + asset.name);
|
|
106
|
+
return buf;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
module.exports = { apiBase, latestRelease, releaseByTag, pickAsset, downloadAsset, validRepo };
|