run_code = async ({
  row,
  table,
  channel,
  configuration: { code, run_where },
  user,
  ...rest
}) => {
  if (run_where === "Client page")
    return {
      eval_js: code,
      row,
      field_names: table ? table.fields.map((f) => f.name) : undefined,
    };
  if (!isNode()) {
    const { isOfflineMode } = getState().mobileConfig;
    if (!isOfflineMode && run_where === "Server") {
      // stop on the app and run the action server side
      return { server_eval: true };
    }
  }
  const Actions = {};
  Object.entries(getState().actions).forEach(([k, v]) => {
    Actions[k] = (args = {}) => {
      return v.run({ row, table, user, configuration: args, ...rest, ...args });
    };
  });
  const trigger_actions = await Trigger.find({
    when_trigger: { or: ["API call", "Never"] },
  });
  for (const trigger of trigger_actions) {
    const state_action = getState().actions[trigger.action];
    Actions[trigger.name] = (args = {}) => {
      return state_action.run({
        row,
        table,
        configuration: trigger.configuration,
        user,
        ...rest,
        ...args,
      });
    };
  }
  const run_js_code = async ({ code, ...restArgs }) => {
    return await run_code({
      row,
      table,
      channel,
      configuration: { code, run_where },
      user,
      ...rest,
      ...restArgs,
    });
  };
  const emitEvent = (eventType, channel, payload) =>
    Trigger.emitEvent(eventType, channel, user, payload);
  const fetchJSON = async (...args) => await (await fetch(...args)).json();
  const sysState = getState();
  const require = (nm) => sysState.codeNPMmodules[nm];
  const f = vm.runInNewContext(`async () => {${code}\n}`, {
    Table,
    table,
    row,
    context: row,
    user,
    console: consoleInterceptor(getState()),
    Actions,
    emitEvent,
    sleep,
    fetchJSON,
    fetch,
    run_js_code,
    tryCatchInTransaction: db.tryCatchInTransaction,
    URL,
    File,
    User,
    View,
    EventLog,
    Buffer: isNode() ? Buffer : require("buffer"),
    Trigger,
    Notification,
    setTimeout,
    interpolate,
    require,
    setConfig: (k, v) =>
      sysState.isFixedConfig(k) ? undefined : sysState.setConfig(k, v),
    getConfig: (k) =>
      sysState.isFixedConfig(k) ? undefined : sysState.getConfig(k),
    channel: table ? table.name : channel,
    session_id: rest.req && getSessionId(rest.req),
    request_headers: rest?.req?.headers,
    request_ip: rest?.req?.ip,
    ...(row || {}),
    ...getState().eval_context,
    ...rest,
  });
  return await f();
}
@param opts
@param opts.row
@param opts.table
@param opts.channel
@param opts.configuration
@param opts.user
@param opts.rest
@returns