@absolutejs/sync 1.7.3 → 1.7.4

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.
@@ -1093,7 +1093,7 @@ var loadIsolatedJsc = async () => {
1093
1093
  }
1094
1094
  };
1095
1095
  var wrap = (source) => `
1096
- (() => {
1096
+ function (args, ctx, __dispatch) {
1097
1097
  const userFn = (${source});
1098
1098
  if (typeof userFn !== 'function') {
1099
1099
  throw new Error(
@@ -1102,55 +1102,26 @@ var wrap = (source) => `
1102
1102
  );
1103
1103
  }
1104
1104
  const actions = {
1105
- insert: (table, data) => __syncAction('insert', table, data),
1106
- update: (table, data) => __syncAction('update', table, data),
1107
- delete: (table, row) => __syncAction('delete', table, row),
1108
- change: (collection, change) => __syncAction('change', collection, change)
1105
+ insert: (table, data) => __dispatch('insert', table, data),
1106
+ update: (table, data) => __dispatch('update', table, data),
1107
+ delete: (table, row) => __dispatch('delete', table, row),
1108
+ change: (collection, change) => __dispatch('change', collection, change)
1109
1109
  };
1110
1110
  return userFn(args, ctx, actions);
1111
- })()
1111
+ }
1112
1112
  `;
1113
- var DEFAULT_RECYCLE_CONTEXT_AFTER = 256;
1114
- var installRouter = async (context, currentActions, Reference) => {
1115
- const router = new Reference((op, ...rest) => {
1116
- const a = currentActions.value;
1117
- if (a === undefined) {
1118
- throw new Error("__syncAction invoked outside an active sandboxed call (shared-slot router)");
1119
- }
1120
- switch (op) {
1121
- case "insert":
1122
- return a.insert(rest[0], rest[1]);
1123
- case "update":
1124
- return a.update(rest[0], rest[1]);
1125
- case "delete":
1126
- return a.delete(rest[0], rest[1]);
1127
- case "change":
1128
- return a.change(rest[0], rest[1]);
1129
- default:
1130
- throw new Error(`unknown sandbox action op: ${String(op)}`);
1131
- }
1132
- });
1133
- await context.setGlobal("__syncAction", router);
1134
- };
1135
1113
  var compile = async (source, config) => {
1136
- const { createIsolate, Reference } = await loadIsolatedJsc();
1114
+ const { createIsolate } = await loadIsolatedJsc();
1137
1115
  const isolate = await createIsolate({
1138
1116
  backend: config.backend ?? "auto",
1139
1117
  memoryLimit: config.memoryLimit ?? 32
1140
1118
  });
1141
- const script = await isolate.compileScript(wrap(source));
1142
1119
  const context = await isolate.createContext();
1143
- const currentActions = {
1144
- value: undefined
1145
- };
1146
- await installRouter(context, currentActions, Reference);
1120
+ const callable = await context.compileCallable(wrap(source));
1147
1121
  return {
1122
+ callable,
1148
1123
  context,
1149
- currentActions,
1150
1124
  isolate,
1151
- runQueue: Promise.resolve(undefined),
1152
- script,
1153
- servedCalls: 0,
1154
1125
  timeoutMs: config.timeout ?? 5000
1155
1126
  };
1156
1127
  };
@@ -1166,37 +1137,26 @@ var makeSandboxedHandler = (source, config = {}) => {
1166
1137
  pending = compile(source, config);
1167
1138
  return pending;
1168
1139
  };
1169
- const recycleContextIfNeeded = async (compiled) => {
1170
- if (compiled.servedCalls < DEFAULT_RECYCLE_CONTEXT_AFTER)
1171
- return;
1172
- const { Reference } = await loadIsolatedJsc();
1173
- await compiled.context.dispose().catch(() => {});
1174
- compiled.context = await compiled.isolate.createContext();
1175
- await installRouter(compiled.context, compiled.currentActions, Reference);
1176
- compiled.servedCalls = 0;
1177
- };
1178
1140
  return async (args, ctx, actions) => {
1141
+ const { Reference } = await loadIsolatedJsc();
1179
1142
  const compiled = await getCompiled();
1180
- const prev = compiled.runQueue;
1181
- const turn = prev.then(async () => {
1182
- await recycleContextIfNeeded(compiled);
1183
- compiled.currentActions.value = actions;
1184
- try {
1185
- await compiled.context.setGlobal("args", args);
1186
- await compiled.context.setGlobal("ctx", ctx);
1187
- const result = await compiled.script.run(compiled.context, {
1188
- timeout: compiled.timeoutMs
1189
- });
1190
- return result;
1191
- } finally {
1192
- compiled.currentActions.value = undefined;
1193
- compiled.servedCalls += 1;
1143
+ const dispatch = new Reference((op, ...rest) => {
1144
+ switch (op) {
1145
+ case "insert":
1146
+ return actions.insert(rest[0], rest[1]);
1147
+ case "update":
1148
+ return actions.update(rest[0], rest[1]);
1149
+ case "delete":
1150
+ return actions.delete(rest[0], rest[1]);
1151
+ case "change":
1152
+ return actions.change(rest[0], rest[1]);
1153
+ default:
1154
+ throw new Error(`unknown sandbox action op: ${String(op)}`);
1194
1155
  }
1195
1156
  });
1196
- compiled.runQueue = turn.catch(() => {
1197
- return;
1157
+ return compiled.callable.call([args, ctx, dispatch], {
1158
+ timeout: compiled.timeoutMs
1198
1159
  });
1199
- return turn;
1200
1160
  };
1201
1161
  };
1202
1162
 
@@ -2811,5 +2771,5 @@ export {
2811
2771
  CdcConsumerSlowError
2812
2772
  };
2813
2773
 
2814
- //# debugId=7E100BFB331BB7CE64756E2164756E21
2774
+ //# debugId=213CD32363E7988D64756E2164756E21
2815
2775
  //# sourceMappingURL=index.js.map