interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / server / jsonp.om
| 1 | note forged server/jsonp.om: shared pieces of the machine form of every repo and user page (ForgeServer, |
| 2 | note prefix fy / tfy; docs/API.md). A slice answers JSON when fjwant is true (Accept names |
| 3 | note application/json and not text/html) at the same URL as its HTML page, and sets fjalt 3 on the HTML |
| 4 | note page so the layout names that URL as its JSON alternate. Errors of a JSON request (any 4xx/5xx a |
| 5 | note handler leaves with an empty fob, including the server's 404 for a repo not in the public table) |
| 6 | note are a forge/error/1 document (ferrpage calls fyerr). |
| 7 | text tfyuser "forge/user/1" |
| 8 | text tfyerr1 "{\"schema\":\"forge/error/1\",\"status\":" |
| 9 | text tfyerr2 ",\"error\":" |
| 10 | text tfyerr3 ",\"message\":" |
| 11 | text tfyrmvg ",\"reason\":\"moving\"" |
| 12 | text tfyend "}\n" |
| 13 | text tfyh1 "{\"schema\":\"" |
| 14 | text tfyh2 "\",\"base\":\"" |
| 15 | text tfyh3 "\",\"repo\":{\"owner\":\"" |
| 16 | text tfyh4 "\",\"name\":\"" |
| 17 | text tfyh5 "\",\"rid\":\"" |
| 18 | text tfyh6 "\",\"path\":\"" |
| 19 | text tfyh7 "\",\"json\":\"" |
| 20 | text tfyh8 ".json\"}" |
| 21 | text tfyu1 "{\"schema\":\"" |
| 22 | text tfyu2 "\",\"base\":\"" |
| 23 | text tfyu3 "\",\"user\":" |
| 24 | note fyerr: the JSON error document for status st; the HTML title and message of the error page |
| 25 | fn fyerr st ulong to void |
| 26 | put fjalt 0 0 |
| 27 | ferrti st |
| 28 | fjstart |
| 29 | fput tfyerr1 |
| 30 | fnum st |
| 31 | fput tfyerr2 |
| 32 | fjtb |
| 33 | for i ulong get ftil 0 bound 256 |
| 34 | fjtc get fti i |
| 35 | end |
| 36 | fjstr |
| 37 | fput tfyerr3 |
| 38 | a = get fobl 0 |
| 39 | ferrmsg st |
| 40 | b = get fobl 0 |
| 41 | fjtb |
| 42 | for k ulong sub b a bound 65536 |
| 43 | fjtc get fob add a k |
| 44 | end |
| 45 | put fobl 0 a |
| 46 | fjstr |
| 47 | if and eq st 503 ne get fmvng 0 0 |
| 48 | fput tfyrmvg |
| 49 | end |
| 50 | fput tfyend |
| 51 | end |
| 52 | note fyrhead t: open a repo page document: {"schema":"<t>","base":"<base>","repo":{owner, name, rid, |
| 53 | note path, json}; the caller adds ,"key":value pairs (fyk) and closes it with fyend |
| 54 | fn fyrhead t view uchar * to void |
| 55 | i = get fridx 0 |
| 56 | fjstart |
| 57 | fput tfyh1 |
| 58 | fput t |
| 59 | fput tfyh2 |
| 60 | fputb |
| 61 | fput tfyh3 |
| 62 | fput tfowner |
| 63 | fput tfyh4 |
| 64 | for k ulong get fanl i bound 128 |
| 65 | fputc frnbyte i k |
| 66 | end |
| 67 | fput tfyh5 |
| 68 | fputid i |
| 69 | fput tfyh6 |
| 70 | fjpath i |
| 71 | fput tfyh7 |
| 72 | fjpath i |
| 73 | fput tfyh8 |
| 74 | end |
| 75 | note fyuhead t: open a user page document: {"schema":"<t>","base":"<base>","user":"<fown>" |
| 76 | fn fyuhead t view uchar * to void |
| 77 | fjstart |
| 78 | fput tfyu1 |
| 79 | fput t |
| 80 | fput tfyu2 |
| 81 | fputb |
| 82 | fput tfyu3 |
| 83 | fjtb |
| 84 | for k ulong get fownl 0 bound 64 |
| 85 | fjtc get fown k |
| 86 | end |
| 87 | fjstr |
| 88 | end |
| 89 | note fyk t: ,"<t>": (t is a plain ASCII key) |
| 90 | fn fyk t view uchar * to void |
| 91 | fputc 44 |
| 92 | fputc 34 |
| 93 | fput t |
| 94 | fputc 34 |
| 95 | fputc 58 |
| 96 | end |
| 97 | note fyk0 t: "<t>": without the comma (first key of an object) |
| 98 | fn fyk0 t view uchar * to void |
| 99 | fputc 34 |
| 100 | fput t |
| 101 | fputc 34 |
| 102 | fputc 58 |
| 103 | end |
| 104 | fn fyend to void |
| 105 | fput tfyend |
| 106 | end |
| 107 | fn fybool b bool to void |
| 108 | if b |
| 109 | fput tfjtrue |
| 110 | ret |
| 111 | end |
| 112 | fput tfjfals |
| 113 | end |
| 114 | fn fynull to void |
| 115 | fput tfjnull |
| 116 | end |
| 117 | note fysx a n: fxo[a, a+n) as a JSON string (fjfx, named here for the slices) |
| 118 | fn fysx a ulong n ulong to void |
| 119 | fjfx a n |
| 120 | end |
| 121 | note fysv s: the bytes of s as a JSON string |
| 122 | fn fysv s view uchar * to void |
| 123 | fjtb |
| 124 | for k ulong len s bound 65536 |
| 125 | fjtc get s k |
| 126 | end |
| 127 | fjstr |
| 128 | end |
| 129 | note fyself: this page's own URL as a JSON string: "<base><path without its first />[?<query>]" |
| 130 | fn fyself to void |
| 131 | fjtb |
| 132 | for k ulong get fbasel 0 bound 64 |
| 133 | fjtc get fbase k |
| 134 | end |
| 135 | n = get fpathl 0 |
| 136 | for k ulong sub n 1 bound 4096 |
| 137 | fjtc get fpath add k 1 |
| 138 | end |
| 139 | if gt get fqsl 0 0 |
| 140 | fjtc 63 |
| 141 | for k ulong get fqsl 0 bound 4096 |
| 142 | fjtc get fqs k |
| 143 | end |
| 144 | end |
| 145 | fjstr |
| 146 | end |
| 147 | note fyalt: the HTML page names its own URL as the JSON alternate (fhalt in pages.om) |
| 148 | fn fyalt to void |
| 149 | put fjalt 0 3 |
| 150 | end |
| 151 | note fypct a n: fxo[a, a+n) percent-encoded for a URL path; unreserved bytes and / are kept (the |
| 152 | note bytes written are URL- and JSON-safe) |
| 153 | fn fypct a ulong n ulong to void |
| 154 | m = hmin n 4096 |
| 155 | for k ulong m bound 4096 |
| 156 | fypcc get fxo add a k |
| 157 | end |
| 158 | end |
| 159 | note fypcc c: one byte of a URL path: unreserved and / as is, else %XX |
| 160 | fn fypcc c uchar to void |
| 161 | if fypok c |
| 162 | fputc c |
| 163 | ret |
| 164 | end |
| 165 | fputc 37 |
| 166 | fputc fjhexu shr ulong c 4 |
| 167 | fputc fjhexu and ulong c 15 |
| 168 | end |
| 169 | fn fypok c uchar to bool |
| 170 | if or fisaln c eq c 47 |
| 171 | ret true |
| 172 | end |
| 173 | if or eq c 46 eq c 45 |
| 174 | ret true |
| 175 | end |
| 176 | ret or eq c 95 eq c 126 |
| 177 | end |
| 178 | note ---- /search?q= (forge/search/1): the repositories whose name or description holds q, then the |
| 179 | note code matches of q on each default branch (git grep, at most 5 per repo and 100 in all), as fsearch |
| 180 | text tfysr1 "{\"schema\":\"forge/search/1\",\"base\":\"" |
| 181 | text tfysr2 "\",\"query\":" |
| 182 | text tfysr3 ",\n\"repos\":[" |
| 183 | text tfysr4 "],\n\"code\":[" |
| 184 | text tfysr5 "],\n\"code_truncated\":" |
| 185 | text tfysc1 "{\"repo\":" |
| 186 | text tfysc2 ",\"rid\":\"" |
| 187 | text tfysc3 "\",\"path\":" |
| 188 | text tfysc4 ",\"line\":" |
| 189 | text tfysc5 ",\"text\":" |
| 190 | text tfysc6 ",\"html\":\"" |
| 191 | text tfysc7 "/blob/" |
| 192 | text tfysc8 "#L" |
| 193 | text tfysc9 "\"}" |
| 194 | fn fyjsrch to void |
| 195 | fjstart |
| 196 | fput tfysr1 |
| 197 | fputb |
| 198 | fput tfysr2 |
| 199 | q = get fql 0 |
| 200 | if eq q 0 |
| 201 | fynull |
| 202 | else |
| 203 | fjtb |
| 204 | for k ulong q bound 256 |
| 205 | fjtc get fq k |
| 206 | end |
| 207 | fjstr |
| 208 | end |
| 209 | fput tfysr3 |
| 210 | n = ulong 0 |
| 211 | if gt q 0 |
| 212 | for i ulong get fan 0 bound FAMAX |
| 213 | if fsrmatch i |
| 214 | if gt n 0 |
| 215 | fput tfjsep |
| 216 | end |
| 217 | fputc 123 |
| 218 | fjcore i |
| 219 | fputc 125 |
| 220 | n = add n 1 |
| 221 | end |
| 222 | end |
| 223 | end |
| 224 | fput tfysr4 |
| 225 | put fhits 0 0 |
| 226 | if gt q 0 |
| 227 | for i ulong get fan 0 bound FAMAX |
| 228 | if lt get fhits 0 100 |
| 229 | fyjcsr i |
| 230 | end |
| 231 | end |
| 232 | end |
| 233 | fput tfysr5 |
| 234 | fybool ge get fhits 0 100 |
| 235 | fyend |
| 236 | end |
| 237 | note fyjcsr: the code matches of repo i (the lines fcsrepo shows) |
| 238 | fn fyjcsr i ulong to void |
| 239 | fgreparg i |
| 240 | st = fexec 10000000000 |
| 241 | if ne st 0 |
| 242 | ret |
| 243 | end |
| 244 | ln = get fxol 0 |
| 245 | p = ulong 0 |
| 246 | for it ulong 100000 |
| 247 | if ge p ln |
| 248 | break |
| 249 | end |
| 250 | if ge get fhits 0 100 |
| 251 | break |
| 252 | end |
| 253 | e = fleol p ln |
| 254 | fyjchit i p e |
| 255 | p = add e 1 |
| 256 | end |
| 257 | end |
| 258 | note fyjchit: one line "refs/heads/<b>:<path>:<line>:<text>" as fcshit reads it; text cut at 300 bytes |
| 259 | fn fyjchit i ulong p ulong e ulong to void |
| 260 | a = add add p 12 get fabl i |
| 261 | if ge a e |
| 262 | ret |
| 263 | end |
| 264 | c1 = fcolon a e |
| 265 | if ge c1 e |
| 266 | ret |
| 267 | end |
| 268 | c2 = fcolon add c1 1 e |
| 269 | if ge c2 e |
| 270 | ret |
| 271 | end |
| 272 | if gt get fhits 0 0 |
| 273 | fputc 44 |
| 274 | end |
| 275 | put fhits 0 add get fhits 0 1 |
| 276 | fput tfysc1 |
| 277 | fjtname i |
| 278 | fjstr |
| 279 | fput tfysc2 |
| 280 | fputid i |
| 281 | fput tfysc3 |
| 282 | fjfx a sub c1 a |
| 283 | fput tfysc4 |
| 284 | fjsize add c1 1 c2 |
| 285 | fput tfysc5 |
| 286 | m = hmin sub sub e c2 1 300 |
| 287 | fjfx add c2 1 m |
| 288 | fput tfysc6 |
| 289 | fjpath i |
| 290 | fput tfysc7 |
| 291 | fyjbr i |
| 292 | fputc 47 |
| 293 | fypct a sub c1 a |
| 294 | fput tfysc8 |
| 295 | fxput add c1 1 sub sub c2 c1 1 |
| 296 | fput tfysc9 |
| 297 | end |
| 298 | note fyjbr: the default branch of repo i, percent-encoded like a path |
| 299 | fn fyjbr i ulong to void |
| 300 | o = get fabo i |
| 301 | for k ulong get fabl i bound 64 |
| 302 | fypcc get fabr add o k |
| 303 | end |
| 304 | end |
| 305 | note ---- /<owner> (forge/user/1) for the forge owner: the page fprofile renders (its follow counts, star |
| 306 | note and favorite sums, repositories and starred repositories); every other profile view is social's |
| 307 | text tfypr1 ",\"did\":\"did:key:" |
| 308 | text tfypr2 "\",\"followers\":" |
| 309 | text tfypr3 ",\"following\":" |
| 310 | text tfypr4 ",\"tab\":\"stars\",\n\"repos\":[" |
| 311 | text tfypr5 "],\n\"stars_received\":" |
| 312 | text tfypr6 ",\"favorites_received\":" |
| 313 | text tfypr7 ",\n\"starred\":" |
| 314 | text tfypr8 ",\n\"users\":null}\n" |
| 315 | fn fyjprof to void |
| 316 | fyuhead tfyuser |
| 317 | fput tfypr1 |
| 318 | fput tfnid |
| 319 | fput tfypr2 |
| 320 | fnum sofolln |
| 321 | fput tfypr3 |
| 322 | fnum sofollg |
| 323 | fput tfypr4 |
| 324 | for i ulong get fan 0 bound FAMAX |
| 325 | if gt i 0 |
| 326 | fput tfjsep |
| 327 | end |
| 328 | fputc 123 |
| 329 | fjcore i |
| 330 | fputc 125 |
| 331 | end |
| 332 | fput tfypr5 |
| 333 | fnum fstarsum |
| 334 | fput tfypr6 |
| 335 | fnum ffavsum |
| 336 | fput tfypr7 |
| 337 | sojstars |
| 338 | fput tfypr8 |
| 339 | end |