Skip to content

Commit

Permalink
Uses our checksums to upload if our version is different
Browse files Browse the repository at this point in the history
  • Loading branch information
jhheider authored and mxcl committed Aug 5, 2022
1 parent 514853b commit 3f875bf
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions scripts/upload-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { S3 } from "s3";
import { crypto } from "deno/crypto/mod.ts";
import useCache from "hooks/useCache.ts";
import { encodeToString } from "encodeToString";
import { readAll, readerFromStreamReader } from "deno/streams/mod.ts";

const s3 = new S3({
accessKeyID: Deno.env.get("AWS_ACCESS_KEY_ID")!,
Expand All @@ -29,12 +30,15 @@ for (const pkg of await useCache().ls()) {

console.log({ checking: key });

if (!await bucket.headObject(key)) {
// path.read() returns a string; this is easier to get a UInt8Array
const contents = await Deno.readFile(bottle.string);
const inRepo = await bucket.headObject(key)
const repoChecksum = inRepo ? await checksum(`https://dist.tea.xyz/${key}.sha256sum`) : undefined

// path.read() returns a string; this is easier to get a UInt8Array
const contents = await Deno.readFile(bottle.string);
const sha256sum = encodeToString(new Uint8Array(await crypto.subtle.digest("SHA-256", contents)))

if (!inRepo || repoChecksum !== sha256sum) {
const basename = key.split("/").pop()
const sha256sum = encodeToString(new Uint8Array(await crypto.subtle.digest("SHA-256", contents)))
const body = new TextEncoder().encode(`${sha256sum} ${basename}`)

await bucket.putObject(key, contents);
Expand All @@ -43,3 +47,12 @@ for (const pkg of await useCache().ls()) {
console.log({ uploaded: key });
}
}

async function checksum(url: string) {
const rsp = await fetch(url)
if (!rsp.ok) throw new Error(`404-not-found: ${url}`)
const rdr = rsp.body?.getReader()
if (!rdr) throw new Error(`Couldn’t read: ${url}`)
const r = await readAll(readerFromStreamReader(rdr))
return new TextDecoder().decode(r).split(' ')[0]
}

0 comments on commit 3f875bf

Please sign in to comment.