tabby/clients/vim/node_scripts/tabby-agent.js

8 lines
8.5 KiB
JavaScript
Raw Normal View History

#!/bin/env node
var Q=Object.create;var j=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames;var Z=Object.getPrototypeOf,ee=Object.prototype.hasOwnProperty;var te=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of Y(e))!ee.call(t,s)&&s!==n&&j(t,s,{get:()=>e[s],enumerable:!(r=X(e,s))||r.enumerable});return t};var H=(t,e,n)=>(n=t!=null?Q(Z(t)):{},te(e||!t||!t.__esModule?j(n,"default",{value:t,enumerable:!0}):n,t));var $=(t,e,n)=>{if(!e.has(t))throw TypeError("Cannot "+n)};var a=(t,e,n)=>($(t,e,"read from private field"),n?n.call(t):e.get(t)),R=(t,e,n)=>{if(e.has(t))throw TypeError("Cannot add the same private member more than once");e instanceof WeakSet?e.add(t):e.set(t,n)},u=(t,e,n,r)=>($(t,e,"write to private field"),r?r.call(t,n):e.set(t,n),n);var J=H(require("axios")),M=require("events"),K=require("assert");function V(t){return new Promise(e=>setTimeout(e,t))}function W(t){return t.match(/.*(?:$|\r?\n)/g).filter(Boolean)}var T=class{constructor(e){this.config=e}};var B=H(require("axios")),U=H(require("form-data"));var C=class extends Error{constructor(n,r,s){super(s);this.name="ApiError",this.url=r.url,this.status=r.status,this.statusText=r.statusText,this.body=r.body,this.request=n}};var O=class extends Error{constructor(e){super(e),this.name="CancelError"}get isCancelled(){return!0}},d,f,m,y,A,v,E,g=class{constructor(e){R(this,d,void 0);R(this,f,void 0);R(this,m,void 0);R(this,y,void 0);R(this,A,void 0);R(this,v,void 0);R(this,E,void 0);u(this,d,!1),u(this,f,!1),u(this,m,!1),u(this,y,[]),u(this,A,new Promise((n,r)=>{u(this,v,n),u(this,E,r);let s=l=>{var p;a(this,d)||a(this,f)||a(this,m)||(u(this,d,!0),(p=a(this,v))==null||p.call(this,l))},o=l=>{var p;a(this,d)||a(this,f)||a(this,m)||(u(this,f,!0),(p=a(this,E))==null||p.call(this,l))},i=l=>{a(this,d)||a(this,f)||a(this,m)||a(this,y).push(l)};return Object.defineProperty(i,"isResolved",{get:()=>a(this,d)}),Object.defineProperty(i,"isRejected",{get:()=>a(this,f)}),Object.defineProperty(i,"isCancelled",{get:()=>a(this,m)}),e(s,o,i)}))}get[Symbol.toStringTag](){return"Cancellable Promise"}then(e,n){return a(this,A).then(e,n)}catch(e){return a(this,A).catch(e)}finally(e){return a(this,A).finally(e)}cancel(){var e;if(!(a(this,d)||a(this,f)||a(this,m))){if(u(this,m,!0),a(this,y).length)try{for(let n of a(this,y))n()}catch(n){console.warn("Cancellation threw an error",n);return}a(this,y).length=0,(e=a(this,E))==null||e.call(this,new O("Request aborted"))}}get isCancelled(){return a(this,m)}};d=new WeakMap,f=new WeakMap,m=new WeakMap,y=new WeakMap,A=new WeakMap,v=new WeakMap,E=new WeakMap;var F=t=>t!=null,q=t=>typeof t=="string",D=t=>q(t)&&t!=="",_=t=>typeof t=="object"&&typeof t.type=="string"&&typeof t.stream=="function"&&typeof t.arrayBuffer=="function"&&typeof t.constructor=="function"&&typeof t.constructor.name=="string"&&/^(Blob|File)$/.test(t.constructor.name)&&/^(Blob|File)$/.test(t[Symbol.toStringTag]),ne=t=>t instanceof U.default,re=t=>t>=200&&t<300,se=t=>{try{return btoa(t)}catch{return Buffer.from(t).toString("base64")}},oe=t=>{let e=[],n=(s,o)=>{e.push(`${encodeURIComponent(s)}=${encodeURIComponent(String(o))}`)},r=(s,o)=>{F(o)&&(Array.isArray(o)?o.forEach(i=>{r(s,i)}):typeof o=="object"?Object.entries(o).forEach(([i,l])=>{r(`${s}[${i}]`,l)}):n(s,o))};return Object.entries(t).forEach(([s,o])=>{r(s,o)}),e.length>0?`?${e.join("&")}`:""},ie=(t,e)=>{let n=t.ENCODE_PATH||encodeURI,r=e.url.replace("{api-version}",t.VERSION).replace(/{(.*?)}/g,(o,i)=>e.path?.hasOwnProperty(i)?n(String(e.path[i])):o),s=`${t.BASE}${r}`;return e.query?`${s}${oe(e.query)}`:s},ae=t=>{if(t.formData){let e=new U.default,n=(r,s)=>{q(s)||_(s)?e.append(r,s):e.append(r,JSON.stringify(s))};return Object.entries(t.formData).filter(([r,s])=>F(s)).forEach(([r,s])=>{Array.isArray(s)?s.forEach(o=>n(r,o)):n(r,s)}),e}},S=async(t,e)=>typeof e=="function"?e(t):e,pe=async(t,e,n)=>{let r=await S(e,t.TOKEN),s=await S(e,t.USERNAME),o=await S(e,t.PASSWORD),i=await S(e,t.HEADERS),l=typeof n?.getHeaders=="function"&&n?.getHeaders()||{},p=Object.entries({Accept:"applic
`)?this.buffer="":this.buffer=r.pop();for(let s of r){let o=null;try{o=JSON.parse(s)}catch(i){this.errLogger.write(JSON.stringify(i,Object.getOwnPropertyNames(i))+`
`);continue}this.handleRequest(o).then(i=>{this.sendResponse(i)})}}}async handleRequest(e){let n=[0,null];try{if(!this.agent)throw new Error(`Agent not bound.
`);n[0]=e[0];let r=e[1].func;if(r==="cancelRequest")n[1]=this.cancelRequest(e);else{let s=this.agent[r];if(!s)throw new Error(`Unknown function: ${r}`);let o=s.apply(this.agent,e[1].args);o instanceof g?(this.ongoingRequests[e[0]]=o,n[1]=await o,delete this.ongoingRequests[e[0]]):n[1]=o}}catch(r){this.errLogger.write(JSON.stringify(r,Object.getOwnPropertyNames(r))+`
`)}finally{return n}}cancelRequest(e){let n=this.ongoingRequests[e[1].args[0]];return n?(n.cancel(),!0):!1}sendResponse(e){this.outStream.write(JSON.stringify(e)+`
`)}bind(e){this.agent=e;for(let n of z)this.agent.on(n,r=>{this.sendResponse([0,r])})}listen(){this.inStream.on("data",this.handleInput.bind(this))}};var fe=new I,G=new w;G.bind(fe);G.listen();