rvlib-shipkit 2.9.0 → 2.11.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/CHANGELOG.md +23 -0
- package/package.json +1 -1
- package/src/cli/cmd_Media.ts +164 -0
- package/src/cli/cmd_Review.ts +2 -1
- package/src/cli/family-pages.ts +1 -0
- package/src/cli/registry.ts +2 -0
- package/src/collections/pings.ts +6 -4
- package/src/core/app-icon.ts +17 -4
- package/src/core/scratch-objects.ts +56 -0
- package/src/core/trace-audit.ts +13 -1
- package/src/home/home-paths.ts +4 -0
- package/src/home/home-routes.ts +36 -2
- package/src/home/hubs.ts +5 -0
- package/src/home/inbox-shape.ts +182 -0
- package/src/home/inbox.ts +48 -0
- package/src/home/run-daemon.ts +12 -2
- package/src/home/serve-web.ts +155 -16
- package/src/ledger/git-scan.ts +73 -25
- package/src/ledger/repo-view.ts +2 -22
- package/src/media-kinds.ts +7 -0
- package/src/media-store/content.ts +51 -0
- package/src/media-store/daemon.ts +91 -0
- package/src/media-store/db.ts +74 -0
- package/src/media-store/fact.ts +9 -0
- package/src/media-store/kind.ts +44 -0
- package/src/media-store/purge.ts +139 -0
- package/src/media-store/remote.ts +93 -0
- package/src/media-store/space.ts +124 -0
- package/src/media-store/sync.ts +101 -0
- package/src/media-store/upload.ts +98 -0
- package/src/repo-config.ts +4 -0
- package/src/review/anim.ts +6 -5
- package/src/review/cells.ts +552 -0
- package/src/review/format.ts +1 -1
- package/src/review/media-kind.ts +84 -0
- package/src/review/media-read.ts +48 -0
- package/src/review/model.ts +43 -5
- package/src/review/previews.ts +153 -0
- package/src/review/routes.ts +105 -47
- package/src/review/store.ts +106 -45
- package/src/review/trailer-desk.ts +79 -46
- package/src/review/trailer-media.ts +163 -0
- package/src/review/trailer-routes.ts +5 -2
- package/src/serve/hub-app/home-st.ts +39 -9
- package/src/serve/hub-app/home.html +2 -1
- package/src/serve/hub-app/hub-app.tsx +7 -23
- package/src/serve/hub-app/icons.tsx +2 -0
- package/src/serve/hub-app/inbox-st.ts +144 -0
- package/src/serve/hub-app/inbox-view.tsx +205 -0
- package/src/serve/hub-app/inbox.css +73 -0
- package/src/serve/hub-app/item-view.tsx +19 -0
- package/src/serve/hub-app/keys.ts +4 -3
- package/src/serve/hub-app/palette-st.ts +2 -1
- package/src/serve/hub-app/project-list.tsx +1 -0
- package/src/serve/hub-app/projects-svc.ts +1 -1
- package/src/serve/hub-app/repo-st.ts +1 -1
- package/src/serve/hub-app/review/kind-view.tsx +21 -22
- package/src/serve/hub-app/review/kit.tsx +11 -12
- package/src/serve/hub-app/review/review-board.tsx +15 -13
- package/src/serve/hub-app/review/review-cells.tsx +241 -0
- package/src/serve/hub-app/review/review-kit.ts +79 -0
- package/src/serve/hub-app/review/review-list.tsx +87 -122
- package/src/serve/hub-app/review/review-page.tsx +22 -79
- package/src/serve/hub-app/review/review-st.ts +269 -698
- package/src/serve/hub-app/review/review-thread.tsx +302 -0
- package/src/serve/hub-app/review/review-views.tsx +322 -253
- package/src/serve/hub-app/review/thread-st.ts +770 -0
- package/src/serve/hub-app/review.css +235 -91
- package/src/serve/hub-runtime.ts +9 -4
- package/src/serve/serve-hub.ts +4 -1
- package/src/serve/warm-cell.ts +6 -1
- package/src/trailer/footage.ts +6 -2
- package/src/trailer/passage.ts +4 -2
- package/src/trailer/prune.ts +6 -10
- package/src/serve/hub-app/review/review-answer.tsx +0 -249
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// the disk lets go of media the Space holds (wiki build/shipkit/media-store.md, The purge). A file goes only when the
|
|
2
|
+
// index says up AND a HEAD answers its size AND its kind's `beforePurge` passed. Kept: every row its kind pins, every
|
|
3
|
+
// row used in the last `older` days, every row nothing names. Oldest first. The candidates and the delete read one path
|
|
4
|
+
// form, `join(store, item, name)`, from the same index rows
|
|
5
|
+
import { existsSync, rmSync, statSync, statfsSync } from 'node:fs'
|
|
6
|
+
import { join, resolve } from 'node:path'
|
|
7
|
+
import { mediaDb, tryWrite } from './db.ts'
|
|
8
|
+
import type { MediaKind } from './kind.ts'
|
|
9
|
+
import type { Space } from './space.ts'
|
|
10
|
+
import { syncKind } from './sync.ts'
|
|
11
|
+
import { mb } from './upload.ts'
|
|
12
|
+
|
|
13
|
+
export const KEEP_DAYS = 14
|
|
14
|
+
/** under this much free disk the daemon purges, when the owner turned it on */
|
|
15
|
+
export const FREE_MIN_BYTES = 40e9
|
|
16
|
+
|
|
17
|
+
export interface Candidate {
|
|
18
|
+
item: string
|
|
19
|
+
name: string
|
|
20
|
+
key: string
|
|
21
|
+
size: number
|
|
22
|
+
/** the row's last use */
|
|
23
|
+
used: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface Kept {
|
|
27
|
+
n: number
|
|
28
|
+
bytes: number
|
|
29
|
+
/** of them, the rows the kind pins */
|
|
30
|
+
pinned: { n: number; bytes: number }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** what a purge of the store may free, oldest first: on the disk, up in the index, neither pinned nor recent */
|
|
34
|
+
export async function purgeCandidates(kind: MediaKind, dir: string, o: { olderDays?: number; now?: number } = {}): Promise<{ candidates: Candidate[]; kept: Kept }> {
|
|
35
|
+
await syncKind(kind, dir, { fill: 0 })
|
|
36
|
+
const now = o.now ?? Date.now()
|
|
37
|
+
const since = now - (o.olderDays ?? KEEP_DAYS) * 86_400_000
|
|
38
|
+
const judge = await kind.holds(resolve(dir), now)
|
|
39
|
+
const db = mediaDb()
|
|
40
|
+
if (!db) throw new Error('the media index does not open: nothing is purged without it')
|
|
41
|
+
const prefix = `${kind.kind}/`
|
|
42
|
+
const rows = db
|
|
43
|
+
.query('select f.item as item, f.name as name, f.key as key, f.size as size from files f join objects o on o.key = f.key where f.store = ? and substr(f.key, 1, ?) = ? and f.local = 1 and o.up is not null')
|
|
44
|
+
.all(resolve(dir), prefix.length, prefix) as { item: string; name: string; key: string; size: number }[]
|
|
45
|
+
const candidates: Candidate[] = []
|
|
46
|
+
const kept: Kept = { n: 0, bytes: 0, pinned: { n: 0, bytes: 0 } }
|
|
47
|
+
for (const r of rows) {
|
|
48
|
+
const h = judge(r)
|
|
49
|
+
if (h.pinned || h.used === undefined || !(h.used < since)) {
|
|
50
|
+
kept.n++
|
|
51
|
+
kept.bytes += r.size
|
|
52
|
+
if (h.pinned) {
|
|
53
|
+
kept.pinned.n++
|
|
54
|
+
kept.pinned.bytes += r.size
|
|
55
|
+
}
|
|
56
|
+
continue
|
|
57
|
+
}
|
|
58
|
+
candidates.push({ ...r, used: h.used })
|
|
59
|
+
}
|
|
60
|
+
candidates.sort((a, b) => a.used - b.used || (a.item < b.item ? -1 : a.item > b.item ? 1 : a.name < b.name ? -1 : 1))
|
|
61
|
+
return { candidates, kept }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface PurgeResult {
|
|
65
|
+
freed: number
|
|
66
|
+
files: number
|
|
67
|
+
/** a file the HEAD or its kind did not confirm: left on the disk */
|
|
68
|
+
refused: { key: string; why: string }[]
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** free the disk of the candidates, oldest first, until `enough` says stop, `jobs` files at a time: each file waits
|
|
72
|
+
* on its HEAD and its kind's check, so one at a time spends the purge on round trips. A file some process holds open
|
|
73
|
+
* stays */
|
|
74
|
+
export async function purge(kind: MediaKind, dir: string, space: Space, list: readonly Candidate[], o: { enough?: () => boolean; say?: (l: string) => void; jobs?: number } = {}): Promise<PurgeResult> {
|
|
75
|
+
const out: PurgeResult = { freed: 0, files: 0, refused: [] }
|
|
76
|
+
const store = resolve(dir)
|
|
77
|
+
const open = openOf(list.map((c) => join(store, c.item, c.name)))
|
|
78
|
+
let said = Date.now()
|
|
79
|
+
let next = 0
|
|
80
|
+
const one = async (c: Candidate): Promise<void> => {
|
|
81
|
+
const path = join(store, c.item, c.name)
|
|
82
|
+
if (statSync(path, { throwIfNoEntry: false })?.size !== c.size) return
|
|
83
|
+
if (open.has(path)) {
|
|
84
|
+
out.refused.push({ key: c.key, why: `${path} is open in a process` })
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const h = await space.head(c.key)
|
|
89
|
+
if (h?.size !== c.size) {
|
|
90
|
+
out.refused.push({ key: c.key, why: h ? `the Space holds ${h.size} bytes, the disk ${c.size}` : 'not in the Space' })
|
|
91
|
+
tryWrite(`${c.key} not up`, (db) => void db.query('update objects set up = null where key = ?').run(c.key))
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
await kind.beforePurge?.(store, space, c)
|
|
95
|
+
} catch (e) {
|
|
96
|
+
out.refused.push({ key: c.key, why: (e as Error).message })
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
rmSync(path, { force: true })
|
|
100
|
+
tryWrite(`${c.item}/${c.name} purged`, (db) => void db.query('update files set local = 0, purged = ? where store = ? and item = ? and name = ?').run(Date.now(), store, c.item, c.name))
|
|
101
|
+
out.freed += c.size
|
|
102
|
+
out.files++
|
|
103
|
+
if (o.say && Date.now() - said > 10_000) {
|
|
104
|
+
said = Date.now()
|
|
105
|
+
o.say(` ${out.files} files, ${mb(out.freed)} freed`)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
const work = async (): Promise<void> => {
|
|
109
|
+
for (;;) {
|
|
110
|
+
if (o.enough?.()) return
|
|
111
|
+
const c = list[next++]
|
|
112
|
+
if (!c) return
|
|
113
|
+
await one(c)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
await Promise.all(Array.from({ length: Math.max(1, o.jobs ?? PURGE_JOBS) }, work))
|
|
117
|
+
return out
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** files purged at once: enough to hide the round trips, few enough that the kinds' checks (an image's previews, a
|
|
121
|
+
* render's sha) leave the machine its cores */
|
|
122
|
+
export const PURGE_JOBS = 6
|
|
123
|
+
|
|
124
|
+
/** the paths of `paths` some process holds open (lsof), so a purge never pulls a file from under a reader */
|
|
125
|
+
export function openOf(paths: readonly string[]): Set<string> {
|
|
126
|
+
const out = new Set<string>()
|
|
127
|
+
const here = paths.filter((p) => existsSync(p))
|
|
128
|
+
for (let i = 0; i < here.length; i += 400) {
|
|
129
|
+
const r = Bun.spawnSync(['lsof', '-Fn', '--', ...here.slice(i, i + 400)], { stdout: 'pipe', stderr: 'ignore' })
|
|
130
|
+
for (const l of r.stdout.toString().split('\n')) if (l.startsWith('n/')) out.add(l.slice(1))
|
|
131
|
+
}
|
|
132
|
+
return out
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** the disk's free bytes where the store lives */
|
|
136
|
+
export function freeBytes(path: string): number {
|
|
137
|
+
const s = statfsSync(existsSync(path) ? path : '/')
|
|
138
|
+
return Number(s.bavail) * Number(s.bsize)
|
|
139
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// a media file from the disk or the Space (wiki build/shipkit/media-store.md, Read): a URL signed for a file the disk
|
|
2
|
+
// let go, and the file fetched back into its place, checked by its sha, for a tool that needs its bytes
|
|
3
|
+
import { existsSync, renameSync, rmSync, statSync } from 'node:fs'
|
|
4
|
+
import { resolve } from 'node:path'
|
|
5
|
+
import { shaOf } from './content.ts'
|
|
6
|
+
import { mediaDb, tryWrite } from './db.ts'
|
|
7
|
+
import type { MediaFact } from './fact.ts'
|
|
8
|
+
import { machineSpace } from './space.ts'
|
|
9
|
+
|
|
10
|
+
/** true when the index saw the key up */
|
|
11
|
+
export function isUp(key: string): boolean {
|
|
12
|
+
const r = mediaDb()?.query('select up from objects where key = ?').get(key) as { up: number | null } | null | undefined
|
|
13
|
+
return r?.up != null
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** the keys of `keys` the index saw up */
|
|
17
|
+
export function upKeys(keys: readonly string[]): Set<string> {
|
|
18
|
+
const db = mediaDb()
|
|
19
|
+
const out = new Set<string>()
|
|
20
|
+
if (!db || keys.length === 0) return out
|
|
21
|
+
const q = db.query('select up from objects where key = ?')
|
|
22
|
+
for (const k of keys) if ((q.get(k) as { up: number | null } | null)?.up != null) out.add(k)
|
|
23
|
+
return out
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** the key marked up, its row made when the index had none (a derived file, an index built again) */
|
|
27
|
+
export const recordUp = (key: string, size: number): void =>
|
|
28
|
+
tryWrite(`${key} up`, (db) => void db.query('insert into objects (key, size, up) values (?, ?, ?) on conflict (key) do update set up = excluded.up, error = null, tries = 0, next = 0').run(key, size, Date.now()))
|
|
29
|
+
|
|
30
|
+
/** a key a HEAD found absent, asked again only after this long: a file deleted before it went up is asked once */
|
|
31
|
+
const ABSENT_MS = 10 * 60_000
|
|
32
|
+
const absent = new Map<string, number>()
|
|
33
|
+
|
|
34
|
+
/** the key is up: the index says so, else one HEAD answers its size (an index built again after a purge) */
|
|
35
|
+
export async function confirmedUp(key: string, size: number): Promise<boolean> {
|
|
36
|
+
if (isUp(key)) return true
|
|
37
|
+
const space = (await machineSpace()).space
|
|
38
|
+
if (!space || Date.now() - (absent.get(key) ?? 0) < ABSENT_MS) return false
|
|
39
|
+
try {
|
|
40
|
+
if ((await space.head(key))?.size !== size) {
|
|
41
|
+
absent.set(key, Date.now())
|
|
42
|
+
return false
|
|
43
|
+
}
|
|
44
|
+
} catch (e) {
|
|
45
|
+
console.error(`WARN media store: HEAD ${key}: ${(e as Error).message}`)
|
|
46
|
+
absent.set(key, Date.now())
|
|
47
|
+
return false
|
|
48
|
+
}
|
|
49
|
+
recordUp(key, size)
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** a signed URL for the key when the Space holds it at `size`; null otherwise */
|
|
54
|
+
export async function signedUrl(key: string, size: number): Promise<string | null> {
|
|
55
|
+
const space = (await machineSpace()).space
|
|
56
|
+
if (!space) return null
|
|
57
|
+
return (await confirmedUp(key, size)) ? space.signed(key) : null
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** a signed URL for a derived object (a preview) the index saw up; never a HEAD */
|
|
61
|
+
export async function signedIfUp(key: string): Promise<string | null> {
|
|
62
|
+
const space = (await machineSpace()).space
|
|
63
|
+
return space && isUp(key) ? space.signed(key) : null
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const fetching = new Map<string, Promise<string | null>>()
|
|
67
|
+
|
|
68
|
+
/** the file at `path`, fetched back from the Space first when the disk let it go: checked by its sha before it lands,
|
|
69
|
+
* its index row marked local again (`row`: the store, item and name it is indexed under). null when neither holds it */
|
|
70
|
+
export function fetchBack(path: string, key: string, f: MediaFact, row?: { store: string; item: string; name: string }): Promise<string | null> {
|
|
71
|
+
if (existsSync(path)) return Promise.resolve(path)
|
|
72
|
+
const held = fetching.get(path)
|
|
73
|
+
if (held) return held
|
|
74
|
+
const job = (async (): Promise<string | null> => {
|
|
75
|
+
const space = (await machineSpace()).space
|
|
76
|
+
if (!space || !(await confirmedUp(key, f.size))) return null
|
|
77
|
+
const tmp = `${path}.${process.pid}.back`
|
|
78
|
+
try {
|
|
79
|
+
await space.get(key, tmp)
|
|
80
|
+
const sha = await shaOf(tmp)
|
|
81
|
+
if (sha !== f.sha || statSync(tmp).size !== f.size) throw new Error(`${key}: the bytes fetched back hash to ${sha.slice(0, 12)}, the record says ${f.sha.slice(0, 12)}`)
|
|
82
|
+
renameSync(tmp, path)
|
|
83
|
+
} catch (e) {
|
|
84
|
+
rmSync(tmp, { force: true })
|
|
85
|
+
console.error(`FAIL: media store: ${path} not fetched back: ${(e as Error).message}`)
|
|
86
|
+
return null
|
|
87
|
+
}
|
|
88
|
+
if (row) tryWrite(`${row.item}/${row.name} back`, (db) => void db.query('update files set local = 1, purged = null where store = ? and item = ? and name = ?').run(resolve(row.store), row.item, row.name))
|
|
89
|
+
return path
|
|
90
|
+
})().finally(() => fetching.delete(path))
|
|
91
|
+
fetching.set(path, job)
|
|
92
|
+
return job
|
|
93
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// the private S3 bucket that keeps every media file of the machine's store (wiki build/shipkit/media-store.md), named by
|
|
2
|
+
// the house config's `serve.mediaSpace`. Every object is PRIVATE: never a public-read ACL, never a bucket policy. A page
|
|
3
|
+
// reads one through a URL signed for one hour, on the CDN edge. The key pair (`KEY:SECRET`) is read through the house's
|
|
4
|
+
// `credsReadCommand`, held in memory only: never written to a file, never printed
|
|
5
|
+
import { S3Client } from 'bun'
|
|
6
|
+
import { mkdirSync, renameSync, rmSync } from 'node:fs'
|
|
7
|
+
import { dirname } from 'node:path'
|
|
8
|
+
import { readSecret } from '../smoke/run-smoke.ts'
|
|
9
|
+
|
|
10
|
+
/** the house config's `serve.mediaSpace`: where the media store's files go up, and the NAME of the secret holding the key pair */
|
|
11
|
+
export interface MediaSpace {
|
|
12
|
+
/** the origin, the bucket in its host: `https://<bucket>.<region>.digitaloceanspaces.com` */
|
|
13
|
+
endpoint: string
|
|
14
|
+
bucket: string
|
|
15
|
+
region: string
|
|
16
|
+
/** the CDN edge a signed URL is handed out on */
|
|
17
|
+
cdn?: string
|
|
18
|
+
/** the secret whose value is `KEY:SECRET`, read through `credsReadCommand` */
|
|
19
|
+
secret: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface SpaceConfig {
|
|
23
|
+
/** the origin, with the bucket in its host (virtual hosted) or in its path (`path: true`, a fake server) */
|
|
24
|
+
endpoint: string
|
|
25
|
+
bucket: string
|
|
26
|
+
region: string
|
|
27
|
+
path?: boolean
|
|
28
|
+
/** the CDN edge a signed URL is handed out on: the URL is signed for the origin, the edge checks it there */
|
|
29
|
+
cdn?: string | null
|
|
30
|
+
creds: { accessKeyId: string; secretAccessKey: string }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** how long a signed URL stays good */
|
|
34
|
+
export const SIGNED_S = 3600
|
|
35
|
+
|
|
36
|
+
export interface Space {
|
|
37
|
+
/** where the objects live, for a line a person reads (never a credential) */
|
|
38
|
+
readonly where: string
|
|
39
|
+
put(key: string, file: string, type: string): Promise<void>
|
|
40
|
+
/** the object's size, null when it is not there; throws on any other answer */
|
|
41
|
+
head(key: string): Promise<{ size: number } | null>
|
|
42
|
+
/** a GET URL good for `seconds`, on the CDN edge when the Space has one: signed for the origin host, which the edge
|
|
43
|
+
* checks it against (a URL signed for the edge's own host answers SignatureDoesNotMatch) */
|
|
44
|
+
signed(key: string, seconds?: number): string
|
|
45
|
+
/** the object written at `to`, through a file beside it renamed: no reader sees half of it */
|
|
46
|
+
get(key: string, to: string): Promise<void>
|
|
47
|
+
delete(key: string): Promise<void>
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const missing = (e: unknown): boolean => {
|
|
51
|
+
const x = e as { code?: string; name?: string; message?: string }
|
|
52
|
+
return x.code === 'NoSuchKey' || x.code === 'NotFound' || /\b404\b|NoSuchKey|not found/i.test(`${x.name ?? ''} ${x.message ?? ''}`)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function spaceOf(c: SpaceConfig): Space {
|
|
56
|
+
const client = new S3Client({ ...c.creds, bucket: c.bucket, region: c.region, endpoint: c.endpoint, virtualHostedStyle: c.path !== true })
|
|
57
|
+
const origin = new URL(c.endpoint).host
|
|
58
|
+
const edge = c.cdn ? new URL(c.cdn).host : null
|
|
59
|
+
return {
|
|
60
|
+
where: `${c.cdn ?? c.endpoint} (${c.bucket})`,
|
|
61
|
+
async put(key, file, type) {
|
|
62
|
+
await client.write(key, Bun.file(file), { type, acl: 'private' })
|
|
63
|
+
},
|
|
64
|
+
async head(key) {
|
|
65
|
+
try {
|
|
66
|
+
const s = await client.stat(key)
|
|
67
|
+
return { size: s.size }
|
|
68
|
+
} catch (e) {
|
|
69
|
+
if (missing(e)) return null
|
|
70
|
+
throw e
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
signed(key, seconds = SIGNED_S) {
|
|
74
|
+
const url = new URL(client.presign(key, { expiresIn: seconds, method: 'GET' }))
|
|
75
|
+
if (edge && url.host === origin) url.host = edge
|
|
76
|
+
return url.toString()
|
|
77
|
+
},
|
|
78
|
+
async get(key, to) {
|
|
79
|
+
mkdirSync(dirname(to), { recursive: true })
|
|
80
|
+
const tmp = `${to}.${process.pid}.get`
|
|
81
|
+
try {
|
|
82
|
+
await Bun.write(tmp, client.file(key))
|
|
83
|
+
renameSync(tmp, to)
|
|
84
|
+
} finally {
|
|
85
|
+
rmSync(tmp, { force: true })
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
async delete(key) {
|
|
89
|
+
await client.delete(key)
|
|
90
|
+
},
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
let held: Promise<{ space: Space | null; why: string }> | null = null
|
|
95
|
+
|
|
96
|
+
/** the machine's Space, from the house config and its key, read once per process; null with the reason when there is none */
|
|
97
|
+
export function machineSpace(): Promise<{ space: Space | null; why: string }> {
|
|
98
|
+
held ??= (async () => {
|
|
99
|
+
if (process.env.SHIPKIT_SPACES === 'off') return { space: null, why: 'SHIPKIT_SPACES=off' }
|
|
100
|
+
const { houseRootOf } = await import('../home/home-start.ts')
|
|
101
|
+
const { loadRepoConfig } = await import('../repo-config.ts')
|
|
102
|
+
const house = houseRootOf(process.cwd())
|
|
103
|
+
if (!house) return { space: null, why: `no house repo seen from ${process.cwd()}` }
|
|
104
|
+
const c = await loadRepoConfig(house)
|
|
105
|
+
const r = c.serve?.mediaSpace
|
|
106
|
+
if (!r) return { space: null, why: `the house config (${house}) names no serve.mediaSpace` }
|
|
107
|
+
if (!c.credsReadCommand) return { space: null, why: `the house config (${house}) declares no credsReadCommand to read ${r.secret}` }
|
|
108
|
+
let v = ''
|
|
109
|
+
try {
|
|
110
|
+
v = readSecret(c.credsReadCommand, r.secret)
|
|
111
|
+
} catch (e) {
|
|
112
|
+
return { space: null, why: (e as Error).message }
|
|
113
|
+
}
|
|
114
|
+
const at = v.indexOf(':')
|
|
115
|
+
if (at <= 0) return { space: null, why: `the secret ${r.secret} is no KEY:SECRET pair` }
|
|
116
|
+
return { space: spaceOf({ ...r, cdn: r.cdn ?? null, creds: { accessKeyId: v.slice(0, at), secretAccessKey: v.slice(at + 1) } }), why: '' }
|
|
117
|
+
})().catch((e: unknown) => ({ space: null, why: `the house config does not load: ${(e as Error).message}` }))
|
|
118
|
+
return held
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** a test's own Space in place of the machine's; undefined gives the machine's back */
|
|
122
|
+
export function useSpace(space: Space | null | undefined): void {
|
|
123
|
+
held = space === undefined ? null : Promise.resolve({ space, why: space ? '' : 'no Space in this test' })
|
|
124
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// a kind's scan into the machine index (db.ts): each file by its key, whether the disk holds it; each key an object row
|
|
2
|
+
// the uploader takes. The rows of one store are written as a whole: a row the scan no longer names goes
|
|
3
|
+
import { resolve } from 'node:path'
|
|
4
|
+
import { mediaKey } from './content.ts'
|
|
5
|
+
import { mediaDb, tryWrite } from './db.ts'
|
|
6
|
+
import type { MediaKind } from './kind.ts'
|
|
7
|
+
|
|
8
|
+
export interface SyncResult {
|
|
9
|
+
files: number
|
|
10
|
+
filled: number
|
|
11
|
+
left: number
|
|
12
|
+
/** files with no fact and no copy on the disk: lost before they went up */
|
|
13
|
+
unknown: number
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** the rows of `kind` in the index are only the keys of its prefix: two kinds never drop each other's rows */
|
|
17
|
+
const prefixOf = (kind: string): string => `${kind}/`
|
|
18
|
+
|
|
19
|
+
/** the store's files into the index. `fill` caps how many files are hashed this pass (Infinity: all) */
|
|
20
|
+
export async function syncKind(kind: MediaKind, dir: string, o: { fill?: number } = {}): Promise<SyncResult> {
|
|
21
|
+
const store = resolve(dir)
|
|
22
|
+
const scan = await kind.scan(store, { fill: o.fill ?? Infinity })
|
|
23
|
+
const out: SyncResult = { files: 0, filled: scan.filled, left: scan.left, unknown: 0 }
|
|
24
|
+
const rows: { item: string; name: string; key: string; size: number; local: number }[] = []
|
|
25
|
+
for (const r of scan.rows) {
|
|
26
|
+
if (!r.fact) {
|
|
27
|
+
if (!r.local) out.unknown++
|
|
28
|
+
continue
|
|
29
|
+
}
|
|
30
|
+
rows.push({ item: r.item, name: r.name, key: mediaKey(kind.kind, r.fact, r.name), size: r.fact.size, local: r.local ? 1 : 0 })
|
|
31
|
+
}
|
|
32
|
+
out.files = rows.length
|
|
33
|
+
const prefix = prefixOf(kind.kind)
|
|
34
|
+
tryWrite(`the ${kind.kind} media of ${store}`, (db) => {
|
|
35
|
+
const was = new Map(
|
|
36
|
+
(db.query('select item, name, key, size, local, purged from files where store = ? and substr(key, 1, ?) = ?').all(store, prefix.length, prefix) as { item: string; name: string; key: string; size: number; local: number; purged: number | null }[]).map(
|
|
37
|
+
(r) => [`${r.item}/${r.name}`, r],
|
|
38
|
+
),
|
|
39
|
+
)
|
|
40
|
+
const put = db.query('insert or replace into files (store, item, name, key, size, local, purged) values (?, ?, ?, ?, ?, ?, ?)')
|
|
41
|
+
const obj = db.query('insert or ignore into objects (key, size) values (?, ?)')
|
|
42
|
+
const seen = new Set<string>()
|
|
43
|
+
for (const r of rows) {
|
|
44
|
+
const k = `${r.item}/${r.name}`
|
|
45
|
+
seen.add(k)
|
|
46
|
+
const held = was.get(k)
|
|
47
|
+
if (held && held.key === r.key && held.size === r.size && held.local === r.local) continue
|
|
48
|
+
put.run(store, r.item, r.name, r.key, r.size, r.local, r.local === 1 ? null : (held?.purged ?? null))
|
|
49
|
+
obj.run(r.key, r.size)
|
|
50
|
+
}
|
|
51
|
+
const drop = db.query('delete from files where store = ? and item = ? and name = ?')
|
|
52
|
+
for (const [k, r] of was) if (!seen.has(k)) drop.run(store, r.item, r.name)
|
|
53
|
+
})
|
|
54
|
+
return out
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type Count = { n: number; bytes: number }
|
|
58
|
+
|
|
59
|
+
export interface MediaCounts {
|
|
60
|
+
store: string
|
|
61
|
+
kind: string
|
|
62
|
+
local: Count
|
|
63
|
+
up: Count
|
|
64
|
+
purged: Count
|
|
65
|
+
waiting: Count
|
|
66
|
+
gone: Count
|
|
67
|
+
failing: { n: number; error: string | null }
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** per store and kind the index knows: what the disk holds, what is up, purged, waiting, gone; the uploads that failed */
|
|
71
|
+
export function mediaCounts(): MediaCounts[] {
|
|
72
|
+
const db = mediaDb()
|
|
73
|
+
if (!db) return []
|
|
74
|
+
const rows = db
|
|
75
|
+
.query(
|
|
76
|
+
`select f.store as store, substr(f.key, 1, instr(f.key, '/') - 1) as kind, f.local as local, f.size as size, o.up is not null as up, o.error as error
|
|
77
|
+
from files f left join objects o on o.key = f.key`,
|
|
78
|
+
)
|
|
79
|
+
.all() as { store: string; kind: string; local: number; size: number; up: number; error: string | null }[]
|
|
80
|
+
const by = new Map<string, MediaCounts>()
|
|
81
|
+
const zero = (): Count => ({ n: 0, bytes: 0 })
|
|
82
|
+
for (const r of rows) {
|
|
83
|
+
const id = `${r.store}\0${r.kind}`
|
|
84
|
+
let c = by.get(id)
|
|
85
|
+
if (!c) by.set(id, (c = { store: r.store, kind: r.kind, local: zero(), up: zero(), purged: zero(), waiting: zero(), gone: zero(), failing: { n: 0, error: null } }))
|
|
86
|
+
const add = (b: Count): void => {
|
|
87
|
+
b.n++
|
|
88
|
+
b.bytes += r.size
|
|
89
|
+
}
|
|
90
|
+
if (r.local) add(c.local)
|
|
91
|
+
if (r.up) add(c.up)
|
|
92
|
+
if (r.up && !r.local) add(c.purged)
|
|
93
|
+
if (r.local && !r.up) add(c.waiting)
|
|
94
|
+
if (!r.local && !r.up) add(c.gone)
|
|
95
|
+
if (r.local && !r.up && r.error) {
|
|
96
|
+
c.failing.n++
|
|
97
|
+
c.failing.error ??= r.error
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return [...by.values()].sort((a, b) => (a.store < b.store ? -1 : a.store > b.store ? 1 : a.kind < b.kind ? -1 : 1))
|
|
101
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// every object the index holds on the disk and not up, sent to the Space: a HEAD first (a file already there is only
|
|
2
|
+
// marked), the PUT, then a HEAD of the same size before it counts as up. A failure waits 1 min, doubling up to 6 h
|
|
3
|
+
import { statSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { typeOf } from './content.ts'
|
|
6
|
+
import { mediaDb, tryWrite } from './db.ts'
|
|
7
|
+
import { recordUp } from './remote.ts'
|
|
8
|
+
import type { Space } from './space.ts'
|
|
9
|
+
|
|
10
|
+
const RETRY_MS = 60_000
|
|
11
|
+
const RETRY_MAX_MS = 6 * 3600_000
|
|
12
|
+
|
|
13
|
+
export interface Waiting {
|
|
14
|
+
key: string
|
|
15
|
+
size: number
|
|
16
|
+
path: string
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** the objects waiting with a local copy, oldest retry first; `store` limits them to one store */
|
|
20
|
+
export function waitingObjects(o: { now?: number; limit?: number; store?: string } = {}): Waiting[] {
|
|
21
|
+
const db = mediaDb()
|
|
22
|
+
if (!db) return []
|
|
23
|
+
const rows = db
|
|
24
|
+
.query(
|
|
25
|
+
`select o.key as key, o.size as size, min(f.store || char(0) || f.item || char(0) || f.name) as at
|
|
26
|
+
from objects o join files f on f.key = o.key and f.local = 1
|
|
27
|
+
where o.up is null and o.next <= ? ${o.store ? 'and f.store = ?' : ''}
|
|
28
|
+
group by o.key order by o.next, o.key limit ?`,
|
|
29
|
+
)
|
|
30
|
+
.all(...([o.now ?? Date.now(), ...(o.store ? [o.store] : []), o.limit ?? 1_000_000] as [number, ...(string | number)[]])) as { key: string; size: number; at: string }[]
|
|
31
|
+
return rows.map((r) => {
|
|
32
|
+
const [store = '', item = '', name = ''] = r.at.split('\0')
|
|
33
|
+
return { key: r.key, size: r.size, path: join(store, item, name) }
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface UploadResult {
|
|
38
|
+
up: number
|
|
39
|
+
/** already in the Space: marked, not sent */
|
|
40
|
+
there: number
|
|
41
|
+
bytes: number
|
|
42
|
+
failed: { key: string; error: string }[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** one object up: true when it was sent, false when the Space already held it; throws with the reason */
|
|
46
|
+
export async function uploadOne(space: Space, w: Waiting): Promise<boolean> {
|
|
47
|
+
const size = statSync(w.path, { throwIfNoEntry: false })?.size
|
|
48
|
+
if (size !== w.size) throw new Error(`${w.path}: ${size === undefined ? 'gone' : `${size} bytes, the index says ${w.size}`}`)
|
|
49
|
+
const before = await space.head(w.key)
|
|
50
|
+
if (before?.size === w.size) {
|
|
51
|
+
recordUp(w.key, w.size)
|
|
52
|
+
return false
|
|
53
|
+
}
|
|
54
|
+
await space.put(w.key, w.path, typeOf(w.path))
|
|
55
|
+
const after = await space.head(w.key)
|
|
56
|
+
if (after?.size !== w.size) throw new Error(`${w.key}: the Space holds ${after ? `${after.size} bytes` : 'nothing'} after the upload, ${w.size} sent`)
|
|
57
|
+
recordUp(w.key, w.size)
|
|
58
|
+
return true
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** every waiting object (or `limit` of them), `jobs` at a time; `say` hears progress lines */
|
|
62
|
+
export async function uploadWaiting(space: Space, o: { limit?: number; jobs?: number; store?: string; now?: number; say?: (l: string) => void } = {}): Promise<UploadResult> {
|
|
63
|
+
const list = waitingObjects({ ...(o.limit !== undefined ? { limit: o.limit } : {}), ...(o.store ? { store: o.store } : {}), ...(o.now !== undefined ? { now: o.now } : {}) })
|
|
64
|
+
const out: UploadResult = { up: 0, there: 0, bytes: 0, failed: [] }
|
|
65
|
+
const total = list.reduce((n, w) => n + w.size, 0)
|
|
66
|
+
let next = 0
|
|
67
|
+
let done = 0
|
|
68
|
+
let said = Date.now()
|
|
69
|
+
const work = async (): Promise<void> => {
|
|
70
|
+
for (;;) {
|
|
71
|
+
const w = list[next++]
|
|
72
|
+
if (!w) return
|
|
73
|
+
try {
|
|
74
|
+
if (await uploadOne(space, w)) {
|
|
75
|
+
out.up++
|
|
76
|
+
out.bytes += w.size
|
|
77
|
+
} else out.there++
|
|
78
|
+
} catch (e) {
|
|
79
|
+
const error = (e as Error).message
|
|
80
|
+
out.failed.push({ key: w.key, error })
|
|
81
|
+
tryWrite(`${w.key} failed`, (db) => {
|
|
82
|
+
const r = db.query('select tries from objects where key = ?').get(w.key) as { tries: number } | null
|
|
83
|
+
const tries = (r?.tries ?? 0) + 1
|
|
84
|
+
db.query('update objects set tries = ?, next = ?, error = ? where key = ?').run(tries, Date.now() + Math.min(RETRY_MS * 2 ** (tries - 1), RETRY_MAX_MS), error, w.key)
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
done += w.size
|
|
88
|
+
if (o.say && Date.now() - said > 10_000) {
|
|
89
|
+
said = Date.now()
|
|
90
|
+
o.say(` ${out.up + out.there + out.failed.length}/${list.length} objects, ${mb(done)} of ${mb(total)}`)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
await Promise.all(Array.from({ length: Math.max(1, o.jobs ?? 4) }, work))
|
|
95
|
+
return out
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const mb = (b: number): string => (b >= 1e9 ? `${(b / 1e9).toFixed(2)} GB` : `${(b / 1e6).toFixed(1)} MB`)
|
package/src/repo-config.ts
CHANGED
|
@@ -70,6 +70,10 @@ export type ServeConfig = {
|
|
|
70
70
|
/** the ids of the global processes this repo needs (any house repo's
|
|
71
71
|
* `globalProcess` unit files). The home rail marks the repo while one is off */
|
|
72
72
|
uses?: string[]
|
|
73
|
+
/** the house's private S3 bucket that keeps every file of the machine's
|
|
74
|
+
* media store (review cards, trailer cuts...), read only in the house
|
|
75
|
+
* config; its key pair read through `credsReadCommand` */
|
|
76
|
+
mediaSpace?: import('./media-store/space.ts').MediaSpace
|
|
73
77
|
}
|
|
74
78
|
|
|
75
79
|
export type RepoConfig = {
|
package/src/review/anim.ts
CHANGED
|
@@ -7,7 +7,8 @@ import { basename, join, resolve } from 'node:path'
|
|
|
7
7
|
import { ffmpeg } from '../trailer/ffmpeg.ts'
|
|
8
8
|
import { sizeOf } from './media.ts'
|
|
9
9
|
import { isGallery, isPair } from './model.ts'
|
|
10
|
-
import {
|
|
10
|
+
import { ensureLocal } from './media-read.ts'
|
|
11
|
+
import { readItem } from './store.ts'
|
|
11
12
|
|
|
12
13
|
export const ANIM_MODES = ['cut', 'fade', 'wipe'] as const
|
|
13
14
|
export type AnimMode = (typeof ANIM_MODES)[number]
|
|
@@ -156,7 +157,7 @@ function graph(mode: AnimMode, w: number, h: number, fps: number, labels: boolea
|
|
|
156
157
|
const making = new Map<string, Promise<Anim>>()
|
|
157
158
|
|
|
158
159
|
/** the animation of view `ask.view` of card `id`, made once and kept under `<dir>/.anims/` (readAll skips dot names) */
|
|
159
|
-
export function pairAnimation(dir: string, id: string, ask: AnimAsk): Promise<Anim> {
|
|
160
|
+
export async function pairAnimation(dir: string, id: string, ask: AnimAsk): Promise<Anim> {
|
|
160
161
|
const x = readItem(dir, id)
|
|
161
162
|
if (!x) return Promise.reject(new Error(`no card ${id}`))
|
|
162
163
|
const v = x.item.views[ask.view]
|
|
@@ -164,14 +165,14 @@ export function pairAnimation(dir: string, id: string, ask: AnimAsk): Promise<An
|
|
|
164
165
|
if (isGallery(v) || !isPair(v.files)) return Promise.reject(new Error(`view ${ask.view} of ${id} is not a before and after`))
|
|
165
166
|
if (v.clip) return Promise.reject(new Error(`view ${ask.view} of ${id} is a pair of clips: no animation`))
|
|
166
167
|
if (v.audio) return Promise.reject(new Error(`view ${ask.view} of ${id} is a pair of sounds: no animation`))
|
|
167
|
-
|
|
168
|
-
const after =
|
|
168
|
+
// a pair the disk let go comes back from the Space first
|
|
169
|
+
const [before, after] = await Promise.all([ensureLocal(dir, id, v.files.before), ensureLocal(dir, id, v.files.after)])
|
|
169
170
|
if (!before || !after) return Promise.reject(new Error(`view ${ask.view} of ${id}: an image is missing`))
|
|
170
171
|
const viewPart = ask.view > 0 ? `-${asciiName(v.label || `view-${ask.view + 1}`, 30)}` : ''
|
|
171
172
|
const name = `${asciiName(x.item.title)}${viewPart}-${ask.mode}.${ask.format}`
|
|
172
173
|
const out = join(dir, '.anims', id, `L${LOOK}-v${ask.view}-${ask.mode}-${ask.labels}.${ask.format}`)
|
|
173
174
|
const newest = Math.max(statSync(before).mtimeMs, statSync(after).mtimeMs)
|
|
174
|
-
if (existsSync(out) && statSync(out).mtimeMs >= newest) return
|
|
175
|
+
if (existsSync(out) && statSync(out).mtimeMs >= newest) return { file: out, name, ms: 0 }
|
|
175
176
|
const pending = making.get(out)
|
|
176
177
|
if (pending) return pending
|
|
177
178
|
const job = render(before, after, out, v.size, ask)
|