code wiki / _hdl_build / nx_archive_live.nx

nx_archive_live.nx source

↩ module page · 62 lines · 3951 B

1// nx_archive_live.nx -- the LIVE network front of the archive pipeline: fetch a real URL over sovereign TLS, run 2// the extractor on the live page, ARCHIVE the real fetched bytes into a WARC, persist it, then reload + retrieve 3// byte-exact -- proving the end-to-end "capture a live resource so it survives the origin vanishing" loop with 4// real network. Run from nxc2 root (certdata + warc path relative). Default target = example.com (always-up, 5// legal); pass any URL as argv[1]. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_x509_trust_store.nx" 8import "nx_trust_store_load_from_certdata.nx" 9import "nx_https_fetch_follow.nx" 10import "nx_media_signal.nx" 11import "nx_media_state.nx" 12import "nx_media_extract.nx" 13import "nx_web_archive.nx" 14const K_MAGIC_4194304: i64 = 4194304 15const K_MAGIC_8388608: i64 = 8388608 16const K_MAGIC_4096: i64 = 4096 17const K_MAGIC_65536: i64 = 65536 18 19func pw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 20func 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 } 21func psl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 22func 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 } 23 24func main(argc: i64, argv: *i64) -> i64 { 25 var url: *u8 = "https://example.com" as *u8 26 if argc >= 2 { url = argv[1] as *u8 } 27 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 28 if r <= 0 { pw("ARCHIVE-LIVE: certdata load failed (run from nxc2 root)\n" as *u8); sys_exit(1); return 1 } 29 let store: *TrustStore = r as *TrustStore 30 let status: *i64 = sys_mmap(8) as *i64 31 32 pw("== nx_archive_live ==\ntarget: " as *u8); pw(url); pw("\n" as *u8) 33 let cap: i64 = K_MAGIC_8388608 34 let html: *u8 = sys_mmap(cap) 35 let hn: i64 = nx_https_fetch_follow(url, store, html, cap, 6, status) 36 pw("fetch status=" as *u8); pn(status[0]); pw(" bytes=" as *u8); pn(hn); pw("\n" as *u8) 37 if hn <= 0 { pw("ARCHIVE-LIVE: fetch FAILED (honest -- site may bot-block or need a real browser render)\n" as *u8); sys_exit(0); return 0 } 38 39 // extractor on the live page (what media would be preserved) 40 let best: *u8 = sys_mmap(K_MAGIC_4096); let kb: *i64 = sys_mmap(8) as *i64 41 let conf: i64 = mx_extract(html, hn, best, K_MAGIC_4096, kb) 42 pw("extract: media kind=" as *u8); pn(kb[0]); pw(" conf=" as *u8); pn(conf); pw("\n" as *u8) 43 44 // ARCHIVE the real fetched bytes 45 let warc: *u8 = sys_mmap(cap + K_MAGIC_65536) 46 let off: i64 = wa_write_resource(warc, 0, url, "2026-07-11T00:00:00Z" as *u8, "text/html" as *u8, html, hn) 47 let path: *u8 = "nx_live_archive.warc" as *u8 48 wa_save(path, warc, off) 49 pw("archived " as *u8); pn(hn); pw(" bytes -> " as *u8); pw(path); pw(" (record total " as *u8); pn(off); pw(" bytes)\n" as *u8) 50 51 // RELOAD + retrieve byte-exact (the origin could now vanish) 52 let warc2: *u8 = sys_mmap(cap + K_MAGIC_65536) 53 let n2: i64 = wa_load(path, warc2, cap + K_MAGIC_65536) 54 let op: *i64 = sys_mmap(8) as *i64; let ol: *i64 = sys_mmap(8) as *i64 55 let found: i64 = wa_lookup(warc2, n2, url, psl(url), op, ol) 56 var exact: i64 = 0 57 if found == 1 { if ol[0] == hn { if memeq(warc2, op[0], html, hn) == 1 { exact = 1 } } } 58 pw("reload+retrieve: found=" as *u8); pn(found); pw(" bytes=" as *u8); pn(ol[0]); pw(" byte-exact=" as *u8); pn(exact); pw("\n" as *u8) 59 60 if exact == 1 { pw("verdict=GREEN (live fetch -> archive -> reload -> serve byte-exact: the resource survives the origin dying)\n" as *u8); sys_exit(0); return 0 } 61 pw("verdict=RED (archive round-trip mismatch)\n" as *u8); sys_exit(1); return 1 62}