@enfyra/kernel 1.1.24 → 1.1.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/execution/worker.js +116 -14
- package/dist/index.js +1 -1
- package/dist/package-metadata.json +1 -1
- package/package.json +1 -1
package/dist/execution/worker.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var D=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports);var B=D((Fe,K)=>{"use strict";var{fork:ce}=require("child_process"),le=require("path");function ue({workerDir:e,activeTaskContexts:t}){let s=new Map,r=new Map,n=new Map,o=null,c=0;function g(f){return Math.max(1,Math.trunc(Number(f)||3e4))}function u(f){for(let a of r.values())clearTimeout(a.timer),a.reject(f);r.clear()}function h(f){let a=typeof f=="string"?JSON.parse(f):f;if(a?.error)throw new Error(a.error);return a?.value}async function w(f){let a=t.get(f.taskId);if(!a)throw new Error(`Package callback context not found: ${f.taskId}`);let i=JSON.stringify(f.callbackId),d=JSON.stringify(f.argsJson||"[]"),_=await a.evalClosure(`return __invokeRuntimeCallback(${i}, ${d});`,[],{arguments:{copy:!0},result:{promise:!0,copy:!0}});return h(_)}function y(){if(o&&o.connected)return o;let f=le.join(e,"package-runtime.child.js"),a=ce(f,[],{stdio:["ignore","ignore","ignore","ipc"]});return o=a,a.on("message",i=>{if(!i||!i.id)return;if(i.type==="pkgStreamChunk"){let _=n.get(i.id);if(!_)return;_.onChunk(Buffer.from(i.chunk||"","base64")).then(()=>{try{a.send({type:"pkgStreamAck",ackId:i.ackId})}catch{}}).catch(v=>{try{a.send({type:"pkgStreamError",ackId:i.ackId,error:{message:v.message,stack:v.stack}})}catch{}});return}if(i.type==="pkgCallbackCall"){w(i).then(_=>{try{a.send({type:"pkgCallbackResult",id:i.id,value:_})}catch{}}).catch(_=>{try{a.send({type:"pkgCallbackError",id:i.id,error:{message:_.message,stack:_.stack}})}catch{}});return}let d=r.get(i.id);if(d)if(clearTimeout(d.timer),r.delete(i.id),n.delete(i.id),i.ok)d.resolve(i.value);else{let _=new Error(i.error?.message||"Package runtime call failed");_.stack=i.error?.stack||_.stack,d.reject(_)}}),a.on("exit",(i,d)=>{o===a&&(o=null),u(new Error(`Package runtime exited${d?` with signal ${d}`:` with code ${i}`}`))}),a.on("error",i=>{o===a&&(o=null),u(i)}),a}function l(f){return new Promise((a,i)=>{let d=y(),_=`pkg_${++c}`,v=g(f.timeoutMs),S=setTimeout(()=>{r.delete(_),n.delete(_);try{d.kill()}catch{}i(new Error(`Package runtime call timed out after ${v}ms`))},v);r.set(_,{resolve:a,reject:i,timer:S});try{d.send({...f,id:_},x=>{x&&(clearTimeout(S),r.delete(_),i(x))})}catch(x){clearTimeout(S),r.delete(_),i(x)}})}function m(f,a,i,d=[],_=3e4){return new Promise((v,S)=>{let x=y(),b=`pkg_${++c}`,F=g(_),H=setTimeout(()=>{r.delete(b),n.delete(b);try{x.kill()}catch{}S(new Error(`Package runtime stream timed out after ${F}ms`))},F);r.set(b,{resolve:v,reject:S,timer:H}),n.set(b,{onChunk:i});try{x.send({op:"streamHandle",taskId:f,handleId:a,path:d,id:b},U=>{U&&(clearTimeout(H),r.delete(b),n.delete(b),S(U))})}catch(U){clearTimeout(H),r.delete(b),n.delete(b),S(U)}})}function P(f){try{if(!o||!o.connected)return;o.send({...f,id:`pkg_${++c}`,fire:!0})}catch{}}async function k(f,a){let i=s.get(f);if(!i)throw new Error("Package context is not available");if(a.handleId)return l({op:a.kind==="get"?"handleGet":"handleCall",taskId:f,handleId:a.handleId,path:a.path||[],argsJson:a.argsJson||"[]",timeoutMs:a.timeoutMs});let d=i.get(a.packageName);if(!d)throw new Error(`Package "${a.packageName}" is not available`);return l({op:a.kind==="construct"?"construct":"call",taskId:f,packageName:a.packageName,package:{name:d.name,fileUrl:d.fileUrl},path:a.path||[],argsJson:a.argsJson||"[]",timeoutMs:a.timeoutMs})}function N(f,a){s.set(f,a)}function j(f){s.delete(f),P({op:"releaseTask",taskId:f})}function p(){if(o){try{o.kill()}catch{}o=null}}return{cleanupTaskPackages:j,executePackageRuntimeCall:k,setTaskPackages:N,streamPackageHandle:m,shutdownPackageRuntime:p}}K.exports={createPackageRuntimeBridge:ue}});var V=D((Ue,q)=>{"use strict";var fe=`
|
|
2
2
|
"use strict";
|
|
3
3
|
let $ctx = {};
|
|
4
4
|
let $pkgs = {};
|
|
@@ -7,6 +7,7 @@ let __taskDeadlineMs = 0;
|
|
|
7
7
|
let __logs = [];
|
|
8
8
|
let __runtimeCallbackCounter = 0;
|
|
9
9
|
let __runtimeCallbacks = new Map();
|
|
10
|
+
let __pendingRuntimeTasks = [];
|
|
10
11
|
|
|
11
12
|
const __applyOpts = { result: { promise: true, copy: true } };
|
|
12
13
|
|
|
@@ -98,6 +99,34 @@ function __encodePkgCallPayload(payload) {
|
|
|
98
99
|
return JSON.stringify({ ...payload, timeoutMs: __remainingTaskTimeoutMs() });
|
|
99
100
|
}
|
|
100
101
|
|
|
102
|
+
function __bytesToBase64(bytes) {
|
|
103
|
+
const input = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes);
|
|
104
|
+
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
105
|
+
let out = '';
|
|
106
|
+
let i = 0;
|
|
107
|
+
for (; i + 2 < input.length; i += 3) {
|
|
108
|
+
const n = (input[i] << 16) | (input[i + 1] << 8) | input[i + 2];
|
|
109
|
+
out += chars[(n >> 18) & 63] + chars[(n >> 12) & 63] + chars[(n >> 6) & 63] + chars[n & 63];
|
|
110
|
+
}
|
|
111
|
+
if (i < input.length) {
|
|
112
|
+
const a = input[i];
|
|
113
|
+
const b = i + 1 < input.length ? input[i + 1] : 0;
|
|
114
|
+
const n = (a << 16) | (b << 8);
|
|
115
|
+
out += chars[(n >> 18) & 63] + chars[(n >> 12) & 63] + (i + 1 < input.length ? chars[(n >> 6) & 63] : '=') + '=';
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function __streamChunkToBytes(chunk) {
|
|
121
|
+
if (chunk === undefined || chunk === null) return new Uint8Array();
|
|
122
|
+
if (typeof chunk === 'string') return new TextEncoder().encode(chunk);
|
|
123
|
+
if (chunk instanceof ArrayBuffer) return new Uint8Array(chunk);
|
|
124
|
+
if (ArrayBuffer.isView(chunk)) {
|
|
125
|
+
return new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength);
|
|
126
|
+
}
|
|
127
|
+
return new TextEncoder().encode(String(chunk));
|
|
128
|
+
}
|
|
129
|
+
|
|
101
130
|
async function __encodeRuntimeArg(value, seen) {
|
|
102
131
|
if (typeof value === 'function') {
|
|
103
132
|
if (value.__pkgHandlePromise) {
|
|
@@ -196,6 +225,41 @@ async function __stringifyRuntimeArgs(args) {
|
|
|
196
225
|
return JSON.stringify(await Promise.all(args.map((arg) => __encodeRuntimeArg(arg))));
|
|
197
226
|
}
|
|
198
227
|
|
|
228
|
+
async function __streamResponseFromLocalReadable(stream, options) {
|
|
229
|
+
await __call('resCall', JSON.stringify({
|
|
230
|
+
method: 'streamStart',
|
|
231
|
+
argsJson: JSON.stringify([null, options || {}]),
|
|
232
|
+
}));
|
|
233
|
+
try {
|
|
234
|
+
for await (const chunk of stream) {
|
|
235
|
+
await __call('resCall', JSON.stringify({
|
|
236
|
+
method: 'streamChunk',
|
|
237
|
+
chunk: __bytesToBase64(__streamChunkToBytes(chunk)),
|
|
238
|
+
}));
|
|
239
|
+
}
|
|
240
|
+
await __call('resCall', JSON.stringify({ method: 'streamEnd' }));
|
|
241
|
+
} catch (error) {
|
|
242
|
+
try {
|
|
243
|
+
await __call('resCall', JSON.stringify({
|
|
244
|
+
method: 'streamError',
|
|
245
|
+
error: {
|
|
246
|
+
message: error && error.message ? error.message : String(error),
|
|
247
|
+
},
|
|
248
|
+
}));
|
|
249
|
+
} catch {}
|
|
250
|
+
throw error;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async function __drainPendingRuntimeTasks() {
|
|
255
|
+
while (__pendingRuntimeTasks.length > 0) {
|
|
256
|
+
const tasks = __pendingRuntimeTasks.splice(0, __pendingRuntimeTasks.length);
|
|
257
|
+
const results = await Promise.allSettled(tasks);
|
|
258
|
+
const failed = results.find((result) => result.status === 'rejected');
|
|
259
|
+
if (failed) throw failed.reason;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
199
263
|
async function __invokeRuntimeCallback(id, argsJson) {
|
|
200
264
|
const fn = __runtimeCallbacks.get(id);
|
|
201
265
|
if (typeof fn !== 'function') {
|
|
@@ -297,6 +361,7 @@ async function __extractCtxChanges() {
|
|
|
297
361
|
}
|
|
298
362
|
|
|
299
363
|
async function __extractResult(result, isAbsent) {
|
|
364
|
+
await __drainPendingRuntimeTasks();
|
|
300
365
|
const valueAbsent = isAbsent === true;
|
|
301
366
|
const out = {
|
|
302
367
|
valueAbsent,
|
|
@@ -615,7 +680,35 @@ function __createPkgProxy(packageName, path) {
|
|
|
615
680
|
});
|
|
616
681
|
}
|
|
617
682
|
|
|
683
|
+
function __createReadableFrom(input) {
|
|
684
|
+
const stream = {
|
|
685
|
+
__enfyraLocalReadable: true,
|
|
686
|
+
async *[Symbol.asyncIterator]() {
|
|
687
|
+
if (input && typeof input[Symbol.asyncIterator] === 'function') {
|
|
688
|
+
for await (const chunk of input) yield chunk;
|
|
689
|
+
return;
|
|
690
|
+
}
|
|
691
|
+
if (input && typeof input[Symbol.iterator] === 'function' && typeof input !== 'string') {
|
|
692
|
+
for (const chunk of input) yield chunk;
|
|
693
|
+
return;
|
|
694
|
+
}
|
|
695
|
+
yield input;
|
|
696
|
+
},
|
|
697
|
+
};
|
|
698
|
+
return stream;
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
const __builtinModules = {
|
|
702
|
+
stream: {
|
|
703
|
+
Readable: {
|
|
704
|
+
from: __createReadableFrom,
|
|
705
|
+
},
|
|
706
|
+
},
|
|
707
|
+
};
|
|
708
|
+
__builtinModules['node:stream'] = __builtinModules.stream;
|
|
709
|
+
|
|
618
710
|
const require = (name) => {
|
|
711
|
+
if (__builtinModules[name] !== undefined) return __builtinModules[name];
|
|
619
712
|
if ($pkgs[name] === undefined) throw new Error('Module "' + name + '" is not available. Install it via Settings \\u2192 Packages');
|
|
620
713
|
return $pkgs[name];
|
|
621
714
|
};
|
|
@@ -635,6 +728,7 @@ function __resetTask(snapshot, taskId, timeoutMs) {
|
|
|
635
728
|
__logs = [];
|
|
636
729
|
__runtimeCallbackCounter = 0;
|
|
637
730
|
__runtimeCallbacks = new Map();
|
|
731
|
+
__pendingRuntimeTasks = [];
|
|
638
732
|
%%PKG_SETUP%%
|
|
639
733
|
$ctx.$pkgs = $pkgs;
|
|
640
734
|
$ctx.$share = $ctx.$share || {};
|
|
@@ -675,8 +769,15 @@ function __resetTask(snapshot, taskId, timeoutMs) {
|
|
|
675
769
|
__call('storageCall', JSON.stringify({ method: String(method), argsJson: await __stringifyRuntimeArgs(args) }))
|
|
676
770
|
});
|
|
677
771
|
$ctx.$res = new Proxy({}, {
|
|
678
|
-
get: (_, method) => async (...args) =>
|
|
679
|
-
|
|
772
|
+
get: (_, method) => async (...args) => {
|
|
773
|
+
const methodName = String(method);
|
|
774
|
+
if (methodName === 'stream' && args[0]?.__enfyraLocalReadable) {
|
|
775
|
+
const task = __streamResponseFromLocalReadable(args[0], args[1]);
|
|
776
|
+
__pendingRuntimeTasks.push(task);
|
|
777
|
+
return task;
|
|
778
|
+
}
|
|
779
|
+
return __call('resCall', JSON.stringify({ method: methodName, argsJson: await __stringifyRuntimeArgs(args) }));
|
|
780
|
+
}
|
|
680
781
|
});
|
|
681
782
|
$ctx.$trigger = async (...args) =>
|
|
682
783
|
__call('dispatchCall', JSON.stringify({ method: 'trigger', argsJson: JSON.stringify(args) }));
|
|
@@ -765,32 +866,33 @@ function __cleanupTask() {
|
|
|
765
866
|
__logs = [];
|
|
766
867
|
__runtimeCallbackCounter = 0;
|
|
767
868
|
__runtimeCallbacks = new Map();
|
|
869
|
+
__pendingRuntimeTasks = [];
|
|
768
870
|
return true;
|
|
769
871
|
}
|
|
770
|
-
`;function de(e){return fe.replace("%%PKG_SETUP%%",e)}
|
|
771
|
-
`),
|
|
772
|
-
`);for(let
|
|
773
|
-
`)}function
|
|
872
|
+
`;function de(e){return fe.replace("%%PKG_SETUP%%",e)}q.exports={buildSetupCode:de}});var{parentPort:R}=require("worker_threads"),W=require("fs"),{fileURLToPath:_e}=require("url"),{createPackageRuntimeBridge:pe}=B(),{buildSetupCode:ge}=V(),I;try{I=require("isolated-vm")}catch(e){R.postMessage({type:"error",message:"isolated-vm not available: "+e.message}),process.exit(1)}var E=new Map,Q=0,J=new Map,O=pe({workerDir:__dirname,activeTaskContexts:J}),he=500,ye=5e3,me=32,z=new WeakMap,Y=new WeakMap,ee=new WeakMap,T={created:0,reused:0,released:0,evicted:0,scrubFailed:0},C=null,ke=0,G=0,X=0;async function we(){let e=Date.now();if(e-G<ye)return X;if(G=e,!C)return 0;let t=0;for(let s of C){let r=s?.isolate;if(!(!r||r.isDisposed))try{let n=await r.getHeapStatistics(),o=n.heap_size_limit||1,c=n.used_heap_size/o;c>t&&(t=c)}catch{}}return X=t,t}async function A(e){e.heapRatio=await we(),e.contextStats=ve(),R.postMessage(e)}function ve(){let e=0;if(C)for(let t of C)e+=t?.idleContexts?.length||0;return{...T,idle:e}}function te(e,t){if(!C){let n=Math.max(1,Math.trunc(Number(t)||8));C=new Array(n).fill(null)}let s=ke++%C.length,r=C[s];return(!r||!r.isolate||r.isolate.isDisposed)&&(r={isolate:new I.Isolate({memoryLimit:e}),idleContexts:[]},C[s]=r),r}async function $(e,t,s){let r=z.get(e);r||(r=new Map,z.set(e,r));let n=r.get(t);if(n)return n;let o=await e.compileScript(t,{filename:s});if(r.set(t,o),r.size>he){let c=r.keys().next().value;r.delete(c)}return o}R.on("message",async e=>{if(e.type==="execute")Oe(e).catch(async t=>{await A({type:"result",id:e.id,success:!1,error:{message:t.message,stack:t.stack}})});else if(e.type==="executeBatch")Ee(e).catch(async t=>{await A({type:"result",id:e.id,success:!1,error:{message:t.message,stack:t.stack}})});else if(e.type==="invokeRuntimeCallback")Pe(e).catch(t=>{R.postMessage({type:"runtimeCallbackResult",callId:e.callId,error:t?.message||String(t)})});else if(e.type==="callResult"){let t=E.get(e.callId);t&&(E.delete(e.callId),t.resolve(e.result))}else if(e.type==="callError"){let t=E.get(e.callId);t&&(E.delete(e.callId),t.reject(new Error(e.error)))}});async function Pe(e){let t=J.get(e.id);if(!t){R.postMessage({type:"runtimeCallbackResult",callId:e.callId,error:`Runtime callback context not found: ${e.id}`});return}let s=await t.evalClosure(`return __invokeRuntimeCallback(${JSON.stringify(e.callbackId)}, ${JSON.stringify(e.argsJson||"[]")});`,[],{arguments:{copy:!0},result:{promise:!0,copy:!0}}),r=JSON.parse(s);R.postMessage({type:"runtimeCallbackResult",callId:e.callId,...r})}function M(e,t,s){return new Promise((r,n)=>{let o=`cb_${++Q}`;E.set(o,{resolve:r,reject:n}),R.postMessage({type:t,id:e,callId:o,...s})})}function be(e,t,s){R.postMessage({type:t,id:e,callId:`f_${++Q}`,...s})}async function Se(e,t){if(t?.method!=="stream")return M(e,"resCall",t);let s=JSON.parse(t.argsJson||"[]"),r=s[0],n=s[1]||{},o=r?.__pkgHandleArg;if(!o)return M(e,"resCall",t);await M(e,"resCall",{method:"streamStart",argsJson:JSON.stringify([null,n])});try{await O.streamPackageHandle(e,o,c=>M(e,"resCall",{method:"streamChunk",chunk:Buffer.from(c).toString("base64")})),await M(e,"resCall",{method:"streamEnd"})}catch(c){try{await M(e,"resCall",{method:"streamError",error:{message:c.message,stack:c.stack}})}catch{}throw c}return JSON.stringify({__e:"u"})}function xe(e){if(typeof e.sourceCode=="string"&&e.sourceCode.length>0)return e.sourceCode;let t=e.filePath;return!t&&typeof e.fileUrl=="string"&&e.fileUrl.startsWith("file://")&&(t=_e(e.fileUrl)),!t||!W.existsSync(t)?null:W.readFileSync(t,"utf8")}async function Ce(e,t,s){try{let r=xe(s);if(!r)return null;let n=await e.compileModule(r,{filename:`pkg:${s.safeName}`});await n.instantiate(t,c=>{throw new Error(`Unresolved import in package ${s.safeName}: ${c}`)}),await n.evaluate({timeout:1e4});let o=await n.namespace.get("default");return o!==void 0&&o.typeof!=="undefined"?o.derefInto({release:!1}):n.namespace.derefInto({release:!1})}catch{return null}}function re(e,t){let s=n=>{let o=2166136261;for(let c=0;c<n.length;c++)o^=n.charCodeAt(c),o=Math.imul(o,16777619);return(o>>>0).toString(36)},r=new Set(t||[]);return e.map(n=>{let o=n.cacheKey||`${n.filePath||""}:${n.fileUrl||""}:${n.mtimeMs||""}:${n.size||""}:${s(n.sourceCode||"")}`,c=r.has(n.name)?"isolate":"proxy";return`${n.name}:${n.version||""}:${o}:${c}`}).join("|")}function ne(e,t){let s=new Set(t.map(u=>u.name)),r=new Set,n=String(e||""),o=/\b(?:\$ctx\.)?\$pkgs\s*\[\s*(['"`])([^'"`]+)\1\s*\]/g,c=/\brequire\s*\(\s*(['"`])([^'"`]+)\1\s*\)/g,g=/\b(?:\$ctx\.)?\$pkgs\.([A-Za-z_$][\w$]*)/g;for(let u of[o,c])for(let h of n.matchAll(u)){let w=h[2];s.has(w)&&r.add(w)}for(let u of n.matchAll(g)){let h=u[1];s.has(h)&&r.add(h)}return r}async function $e(e,t,s,r){let n=await e.createContext(),o=n.global;await o.set("global",o.derefInto());let c=[],g=[],u=new Map;for(let l of s){u.set(l.name,l);let m=r.has(l.name)?await Ce(e,n,l):null;m!==null?(await o.set(`__pkg_${l.safeName}`,m),c.push(l)):g.push(l)}O.setTaskPackages(t,u),await o.set("__callRef",new I.Reference(async(l,m,P)=>{let k=JSON.parse(P);return m==="pkgCall"?JSON.stringify(await O.executePackageRuntimeCall(l,k)):m==="resCall"?Se(l,k):M(l,m,k)})),await o.set("__fireRef",new I.Reference((l,m,P)=>{let k=JSON.parse(P);be(l,m,k)}));let h=c.map(l=>`$pkgs[${JSON.stringify(l.name)}] = __pkg_${l.safeName};`).concat(g.map(l=>`$pkgs[${JSON.stringify(l.name)}] = __createPkgProxy(${JSON.stringify(l.name)}, []);`)).join(`
|
|
873
|
+
`),w=ge(h);return await(await $(e,w,"setup.js")).run(n,{timeout:5e3}),Y.set(n,re(s,r)),ee.set(n,u),n}async function ae(e,t,s,r,n,o){let c=e.isolate,g=re(s,n),u=null;for(let y=e.idleContexts.length-1;y>=0;y--){let l=e.idleContexts[y];if(e.idleContexts.splice(y,1),Y.get(l)===g){u=l,T.reused++;break}try{l.release()}catch{}}u||(u=await $e(c,t,s,n),T.created++),O.setTaskPackages(t,ee.get(u)||new Map);let h=u.global;return await h.set("__snapshot",new I.ExternalCopy(r).copyInto()),await h.set("__nextTaskId",t),await h.set("__nextTaskTimeoutMs",o),await(await $(c,"__resetTask(__snapshot, __nextTaskId, __nextTaskTimeoutMs);","reset-task.js")).run(u,{timeout:5e3}),u}async function se(e,t){if(!(!t||e.isolate.isDisposed)){try{await(await $(e.isolate,"__cleanupTask();","cleanup-task.js")).run(t,{timeout:5e3})}catch{T.scrubFailed++;try{t.release()}catch{}return}if(e.idleContexts.length>=me){try{t.release()}catch{}T.evicted++;return}e.idleContexts.push(t),T.released++}}function Re(e){try{let t=JSON.parse(e.message);if(t&&t.__userThrow)return{...t,message:t.message??e.message}}catch{}return null}var Me=3,Ae=["setup.js","extract.js","extract-error-ctx.js","set-status.js","populate-error.js"];function Ne(e){if(!e)return null;let t=e.split(`
|
|
874
|
+
`);for(let s of t){let r=s.match(/at\s+(.+\.js):(\d+):(\d+)/);if(!r)continue;let n=r[1].trim();if(Ae.includes(n))continue;let c=parseInt(r[2])-Me;if(!(c<=0))return{line:c,column:parseInt(r[3]),phase:n.replace(".js","")}}return null}function Te(e,t,s){if(!e||!t)return;let r=String(e).split(/\r?\n/),n=Math.max(1,t-1),o=Math.min(r.length,t+1),c=String(o).length,g=[];for(let u=n;u<=o;u++){let h=u===t?">":" ";g.push(`${h} ${String(u).padStart(c," ")}. ${r[u-1]??""}`),u===t&&s&&s>0&&g.push(` ${" ".repeat(c)} ${" ".repeat(Math.max(0,s-1))}^`)}return g.join(`
|
|
875
|
+
`)}function Je(e,t){if(!(!e||!t)){if(t[e.phase])return t[e.phase];if(e.phase?.startsWith("preHook"))return t.preHook;if(e.phase?.startsWith("postHook"))return t.postHook}}function L(e,t){let s=Re(e);if(s)return{message:s.message,statusCode:s.statusCode,errorPath:s.errorPath,details:s.details};let r=Ne(e.stack),n=e.message||"Unknown error",o=r?`${n} (${r.phase}, line ${r.line})`:n,c=Je(r,t),g=r?Te(c,r.line,r.column):void 0;return{message:o,statusCode:e.statusCode||null,details:r?{...r,codeFrame:g}:void 0}}async function oe(e,t){let s=L(t);await A({type:"result",id:e,success:!1,error:{...s,stack:t.stack,code:t.code}})}async function Oe(e){let{id:t,code:s,pkgSources:r,snapshot:n,memoryLimitMb:o,isolatePoolSize:c}=e,g=r||[],u=Math.max(1,Math.trunc(Number(e.timeoutMs)||3e4)),h=ne(s,g),w=te(o,c),y=w.isolate,l=await ae(w,t,g,n,h,u);J.set(t,l);try{let m=`
|
|
774
876
|
(async () => {
|
|
775
877
|
"use strict";
|
|
776
|
-
${
|
|
878
|
+
${s}
|
|
777
879
|
})().then((__result) => __extractResult(__result, __result === undefined));
|
|
778
|
-
`,k=await(await $(y,m,"handler.js")).run(l,{timeout:u,promise:!0}),
|
|
779
|
-
`),g),
|
|
880
|
+
`,k=await(await $(y,m,"handler.js")).run(l,{timeout:u,promise:!0}),N=JSON.parse(k);await A({type:"result",id:t,success:!0,...N})}catch(m){await oe(t,m)}finally{J.delete(t),await se(w,l),ie(t)}}async function Ee(e){let{id:t,codeBlocks:s,pkgSources:r,snapshot:n,memoryLimitMb:o,isolatePoolSize:c}=e,g=r||[],u=Math.max(1,Math.trunc(Number(e.timeoutMs)||3e4)),h=ne((s||[]).map(p=>p.code||"").join(`
|
|
881
|
+
`),g),w=te(o,c),y=w.isolate,l=await ae(w,t,g,n,h,u);J.set(t,l);let m=[],P=[],k={},N=0,j=0;for(let p of s){let f=p.type||"handler";f==="postHook"?(j++,k[`postHook #${j}`]=p.code||"",k.postHook=p.code||"",P.push(p)):(f==="handler"?k.handler=p.code||"":(N++,k[`${f} #${N}`]=p.code||"",k.preHook=p.code||""),m.push(p))}try{let p=null;for(let a=0;a<m.length;a++){let i=m[a],d=i.type||"handler",_=d==="handler"?"handler":`${d} #${a+1}`,v;d==="preHook"?v=`
|
|
780
882
|
(async () => {
|
|
781
883
|
"use strict";
|
|
782
884
|
${i.code}
|
|
783
885
|
})().then((__r) => {
|
|
784
886
|
if (__r !== undefined) return JSON.stringify({ __shortCircuit: true, value: __r });
|
|
785
887
|
return JSON.stringify({ __shortCircuit: false });
|
|
786
|
-
});`:
|
|
888
|
+
});`:v=`
|
|
787
889
|
(async () => {
|
|
788
890
|
"use strict";
|
|
789
891
|
${i.code}
|
|
790
892
|
})().then((__r) => {
|
|
791
893
|
$ctx.$data = __r;
|
|
792
894
|
return "__handler_done__";
|
|
793
|
-
});`;try{let
|
|
895
|
+
});`;try{let x=await(await $(y,v,`${_}.js`)).run(l,{timeout:u,promise:!0});if(d==="preHook"&&typeof x=="string"){let b=JSON.parse(x);if(b.__shortCircuit){let F=await Z(y,l,b.value);await A({type:"result",id:t,success:!0,...F,shortCircuit:!0});return}}}catch(S){p=S;break}}if(!p&&P.length>0&&await(await $(y,'$ctx.$statusCode = 200; "__status_set__";',"set-status.js")).run(l,{timeout:5e3}),p&&P.length>0){let a=L(p,k),i={message:a.message||"Unknown error",name:p.constructor?.name||"Error",stack:p.stack||"",statusCode:a.statusCode||500,details:a.details||{},timestamp:new Date().toISOString()},d=`
|
|
794
896
|
$ctx.$error = ${JSON.stringify(i)};
|
|
795
897
|
$ctx.$data = null;
|
|
796
898
|
$ctx.$statusCode = ${i.statusCode};
|
|
@@ -804,11 +906,11 @@ if ($ctx.$api) {
|
|
|
804
906
|
timestamp: ${JSON.stringify(i.timestamp)},
|
|
805
907
|
};
|
|
806
908
|
}
|
|
807
|
-
"__error_populated__";`;await(await y.compileScript(d,{filename:"populate-error.js"})).run(l,{timeout:5e3})}for(let
|
|
909
|
+
"__error_populated__";`;await(await y.compileScript(d,{filename:"populate-error.js"})).run(l,{timeout:5e3})}for(let a=0;a<P.length;a++){let i=P[a],d=`postHook #${a+1}`,_=`
|
|
808
910
|
(async () => {
|
|
809
911
|
"use strict";
|
|
810
912
|
${i.code}
|
|
811
913
|
})().then((__r) => {
|
|
812
914
|
if (__r !== undefined) $ctx.$data = __r;
|
|
813
915
|
return "__posthook_done__";
|
|
814
|
-
});`;try{await(await $(y,
|
|
916
|
+
});`;try{await(await $(y,_,`${d}.js`)).run(l,{timeout:u,promise:!0})}catch{}}if(p){let a=L(p,k),i;try{let v=await(await $(y,"(async () => JSON.stringify(await __extractCtxChanges()))()","extract-error-ctx.js")).run(l,{timeout:5e3,promise:!0});i=JSON.parse(v)}catch{}await A({type:"result",id:t,success:!1,error:{message:a.message,statusCode:a.statusCode,errorPath:a.errorPath,stack:p.stack,code:p.code,details:a.details},ctxChanges:i});return}let f=await Z(y,l);await A({type:"result",id:t,success:!0,...f})}catch(p){await oe(t,p)}finally{J.delete(t),await se(w,l),ie(t)}}function ie(e){O.cleanupTaskPackages(e)}function Ie(){O.shutdownPackageRuntime()}process.on("exit",Ie);async function Z(e,t,s){if(s!==void 0){let c=`__extractResult(${JSON.stringify(s)}, false)`,u=await(await e.compileScript(c,{filename:"extract.js"})).run(t,{timeout:5e3,promise:!0});return JSON.parse(u)}let o=await(await $(e,"__extractResult($ctx.$data, $ctx.$data === undefined)","extract.js")).run(t,{timeout:5e3,promise:!0});return JSON.parse(o)}
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
`));return t.outputText.trimEnd()}assertExecutableJavaScript(e){e&&new Function(`return (async () => {
|
|
4
4
|
"use strict";
|
|
5
5
|
${e}
|
|
6
|
-
});`)}},ne=new Gt;function Yt(r,e){return ne.compileSource(r,e)}function Fi(r){return ne.isExecutableJavaScript(r)}function Ci(r,e){return ne.normalizeRecord(r,e)}function Ri(r,e,t){return ne.normalizePatch(r,e,t)}function xi(r){return ne.getExecutableScript(r)}function vi(r){return ne.resolveExecutableScript(r)}function Ii(r){return ne.normalizeFlowStepScriptConfig(r)}var Di=hr.join(__dirname,"execution/worker.js"),Ki=["Invalid or unexpected token","Unexpected identifier","Unexpected token","Missing initializer in const declaration","await is only valid"],br=new gr.AsyncLocalStorage;function _r(){return br.getStore()}var Rt=class{constructor(e,t,n,o,i,s,a,u=6e4){this.scriptPath=i;this.onCrash=s;this.onRotate=a;this.entries=[];this.waiting=[];this.cachedMemoryOk=!0;this.lastMemoryCheckMs=0;this.memorySampleIntervalMs=200;this.destroying=!1;this.entryCounter=0;this.max=e,this.effectiveMemory=t,this.rssCeiling=n,this.tasksCap=o,this.drainTimeoutMs=u;for(let l=0;l<e;l++)this.spawnEntry()}getEntries(){return this.entries}spawnEntry(){let e=new mr.Worker(this.scriptPath),t={id:++this.entryCounter,worker:e,tasks:new Map,draining:!1,spawnedAt:Date.now()};return e.on("message",n=>{let o=t.tasks.get(n.id);o&&(n.type==="result"?(typeof n.heapRatio=="number"&&(t.lastHeapRatio=n.heapRatio),n.contextStats&&(t.contextStats=n.contextStats),o.onResult(n),!t.draining&&typeof n.heapRatio=="number"&&n.heapRatio>=.8&&this.rotateEntry(t,`heap=${Math.round(n.heapRatio*100)}%`)):o.onIoCall(n))}),e.on("exit",()=>{let n=t.draining,o=this.destroying;t.drainTimeout&&(clearTimeout(t.drainTimeout),t.drainTimeout=void 0);for(let[,s]of t.tasks)s.abortController.abort(),s.onResult({type:"result",success:!1,error:{message:o?"Worker terminated":"Worker crashed"}});t.tasks.clear();let i=this.entries.indexOf(t);i!==-1&&this.entries.splice(i,1),!n&&!o&&(this.onCrash&&this.onCrash(),this.entries.length<this.max&&this.spawnEntry()),this.drainWaiting()}),this.entries.push(t),t}rotateEntry(e,t){if(e.draining)return;e.draining=!0;let n=Date.now()-e.spawnedAt;if(this.onRotate&&this.onRotate({reason:t,ageMs:n}),this.spawnEntry(),e.tasks.size===0){e.worker.terminate();return}e.drainTimeout=setTimeout(()=>{e.drainTimeout=void 0,e.worker.terminate()},this.drainTimeoutMs)}isMemoryAvailable(){let e=Date.now();return e-this.lastMemoryCheckMs>=this.memorySampleIntervalMs&&(this.cachedMemoryOk=process.memoryUsage().rss/this.effectiveMemory<this.rssCeiling,this.lastMemoryCheckMs=e),this.cachedMemoryOk}findLeastBusy(){let e=null;for(let t of this.entries)t.draining||t.tasks.size<this.tasksCap&&(!e||t.tasks.size<e.tasks.size)&&(e=t);return e}dispatch(){let e=this.findLeastBusy();return e&&(e.tasks.size===0||this.isMemoryAvailable())?Promise.resolve(e):new Promise(t=>this.waiting.push(t))}registerTask(e,t,n){e.tasks.set(t,n)}unregisterTask(e,t){e.tasks.delete(t),e.draining&&e.tasks.size===0&&(e.drainTimeout&&(clearTimeout(e.drainTimeout),e.drainTimeout=void 0),e.worker.terminate()),this.drainWaiting()}drainWaiting(){for(;this.waiting.length>0;){let e=this.findLeastBusy();if(e&&(e.tasks.size===0||this.isMemoryAvailable()))this.waiting.shift()(e);else break}}terminateEntry(e){e.worker.terminate()}resize(e){for(this.max=Math.max(1,e);this.entries.length<this.max;)this.spawnEntry()}getMax(){return this.max}getWaitingCount(){return this.waiting.length}getTotalActiveTasks(){let e=0;for(let t of this.entries)e+=t.tasks.size;return e}getMetrics(){return{max:this.max,activeTasks:this.getTotalActiveTasks(),waitingTasks:this.waiting.length,workers:this.entries.map(e=>({id:e.id,activeTasks:e.tasks.size,draining:e.draining,ageMs:Date.now()-e.spawnedAt,lastHeapRatio:e.lastHeapRatio??0,contextStats:e.contextStats??{}}))}}destroyAll(){this.destroying=!0;for(let e of this.entries)e.worker.terminate();this.entries.length=0,this.waiting.length=0}},pr=new Set(["$helpers","$cache","$repos","$req","$res","$socket","$storage","$logs","$throw","$trigger","$pkgs","$debug"]);function In(r){if(r===void 0)return JSON.stringify({__e:"u"});try{return JSON.stringify({__e:"v",d:r},(e,t)=>typeof t=="bigint"?t.toString():t)}catch{return JSON.stringify({__e:"v",d:{__serializationError:!0,message:"Result is not JSON-serializable"}})}}var We=class r{constructor(e){this.logger=new Jt(r.name);this.effectiveMemory=Ft();this.isolationTuning=Ht();this.pool=new Rt(this.isolationTuning.maxConcurrentWorkers,this.effectiveMemory,.9,this.isolationTuning.tasksPerWorkerCap,Di,()=>{this.crashesTotal++,this.logger.warn("Worker crashed, replacement spawned")},({reason:e,ageMs:t})=>{this.rotationsTotal++,this.logger.log(`Worker rotation: reason=${e} age=${Math.round(t/1e3)}s`)});this.ceiling=this.isolationTuning.maxConcurrentWorkers;this.prevCpuUsage=process.cpuUsage();this.prevCpuTime=process.hrtime.bigint();this.pressureTicks=0;this.recoveryTicks=0;this.taskCounter=0;this.taskDoneTotal=0;this.taskErrorTotal=0;this.taskTimeoutTotal=0;this.rotationsTotal=0;this.crashesTotal=0;this.responseStreams=new Map;this.taskDurationsMs=[];this.packageCacheService=e.packageCacheService,this.packageCdnLoaderService=e.packageCdnLoaderService,this.logger.log(`Worker pool started: ${this.isolationTuning.maxConcurrentWorkers} workers, ${this.isolationTuning.isolateMemoryLimitMb}MB per isolate, ${this.isolationTuning.isolatePoolSize} isolates/worker, ${this.isolationTuning.tasksPerWorkerCap} tasks/worker cap`),this.tuneTimer=setInterval(()=>this.autoTune(),3e4)}onDestroy(){this.tuneTimer&&clearInterval(this.tuneTimer),this.pool.destroyAll()}autoTune(){let t=process.memoryUsage().rss/this.effectiveMemory,n=process.hrtime.bigint(),o=process.cpuUsage(this.prevCpuUsage),i=Number(n-this.prevCpuTime)/1e3,s=i>0?(o.user+o.system)/i:0;this.prevCpuUsage=process.cpuUsage(),this.prevCpuTime=n;let a=this.pool.getMax(),u=this.pool.getWaitingCount(),l=t>.85||s>.7,f=t<.7&&s<.5,c=u>0,p=a;l?(this.recoveryTicks=0,this.pressureTicks++,this.pressureTicks>=3&&(p=Math.max(1,a-1))):f&&c?(this.pressureTicks=0,this.recoveryTicks++,this.recoveryTicks>=3&&(p=Math.min(this.ceiling,a+2))):(this.pressureTicks=0,this.recoveryTicks=0),p!==a&&(this.pool.resize(p),this.pressureTicks=0,this.recoveryTicks=0,this.logger.log(`Worker pool adjusted: ${a} -> ${p} (rss=${Math.round(t*100)}% cpu=${Math.round(s*100)}% queue=${u})`))}createSnapshot(e){let t={$body:e.$body,$query:e.$query,$params:e.$params,$user:e.$user,$share:e.$share,$data:e.$data,$api:{request:e.$api?.request},$uploadedFile:e.$uploadedFile,$req:this.cloneSnapshotValue(e.$req)};for(let o of Object.keys(e)){if(!o.startsWith("$")||o in t||pr.has(o))continue;let i=this.cloneSnapshotValue(e[o]);i!==void 0&&(t[o]=i)}let n=e.$flow;if(n!=null)try{t.$flow=this.cloneSnapshotValue(n)??{}}catch{t.$flow={}}return t}cloneSnapshotValue(e){if(typeof e!="function")try{return JSON.parse(JSON.stringify(e,(t,n)=>{if(typeof n=="bigint")return String(n);if(typeof n!="function")return n}))}catch{return}}mergeCtxChanges(e,t){if(t){t.$body!==void 0&&(e.$body=t.$body),t.$query!==void 0&&(e.$query=t.$query),t.$params!==void 0&&(e.$params=t.$params),t.$data!==void 0&&(e.$data=t.$data),t.$error!==void 0&&(e.$error=t.$error),t.$statusCode!==void 0&&(e.$statusCode=t.$statusCode),t.$share!==void 0&&(e.$share=t.$share),t.$api!==void 0&&e.$api&&(t.$api.error&&(e.$api.error=t.$api.error),t.$api.response&&(e.$api.response=t.$api.response)),t.$flow!==void 0&&t.$flow!==null&&typeof t.$flow=="object"&&e.$flow!=null&&typeof e.$flow=="object"&&Object.assign(e.$flow,t.$flow);for(let[n,o]of Object.entries(t))n.startsWith("$")&&(n==="$body"||n==="$query"||n==="$params"||n==="$data"||n==="$error"||n==="$statusCode"||n==="$share"||n==="$api"||n==="$flow"||pr.has(n)||n==="$env"||(e[n]=o));delete e.$pkgs}}spawnAndExecute(e,t,n,o){return(async()=>{let i=await this.pool.dispatch(),s=`t_${++this.taskCounter}`,a=Date.now();return new Promise((u,l)=>{let f=!1,c=new AbortController,p=setTimeout(()=>{if(f)return;f=!0,c.abort();let d=new Error(`Script execution timed out after ${o}ms`);this.cleanupResponseStream(s,d),this.pool.unregisterTask(i,s),this.taskTimeoutTotal++,z({event:"task_timeout",id:s,timeoutMs:o}),this.pool.terminateEntry(i),d.code="ERR_SCRIPT_EXECUTION_TIMEOUT",d.isTimeout=!0,l(d)},o);this.pool.registerTask(i,s,{abortController:c,onResult:d=>{if(!f)if(f=!0,clearTimeout(p),this.cleanupResponseStream(s),this.pool.unregisterTask(i,s),this.recordTaskDuration(Date.now()-a),d.success){this.taskDoneTotal++,z({event:"task_done",id:s,ok:!0});let g={value:d.value,valueAbsent:d.valueAbsent===!0,ctxChanges:d.ctxChanges};d.shortCircuit&&(g.shortCircuit=!0),u(g)}else{this.taskErrorTotal++,z({event:"task_done",id:s,ok:!1,message:d.error?.message});let g=new Error(d.error?.message||"Handler execution failed");g.statusCode=d.error?.statusCode,g.code=d.error?.code,g.details=d.error?.details,d.error?.stack&&(g.stack=d.error.stack),d.ctxChanges&&(g.ctxChanges=d.ctxChanges),this.cleanupResponseStream(s,g),l(g)}},onIoCall:d=>{if(f)return;let g=c.signal;d.type==="repoCall"?this.handleIoCall(()=>this.execRepoCall(d,n),i.worker,d.callId,g):d.type==="helpersCall"?this.handleIoCall(()=>this.execHelpersCall(d,n),i.worker,d.callId,g):d.type==="socketCall"?this.handleIoCall(()=>this.execSocketCall(d,n),i.worker,d.callId,g):d.type==="cacheCall"?this.handleIoCall(()=>this.execCacheCall(d,n),i.worker,d.callId,g):d.type==="storageCall"?this.handleIoCall(()=>this.execStorageCall(d,n),i.worker,d.callId,g):d.type==="dispatchCall"?this.handleIoCall(()=>this.execDispatchCall(d,n),i.worker,d.callId,g):d.type==="resCall"&&this.handleIoCall(()=>this.execResCall(d,n),i.worker,d.callId,g)}}),i.worker.postMessage({type:e,id:s,...t})})})()}async handleIoCall(e,t,n,o){if(!o?.aborted)try{let i=await(o?br.run(o,e):e());if(o?.aborted)return;try{t.postMessage({type:"callResult",callId:n,result:In(i)})}catch{}}catch(i){if(o?.aborted)return;try{let s={__userThrow:!0,message:i.message,statusCode:i.statusCode,code:i.errorCode??i.code,details:i.details,messages:i.messages};t.postMessage({type:"callError",callId:n,error:JSON.stringify(s)})}catch{}}}async execRepoCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$repos?.[e.table];if(!o||typeof o[e.method]!="function")throw new Error(`Repo method not found: ${e.table}.${e.method}`);return o[e.method](...n)}async execHelpersCall(e,t){let n=JSON.parse(e.argsJson),o=e.name.split("."),i=t?.$helpers;for(let s of o)i=i?.[s];if(typeof i!="function")throw new Error(`Helper not found: ${e.name}`);return i(...n)}async execSocketCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$socket?.[e.method];if(typeof o!="function")throw new Error(`Socket method not found: ${e.method}`);return o(...n)}async execCacheCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$cache?.[e.method];if(typeof o!="function")throw new Error(`Cache method not found: ${e.method}`);return o(...n)}async execStorageCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$storage?.[e.method];if(typeof o!="function")throw new Error(`Storage method not found: ${e.method}`);return o(...n)}async execDispatchCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$trigger;if(typeof o!="function")throw new Error("$trigger is not available");return o(...n)}async execResCall(e,t){if(e.method==="streamStart"){let s=JSON.parse(e.argsJson||"[]")[1]||{},a=t?.$res?.stream;if(typeof a!="function")throw new Error("Response method not found: stream");let u=new yr.PassThrough;return this.responseStreams.set(e.id,{stream:u}),a.call(t.$res,u,s),!0}if(e.method==="streamChunk"){let i=this.responseStreams.get(e.id);if(!i)throw new Error(`Response stream not found: ${e.id}`);let s=Buffer.from(e.chunk||"","base64");return i.stream.write(s)||await new Promise((a,u)=>{let l=()=>{c(),a()},f=p=>{c(),u(p)},c=()=>{i.stream.off("drain",l),i.stream.off("error",f)};i.stream.once("drain",l),i.stream.once("error",f)}),!0}if(e.method==="streamEnd"){let i=this.responseStreams.get(e.id);return i&&(i.stream.end(),this.responseStreams.delete(e.id)),!0}if(e.method==="streamError"){let i=this.responseStreams.get(e.id);if(i){let s=e.error?.message||"Response stream failed";i.stream.destroy(new Error(s)),this.responseStreams.delete(e.id)}return!0}let n=JSON.parse(e.argsJson||"[]"),o=t?.$res?.[e.method];if(typeof o!="function")throw new Error(`Response method not found: ${e.method}`);return o.apply(t.$res,n)}cleanupResponseStream(e,t){let n=this.responseStreams.get(e);n&&(this.responseStreams.delete(e),t?n.stream.destroy(t):n.stream.end())}isScriptParseFailure(e){let t=e.message||"";return Ki.some(n=>t.includes(n))}async run(e,t,n){let o=Math.max(1,Math.trunc(Number(n)||3e4)),i=await this.packageCacheService.getPackages(),s=this.packageCdnLoaderService.getPackageSources(i),a=this.createSnapshot(t);z({event:"isolated_run_start",timeoutMs:o,codeLen:e?.length??0});let u;try{let l={code:e,pkgSources:s,snapshot:a,timeoutMs:o,memoryLimitMb:this.isolationTuning.isolateMemoryLimitMb,isolatePoolSize:this.isolationTuning.isolatePoolSize};try{u=await this.spawnAndExecute("execute",l,t,o)}catch(f){let c=f,p=Yt(e,"typescript")||"";if(!this.isScriptParseFailure(c)||p===e)throw f;z({event:"isolated_run_ts_retry",message:c.message}),u=await this.spawnAndExecute("execute",{...l,code:p},t,o)}}catch(l){let f=l;if(z({event:"isolated_run_error",message:f.message,code:f.code,isTimeout:!!f.isTimeout}),f.isTimeout||f.code==="ERR_SCRIPT_EXECUTION_TIMEOUT")throw new we(o,e);let c=f.statusCode??(typeof f.status=="number"?f.status:void 0);throw Ne.createException(f.errorPath,c,f.message||"Unknown error",e,f.details||{})}return this.mergeCtxChanges(t,u.ctxChanges||{}),z({event:"isolated_run_ok"}),u.valueAbsent?void 0:u.value}async runBatch(e,t,n){let o=Math.max(1,Math.trunc(Number(n)||3e4)),i=await this.packageCacheService.getPackages(),s=this.packageCdnLoaderService.getPackageSources(i),a=this.createSnapshot(t);z({event:"isolated_batch_start",timeoutMs:o,blocks:e.length});let u;try{let l={codeBlocks:e,pkgSources:s,snapshot:a,timeoutMs:o,memoryLimitMb:this.isolationTuning.isolateMemoryLimitMb,isolatePoolSize:this.isolationTuning.isolatePoolSize};try{u=await this.spawnAndExecute("executeBatch",l,t,o)}catch(f){let c=f,p=e.map(g=>({...g,code:Yt(g.code,"typescript")||""})),d=p.some((g,y)=>g.code!==e[y]?.code);if(!this.isScriptParseFailure(c)||!d)throw f;z({event:"isolated_batch_ts_retry",message:c.message}),u=await this.spawnAndExecute("executeBatch",{...l,codeBlocks:p},t,o)}}catch(l){let f=l;if(z({event:"isolated_batch_error",message:f.message,code:f.code,isTimeout:!!f.isTimeout}),f.ctxChanges&&this.mergeCtxChanges(t,f.ctxChanges),f.isTimeout||f.code==="ERR_SCRIPT_EXECUTION_TIMEOUT")throw new we(o,"(batch execution)");let c=f.statusCode??(typeof f.status=="number"?f.status:void 0);throw Ne.createException(f.errorPath,c,f.message||"Unknown error","(batch execution)",f.details||{})}return this.mergeCtxChanges(t,u.ctxChanges||{}),z({event:"isolated_batch_ok"}),{value:u.valueAbsent?void 0:u.value,shortCircuit:u.shortCircuit===!0}}recordTaskDuration(e){this.taskDurationsMs.push(e),this.taskDurationsMs.length>512&&this.taskDurationsMs.shift()}percentile(e,t){if(e.length===0)return 0;let n=[...e].sort((i,s)=>i-s),o=Math.min(n.length-1,Math.max(0,Math.ceil(t/100*n.length)-1));return n[o]}getMetrics(){let e=this.pool.getMetrics(),t=this.taskDurationsMs,n=t.length===0?0:t.reduce((o,i)=>o+i,0)/t.length;return{tuning:this.isolationTuning,pool:e,taskDoneTotal:this.taskDoneTotal,taskErrorTotal:this.taskErrorTotal,taskTimeoutTotal:this.taskTimeoutTotal,rotationsTotal:this.rotationsTotal,crashesTotal:this.crashesTotal,avgTaskMs:n,p95TaskMs:this.percentile(t,95),p99TaskMs:this.percentile(t,99),maxHeapRatio:Math.max(0,...e.workers.map(o=>o.lastHeapRatio||0))}}};var Tn=require("node:fs"),ii=require("node:path");var Mn=require("mongodb");Ue();function Ae(r){return r.type==="one-to-many"||r.isInverse===!0||r.type==="one-to-one"&&!!r.mappedBy}function H(r){return(r.type==="many-to-one"||r.type==="one-to-one")&&!Ae(r)&&(r.foreignKeyColumn||r.propertyName)||null}function W(r){return r.foreignKeyColumn||r.mappedBy||r.propertyName||null}function ae(r){if(!r||typeof r!="object")return r;if(r instanceof Mn.ObjectId)return r.toString();if(r instanceof Date)return r.toISOString();if(Array.isArray(r))return r.map(t=>ae(t));if("buffer"in r&&r.buffer&&typeof r.buffer=="object"&&Object.keys(r.buffer).length===12)try{let t=r.buffer,n=Object.keys(t).sort((o,i)=>parseInt(o)-parseInt(i)).map(o=>t[o]);return new Mn.ObjectId(Buffer.from(n)).toString()}catch{}let e={};for(let[t,n]of Object.entries(r))e[t]=n&&typeof n=="object"&&!(n instanceof Buffer)?ae(n):n;return e}function de(r,e){if(e.type!=="many-to-many")return null;let t=e.targetTableName||e.targetTable;if(!t)return null;if(e.mappedBy){let a=t,u=e.mappedBy,l=r,f=e.junctionTableName||xt(a,u,l);if(e.junctionSourceColumn&&e.junctionTargetColumn)return{junctionName:f,selfColumn:e.junctionSourceColumn,otherColumn:e.junctionTargetColumn,owningTable:a,inverseTable:l};let c=vt(a,u,l);return{junctionName:f,selfColumn:c.targetColumn,otherColumn:c.sourceColumn,owningTable:a,inverseTable:l}}let n=r,o=t,i=e.junctionTableName||xt(n,e.propertyName,o);if(e.junctionSourceColumn&&e.junctionTargetColumn)return{junctionName:i,selfColumn:e.junctionSourceColumn,otherColumn:e.junctionTargetColumn,owningTable:n,inverseTable:o};let s=vt(n,e.propertyName,o);return{junctionName:i,selfColumn:s.sourceColumn,otherColumn:s.targetColumn,owningTable:n,inverseTable:o}}async function It(r,e,t,n){if(!r)return{scalarFields:[],relations:[]};let o=r.tables?.get(e);if(!o)return{scalarFields:[],relations:[]};let i=new Map;for(let l of t)if(l==="*")i.has("")||i.set("",[]),i.get("").push(l);else if(l.includes(".")){let f=l.split("."),c=f[0],p=f.slice(1).join(".");i.has(c)||i.set(c,[]),i.get(c).push(p)}else o.relations?.some(c=>c.propertyName===l)?i.has(l)||i.set(l,["_id"]):(i.has("")||i.set("",[]),i.get("").push(l));if(n&&typeof n=="object")for(let[l,f]of Object.entries(n)){if(i.has(l))continue;let c=f&&typeof f=="object"&&f.fields!=null?Array.isArray(f.fields)?f.fields:String(f.fields).split(",").map(p=>p.trim()).filter(Boolean):["*"];i.set(l,c)}let s=[],a=[],u=i.get("")||[];for(let l of u)if(l==="*"){if(o.columns)for(let f of o.columns)s.includes(f.name)||s.push(f.name);if(o.relations)for(let f of o.relations)i.has(f.propertyName)||i.set(f.propertyName,["_id"])}else o.columns?.some(c=>c.name===l)&&!s.includes(l)&&s.push(l);for(let[l,f]of i.entries()){if(l==="")continue;let c=o.relations?.find(h=>h.propertyName===l);if(!c)continue;let p="",d="",g=!1;c.type==="many-to-one"||c.type==="one-to-one"?Ae(c)?(p="_id",d=W(c)||c.propertyName,g=!0):(p=H(c)||c.propertyName,d="_id",g=!1):c.type==="one-to-many"?(p="_id",d=W(c)||c.propertyName,g=!0):c.type==="many-to-many"&&(c.mappedBy?(p="_id",d=c.mappedBy,g=!0):(p=c.propertyName,d="_id",g=!1));let y=c.type==="one-to-many"||c.type==="many-to-many";a.push({propertyName:l,targetTable:c.targetTableName,localField:p,foreignField:d,type:y?"many":"one",isInverse:g,nestedFields:f})}return{scalarFields:s,relations:a}}var Zt=fe(require("fs")),On=fe(require("path"));var Wi=new RegExp("\\p{Mark}","gu");function xr(r){return r==null?"":String(r).normalize("NFKD").replace(Wi,"").toLowerCase()}function An(){let r={};for(let e=128;e<=1114111;e++)try{let t=String.fromCodePoint(e),n=xr(t);n!==t&&(r[t]=n)}catch{}return JSON.stringify(r)}function Xt(){return`function() {
|
|
6
|
+
});`)}},ne=new Gt;function Yt(r,e){return ne.compileSource(r,e)}function Fi(r){return ne.isExecutableJavaScript(r)}function Ci(r,e){return ne.normalizeRecord(r,e)}function Ri(r,e,t){return ne.normalizePatch(r,e,t)}function xi(r){return ne.getExecutableScript(r)}function vi(r){return ne.resolveExecutableScript(r)}function Ii(r){return ne.normalizeFlowStepScriptConfig(r)}var Di=hr.join(__dirname,"execution/worker.js"),Ki=["Invalid or unexpected token","Unexpected identifier","Unexpected token","Missing initializer in const declaration","await is only valid"],br=new gr.AsyncLocalStorage;function _r(){return br.getStore()}var Rt=class{constructor(e,t,n,o,i,s,a,u=6e4){this.scriptPath=i;this.onCrash=s;this.onRotate=a;this.entries=[];this.waiting=[];this.cachedMemoryOk=!0;this.lastMemoryCheckMs=0;this.memorySampleIntervalMs=200;this.destroying=!1;this.entryCounter=0;this.max=e,this.effectiveMemory=t,this.rssCeiling=n,this.tasksCap=o,this.drainTimeoutMs=u;for(let l=0;l<e;l++)this.spawnEntry()}getEntries(){return this.entries}spawnEntry(){let e=new mr.Worker(this.scriptPath),t={id:++this.entryCounter,worker:e,tasks:new Map,draining:!1,spawnedAt:Date.now()};return e.on("message",n=>{let o=t.tasks.get(n.id);o&&(n.type==="result"?(typeof n.heapRatio=="number"&&(t.lastHeapRatio=n.heapRatio),n.contextStats&&(t.contextStats=n.contextStats),o.onResult(n),!t.draining&&typeof n.heapRatio=="number"&&n.heapRatio>=.8&&this.rotateEntry(t,`heap=${Math.round(n.heapRatio*100)}%`)):o.onIoCall(n))}),e.on("exit",()=>{let n=t.draining,o=this.destroying;t.drainTimeout&&(clearTimeout(t.drainTimeout),t.drainTimeout=void 0);for(let[,s]of t.tasks)s.abortController.abort(),s.onResult({type:"result",success:!1,error:{message:o?"Worker terminated":"Worker crashed"}});t.tasks.clear();let i=this.entries.indexOf(t);i!==-1&&this.entries.splice(i,1),!n&&!o&&(this.onCrash&&this.onCrash(),this.entries.length<this.max&&this.spawnEntry()),this.drainWaiting()}),this.entries.push(t),t}rotateEntry(e,t){if(e.draining)return;e.draining=!0;let n=Date.now()-e.spawnedAt;if(this.onRotate&&this.onRotate({reason:t,ageMs:n}),this.spawnEntry(),e.tasks.size===0){e.worker.terminate();return}e.drainTimeout=setTimeout(()=>{e.drainTimeout=void 0,e.worker.terminate()},this.drainTimeoutMs)}isMemoryAvailable(){let e=Date.now();return e-this.lastMemoryCheckMs>=this.memorySampleIntervalMs&&(this.cachedMemoryOk=process.memoryUsage().rss/this.effectiveMemory<this.rssCeiling,this.lastMemoryCheckMs=e),this.cachedMemoryOk}findLeastBusy(){let e=null;for(let t of this.entries)t.draining||t.tasks.size<this.tasksCap&&(!e||t.tasks.size<e.tasks.size)&&(e=t);return e}dispatch(){let e=this.findLeastBusy();return e&&(e.tasks.size===0||this.isMemoryAvailable())?Promise.resolve(e):new Promise(t=>this.waiting.push(t))}registerTask(e,t,n){e.tasks.set(t,n)}unregisterTask(e,t){e.tasks.delete(t),e.draining&&e.tasks.size===0&&(e.drainTimeout&&(clearTimeout(e.drainTimeout),e.drainTimeout=void 0),e.worker.terminate()),this.drainWaiting()}drainWaiting(){for(;this.waiting.length>0;){let e=this.findLeastBusy();if(e&&(e.tasks.size===0||this.isMemoryAvailable()))this.waiting.shift()(e);else break}}terminateEntry(e){e.worker.terminate()}resize(e){for(this.max=Math.max(1,e);this.entries.length<this.max;)this.spawnEntry()}getMax(){return this.max}getWaitingCount(){return this.waiting.length}getTotalActiveTasks(){let e=0;for(let t of this.entries)e+=t.tasks.size;return e}getMetrics(){return{max:this.max,activeTasks:this.getTotalActiveTasks(),waitingTasks:this.waiting.length,workers:this.entries.map(e=>({id:e.id,activeTasks:e.tasks.size,draining:e.draining,ageMs:Date.now()-e.spawnedAt,lastHeapRatio:e.lastHeapRatio??0,contextStats:e.contextStats??{}}))}}destroyAll(){this.destroying=!0;for(let e of this.entries)e.worker.terminate();this.entries.length=0,this.waiting.length=0}},pr=new Set(["$helpers","$cache","$repos","$req","$res","$socket","$storage","$logs","$throw","$trigger","$pkgs","$debug"]);function In(r){if(r===void 0)return JSON.stringify({__e:"u"});try{return JSON.stringify({__e:"v",d:r},(e,t)=>typeof t=="bigint"?t.toString():t)}catch{return JSON.stringify({__e:"v",d:{__serializationError:!0,message:"Result is not JSON-serializable"}})}}var We=class r{constructor(e){this.logger=new Jt(r.name);this.effectiveMemory=Ft();this.isolationTuning=Ht();this.pool=new Rt(this.isolationTuning.maxConcurrentWorkers,this.effectiveMemory,.9,this.isolationTuning.tasksPerWorkerCap,Di,()=>{this.crashesTotal++,this.logger.warn("Worker crashed, replacement spawned")},({reason:e,ageMs:t})=>{this.rotationsTotal++,this.logger.log(`Worker rotation: reason=${e} age=${Math.round(t/1e3)}s`)});this.ceiling=this.isolationTuning.maxConcurrentWorkers;this.prevCpuUsage=process.cpuUsage();this.prevCpuTime=process.hrtime.bigint();this.pressureTicks=0;this.recoveryTicks=0;this.taskCounter=0;this.taskDoneTotal=0;this.taskErrorTotal=0;this.taskTimeoutTotal=0;this.rotationsTotal=0;this.crashesTotal=0;this.responseStreams=new Map;this.taskDurationsMs=[];this.runtimeCallbackCounter=0;this.packageCacheService=e.packageCacheService,this.packageCdnLoaderService=e.packageCdnLoaderService,this.logger.log(`Worker pool started: ${this.isolationTuning.maxConcurrentWorkers} workers, ${this.isolationTuning.isolateMemoryLimitMb}MB per isolate, ${this.isolationTuning.isolatePoolSize} isolates/worker, ${this.isolationTuning.tasksPerWorkerCap} tasks/worker cap`),this.tuneTimer=setInterval(()=>this.autoTune(),3e4)}onDestroy(){this.tuneTimer&&clearInterval(this.tuneTimer),this.pool.destroyAll()}autoTune(){let t=process.memoryUsage().rss/this.effectiveMemory,n=process.hrtime.bigint(),o=process.cpuUsage(this.prevCpuUsage),i=Number(n-this.prevCpuTime)/1e3,s=i>0?(o.user+o.system)/i:0;this.prevCpuUsage=process.cpuUsage(),this.prevCpuTime=n;let a=this.pool.getMax(),u=this.pool.getWaitingCount(),l=t>.85||s>.7,f=t<.7&&s<.5,c=u>0,p=a;l?(this.recoveryTicks=0,this.pressureTicks++,this.pressureTicks>=3&&(p=Math.max(1,a-1))):f&&c?(this.pressureTicks=0,this.recoveryTicks++,this.recoveryTicks>=3&&(p=Math.min(this.ceiling,a+2))):(this.pressureTicks=0,this.recoveryTicks=0),p!==a&&(this.pool.resize(p),this.pressureTicks=0,this.recoveryTicks=0,this.logger.log(`Worker pool adjusted: ${a} -> ${p} (rss=${Math.round(t*100)}% cpu=${Math.round(s*100)}% queue=${u})`))}createSnapshot(e){let t={$body:e.$body,$query:e.$query,$params:e.$params,$user:e.$user,$share:e.$share,$data:e.$data,$api:{request:e.$api?.request},$uploadedFile:e.$uploadedFile,$req:this.cloneSnapshotValue(e.$req)};for(let o of Object.keys(e)){if(!o.startsWith("$")||o in t||pr.has(o))continue;let i=this.cloneSnapshotValue(e[o]);i!==void 0&&(t[o]=i)}let n=e.$flow;if(n!=null)try{t.$flow=this.cloneSnapshotValue(n)??{}}catch{t.$flow={}}return t}cloneSnapshotValue(e){if(typeof e!="function")try{return JSON.parse(JSON.stringify(e,(t,n)=>{if(typeof n=="bigint")return String(n);if(typeof n!="function")return n}))}catch{return}}mergeCtxChanges(e,t){if(t){t.$body!==void 0&&(e.$body=t.$body),t.$query!==void 0&&(e.$query=t.$query),t.$params!==void 0&&(e.$params=t.$params),t.$data!==void 0&&(e.$data=t.$data),t.$error!==void 0&&(e.$error=t.$error),t.$statusCode!==void 0&&(e.$statusCode=t.$statusCode),t.$share!==void 0&&(e.$share=t.$share),t.$api!==void 0&&e.$api&&(t.$api.error&&(e.$api.error=t.$api.error),t.$api.response&&(e.$api.response=t.$api.response)),t.$flow!==void 0&&t.$flow!==null&&typeof t.$flow=="object"&&e.$flow!=null&&typeof e.$flow=="object"&&Object.assign(e.$flow,t.$flow);for(let[n,o]of Object.entries(t))n.startsWith("$")&&(n==="$body"||n==="$query"||n==="$params"||n==="$data"||n==="$error"||n==="$statusCode"||n==="$share"||n==="$api"||n==="$flow"||pr.has(n)||n==="$env"||(e[n]=o));delete e.$pkgs}}spawnAndExecute(e,t,n,o){return(async()=>{let i=await this.pool.dispatch(),s=`t_${++this.taskCounter}`,a=Date.now();return new Promise((u,l)=>{let f=!1,c=new AbortController,p=setTimeout(()=>{if(f)return;f=!0,c.abort();let d=new Error(`Script execution timed out after ${o}ms`);this.cleanupResponseStream(s,d),this.pool.unregisterTask(i,s),this.taskTimeoutTotal++,z({event:"task_timeout",id:s,timeoutMs:o}),this.pool.terminateEntry(i),d.code="ERR_SCRIPT_EXECUTION_TIMEOUT",d.isTimeout=!0,l(d)},o);this.pool.registerTask(i,s,{abortController:c,onResult:d=>{if(!f)if(f=!0,clearTimeout(p),this.cleanupResponseStream(s),this.pool.unregisterTask(i,s),this.recordTaskDuration(Date.now()-a),d.success){this.taskDoneTotal++,z({event:"task_done",id:s,ok:!0});let g={value:d.value,valueAbsent:d.valueAbsent===!0,ctxChanges:d.ctxChanges};d.shortCircuit&&(g.shortCircuit=!0),u(g)}else{this.taskErrorTotal++,z({event:"task_done",id:s,ok:!1,message:d.error?.message});let g=new Error(d.error?.message||"Handler execution failed");g.statusCode=d.error?.statusCode,g.code=d.error?.code,g.details=d.error?.details,d.error?.stack&&(g.stack=d.error.stack),d.ctxChanges&&(g.ctxChanges=d.ctxChanges),this.cleanupResponseStream(s,g),l(g)}},onIoCall:d=>{if(f)return;let g=c.signal;d.type==="repoCall"?this.handleIoCall(()=>this.execRepoCall(d,n),i.worker,d.callId,g):d.type==="helpersCall"?this.handleIoCall(()=>this.execHelpersCall(d,n),i.worker,d.callId,g):d.type==="socketCall"?this.handleIoCall(()=>this.execSocketCall(d,n),i.worker,d.callId,g):d.type==="cacheCall"?this.handleIoCall(()=>this.execCacheCall(d,n),i.worker,d.callId,g):d.type==="storageCall"?this.handleIoCall(()=>this.execStorageCall(d,n,i.worker),i.worker,d.callId,g):d.type==="dispatchCall"?this.handleIoCall(()=>this.execDispatchCall(d,n),i.worker,d.callId,g):d.type==="resCall"&&this.handleIoCall(()=>this.execResCall(d,n),i.worker,d.callId,g)}}),i.worker.postMessage({type:e,id:s,...t})})})()}async handleIoCall(e,t,n,o){if(!o?.aborted)try{let i=await(o?br.run(o,e):e());if(o?.aborted)return;try{t.postMessage({type:"callResult",callId:n,result:In(i)})}catch{}}catch(i){if(o?.aborted)return;try{let s={__userThrow:!0,message:i.message,statusCode:i.statusCode,code:i.errorCode??i.code,details:i.details,messages:i.messages};t.postMessage({type:"callError",callId:n,error:JSON.stringify(s)})}catch{}}}async execRepoCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$repos?.[e.table];if(!o||typeof o[e.method]!="function")throw new Error(`Repo method not found: ${e.table}.${e.method}`);return o[e.method](...n)}async execHelpersCall(e,t){let n=JSON.parse(e.argsJson),o=e.name.split("."),i=t?.$helpers;for(let s of o)i=i?.[s];if(typeof i!="function")throw new Error(`Helper not found: ${e.name}`);return i(...n)}async execSocketCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$socket?.[e.method];if(typeof o!="function")throw new Error(`Socket method not found: ${e.method}`);return o(...n)}async execCacheCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$cache?.[e.method];if(typeof o!="function")throw new Error(`Cache method not found: ${e.method}`);return o(...n)}async execStorageCall(e,t,n){let o=this.parseStorageArgs(e.argsJson,n,e.id),i=t?.$storage?.[e.method];if(typeof i!="function")throw new Error(`Storage method not found: ${e.method}`);return i(...o)}parseStorageArgs(e,t,n){return JSON.parse(e,(o,i)=>{if(i?.__fnRef&&typeof i.__fnRef=="string"){let s=i.__fnRef;return(...a)=>this.invokeRuntimeCallback(t,n,s,a)}return i})}invokeRuntimeCallback(e,t,n,o){let i=`rtcb_${++this.runtimeCallbackCounter}`;return new Promise((s,a)=>{let u=setTimeout(()=>{f(),a(new Error(`Runtime callback timed out: ${n}`))},3e4),l=c=>{if(!(c?.type!=="runtimeCallbackResult"||c.callId!==i)){if(f(),c.error){a(new Error(c.error));return}s(c.value)}},f=()=>{clearTimeout(u),e.off("message",l)};e.on("message",l),e.postMessage({type:"invokeRuntimeCallback",id:t,callId:i,callbackId:n,argsJson:JSON.stringify(o)})})}async execDispatchCall(e,t){let n=JSON.parse(e.argsJson),o=t?.$trigger;if(typeof o!="function")throw new Error("$trigger is not available");return o(...n)}async execResCall(e,t){if(e.method==="streamStart"){let s=JSON.parse(e.argsJson||"[]")[1]||{},a=t?.$res?.stream;if(typeof a!="function")throw new Error("Response method not found: stream");let u=new yr.PassThrough;return this.responseStreams.set(e.id,{stream:u}),a.call(t.$res,u,s),!0}if(e.method==="streamChunk"){let i=this.responseStreams.get(e.id);if(!i)throw new Error(`Response stream not found: ${e.id}`);let s=Buffer.from(e.chunk||"","base64");return i.stream.write(s)||await new Promise((a,u)=>{let l=()=>{c(),a()},f=p=>{c(),u(p)},c=()=>{i.stream.off("drain",l),i.stream.off("error",f)};i.stream.once("drain",l),i.stream.once("error",f)}),!0}if(e.method==="streamEnd"){let i=this.responseStreams.get(e.id);return i&&(i.stream.end(),this.responseStreams.delete(e.id)),!0}if(e.method==="streamError"){let i=this.responseStreams.get(e.id);if(i){let s=e.error?.message||"Response stream failed";i.stream.destroy(new Error(s)),this.responseStreams.delete(e.id)}return!0}let n=JSON.parse(e.argsJson||"[]"),o=t?.$res?.[e.method];if(typeof o!="function")throw new Error(`Response method not found: ${e.method}`);return o.apply(t.$res,n)}cleanupResponseStream(e,t){let n=this.responseStreams.get(e);n&&(this.responseStreams.delete(e),t?n.stream.destroy(t):n.stream.end())}isScriptParseFailure(e){let t=e.message||"";return Ki.some(n=>t.includes(n))}async run(e,t,n){let o=Math.max(1,Math.trunc(Number(n)||3e4)),i=await this.packageCacheService.getPackages(),s=this.packageCdnLoaderService.getPackageSources(i),a=this.createSnapshot(t);z({event:"isolated_run_start",timeoutMs:o,codeLen:e?.length??0});let u;try{let l={code:e,pkgSources:s,snapshot:a,timeoutMs:o,memoryLimitMb:this.isolationTuning.isolateMemoryLimitMb,isolatePoolSize:this.isolationTuning.isolatePoolSize};try{u=await this.spawnAndExecute("execute",l,t,o)}catch(f){let c=f,p=Yt(e,"typescript")||"";if(!this.isScriptParseFailure(c)||p===e)throw f;z({event:"isolated_run_ts_retry",message:c.message}),u=await this.spawnAndExecute("execute",{...l,code:p},t,o)}}catch(l){let f=l;if(z({event:"isolated_run_error",message:f.message,code:f.code,isTimeout:!!f.isTimeout}),f.isTimeout||f.code==="ERR_SCRIPT_EXECUTION_TIMEOUT")throw new we(o,e);let c=f.statusCode??(typeof f.status=="number"?f.status:void 0);throw Ne.createException(f.errorPath,c,f.message||"Unknown error",e,f.details||{})}return this.mergeCtxChanges(t,u.ctxChanges||{}),z({event:"isolated_run_ok"}),u.valueAbsent?void 0:u.value}async runBatch(e,t,n){let o=Math.max(1,Math.trunc(Number(n)||3e4)),i=await this.packageCacheService.getPackages(),s=this.packageCdnLoaderService.getPackageSources(i),a=this.createSnapshot(t);z({event:"isolated_batch_start",timeoutMs:o,blocks:e.length});let u;try{let l={codeBlocks:e,pkgSources:s,snapshot:a,timeoutMs:o,memoryLimitMb:this.isolationTuning.isolateMemoryLimitMb,isolatePoolSize:this.isolationTuning.isolatePoolSize};try{u=await this.spawnAndExecute("executeBatch",l,t,o)}catch(f){let c=f,p=e.map(g=>({...g,code:Yt(g.code,"typescript")||""})),d=p.some((g,y)=>g.code!==e[y]?.code);if(!this.isScriptParseFailure(c)||!d)throw f;z({event:"isolated_batch_ts_retry",message:c.message}),u=await this.spawnAndExecute("executeBatch",{...l,codeBlocks:p},t,o)}}catch(l){let f=l;if(z({event:"isolated_batch_error",message:f.message,code:f.code,isTimeout:!!f.isTimeout}),f.ctxChanges&&this.mergeCtxChanges(t,f.ctxChanges),f.isTimeout||f.code==="ERR_SCRIPT_EXECUTION_TIMEOUT")throw new we(o,"(batch execution)");let c=f.statusCode??(typeof f.status=="number"?f.status:void 0);throw Ne.createException(f.errorPath,c,f.message||"Unknown error","(batch execution)",f.details||{})}return this.mergeCtxChanges(t,u.ctxChanges||{}),z({event:"isolated_batch_ok"}),{value:u.valueAbsent?void 0:u.value,shortCircuit:u.shortCircuit===!0}}recordTaskDuration(e){this.taskDurationsMs.push(e),this.taskDurationsMs.length>512&&this.taskDurationsMs.shift()}percentile(e,t){if(e.length===0)return 0;let n=[...e].sort((i,s)=>i-s),o=Math.min(n.length-1,Math.max(0,Math.ceil(t/100*n.length)-1));return n[o]}getMetrics(){let e=this.pool.getMetrics(),t=this.taskDurationsMs,n=t.length===0?0:t.reduce((o,i)=>o+i,0)/t.length;return{tuning:this.isolationTuning,pool:e,taskDoneTotal:this.taskDoneTotal,taskErrorTotal:this.taskErrorTotal,taskTimeoutTotal:this.taskTimeoutTotal,rotationsTotal:this.rotationsTotal,crashesTotal:this.crashesTotal,avgTaskMs:n,p95TaskMs:this.percentile(t,95),p99TaskMs:this.percentile(t,99),maxHeapRatio:Math.max(0,...e.workers.map(o=>o.lastHeapRatio||0))}}};var Tn=require("node:fs"),ii=require("node:path");var Mn=require("mongodb");Ue();function Ae(r){return r.type==="one-to-many"||r.isInverse===!0||r.type==="one-to-one"&&!!r.mappedBy}function H(r){return(r.type==="many-to-one"||r.type==="one-to-one")&&!Ae(r)&&(r.foreignKeyColumn||r.propertyName)||null}function W(r){return r.foreignKeyColumn||r.mappedBy||r.propertyName||null}function ae(r){if(!r||typeof r!="object")return r;if(r instanceof Mn.ObjectId)return r.toString();if(r instanceof Date)return r.toISOString();if(Array.isArray(r))return r.map(t=>ae(t));if("buffer"in r&&r.buffer&&typeof r.buffer=="object"&&Object.keys(r.buffer).length===12)try{let t=r.buffer,n=Object.keys(t).sort((o,i)=>parseInt(o)-parseInt(i)).map(o=>t[o]);return new Mn.ObjectId(Buffer.from(n)).toString()}catch{}let e={};for(let[t,n]of Object.entries(r))e[t]=n&&typeof n=="object"&&!(n instanceof Buffer)?ae(n):n;return e}function de(r,e){if(e.type!=="many-to-many")return null;let t=e.targetTableName||e.targetTable;if(!t)return null;if(e.mappedBy){let a=t,u=e.mappedBy,l=r,f=e.junctionTableName||xt(a,u,l);if(e.junctionSourceColumn&&e.junctionTargetColumn)return{junctionName:f,selfColumn:e.junctionSourceColumn,otherColumn:e.junctionTargetColumn,owningTable:a,inverseTable:l};let c=vt(a,u,l);return{junctionName:f,selfColumn:c.targetColumn,otherColumn:c.sourceColumn,owningTable:a,inverseTable:l}}let n=r,o=t,i=e.junctionTableName||xt(n,e.propertyName,o);if(e.junctionSourceColumn&&e.junctionTargetColumn)return{junctionName:i,selfColumn:e.junctionSourceColumn,otherColumn:e.junctionTargetColumn,owningTable:n,inverseTable:o};let s=vt(n,e.propertyName,o);return{junctionName:i,selfColumn:s.sourceColumn,otherColumn:s.targetColumn,owningTable:n,inverseTable:o}}async function It(r,e,t,n){if(!r)return{scalarFields:[],relations:[]};let o=r.tables?.get(e);if(!o)return{scalarFields:[],relations:[]};let i=new Map;for(let l of t)if(l==="*")i.has("")||i.set("",[]),i.get("").push(l);else if(l.includes(".")){let f=l.split("."),c=f[0],p=f.slice(1).join(".");i.has(c)||i.set(c,[]),i.get(c).push(p)}else o.relations?.some(c=>c.propertyName===l)?i.has(l)||i.set(l,["_id"]):(i.has("")||i.set("",[]),i.get("").push(l));if(n&&typeof n=="object")for(let[l,f]of Object.entries(n)){if(i.has(l))continue;let c=f&&typeof f=="object"&&f.fields!=null?Array.isArray(f.fields)?f.fields:String(f.fields).split(",").map(p=>p.trim()).filter(Boolean):["*"];i.set(l,c)}let s=[],a=[],u=i.get("")||[];for(let l of u)if(l==="*"){if(o.columns)for(let f of o.columns)s.includes(f.name)||s.push(f.name);if(o.relations)for(let f of o.relations)i.has(f.propertyName)||i.set(f.propertyName,["_id"])}else o.columns?.some(c=>c.name===l)&&!s.includes(l)&&s.push(l);for(let[l,f]of i.entries()){if(l==="")continue;let c=o.relations?.find(h=>h.propertyName===l);if(!c)continue;let p="",d="",g=!1;c.type==="many-to-one"||c.type==="one-to-one"?Ae(c)?(p="_id",d=W(c)||c.propertyName,g=!0):(p=H(c)||c.propertyName,d="_id",g=!1):c.type==="one-to-many"?(p="_id",d=W(c)||c.propertyName,g=!0):c.type==="many-to-many"&&(c.mappedBy?(p="_id",d=c.mappedBy,g=!0):(p=c.propertyName,d="_id",g=!1));let y=c.type==="one-to-many"||c.type==="many-to-many";a.push({propertyName:l,targetTable:c.targetTableName,localField:p,foreignField:d,type:y?"many":"one",isInverse:g,nestedFields:f})}return{scalarFields:s,relations:a}}var Zt=fe(require("fs")),On=fe(require("path"));var Wi=new RegExp("\\p{Mark}","gu");function xr(r){return r==null?"":String(r).normalize("NFKD").replace(Wi,"").toLowerCase()}function An(){let r={};for(let e=128;e<=1114111;e++)try{let t=String.fromCodePoint(e),n=xr(t);n!==t&&(r[t]=n)}catch{}return JSON.stringify(r)}function Xt(){return`function() {
|
|
7
7
|
var M = ${An()};
|
|
8
8
|
function foldStr(s) {
|
|
9
9
|
if (s == null) return '';
|