interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / interplane-forge / server / json.om

642 lines · 11310 bytes Raw Blame History
1note forged server/json.om: the machine form of /explore (schema forge/explore/1) and of the repo
2note home (schema forge/repo/1), docs/API.md. A request gets JSON when its path ends in .json
3note (/explore.json, /<owner>/<repo>.json) or its Accept header names application/json and not
4note text/html (/, /explore, /<owner>/<repo>). Same data as the HTML page; keys are stable; ids are
5note RIDs (rad:...) and full commit shas; times are ISO-8601 UTC. HTML stays the default.
6global facc uchar 512
7global faccl ulong 1
8note fjalt: the HTML page offers its JSON form in <head>: 0 none, 1 explore, 2 the repo fridx
9global fjalt ulong 1
10note fjt: the bytes of one JSON string, as large as the response body (fob, 8 MiB): a string that fits
11note the response is written whole; a longer one cannot be sent, so fjtc marks the body (fobtr) and the
12note answer is the whole 500 of fovfl, never a string cut short inside a 200.
13global fjt uchar 8388608
14global fjtl ulong 1
15text tfjctype "application/json; charset=utf-8"
16text tfjaj "application/json"
17text tfjth "text/html"
18text tfjnull "null"
19text tfjrep "\\ufffd"
20text tfjbq "\\\""
21text tfjbb "\\\\"
22text tfju00 "\\u00"
23text tfjtrue "true"
24text tfjfals "false"
25note ---- Accept negotiation
26fn fjhasat t view uchar * s ulong to bool
27for k ulong len t bound 512
28if ne flow get facc add s k get t k
29ret false
30end
31end
32ret true
33end
34fn fjhas t view uchar * to bool
35n = get faccl 0
36m = len t
37if gt m n
38ret false
39end
40for s ulong add sub n m 1 bound 512
41if fjhasat t s
42ret true
43end
44end
45ret false
46end
47fn fjwant to bool
48if not fjhas tfjaj
49ret false
50end
51ret not fjhas tfjth
52end
53note ---- string building: fjt holds the bytes, fjstr writes them as one JSON string
54fn fjtb to void
55put fjtl 0 0
56end
57fn fjtc c uchar to void
58n = get fjtl 0
59if lt n len fjt
60put fjt n c
61put fjtl 0 add n 1
62else
63put fobtr 0 1
64end
65end
66fn fjhex v ulong to uchar
67if lt v 10
68ret uchar add v 48
69end
70ret uchar add v 87
71end
72fn fjasc c uchar to void
73if eq c 34
74fput tfjbq
75ret
76end
77if eq c 92
78fput tfjbb
79ret
80end
81if or or lt c 32 eq c 127 or or eq c 60 eq c 62 eq c 38
82fput tfju00
83fputc fjhex shr ulong c 4
84fputc fjhex and ulong c 15
85ret
86end
87fputc c
88end
89fn fjcont i ulong to bool
90if ge i get fjtl 0
91ret false
92end
93c = get fjt i
94ret and ge c 128 le c 191
95end
96fn fjul3 i ulong c uchar to ulong
97if not fjcont add i 1
98ret 0
99end
100if not fjcont add i 2
101ret 0
102end
103d = get fjt add i 1
104if and eq c 224 lt d 160
105ret 0
106end
107if and eq c 237 gt d 159
108ret 0
109end
110ret 3
111end
112fn fjul4 i ulong c uchar to ulong
113if not fjcont add i 1
114ret 0
115end
116if not fjcont add i 2
117ret 0
118end
119if not fjcont add i 3
120ret 0
121end
122d = get fjt add i 1
123if and eq c 240 lt d 144
124ret 0
125end
126if and eq c 244 gt d 143
127ret 0
128end
129ret 4
130end
131note fjulen: length of the well-formed UTF-8 sequence at fjt[i] (1..4), 0 when it is not well formed
132fn fjulen i ulong to ulong
133c = get fjt i
134if lt c 128
135ret 1
136end
137if and ge c 194 le c 223
138if fjcont add i 1
139ret 2
140end
141ret 0
142end
143if and ge c 224 le c 239
144ret fjul3 i c
145end
146if and ge c 240 le c 244
147ret fjul4 i c
148end
149ret 0
150end
151note fjstr: "..." with JSON escapes; ill-formed UTF-8 bytes become U+FFFD, so the body is valid JSON
152fn fjstr to void
153fputc 34
154n = get fjtl 0
155i = ulong 0
156for it ulong n bound 8388608
157if ge i n
158break
159end
160l = fjulen i
161if eq l 0
162fput tfjrep
163i = add i 1
164continue
165end
166if eq l 1
167fjasc get fjt i
168i = add i 1
169continue
170end
171for k ulong l bound 4
172fputc get fjt add i k
173end
174i = add i l
175end
176fputc 34
177end
178fn fjtxt t view uchar * to void
179fjtb
180for k ulong len t bound 4096
181fjtc get t k
182end
183fjstr
184end
185note fjfx: fxo[a, a+n) as a JSON string
186fn fjfx a ulong n ulong to void
187fjtb
188note the bound is the size of fxo: a file name or a path can be longer than 65,536 bytes, and bound
189note 65536 was trap 4 (exit 104) on one. A string over 8 MiB is the whole 500 (fjtc).
190for k ulong n bound 16777216
191fjtc get fxo add a k
192end
193fjstr
194end
195note ---- times: ISO-8601 UTC from an epoch (days to civil date, proleptic Gregorian)
196fn fj2 v ulong to void
197fputc uchar add div v 10 48
198fputc uchar add rem v 10 48
199end
200fn fj4 v ulong to void
201fj2 div v 100
202fj2 rem v 100
203end
204fn fjyoe doe ulong to ulong
205a = sub doe div doe 1460
206b = add a div doe 36524
207ret div sub b div doe 146096 365
208end
209fn fjdate dd ulong to void
210z = add dd 719468
211era = div z 146097
212doe = sub z mul era 146097
213yoe = fjyoe doe
214y = add yoe mul era 400
215doy = sub doe sub add mul 365 yoe div yoe 4 div yoe 100
216mp = div add mul 5 doy 2 153
217d = add sub doy div add mul 153 mp 2 5 1
218m = ulong 0
219if lt mp 10
220m = add mp 3
221else
222m = sub mp 9
223end
224if le m 2
225y = add y 1
226end
227fj4 y
228fputc 45
229fj2 m
230fputc 45
231fj2 d
232end
233note fjisob: YYYY-MM-DDTHH:MM:SSZ (no quotes); fjiso: the same as a JSON string, or null for 0
234fn fjisob t ulong to void
235s = rem t 86400
236fjdate div t 86400
237fputc 84
238fj2 div s 3600
239fputc 58
240fj2 div rem s 3600 60
241fputc 58
242fj2 rem s 60
243fputc 90
244end
245fn fjiso t ulong to void
246if eq t 0
247fput tfjnull
248ret
249end
250fputc 34
251fjisob t
252fputc 34
253end
254note fjday: YYYY-MM-DD (UTC) for the HTML pages
255fn fjday t ulong to void
256fjdate div t 86400
257end
258note ---- fields shared by explore items and the repo object
259text tfjo1 "\"owner\":\""
260text tfjo2 "\",\"name\":"
261text tfjo3 ",\"rid\":\""
262text tfjo4 "\",\"path\":\""
263text tfjo5 "\",\"json\":\""
264text tfjo6 ".json\",\"description\":"
265text tfjo7 ",\"default_branch\":\""
266text tfjo8 "\",\"stars\":"
267text tfjo9 ",\"last_commit\":"
268text tfjlc1 "{\"sha\":\""
269text tfjlc2 "\",\"time\":"
270text tfjlc3 ",\"subject\":"
271fn fjpath i ulong to void
272fputb
273fput tfowner
274fputc 47
275for k ulong get fanl i bound 128
276fputc frnbyte i k
277end
278end
279fn fjtdesc i ulong to void
280fjtb
281o = get fado i
282for k ulong get fadl i bound 4096
283fjtc get fadesc add o k
284end
285end
286fn fjtname i ulong to void
287fjtb
288for k ulong get fanl i bound 128
289fjtc frnbyte i k
290end
291end
292note fjsubj: the subject from falc "%cs %h %s" (after the second space)
293fn fjsubj i ulong to void
294fjtb
295o = get falco i
296n = get falcl i
297sp = ulong 0
298for k ulong n bound 1024
299c = get falc add o k
300if lt sp 2
301if eq c 32
302sp = add sp 1
303end
304continue
305end
306fjtc c
307end
308fjstr
309end
310fn fjlast i ulong to void
311if ne get falsl i 40
312fput tfjnull
313ret
314end
315fput tfjlc1
316o = mul i 40
317for k ulong 40
318fputc get falsh add o k
319end
320fput tfjlc2
321fjiso get falct i
322fput tfjlc3
323fjsubj i
324fputc 125
325end
326fn fjcore i ulong to void
327fput tfjo1
328fput tfowner
329fput tfjo2
330fjtname i
331fjstr
332fput tfjo3
333fputid i
334fput tfjo4
335fjpath i
336fput tfjo5
337fjpath i
338fput tfjo6
339if eq get fadl i 0
340fput tfjnull
341else
342fjtdesc i
343fjstr
344end
345fput tfjo7
346o = get fabo i
347for k ulong get fabl i bound 64
348fputc get fabr add o k
349end
350fput tfjo8
351fnum sostarn i
352fput tfjo9
353fjlast i
354end
355note ---- /explore.json (also / and /explore with Accept: application/json)
356text tfjex1 "{\"schema\":\"forge/explore/1\",\"base\":\""
357text tfjex2 "\",\"count\":"
358text tfjex3 ",\"repos\":[\n"
359text tfjex4 "\n]}\n"
360text tfjsep ",\n"
361fn fjstart to void
362put fkind 0 1
363put fobl 0 0
364fsetct tfjctype
365end
366fn fjexplore to void
367fjstart
368fput tfjex1
369fputb
370fput tfjex2
371fnum get fan 0
372fput tfjex3
373for i ulong get fan 0 bound FAMAX
374if gt i 0
375fput tfjsep
376end
377fputc 123
378fjcore i
379fputc 125
380end
381fput tfjex4
382end
383note ---- /<owner>/<repo>.json (also /<owner>/<repo> with Accept: application/json)
384text tfjr1 "{\"schema\":\"forge/repo/1\",\n"
385text tfjr2 ",\n\"favorites\":"
386text tfjar1 ",\n\"archives\":{\"zip\":\""
387text tfjarh "/archive/refs/heads/"
388text tfjar2 ".zip\",\"tar_gz\":\""
389text tfjar3 ".tar.gz\"}"
390text tfjr3 ",\n\"counts\":"
391text tfjr4 "\n}\n"
392fn fjrepo to void
393i = get fridx 0
394fjstart
395fput tfjr1
396fjcore i
397fput tfjr2
398fnum sofavn i
399fjclone i
400fput tfjar1
401fjarch i
402fput tfjar2
403fjarch i
404fput tfjar3
405fjfiles i
406fput tfjr3
407fzjson i
408fput tfjr4
409end
410fn fjarch i ulong to void
411fjpath i
412fput tfjarh
413fputbr i
414end
415note ---- top-level files of the default branch: git ls-tree -z -l <ref> (the browse home's list)
416text tfjlst "ls-tree"
417text tfjz "-z"
418text tfjl "-l"
419text tfjf1 ",\n\"files\":"
420text tfjf2 "[\n"
421text tfjf3 "\n]"
422text tfjf4 "{\"name\":"
423text tfjf5 ",\"type\":\""
424text tfjf6 "\",\"sha\":\""
425text tfjf7 "\",\"size\":"
426text tfjf8 ",\n\"files_truncated\":"
427text tfjrm1 ",\n\"readme\":"
428text tfjrm2 "{\"name\":"
429text tfjrm3 ",\"html\":\""
430text tfjblob "/blob/"
431text tfjrm4 "\",\"raw\":\""
432text tfjraw "/raw/"
433text tfjrm5 "\"}"
434text tfjrdm "readme"
435text tfjrdmd "readme.md"
436global fjrda ulong 1
437global fjrdn ulong 1
438global fjrdq ulong 1
439fn fjlsarg i ulong ns ulong to void
440fkgiti i
441fka tfjlst
442fka tfjz
443fka tfjl
444fkopen
445if eq ns 1
446fkt tfrns
447fkt tfnid
448fkt tfrnsh
449else
450fkt tfrh
451end
452o = get fabo i
453for k ulong get fabl i bound 64
454fkc get fabr add o k
455end
456end
457fn fjfiles i ulong to void
458put fjrdq 0 0
459fjlsarg i 0
460st = fexec 5000000000
461if ne st 0
462fjlsarg i 1
463st = fexec 5000000000
464end
465fput tfjf1
466if ne st 0
467fput tfjnull
468fput tfjrm1
469fput tfjnull
470ret
471end
472fput tfjf2
473ln = get fxol 0
474p = ulong 0
475n = ulong 0
476for it ulong 100000
477if ge p ln
478break
479end
480e = fjnul p ln
481note a listing cut by fxo ends in a name without its NUL: that piece is not an entry
482if and ge e ln ne get fxotr 0 0
483break
484end
485if lt n 1000
486if gt n 0
487fput tfjsep
488end
489fjent p e
490end
491n = add n 1
492p = add e 1
493end
494fput tfjf3
495fput tfjf8
496note truncated: more than the 1000 entries listed, or a listing longer than fxo (entries past it unknown)
497if or gt n 1000 ne get fxotr 0 0
498fput tfjtrue
499else
500fput tfjfals
501end
502fput tfjrm1
503fjrdme i
504end
505fn fjnul p ulong ln ulong to ulong
506j = p
507for it ulong sub ln p bound 16777216
508if eq get fxo j 0
509ret j
510end
511j = add j 1
512end
513ret ln
514end
515fn fjch a ulong e ulong c uchar to ulong
516j = a
517for it ulong sub e a bound 16777216
518if eq get fxo j c
519ret j
520end
521j = add j 1
522end
523ret e
524end
525note fjent: one ls-tree -l record "<mode> <type> <sha> <size>\t<name>" in fxo[p, e)
526fn fjent p ulong e ulong to void
527s1 = fjch p e 32
528s2 = fjch add s1 1 e 32
529s3 = fjch add s2 1 e 32
530tb = fjch add s3 1 e 9
531if ge tb e
532fput tfjnull
533ret
534end
535fput tfjf4
536fjfx add tb 1 sub sub e tb 1
537fput tfjf5
538fxput add s1 1 sub sub s2 s1 1
539fput tfjf6
540fxput add s2 1 sub sub s3 s2 1
541fput tfjf7
542fjsize add s3 1 tb
543fputc 125
544fjrdsee add s1 1 add tb 1 e
545end
546fn fjsize a ulong e ulong to void
547v = ulong 0
548d = ulong 0
549m = hmin sub e a 64
550for k ulong m bound 64
551c = get fxo add a k
552if fisdig c
553v = add mul v 10 ulong sub c 48
554d = add d 1
555end
556end
557if eq d 0
558fput tfjnull
559ret
560end
561fnum v
562end
563note fjrdsee: remember the README among the blobs (readme.md first, then any name starting readme)
564fn fjrdsee t ulong a ulong e ulong to void
565if ne get fxo t 98
566ret
567end
568n = sub e a
569q = fjrdkind a n
570if eq q 0
571ret
572end
573r = get fjrdq 0
574if and ne r 0 le r q
575ret
576end
577put fjrdq 0 q
578put fjrda 0 a
579put fjrdn 0 n
580end
581fn fjrdkind a ulong n ulong to ulong
582if lt n len tfjrdm
583ret 0
584end
585for k ulong len tfjrdm
586if ne flow get fxo add a k get tfjrdm k
587ret 0
588end
589end
590if ne n len tfjrdmd
591ret 2
592end
593for k ulong len tfjrdmd
594if ne flow get fxo add a k get tfjrdmd k
595ret 2
596end
597end
598ret 1
599end
600fn fjrdme i ulong to void
601if eq get fjrdq 0 0
602fput tfjnull
603ret
604end
605a = get fjrda 0
606n = get fjrdn 0
607fput tfjrm2
608fjfx a n
609fput tfjrm3
610fjpath i
611fput tfjblob
612fputbr i
613fputc 47
614fjpct a n
615fput tfjrm4
616fjpath i
617fput tfjraw
618fputbr i
619fputc 47
620fjpct a n
621fput tfjrm5
622end
623note fjpct: fxo[a, a+n) percent-encoded for a URL path (unreserved bytes kept)
624fn fjpct a ulong n ulong to void
625m = hmin n 4096
626for k ulong m bound 4096
627c = get fxo add a k
628if or fisaln c or or eq c 46 eq c 45 eq c 95
629fputc c
630else
631fputc 37
632fputc fjhexu shr ulong c 4
633fputc fjhexu and ulong c 15
634end
635end
636end
637fn fjhexu v ulong to uchar
638if lt v 10
639ret uchar add v 48
640end
641ret uchar add v 55
642end