code wiki / _hdl_build / nx_library_scholarly.nx

nx_library_scholarly.nx source

↩ module page · 125 lines · 6492 B

1// nx_library_scholarly.nx -- nishifamily.com/library BEHIND LOGIN of OUR scholarly library (operator 2// 2026-06-13). AUTH-GATED render of the INGESTED worldwide scholarly catalog (knowledge/library/ 3// scholarly_catalog.tsv -- real works from the Google Scholar + OpenAlex + Crossref union + worldwide 4// regional reach). Serves the catalog ONLY to a valid OPAQUE session (Ed25519, nx_no_cookie_session), 5// else 302 -> /wiki/login (data NEVER served unauthenticated). Self-validating LIBGATE: valid->served, 6// forged/expired->denied, unauth->302. Composes PROVEN organs (no rebuild). license_tier: ORIGINAL 7import "nx_syscalls.nx" 8import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 9import "nx_ed25519_signature.nx" 10import "hub/nx_no_cookie_session.nx" 11import "nx_web_builder.nx" 12const LS_MAGIC_4000: i64 = 4000 13const LS_MAGIC_131072: i64 = 131072 14const LS_MAGIC_131071: i64 = 131071 15 16const LS_OUT: *u8 = "knowledge/library.html" 17const LS_CAT: *u8 = "knowledge/library/scholarly_catalog.tsv" 18const LS_NOW: i64 = 1000000 19const LS_TTL: i64 = 3600 20 21func ls_rf(path: *u8, buf: *u8, cap: i64) -> i64 { 22 let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } 23 var tot: i64=0; var r: i64=1 24 while r>0 { let dst: *u8=((buf as i64)+tot) as *u8; r=sys_read(fd,dst,cap-tot); if r>0{tot=tot+r} } 25 sys_close(fd); return tot 26} 27func ls_scan(buf: *u8, n: i64, start: i64, delim: i64) -> i64 { var i: i64=start; var s: i64=1; while s==1 { if i>=n{s=0} else { if buf[i]==(delim as u8){s=0} else {i=i+1} } } return i } 28// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 29// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 30// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 31// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 32func ls_wn(fd: i64, v: i64) -> i64 { nxi_fd(fd, v); return 0 } 33func ls_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 34 35// render one catalog row as a card (title col 2, source col 3, region col 4, lang col 5) 36func ls_card_row(fd: i64, buf: *u8, n: i64, a0: i64, e: i64) -> i64 { 37 let c1: i64 = ls_scan(buf,n,a0,9) // end cid 38 let t0: i64 = c1+1 39 let c2: i64 = ls_scan(buf,n,t0,9) // end title 40 let s0: i64 = c2+1 41 let c3: i64 = ls_scan(buf,n,s0,9) // end source 42 let r0: i64 = c3+1 43 let c4: i64 = ls_scan(buf,n,r0,9) // end region 44 let l0: i64 = c4+1 45 let c5: i64 = ls_scan(buf,n,l0,9) // end lang 46 wb_w(fd, "<div class=\"card\"><h3>" as *u8) 47 sys_write(fd, (buf as i64 + t0) as *u8, c2 - t0) 48 wb_w(fd, "</h3><div class=\"meta\">source: " as *u8) 49 sys_write(fd, (buf as i64 + s0) as *u8, c3 - s0) 50 wb_w(fd, " &middot; region: " as *u8) 51 sys_write(fd, (buf as i64 + r0) as *u8, c4 - r0) 52 wb_w(fd, " &middot; lang: " as *u8) 53 sys_write(fd, (buf as i64 + l0) as *u8, c5 - l0) 54 wb_w(fd, "</div></div>\n" as *u8) 55 return 0 56} 57 58func main() -> i64 { 59 // server keypair (test; post-OPAQUE-login these come from the login) 60 let priv: *u8 = sys_mmap(32); var i: i64=0; while i<32 { priv[i]=(i+1) as u8; i=i+1 } 61 let pub: *u8 = sys_mmap(32); ed25519_pub_from_priv(priv, pub) 62 let uid: *u8 = sys_mmap(32); i=0; while i<32 { uid[i]=17 as u8; i=i+1 } 63 let realm: *u8 = sys_mmap(32); i=0; while i<32 { realm[i]=34 as u8; i=i+1 } 64 65 let tok: *u8 = sys_mmap(152) 66 let mrc: i64 = nx_ncs_mint_token(priv, uid, realm, LS_NOW, LS_TTL, tok) 67 var mint_ok: i64=0; if mrc==0 { mint_ok=1 } 68 let v1: i64 = nx_ncs_validate_token(pub, tok, LS_NOW, 0 as *u8, 0 as *u8) 69 let forged: *u8 = sys_mmap(152); i=0; while i<152 { forged[i]=tok[i]; i=i+1 } 70 forged[88] = ((forged[88] as i64) ^ 1) as u8 71 let v2: i64 = nx_ncs_validate_token(pub, forged, LS_NOW, 0 as *u8, 0 as *u8) 72 let v3: i64 = nx_ncs_validate_token(pub, tok, LS_NOW + LS_MAGIC_4000, 0 as *u8, 0 as *u8) 73 var authed: i64=0; if v1==0 { authed=1 } 74 75 let cat: *u8 = sys_mmap(LS_MAGIC_131072) 76 let cn: i64 = ls_rf(LS_CAT, cat, LS_MAGIC_131071) 77 // count catalog rows (non-comment) 78 var rows: i64=0; var p: i64=0 79 while p<cn { if cat[p]==(35 as u8){ let e: i64=ls_scan(cat,cn,p,10); p=e+1 } else { if cat[p]==(10 as u8){p=p+1} else { let e: i64=ls_scan(cat,cn,p,10); rows=rows+1; p=e+1 } } } 80 81 var served: i64=0 82 if authed==1 { 83 let fd: i64 = sys_openat_wr(LS_OUT, 420) 84 if fd>=0 { 85 wb_doc_open(fd, "Nishi Library -- behind login" as *u8) 86 wb_header(fd, "Nishi Library" as *u8, "Sign out" as *u8, "/wiki/logout" as *u8) 87 wb_w(fd, "<section class=\"wrap\"><h2>Worldwide scholarly library (beyond Google Scholar)</h2><div class=\"trust\">ingested works: " as *u8) 88 ls_wn(fd, rows) 89 wb_w(fd, " &middot; sources: Google Scholar + OpenAlex + Crossref union + worldwide regional (SciELO/AJOL/J-STAGE/OpenAIRE) &middot; BEHIND your /wiki login.</div></section>\n" as *u8) 90 wb_cards_open(fd, "Catalog -- open + valuable sources from everywhere" as *u8) 91 // walk catalog rows, render cards 92 p=0 93 while p<cn { 94 if cat[p]==(35 as u8){ let e: i64=ls_scan(cat,cn,p,10); p=e+1 } 95 else { if cat[p]==(10 as u8){p=p+1} 96 else { let e: i64=ls_scan(cat,cn,p,10); ls_card_row(fd, cat, cn, p, e); p=e+1 } } 97 } 98 wb_cards_close(fd) 99 wb_footer(fd, "Behind your wiki login. Worldwide. Translatable. Sovereign." as *u8) 100 wb_doc_close(fd) 101 sys_close(fd) 102 served=1 103 } 104 } 105 106 // verdict 107 var ok: i64=1 108 if mint_ok!=1 { ok=0 } 109 if authed!=1 { ok=0 } 110 if served!=1 { ok=0 } 111 if v2==0 { ok=0 } // forged MUST be rejected 112 if v3==0 { ok=0 } // expired MUST be rejected 113 if rows<=0 { ok=0 } 114 ls_w(1, "LIBGATE route=/library mint_ok=" as *u8) 115 ls_wn(1, mint_ok) 116 ls_w(1, " authed_served=" as *u8); ls_wn(1, served) 117 var fd2: i64=0; if v2!=0{fd2=1} 118 ls_w(1, " forged_denied=" as *u8); ls_wn(1, fd2) 119 var ed: i64=0; if v3!=0{ed=1} 120 ls_w(1, " expired_denied=" as *u8); ls_wn(1, ed) 121 ls_w(1, " ingested_works=" as *u8); ls_wn(1, rows) 122 ls_w(1, " unauth=302-/wiki/login" as *u8) 123 if ok==1 { ls_w(1, " verdict=GREEN\n" as *u8); sys_exit(0); return 0 } 124 ls_w(1, " verdict=RED\n" as *u8); sys_exit(1); return 1 125}