@absolutejs/sync 1.7.4 → 1.7.5

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
- function (args, ctx, __dispatch) {
1096
+ function (__callId, args, ctx) {
1097
1097
  const userFn = (${source});
1098
1098
  if (typeof userFn !== 'function') {
1099
1099
  throw new Error(
@@ -1102,26 +1102,48 @@ var wrap = (source) => `
1102
1102
  );
1103
1103
  }
1104
1104
  const actions = {
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)
1105
+ insert: (table, data) => __dispatch(__callId, 'insert', table, data),
1106
+ update: (table, data) => __dispatch(__callId, 'update', table, data),
1107
+ delete: (table, row) => __dispatch(__callId, 'delete', table, row),
1108
+ change: (collection, change) => __dispatch(__callId, 'change', collection, change)
1109
1109
  };
1110
1110
  return userFn(args, ctx, actions);
1111
1111
  }
1112
1112
  `;
1113
1113
  var compile = async (source, config) => {
1114
- const { createIsolate } = await loadIsolatedJsc();
1114
+ const { createIsolate, Reference } = await loadIsolatedJsc();
1115
1115
  const isolate = await createIsolate({
1116
1116
  backend: config.backend ?? "auto",
1117
1117
  memoryLimit: config.memoryLimit ?? 32
1118
1118
  });
1119
1119
  const context = await isolate.createContext();
1120
+ const callMap = new Map;
1121
+ const dispatch = new Reference((callId, op, ...rest) => {
1122
+ const a = callMap.get(callId);
1123
+ if (a === undefined) {
1124
+ throw new Error(`__dispatch invoked for orphan callId ${String(callId)}`);
1125
+ }
1126
+ switch (op) {
1127
+ case "insert":
1128
+ return a.insert(rest[0], rest[1]);
1129
+ case "update":
1130
+ return a.update(rest[0], rest[1]);
1131
+ case "delete":
1132
+ return a.delete(rest[0], rest[1]);
1133
+ case "change":
1134
+ return a.change(rest[0], rest[1]);
1135
+ default:
1136
+ throw new Error(`unknown sandbox action op: ${String(op)}`);
1137
+ }
1138
+ });
1139
+ await context.setGlobal("__dispatch", dispatch);
1120
1140
  const callable = await context.compileCallable(wrap(source));
1121
1141
  return {
1122
1142
  callable,
1143
+ callMap,
1123
1144
  context,
1124
1145
  isolate,
1146
+ nextCallId: 1,
1125
1147
  timeoutMs: config.timeout ?? 5000
1126
1148
  };
1127
1149
  };
@@ -1138,25 +1160,16 @@ var makeSandboxedHandler = (source, config = {}) => {
1138
1160
  return pending;
1139
1161
  };
1140
1162
  return async (args, ctx, actions) => {
1141
- const { Reference } = await loadIsolatedJsc();
1142
1163
  const compiled = await getCompiled();
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)}`);
1155
- }
1156
- });
1157
- return compiled.callable.call([args, ctx, dispatch], {
1158
- timeout: compiled.timeoutMs
1159
- });
1164
+ const callId = compiled.nextCallId++;
1165
+ compiled.callMap.set(callId, actions);
1166
+ try {
1167
+ return await compiled.callable.call([callId, args, ctx], {
1168
+ timeout: compiled.timeoutMs
1169
+ });
1170
+ } finally {
1171
+ compiled.callMap.delete(callId);
1172
+ }
1160
1173
  };
1161
1174
  };
1162
1175
 
@@ -2771,5 +2784,5 @@ export {
2771
2784
  CdcConsumerSlowError
2772
2785
  };
2773
2786
 
2774
- //# debugId=213CD32363E7988D64756E2164756E21
2787
+ //# debugId=8D6F90BB348646A664756E2164756E21
2775
2788
  //# sourceMappingURL=index.js.map