@usebruno/converters
Version:
The converters package is responsible for converting collections from one format to a Bruno collection. It can be used as a standalone package or as a part of the Bruno framework.
3 lines (2 loc) • 2.52 kB
JavaScript
const{Worker:e}=require("node:worker_threads"),r=require("node:path"),t=require("node:os");class s{constructor(e,r){this.workers=[],this.idle=[],this.queue=[],this.scriptPath=e,this.size=r}initialize(){for(let r=0;r<this.size;r++){const t=new e(this.scriptPath);this.workers.push(t),this.idle.push(r)}}runTask(e){return new Promise(((r,t)=>{const s={data:e,resolve:r,reject:t};this.idle.length>0?this._runTaskOnWorker(this.idle.shift(),s):this.queue.push(s)}))}_runTaskOnWorker(e,r){const t=this.workers[e],s=n=>{t.removeListener("message",s),t.removeListener("error",i),this.idle.push(e),this.queue.length>0&&this._runTaskOnWorker(e,this.queue.shift()),r.resolve(n)},i=n=>{t.removeListener("message",s),t.removeListener("error",i),this.idle.push(e),this.queue.length>0&&this._runTaskOnWorker(e,this.queue.shift()),r.reject(n)};t.on("message",s),t.on("error",i),t.postMessage(r.data)}terminate(){for(const e of this.workers)e.terminate();this.workers=[],this.idle=[]}}function i([e,r]){let t=0;const{events:s}=r;return s&&Array.isArray(s)&&s.forEach((({script:e})=>{e&&e.exec&&(t+=function(e){return e?Array.isArray(e)?e.length:e.split("\n").length:0}(e.exec))})),{uid:e,entry:r,complexity:t||1}}const n=async e=>{const n=Array.from(e.entries()),o=Math.max(t.availableParallelism(),1);if(n.length<=50){const e=new s(r.join(__dirname,"./src/workers/scripts/translate-postman-scripts.js"),1);e.initialize();try{const r=new Map,t=await e.runTask({scripts:n});if(t.error)throw console.error("Error in script translation worker:",t.error),new Error(t.error);return t.forEach((([e,{request:t}])=>{r.set(e,{request:t})})),r}finally{e.terminate()}}const a=Math.min(o,4),l=function(e,r){const t=e.map(i);t.sort(((e,r)=>r.complexity-e.complexity));const s=Array.from({length:r},(()=>({entries:[],totalComplexity:0})));for(const{uid:e,entry:r,complexity:i}of t){const t=s.reduce(((e,r)=>r.totalComplexity<e.totalComplexity?r:e));t.entries.push({uid:e,entry:r}),t.totalComplexity+=i}return s.map((e=>e.entries.map((({uid:e,entry:r})=>[e,r])))).filter((e=>e.length>0))}(n,a),h=new Map,c=new s(r.join(__dirname,"./src/workers/scripts/translate-postman-scripts.js"),a);c.initialize();const u=l.map((e=>c.runTask({scripts:e}).then((e=>{e.forEach((([e,{request:r}])=>{h.set(e,{request:r})}))})).catch((e=>{throw console.error("Error in script translation worker:",e),new Error(e)}))));try{await Promise.allSettled(u)}finally{c.terminate()}return h};export{n as default};
//# sourceMappingURL=postman-translator-worker-fd766331.js.map