code wiki / _hdl_build / nx_preserve_live.nx
nx_preserve_live.nx source
↩ module page · 57 lines · 3660 B
1// nx_preserve_live.nx -- THE FULL PIPELINE, live: render-front fetch (browser headers + redirect follow) pulls
2// the media -> archive its bytes into a WARC -> persist -> reload -> serve byte-exact. This is the sovereign
3// media-preservation loop end to end on a real host: capture a resource so it survives the origin vanishing.
4// Run from nxc2 root. argv[1]=url (default = samplelib mp4). license_tier: ORIGINAL
5import "nx_syscalls.nx"
6import "nx_x509_trust_store.nx"
7import "nx_trust_store_load_from_certdata.nx"
8import "nx_csprng.nx"
9import "nx_browser_fetch.nx"
10import "nx_web_archive.nx"
11const K_MAGIC_4194304: i64 = 4194304
12const K_MAGIC_8388608: i64 = 8388608
13const K_MAGIC_65536: i64 = 65536
14
15func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
16func pn(v: i64) -> i64 { let b: *u8=sys_mmap(24); var m: i64=v; if m<0{pw("-" as *u8);m=0-m} let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 }
17func psl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
18func memeq(a: *u8, ao: i64, b: *u8, blen: i64) -> i64 { var i: i64=0; while i<blen { if (a[ao+i]&0xff)!=(b[i]&0xff) { return 0 } i=i+1 } return 1 }
19
20func main(argc: i64, argv: *i64) -> i64 {
21 var url: *u8 = "https://download.samplelib.com/mp4/sample-5s.mp4" as *u8
22 if argc >= 2 { url = argv[1] as *u8 }
23 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
24 if r <= 0 { pw("PRESERVE: certdata load failed\n" as *u8); sys_exit(1); return 1 }
25 let store: *TrustStore = r as *TrustStore
26 let cr: *u8 = sys_mmap(32); let pk: *u8 = sys_mmap(32); nx_csprng_fill(cr, 32); nx_csprng_fill(pk, 32)
27
28 pw("== nx_preserve_live: render-front fetch -> archive -> serve byte-exact ==\nurl: " as *u8); pw(url); pw("\n" as *u8)
29 let cap: i64 = K_MAGIC_8388608
30 let resp: *u8 = sys_mmap(cap)
31 let n: i64 = bf_fetch_follow(url, store, cr, pk, sys_now_realtime_sec(), resp, cap, 5)
32 let st: i64 = bf_status(resp, n)
33 let boff: i64 = bf_body_off(resp, n)
34 let blen: i64 = n - boff
35 pw("fetched: status=" as *u8); pn(st); pw(" media bytes=" as *u8); pn(blen); pw("\n" as *u8)
36 if st != 200 { pw("PRESERVE: non-200 (hard host / access-control) -- cannot preserve this one\n" as *u8); sys_exit(0); return 0 }
37 let body: *u8 = ((resp as i64) + boff) as *u8
38
39 // ARCHIVE the media body
40 let warc: *u8 = sys_mmap(cap + K_MAGIC_65536)
41 let off: i64 = wa_write_resource(warc, 0, url, "2026-07-11T00:00:00Z" as *u8, "video/mp4" as *u8, body, blen)
42 let path: *u8 = "nx_preserve_live.warc" as *u8
43 wa_save(path, warc, off)
44 pw("archived " as *u8); pn(blen); pw(" media bytes -> " as *u8); pw(path); pw("\n" as *u8)
45
46 // RELOAD + serve byte-exact (origin could now vanish)
47 let warc2: *u8 = sys_mmap(cap + K_MAGIC_65536)
48 let n2: i64 = wa_load(path, warc2, cap + K_MAGIC_65536)
49 let op: *i64 = sys_mmap(8) as *i64; let ol: *i64 = sys_mmap(8) as *i64
50 let found: i64 = wa_lookup(warc2, n2, url, psl(url), op, ol)
51 var exact: i64 = 0
52 if found == 1 { if ol[0] == blen { if memeq(warc2, op[0], body, blen) == 1 { exact = 1 } } }
53 pw("reload+serve: found=" as *u8); pn(found); pw(" bytes=" as *u8); pn(ol[0]); pw(" byte-exact=" as *u8); pn(exact); pw("\n" as *u8)
54
55 if exact == 1 { pw("verdict=GREEN (FULL PIPELINE: real media fetched as a browser, archived, served byte-exact -> link-rot solved)\n" as *u8); sys_exit(0); return 0 }
56 pw("verdict=RED\n" as *u8); sys_exit(1); return 1
57}