hyperscript.org
Version:
a small scripting language for the web
3 lines (2 loc) • 3.54 kB
JavaScript
var w=class s{errors=[];collectErrors(e){if(e||(e=new Set),e.has(this))return[];e.add(this);var t=[...this.errors];for(var o of Object.keys(this))for(var n of[this[o]].flat())n instanceof s&&t.push(...n.collectErrors(e));return t}sourceFor(){return this.programSource.substring(this.startToken.start,this.endToken.end)}lineFor(){return this.programSource.split(`
`)[this.startToken.line-1]}static parseEventArgs(e){var t=[];if(e.token(0).value==="("&&(e.token(1).value===")"||e.token(2).value===","||e.token(2).value===")")){e.matchOpToken("(");do t.push(e.requireTokenType("IDENTIFIER"));while(e.matchOpToken(","));e.requireOpToken(")")}return t}};var d=class extends w{isFeature=!0;constructor(){super(),this.constructor.keyword&&(this.type=this.constructor.keyword+"Feature")}install(e,t,o,n){}static parseErrorAndFinally(e){var t,o,n;return e.matchToken("catch")&&(t=e.requireTokenType("IDENTIFIER").value,o=e.requireElement("commandList"),e.ensureTerminated(o)),e.matchToken("finally")&&(n=e.requireElement("commandList"),e.ensureTerminated(n)),{errorHandler:o,errorSymbol:t,finallyHandler:n}}};function L(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(s){var e=Math.random()*16|0,t=s=="x"?e:e&3|8;return t.toString(16)})}function S(s){var e=s;if(e.startsWith("/")){var t=window.location.hostname+(window.location.port?":"+window.location.port:"");window.location.protocol==="https:"?e="wss://"+t+e:window.location.protocol==="http:"&&(e="ws://"+t+e)}return e}var F=["then","catch","length","toJSON"],f=class s extends d{static keyword="socket";constructor(e,t,o,n){super(),this.socketName=e,this.nameSpace=t,this.socketObject=o,this.messageHandler=n}install(e,t,o,n){this.runtime=n,n.assignToNamespace(e,this.nameSpace,this.socketName,this.socketObject)}static parse(e){if(!e.matchToken("socket"))return;var t=e.requireElement("dotOrColonPath"),o=t.evalStatically(),n=o.split("."),N=n.pop(),l={},y=e.parseURLOrExpression(),T=1e4;e.matchToken("with")&&(e.requireToken("timeout"),T=e.requireElement("expression").evalStatically());var p=!1,m=null;e.matchToken("on")&&(e.requireToken("message"),e.matchToken("as")&&(e.matchToken("JSON")||e.requireToken("json"),p=!0),m=e.requireElement("commandList"),e.ensureTerminated(m));var a=new WebSocket(S(y.evalStatically()));function x(u){return new Proxy({},{get:function(c,r){return F.includes(r)?null:r==="noTimeout"?x(-1):r==="timeout"?function(i){return x(parseInt(i))}:function(){for(var i=L(),k=[],g=0;g<arguments.length;g++)k.push(arguments[g]);var q={iid:i,function:r,args:k};a=a||new WebSocket(S(y.evalStatically())),a.send(JSON.stringify(q));var b=new Promise(function(j,I){l[i]={resolve:j,reject:I}});return u>=0&&setTimeout(function(){l[i]&&l[i].reject("Timed out"),delete l[i]},u),b}}})}var O=x(T),v={raw:a,dispatchEvent:function(u){var c=u.detail;delete c.sender,delete c._namedArgList_,a.send(JSON.stringify(Object.assign({type:u.type},c)))},rpc:O},h=new s(N,n,v,m);return a.onmessage=function(u){var c=u.data;try{var r=JSON.parse(c)}catch{}if(r&&r.iid&&(r.throw?l[r.iid].reject(r.throw):l[r.iid].resolve(r.return),delete l[r.iid]),m){var i=h.runtime.makeContext(v,h,v);if(p)if(r)i.locals.message=r,i.result=r;else throw new Error("Received non-JSON message from socket: "+c);else i.locals.message=c,i.result=c;m.execute(i)}},a.addEventListener("close",function(u){a=null}),m&&e.setParent(m,h),h}};function E(s){s.addFeature(f.keyword,f.parse.bind(f))}typeof self<"u"&&self._hyperscript&&self._hyperscript.use(E);export{f as SocketFeature,E as default};