-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBlik_2024_static.js
More file actions
158 lines (151 loc) · 6.91 KB
/
Blik_2024_static.js
File metadata and controls
158 lines (151 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {note,search,merge,prune,record,remember,compose,spill,buffer,when,collect,infer,combine,wait,drop,slip,lift,differ,crop,each,swap,pass,not,compound,functor,native,exit,tether,either,whether,describe,string,pattern,major,match,has,is,are,defined,flip,heritage} from "./Blik_2023_inference.js";
import {access,persist,purge,resolve,list,interpret,cookies,cookie,query,fetch,path,loader,delegate,location,window,listen,infrastructure} from "./Blik_2023_interface.js";
import {document,hypertext,css,throttle,error,capture,defer} from "./Blik_2023_fragment.js";
import {serialize,mime,proceduralize,sourcemap} from "./Blik_2023_meta.js";
import {random,extract} from "./Blik_2023_search.js";
export var encryption={code:undefined};
export var classified=[/.*\.git.*/];
export var classify=compose
(when(are(either(string,pattern)))
,each(whether(string,compose(crop(1),slip("path","resolve"),resolve.bind(import.meta.url)),crop(1)))
,spill,classified.push.bind(classified)
);
export var published=[];
export var publish=compose
(when(are(either(string,pattern)))
,each(whether(string,compose(crop(1),slip("path","resolve"),resolve.bind(import.meta.url)),crop(1)))
,spill,published.push.bind(published)
);
export async function permit(name,list,inclusive)
{let path=[await resolve.bind(import.meta.url)("path","resolve",name),name.endsWith("/")?"/":""].join("");
let includes=list.some(term=>string(term)?path.startsWith(term):term.test(path));
return (inclusive?includes:!includes)||exit(Error(inclusive?"Unauthorized":"Classified"));
};
let filesystem=compose
(drop(1),path,infer("split","/"),"reverse"
,infer("reduce",(route,file,index,path)=>(
{[file==="get"?"files":file]:compose(combine
(compose(swap("path","resolve",".",path.slice(index,path.at(-1)==="files"?-1:undefined).reverse().join("/")),resolve.bind(import.meta.url)
,pass(permit,classified),access)
,compose(drop(1),"url",query,either("format",swap("binary")))
),lift,whether(infer("isDirectory")
,index?swap(route):compose("path",true,classified,list)
,access))
}),{})
);
let routes=compose
(combine(crop(1),compose(drop(1),path)),flip,whether(is("get")
,compose(drop(1),tether(prune,([field,term])=>functor(term)
?/[()]/.test(term.name)?term.name:serialize(term,null)
:!native(term)?String(term):term))
,swap({}))
);
export default
{infrastructure(){return delegate.call(loader,"infrastructure");}
,sources(){return infrastructure();}
,get:compose(combine(filesystem,routes),merge)
,put:compose
(drop(1),pass(buffer(combine
(compose(path,slip("path","resolve","./"),resolve.bind(import.meta.url),published,true,permit)
,compose(drop(1),wait(0),JSON.parse,Object.keys,"length",when(major(0)))
),compose(drop(1),infer(authorize,"ranger"))))
,combine
(compose(path,slip("path","resolve","./"),resolve.bind(import.meta.url),pass(permit,classified))
,compose(drop(1),buffer(JSON.parse,compose(drop(1,2),"base64",Buffer.from,"toString")))
,compose("url",query,buffer(differ("override"),swap(undefined)),is(true))
)
,combine
(crop(1)
,compose(combine(buffer(compose(crop(1),infer(access,true),buffer(JSON.parse,drop(1))),swap({})),drop(1)),merge)
)
,true,access,true,combine
(compose(access,buffer(JSON.parse,drop(1)))
,compose(crop(1),slip("path","relative","./"),resolve.bind(import.meta.url),"/","split")
),record
),interface:compose
(crop(1),"get",routes=>(
{pre:{"#text":JSON.stringify(routes,null,2)}
,style:{body:{background:"black",color:"white"}}
}),"interface","svg",[],"./style",hypertext,document,spill,lift,crop(1)
),style:compose
(drop(),{"@font-face":{"font-family":"averia",src:"url(/Sayers_2011_averia.ttf)"}}
,css,["body"],record,{type:"css"},merge
),feed()
{let body=serialize({imports:{"./Blik_2023_search.js":["","merge"]},exports:{default:{}}},"module");
return {body,type:mime("js")};
},svg:compose
(drop(),{svg:{viewBox:"0 0 1 1",width:"10px",height:"10px",circle:{cx:"0.5",cy:"0.5",r:"0.3"}}},document
),namespace()
{return compose.call
(this,when(modular),tether(prune,([field,term],path,trace)=>
trace.includes(term)?path:functor(term)
?"data:text/javascript;"+serialize(term,field)
:!native(term)?String(term):term)
);
},module(request,body,response,route)
{let file=route.join("/");
let term=path(request).replace(file+"/module","");
let {origin}=new URL("http"+(request.client.encrypted?"s":"")+"://"+request.headers.host);
return compose
(...simple(this)?["module",serialize]:["toString"],term?compose
(origin+"/"+file,buffer(interpret
,compose(drop(2),origin,location,"replace",resolve.bind(import.meta.url))),either("namespace",crop(1))
):compose("//# sourceMappingURL=./sourcemap",collect,"\n","join",["body"],record
,{type:mime("js"),headers:["X-",""].map(field=>(
{[field+"SourceMap"]:"./sourcemap"})).reduce(merge)
},merge)
)(this);
},sourcemap
,error
};
export function persistence(resource)
{classify(resource);
return (
{get:async function(request)
{let route=request?.url?.split("/")||request||[];
let create=whether(compose("code",is("ENOENT")),compose(drop(1),"{}",true,access,"object",access),exit);
let records=await buffer(compose("object",access),create)(resource);
records=prune.call(records,([field,value])=>request&&["signature","code"].includes(field)?undefined:value,0,2);
return prune.call(records,([field,value],path)=>path.at(-1)==="pub"&&route.length>4
?compose(swap(field),pass(permit,classified),"binary",access):value);
},put:async function(request,body)
{let {2:name}=request.url.split("/");
let {signature}=cookies(request.headers.cookie||"");
body=body?await prune.call(JSON.parse(body)
,([field,value])=>encryption[field]?.(value)||value):{};
let credentials=extract.call(body,Object.keys(encryption));
let anonymous=!signature&&!Object.keys(credentials).length;
if(anonymous)
exit(Error("missing "+Object.keys(credentials)+" or signature for "+name));
let records=await this.get();
let record=search.call(records,name);
let mismatch=record&&Object.entries(credentials).find(([field,value])=>value!==record[field])?.[0];
if(mismatch)
exit(Error(mismatch+" not authorized."));
let unauthorised=!signature||signature!==record.signature;
if(!body.code&&unauthorised)
exit(Error("invalid signature."));
let put=Date.now();
let expiry=1000*60*60*6;
let expired=put-record?.put>expiry;
signature=signature&&!expired?signature:random(20);
record=[record,body,{signature,put}].reduce(merge);
merge(records,record,name);
await access(resource,JSON.stringify(records),true);
record=prune.call(record,([field,value])=>
["signature","code"].includes(field)?undefined:value,0,1);
return merge(records
,{cookie:{signature,path:"/",expires:new Date(record.put+expiry).toUTCString(),httponly:true,samesite:true}
,body:record
},name);
}});
};
export async function authorize({headers:{cookie}},authority)
{when(defined)(authority);
let {author:name}=cookies(cookie||"");
if(!name)exit(Error("unauthorized"));
let author=await compose(fetch,wait(3000),"json")("/author/"+name,{method:"put",headers:{cookie}});
if(author.rank!==authority)
exit(Error("unauthorised"));
};
export var peer={};