workerapt-upload 0.1.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/LICENSE +21 -0
- package/README.md +12 -0
- package/bin/workerapt-upload.mjs +226 -0
- package/package.json +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Infrawrench LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# workerapt-upload
|
|
2
|
+
|
|
3
|
+
CLI for uploading `.deb` packages to a [workerapt](https://github.com/) APT
|
|
4
|
+
repository running on Cloudflare Workers + R2.
|
|
5
|
+
|
|
6
|
+
## Usage
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npx workerapt-upload --help
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
See `workerapt-upload --help` for the `pool` / `publish` subcommands and flags.
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createReadStream } from 'node:fs';
|
|
3
|
+
import { mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { createHash } from 'node:crypto';
|
|
5
|
+
import { basename, join } from 'node:path';
|
|
6
|
+
|
|
7
|
+
function usage() {
|
|
8
|
+
process.stderr.write(`Usage:
|
|
9
|
+
workerapt-upload pool [flags] <file>...
|
|
10
|
+
workerapt-upload publish [flags] <manifest>...
|
|
11
|
+
workerapt-upload [flags] <file>... (legacy: pool + publish together)
|
|
12
|
+
|
|
13
|
+
Splits the upload pipeline into two steps so parallel CI jobs can each push
|
|
14
|
+
a .deb to the pool independently, and a single reconcile job can publish the
|
|
15
|
+
collected manifests as one signed release.
|
|
16
|
+
|
|
17
|
+
pool Hashes each file, asks the Worker for a presigned R2 PUT URL,
|
|
18
|
+
uploads directly to R2, and emits a JSON manifest per file
|
|
19
|
+
containing { fileId, md5, sha256, name, size }.
|
|
20
|
+
|
|
21
|
+
publish Reads pool manifests and POSTs them as one batch to
|
|
22
|
+
/<repo>/dists/<dist>/<cat>, regenerating the signed indices.
|
|
23
|
+
|
|
24
|
+
Common flags:
|
|
25
|
+
--url Worker base URL (env: WORKERAPT_URL)
|
|
26
|
+
--key Bearer token matching Worker's KEY (env: WORKERAPT_KEY)
|
|
27
|
+
--repo Repository name
|
|
28
|
+
|
|
29
|
+
pool extra flags:
|
|
30
|
+
--out <dir> Write one <fileId>.json manifest per file into <dir>.
|
|
31
|
+
Default: print a JSON array of manifests to stdout.
|
|
32
|
+
|
|
33
|
+
publish extra flags:
|
|
34
|
+
--dist <dist> Distribution name (e.g. stable)
|
|
35
|
+
--cat <cat> Component name (e.g. main)
|
|
36
|
+
|
|
37
|
+
Positional manifest args may be:
|
|
38
|
+
- a path to a .json file (object or array of objects)
|
|
39
|
+
- a directory (all *.json inside are read, sorted)
|
|
40
|
+
- "-" to read a JSON array from stdin
|
|
41
|
+
|
|
42
|
+
Legacy (no subcommand): same as pool + publish; takes --dist, --cat, and files.
|
|
43
|
+
`);
|
|
44
|
+
process.exit(2);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function parseArgs(argv) {
|
|
48
|
+
const subcommands = new Set(['pool', 'publish']);
|
|
49
|
+
let cmd = 'legacy';
|
|
50
|
+
let rest = argv;
|
|
51
|
+
if (argv.length > 0 && subcommands.has(argv[0])) {
|
|
52
|
+
cmd = argv[0];
|
|
53
|
+
rest = argv.slice(1);
|
|
54
|
+
}
|
|
55
|
+
const out = { cmd, files: [] };
|
|
56
|
+
for (let i = 0; i < rest.length; i++) {
|
|
57
|
+
const a = rest[i];
|
|
58
|
+
if (a === '--url') out.url = rest[++i];
|
|
59
|
+
else if (a === '--key') out.key = rest[++i];
|
|
60
|
+
else if (a === '--repo') out.repo = rest[++i];
|
|
61
|
+
else if (a === '--dist') out.dist = rest[++i];
|
|
62
|
+
else if (a === '--cat') out.cat = rest[++i];
|
|
63
|
+
else if (a === '--out') out.outDir = rest[++i];
|
|
64
|
+
else if (a === '-h' || a === '--help') usage();
|
|
65
|
+
else if (a !== '-' && a.startsWith('-')) {
|
|
66
|
+
process.stderr.write(`Unknown flag: ${a}\n`);
|
|
67
|
+
usage();
|
|
68
|
+
} else out.files.push(a);
|
|
69
|
+
}
|
|
70
|
+
out.url ??= process.env.WORKERAPT_URL;
|
|
71
|
+
out.key ??= process.env.WORKERAPT_KEY;
|
|
72
|
+
if (!out.url || !out.key || !out.repo) usage();
|
|
73
|
+
if (out.cmd === 'pool' && out.files.length === 0) usage();
|
|
74
|
+
if (out.cmd === 'publish' && (!out.dist || !out.cat || out.files.length === 0)) usage();
|
|
75
|
+
if (out.cmd === 'legacy' && (!out.dist || !out.cat || out.files.length === 0)) usage();
|
|
76
|
+
out.url = out.url.replace(/\/$/, '');
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function hashFile(path) {
|
|
81
|
+
const md5 = createHash('md5');
|
|
82
|
+
const sha = createHash('sha256');
|
|
83
|
+
let size = 0;
|
|
84
|
+
for await (const chunk of createReadStream(path)) {
|
|
85
|
+
md5.update(chunk);
|
|
86
|
+
sha.update(chunk);
|
|
87
|
+
size += chunk.length;
|
|
88
|
+
}
|
|
89
|
+
return { md5: md5.digest('hex'), sha256: sha.digest('hex'), size };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function presign({ url, key, repo }) {
|
|
93
|
+
const r = await fetch(`${url}/${encodeURIComponent(repo)}/pool/presign`, {
|
|
94
|
+
method: 'POST',
|
|
95
|
+
headers: { Authorization: `Bearer ${key}` },
|
|
96
|
+
});
|
|
97
|
+
if (!r.ok) throw new Error(`presign failed ${r.status}: ${await r.text()}`);
|
|
98
|
+
return r.json();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function uploadToR2(uploadUrl, path, size) {
|
|
102
|
+
const r = await fetch(uploadUrl, {
|
|
103
|
+
method: 'PUT',
|
|
104
|
+
body: createReadStream(path),
|
|
105
|
+
duplex: 'half',
|
|
106
|
+
headers: { 'Content-Length': String(size) },
|
|
107
|
+
});
|
|
108
|
+
if (!r.ok) throw new Error(`R2 upload failed ${r.status}: ${await r.text()}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function publish({ url, key, repo, dist, cat }, debs) {
|
|
112
|
+
const r = await fetch(`${url}/${encodeURIComponent(repo)}/dists/${encodeURIComponent(dist)}/${encodeURIComponent(cat)}`, {
|
|
113
|
+
method: 'PUT',
|
|
114
|
+
headers: { Authorization: `Bearer ${key}`, 'Content-Type': 'application/json' },
|
|
115
|
+
body: JSON.stringify({ debs }),
|
|
116
|
+
});
|
|
117
|
+
if (!r.ok) throw new Error(`publish failed ${r.status}: ${await r.text()}`);
|
|
118
|
+
return r.json();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function poolOne(args, file) {
|
|
122
|
+
const name = basename(file);
|
|
123
|
+
process.stderr.write(`hashing ${name}... `);
|
|
124
|
+
const { md5, sha256, size } = await hashFile(file);
|
|
125
|
+
process.stderr.write(`${size}B md5=${md5}\n`);
|
|
126
|
+
|
|
127
|
+
process.stderr.write(`presigning ${name}... `);
|
|
128
|
+
const { fileId, uploadUrl } = await presign(args);
|
|
129
|
+
process.stderr.write(`${fileId}\n`);
|
|
130
|
+
|
|
131
|
+
process.stderr.write(`uploading ${name}... `);
|
|
132
|
+
await uploadToR2(uploadUrl, file, size);
|
|
133
|
+
process.stderr.write(`done\n`);
|
|
134
|
+
|
|
135
|
+
return { fileId, md5, sha256, name, size };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function readStdin() {
|
|
139
|
+
let data = '';
|
|
140
|
+
process.stdin.setEncoding('utf8');
|
|
141
|
+
for await (const chunk of process.stdin) data += chunk;
|
|
142
|
+
return data;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async function readManifests(paths) {
|
|
146
|
+
const manifests = [];
|
|
147
|
+
for (const p of paths) {
|
|
148
|
+
if (p === '-') {
|
|
149
|
+
const text = await readStdin();
|
|
150
|
+
const parsed = JSON.parse(text);
|
|
151
|
+
manifests.push(...(Array.isArray(parsed) ? parsed : [parsed]));
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const s = await stat(p);
|
|
155
|
+
if (s.isDirectory()) {
|
|
156
|
+
const entries = (await readdir(p)).filter((e) => e.endsWith('.json')).sort();
|
|
157
|
+
for (const e of entries) {
|
|
158
|
+
const text = await readFile(join(p, e), 'utf8');
|
|
159
|
+
const parsed = JSON.parse(text);
|
|
160
|
+
manifests.push(...(Array.isArray(parsed) ? parsed : [parsed]));
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
const text = await readFile(p, 'utf8');
|
|
164
|
+
const parsed = JSON.parse(text);
|
|
165
|
+
manifests.push(...(Array.isArray(parsed) ? parsed : [parsed]));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return manifests;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async function runPool(args) {
|
|
172
|
+
const manifests = [];
|
|
173
|
+
if (args.outDir) await mkdir(args.outDir, { recursive: true });
|
|
174
|
+
for (const file of args.files) {
|
|
175
|
+
const m = await poolOne(args, file);
|
|
176
|
+
manifests.push(m);
|
|
177
|
+
if (args.outDir) {
|
|
178
|
+
await writeFile(join(args.outDir, `${m.fileId}.json`), JSON.stringify(m, null, 2) + '\n');
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (!args.outDir) {
|
|
182
|
+
process.stdout.write(JSON.stringify(manifests, null, 2) + '\n');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function runPublish(args) {
|
|
187
|
+
const manifests = await readManifests(args.files);
|
|
188
|
+
if (manifests.length === 0) {
|
|
189
|
+
process.stderr.write('no manifests to publish\n');
|
|
190
|
+
process.exit(1);
|
|
191
|
+
}
|
|
192
|
+
for (const m of manifests) {
|
|
193
|
+
if (!m || typeof m.fileId !== 'string' || typeof m.md5 !== 'string' || typeof m.sha256 !== 'string') {
|
|
194
|
+
throw new Error(`invalid manifest entry: ${JSON.stringify(m)}`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const debs = manifests.map(({ fileId, md5, sha256 }) => ({ fileId, md5, sha256 }));
|
|
198
|
+
process.stderr.write(`publishing ${debs.length} package(s) to ${args.repo}/${args.dist}/${args.cat}... `);
|
|
199
|
+
const result = await publish(args, debs);
|
|
200
|
+
process.stderr.write(`done\n`);
|
|
201
|
+
process.stdout.write(JSON.stringify(result, null, 2) + '\n');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async function runLegacy(args) {
|
|
205
|
+
const debs = [];
|
|
206
|
+
for (const file of args.files) {
|
|
207
|
+
const m = await poolOne(args, file);
|
|
208
|
+
debs.push({ fileId: m.fileId, md5: m.md5, sha256: m.sha256 });
|
|
209
|
+
}
|
|
210
|
+
process.stderr.write(`publishing ${debs.length} package(s) to ${args.repo}/${args.dist}/${args.cat}... `);
|
|
211
|
+
const result = await publish(args, debs);
|
|
212
|
+
process.stderr.write(`done\n`);
|
|
213
|
+
process.stdout.write(JSON.stringify(result, null, 2) + '\n');
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async function main() {
|
|
217
|
+
const args = parseArgs(process.argv.slice(2));
|
|
218
|
+
if (args.cmd === 'pool') return runPool(args);
|
|
219
|
+
if (args.cmd === 'publish') return runPublish(args);
|
|
220
|
+
return runLegacy(args);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
main().catch((e) => {
|
|
224
|
+
process.stderr.write(`${e?.message ?? e}\n`);
|
|
225
|
+
process.exit(1);
|
|
226
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "workerapt-upload",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "CLI to upload .deb packages to a workerapt-backed APT repository on Cloudflare Workers + R2.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "bin/workerapt-upload.mjs",
|
|
8
|
+
"bin": {
|
|
9
|
+
"workerapt-upload": "bin/workerapt-upload.mjs"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"bin/workerapt-upload.mjs",
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">=18"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["apt", "debian", "cloudflare", "workers", "r2", "cli"]
|
|
20
|
+
}
|