code wiki / (root) / nx_yt_nsolve.nx

nx_yt_nsolve.nx source

↩ module page · 111 lines · 5535 B

1// nx_yt_nsolve.nx -- the YouTube n-THROTTLE SOLVER. Modern googlevideo URLs carry an &n=<token> param; if you 2// fetch with the RAW n, the CDN throttles you to ~50KB/s (broken/slow downloads). base.js ships a JS function 3// that transforms n -> n' ; requesting with n' gives full speed. That function is JS (array/char scrambling), 4// exactly the class our engine already runs (see nx_js_ytsig_gate: split->transform->join). So the solver is: 5// (1) EXTRACT the named function from base.js text -- brace-balanced + STRING-AWARE so literals with { } " ' in 6// the body don't truncate it; (2) BUILD a self-contained runnable script (def + call); (3) RUN it via 7// js_run_source and read the string result. This turns an extracted throttled URL into a FAST one, sovereignly 8// (our own JS engine -- no Node, no remote). The function NAME + its helper-array location are themselves 9// site-shape patterns -> feed the self-healing table (nx_extract_heal) when YouTube reshapes base.js. license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_media_signal.nx" // xt_find 12import "nx_js_eval.nx" // js_run_source + VAL_STR + ev_str_bytes/len 13 14func ns_apps(dst: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8) { dst[off]=s[i]; off=off+1; i=i+1 } return off } 15func ns_appb(dst: *u8, off: i64, src: *u8, from: i64, n: i64) -> i64 { var i: i64=0; while i<n { dst[off]=src[from+i]; off=off+1; i=i+1 } return off } 16 17// skip a JS string literal starting at the opening quote `q` (34=" 39='); returns index AFTER the closing quote, 18// honoring backslash escapes. Keeps brace-counting from being fooled by { } inside "..." / '...'. 19func ns_skip_str(js: *u8, jl: i64, start: i64, q: i64) -> i64 { 20 var j: i64 = start + 1 21 var go: i64 = 1 22 while go == 1 { 23 if j >= jl { go = 0 } 24 else { let c: i64 = js[j] & 0xff 25 if c == 92 { j = j + 2 } 26 else { if c == q { j = j + 1; go = 0 } else { j = j + 1 } } } 27 } 28 return j 29} 30 31// find the START of the function named `name` in base.js: assignment form `name=function` OR declaration form 32// `function name`. Sets isdecl[0] (0=assignment, 1=declaration). Returns start index or -1. 33func ns_find_fn_start(js: *u8, jl: i64, name: *u8, namelen: i64, isdecl: *i64) -> i64 { 34 let nd: *u8 = sys_mmap(512) 35 // assignment: name + "=function" 36 var o: i64 = ns_appb(nd, 0, name, 0, namelen) 37 o = ns_apps(nd, o, "=function" as *u8) 38 let p: i64 = xt_find(js, jl, nd, o, 0) 39 if p >= 0 { isdecl[0] = 0; return p } 40 // declaration: "function " + name 41 o = ns_apps(nd, 0, "function " as *u8) 42 o = ns_appb(nd, o, name, 0, namelen) 43 let q: i64 = xt_find(js, jl, nd, o, 0) 44 if q >= 0 { isdecl[0] = 1; return q } 45 return 0 - 1 46} 47 48// from `from`, find the first `{`, then return the index just AFTER its matching `}` (string-aware). -1 if none. 49func ns_body_end(js: *u8, jl: i64, from: i64) -> i64 { 50 var i: i64 = from 51 var found: i64 = 0 - 1 52 while i < jl { if (js[i]&0xff) == 123 { found = i; i = jl } else { i = i + 1 } } 53 if found < 0 { return 0 - 1 } 54 var depth: i64 = 0 55 var j: i64 = found 56 var end: i64 = 0 - 1 57 var go: i64 = 1 58 while go == 1 { 59 if j >= jl { go = 0 } 60 else { 61 let c: i64 = js[j] & 0xff 62 if c == 34 { j = ns_skip_str(js, jl, j, 34) } 63 else { if c == 39 { j = ns_skip_str(js, jl, j, 39) } 64 else { 65 if c == 123 { depth = depth + 1 } 66 if c == 125 { depth = depth - 1 } 67 if c == 125 { if depth == 0 { end = j + 1; go = 0 } } 68 j = j + 1 69 } } 70 } 71 } 72 return end 73} 74 75// BUILD a self-contained runnable script that defines the extracted function and CALLS it with `arg`. For the 76// assignment form we prepend `var ` so the name is bound; the last expression is the call (its value = result). 77// Returns script length in `out`, or a negative extract error. 78func ns_build_script(js: *u8, jl: i64, name: *u8, namelen: i64, arg: *u8, arglen: i64, out: *u8, cap: i64) -> i64 { 79 let isdecl: *i64 = sys_mmap(8) as *i64 80 let start: i64 = ns_find_fn_start(js, jl, name, namelen, isdecl) 81 if start < 0 { return 0 - 1 } 82 let end: i64 = ns_body_end(js, jl, start) 83 if end < 0 { return 0 - 2 } 84 var o: i64 = 0 85 if isdecl[0] == 0 { o = ns_apps(out, o, "var " as *u8) } 86 o = ns_appb(out, o, js, start, end - start) 87 o = ns_apps(out, o, ";" as *u8) 88 o = ns_appb(out, o, name, 0, namelen) 89 o = ns_apps(out, o, "(\"" as *u8) 90 o = ns_appb(out, o, arg, 0, arglen) 91 o = ns_apps(out, o, "\")" as *u8) 92 out[o] = 0 as u8 93 return o 94} 95 96// SOLVE: extract the n-function `name` from base.js, run it on `arg` through our JS engine, write the transformed 97// string to `out`. Returns the result length, or a negative error (-1/-2 extract, -3 run, -4 non-string result). 98func ns_solve(js: *u8, jl: i64, name: *u8, namelen: i64, arg: *u8, arglen: i64, out: *u8, cap: i64) -> i64 { 99 let script: *u8 = sys_mmap(1048576) 100 let slen: i64 = ns_build_script(js, jl, name, namelen, arg, arglen, script, 1048576) 101 if slen < 0 { return slen } 102 let res: *i64 = sys_mmap(16) as *i64 103 if js_run_source(script, slen, res) != 0 { return 0 - 3 } 104 if res[0] != VAL_STR { return 0 - 4 } 105 let srec: *i64 = (res[1]) as *i64 106 let sb: *u8 = ev_str_bytes(srec); let sl: i64 = ev_str_len(srec) 107 var i: i64 = 0 108 while i < sl { if i < (cap - 1) { out[i] = sb[i] } i = i + 1 } 109 out[sl] = 0 as u8 110 return sl 111}