pantry/scripts/fixup-checksums.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-09-08 23:40:35 +03:00
#!/usr/bin/env -S tea -E
/*---
args:
- deno
- run
- --allow-net
2022-09-13 19:46:10 +03:00
- --allow-env=AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_S3_BUCKET
2022-09-08 23:40:35 +03:00
- --import-map={{ srcroot }}/import-map.json
---*/
2022-09-20 14:53:40 +03:00
import { S3 } from "s3"
import { Sha256 } from "deno/hash/sha256.ts"
import { readerFromStreamReader, readAll } from "deno/streams/conversion.ts"
2022-09-08 23:40:35 +03:00
const s3 = new S3({
accessKeyID: Deno.env.get("AWS_ACCESS_KEY_ID")!,
secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY")!,
region: "us-east-1",
});
2022-09-13 16:18:47 +03:00
const bucket = s3.getBucket(Deno.env.get("AWS_S3_BUCKET")!);
2022-09-08 23:40:35 +03:00
for await (const pkg of bucket.listAllObjects({ batchSize: 200 })) {
if (!pkg.key?.endsWith('.tar.gz')) { continue }
console.log({ checking: pkg.key });
if (!await bucket.headObject(`${pkg.key}.sha256sum`)) {
console.log({ missingChecksum: pkg.key })
const reader = (await bucket.getObject(pkg.key))!.body.getReader()
const contents = await readAll(readerFromStreamReader(reader))
const basename = pkg.key.split("/").pop()
2022-09-20 14:53:40 +03:00
const sha256sum = new Sha256().update(contents).toString()
2022-09-08 23:40:35 +03:00
const body = new TextEncoder().encode(`${sha256sum} ${basename}`)
await bucket.putObject(`${pkg.key}.sha256sum`, body);
console.log({ uploaded: `${pkg.key}.sha256sum` });
}
}