@absolutejs/sync 1.8.0 → 1.8.1

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.
@@ -1127,12 +1127,7 @@ var wrap = (source) => `
1127
1127
  }
1128
1128
  `;
1129
1129
  var compile = async (source, config, bridgeFetch) => {
1130
- const { createIsolate, Reference } = await loadIsolatedJsc();
1131
- const isolate = await createIsolate({
1132
- backend: config.backend ?? "auto",
1133
- memoryLimit: config.memoryLimit ?? 32
1134
- });
1135
- const context = await isolate.createContext();
1130
+ const { Reference, createIsolatedRunner, resolveIsolatePolicy } = await loadIsolatedJsc();
1136
1131
  const callMap = new Map;
1137
1132
  const dispatch = new Reference((callId, op, ...rest) => {
1138
1133
  const a = callMap.get(callId);
@@ -1156,15 +1151,25 @@ var compile = async (source, config, bridgeFetch) => {
1156
1151
  throw new Error(`unknown sandbox action op: ${String(op)}`);
1157
1152
  }
1158
1153
  });
1159
- await context.setGlobal("__dispatch", dispatch);
1160
- const callable = await context.compileCallable(wrap(source));
1154
+ const timeoutMs = config.timeout ?? 5000;
1155
+ const sourceToCall = wrap(source);
1156
+ const policy = resolveIsolatePolicy("tenant-script", {
1157
+ allowWorkerFallback: true,
1158
+ backend: config.backend ?? "auto",
1159
+ memoryLimit: config.memoryLimit ?? 32,
1160
+ timeout: timeoutMs
1161
+ });
1162
+ const runner = createIsolatedRunner({
1163
+ globals: { __dispatch: dispatch },
1164
+ policy
1165
+ });
1166
+ await runner.precompile("sandboxedHandler", sourceToCall);
1161
1167
  return {
1162
- callable,
1163
1168
  callMap,
1164
- context,
1165
- isolate,
1166
1169
  nextCallId: 1,
1167
- timeoutMs: config.timeout ?? 5000
1170
+ runner,
1171
+ source: sourceToCall,
1172
+ timeoutMs
1168
1173
  };
1169
1174
  };
1170
1175
  var runBridgeFetch = async (config, url, init) => {
@@ -1220,10 +1225,7 @@ var makeSandboxedHandler = (source, config = {}, engineExtras) => {
1220
1225
  const bridgeFetch = engineExtras?.bridgeFetch;
1221
1226
  const getCompiled = async () => {
1222
1227
  if (pending !== undefined) {
1223
- const compiled = await pending;
1224
- if (!compiled.isolate.isDisposed)
1225
- return compiled;
1226
- pending = undefined;
1228
+ return pending;
1227
1229
  }
1228
1230
  pending = compile(source, config, bridgeFetch);
1229
1231
  return pending;
@@ -1234,9 +1236,13 @@ var makeSandboxedHandler = (source, config = {}, engineExtras) => {
1234
1236
  compiled.callMap.set(callId, actions);
1235
1237
  if (metricsHook === undefined) {
1236
1238
  try {
1237
- return await compiled.callable.call([callId, args, ctx], {
1238
- timeout: compiled.timeoutMs
1239
- });
1239
+ return await compiled.runner.call("sandboxedHandler", compiled.source, [callId, args, ctx], { run: { timeout: compiled.timeoutMs } });
1240
+ } catch (error) {
1241
+ if (isIsolateDisposalError(error)) {
1242
+ pending = undefined;
1243
+ await disposeCompiled(compiled);
1244
+ }
1245
+ throw error;
1240
1246
  } finally {
1241
1247
  compiled.callMap.delete(callId);
1242
1248
  }
@@ -1244,8 +1250,9 @@ var makeSandboxedHandler = (source, config = {}, engineExtras) => {
1244
1250
  const startedAt = performance.now();
1245
1251
  const id = makeRandomId();
1246
1252
  try {
1247
- const { result, metrics } = await compiled.callable.callWithMetrics([callId, args, ctx], { timeout: compiled.timeoutMs });
1253
+ const { result, metrics } = await compiled.runner.call("sandboxedHandler", compiled.source, [callId, args, ctx], { run: { timeout: compiled.timeoutMs }, withMetrics: true });
1248
1254
  fireMetrics(metricsHook.onMetrics, {
1255
+ backend: metrics.backend,
1249
1256
  cpuMs: metrics.cpuMs,
1250
1257
  durationMs: performance.now() - startedAt,
1251
1258
  heapBytes: metrics.heapBytes,
@@ -1256,6 +1263,10 @@ var makeSandboxedHandler = (source, config = {}, engineExtras) => {
1256
1263
  });
1257
1264
  return result;
1258
1265
  } catch (error) {
1266
+ if (isIsolateDisposalError(error)) {
1267
+ pending = undefined;
1268
+ await disposeCompiled(compiled);
1269
+ }
1259
1270
  fireMetrics(metricsHook.onMetrics, {
1260
1271
  cpuMs: 0,
1261
1272
  durationMs: performance.now() - startedAt,
@@ -1274,6 +1285,12 @@ var makeSandboxedHandler = (source, config = {}, engineExtras) => {
1274
1285
  };
1275
1286
  };
1276
1287
  var makeRandomId = () => `hm_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
1288
+ var isIsolateDisposalError = (error) => error instanceof Error && (error.name === "TimeoutError" || error.name === "MemoryLimitError" || error.name === "IsolateDisposedError");
1289
+ var disposeCompiled = async (compiled) => {
1290
+ try {
1291
+ await compiled.runner.dispose();
1292
+ } catch {}
1293
+ };
1277
1294
  var fireMetrics = (hook, record) => {
1278
1295
  let outcome;
1279
1296
  try {
@@ -2904,5 +2921,5 @@ export {
2904
2921
  CdcConsumerSlowError
2905
2922
  };
2906
2923
 
2907
- //# debugId=783626175939945A64756E2164756E21
2924
+ //# debugId=2984A33F7845F42C64756E2164756E21
2908
2925
  //# sourceMappingURL=index.js.map