code wiki / (root) / nx_lib_ingest_json.nx

nx_lib_ingest_json.nx source

↩ module page · 38 lines · 1640 B

1// nx_lib_ingest_json.nx -- the deterministic tail of the sovereign ingest: 2// an HTTP response (from nx_lib_fetch) -> strip headers -> nx_lib_parse_work 3// -> nx_lib_store_put_work. Pure/local/gateable (no network). Composed with 4// nx_lib_fetch this is the full fetch->parse->store ingest that retires the 5// Python httpx+json+pydantic pipeline end to end. 6// license_tier: ORIGINAL | genealogy_id: nishi_library_sovereign_ingest_json_2026_07_01 7import "nx_lib_parse.nx" 8import "nx_lib_store.nx" 9 10// parse an HTTP response body as a work JSON and store it under hk. 11// returns the stored record length, or a negative error: 12// -2 = unparseable, -3 = no title (not a valid work). 13func nx_lib_ingest_json_response(resp: *u8, resplen: i64, hk: *u8, hklen: i64) -> i64 { 14 // locate the body after the CRLF CRLF header terminator (if any) 15 var bs: i64 = 0 - 1 16 var bi: i64 = 0 17 while bi + 3 < resplen { 18 if bs < 0 { 19 if resp[bi] == 13 as u8 { if resp[bi+1] == 10 as u8 { if resp[bi+2] == 13 as u8 { if resp[bi+3] == 10 as u8 { bs = bi + 4 } } } } 20 } 21 bi = bi + 1 22 } 23 var bodyptr: *u8 = resp 24 var bodylen: i64 = resplen 25 if bs >= 0 { bodyptr = ((resp as i64) + bs) as *u8; bodylen = resplen - bs } 26 27 let rec: *u8 = sys_mmap(131072) 28 let n: i64 = nx_lib_parse_work(bodyptr, bodylen, rec) 29 if n <= 0 { return 0 - 2 } 30 if rec[0] == 9 as u8 { return 0 - 3 } // empty title -> reject 31 32 let hkbuf: *u8 = sys_mmap(1024) 33 var k: i64 = 0 34 while k < hklen { hkbuf[k] = hk[k]; k = k + 1 } 35 hkbuf[k] = 0 as u8 36 nx_lib_store_put_work(hkbuf, rec, n) 37 return n 38}