code wiki / _hdl_build / nx_dist_publish.nx

nx_dist_publish.nx source

↩ module page · 59 lines · 3885 B

1// nx_dist_publish.nx -- LIB: the HUB-AS-ENABLER multi-transport publish orchestrator (operator: "when we get data 2// on the hub we can pass it to other systems via torrent OR download OR api OR whatever"). When a file lands, ONE 3// call turns it into EVERY transport at once, content-addressed: 4// content-id = SHA-256 of the bytes (nx_sha256, streamed 8MB -- multi-GB safe) [the CID / v2 multihash] 5// magnet = magnet:?xt=urn:btmh:1220<sha256hex>&dn=<name> [BitTorrent v2 -> the swarm] 6// download = https://nishifamily.com/dist/<cid>/<name> [direct HTTPS -> the sites daemon] 7// api = /api/dist/<cid> [content-addressed API] 8// index-row = name<TAB>cid<TAB>size<TAB>magnet<TAB>url -> the Napster-style registry (who-has-what SSOT) 9// Composes our shipped organs (nx_sha256; live rung wires nx_torrent_up to seed + the sites daemon to serve + the 10// mgmt API to register). NO TLS import -> nx_sha256 shares this unit cleanly. No main -> built via its _gate. 11// license_tier: ORIGINAL 12import "nx_sha256.nx" 13import "nx_syscalls.nx" 14const K_MAGIC_8388608: i64 = 8388608 15 16func dp_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64=off; var i: i64=0; while s[i]!=(0 as u8){dst[o]=s[i];o=o+1;i=i+1} return o } 17func dp_wdec(dst: *u8, off: i64, v: i64) -> i64 { var o: i64=off; if v==0 { dst[o]=48 as u8; return o+1 } let t:*u8=sys_mmap(24); var k:i64=0; var m:i64=v; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } var j:i64=k-1; while j>=0 { dst[o]=t[j]; o=o+1; j=j-1 } return o } 18func dp_hexenc(inp: *u8, n: i64, out: *u8) -> i64 { let hx: *u8="0123456789abcdef" as *u8; var i: i64=0; while i<n { out[i*2]=hx[((inp[i] as i64)>>4)&15]; out[i*2+1]=hx[(inp[i] as i64)&15]; i=i+1 } out[n*2]=0 as u8; return n*2 } 19 20// content-id = SHA-256 of the file (streamed, multi-GB safe) -> out_hex (64 lowercase hex); returns byte count or -1. 21func dp_content_id(path: *u8, out_hex: *u8) -> i64 { 22 let ctx_raw: *u8 = sys_mmap(256) 23 let ctx: *Sha256 = ctx_raw as *Sha256 24 sha256_init(ctx) 25 let fd: i64 = sys_openat_rd(path) 26 if fd < 0 { return 0 - 1 } 27 let cap: i64 = K_MAGIC_8388608 28 let buf: *u8 = sys_mmap(cap) 29 var total: i64 = 0 30 var got: i64 = sys_read(fd, buf, cap) 31 while got > 0 { sha256_update(ctx, buf, got); total = total + got; got = sys_read(fd, buf, cap) } 32 sys_close(fd) 33 let dig: *u8 = sys_mmap(32); sha256_final(ctx, dig) 34 dp_hexenc(dig, 32, out_hex) 35 return total 36} 37 38// BitTorrent v2 magnet (urn:btmh, 1220 = the multihash prefix for sha2-256/32B). 39func dp_magnet(cid_hex: *u8, name: *u8, out: *u8) -> i64 { 40 var o: i64 = dp_cat(out, 0, "magnet:?xt=urn:btmh:1220" as *u8) 41 o = dp_cat(out, o, cid_hex); o = dp_cat(out, o, "&dn=" as *u8); o = dp_cat(out, o, name) 42 out[o] = 0 as u8; return o 43} 44// direct HTTPS download URL (served by the sovereign sites daemon). 45func dp_dl_url(cid_hex: *u8, name: *u8, out: *u8) -> i64 { 46 var o: i64 = dp_cat(out, 0, "https://nishifamily.com/dist/" as *u8) 47 o = dp_cat(out, o, cid_hex); o = dp_cat(out, o, "/" as *u8); o = dp_cat(out, o, name) 48 out[o] = 0 as u8; return o 49} 50// content-addressed API endpoint. 51func dp_api(cid_hex: *u8, out: *u8) -> i64 { var o: i64 = dp_cat(out, 0, "/api/dist/" as *u8); o = dp_cat(out, o, cid_hex); out[o]=0 as u8; return o } 52// Napster-style distribution-INDEX row (the who-has-what registry SSOT): name<TAB>cid<TAB>size<TAB>magnet<TAB>url. 53func dp_index_row(name: *u8, cid_hex: *u8, size: i64, magnet: *u8, url: *u8, out: *u8) -> i64 { 54 var o: i64 = dp_cat(out, 0, name); out[o]=9 as u8; o=o+1 55 o = dp_cat(out, o, cid_hex); out[o]=9 as u8; o=o+1 56 o = dp_wdec(out, o, size); out[o]=9 as u8; o=o+1 57 o = dp_cat(out, o, magnet); out[o]=9 as u8; o=o+1 58 o = dp_cat(out, o, url); out[o]=0 as u8; return o 59}