@dynatrace/runtime-simulator
Version:
The Dynatrace JavaScript runtime simulator.
18 lines (17 loc) • 3.34 kB
JavaScript
var O=Object.create;var l=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var E=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var R=(t,e)=>{for(var o in e)l(t,o,{get:e[o],enumerable:!0})},h=(t,e,o,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of x(e))!L.call(t,r)&&r!==o&&l(t,r,{get:()=>e[r],enumerable:!(i=S(e,r))||i.enumerable});return t};var z=(t,e,o)=>(o=t!=null?O(E(t)):{},h(e||!t||!t.__esModule?l(o,"default",{value:t,enumerable:!0}):o,t)),B=t=>h(l({},"__esModule",{value:!0}),t);var C={};R(C,{dispatch:()=>k,initialize:()=>P});module.exports=B(C);var w=require("chalk"),p=z(require("which")),y=require("child_process"),g=require("events"),T=require("net"),u=require("path"),a=require("v8");var f=new g.EventEmitter,s,v=0,m=0;async function P(t){m=1;let e=(process.env.NODE_OPTIONS??"")+" --experimental-vm-modules --no-warnings --enable-source-maps";return s=(0,y.spawn)((process.platform==="win32"?`"${p.sync("node",{nothrow:!0})}"`:p.sync("node",{nothrow:!0}))??process.execPath,[process.platform==="win32"?t?`"${t}"`:`"${(0,u.join)(__dirname,"server.js")}"`:t||(0,u.join)(__dirname,"server.js")],{env:{...process.env,NODE_OPTIONS:e,FORCE_COLOR:"1"},...process.platform==="win32"&&{shell:!0}}),s.stderr.on("data",o=>{console.error((0,w.yellow)("[runtime-simulator STDERR]"),o.toString())}),new Promise((o,i)=>{let r=n=>{n.toString().startsWith("Listening on port")&&(v=Number.parseInt(n.toString().substring(17).trim()),clearTimeout(c),s.stdout.removeListener("data",r),o())},c=setTimeout(()=>{s.stdout.removeListener("data",r),i(new Error("Failed to start the app function worker server: Timeout"))},8e3);s.stdout.on("data",r)}).then(()=>(m=2,s.stdout.on("data",o=>{console.log(o.toString().replace(/\n$/,""))}),f.listenerCount("")>0&&f.emit(""),s))}async function k(t){if(m===0)throw new Error("Failed to dispatch app function worker task: controller not initialized");m===1&&await new Promise(o=>{f.once("",()=>o())});let e=new T.Socket;return new Promise((o,i)=>{let r=setTimeout(()=>{i(new Error("Failed to connect to the app function worker server: Timeout"))},8e3);e.connect(v,"localhost",()=>{clearTimeout(r),o()})}).then(()=>new Promise((o,i)=>{let r=[];function c(n){r.push(n)}e.on("data",c),e.once("close",()=>{e.removeListener("data",c);let n=(0,a.deserialize)(Buffer.concat(r));Object.prototype.hasOwnProperty.call(n,"result")?o(n.result):i((0,a.deserialize)(n.error))});try{let n=(0,a.serialize)(t);e.write(Buffer.concat([Buffer.from(n.byteLength.toString()),n]),d=>{d&&i(d)})}catch(n){i(n)}})).finally(()=>{e.readable&&e.end()})}process.once("exit",()=>{s&&s.kill()});0&&(module.exports={dispatch,initialize});
/**
* @license
* Copyright 2024 Dynatrace LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//# sourceMappingURL=index.js.map